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