Loading [MathJax]/extensions/tex2jax.js
KaliVeda
Toolkit for HIC analysis
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Modules Pages
KVRawDataAnalyser.cpp
1 //Created by KVClassFactory on Thu Sep 24 11:07:45 2009
2 //Author: John Frankland,,,
3 
4 #include "KVRawDataAnalyser.h"
5 #include "KVMultiDetArray.h"
6 #include "KVClassFactory.h"
7 #include "KVDataSet.h"
8 #include "TSystem.h"
9 #include "TROOT.h"
10 
11 using namespace std;
12 
14 
15 
16 
17 
18 
22 {
23  // Default constructor
24  fRunFile = 0;
25  TotalEntriesToRead = 0;
26  fTreeFile = nullptr;
27 }
28 
29 
30 
33 
35 {
36  // Destructor
37 }
38 
39 
40 
53 
55 {
56  // Perform treatment of a given run
57  // Before processing each run, after opening the associated file, user's InitRun() method is called.
58  // After each run, user's EndRun() is called.
59  // For each event of each run, user's Analysis() method is called just after calling
60  // gMultiDetArray->HandleRawDataEvent(fRunfile). In the Analysis() method the user can
61  // test gMultiDetArray->HandledRawData() to know if some pertinent data was found in
62  // the event or not.
63  //
64  // For further customisation, the pre/post-methods are called just before and just after
65  // each of these methods (preInitRun(), postAnalysis(), etc. etc.)
66 
67  //Open data file
68  KVString raw_file = gDataSet->GetFullPathToRunfile(GetDataType().Data(), fRunNumber);
69  fRunFile = gDataSet->OpenRunfile<KVRawDataReader>(GetDataType().Data(), fRunNumber);
70  if ((!fRunFile) || fRunFile->IsZombie()) {
71  //skip run if file cannot be opened
72  if (fRunFile) delete fRunFile;
73  return;
74  }
75 
76  //warning! real number of run may be different from that deduced from file name
77  //we get the real run number from the data and use it to name any new files
78  // Not possible for INDRAFAZIA MFM data (we use 'fake' run numbers)
79  // Is this still necessary? (which dataset was concerned? camp5?)
80 // Int_t newrun = fRunFile->GetRunNumberReadFromFile();
81 // if (newrun && newrun != fRunNumber) {
82 // cout << " *** WARNING *** run number read from file = " << newrun << endl;
83 // fRunNumber = newrun;
84 // }
85 
86  KVMultiDetArray::MakeMultiDetector(gDataSet->GetName(), fRunNumber.run());
87  fCurrentRun = gExpDB->GetDBRun(fRunNumber.run());
88 
89  // perform any initialisations which may be required, dependent on the
90  // format of the data, the multidetector, etc.
91  gMultiDetArray->InitialiseRawDataReading(fRunFile);
92 
93  fEventNumber = 1; //event number
94 
95  Long64_t nevents = GetNbEventToRead();
96  if (nevents <= 0) {
97  nevents = 1000000000;
98  cout << endl << "Reading all events from file " << raw_file.Data() << endl;
99  }
100  else {
101  cout << endl << "Reading " << nevents << " events from file " << raw_file.Data() << endl;
102  }
103 
104  cout << "Starting analysis of run " << fRunNumber << " on : ";
105  TDatime now;
106  cout << now.AsString() << endl << endl;
107 
108  fCurrentRun->Print();
109 
110  preInitRun();
111  //call user's beginning of run
112  InitRun();
113  postInitRun();
114 
115  //loop over events in file
116  while ((nevents-- ? fRunFile->GetNextEvent() : kFALSE) && !AbortProcessingLoop()) {
117 
118  gMultiDetArray->HandleRawDataEvent(fRunFile);
119 
120  // skip events that any active patches reject
121  if (fRustines.HasActivePatches())
122  if (fRustines.SkipEvent(gMultiDetArray))
123  continue;
124 
125  preAnalysis();
126  //call user's analysis. stop if returns kFALSE.
127  if (!Analysis()) break;
128  postAnalysis();
129 
130  if (CheckStatusUpdateInterval(fEventNumber)) DoStatusUpdate(fEventNumber);
131 
132  fEventNumber += 1;
133  }
134 
135  cout << "Ending analysis of run " << fRunNumber << " on : ";
136  TDatime now2;
137  cout << now2.AsString() << endl << endl;
138  cout << endl << "Finished reading " << fEventNumber - 1 << " events from file " << raw_file.Data() << endl << endl;
139 
140  preEndRun();
141  //call user's end of run function
142  EndRun();
143  postEndRun();
144 
145  delete fRunFile;
146 }
147 
148 
149 
150 
162 
164 {
165  // Perform analysis of chosen runs
166  // Before beginning the loop over the runs, the user's InitAnalysis() method is called.
167  // After completing the analysis of all runs, the user's EndAnalysis() method is called.
168  //
169  // Further customisation of the event loop is possible by overriding the methods
170  // preInitAnalysis()
171  // postInitAnalysis()
172  // preEndAnalysis()
173  // postEndAnalysis()
174  // which are executed respectively just before and just after the methods.
175 
176  if (gDataSet != GetDataSet()) GetDataSet()->cd();
177 
178  // prepare any user-supplied options for the analysis
179  fOptionList.ParseOptions(GetUserClassOptions());
180 
181  preInitAnalysis();
182  //call user's initialisation
183  InitAnalysis();
184  postInitAnalysis();
185 
186  CalculateTotalEventsToRead();
187 
188  //loop over runs
189  for (auto& run : GetRunList()) {
190  if (AbortProcessingLoop()) break;
191  fRunNumber = run;
192  fRustines.InitializePatchList(gDataSet->GetName(), fRunNumber);
193  fRustines.Print();
194  ProcessRun();
195  }
196 
197  if (fCombinedOutputFile != "") {
198  Info("Terminate", "combine = %s", fCombinedOutputFile.Data());
199  // combine histograms and trees from analysis into one file
200  TString file1, file2;
201  file1.Form("HistoFileFrom%s.root", ClassName());
202  file2.Form("TreeFileFrom%s.root", ClassName());
203  if (fHistoList.GetEntries()) {
204  if (fTreeFile) {
205  Info("Terminate", "both");
206  SaveHistos();
207  fTreeFile->Write();
208  delete fTreeFile;
209  KVBase::CombineFiles(file1, file2, fCombinedOutputFile, kFALSE);
210  }
211  else {
212  // no trees - just rename histo file
213  Info("Terminate", "histo");
214  SaveHistos(fCombinedOutputFile);
215  }
216  }
217  else if (fTreeFile) {
218  // no histos - just rename tree file
219  Info("Terminate", "tree");
220  fTreeFile->Write();
221  delete fTreeFile;
222  gSystem->Rename(file2, fCombinedOutputFile);
223  }
224  else Info("Terminate", "none");
225  }
226  else {
227  SaveHistos();
228  if (fTreeFile) {
229  fTreeFile->Write();
230  delete fTreeFile;
231  }
232  }
233 
234  preEndAnalysis();
235  //call user's end of analysis
236  EndAnalysis();
237  postEndAnalysis();
238 }
239 
240 
241 
242 
245 
247 {
248  //loop over runs and calculate total events
249  TotalEntriesToRead = 0;
250  for (auto& r : GetRunList()) {
251  TotalEntriesToRead += gExpDB->GetDBRunFile(r).GetEvents();
252  }
253 }
254 
255 
256 
261 
263 {
264  // Add a user analysis results TTree to list which will be automatically
265  // written to disk at end of analysis.
266  // User must call CreateTreeFile(const Char_t*) before calling this method
267 
268  if (!fTreeFile) {
269  Error("AddTree", "You must call CreateTreeFile(const Char_t*) before using this method!");
270  return;
271  }
272  tree->SetDirectory(fTreeFile);
273  tree->AutoSave();
274  fTreeList.Add(tree);
275 }
276 
277 
278 
284 
285 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, Double_t one, Double_t two, Double_t three, Double_t four)
286 {
287  //Find in the list, if there is an histogram named "histo_name"
288  //If not print an error message
289  //If yes redirect to the right method according to its closest mother class
290  //to fill it
291 
292  TH1* h1 = 0;
293  if ((h1 = GetHisto(histo_name))) {
294  if (h1->InheritsFrom("TH3"))
295  FillTH3((TH3*)h1, one, two, three, four);
296  else if (h1->InheritsFrom("TProfile2D"))
297  FillTProfile2D((TProfile2D*)h1, one, two, three, four);
298  else if (h1->InheritsFrom("TH2"))
299  FillTH2((TH2*)h1, one, two, three);
300  else if (h1->InheritsFrom("TProfile"))
301  FillTProfile((TProfile*)h1, one, two, three);
302  else if (h1->InheritsFrom("TH1"))
303  FillTH1(h1, one, two);
304  else
305  Warning("FillHisto", "%s -> Classe non prevue ...", fHistoList.FindObject(histo_name)->ClassName());
306  }
307  else {
308  Warning("FillHisto", "%s introuvable", histo_name);
309  }
310 }
311 
312 
313 
316 
317 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, const Char_t* label, Double_t weight)
318 {
319  // Fill 1D histogram with named bins
320  TH1* h1 = 0;
321  if ((h1 = GetHisto(histo_name))) {
322  h1->Fill(label, weight);
323  }
324  else {
325  Warning("FillHisto", "%s introuvable", histo_name);
326  }
327 }
328 
329 
330 
337 
338 void KVRawDataAnalyser::FillTree(const Char_t* tree_name)
339 {
340  //Filltree method, the tree named tree_name
341  //has to be declared with AddTTree(TTree*) method
342  //
343  //if no sname="", all trees in the list is filled
344  //
345  if (!strcmp(tree_name, "")) {
346  fTreeList.Execute("Fill", "");
347  }
348  else {
349  TTree* tt = 0;
350  if ((tt = GetTree(tree_name))) {
351  tt->Fill();
352  }
353  else {
354  Warning("FillTree", "%s introuvable", tree_name);
355  }
356  }
357 }
358 
359 
360 
374 
375 void KVRawDataAnalyser::SaveHistos(const Char_t* filename, Option_t* option, Bool_t onlyfilled)
376 {
377  // Write in file all histograms declared with AddHisto(TH1*)
378  //
379  // If no filename is specified, set default name : HistoFileFrom[name_of_class].root
380  //
381  // If a filename is specified, search in gROOT->GetListOfFiles() if
382  // this file has been already opened
383  // - if yes write in it
384  // - if not, create it with the corresponding option, write in it
385  // and close it just after
386  //
387  // onlyfilled flag allow to write all (onlyfilled=kFALSE, default)
388  // or only histograms (onlyfilled=kTRUE) those have been filled
389 
390  if (!fHistoList.GetEntries()) return;
391 
392  TString histo_file_name = "";
393  if (!strcmp(filename, ""))
394  histo_file_name.Form("HistoFileFrom%s.root", ClassName());
395  else
396  histo_file_name = filename;
397 
398  Bool_t justopened = kFALSE;
399 
400  TFile* file = 0;
401  TDirectory* pwd = gDirectory;
402  //if filename correspond to an already opened file, write in it
403  //if not open/create it, depending on the option ("recreate" by default)
404  //and write in it
405  if (!(file = (TFile*)gROOT->GetListOfFiles()->FindObject(histo_file_name.Data()))) {
406  file = new TFile(histo_file_name.Data(), option);
407  justopened = kTRUE;
408  }
409  file->cd();
410  TIter next(GetHistoList());
411  TObject* obj = 0;
412  while ((obj = next())) {
413  if (obj->InheritsFrom("TH1")) {
414  if (onlyfilled) {
415  if (((TH1*)obj)->GetEntries() > 0) {
416  obj->Write();
417  }
418  }
419  else {
420  obj->Write();
421  }
422  }
423  }
424  if (justopened)
425  file->Close();
426  pwd->cd();
427 }
428 
429 
430 
434 
436 {
437  // This method must be called before creating any user TTree in InitAnalysis().
438  // If no filename is given, default name="TreeFileFrom[name of analysis class].root"
439 
440  TString tree_file_name;
441  if (!strcmp(filename, ""))
442  tree_file_name.Form("TreeFileFrom%s.root", ClassName());
443  else
444  tree_file_name = filename;
445 
446  TDirectory* savedir = gDirectory;
447  fTreeFile = new TFile(tree_file_name, "RECREATE");
448  savedir->cd();
449 
450  return kTRUE;
451 }
452 
453 
454 
455 
458 
459 void KVRawDataAnalyser::Make(const Char_t* kvsname)
460 {
461  //Automatic generation of derived class for raw data analysis
462 
463  KVClassFactory cf(kvsname, "Analysis of raw data", "",
464  kTRUE, "RawAnalysisTemplate");
465 
466  cf.AddImplIncludeFile("KVMultiDetArray.h");
467  cf.GenerateCode();
468 }
469 
470 
471 
474 
476 {
477  // Method called to abort analysis during processing of a run
478 }
479 
480 
ROOT::R::TRInterface & r
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
#define gDirectory
Option_t Option_t option
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
#define gROOT
R__EXTERN TSystem * gSystem
static void CombineFiles(const Char_t *file1, const Char_t *file2, const Char_t *newfilename, Bool_t keep=kTRUE)
Definition: KVBase.cpp:1495
Factory class for generating skeleton files for new classes.
void GenerateCode()
Generate header and implementation file for currently-defined class.
void AddImplIncludeFile(const Char_t *filename)
ULong64_t GetEvents() const
Definition: KVDBRunFile.h:148
TString GetFullPathToRunfile(const KVString &type, const run_index_t &run) const
Definition: KVDataSet.cpp:931
FileType * OpenRunfile(const KVString &type, const run_index_t &run)
Definition: KVDataSet.h:164
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:89
const KVDBRunFile & GetDBRunFile(const run_index_t &r) const
Definition: KVExpDB.h:93
virtual void InitialiseRawDataReading(KVRawDataReader *)
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray")
virtual Bool_t HandleRawDataEvent(KVRawDataReader *)
Abstract base class for user analysis of raw data.
void SubmitTask() override
virtual void SaveHistos(const Char_t *filename="", Option_t *option="recreate", Bool_t onlyfilled=kFALSE)
Bool_t CreateTreeFile(const Char_t *filename="")
void FillTree(const Char_t *sname="")
void FillHisto(const Char_t *sname, Double_t one, Double_t two=1, Double_t three=1, Double_t four=1)
virtual ~KVRawDataAnalyser()
Destructor.
static void Make(const Char_t *kvsname="MyOwnRawDataAnalyser")
Automatic generation of derived class for raw data analysis.
void CalculateTotalEventsToRead()
loop over runs and calculate total events
void AddTree(TTree *tree)
void AbortDuringRunProcessing()
Method called to abort analysis during processing of a run.
Abstract base class for reading raw (DAQ) data.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
const char * AsString() const
virtual Bool_t cd()
virtual Int_t Fill(const char *name, Double_t w)
const char * GetName() const override
virtual Int_t Write(const char *name=nullptr, Int_t option=0, Int_t bufsize=0)
virtual Bool_t InheritsFrom(const char *classname) const
const char * Data() const
void Form(const char *fmt,...)
virtual int Rename(const char *from, const char *to)
long long Long64_t
RooCmdArg ClassName(const char *name)
TH1F * h1
str tree_name
BinData::ErrorType GetDataType(const TGraph *gr, DataOptions &fitOpt)
void Error(const char *location, const char *fmt,...)
void Info(const char *location, const char *fmt,...)
void Warning(const char *location, const char *fmt,...)
auto * tt
ClassImp(TPyArg)