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 
51 
53 {
54  // Look for file scalers.root and read scalers from it
55  // scalers are assumed to be stored as 64-bit parameters in the list
56 
57  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "scalers.root");
58  if (runinfos == "") return;
59 
60  Info("ReadScalerInfos", "Reading scaler infos from %s", runinfos.Data());
61  TFile runinfos_file(runinfos);
62  TIter it_run(GetRuns());
63  KVDBRun* run;
64  while ((run = (KVDBRun*)it_run())) {
65  auto file_list = run->GetRunIndexList();
66  for (auto& rf : file_list) {
67  auto scalist = (KVNameValueList*)runinfos_file.Get(Form("run%s", rf.as_string().Data()));
68  if (!scalist) scalist = (KVNameValueList*)runinfos_file.Get(Form("run_%06d", run->GetNumber() * index_multiplier));
69  if (scalist) {
70  int npar = scalist->GetNpar();
71  for (int i = 0; i < npar; i += 2) {
72  TString parname = scalist->GetParameter(i)->GetName();
73  parname.Remove(parname.Length() - 3, 3);
74  run->GetRunFile(rf).SetScaler64(parname, scalist->GetValue64bit(parname));
75  }
76  }
77  }
78  }
79 }
80 
81 
82 
86 
88 {
89  // If a file ParameterFiles.dat is present in the dataset files, we read all files
90  // listed in it and add the corresponding parameters to the KVDBRunFile for each runfile.
91 
92  TString filename = "ParameterFiles.dat";
94  KVFileReader fr;
96  Info("ReadParameterFiles", "Using file %s", path.Data());
97  fr.OpenFileToRead(path);
98  while (fr.IsOK()) {
99  fr.ReadLine(0);
100 
101  if (fr.GetCurrentLine() != "" && !fr.GetCurrentLine().BeginsWith("#"))
103  }
104  fr.CloseFile();
105  }
106 }
107 
108 
109 
127 
129 {
130  // Read parameter values from filename and attribute them to each KVDBRunFile
131  //
132  // Format of file:
133  //~~~
134  //Parameter.Name: [name]
135  //Parameter.Type: [double or string]
136  //[Parameter.Units: [units]]
137  //
138  //[runfilelist1]: [value1]
139  //[runfilelist2]: [value2]
140  //~~~
141  //
142  // The `Parameter.Units` is optional. If given, an additional parameter,
143  // `[name].Units` will be added to each KVDBRunFile.
144  //
145  // The lists of runfiles use the syntax detailed in KVExpDB::SetRunIndexListFromString().
146 
147  TString fullpath = "";
148  if (!KVBase::SearchKVFile(filename, fullpath, fDataSet)) {
149  Info("LoadParameters", "%s does not exist or not found", filename.Data());
150  return;
151  }
152 
153  Info("LoadParameters", "file : %s found", fullpath.Data());
154  TEnv env;
155  env.ReadFile(fullpath, kEnvAll);
156 
157  KVString parname = env.GetValue("Parameter.Name", "toto");
158  KVString partype = env.GetValue("Parameter.Type", "Double_t");
159  KVString parunits = env.GetValue("Parameter.Units", "");
160 
161  partype.ToUpper();
162  if (partype.Contains("DOUBLE"))
163  partype = "D";
164  else
165  partype = "S";
166 
167  TIter next(env.GetTable());
168  TEnvRec* rec = 0;
169 
170  while ((rec = (TEnvRec*)next())) {
171 
172  KVString sname(rec->GetName());
173  if (sname.BeginsWith("Parameter")) continue;
174 
175  auto runlist = SetRunIndexListFromString(sname);
176 
177  KVString parval(rec->GetValue());
178 
179  for (auto& run : runlist) {
180  if (partype == "D")
181  GetDBRunFile(run).Set(parname, parval.Atof());
182  else
183  GetDBRunFile(run).Set(parname, parval);
184  if (parunits != "")
185  GetDBRunFile(run).Set(Form("%s.Units", parname.Data()), parunits);
186  }
187  }
188 }
189 
190 
191 
197 
199 {
200  // Build minimal database (runs & systems).
201  //
202  // If no run infos are available (perhaps no data yet available for dataset), we make a dummy
203  // database with runs numbered from 1 to 100.
204  try {
205  FillRunsTable();
206  }
207  catch (std::runtime_error& rte) {
208 
209  // an exception may be thrown if a 'bad' runfile is not found,
210  // which indicates an inconsistency in bad_runfiles.dat
211  if (TString(rte.what()) != "run infos file not found") throw;
212 
213  Info("Build", "No informations on dataset runs available");
214  Info("Build", "Dummy database runs numbered from 1 to 100 will be created");
215  for (int i = 1; i <= 100; ++i) {
216  AddRun(new KVDBRun(i, "Dummy database run"));
217  }
218  }
219 
220  ReadComments();
221  ReadSystemList();
222 }
223 
224 
225 
233 
235 {
236  // Build extended database.
237  //
238  // Calibrations are handled by each multidetector
239  //
240  // If ParameterFiles.dat file exists for dataset, we read any files it lists and
241  // add the corresponding parameter values to the appropriate KVDBRunFile objects
242 
243  ReadScalerInfos();
245 
246  KVMultiDetArray::MakeMultiDetector(fDataSet, -1, "KVMultiDetArray", this);
247  gMultiDetArray->MakeCalibrationTables(this);
248 }
249 
250 
251 
271 
273 {
274  // Fill the Runs table using the informations in file runinfos.root
275  // (which can be generated using KVRunListCreator).
276  //
277  // If there are no run infos available (perhaps because no data yet exists for the dataset),
278  // this method throws an exception and the database will have a dummy list of runs
279  // numbered from 1 to 100
280  //
281  // Any runfiles which are bad i.e. not to be considered for further analysis should
282  // be written in a file bad_runfiles.dat with the given format (TEnv):
283  // - comment lines begin with '#'
284  // - BadFiles.First: first runfile of each run in list is bad
285  // - BadFiles.All: all runfiles for each run in list are bad
286  // - BadFiles.Singles: individual runfiles given as run.index are bad
287  //~~~
288  //BadFiles.First: 17-26 30 35-42 44 52-54 57-63 65-73 76 83-111 123-141
289  //BadFiles.All: 16 27-29 43 46 51 55 56 64 74 75 110 112-122 138
290  //BadFiles.Singles: 26.101 86.8 108.21 109.46 111.31 137.9
291  //~~~
292 
293  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "runinfos.root");
294 
295  if (runinfos == "")
296  throw std::runtime_error("run infos file not found");
297 
298  int run_num{-1}, runfile_index{-1};
299 
300  Info("FillRunsTable", "Reading run infos from %s", runinfos.Data());
301  TFile runinfos_file(runinfos);
302  TIter it(runinfos_file.GetListOfKeys());
303  TKey* run_key;
304  KVList garbage;
305  run_index_t run_index;
306  while ((run_key = (TKey*)it())) {
307  if (TString(run_key->GetClassName()) == "KVNameValueList") {
308  // make sure we only use the highest cycle number of each key
309  if (run_key->GetCycle() == runinfos_file.GetKey(run_key->GetName())->GetCycle()) {
310  KVNameValueList* run = (KVNameValueList*)run_key->ReadObj();
311  garbage.Add(run);
312  if (with_index_multiplier) {
313  run_num = run->GetIntValue("Run") / index_multiplier;
314  runfile_index = run->GetIntValue("Run") % index_multiplier;
315  if (!runfile_index) run_index = run_index_t{run_num, std::nullopt};
316  else run_index = run_index_t{run_num, runfile_index};
317  }
318  else if (HasRawFilesWithIndex()) {
319  run_num = run->GetIntValue("Run");
320  KVDBRun* dbrun = GetDBRun(run_num);
321  if (!dbrun) {
322  dbrun = new KVDBRun;
323  dbrun->SetNumber(run_num);
324  AddRun(dbrun);
325  }
326  KVNumberList index_list(run->GetStringValue("IndexList"));
327  for (auto index : index_list) {
328  run_index = index ? run_index_t{run_num, index} :
329  run_index_t{run_num, std::nullopt};
330  auto runfile = new KVDBRunFile("", run_index);
331  runfile->SetStartDate(run->GetStringValue(Form("Start.%d", index)));
332  runfile->SetEndDate(run->GetStringValue(Form("End.%d", index)));
333  if (run->HasValue64bit(Form("Size.%d", index)))
334  runfile->SetSize(run->GetValue64bit(Form("Size.%d", index)) / 1024. / 1024.);
335  else
336  runfile->SetSize(run->GetIntValue(Form("Size.%d", index)) / 1024. / 1024.);
337 
338  if (run->HasValue64bit(Form("Events.%d", index))) {
339  runfile->SetEvents(run->GetValue64bit(Form("Events.%d", index)));
340  }
341  else
342  runfile->SetEvents(run->GetIntValue(Form("Events.%d", index)));
343  dbrun->AddRunFile(runfile);
344  }
345  continue;
346  }
347  else {
348  run_num = run->GetIntValue("Run");
349  run_index = run_index_t{run_num, std::nullopt};
350  }
351  KVDBRun* dbrun = GetDBRun(run_num);
352  if (!dbrun) {
353  dbrun = new KVDBRun;
354  dbrun->SetNumber(run_num);
355  AddRun(dbrun);
356  }
357  auto runfile = new KVDBRunFile("", run_index);
358  runfile->SetStartDate(run->GetStringValue("Start"));
359  runfile->SetEndDate(run->GetStringValue("End"));
360  if (run->HasValue64bit("Size"))
361  runfile->SetSize(run->GetValue64bit("Size") / 1024. / 1024.);
362  else
363  runfile->SetSize(run->GetIntValue("Size") / 1024. / 1024.);
364 
365  if (run->HasValue64bit("Events")) {
366  runfile->SetEvents(run->GetValue64bit("Events"));
367  }
368  else
369  runfile->SetEvents(run->GetIntValue("Events"));
370  dbrun->AddRunFile(runfile);
371  }
372  }
373  }
374  TString badinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "bad_runfiles.dat");
375  if (!badinfos.IsNull()) {
376  Info("FillRunsTable", "Reading bad runfile infos from %s", badinfos.Data());
377  TEnv badfiles;
378  badfiles.ReadFile(badinfos, kEnvLocal);
379  KVNumberList all(badfiles.GetValue("BadFiles.All", ""));
380  all.Begin();
381  while (!all.End()) {
382  auto br = all.Next();
383  auto dbrun = GetDBRun(br);
384  if (!dbrun) {
385  Error("FillRunsTable", "%s:BadFiles.All contains run number %d which is not in %s",
386  badinfos.Data(), br, runinfos.Data());
387  throw std::runtime_error(Form("inconsistency between %s:BadFiles.All and %s",
388  badinfos.Data(), runinfos.Data()));
389  }
390  dbrun->SetBadAllRunFiles();
391  }
392  KVNumberList first(badfiles.GetValue("BadFiles.First", ""));
393  first.Begin();
394  while (!first.End()) {
395  auto br = first.Next();
396  auto dbrun = GetDBRun(br);
397  if (!dbrun) {
398  Error("FillRunsTable", "%s:BadFiles.First contains run number %d which is not in %s",
399  badinfos.Data(), br, runinfos.Data());
400  throw std::runtime_error(Form("inconsistency between %s:BadFiles.First and %s",
401  badinfos.Data(), runinfos.Data()));
402  }
403  dbrun->GetFirstFile().SetBad();
404  }
405  run_index_list singles(badfiles.GetValue("BadFiles.Singles", ""));
406  for (auto& ri : singles) {
407  try {
408  GetDBRunFile(ri).SetBad();
409  }
410  catch (std::exception& e) {
411  Error("FillRunsTable", "%s:BadFiles.Singles contains run_index %s which is unknown",
412  badinfos.Data(), ri.as_string().Data());
413  throw;
414  }
415  }
416  }
417  // now set global runfile index for all good files
418  Int_t global_runfile_number = 0;
419  for (auto run : fListOfRuns) {
420  auto r = GetDBRun(run);
421  auto ril = r->GetRunIndexList();
422  for (auto& ri : ril) {
423  if (!GetDBRunFile(ri).IsBad()) {
424  GetDBRunFile(ri).SetGlobalRunFileNumber(++global_runfile_number);
425  fMapGlobRunFileNumberToRunIndex[global_runfile_number] = ri;
426  }
427  }
428  }
429 }
430 
431 
432 //____________________________________________________________________________//
433 
434 
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:541
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:2067
static Bool_t FindDataSetFile(const TString &dataset, const Char_t *filename)
Definition: KVDataSet.cpp:2101
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:913
virtual void ReadComments()
Definition: KVExpDB.cpp:1046
virtual void ReadSystemList()
Definition: KVExpDB.cpp:423
void AddRun(KVDBRun *r)
Definition: KVExpDB.h:135
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:144
virtual KVSeqCollection * GetRuns() const
Definition: KVExpDB.h:140
bool with_index_multiplier
Definition: KVExpDB.h:72
const KVDBRunFile & GetDBRunFile(const run_index_t &r) const
Definition: KVExpDB.h:148
int index_multiplier
Definition: KVExpDB.h:71
Bool_t HasRawFilesWithIndex() const
Definition: KVExpDB.h:254
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 extend_minimal_database() override
void ReadParameterFiles()
void make_minimal_database() override
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)