KaliVeda
Toolkit for HIC analysis
KVExpSetUpDB.cpp
1 //Created by KVClassFactory on Fri Jul 20 15:49:04 2018
2 //Author: eindra
3 
4 #include "KVExpSetUpDB.h"
5 #include "KVDataSet.h"
6 #include "KVMultiDetArray.h"
7 
8 #include <TKey.h>
9 
11 
12 
13 
17  : KVExpDB()
18 {
19  // Default constructor
20 }
21 
22 
23 
24 
27 
29  : KVExpDB(name)
30 {
31  // Constructor inherited from KVExpDB
32 }
33 
34 
35 
36 
39 
40 KVExpSetUpDB::KVExpSetUpDB(const Char_t* name, const Char_t* title)
41  : KVExpDB(name, title)
42 {
43  // Constructor inherited from KVExpDB
44 }
45 
46 
47 
48 
51 
53 {
54  // Destructor
55 }
56 
57 
58 
70 
71 void KVExpSetUpDB::Build(bool minimal)
72 {
73  // \param[in] opt if =true, only a minimal database with runs/systems info is built
74  //
75  // Build the database.
76  // Runs & Systems tables are handled by us, calibrations are handled by each multidetector
77  //
78  // If ParameterFiles.dat file exists for dataset, we read any files it lists and
79  // add the corresponding parameter values to the appropriate KVDBRunFile objects
80  //
81  // If no run infos are available (perhaps no data yet available for dataset), we make a dummy
82  // database with runs numbered from 1 to 100.
83 
84  try {
85  FillRunsTable();
86  }
87  catch (std::runtime_error& rte) {
88 
89  // an exception may be thrown if a 'bad' runfile is not found,
90  // which indicates an inconsistency in bad_runfiles.dat
91  if (TString(rte.what()) != "run infos file not found") throw;
92 
93  Info("Build", "No informations on dataset runs available");
94  Info("Build", "Dummy database runs numbered from 1 to 100 will be created");
95  for (int i = 1; i <= 100; ++i) {
96  AddRun(new KVDBRun(i, "Dummy database run"));
97  }
98  }
99 
100  ReadComments();
101  ReadSystemList();
102 
103  if(minimal)
104  {
105  // minimal DB has only run/system infos
106  minimal_db = true;
107  return;
108  }
109 
110  ReadScalerInfos();
112 
113  KVMultiDetArray::MakeMultiDetector(fDataSet, -1, "KVMultiDetArray", this);
114  gMultiDetArray->MakeCalibrationTables(this);
115 }
116 
117 
118 
122 
124 {
125  // Look for file scalers.root and read scalers from it
126  // scalers are assumed to be stored as 64-bit parameters in the list
127 
128  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "scalers.root");
129  if (runinfos == "") return;
130 
131  Info("ReadScalerInfos", "Reading scaler infos from %s", runinfos.Data());
132  TFile runinfos_file(runinfos);
133  TIter it_run(GetRuns());
134  KVDBRun* run;
135  while ((run = (KVDBRun*)it_run())) {
136  auto file_list = run->GetRunIndexList();
137  for (auto& rf : file_list) {
138  auto scalist = (KVNameValueList*)runinfos_file.Get(Form("run%s", rf.as_string().Data()));
139  if (!scalist) scalist = (KVNameValueList*)runinfos_file.Get(Form("run_%06d", run->GetNumber() * index_multiplier));
140  if (scalist) {
141  int npar = scalist->GetNpar();
142  for (int i = 0; i < npar; i += 2) {
143  TString parname = scalist->GetParameter(i)->GetName();
144  parname.Remove(parname.Length() - 3, 3);
145  run->GetRunFile(rf).SetScaler64(parname, scalist->GetValue64bit(parname));
146  }
147  }
148  }
149  }
150 }
151 
152 
153 
157 
159 {
160  // If a file ParameterFiles.dat is present in the dataset files, we read all files
161  // listed in it and add the corresponding parameters to the KVDBRunFile for each runfile.
162 
163  TString filename = "ParameterFiles.dat";
165  KVFileReader fr;
167  Info("ReadParameterFiles", "Using file %s", path.Data());
168  fr.OpenFileToRead(path);
169  while (fr.IsOK()) {
170  fr.ReadLine(0);
171 
172  if (fr.GetCurrentLine() != "" && !fr.GetCurrentLine().BeginsWith("#"))
174  }
175  fr.CloseFile();
176  }
177 }
178 
179 
180 
198 
200 {
201  // Read parameter values from filename and attribute them to each KVDBRunFile
202  //
203  // Format of file:
204  //~~~
205  //Parameter.Name: [name]
206  //Parameter.Type: [double or string]
207  //[Parameter.Units: [units]]
208  //
209  //[runfilelist1]: [value1]
210  //[runfilelist2]: [value2]
211  //~~~
212  //
213  // The `Parameter.Units` is optional. If given, an additional parameter,
214  // `[name].Units` will be added to each KVDBRunFile.
215  //
216  // The lists of runfiles use the syntax detailed in KVExpDB::SetRunIndexListFromString().
217 
218  TString fullpath = "";
219  if (!KVBase::SearchKVFile(filename, fullpath, fDataSet)) {
220  Info("LoadParameters", "%s does not exist or not found", filename.Data());
221  return;
222  }
223 
224  Info("LoadParameters", "file : %s found", fullpath.Data());
225  TEnv env;
226  env.ReadFile(fullpath, kEnvAll);
227 
228  KVString parname = env.GetValue("Parameter.Name", "toto");
229  KVString partype = env.GetValue("Parameter.Type", "Double_t");
230  KVString parunits = env.GetValue("Parameter.Units", "");
231 
232  partype.ToUpper();
233  if (partype.Contains("DOUBLE"))
234  partype = "D";
235  else
236  partype = "S";
237 
238  TIter next(env.GetTable());
239  TEnvRec* rec = 0;
240 
241  while ((rec = (TEnvRec*)next())) {
242 
243  KVString sname(rec->GetName());
244  if (sname.BeginsWith("Parameter")) continue;
245 
246  auto runlist = SetRunIndexListFromString(sname);
247 
248  KVString parval(rec->GetValue());
249 
250  for (auto& run : runlist) {
251  if (partype == "D")
252  GetDBRunFile(run).Set(parname, parval.Atof());
253  else
254  GetDBRunFile(run).Set(parname, parval);
255  if (parunits != "")
256  GetDBRunFile(run).Set(Form("%s.Units", parname.Data()), parunits);
257  }
258  }
259 }
260 
261 
262 
282 
284 {
285  // Fill the Runs table using the informations in file runinfos.root
286  // (which can be generated using KVRunListCreator).
287  //
288  // If there are no run infos available (perhaps because no data yet exists for the dataset),
289  // this method throws an exception and the database will have a dummy list of runs
290  // numbered from 1 to 100
291  //
292  // Any runfiles which are bad i.e. not to be considered for further analysis should
293  // be written in a file bad_runfiles.dat with the given format (TEnv):
294  // - comment lines begin with '#'
295  // - BadFiles.First: first runfile of each run in list is bad
296  // - BadFiles.All: all runfiles for each run in list are bad
297  // - BadFiles.Singles: individual runfiles given as run.index are bad
298  //~~~
299  //BadFiles.First: 17-26 30 35-42 44 52-54 57-63 65-73 76 83-111 123-141
300  //BadFiles.All: 16 27-29 43 46 51 55 56 64 74 75 110 112-122 138
301  //BadFiles.Singles: 26.101 86.8 108.21 109.46 111.31 137.9
302  //~~~
303 
304  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "runinfos.root");
305 
306  if (runinfos == "")
307  throw std::runtime_error("run infos file not found");
308 
309  int run_num{-1}, runfile_index{-1};
310 
311  Info("FillRunsTable", "Reading run infos from %s", runinfos.Data());
312  TFile runinfos_file(runinfos);
313  TIter it(runinfos_file.GetListOfKeys());
314  TKey* run_key;
315  KVList garbage;
316  run_index_t run_index;
317  while ((run_key = (TKey*)it())) {
318  if (TString(run_key->GetClassName()) == "KVNameValueList") {
319  // make sure we only use the highest cycle number of each key
320  if (run_key->GetCycle() == runinfos_file.GetKey(run_key->GetName())->GetCycle()) {
321  KVNameValueList* run = (KVNameValueList*)run_key->ReadObj();
322  garbage.Add(run);
323  if (with_index_multiplier) {
324  run_num = run->GetIntValue("Run") / index_multiplier;
325  runfile_index = run->GetIntValue("Run") % index_multiplier;
326  if (!runfile_index) run_index = run_index_t{run_num, std::nullopt};
327  else run_index = run_index_t{run_num, runfile_index};
328  }
329  else if (HasRawFilesWithIndex()) {
330  run_num = run->GetIntValue("Run");
331  KVDBRun* dbrun = GetDBRun(run_num);
332  if (!dbrun) {
333  dbrun = new KVDBRun;
334  dbrun->SetNumber(run_num);
335  AddRun(dbrun);
336  }
337  KVNumberList index_list(run->GetStringValue("IndexList"));
338  for (auto index : index_list) {
339  run_index = index ? run_index_t{run_num, index} :
340  run_index_t{run_num, std::nullopt};
341  auto runfile = new KVDBRunFile("", run_index);
342  runfile->SetStartDate(run->GetStringValue(Form("Start.%d", index)));
343  runfile->SetEndDate(run->GetStringValue(Form("End.%d", index)));
344  if (run->HasValue64bit(Form("Size.%d", index)))
345  runfile->SetSize(run->GetValue64bit(Form("Size.%d", index)) / 1024. / 1024.);
346  else
347  runfile->SetSize(run->GetIntValue(Form("Size.%d", index)) / 1024. / 1024.);
348 
349  if (run->HasValue64bit(Form("Events.%d", index))) {
350  runfile->SetEvents(run->GetValue64bit(Form("Events.%d", index)));
351  }
352  else
353  runfile->SetEvents(run->GetIntValue(Form("Events.%d", index)));
354  dbrun->AddRunFile(runfile);
355  }
356  continue;
357  }
358  else {
359  run_num = run->GetIntValue("Run");
360  run_index = run_index_t{run_num, std::nullopt};
361  }
362  KVDBRun* dbrun = GetDBRun(run_num);
363  if (!dbrun) {
364  dbrun = new KVDBRun;
365  dbrun->SetNumber(run_num);
366  AddRun(dbrun);
367  }
368  auto runfile = new KVDBRunFile("", run_index);
369  runfile->SetStartDate(run->GetStringValue("Start"));
370  runfile->SetEndDate(run->GetStringValue("End"));
371  if (run->HasValue64bit("Size"))
372  runfile->SetSize(run->GetValue64bit("Size") / 1024. / 1024.);
373  else
374  runfile->SetSize(run->GetIntValue("Size") / 1024. / 1024.);
375 
376  if (run->HasValue64bit("Events")) {
377  runfile->SetEvents(run->GetValue64bit("Events"));
378  }
379  else
380  runfile->SetEvents(run->GetIntValue("Events"));
381  dbrun->AddRunFile(runfile);
382  }
383  }
384  }
385  TString badinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "bad_runfiles.dat");
386  if (!badinfos.IsNull()) {
387  Info("FillRunsTable", "Reading bad runfile infos from %s", badinfos.Data());
388  TEnv badfiles;
389  badfiles.ReadFile(badinfos, kEnvLocal);
390  KVNumberList all(badfiles.GetValue("BadFiles.All", ""));
391  all.Begin();
392  while (!all.End()) {
393  auto br = all.Next();
394  auto dbrun = GetDBRun(br);
395  if (!dbrun) {
396  Error("FillRunsTable", "%s:BadFiles.All contains run number %d which is not in %s",
397  badinfos.Data(), br, runinfos.Data());
398  throw std::runtime_error(Form("inconsistency between %s:BadFiles.All and %s",
399  badinfos.Data(), runinfos.Data()));
400  }
401  dbrun->SetBadAllRunFiles();
402  }
403  KVNumberList first(badfiles.GetValue("BadFiles.First", ""));
404  first.Begin();
405  while (!first.End()) {
406  auto br = first.Next();
407  auto dbrun = GetDBRun(br);
408  if (!dbrun) {
409  Error("FillRunsTable", "%s:BadFiles.First contains run number %d which is not in %s",
410  badinfos.Data(), br, runinfos.Data());
411  throw std::runtime_error(Form("inconsistency between %s:BadFiles.First and %s",
412  badinfos.Data(), runinfos.Data()));
413  }
414  dbrun->GetFirstFile().SetBad();
415  }
416  run_index_list singles(badfiles.GetValue("BadFiles.Singles", ""));
417  for (auto& ri : singles) {
418  try {
419  GetDBRunFile(ri).SetBad();
420  }
421  catch (std::exception& e) {
422  Error("FillRunsTable", "%s:BadFiles.Singles contains run_index %s which is unknown",
423  badinfos.Data(), ri.as_string().Data());
424  throw;
425  }
426  }
427  }
428  // now set global runfile index for all good files
429  Int_t global_runfile_number = 0;
430  for (auto run : fListOfRuns) {
431  auto r = GetDBRun(run);
432  auto ril = r->GetRunIndexList();
433  for (auto& ri : ril) {
434  if (!GetDBRunFile(ri).IsBad()) {
435  GetDBRunFile(ri).SetGlobalRunFileNumber(++global_runfile_number);
436  fMapGlobRunFileNumberToRunIndex[global_runfile_number] = ri;
437  }
438  }
439  }
440 }
441 
442 
443 //____________________________________________________________________________//
444 
445 
int Int_t
ROOT::R::TRInterface & r
#define e(i)
char Char_t
kEnvAll
kEnvLocal
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t index
char name[80]
char * Form(const char *fmt,...)
static Bool_t SearchKVFile(const Char_t *name, TString &fullpath, const Char_t *kvsubdir="")
Definition: KVBase.cpp:533
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:73
A single raw data file associated with an experimental run ,,.
Definition: KVDBRunFile.h:39
void SetScaler64(const Char_t *name, ULong64_t val)
Set value for 64-bit scaler with the given name for this run.
Definition: KVDBRunFile.h:99
void Set(const Char_t *param, Double_t val)
Set numerical (non-scaler) characteristic of run.
Definition: KVDBRunFile.h:110
void SetGlobalRunFileNumber(Int_t r)
Definition: KVDBRunFile.h:51
void SetBad(Bool_t is_bad=kTRUE)
Definition: KVDBRunFile.h:230
Description of an experimental run in database ,,.
Definition: KVDBRun.h:41
void AddRunFile(KVDBRunFile *rf)
Definition: KVDBRun.h:91
void SetNumber(Int_t n) override
Definition: KVDBRun.h:192
const run_index_list & GetRunIndexList() const
Definition: KVDBRun.h:101
KVDBRunFile & GetRunFile(const run_index_t &run_index)
Definition: KVDBRun.h:73
TString GetFullPathToDataSetFile(const Char_t *filename) const
Definition: KVDataSet.cpp:2049
static Bool_t FindDataSetFile(const TString &dataset, const Char_t *filename)
Definition: KVDataSet.cpp:2083
Base class to describe database of an experiment ,,.
Definition: KVExpDB.h:61
KVNumberList fListOfRuns
list of all run numbers
Definition: KVExpDB.h:68
std::map< int, run_index_t > fMapGlobRunFileNumberToRunIndex
cannot be saved, no streamer for run_index_t
Definition: KVExpDB.h:74
run_index_list SetRunIndexListFromString(const TString &) const
Definition: KVExpDB.cpp:733
virtual void ReadComments()
Definition: KVExpDB.cpp:844
virtual void ReadSystemList()
Definition: KVExpDB.cpp:248
void AddRun(KVDBRun *r)
Definition: KVExpDB.h:123
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:132
virtual KVSeqCollection * GetRuns() const
Definition: KVExpDB.h:128
bool with_index_multiplier
Definition: KVExpDB.h:72
const KVDBRunFile & GetDBRunFile(const run_index_t &r) const
Definition: KVExpDB.h:136
int index_multiplier
Definition: KVExpDB.h:71
Bool_t HasRawFilesWithIndex() const
Definition: KVExpDB.h:221
bool minimal_db
Definition: KVExpDB.h:75
TString fDataSet
the name of the dataset to which this database is associated
Definition: KVExpDB.h:64
Calibration database for experiments using coupled detector arrays.
Definition: KVExpSetUpDB.h:17
void FillRunsTable()
void LoadParameters(const KVString &)
KVExpSetUpDB()
Default constructor.
void ReadParameterFiles()
void Build(bool minimal=false) override
virtual ~KVExpSetUpDB()
Destructor.
void ReadScalerInfos()
Handle reading columns of numeric data in text files.
Definition: KVFileReader.h:121
KVString GetCurrentLine()
Definition: KVFileReader.h:320
void CloseFile()
Definition: KVFileReader.h:237
ReadStatus ReadLine(const KVString &pattern="")
Definition: KVFileReader.h:243
Bool_t IsOK()
Definition: KVFileReader.h:231
Bool_t OpenFileToRead(const KVString &filename)
Definition: KVFileReader.h:210
Extended TList class which owns its objects by default.
Definition: KVList.h:22
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray", KVExpDB *db=nullptr)
virtual void MakeCalibrationTables(KVExpDB *)
Handles lists of named parameters with different types, a list of KVNamedParameter objects.
Int_t GetIntValue(const Char_t *name) const
Bool_t HasValue64bit(const Char_t *name) const
ULong64_t GetValue64bit(const Char_t *name) const
const Char_t * GetStringValue(const Char_t *name) const
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:85
Bool_t End(void) const
Definition: KVNumberList.h:199
void Begin(void) const
Int_t Next(void) const
void Add(TObject *obj) override
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
T * Get(const char *namecycle)
TKey * GetKey(const char *name, Short_t cycle=9999) const override
TList * GetListOfKeys() const override
THashList * GetTable() const
virtual const char * GetValue(const char *name, const char *dflt) const
virtual Int_t ReadFile(const char *fname, EEnvLevel level)
virtual const char * GetClassName() const
Short_t GetCycle() const
virtual TObject * ReadObj()
const char * GetName() const override
virtual void Error(const char *method, const char *msgfmt,...) const
virtual void Info(const char *method, const char *msgfmt,...) const
Ssiz_t Length() const
Double_t Atof() const
const char * Data() const
void ToUpper()
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Bool_t IsNull() const
TString & Remove(EStripType s, char c)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
List of runfiles specified by run number and file index ,.
Specifies a runfile according to run number and file index ,.
Definition: run_index.h:31
rec
ClassImp(TPyArg)