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  KVString _path(path);
43  gSystem->ExpandPathName(_path);
44  if (!gSystem->IsAbsoluteFileName(_path)) {
45  if (_path.CountChar('.') == 1) {
46  _path.ReplaceAll(".", "$(PWD)");
47  gSystem->ExpandPathName(_path);
48  }
49  }
50  SetTitle(_path);
51 }
52 
53 
54 
57 
59 {
60  // Default initialisations
61  fSimData.SetName("Simulated Data");
62  fFiltData.SetName("Filtered Simulated Data");
63 }
64 
65 
66 
67 
74 
76 {
77  // Copy constructor
78  // This ctor is used to make a copy of an existing object (for example
79  // when a method returns an object), and it is always a good idea to
80  // implement it.
81  // If your class allocates memory in its constructor(s) then it is ESSENTIAL :-)
82 
83  obj.Copy(*this);
84 }
85 
86 
87 
88 
96 
97 void KVSimDir::Copy(TObject& obj) const
98 {
99  // This method copies the current state of 'this' object into 'obj'
100  // You should add here any member variables, for example:
101  // (supposing a member variable KVSimDir::fToto)
102  // CastedObj.fToto = fToto;
103  // or
104  // CastedObj.SetToto( GetToto() );
105 
106  KVBase::Copy(obj);
107  //KVSimDir& CastedObj = (KVSimDir&)obj;
108 }
109 
110 
111 
117 
118 void KVSimDir::SetAuxDirectory(const TString& fullpath)
119 {
120  // Add an auxiliary directory to this simulated dataset.
121  //
122  // When AnalyseDirectory() is called, the auxiliary directory will also be scanned
123  // and any files found will be added to use
124 
125  fAuxDir = std::make_unique<KVSimDir>("aux",fullpath);
126 }
127 
128 
129 
131 
133 {
134  if(fAuxDir)
135  return fAuxDir->GetDirectory();
136  return "";
137 }
138 
139 
140 
142 
144 {
145  return (Bool_t)fAuxDir;
146 }
147 
148 
149 
152 
154 {
155  // returns true if given file is in the auxiliary directory
156  if(sf->GetSimDir() == this)
157  return false;
158  if(fAuxDir && sf->GetSimDir() == fAuxDir.get()) return true;
159  Error("IsFromAuxDirectory", "The file %s is neither associated to the main nor the auxiliary directory of dataset %s",
160  sf->GetName(), GetName());
161  return false;
162 }
163 
164 
165 
166 
170 
172 {
173  // Read contents of directory given to ctor.
174  // Each ROOT file will be analysed by AnalyseFile().
175 
176  Info("AnalyseDirectory", "Analysing %s...", GetDirectory());
177  fSimData.Clear();
178  fFiltData.Clear();
179  //loop over files in current directory
180  KVSystemDirectory thisDir(".", GetDirectory());
181  auto fileList = thisDir.GetListOfFiles();
182  TIter nextFile(fileList);
183  KVSystemFile* aFile = 0;
184  while ((aFile = (KVSystemFile*)nextFile())) {
185 
186  KVString fileName = aFile->GetName();
187  if (!fileName.EndsWith(".root") && !fileName.Contains(".root.")) continue; /* skip non-ROOT files */
188 
189  AnalyseFile(fileName);
190  }
191 
192  // analyse auxiliary directory, if set
193  if(fAuxDir){
194  fAuxDir->AnalyseDirectory();
195  // copy all files to our lists
196  fSimData.AddAll(&(fAuxDir->fSimData));
197  fFiltData.AddAll(&(fAuxDir->fFiltData));
198  }
199 }
200 
201 
202 
203 
223 
224 void KVSimDir::AnalyseFile(const Char_t* filename)
225 {
226  // Analyse ROOT file given as argument.
227  // If there is a TTree in the file, then we look at all of its branches until we find one
228  // containing objects which derive from KVEvent:
229  //
230  // -- if they inherit from KVSimEvent, we add the file to the list of simulated data:
231  // * a KVSimFile is created. The title of the TTree where data were found will
232  // be used as 'Information' on the nature of the simulation.
233  // -- if they inherit from KVReconstructedEvent, we add the file to the list of filtered data.
234  // * a KVSimFile is created. Informations on the filtered data are extracted from
235  // TNamed objects in the file with names 'Dataset', 'System', 'Run', 'Origin'
236  // (i.e. the name of the simulation
237  // file which was filtered), 'Filter' (type of filter: Geo, GeoThresh or Full).
238  // These objects are automatically created when data is filtered using KVEventFiltering.
239  //
240  // Analysis of the file stops after the first TTree with a branch satisfying one of the
241  // two criteria is found (it is assumed that in each file there is only one TTree containing
242  // either simulated or filtered data).
243 
244  //Info("AnalyseFile", "Analysing file %s...", filename);
245  TString fullpath;
247  unique_ptr<TFile> file(TFile::Open(fullpath));
248  if (!file.get() || file->IsZombie()) return;
249  // look for TTrees in file
250  TIter next(file->GetListOfKeys());
251  TKey* key;
252  while ((key = (TKey*)next())) {
253  TString cn = key->GetClassName();
254  if (cn == "TTree") {
255  // look for branch with KVEvent objects
256  TTree* tree = (TTree*)file->Get(key->GetName());
257  TSeqCollection* branches = tree->GetListOfBranches();
258  TIter nextB(branches);
259  TBranchElement* branch;
260  while ((branch = (TBranchElement*)nextB())) {
261  TString branch_classname = branch->GetClassName();
262  TClass* branch_class = TClass::GetClass(branch_classname, kFALSE, kTRUE);
263  if (branch_class && branch_class->InheritsFrom("KVEvent")) {
264  if (branch_class->InheritsFrom("KVReconstructedEvent")) {
265  // filtered data. there must be TNamed called 'Dataset', and possibly 'System' and/or 'Run' in the file.
266  unique_ptr<TNamed> ds((TNamed*)file->Get("Dataset"));
267  unique_ptr<TNamed> orig((TNamed*)file->Get("Origin"));
268  unique_ptr<TNamed> sys((TNamed*)file->Get("System"));
269  unique_ptr<TNamed> r((TNamed*)file->Get("Run"));
270  unique_ptr<TNamed> f((TNamed*)file->Get("Filter"));
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  TString origin;
279  if (orig) origin = orig->GetTitle();
280  TString filter;
281  if (f) filter = f->GetTitle();
282  Int_t run_number = -1;
283  if (run.IsDec()) run_number = run.Atoi();
284  TString _dqa;
285  if(dqa) _dqa = dqa->GetTitle();
286  KVSimFile* fff = new KVSimFile(this, filename, tree->GetTitle(), tree->GetEntries(), tree->GetName(), branch->GetName(),
287  dataset, system, run_number, origin, filter, _dqa);
288  fFiltData.Add(fff);
289  unique_ptr<TNamed> gem((TNamed*)file->Get("Gemini++"));
290  if (gem.get()) {
291  if (!strcmp(gem->GetTitle(), "yes")) fff->SetGemini();
292  unique_ptr<TNamed> gemdec((TNamed*)file->Get("GemDecayPerEvent"));
293  if (gemdec.get()) {
294  TString gemdecperev = gemdec->GetTitle();
295  fff->SetGemDecayPerEvent(gemdecperev.Atoi());
296  }
297  }
298  return;
299  }
300  else {
301  fSimData.Add(new KVSimFile(this, filename, tree->GetTitle(), tree->GetEntries(), tree->GetName(), branch->GetName()));
302  return;
303  }
304  }
305  }
306  }
307  }
308 }
309 
310 
311 
316 
317 void KVSimDir::AddSimData(KVSimFile* f, bool set_parent_aux_dir)
318 {
319  // adds a simulated data file to dataset
320  //
321  // if set_parent_aux_dir=true: set the auxiliary simdir as parent
322  fSimData.Add(f);
323  if(set_parent_aux_dir) f->SetSimDir(fAuxDir.get());
324  else f->SetSimDir(this);
325 }
326 
327 
328 
333 
334 void KVSimDir::AddFiltData(KVSimFile* f, bool set_parent_aux_dir)
335 {
336  // adds a filtered data file to dataset
337  //
338  // if set_parent_aux_dir=true: set the auxiliary simdir as parent
339  fFiltData.Add(f);
340  if(set_parent_aux_dir) f->SetSimDir(fAuxDir.get());
341  else f->SetSimDir(this);
342 }
343 
344 
345 
346 
348 
349 void KVSimDir::ls(Option_t*) const
350 {
352  cout << "SIMULATION SET: " << GetName() << endl;
353  cout << "DIRECTORY: " << GetDirectory() << endl;
354  cout << "CONTENTS:" << endl;
355  cout << "--simulated data:" << endl;
356  fSimData.ls();
357  cout << "--filtered data:" << endl;
358  fFiltData.ls();
359 }
360 
361 
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]
void AssignAndDelete(TString &target, char *tobedeleted)
R__EXTERN TSystem * gSystem
Base class for KaliVeda framework.
Definition: KVBase.h:139
void Copy(TObject &) const override
Make a copy of this object.
Definition: KVBase.cpp:373
void Add(TObject *obj) override
void Clear(Option_t *option="") override
Handle directory containing simulated and/or filtered simulated data ,.
Definition: KVSimDir.h:43
void ls(Option_t *opt="") const override
Definition: KVSimDir.cpp:349
void Copy(TObject &) const override
Definition: KVSimDir.cpp:97
void SetAuxDirectory(const TString &)
Definition: KVSimDir.cpp:118
void AddFiltData(KVSimFile *, bool=false)
Definition: KVSimDir.cpp:334
void init()
Default initialisations.
Definition: KVSimDir.cpp:58
Bool_t HasAuxDirectory() const
Definition: KVSimDir.cpp:143
KVSimDir()
Default constructor.
Definition: KVSimDir.cpp:25
const Char_t * GetDirectory() const
Definition: KVSimDir.h:61
Bool_t IsFromAuxDirectory(KVSimFile *) const
returns true if given file is in the auxiliary directory
Definition: KVSimDir.cpp:153
std::unique_ptr< KVSimDir > fAuxDir
Definition: KVSimDir.h:47
void AnalyseFile(const Char_t *)
Definition: KVSimDir.cpp:224
KVList fSimData
list of simulated data files
Definition: KVSimDir.h:45
KVList fFiltData
list of filtered simulated data files
Definition: KVSimDir.h:46
void AnalyseDirectory()
Definition: KVSimDir.cpp:171
TString GetAuxDirectory() const
Definition: KVSimDir.cpp:132
void AddSimData(KVSimFile *, bool=false)
Definition: KVSimDir.cpp:317
Handle file containing simulated and/or filtered simulated data ,.
Definition: KVSimFile.h:20
void SetGemini(Bool_t yes=kTRUE)
Definition: KVSimFile.h:54
KVSimDir * GetSimDir() const
Definition: KVSimFile.h:68
void SetGemDecayPerEvent(Int_t n)
Definition: KVSimFile.h:58
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
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 void Error(const char *method, const char *msgfmt,...) const
virtual const char * GetTitle() const
virtual void Info(const char *method, const char *msgfmt,...) const
static void IndentLevel()
Bool_t IsDec() const
Int_t Atoi() const
Bool_t EndsWith(const char *pat, ECaseCompare cmp=kExact) const
Int_t CountChar(Int_t c) const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
TString & ReplaceAll(const char *s1, const char *s2)
virtual char * ConcatFileName(const char *dir, const char *name)
virtual Bool_t IsAbsoluteFileName(const char *dir)
virtual char * ExpandPathName(const char *path)
void init()
ClassImp(TPyArg)