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 
65 
67 {
68  // Build the database.
69  // Runs & Systems tables are handled by us, calibrations are handled by each multidetector
70  //
71  // If no run infos are available (perhaps no data yet available for dataset), we make a dummy
72  // database with runs numbered from 1 to 100.
73 
74  try {
75  FillRunsTable();
76  }
77  catch (std::runtime_error& rte) {
78 
79  // an exception may be thrown if a 'bad' runfile is not found,
80  // which indicates an inconsistency in bad_runfiles.dat
81  if (TString(rte.what()) != "run infos file not found") throw;
82 
83  Info("Build", "No informations on dataset runs available");
84  Info("Build", "Dummy database runs numbered from 1 to 100 will be created");
85  for (int i = 1; i <= 100; ++i) {
86  AddRun(new KVDBRun(i, "Dummy database run"));
87  }
88  }
89 
90  ReadComments();
93 
94  KVMultiDetArray::MakeMultiDetector(fDataSet, -1, "KVMultiDetArray", this);
95  gMultiDetArray->MakeCalibrationTables(this);
96 }
97 
98 
99 
103 
105 {
106  // Look for file scalers.root and read scalers from it
107  // scalers are assumed to be stored as 64-bit parameters in the list
108 
109  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "scalers.root");
110  if (runinfos == "") return;
111 
112  Info("ReadScalerInfos", "Reading scaler infos from %s", runinfos.Data());
113  TFile runinfos_file(runinfos);
114  TIter it_run(GetRuns());
115  KVDBRun* run;
116  while ((run = (KVDBRun*)it_run())) {
117  auto file_list = run->GetRunIndexList();
118  for (auto& rf : file_list) {
119  auto scalist = (KVNameValueList*)runinfos_file.Get(Form("run%s", rf.as_string().Data()));
120  if (!scalist) scalist = (KVNameValueList*)runinfos_file.Get(Form("run_%06d", run->GetNumber() * index_multiplier));
121  if (scalist) {
122  int npar = scalist->GetNpar();
123  for (int i = 0; i < npar; i += 2) {
124  TString parname = scalist->GetParameter(i)->GetName();
125  parname.Remove(parname.Length() - 3, 3);
126  run->GetRunFile(rf).SetScaler64(parname, scalist->GetValue64bit(parname));
127  }
128  }
129  }
130  }
131 }
132 
133 
134 
154 
156 {
157  // Fill the Runs table using the informations in file runinfos.root
158  // (which can be generated using KVRunListCreator).
159  //
160  // If there are no run infos available (perhaps because no data yet exists for the dataset),
161  // this method throws an exception and the database will have a dummy list of runs
162  // numbered from 1 to 100
163  //
164  // Any runfiles which are bad i.e. not to be considered for further analysis should
165  // be written in a file bad_runfiles.dat with the given format (TEnv):
166  // - comment lines begin with '#'
167  // - BadFiles.First: first runfile of each run in list is bad
168  // - BadFiles.All: all runfiles for each run in list are bad
169  // - BadFiles.Singles: individual runfiles given as run.index are bad
170  //~~~
171  //BadFiles.First: 17-26 30 35-42 44 52-54 57-63 65-73 76 83-111 123-141
172  //BadFiles.All: 16 27-29 43 46 51 55 56 64 74 75 110 112-122 138
173  //BadFiles.Singles: 26.101 86.8 108.21 109.46 111.31 137.9
174  //~~~
175 
176  TString runinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "runinfos.root");
177 
178  if (runinfos == "")
179  throw std::runtime_error("run infos file not found");
180 
181  int run_num{-1}, runfile_index{-1};
182 
183  Info("FillRunsTable", "Reading run infos from %s", runinfos.Data());
184  TFile runinfos_file(runinfos);
185  TIter it(runinfos_file.GetListOfKeys());
186  TKey* run_key;
187  KVList garbage;
188  run_index_t run_index;
189  while ((run_key = (TKey*)it())) {
190  if (TString(run_key->GetClassName()) == "KVNameValueList") {
191  // make sure we only use the highest cycle number of each key
192  if (run_key->GetCycle() == runinfos_file.GetKey(run_key->GetName())->GetCycle()) {
193  KVNameValueList* run = (KVNameValueList*)run_key->ReadObj();
194  garbage.Add(run);
195  if (with_index_multiplier) {
196  run_num = run->GetIntValue("Run") / index_multiplier;
197  runfile_index = run->GetIntValue("Run") % index_multiplier;
198  if (!runfile_index) run_index = run_index_t{run_num, std::nullopt};
199  else run_index = run_index_t{run_num, runfile_index};
200  }
201  else if (HasRawFilesWithIndex()) {
202  run_num = run->GetIntValue("Run");
203  KVDBRun* dbrun = GetDBRun(run_num);
204  if (!dbrun) {
205  dbrun = new KVDBRun;
206  dbrun->SetNumber(run_num);
207  AddRun(dbrun);
208  }
209  KVNumberList index_list(run->GetStringValue("IndexList"));
210  for (auto index : index_list) {
211  run_index = index ? run_index_t{run_num, index} :
212  run_index_t{run_num, std::nullopt};
213  auto runfile = new KVDBRunFile("", run_index);
214  runfile->SetStartDate(run->GetStringValue(Form("Start.%d", index)));
215  runfile->SetEndDate(run->GetStringValue(Form("End.%d", index)));
216  if (run->HasValue64bit(Form("Size.%d", index)))
217  runfile->SetSize(run->GetValue64bit(Form("Size.%d", index)) / 1024. / 1024.);
218  else
219  runfile->SetSize(run->GetIntValue(Form("Size.%d", index)) / 1024. / 1024.);
220 
221  if (run->HasValue64bit(Form("Events.%d", index))) {
222  runfile->SetEvents(run->GetValue64bit(Form("Events.%d", index)));
223  }
224  else
225  runfile->SetEvents(run->GetIntValue(Form("Events.%d", index)));
226  dbrun->AddRunFile(runfile);
227  }
228  continue;
229  }
230  else {
231  run_num = run->GetIntValue("Run");
232  run_index = run_index_t{run_num, std::nullopt};
233  }
234  KVDBRun* dbrun = GetDBRun(run_num);
235  if (!dbrun) {
236  dbrun = new KVDBRun;
237  dbrun->SetNumber(run_num);
238  AddRun(dbrun);
239  }
240  auto runfile = new KVDBRunFile("", run_index);
241  runfile->SetStartDate(run->GetStringValue("Start"));
242  runfile->SetEndDate(run->GetStringValue("End"));
243  if (run->HasValue64bit("Size"))
244  runfile->SetSize(run->GetValue64bit("Size") / 1024. / 1024.);
245  else
246  runfile->SetSize(run->GetIntValue("Size") / 1024. / 1024.);
247 
248  if (run->HasValue64bit("Events")) {
249  runfile->SetEvents(run->GetValue64bit("Events"));
250  }
251  else
252  runfile->SetEvents(run->GetIntValue("Events"));
253  dbrun->AddRunFile(runfile);
254  }
255  }
256  }
257  TString badinfos = KVDataSet::GetFullPathToDataSetFile(fDataSet, "bad_runfiles.dat");
258  if (!badinfos.IsNull()) {
259  Info("FillRunsTable", "Reading bad runfile infos from %s", badinfos.Data());
260  TEnv badfiles;
261  badfiles.ReadFile(badinfos, kEnvLocal);
262  KVNumberList all(badfiles.GetValue("BadFiles.All", ""));
263  all.Begin();
264  while (!all.End()) {
265  auto br = all.Next();
266  auto dbrun = GetDBRun(br);
267  if (!dbrun) {
268  Error("FillRunsTable", "%s:BadFiles.All contains run number %d which is not in %s",
269  badinfos.Data(), br, runinfos.Data());
270  throw std::runtime_error(Form("inconsistency between %s:BadFiles.All and %s",
271  badinfos.Data(), runinfos.Data()));
272  }
273  dbrun->SetBadAllRunFiles();
274  }
275  KVNumberList first(badfiles.GetValue("BadFiles.First", ""));
276  first.Begin();
277  while (!first.End()) {
278  auto br = first.Next();
279  auto dbrun = GetDBRun(br);
280  if (!dbrun) {
281  Error("FillRunsTable", "%s:BadFiles.First contains run number %d which is not in %s",
282  badinfos.Data(), br, runinfos.Data());
283  throw std::runtime_error(Form("inconsistency between %s:BadFiles.First and %s",
284  badinfos.Data(), runinfos.Data()));
285  }
286  dbrun->GetFirstFile().SetBad();
287  }
288  run_index_list singles(badfiles.GetValue("BadFiles.Singles", ""));
289  for (auto& ri : singles) {
290  try {
291  GetDBRunFile(ri).SetBad();
292  }
293  catch (std::exception& e) {
294  Error("FillRunsTable", "%s:BadFiles.Singles contains run_index %s which is unknown",
295  badinfos.Data(), ri.as_string().Data());
296  throw;
297  }
298  }
299  }
300  // now set global runfile index for all good files
301  Int_t global_runfile_number = 0;
302  for (auto run : fListOfRuns) {
303  auto r = GetDBRun(run);
304  auto ril = r->GetRunIndexList();
305  for (auto& ri : ril) {
306  if (!GetDBRunFile(ri).IsBad())
307  GetDBRunFile(ri).SetGlobalRunFileNumber(++global_runfile_number);
308  }
309  }
310 }
311 
312 
313 //____________________________________________________________________________//
314 
315 
int Int_t
ROOT::R::TRInterface & r
#define e(i)
char Char_t
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 index
char name[80]
char * Form(const char *fmt,...)
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:73
A single raw data file associated with an experimental run ,,.
Definition: KVDBRunFile.h:38
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:91
void SetGlobalRunFileNumber(Int_t r)
Definition: KVDBRunFile.h:243
void SetBad(Bool_t is_bad=kTRUE)
Definition: KVDBRunFile.h:222
Description of an experimental run in database ,,.
Definition: KVDBRun.h:40
KVDBRunFile * GetRunFile(int index)
Definition: KVDBRun.h:48
void AddRunFile(KVDBRunFile *rf)
Definition: KVDBRun.h:84
void SetNumber(Int_t n) override
Definition: KVDBRun.h:164
run_index_list GetRunIndexList() const
Definition: KVDBRun.h:92
TString GetFullPathToDataSetFile(const Char_t *filename)
Definition: KVDataSet.cpp:1924
Base class to describe database of an experiment ,,.
Definition: KVExpDB.h:20
KVNumberList fListOfRuns
list of all run numbers
Definition: KVExpDB.h:27
virtual void ReadComments()
Definition: KVExpDB.cpp:705
virtual void ReadSystemList()
Definition: KVExpDB.cpp:248
void AddRun(KVDBRun *r)
Definition: KVExpDB.h:80
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:89
virtual KVSeqCollection * GetRuns() const
Definition: KVExpDB.h:85
bool with_index_multiplier
Definition: KVExpDB.h:31
const KVDBRunFile & GetDBRunFile(const run_index_t &r) const
Definition: KVExpDB.h:93
int index_multiplier
Definition: KVExpDB.h:30
Bool_t HasRawFilesWithIndex() const
Definition: KVExpDB.h:178
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()
KVExpSetUpDB()
Default constructor.
virtual ~KVExpSetUpDB()
Destructor.
void ReadScalerInfos()
void Build() override
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
T * Get(const char *namecycle)
TKey * GetKey(const char *name, Short_t cycle=9999) const override
TList * GetListOfKeys() const override
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
const char * Data() const
Bool_t IsNull() const
TString & Remove(EStripType s, char c)
Specifies a runfile according to run number and file index ,.
Definition: run_index.h:33
ClassImp(TPyArg)