KaliVeda
Toolkit for HIC analysis
KVINDRAReconDataAnalyser.cpp
1 /*
2 $Id: KVINDRAReconDataAnalyser.cpp,v 1.4 2007/11/15 14:59:45 franklan Exp $
3 $Revision: 1.4 $
4 $Date: 2007/11/15 14:59:45 $
5 */
6 
7 //Created by KVClassFactory on Wed Apr 5 23:50:04 2006
8 //Author: John Frankland
9 
10 #include "KVBase.h"
11 #include "KVINDRAReconDataAnalyser.h"
12 #include "KVINDRADBRun.h"
13 #include "KVINDRADB.h"
14 #include "KVDataAnalysisTask.h"
15 #include "KVDataSet.h"
16 #include "TChain.h"
17 #include "TObjString.h"
18 #include "TChain.h"
19 #include "KVAvailableRunsFile.h"
20 #include "KVINDRA.h"
21 #ifdef WITH_PROOF
22 #include "TProof.h"
23 #endif
24 #include "KVINDRATriggerConditions.h"
25 #include "KVINDRAEventSelector.h"
26 
27 #include <KVReconEventSelector.h>
28 
29 using namespace std;
30 
32 
33 
34 
37  : fSelector(nullptr), fOldSelector(nullptr), theChain(nullptr),
38  theRawData(nullptr), theGeneData(nullptr),
39  ParVal(nullptr), ParNum(nullptr), parList(nullptr)
40 {
41 }
42 
43 
44 
47 
49 {
50  //Reset task variables
52  theChain = nullptr;
53  theRawData = nullptr;
54  theGeneData = nullptr;
55  ParVal = nullptr;
56  ParNum = nullptr;
57  fSelector = nullptr;
58  fOldSelector = nullptr;
59  TotalEntriesToRead = 0;
60 }
61 
62 
63 
64 
65 
66 
69 
71 {
72  // Checks the task variables
73 
75 
76  cout << "============> Analysis summary <=============" << endl;
77  cout << "Analysis of runs " << GetRunList().
78  GetList() << " with the class ";
79  cout << "\"" << GetUserClassName() << "\"." << endl;
80  if (GetNbEventToRead()) {
81  cout << GetNbEventToRead() << " events will be processed." << endl;
82  }
83  else {
84  cout << "All events will be processed." << endl;
85  }
86  cout << "=============================================" << endl;
87 
88  return kTRUE;
89 }
90 
91 
92 
93 
98 
100 {
101  //Run the interactive analysis
102 
103  //make the chosen dataset the active dataset ( = gDataSet; note this also opens database
104  //and positions gDataBase & gIndraDB).
105  GetDataSet()->cd();
106  fSelector = nullptr;
107  fOldSelector = nullptr;
108 
109  theChain = new TChain("ReconstructedEvents");
110  theChain->SetDirectory(0); // we handle delete
111 
112  // open and add to TChain all required files
113  // we force the opening of the files to avoid problems with xrootd which sometimes
114  // seems to have a little difficulty
115  for (auto& run : GetRunList()) {
116  TString fullPathToRunfile = gDataSet->GetFullPathToRunfile(GetDataType(), run);
117  cout << "Opening file " << fullPathToRunfile << endl;
118  TFile* f = gDataSet->OpenRunfile<TFile>(GetDataType(), run);
119  cout << "Adding file " << fullPathToRunfile;
120  cout << " to the TChain." << endl;
121  dynamic_cast<TChain*>(theChain)->Add(fullPathToRunfile);
122  if (f && !f->IsZombie()) {
123  // update run infos in available runs file if necessary
125  if (ARF->InfosNeedUpdate(run, gSystem->BaseName(fullPathToRunfile))) {
126  if (!((TTree*)f->Get("ReconstructedEvents"))) {
127  Error("SubmitTask", "No tree named ReconstructedEvents is present in the current file");
128  delete theChain;
129  return;
130  }
131  TEnv* treeInfos = (TEnv*)((TTree*)f->Get("ReconstructedEvents"))->GetUserInfo()->FindObject("TEnv");
132  if (treeInfos) {
133  TString kvversion = treeInfos->GetValue("KVBase::GetKVVersion()", "");
134  TString username = treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "");
135  if (kvversion != "") ARF->UpdateInfos(run, gSystem->BaseName(fullPathToRunfile), kvversion, username);
136  }
137  else {
138  Info("SubmitTask", "No TEnv object associated to the tree");
139  }
140  }
141  }
142  }
143  TotalEntriesToRead = theChain->GetEntries();
144  TString option("");
145 
146  // Add the total run list in option
147  if (!(option.IsNull())) option += ",";
148  option += Form("FullRunList=%s", GetFullRunList().GetList().Data());
149 
150  // Add any user-defined options
151  if (GetUserClassOptions() != "") {
152  option += ",";
153  option += GetUserClassOptions();
154  }
155 
156  set_up_analyser_for_task(this);
157 
158  // for backwards compatibility, we allow user class to inherit from
159  // KVOldINDRASelector instead of KVINDRAEventSelector
160  // for forwards compatibility, we allow it to inherit from KVReconEventSelector too!
161  TObject* new_selector = GetInstanceOfUserClass("KVOldINDRASelector,KVReconEventSelector");
162 
163  if (!new_selector || !new_selector->InheritsFrom("TSelector")) {
164  cout << "The selector \"" << GetUserClassName() << "\" is not valid." << endl;
165  cout << "Process aborted." << endl;
166  SafeDelete(new_selector);
167  }
168  else {
169  SafeDelete(new_selector);
170  Info("SubmitTask", "Beginning TChain::Process...");
171 #ifdef WITH_CPP11
172  if (GetProofMode() != KVDataAnalyser::EProofMode::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
173 #else
174  if (GetProofMode() != KVDataAnalyser::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
175 #endif
176  TString analysis_class;
177  if (GetAnalysisTask()->WithUserClass()) analysis_class.Form("%s%s", GetUserClass().full_path_imp().Data(), GetACliCMode());
178  else analysis_class = GetUserClassName();
179 
180  if (GetNbEventToRead()) {
181  theChain->Process(analysis_class, option.Data(), GetNbEventToRead());
182  }
183  else {
184  theChain->Process(analysis_class, option.Data());
185  }
186  }
187  delete theChain;
188  fSelector = nullptr; //deleted by TChain/TTreePlayer
189  fOldSelector = nullptr; //deleted by TChain/TTreePlayer
190 }
191 
192 
193 
194 
200 
202 {
203  //Save (in the TEnv fBatchEnv) all necessary information on analysis task which can be used to execute it later
204  //(i.e. when batch processing system executes the job).
205  //If save=kTRUE (default), write the information in a file whose name is given by ".jobname"
206  //where 'jobname' is the name of the job as given to the batch system.
207 
209  // backwards-compatible fix for old KVSelector analysis classes
210  // for forwards compatibility, we allow it to inherit from KVReconEventSelector too!
211  GetBatchInfoFile()->SetValue("UserClassAlternativeBaseClass", "KVOldINDRASelector,KVReconEventSelector");
212  if (save) GetBatchInfoFile()->SaveLevel(kEnvUser);
213 }
214 
215 
216 
217 
227 
229 {
230  // Called by currently-processed KVEventSelector before user's InitAnalysis() method.
231  // We build the multidetector for the current dataset in case informations on
232  // detector are needed e.g. to define histograms in InitAnalysis().
233  // Note that at this stage we are not analysing a given run, so the parameters
234  // of the array are not set (they will be set in preInitRun()).
235  //
236  // Note for PROOF: as this will be called both on master and on slave workers,
237  // in order to reduce memory footprint we only build INDRA on the slaves
238 
239 #ifdef WITH_PROOF
240  if (!gProof || !gProof->IsMaster())
241 #endif
242  if (!gIndra) KVMultiDetArray::MakeMultiDetector(GetDataSet()->GetName());
243 
244  // in case data is being analysed with a "vanilla" KVReconEventSelector, we need
245  // to set the correct branch name for reconstructed INDRA data which is "INDRAReconEvent"
246  // instead of the default "ReconEvent"
247  if (fSelector) fSelector->SetBranchName("INDRAReconEvent");
248  else Warning("preInitAnalysis", "could not set branch name correctly");
249 }
250 
251 
252 
254 
256 {
257  if (fSelector) {
258  if (fSelector->InheritsFrom("KVINDRAEventSelector"))
259  dynamic_cast<KVINDRAEventSelector*>(fSelector)->SetCurrentRun(CurrentRun);
260  // WARNING - horrible kludge...
261  else if (fSelector->InheritsFrom("KVReconEventSelector"))
262  dynamic_cast<KVReconEventSelector*>(fSelector)->SetCurrentRun(CurrentRun);
263  }
264  else {
265  fOldSelector->SetCurrentRun(CurrentRun);
266  }
267 }
268 
269 
270 
286 
288 {
289  // Called by currently-processed TSelector when a new file in the TChain is opened.
290  // We call gIndra->SetParameters for the current run: whether only physics parameters will be set,
291  // or the full set of identification/calibration parameters depends on the value of the
292  // environment variable
293  //~~~
294  // [dataset].ReconAnalysis.WithCalibInfos: [yes/no]
295  //~~~
296  //This can be overridden for any individual analysis by setting the analysis class option
297  //~~~~
298  // WithCalibInfos=yes
299  //~~~~
300  // We connect the acquisition parameter objects to the branches of the raw data tree.
301  // Infos on currently read file/tree are printed.
302  // Any required data patches ("rustines") are initialized.
303 
304  Bool_t physics_parameters_only = !gDataSet->GetDataSetEnv("ReconAnalysis.WithCalibInfos", kTRUE);
305  if (fSelector->IsOptGiven("WithCalibInfos"))
306  physics_parameters_only = (fSelector->GetOpt("WithCalibInfos") != "yes");
307 
308  Int_t run = GetRunNumberFromFileName(theChain->GetCurrentFile()->GetName());
309  gIndra->SetParameters(run, physics_parameters_only);
310  KVINDRADBRun* CurrentRun = gIndraDB->GetRun(run);
311  SetCurrentRun(CurrentRun);
312  SetSelectorCurrentRun(CurrentRun);
313  cout << endl << " =================== New Run =================== " <<
314  endl << endl;
315 
316  CurrentRun->Print();
317  if (CurrentRun->GetSystem()) {
318  SetSystem(CurrentRun->GetSystem());
319  if (CurrentRun->GetSystem()->GetKinematics())
320  CurrentRun->GetSystem()->GetKinematics()->Print();
321  }
322 
323  cout << endl << " ================================================= " <<
324  endl << endl;
325 
326  ConnectRawDataTree();
327  ConnectGeneDataTree();
328  PrintTreeInfos();
329  Info("preInitRun", "Data written with series %s, release %d", GetDataSeries().Data(),
330  GetDataReleaseNumber());
331  fRustines.InitializePatchList(GetDataSet()->GetName(), GetDataType(), run, GetDataSeries(),
332  GetDataReleaseNumber(), theChain->GetCurrentFile()->GetStreamerInfoCache());
333  fRustines.Print();
334 }
335 
336 
337 
339 
341 {
342  Long64_t rawEntry = (fSelector ? fSelector->GetEventNumber() - 1
343  : fOldSelector->GetEventNumber() - 1);
344 
345  return rawEntry;
346 }
347 
348 
349 
351 
353 {
354  return (fSelector ? dynamic_cast<KVReconstructedEvent*>(fSelector->GetEvent()) : fOldSelector->GetEvent());
355 }
356 
357 
358 #ifdef USING_ROOT6
359 
361 
363 {
365  trig.SetTriggerConditionsForRun(fSelector, run);
366 }
367 
368 #endif
369 
370 
374 
376 {
377  // Read and set raw data for the current reconstructed event
378  // Any required data patches ("rustines") are applied.
379 
380  if (!theRawData) return;
381  // all recon events are numbered 1, 2, ... : therefore entry number is N-1
382  Long64_t rawEntry = GetRawEntryNumber();
383  theRawData->GetEntry(rawEntry);
385  for (int i = 0; i < NbParFired; i++)
386  gIndra->handle_ebyedat_raw_data_parameter((*parList)[ParNum[i]]->GetName(), ParVal[i]);
387 
388  // as rustines often depend on a knowledge of the original raw data,
389  // we apply them after it has been read in
390  KVINDRAReconEvent* event = (KVINDRAReconEvent*)GetReconstructedEvent();
391  if (fRustines.HasActivePatches()) fRustines.Apply(event);
392 }
393 
394 
395 
402 
404 {
405  // Called by preInitRun().
406  // When starting to read a new run (=new file), we look for the TTree "RawData" in the
407  // current file (it should have been created by KVINDRARawDataReconstructor).
408  // If found, it will be used by ReadRawData() to set the values of all acquisition parameters
409  // for each event.
410 
411  theRawData = (TTree*)theChain->GetCurrentFile()->Get("RawData");
412  if (!theRawData) {
413  Warning("ConnectRawDataTree", "RawData tree not found in file; raw data parameters of detectors will not be available in analysis");
414  return;
415  }
416  else
417  Info("ConnectRawDataTree", "Found RawData tree in file");
418  Int_t maxNopar = theRawData->GetMaximum("NbParFired");
419  if (ParVal) delete [] ParVal;
420  if (ParNum) delete [] ParNum;
421  ParVal = new UShort_t[maxNopar];
422  ParNum = new UInt_t[maxNopar];
423  parList = (TObjArray*)theRawData->GetUserInfo()->FindObject("ParameterList");
424  theRawData->SetBranchAddress("NbParFired", &NbParFired);
425  theRawData->SetBranchAddress("ParNum", ParNum);
426  theRawData->SetBranchAddress("ParVal", ParVal);
427  Info("ConnectRawDataTree", "Connected raw data parameters");
428  Entry = 0;
429 }
430 
431 
432 
437 
439 {
440  // Called by preInitRun().
441  // When starting to read a new run (=new file), we look for the TTree "GeneData" in the
442  // current file (it should have been created by KVINDRARawDataReconstructor).
443 
444  theGeneData = (TTree*)theChain->GetCurrentFile()->Get("GeneData");
445  if (!theGeneData) {
446  cout << " --> No pulser & laser data for this run !!!" << endl << endl;
447  }
448  else {
449  cout << " --> Pulser & laser data tree contains " << theGeneData->GetEntries()
450  << " events" << endl << endl;
451  }
452 }
453 
454 
455 
457 
459 {
460  return (TEnv*)theChain->GetTree()->GetUserInfo()->FindObject("TEnv");
461 }
462 
463 
464 
467 
469 {
470  // Print informations on currently analysed TTree
471  TEnv* treeInfos = GetReconDataTreeInfos();
472  if (!treeInfos) return;
473  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
474  cout << "INFORMATIONS ON VERSION OF KALIVEDA USED TO GENERATE FILE:" << endl << endl;
475  fDataVersion = treeInfos->GetValue("KVBase::GetKVVersion()", "(unknown)");
476  cout << "version = " << fDataVersion << endl ;
477  cout << "build date = " << treeInfos->GetValue("KVBase::GetKVBuildDate()", "(unknown)") << endl ;
478  cout << "source directory = " << treeInfos->GetValue("KVBase::GetKVSourceDir()", "(unknown)") << endl ;
479  cout << "KVROOT = " << treeInfos->GetValue("KVBase::GetKVRoot()", "(unknown)") << endl ;
480  cout << "BZR branch name = " << treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)") << endl ;
481  cout << "BZR revision #" << treeInfos->GetValue("KVBase::bzrRevisionNumber()", "(unknown)") << endl ;
482  cout << "BZR revision ID = " << treeInfos->GetValue("KVBase::bzrRevisionId()", "(unknown)") << endl ;
483  cout << "BZR revision date = " << treeInfos->GetValue("KVBase::bzrRevisionDate()", "(unknown)") << endl ;
484  cout << endl << "INFORMATIONS ON GENERATION OF FILE:" << endl << endl;
485  cout << "Generated by : " << treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "(unknown)") << endl ;
486  cout << "Analysis task : " << treeInfos->GetValue("AnalysisTask", "(unknown)") << endl ;
487  cout << "Job name : " << treeInfos->GetValue("BatchSystem.JobName", "(unknown)") << endl ;
488  cout << "Job submitted from : " << treeInfos->GetValue("LaunchDirectory", "(unknown)") << endl ;
489  cout << "Runs : " << treeInfos->GetValue("Runs", "(unknown)") << endl ;
490  cout << "Number of events requested : " << treeInfos->GetValue("NbToRead", "(unknown)") << endl ;
491  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
492 
493  // if possible, parse fDataVersion into series and release number
494  // e.g. if fDataVersion = "1.8.10":
495  // => fDataSeries = "1.8" fDataReleaseNum = 10
496  Int_t a, b, c;
497  if (fDataVersion != "(unknown)") {
498  if (sscanf(fDataVersion.Data(), "%d.%d.%d", &a, &b, &c) == 3) {
499  fDataSeries.Form("%d.%d", a, b);
500  fDataReleaseNum = c;
501  }
502  }
503  else {
504  fDataSeries = "";
505  fDataReleaseNum = -1;
506  }
507 }
508 
509 
510 
512 
514 {
515  if (theRawData) theRawData->CloneTree(-1, "fast"); //copy raw data tree to file
516  if (theGeneData) theGeneData->CloneTree(-1, "fast"); //copy pulser & laser (gene) tree to file
517 }
518 
519 
520 
527 
529 {
530  // Overrides KVDataAnalyser method, for backwards & forwards compatibility.
531  //
532  // Old user analysis classes may derive from KVOldINDRASelector (aka KVSelector) instead of KVINDRAEventSelector.
533  //
534  // It is also possible to analyse data with a "vanilla" KVReconEventSelector analysis class.
535  return KVDataAnalyser::CheckIfUserClassIsValid("KVOldINDRASelector,KVReconEventSelector");
536 }
537 
538 
int Int_t
unsigned int UInt_t
#define SafeDelete(p)
#define f(i)
#define c(i)
bool Bool_t
unsigned short UShort_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
kEnvUser
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 b
R__EXTERN TProof * gProof
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
void Print(Option_t *opt="") const override
Definition: KV2Body.cpp:813
Handles lists of available runs for different datasets and types of data.
Bool_t InfosNeedUpdate(const run_index_t &run, const KVString &filename)
void UpdateInfos(const run_index_t &run, const KVString &filename, const KVString &kvversion, const KVString &username)
void Print(Option_t *option="") const override
Definition: KVDBRun.cpp:61
KVDBSystem * GetSystem() const
Definition: KVDBRun.cpp:240
KV2Body * GetKinematics()
Definition: KVDBSystem.cpp:80
virtual void Reset()
virtual Bool_t CheckIfUserClassIsValid(const KVString &alternative_base_class="")
virtual Bool_t CheckTaskVariables()
void WriteBatchEnvFile(const TString &jobname, Bool_t save=kTRUE) override
TString GetFullPathToRunfile(const KVString &type, const run_index_t &run) const
Definition: KVDataSet.cpp:929
KVAvailableRunsFile * GetAvailableRunsFile(const Char_t *type) const
Definition: KVDataSet.cpp:50
KVString GetDataSetEnv(const Char_t *type, const Char_t *defval="") const
Definition: KVDataSet.cpp:782
FileType * OpenRunfile(const KVString &type, const run_index_t &run)
Definition: KVDataSet.h:164
Database entry for each run of an INDRA experiment.
Definition: KVINDRADBRun.h:30
KVINDRADBRun * GetRun(Int_t run) const
Definition: KVINDRADB.h:133
Base class for analysis of reconstructed INDRA events.
Manage analysis of reconstructed INDRA data.
void SetTriggerConditionsForRun(int) override
void SetSelectorCurrentRun(KVINDRADBRun *CurrentRun)
void WriteBatchEnvFile(const TString &, Bool_t sav=kTRUE) override
void PrintTreeInfos()
Print informations on currently analysed TTree.
KVReconstructedEvent * GetReconstructedEvent()
Bool_t CheckIfUserClassIsValid(const KVString &="") override
Bool_t CheckTaskVariables(void) override
Checks the task variables.
void Reset() override
Reset task variables.
Event reconstructed from energy losses in INDRA multidetector.
Set trigger conditions for analysis of reconstructed INDRA data.
void SetTriggerConditionsForRun(KVEventSelector *, Int_t, Bool_t=kFALSE) override
void handle_ebyedat_raw_data_parameter(const char *param_name, uint16_t val)
Definition: KVINDRA.cpp:417
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray", KVExpDB *db=nullptr)
void prepare_to_handle_new_raw_data()
reset acquisition parameters etc. before reading new raw data event
virtual void SetParameters(UInt_t n, Bool_t physics_parameters_only=kFALSE)
Base class for user analysis of reconstructed data.
void SetCurrentRun(KVDBRun *r) override
Event containing KVReconstructedNucleus nuclei reconstructed from hits in detectors.
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
virtual void SetProof(Bool_t on=kTRUE, Bool_t refresh=kFALSE, Bool_t gettreeheader=kFALSE)
virtual const char * GetValue(const char *name, const char *dflt) const
virtual TObject * FindObject(const char *name) const
virtual Bool_t InheritsFrom(const char *classname) const
Bool_t IsMaster() const
void Form(const char *fmt,...)
virtual const char * BaseName(const char *pathname)
long long Long64_t
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,...)
Add
TArc a
ClassImp(TPyArg)