KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVRawDataAnalyser.cpp
Go to the documentation of this file.
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 
87  fCurrentRun = gExpDB->GetDBRun(fRunNumber);
88 
89  fEventNumber = 1; //event number
90 
91  Long64_t nevents = GetNbEventToRead();
92  if (nevents <= 0) {
93  nevents = 1000000000;
94  cout << endl << "Reading all events from file " << raw_file.Data() << endl;
95  }
96  else {
97  cout << endl << "Reading " << nevents << " events from file " << raw_file.Data() << endl;
98  }
99 
100  cout << "Starting analysis of run " << fRunNumber << " on : ";
101  TDatime now;
102  cout << now.AsString() << endl << endl;
103 
104  fCurrentRun->Print();
105 
106  preInitRun();
107  //call user's beginning of run
108  InitRun();
109  postInitRun();
110 
111  //loop over events in file
112  while ((nevents-- ? fRunFile->GetNextEvent() : kFALSE) && !AbortProcessingLoop()) {
113 
115  preAnalysis();
116  //call user's analysis. stop if returns kFALSE.
117  if (!Analysis()) break;
118  postAnalysis();
119 
120  if (CheckStatusUpdateInterval(fEventNumber)) DoStatusUpdate(fEventNumber);
121 
122  fEventNumber += 1;
123  }
124 
125  cout << "Ending analysis of run " << fRunNumber << " on : ";
126  TDatime now2;
127  cout << now2.AsString() << endl << endl;
128  cout << endl << "Finished reading " << fEventNumber - 1 << " events from file " << raw_file.Data() << endl << endl;
129 
130  preEndRun();
131  //call user's end of run function
132  EndRun();
133  postEndRun();
134 
135  delete fRunFile;
136 }
137 
138 
139 
140 
152 
154 {
155  // Perform analysis of chosen runs
156  // Before beginning the loop over the runs, the user's InitAnalysis() method is called.
157  // After completing the analysis of all runs, the user's EndAnalysis() method is called.
158  //
159  // Further customisation of the event loop is possible by overriding the methods
160  // preInitAnalysis()
161  // postInitAnalysis()
162  // preEndAnalysis()
163  // postEndAnalysis()
164  // which are executed respectively just before and just after the methods.
165 
166  if (gDataSet != GetDataSet()) GetDataSet()->cd();
167 
168  preInitAnalysis();
169  //call user's initialisation
170  InitAnalysis();
171  postInitAnalysis();
172 
173  CalculateTotalEventsToRead();
174 
175  //loop over runs
176  GetRunList().Begin();
177  while (!GetRunList().End() && !AbortProcessingLoop()) {
178  fRunNumber = GetRunList().Next();
179  ProcessRun();
180  }
181 
182  if (fCombinedOutputFile != "") {
183  Info("Terminate", "combine = %s", fCombinedOutputFile.Data());
184  // combine histograms and trees from analysis into one file
185  TString file1, file2;
186  file1.Form("HistoFileFrom%s.root", ClassName());
187  file2.Form("TreeFileFrom%s.root", ClassName());
188  if (fHistoList.GetEntries()) {
189  if (fTreeFile) {
190  Info("Terminate", "both");
191  SaveHistos();
192  fTreeFile->Write();
193  delete fTreeFile;
194  KVBase::CombineFiles(file1, file2, fCombinedOutputFile, kFALSE);
195  }
196  else {
197  // no trees - just rename histo file
198  Info("Terminate", "histo");
199  SaveHistos(fCombinedOutputFile);
200  }
201  }
202  else if (fTreeFile) {
203  // no histos - just rename tree file
204  Info("Terminate", "tree");
205  fTreeFile->Write();
206  delete fTreeFile;
207  gSystem->Rename(file2, fCombinedOutputFile);
208  }
209  else Info("Terminate", "none");
210  }
211  else {
212  SaveHistos();
213  if (fTreeFile) {
214  fTreeFile->Write();
215  delete fTreeFile;
216  }
217  }
218 
219  preEndAnalysis();
220  //call user's end of analysis
221  EndAnalysis();
222  postEndAnalysis();
223 }
224 
225 
226 
227 
230 
232 {
233  //loop over runs and calculate total events
234  TotalEntriesToRead = 0;
235  GetRunList().Begin();
236  while (!GetRunList().End()) {
237  Int_t r = GetRunList().Next();
238  TotalEntriesToRead += gExpDB->GetDBRun(r)->GetEvents();
239  }
240 }
241 
242 
243 
248 
250 {
251  // Add a user analysis results TTree to list which will be automatically
252  // written to disk at end of analysis.
253  // User must call CreateTreeFile(const Char_t*) before calling this method
254 
255  if (!fTreeFile) {
256  Error("AddTree", "You must call CreateTreeFile(const Char_t*) before using this method!");
257  return;
258  }
259  tree->SetDirectory(fTreeFile);
260  tree->AutoSave();
261  fTreeList.Add(tree);
262 }
263 
264 
265 
271 
272 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, Double_t one, Double_t two, Double_t three, Double_t four)
273 {
274  //Find in the list, if there is an histogram named "histo_name"
275  //If not print an error message
276  //If yes redirect to the right method according to its closest mother class
277  //to fill it
278 
279  TH1* h1 = 0;
280  if ((h1 = GetHisto(histo_name))) {
281  if (h1->InheritsFrom("TH3"))
282  FillTH3((TH3*)h1, one, two, three, four);
283  else if (h1->InheritsFrom("TProfile2D"))
284  FillTProfile2D((TProfile2D*)h1, one, two, three, four);
285  else if (h1->InheritsFrom("TH2"))
286  FillTH2((TH2*)h1, one, two, three);
287  else if (h1->InheritsFrom("TProfile"))
288  FillTProfile((TProfile*)h1, one, two, three);
289  else if (h1->InheritsFrom("TH1"))
290  FillTH1(h1, one, two);
291  else
292  Warning("FillHisto", "%s -> Classe non prevue ...", fHistoList.FindObject(histo_name)->ClassName());
293  }
294  else {
295  Warning("FillHisto", "%s introuvable", histo_name);
296  }
297 }
298 
299 
300 
303 
304 void KVRawDataAnalyser::FillHisto(const Char_t* histo_name, const Char_t* label, Double_t weight)
305 {
306  // Fill 1D histogram with named bins
307  TH1* h1 = 0;
308  if ((h1 = GetHisto(histo_name))) {
309  h1->Fill(label, weight);
310  }
311  else {
312  Warning("FillHisto", "%s introuvable", histo_name);
313  }
314 }
315 
316 
317 
324 
325 void KVRawDataAnalyser::FillTree(const Char_t* tree_name)
326 {
327  //Filltree method, the tree named tree_name
328  //has to be declared with AddTTree(TTree*) method
329  //
330  //if no sname="", all trees in the list is filled
331  //
332  if (!strcmp(tree_name, "")) {
333  fTreeList.Execute("Fill", "");
334  }
335  else {
336  TTree* tt = 0;
337  if ((tt = GetTree(tree_name))) {
338  tt->Fill();
339  }
340  else {
341  Warning("FillTree", "%s introuvable", tree_name);
342  }
343  }
344 }
345 
346 
347 
361 
362 void KVRawDataAnalyser::SaveHistos(const Char_t* filename, Option_t* option, Bool_t onlyfilled)
363 {
364  // Write in file all histograms declared with AddHisto(TH1*)
365  //
366  // If no filename is specified, set default name : HistoFileFrom[name_of_class].root
367  //
368  // If a filename is specified, search in gROOT->GetListOfFiles() if
369  // this file has been already opened
370  // - if yes write in it
371  // - if not, create it with the corresponding option, write in it
372  // and close it just after
373  //
374  // onlyfilled flag allow to write all (onlyfilled=kFALSE, default)
375  // or only histograms (onlyfilled=kTRUE) those have been filled
376 
377  if (!fHistoList.GetEntries()) return;
378 
379  TString histo_file_name = "";
380  if (!strcmp(filename, ""))
381  histo_file_name.Form("HistoFileFrom%s.root", ClassName());
382  else
383  histo_file_name = filename;
384 
385  Bool_t justopened = kFALSE;
386 
387  TFile* file = 0;
388  TDirectory* pwd = gDirectory;
389  //if filename correspond to an already opened file, write in it
390  //if not open/create it, depending on the option ("recreate" by default)
391  //and write in it
392  if (!(file = (TFile*)gROOT->GetListOfFiles()->FindObject(histo_file_name.Data()))) {
393  file = new TFile(histo_file_name.Data(), option);
394  justopened = kTRUE;
395  }
396  file->cd();
397  TIter next(GetHistoList());
398  TObject* obj = 0;
399  while ((obj = next())) {
400  if (obj->InheritsFrom("TH1")) {
401  if (onlyfilled) {
402  if (((TH1*)obj)->GetEntries() > 0) {
403  obj->Write();
404  }
405  }
406  else {
407  obj->Write();
408  }
409  }
410  }
411  if (justopened)
412  file->Close();
413  pwd->cd();
414 }
415 
416 
417 
421 
423 {
424  // This method must be called before creating any user TTree in InitAnalysis().
425  // If no filename is given, default name="TreeFileFrom[name of analysis class].root"
426 
427  TString tree_file_name;
428  if (!strcmp(filename, ""))
429  tree_file_name.Form("TreeFileFrom%s.root", ClassName());
430  else
431  tree_file_name = filename;
432 
433  TDirectory* savedir = gDirectory;
434  fTreeFile = new TFile(tree_file_name, "RECREATE");
435  savedir->cd();
436 
437  return kTRUE;
438 }
439 
440 
441 
442 
445 
446 void KVRawDataAnalyser::Make(const Char_t* kvsname)
447 {
448  //Automatic generation of derived class for raw data analysis
449 
450  KVClassFactory cf(kvsname, "Analysis of raw data", "",
451  kTRUE, "RawAnalysisTemplate");
452 
453  cf.AddImplIncludeFile("KVMultiDetArray.h");
454  cf.GenerateCode();
455 }
456 
457 
458 
461 
463 {
464  // Method called to abort analysis during processing of a run
465 }
466 
467 
int Int_t
KVDataSet * gDataSet
Definition: KVDataSet.cpp:30
KVExpDB * gExpDB
Definition: KVExpDB.cpp:12
KVMultiDetArray * gMultiDetArray
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
ROOT::R::TRInterface & r
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
const char Option_t
#define gDirectory
#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:1490
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: KVDBRun.h:133
FileType * OpenRunfile(const Char_t *type, Int_t run)
Definition: KVDataSet.h:333
TString GetFullPathToRunfile(const Char_t *type, Int_t run) const
Definition: KVDataSet.cpp:887
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:74
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray")
Bool_t HandleRawDataEvent(KVRawDataReader *)
Abstract base class for user analysis of raw data.
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:72
const char * AsString() const
virtual Bool_t cd(const char *path=nullptr)
virtual Int_t Fill(const char *name, Double_t w)
virtual const char * GetName() const
virtual Int_t Write(const char *name=0, 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
BinData::ErrorType GetDataType(const TGraph *gr, DataOptions &fitOpt)
void Info(const char *location, const char *va_(fmt),...)
void Error(const char *location, const char *va_(fmt),...)
void Warning(const char *location, const char *va_(fmt),...)
void End()
TH1 * GetHisto(const std::string file, const std::string path, const std::string obj)
auto * tt