KaliVeda
Toolkit for HIC analysis
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 
65 
67 {
68  // Perform treatment of a given runfile
69  //
70  // Before processing begins, after opening the runfile, and initialising the experimental
71  // setup (KVMultiDetArray object accessible through global pointer gMultiDetArray), the
72  // user's InitRun() method is called.
73  //
74  // After reading each runfile, user's EndRun() method is called.
75  //
76  // For each event of each runfile, user's Analysis() method is called just after calling
77  // gMultiDetArray->HandleRawDataEvent(fRunfile).
78  //
79  // In the Analysis() method the user can test gMultiDetArray->HandledRawData() to know if some pertinent data was found in
80  // the event or not.
81  //
82  // For further customisation, the following methods are called just before and just after
83  // each of these methods:
84  //
85  // - preInitRun()
86  // - postInitRun()
87  // - preAnalysis()
88  // - postAnalysis()
89  // - preEndRun()
90  // - postEndRun()
91 
92  KVString raw_file = gDataSet->GetFullPathToRunfile(GetDataType().Data(), fRunNumber);
93  fRunFile = gDataSet->OpenRunfile<KVRawDataReader>(GetDataType().Data(), fRunNumber);
94  if ((!fRunFile) || fRunFile->IsZombie()) {
95  //skip run if file cannot be opened
96  if (fRunFile) delete fRunFile;
97  return;
98  }
99 
100  //warning! real number of run may be different from that deduced from file name
101  //we get the real run number from the data and use it to name any new files
102  // Not possible for INDRAFAZIA MFM data (we use 'fake' run numbers)
103  // Is this still necessary? (which dataset was concerned? camp5?)
104 // Int_t newrun = fRunFile->GetRunNumberReadFromFile();
105 // if (newrun && newrun != fRunNumber) {
106 // cout << " *** WARNING *** run number read from file = " << newrun << endl;
107 // fRunNumber = newrun;
108 // }
109 
110  KVMultiDetArray::MakeMultiDetector(gDataSet->GetName(), fRunNumber.run());
111  fCurrentRun = gExpDB->GetDBRun(fRunNumber.run());
112 
113  // perform any initialisations which may be required, dependent on the
114  // format of the data, the multidetector, etc.
115  gMultiDetArray->InitialiseRawDataReading(fRunFile);
116 
117  fEventNumber = 1; //event number
118 
119  Long64_t nevents = GetNbEventToRead();
120  if (nevents <= 0) {
121  nevents = 1000000000;
122  cout << endl << "Reading all events from file " << raw_file.Data() << endl;
123  }
124  else {
125  cout << endl << "Reading " << nevents << " events from file " << raw_file.Data() << endl;
126  }
127 
128  cout << "Starting analysis of run " << fRunNumber << " on : ";
129  TDatime now;
130  cout << now.AsString() << endl << endl;
131 
132  fCurrentRun->Print();
133 
134  preInitRun();
135  //call user's beginning of run
136  InitRun();
137  postInitRun();
138 
139  //loop over events in file
140  while ((nevents-- ? fRunFile->GetNextEvent() : kFALSE) && !AbortProcessingLoop()) {
141 
142  gMultiDetArray->HandleRawDataEvent(fRunFile);
143 
144  // skip events that any active patches reject
145  if (fRustines.HasActivePatches())
146  if (fRustines.SkipEvent(gMultiDetArray))
147  continue;
148 
149  preAnalysis();
150  //call user's analysis. stop if returns kFALSE.
151  if (!Analysis()) break;
152  postAnalysis();
153 
154  if (CheckStatusUpdateInterval(fEventNumber)) DoStatusUpdate(fEventNumber);
155 
156  fEventNumber += 1;
157  }
158 
159  cout << "Ending analysis of run " << fRunNumber << " on : ";
160  TDatime now2;
161  cout << now2.AsString() << endl << endl;
162  cout << endl << "Finished reading " << fEventNumber - 1 << " events from file " << raw_file.Data() << endl << endl;
163 
164  preEndRun();
165  //call user's end of run function
166  EndRun();
167  postEndRun();
168 
169  delete fRunFile;
170 }
171 
172 
173 
174 
189 
191 {
192  // Perform analysis of chosen runs
193  //
194  // Before beginning the loop over the runs, the user's InitAnalysis() method is called.
195  // After completing the analysis of all runs, the user's EndAnalysis() method is called.
196  //
197  // Further customisation of the event loop is possible by overriding the methods
198  //
199  // - preInitAnalysis()
200  // - postInitAnalysis()
201  // - preEndAnalysis()
202  // - postEndAnalysis()
203  //
204  // which are executed respectively just before and just after those methods.
205 
206  if (gDataSet != GetDataSet()) GetDataSet()->cd();
207 
208  // prepare any user-supplied options for the analysis
209  fOptionList.ParseOptions(GetUserClassOptions());
210 
211  preInitAnalysis();
212  //call user's initialisation
213  InitAnalysis();
214  postInitAnalysis();
215 
216  CalculateTotalEventsToRead();
217 
218  //loop over runs
219  for (auto& run : GetRunList()) {
220  if (AbortProcessingLoop()) break;
221  fRunNumber = run;
222  fRustines.InitializePatchList(gDataSet->GetName(), fRunNumber);
223  fRustines.Print();
224  ProcessRun();
225  }
226 
227  if (fCombinedOutputFile != "") {
228  Info("Terminate", "combine = %s", fCombinedOutputFile.Data());
229  // combine histograms and trees from analysis into one file
230  TString file1, file2;
231  file1.Form("HistoFileFrom%s.root", ClassName());
232  file2.Form("TreeFileFrom%s.root", ClassName());
233  if (fHistoList.GetEntries()) {
234  if (fTreeFile) {
235  Info("Terminate", "both");
236  SaveHistos();
237  fTreeFile->Write();
238  delete fTreeFile;
239  KVBase::CombineFiles(file1, file2, fCombinedOutputFile, kFALSE);
240  }
241  else {
242  // no trees - just rename histo file
243  Info("Terminate", "histo");
244  SaveHistos(fCombinedOutputFile);
245  }
246  }
247  else if (fTreeFile) {
248  // no histos - just rename tree file
249  Info("Terminate", "tree");
250  fTreeFile->Write();
251  delete fTreeFile;
252  gSystem->Rename(file2, fCombinedOutputFile);
253  }
254  else Info("Terminate", "none");
255  }
256  else {
257  SaveHistos();
258  if (fTreeFile) {
259  fTreeFile->Write();
260  delete fTreeFile;
261  }
262  }
263 
264  preEndAnalysis();
265  //call user's end of analysis
266  EndAnalysis();
267  postEndAnalysis();
268 }
269 
270 
271 
272 
275 
277 {
278  //loop over runs and calculate total events
279  TotalEntriesToRead = 0;
280  for (auto& r : GetRunList()) {
281  TotalEntriesToRead += gExpDB->GetDBRunFile(r).GetEvents();
282  }
283 }
284 
285 
286 
287 
288 
294 
295 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, Double_t one, Double_t two, Double_t three, Double_t four)
296 {
297  //Find in the list, if there is an histogram named "histo_name"
298  //If not print an error message
299  //If yes redirect to the right method according to its closest mother class
300  //to fill it
301 
302  TH1* h1 = 0;
303  if ((h1 = GetHisto(histo_name))) {
304  if (h1->InheritsFrom("TH3"))
305  FillTH3((TH3*)h1, one, two, three, four);
306  else if (h1->InheritsFrom("TProfile2D"))
307  FillTProfile2D((TProfile2D*)h1, one, two, three, four);
308  else if (h1->InheritsFrom("TH2"))
309  FillTH2((TH2*)h1, one, two, three);
310  else if (h1->InheritsFrom("TProfile"))
311  FillTProfile((TProfile*)h1, one, two, three);
312  else if (h1->InheritsFrom("TH1"))
313  FillTH1(h1, one, two);
314  else
315  Warning("FillHisto", "%s -> Classe non prevue ...", fHistoList.FindObject(histo_name)->ClassName());
316  }
317  else {
318  Warning("FillHisto", "%s introuvable", histo_name);
319  }
320 }
321 
322 
323 
326 
327 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, const Char_t* label, Double_t weight)
328 {
329  // Fill 1D histogram with named bins
330  TH1* h1 = 0;
331  if ((h1 = GetHisto(histo_name))) {
332  h1->Fill(label, weight);
333  }
334  else {
335  Warning("FillHisto", "%s introuvable", histo_name);
336  }
337 }
338 
339 
340 
347 
348 void KVRawDataAnalyser::FillTree(const Char_t* tree_name)
349 {
350  //Filltree method, the tree named tree_name
351  //has to be declared with AddTTree(TTree*) method
352  //
353  //if no sname="", all trees in the list is filled
354  //
355  if (!strcmp(tree_name, "")) {
356  fTreeList.Execute("Fill", "");
357  }
358  else {
359  TTree* tt = 0;
360  if ((tt = GetTree(tree_name))) {
361  tt->Fill();
362  }
363  else {
364  Warning("FillTree", "%s introuvable", tree_name);
365  }
366  }
367 }
368 
369 
370 
384 
385 void KVRawDataAnalyser::SaveHistos(const Char_t* filename, Option_t* option, Bool_t onlyfilled)
386 {
387  // Write in file all histograms declared with AddHisto(TH1*)
388  //
389  // If no filename is specified, set default name : HistoFileFrom[name_of_class].root
390  //
391  // If a filename is specified, search in gROOT->GetListOfFiles() if
392  // this file has been already opened
393  // - if yes write in it
394  // - if not, create it with the corresponding option, write in it
395  // and close it just after
396  //
397  // onlyfilled flag allow to write all (onlyfilled=kFALSE, default)
398  // or only histograms (onlyfilled=kTRUE) those have been filled
399 
400  if (!fHistoList.GetEntries()) return;
401 
402  TString histo_file_name = "";
403  if (!strcmp(filename, ""))
404  histo_file_name.Form("HistoFileFrom%s.root", ClassName());
405  else
406  histo_file_name = filename;
407 
408  Bool_t justopened = kFALSE;
409 
410  TFile* file = 0;
411  TDirectory* pwd = gDirectory;
412  //if filename correspond to an already opened file, write in it
413  //if not open/create it, depending on the option ("recreate" by default)
414  //and write in it
415  if (!(file = (TFile*)gROOT->GetListOfFiles()->FindObject(histo_file_name.Data()))) {
416  file = new TFile(histo_file_name.Data(), option);
417  justopened = kTRUE;
418  }
419  file->cd();
420  TIter next(GetHistoList());
421  TObject* obj = 0;
422  while ((obj = next())) {
423  if (obj->InheritsFrom("TH1")) {
424  if (onlyfilled) {
425  if (((TH1*)obj)->GetEntries() > 0) {
426  obj->Write();
427  }
428  }
429  else {
430  obj->Write();
431  }
432  }
433  }
434  if (justopened)
435  file->Close();
436  pwd->cd();
437 }
438 
439 
440 
444 
446 {
447  // This method must be called before creating any user TTree in InitAnalysis().
448  // If no filename is given, default name="TreeFileFrom[name of analysis class].root"
449 
450  TString tree_file_name;
451  if (!strcmp(filename, ""))
452  tree_file_name.Form("TreeFileFrom%s.root", ClassName());
453  else
454  tree_file_name = filename;
455 
456  TDirectory* savedir = gDirectory;
457  fTreeFile = new TFile(tree_file_name, "RECREATE");
458  savedir->cd();
459 
460  return kTRUE;
461 }
462 
463 
464 
465 
468 
469 void KVRawDataAnalyser::Make(const Char_t* kvsname)
470 {
471  //Automatic generation of derived class for raw data analysis
472 
473  KVClassFactory cf(kvsname, "Analysis of raw data", "",
474  kTRUE, "RawAnalysisTemplate");
475 
476  cf.AddImplIncludeFile("KVMultiDetArray.h");
477  cf.GenerateCode();
478 }
479 
480 
481 
484 
486 {
487  // Method called to abort analysis during processing of a run
488 }
489 
490 
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:149
TString GetFullPathToRunfile(const KVString &type, const run_index_t &run) const
Definition: KVDataSet.cpp:929
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
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray", KVExpDB *db=nullptr)
virtual void InitialiseRawDataReading(KVRawDataReader *)
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 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 Info(const char *location, const char *fmt,...)
void Warning(const char *location, const char *fmt,...)
auto * tt
ClassImp(TPyArg)