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 
66 void KVRawDataAnalyser::ProcessRun()
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 (GetHistoList().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 
300 
301 void KVRawDataAnalyser::SaveHistos(const Char_t* filename, Option_t* option, Bool_t onlyfilled)
302 {
303  // Write in file all histograms declared with AddHisto(TH1*)
304  //
305  // If no filename is specified, set default name : HistoFileFrom[name_of_class].root
306  //
307  // If a filename is specified, search in gROOT->GetListOfFiles() if
308  // this file has been already opened
309  // - if yes write in it
310  // - if not, create it with the corresponding option, write in it
311  // and close it just after
312  //
313  // onlyfilled flag allow to write all (onlyfilled=kFALSE, default)
314  // or only histograms (onlyfilled=kTRUE) those have been filled
315 
316  if (!GetHistoList().GetEntries()) return;
317 
318  TString histo_file_name = "";
319  if (!strcmp(filename, ""))
320  histo_file_name.Form("HistoFileFrom%s.root", ClassName());
321  else
322  histo_file_name = filename;
323 
324  Bool_t justopened = kFALSE;
325 
326  TFile* file = 0;
327  TDirectory* pwd = gDirectory;
328  //if filename correspond to an already opened file, write in it
329  //if not open/create it, depending on the option ("recreate" by default)
330  //and write in it
331  if (!(file = (TFile*)gROOT->GetListOfFiles()->FindObject(histo_file_name.Data()))) {
332  file = new TFile(histo_file_name.Data(), option);
333  justopened = kTRUE;
334  }
335  file->cd();
336  TIter next(&GetHistoList());
337  TObject* obj = 0;
338  while ((obj = next())) {
339  if (obj->InheritsFrom("TH1")) {
340  if (onlyfilled) {
341  if (((TH1*)obj)->GetEntries() > 0) {
342  obj->Write();
343  }
344  }
345  else {
346  obj->Write();
347  }
348  }
349  }
350  if (justopened)
351  file->Close();
352  pwd->cd();
353 }
354 
355 
356 
360 
361 Bool_t KVRawDataAnalyser::CreateTreeFile(const Char_t* filename)
362 {
363  // This method must be called before creating any user TTree in InitAnalysis().
364  // If no filename is given, default name="TreeFileFrom[name of analysis class].root"
365 
366  if(fTreeFile)
367  return kFALSE;
368 
369  TString tree_file_name;
370  if (!strcmp(filename, ""))
371  tree_file_name.Form("TreeFileFrom%s.root", ClassName());
372  else
373  tree_file_name = filename;
374 
375  TDirectory* savedir = gDirectory;
376  fTreeFile = new TFile(tree_file_name, "RECREATE");
377  savedir->cd();
378 
379  return kTRUE;
380 }
381 
382 
383 
384 
387 
388 void KVRawDataAnalyser::Make(const Char_t* kvsname)
389 {
390  //Automatic generation of derived class for raw data analysis
391 
392  KVClassFactory cf(kvsname, "Analysis of raw data", "",
393  kTRUE, "RawAnalysisTemplate");
394 
395  cf.AddImplIncludeFile("KVMultiDetArray.h");
396  cf.GenerateCode();
397 }
398 
399 
400 
403 
404 void KVRawDataAnalyser::AbortDuringRunProcessing()
405 {
406  // Method called to abort analysis during processing of a run
407 }
408 
409 
ROOT::R::TRInterface & r
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
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:1518
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:157
TString GetFullPathToRunfile(const KVString &type, const run_index_t &run) const
Definition: KVDataSet.cpp:885
FileType * OpenRunfile(const KVString &type, const run_index_t &run)
Definition: KVDataSet.h:285
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:144
const KVDBRunFile & GetDBRunFile(const run_index_t &r) const
Definition: KVExpDB.h:148
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)
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
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()
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)
BinData::ErrorType GetDataType(const TGraph *gr, DataOptions &fitOpt)
void Info(const char *location, const char *fmt,...)
ClassImp(TPyArg)