KaliVeda
Toolkit for HIC analysis
KVSimDir.cpp
1 //Created by KVClassFactory on Tue Jul 17 08:46:41 2012
2 //Author: John Frankland,,,
3 
4 #include "KVSimDir.h"
5 #include "KVSimFile.h"
6 #include "KVSystemDirectory.h"
7 #include "TSystem.h"
8 #include "KVString.h"
9 #include "TTree.h"
10 #include "TKey.h"
11 #include "TFile.h"
12 #include "TBranchElement.h"
13 #include <KVSystemFile.h>
14 #include <iostream>
15 using namespace std;
16 
18 
19 
20 
21 
22 
26 {
27  // Default constructor
28  init();
29 }
30 
31 
32 
35 
36 KVSimDir::KVSimDir(const Char_t* name, const Char_t* path)
37  : KVBase(name, path)
38 {
39  // Ctor with name and directory to analyse
40  init();
41  // make sure we have absolute path
42  auto _path = AbsoluteUnixPath(path);
43  if (_path)
44  SetTitle(_path.value());
45  else
46  throw std::runtime_error(Form("<KVSimDir::KVSimDir> : invalid path %s given to constructor", path));
47 }
48 
49 
50 
53 
55 {
56  // Default initialisations
57  fSimData.SetName("Simulated Data");
58  fFiltData.SetName("Filtered Simulated Data");
59 }
60 
61 
62 
63 
70 
72 {
73  // Copy constructor
74  // This ctor is used to make a copy of an existing object (for example
75  // when a method returns an object), and it is always a good idea to
76  // implement it.
77  // If your class allocates memory in its constructor(s) then it is ESSENTIAL :-)
78 
79  obj.Copy(*this);
80 }
81 
82 
83 
84 
92 
93 void KVSimDir::Copy(TObject& obj) const
94 {
95  // This method copies the current state of 'this' object into 'obj'
96  // You should add here any member variables, for example:
97  // (supposing a member variable KVSimDir::fToto)
98  // CastedObj.fToto = fToto;
99  // or
100  // CastedObj.SetToto( GetToto() );
101 
102  KVBase::Copy(obj);
103  //KVSimDir& CastedObj = (KVSimDir&)obj;
104 }
105 
106 
107 
113 
114 void KVSimDir::SetAuxDirectory(const TString& fullpath)
115 {
116  // Add an auxiliary directory to this simulated dataset.
117  //
118  // When AnalyseDirectory() is called, the auxiliary directory will also be scanned
119  // and any files found will be added to use
120 
121  fAuxDir = std::make_unique<KVSimDir>("aux", fullpath);
122 }
123 
124 
125 
127 
129 {
130  if (fAuxDir)
131  return fAuxDir->GetDirectory();
132  return "";
133 }
134 
135 
136 
138 
140 {
141  return (Bool_t)fAuxDir;
142 }
143 
144 
145 
148 
150 {
151  // returns true if given file is in the auxiliary directory
152  if (sf->GetSimDir() == this)
153  return false;
154  if (fAuxDir && sf->GetSimDir() == fAuxDir.get()) return true;
155  Error("IsFromAuxDirectory", "The file %s is neither associated to the main nor the auxiliary directory of dataset %s",
156  sf->GetName(), GetName());
157  return false;
158 }
159 
160 
161 
162 
166 
168 {
169  // Read contents of directory given to ctor.
170  // Each ROOT file will be analysed by AnalyseFile().
171 
172  Info("AnalyseDirectory", "Analysing %s...", GetDirectory());
173  fSimData.Clear();
174  fFiltData.Clear();
175  //loop over files in current directory
176  KVSystemDirectory thisDir(".", GetDirectory());
177  auto fileList = thisDir.GetListOfFiles();
178  TIter nextFile(fileList);
179  KVSystemFile* aFile = 0;
180  while ((aFile = (KVSystemFile*)nextFile())) {
181 
182  KVString fileName = aFile->GetName();
183  if (!fileName.EndsWith(".root") && !fileName.Contains(".root.")) continue; /* skip non-ROOT files */
184 
185  AnalyseFile(fileName);
186  }
187 
188  // analyse auxiliary directory, if set
189  if (fAuxDir) {
190  fAuxDir->AnalyseDirectory();
191  // copy all files to our lists
192  fSimData.AddAll(&(fAuxDir->fSimData));
193  fFiltData.AddAll(&(fAuxDir->fFiltData));
194  // we become the owner of these files
195  fAuxDir->fSimData.Clear("nodelete");
196  fAuxDir->fFiltData.Clear("nodelete");
197  }
198 }
199 
200 
201 
202 
222 
223 void KVSimDir::AnalyseFile(const Char_t* filename)
224 {
225  // Analyse ROOT file given as argument.
226  // If there is a TTree in the file, then we look at all of its branches until we find one
227  // containing objects which derive from KVEvent:
228  //
229  // -- if they inherit from KVSimEvent, we add the file to the list of simulated data:
230  // * a KVSimFile is created. The title of the TTree where data were found will
231  // be used as 'Information' on the nature of the simulation.
232  // -- if they inherit from KVReconstructedEvent, we add the file to the list of filtered data.
233  // * a KVSimFile is created. Informations on the filtered data are extracted from
234  // TNamed objects in the file with names 'Dataset', 'System', 'Run', 'Origin'
235  // (i.e. the name of the simulation
236  // file which was filtered), 'Filter' (type of filter: Geo, GeoThresh or Full).
237  // These objects are automatically created when data is filtered using KVEventFiltering.
238  //
239  // Analysis of the file stops after the first TTree with a branch satisfying one of the
240  // two criteria is found (it is assumed that in each file there is only one TTree containing
241  // either simulated or filtered data).
242 
243  //Info("AnalyseFile", "Analysing file %s...", filename);
244  TString fullpath;
246  unique_ptr<TFile> file(TFile::Open(fullpath));
247  if (!file.get() || file->IsZombie()) return;
248  // look for TTrees in file
249  TIter next(file->GetListOfKeys());
250  TKey* key;
251  while ((key = (TKey*)next())) {
252  TString cn = key->GetClassName();
253  if (cn == "TTree") {
254  // look for branch with KVEvent objects
255  TTree* tree = (TTree*)file->Get(key->GetName());
256  TSeqCollection* branches = tree->GetListOfBranches();
257  TIter nextB(branches);
258  TBranchElement* branch;
259  while ((branch = (TBranchElement*)nextB())) {
260  TString branch_classname = branch->GetClassName();
261  TClass* branch_class = TClass::GetClass(branch_classname, kFALSE, kTRUE);
262  if (branch_class && branch_class->InheritsFrom("KVEvent")) {
263  if (branch_class->InheritsFrom("KVReconstructedEvent")) {
264  // filtered data. there must be TNamed called 'Dataset', and possibly 'System' and/or 'Run' in the file.
265  unique_ptr<TNamed> ds((TNamed*)file->Get("Dataset"));
266  unique_ptr<TNamed> orig((TNamed*)file->Get("Origin"));
267  unique_ptr<TNamed> sys((TNamed*)file->Get("System"));
268  unique_ptr<TNamed> r((TNamed*)file->Get("Run"));
269  unique_ptr<TNamed> f((TNamed*)file->Get("Filter"));
270  unique_ptr<TNamed> phi((TNamed*)file->Get("RandomPhi"));
271  unique_ptr<TNamed> dqa((TNamed*)file->Get("DataQualityAudit"));
272  TString dataset;
273  if (ds) dataset = ds->GetTitle();
274  TString system;
275  if (sys) system = sys->GetTitle();
276  TString run;
277  if (r) run = r->GetTitle();
278  Bool_t random_phi = true;
279  if (phi) random_phi = (TString(phi->GetTitle()) == "yes");
280  TString origin;
281  if (orig) {
282  origin = orig->GetTitle();
283  // possibly need to correct name of original file
284  // due to a bug in KVEventFiltering, when filtering files with names like
285  // HIPSE_197Au_197Au_40.0AMeV_ASYM.root.36
286  // the 'original file name' stored in this TNamed object became
287  // HIPSE_197Au_197Au_40.0AMeV_ASYM.36.root
288  // which is almost the same, but not quite...
289  KVString o(origin);
290  o.Begin(".");
291  std::vector<KVString> bits;
292  while (!o.End()) {
293  bits.push_back(o.Next());
294  }
295  if (bits.size() > 2) {
296  // if the last two bits are an integer and "root", swap them round
297  auto last_bit = bits.size() - 1;
298  auto last_but_one_bit = last_bit - 1;
299  if (bits[last_bit] == "root" && bits[last_but_one_bit].IsDec()) {
300  origin.Remove(origin.Index(Form(".%d.root", bits[last_but_one_bit].Atoi())),
301  origin.Length() - origin.Index(Form(".%d.root", bits[last_but_one_bit].Atoi())));
302  origin += Form(".root.%d", bits[last_but_one_bit].Atoi());
303  }
304  }
305  }
306  TString filter;
307  if (f) filter = f->GetTitle();
308  Int_t run_number = -1;
309  if (run.IsDec()) run_number = run.Atoi();
310  TString _dqa;
311  if (dqa) _dqa = dqa->GetTitle();
312  KVSimFile* fff = new KVSimFile(this, filename, tree->GetTitle(), tree->GetEntries(), tree->GetName(), branch->GetName(),
313  dataset, system, run_number, origin, filter, _dqa);
314  fFiltData.Add(fff);
315  fff->SetRandomPhi(random_phi);
316  unique_ptr<TNamed> gem((TNamed*)file->Get("Gemini++"));
317  if (gem) {
318  if (!strcmp(gem->GetTitle(), "yes")) fff->SetGemini();
319  unique_ptr<TNamed> gemdec((TNamed*)file->Get("GemDecayPerEvent"));
320  if (gemdec) {
321  TString gemdecperev = gemdec->GetTitle();
322  fff->SetGemDecayPerEvent(gemdecperev.Atoi());
323  }
324  unique_ptr<TNamed> gemrot((TNamed*)file->Get("GemAddRotEner"));
325  if (gemrot) {
326  fff->SetGemAddRotEner(TString(gemrot->GetTitle()) == "yes");
327  }
328  }
329  return;
330  }
331  else {
332  fSimData.Add(new KVSimFile(this, filename, tree->GetTitle(), tree->GetEntries(), tree->GetName(), branch->GetName()));
333  return;
334  }
335  }
336  }
337  }
338  }
339 }
340 
341 
342 
347 
348 void KVSimDir::AddSimData(KVSimFile* f, bool set_parent_aux_dir)
349 {
350  // adds a simulated data file to dataset
351  //
352  // if set_parent_aux_dir=true: set the auxiliary simdir as parent
353  fSimData.Add(f);
354  if (set_parent_aux_dir) f->SetSimDir(fAuxDir.get());
355  else f->SetSimDir(this);
356 }
357 
358 
359 
364 
365 void KVSimDir::AddFiltData(KVSimFile* f, bool set_parent_aux_dir)
366 {
367  // adds a filtered data file to dataset
368  //
369  // if set_parent_aux_dir=true: set the auxiliary simdir as parent
370  fFiltData.Add(f);
371  if (set_parent_aux_dir) f->SetSimDir(fAuxDir.get());
372  else f->SetSimDir(this);
373 }
374 
375 
376 
377 
379 
380 void KVSimDir::ls(Option_t*) const
381 {
383  cout << "SIMULATION SET: " << GetName() << endl;
384  cout << "DIRECTORY: " << GetDirectory() << endl;
385  cout << "CONTENTS:" << endl;
386  cout << "--simulated data:" << endl;
387  fSimData.ls();
388  cout << "--filtered data:" << endl;
389  fFiltData.ls();
390 }
391 
392 
int Int_t
ROOT::R::TRInterface & r
#define f(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
const char Option_t
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
char name[80]
char * Form(const char *fmt,...)
void AssignAndDelete(TString &target, char *tobedeleted)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:140
void Error(const char *method, const char *msgfmt,...) const override
colourised errors (red) !
Definition: KVBase.cpp:1686
void Copy(TObject &) const override
Make a copy of this object.
Definition: KVBase.cpp:397
static std::optional< TString > AbsoluteUnixPath(const TString &)
Definition: KVBase.cpp:1729
void Add(TObject *obj) override
void Clear(Option_t *option="") override
Handle directory containing simulated and/or filtered simulated data ,.
Definition: KVSimDir.h:46
void ls(Option_t *opt="") const override
Definition: KVSimDir.cpp:380
void Copy(TObject &) const override
Definition: KVSimDir.cpp:93
void SetAuxDirectory(const TString &)
Definition: KVSimDir.cpp:114
void AddFiltData(KVSimFile *, bool=false)
Definition: KVSimDir.cpp:365
void init()
Default initialisations.
Definition: KVSimDir.cpp:54
Bool_t HasAuxDirectory() const
Definition: KVSimDir.cpp:139
KVSimDir()
Default constructor.
Definition: KVSimDir.cpp:25
const Char_t * GetDirectory() const
Definition: KVSimDir.h:64
Bool_t IsFromAuxDirectory(KVSimFile *) const
returns true if given file is in the auxiliary directory
Definition: KVSimDir.cpp:149
std::unique_ptr< KVSimDir > fAuxDir
potential auxiliary directory associated with this one
Definition: KVSimDir.h:50
void AnalyseFile(const Char_t *)
Definition: KVSimDir.cpp:223
KVList fSimData
list of simulated data files
Definition: KVSimDir.h:48
KVList fFiltData
list of filtered simulated data files
Definition: KVSimDir.h:49
void AnalyseDirectory()
Definition: KVSimDir.cpp:167
TString GetAuxDirectory() const
Definition: KVSimDir.cpp:128
void AddSimData(KVSimFile *, bool=false)
Definition: KVSimDir.cpp:348
Handle file containing simulated and/or filtered simulated data ,.
Definition: KVSimFile.h:20
void SetRandomPhi(bool yes=true)
Definition: KVSimFile.h:172
void SetGemini(Bool_t yes=kTRUE)
Definition: KVSimFile.h:56
KVSimDir * GetSimDir() const
Definition: KVSimFile.h:80
void SetGemAddRotEner(bool yes=true)
Definition: KVSimFile.h:68
void SetGemDecayPerEvent(Int_t n)
Definition: KVSimFile.h:60
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
void Begin(TString delim) const
Definition: KVString.cpp:565
Bool_t End() const
Definition: KVString.cpp:634
KVString Next(Bool_t strip_whitespace=kFALSE) const
Definition: KVString.cpp:695
Extension of ROOT TSystemDirectory class, handling browsing directories on disk.
TList * GetListOfFiles() const override
Extended ROOT TSystemFile with added info on file size etc.
Definition: KVSystemFile.h:18
const char * GetClassName() const override
static TClass * GetClass(Bool_t load=kTRUE, Bool_t silent=kFALSE)
Bool_t InheritsFrom(const char *cl) const override
void ls(Option_t *option="") const override
void SetName(const char *name)
virtual void AddAll(const TCollection *col)
static TFile * Open(const char *name, Option_t *option="", const char *ftitle="", Int_t compress=ROOT::RCompressionSetting::EDefaults::kUseCompiledDefault, Int_t netopt=0)
virtual const char * GetClassName() const
virtual void SetTitle(const char *title="")
const char * GetName() const override
virtual const char * GetTitle() const
virtual void Info(const char *method, const char *msgfmt,...) const
static void IndentLevel()
Ssiz_t Length() const
Bool_t IsDec() const
Int_t Atoi() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
TString & Remove(EStripType s, char c)
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
virtual char * ConcatFileName(const char *dir, const char *name)
void init()
ClassImp(TPyArg)