KaliVeda  1.12/06
Heavy-Ion Analysis Toolkit
KVINDRAReconDataAnalyser.cpp
Go to the documentation of this file.
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"
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 #include "TProof.h"
23 
24 #include <KVReconEventSelector.h>
25 
26 using namespace std;
27 
29 
30 
31 
34  : fSelector(nullptr), fOldSelector(nullptr), theChain(nullptr),
35  theRawData(nullptr), theGeneData(nullptr),
36  ParVal(nullptr), ParNum(nullptr), parList(nullptr)
37 {
38 }
39 
40 
41 
44 
46 {
47  //Reset task variables
49  theChain = nullptr;
50  theRawData = nullptr;
51  theGeneData = nullptr;
52  ParVal = nullptr;
53  ParNum = nullptr;
54  fSelector = nullptr;
55  fOldSelector = nullptr;
56  TotalEntriesToRead = 0;
57 }
58 
59 
60 
63 
64 KVINDRAReconDataAnalyser::~KVINDRAReconDataAnalyser()
65 {
66  //Destructor
67  if (ParVal) delete [] ParVal;
68  if (ParNum) delete [] ParNum;
69  SafeDelete(fSelector);
70  SafeDelete(fOldSelector);
71 }
72 
73 
74 
75 
78 
80 {
81  // Checks the task variables
82 
84 
85  cout << "============> Analysis summary <=============" << endl;
86  cout << "Analysis of runs " << GetRunList().
87  GetList() << " with the class ";
88  cout << "\"" << GetUserClass() << "\"." << endl;
89  if (GetNbEventToRead()) {
90  cout << GetNbEventToRead() << " events will be processed." << endl;
91  }
92  else {
93  cout << "All events will be processed." << endl;
94  }
95  cout << "=============================================" << endl;
96 
97  return kTRUE;
98 }
99 
100 
101 
102 
107 
109 {
110  //Run the interactive analysis
111 
112  //make the chosen dataset the active dataset ( = gDataSet; note this also opens database
113  //and positions gDataBase & gIndraDB).
114  GetDataSet()->cd();
115  fSelector = nullptr;
116  fOldSelector = nullptr;
117 
118  theChain = new TChain("ReconstructedEvents");
119  theChain->SetDirectory(0); // we handle delete
120 
121  GetRunList().Begin();
122  Int_t run;
123 
124  // open and add to TChain all required files
125  // we force the opening of the files to avoid problems with xrootd which sometimes
126  // seems to have a little difficulty
127  while (!GetRunList().End()) {
128  run = GetRunList().Next();
129  TString fullPathToRunfile = gDataSet->GetFullPathToRunfile(GetDataType(), run);
130  cout << "Opening file " << fullPathToRunfile << endl;
132  cout << "Adding file " << fullPathToRunfile;
133  cout << " to the TChain." << endl;
134  dynamic_cast<TChain*>(theChain)->Add(fullPathToRunfile);
135  if (f && !f->IsZombie()) {
136  // update run infos in available runs file if necessary
138  if (ARF->InfosNeedUpdate(run, gSystem->BaseName(fullPathToRunfile))) {
139  if (!((TTree*)f->Get("ReconstructedEvents"))) {
140  Error("SubmitTask", "No tree named ReconstructedEvents is present in the current file");
141  delete theChain;
142  return;
143  }
144  TEnv* treeInfos = (TEnv*)((TTree*)f->Get("ReconstructedEvents"))->GetUserInfo()->FindObject("TEnv");
145  if (treeInfos) {
146  TString kvversion = treeInfos->GetValue("KVBase::GetKVVersion()", "");
147  TString username = treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "");
148  if (kvversion != "") ARF->UpdateInfos(run, gSystem->BaseName(fullPathToRunfile), kvversion, username);
149  }
150  else {
151  Info("SubmitTask", "No TEnv object associated to the tree");
152  }
153  }
154  }
155  }
156  TotalEntriesToRead = theChain->GetEntries();
157  TString option("");
158 
159  // Add the total run list in option
160  if (!(option.IsNull())) option += ",";
161  option += Form("FullRunList=%s", GetFullRunList().GetList());
162 
163  // Add any user-defined options
164  if (GetUserClassOptions() != "") {
165  option += ",";
166  option += GetUserClassOptions();
167  }
168 
169  set_up_analyser_for_task(this);
170 
171  // for backwards compatibility, we allow user class to inherit from
172  // KVOldINDRASelector instead of KVINDRAEventSelector
173  // for forwards compatibility, we allow it to inherit from KVReconEventSelector too!
174  TObject* new_selector = GetInstanceOfUserClass("KVOldINDRASelector,KVReconEventSelector");
175 
176  if (!new_selector || !new_selector->InheritsFrom("TSelector")) {
177  cout << "The selector \"" << GetUserClass() << "\" is not valid." << endl;
178  cout << "Process aborted." << endl;
179  SafeDelete(new_selector);
180  }
181  else {
182  SafeDelete(new_selector);
183  Info("SubmitTask", "Beginning TChain::Process...");
184 #ifdef WITH_CPP11
185  if (GetProofMode() != KVDataAnalyser::EProofMode::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
186 #else
187  if (GetProofMode() != KVDataAnalyser::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
188 #endif
189  TString analysis_class;
190  if (GetAnalysisTask()->WithUserClass()) analysis_class.Form("%s%s", GetUserClassImp().Data(), GetACliCMode());
191  else analysis_class = GetUserClass();
192 
193  if (GetNbEventToRead()) {
194  theChain->Process(analysis_class, option.Data(), GetNbEventToRead());
195  }
196  else {
197  theChain->Process(analysis_class, option.Data());
198  }
199  }
200  delete theChain;
201  fSelector = nullptr; //deleted by TChain/TTreePlayer
202  fOldSelector = nullptr; //deleted by TChain/TTreePlayer
203 }
204 
205 
206 
207 
213 
215 {
216  //Save (in the TEnv fBatchEnv) all necessary information on analysis task which can be used to execute it later
217  //(i.e. when batch processing system executes the job).
218  //If save=kTRUE (default), write the information in a file whose name is given by ".jobname"
219  //where 'jobname' is the name of the job as given to the batch system.
220 
222  // backwards-compatible fix for old KVSelector analysis classes
223  GetBatchInfoFile()->SetValue("UserClassAlternativeBaseClass", "KVOldINDRASelector");
224  if (save) GetBatchInfoFile()->SaveLevel(kEnvUser);
225 }
226 
227 
228 
233 
235 {
236  //Prints list of available runs, sorted according to multiplicity
237  //trigger, for selected dataset, data type/analysis task, and system
238  //Returns list containing all run numbers
239 
240  KVNumberList all_runs =
241  GetDataSet()->GetRunList(datatype.Data(), GetSystem());
242  KVINDRADBRun* dbrun;
243 
244  //first read list and find what triggers are available
245  vector<int> triggers;
246  all_runs.Begin();
247  while (!all_runs.End()) {
248  dbrun = (KVINDRADBRun*)GetDataSet()->GetDataBase()->GetDBRun(all_runs.Next());
249  if (triggers.size() == 0
250  || std::find(triggers.begin(), triggers.end(), dbrun->GetTrigger()) != triggers.end()) {
251  triggers.push_back(dbrun->GetTrigger());
252  }
253  }
254  //sort triggers in ascending order
255  std::sort(triggers.begin(), triggers.end());
256 
257  for (std::vector<int>::iterator it = triggers.begin(); it != triggers.end(); ++it) {
258  cout << " ---> Trigger M>" << *it << endl;
259  all_runs.Begin();
260  while (!all_runs.End()) {
261  dbrun = (KVINDRADBRun*)GetDataSet()->GetDataBase()->GetDBRun(all_runs.Next());
262  if (dbrun->GetTrigger() == *it) {
263  cout << " " << Form("%4d", dbrun->GetNumber());
264  cout << Form("\t(%9llu events)", dbrun->GetEvents());
265  cout << "\t[File written: " << dbrun->GetDatime().
266  AsString() << "]";
267  if (dbrun->GetComments())
268  cout << "\t" << dbrun->GetComments();
269  cout << endl;
270  }
271  }
272  cout << endl;
273  }
274  return all_runs;
275 }
276 
277 
278 
288 
290 {
291  // Called by currently-processed KVEventSelector before user's InitAnalysis() method.
292  // We build the multidetector for the current dataset in case informations on
293  // detector are needed e.g. to define histograms in InitAnalysis().
294  // Note that at this stage we are not analysing a given run, so the parameters
295  // of the array are not set (they will be set in preInitRun()).
296  //
297  // Note for PROOF: as this will be called both on master and on slave workers,
298  // in order to reduce memory footprint we only build INDRA on the slaves
299 
300  if (!gProof || !gProof->IsMaster()) {
301  if (!gIndra) KVMultiDetArray::MakeMultiDetector(GetDataSet()->GetName());
302  }
303  // in case data is being analysed with a "vanilla" KVReconEventSelector, we need
304  // to set the correct branch name for reconstructed INDRA data which is "INDRAReconEvent"
305  // instead of the default "ReconEvent"
306  if (fSelector) fSelector->SetBranchName("INDRAReconEvent");
307  else Warning("preInitAnalysis", "could not set branch name correctly");
308 }
309 
310 
311 
313 
315 {
316  if (fSelector) {
317  if (fSelector->InheritsFrom("KVINDRAEventSelector"))
318  dynamic_cast<KVINDRAEventSelector*>(fSelector)->SetCurrentRun(CurrentRun);
319  // WARNING - horrible kludge...
320  else if (fSelector->InheritsFrom("KVReconEventSelector"))
321  dynamic_cast<KVReconEventSelector*>(fSelector)->SetCurrentRun(CurrentRun);
322  }
323  else {
324  fOldSelector->SetCurrentRun(CurrentRun);
325  }
326 }
327 
328 
329 
338 
340 {
341  // Called by currently-processed TSelector when a new file in the TChain is opened.
342  // We call gIndra->SetParameters for the current run: only physics parameters will be set,
343  // not the full set of identification/calibration parameters.
344  //
345  // We connect the acquisition parameter objects to the branches of the raw data tree.
346  // Infos on currently read file/tree are printed.
347  // Any required data patches ("rustines") are initialized.
348 
349  Int_t run = GetRunNumberFromFileName(theChain->GetCurrentFile()->GetName());
350  gIndra->SetParameters(run, kTRUE);
351  KVINDRADBRun* CurrentRun = gIndraDB->GetRun(run);
352  SetCurrentRun(CurrentRun);
353  SetSelectorCurrentRun(CurrentRun);
354  cout << endl << " =================== New Run =================== " <<
355  endl << endl;
356 
357  CurrentRun->Print();
358  if (CurrentRun->GetSystem()) {
359  SetSystem(CurrentRun->GetSystem());
360  if (CurrentRun->GetSystem()->GetKinematics())
361  CurrentRun->GetSystem()->GetKinematics()->Print();
362  }
363 
364  cout << endl << " ================================================= " <<
365  endl << endl;
366 
367  ConnectRawDataTree();
368  ConnectGeneDataTree();
369  PrintTreeInfos();
370  Info("preInitRun", "Data written with series %s, release %d", GetDataSeries().Data(),
371  GetDataReleaseNumber());
372  fRustines.InitializePatchList(GetDataSet()->GetName(), GetDataType(), run, GetDataSeries(),
373  GetDataReleaseNumber(), theChain->GetCurrentFile()->GetStreamerInfoCache());
374  fRustines.Print();
375 }
376 
377 
378 
380 
382 {
383  Long64_t rawEntry = (fSelector ? fSelector->GetEventNumber() - 1
384  : fOldSelector->GetEventNumber() - 1);
385 
386  return rawEntry;
387 }
388 
389 
390 
392 
394 {
395  return (fSelector ? dynamic_cast<KVReconstructedEvent*>(fSelector->GetEvent()) : fOldSelector->GetEvent());
396 }
397 
398 
399 #ifdef USING_ROOT6
400 
402 
404 {
406  trig.SetTriggerConditionsForRun(fSelector, run);
407 }
408 
409 #endif
410 
411 
415 
417 {
418  // Read and set raw data for the current reconstructed event
419  // Any required data patches ("rustines") are applied.
420 
421  if (!theRawData) return;
422  // all recon events are numbered 1, 2, ... : therefore entry number is N-1
423  Long64_t rawEntry = GetRawEntryNumber();
424 
425  TIter it(gIndra->GetACQParams());
426  KVACQParam* a;
427  while ((a = (KVACQParam*)it())) a->Clear();
428 
429  theRawData->GetEntry(rawEntry);
430  for (int i = 0; i < NbParFired; i++) {
431  KVACQParam* par = gIndra->GetACQParam((*parList)[ParNum[i]]->GetName());
432  if (par) {
433  par->SetData(ParVal[i]);
434  }
435  }
436  gIndra->SetMinimumOKMultiplicity(GetReconstructedEvent());
437 
438  // as rustines often depend on a knowledge of the original raw data,
439  // we apply them after it has been read in
440  KVINDRAReconEvent* event = (KVINDRAReconEvent*)GetReconstructedEvent();
441  if (fRustines.HasActivePatches()) fRustines.Apply(event);
442 }
443 
444 
445 
452 
454 {
455  // Called by preInitRun().
456  // When starting to read a new run (=new file), we look for the TTree "RawData" in the
457  // current file (it should have been created by KVINDRARawDataReconstructor).
458  // If found, it will be used by ReadRawData() to set the values of all acquisition parameters
459  // for each event.
460 
461  theRawData = (TTree*)theChain->GetCurrentFile()->Get("RawData");
462  if (!theRawData) {
463  Warning("ConnectRawDataTree", "RawData tree not found in file; raw data parameters of detectors will not be available in analysis");
464  return;
465  }
466  else
467  Info("ConnectRawDataTree", "Found RawData tree in file");
468  Int_t maxNopar = theRawData->GetMaximum("NbParFired");
469  if (ParVal) delete [] ParVal;
470  if (ParNum) delete [] ParNum;
471  ParVal = new UShort_t[maxNopar];
472  ParNum = new UInt_t[maxNopar];
473  parList = (TObjArray*)theRawData->GetUserInfo()->FindObject("ParameterList");
474  theRawData->SetBranchAddress("NbParFired", &NbParFired);
475  theRawData->SetBranchAddress("ParNum", ParNum);
476  theRawData->SetBranchAddress("ParVal", ParVal);
477  Info("ConnectRawDataTree", "Connected raw data parameters");
478  Entry = 0;
479 }
480 
481 
482 
487 
489 {
490  // Called by preInitRun().
491  // When starting to read a new run (=new file), we look for the TTree "GeneData" in the
492  // current file (it should have been created by KVINDRARawDataReconstructor).
493 
494  theGeneData = (TTree*)theChain->GetCurrentFile()->Get("GeneData");
495  if (!theGeneData) {
496  cout << " --> No pulser & laser data for this run !!!" << endl << endl;
497  }
498  else {
499  cout << " --> Pulser & laser data tree contains " << theGeneData->GetEntries()
500  << " events" << endl << endl;
501  }
502 }
503 
504 
505 
507 
509 {
510  return (TEnv*)theChain->GetTree()->GetUserInfo()->FindObject("TEnv");
511 }
512 
513 
514 
517 
519 {
520  // Print informations on currently analysed TTree
521  TEnv* treeInfos = GetReconDataTreeInfos();
522  if (!treeInfos) return;
523  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
524  cout << "INFORMATIONS ON VERSION OF KALIVEDA USED TO GENERATE FILE:" << endl << endl;
525  fDataVersion = treeInfos->GetValue("KVBase::GetKVVersion()", "(unknown)");
526  cout << "version = " << fDataVersion << endl ;
527  cout << "build date = " << treeInfos->GetValue("KVBase::GetKVBuildDate()", "(unknown)") << endl ;
528  cout << "source directory = " << treeInfos->GetValue("KVBase::GetKVSourceDir()", "(unknown)") << endl ;
529  cout << "KVROOT = " << treeInfos->GetValue("KVBase::GetKVRoot()", "(unknown)") << endl ;
530  cout << "BZR branch name = " << treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)") << endl ;
531  cout << "BZR revision #" << treeInfos->GetValue("KVBase::bzrRevisionNumber()", "(unknown)") << endl ;
532  cout << "BZR revision ID = " << treeInfos->GetValue("KVBase::bzrRevisionId()", "(unknown)") << endl ;
533  cout << "BZR revision date = " << treeInfos->GetValue("KVBase::bzrRevisionDate()", "(unknown)") << endl ;
534  cout << endl << "INFORMATIONS ON GENERATION OF FILE:" << endl << endl;
535  cout << "Generated by : " << treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "(unknown)") << endl ;
536  cout << "Analysis task : " << treeInfos->GetValue("AnalysisTask", "(unknown)") << endl ;
537  cout << "Job name : " << treeInfos->GetValue("BatchSystem.JobName", "(unknown)") << endl ;
538  cout << "Job submitted from : " << treeInfos->GetValue("LaunchDirectory", "(unknown)") << endl ;
539  cout << "Runs : " << treeInfos->GetValue("Runs", "(unknown)") << endl ;
540  cout << "Number of events requested : " << treeInfos->GetValue("NbToRead", "(unknown)") << endl ;
541  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
542 
543  // if possible, parse fDataVersion into series and release number
544  // e.g. if fDataVersion = "1.8.10":
545  // => fDataSeries = "1.8" fDataReleaseNum = 10
546  Int_t a, b, c;
547  if (fDataVersion != "(unknown)") {
548  if (sscanf(fDataVersion.Data(), "%d.%d.%d", &a, &b, &c) == 3) {
549  fDataSeries.Form("%d.%d", a, b);
550  fDataReleaseNum = c;
551  }
552  }
553  else {
554  fDataSeries = "";
555  fDataReleaseNum = -1;
556  }
557 }
558 
559 
560 
562 
564 {
565  if (theRawData) theRawData->CloneTree(-1, "fast"); //copy raw data tree to file
566  if (theGeneData) theGeneData->CloneTree(-1, "fast"); //copy pulser & laser (gene) tree to file
567 }
568 
569 
570 
577 
579 {
580  // Overrides KVDataAnalyser method, for backwards & forwards compatibility.
581  //
582  // Old user analysis classes may derive from KVOldINDRASelector (aka KVSelector) instead of KVINDRAEventSelector.
583  //
584  // It is also possible to analyse data with a "vanilla" KVReconEventSelector analysis class.
585  return KVDataAnalyser::CheckIfUserClassIsValid("KVOldINDRASelector,KVReconEventSelector");
586 }
587 
588 
int Int_t
unsigned int UInt_t
KVDataSet * gDataSet
Definition: KVDataSet.cpp:30
KVINDRADB * gIndraDB
Definition: KVINDRADB.cpp:38
KVINDRA * gIndra
Definition: KVINDRA.cpp:78
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define SafeDelete(p)
#define b(i)
#define f(i)
#define c(i)
unsigned short UShort_t
char Char_t
const Bool_t kFALSE
bool Bool_t
const Bool_t kTRUE
kEnvUser
R__EXTERN TProof * gProof
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
void Print(Option_t *opt="") const
Definition: KV2Body.cpp:814
GANIL VXI/VME acquisition parameter.
Definition: KVACQParam.h:15
void SetData(UShort_t val)
Definition: KVACQParam.h:58
Handles lists of available runs for different datasets and types of data.
virtual void UpdateInfos(Int_t run, const Char_t *filename, const Char_t *kvversion, const Char_t *username)
virtual Bool_t InfosNeedUpdate(Int_t run, const Char_t *filename)
virtual Int_t GetNumber() const
Definition: KVDBRecord.h:72
ULong64_t GetEvents() const
Definition: KVDBRun.h:133
KVDBSystem * GetSystem() const
Definition: KVDBRun.cpp:242
const TDatime & GetDatime() const
Definition: KVDBRun.h:113
virtual void Print(Option_t *option="") const
Definition: KVDBRun.cpp:69
Int_t GetTrigger() const
Definition: KVDBRun.h:103
const Char_t * GetComments() const
Definition: KVDBRun.h:146
KV2Body * GetKinematics()
Definition: KVDBSystem.cpp:79
virtual void Reset()
virtual Bool_t CheckIfUserClassIsValid(const KVString &alternative_base_class="")
virtual Bool_t CheckTaskVariables()
void WriteBatchEnvFile(const Char_t *jobname, Bool_t save=kTRUE)
KVAvailableRunsFile * GetAvailableRunsFile(const Char_t *type) const
Definition: KVDataSet.cpp:49
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
Database entry for each run of an INDRA experiment.
Definition: KVINDRADBRun.h:29
KVINDRADBRun * GetRun(Int_t run) const
Definition: KVINDRADB.h:132
Base class for analysis of reconstructed INDRA events.
Manage analysis of reconstructed INDRA data.
virtual void Reset()
Reset task variables.
void SetSelectorCurrentRun(KVINDRADBRun *CurrentRun)
void PrintTreeInfos()
Print informations on currently analysed TTree.
KVReconstructedEvent * GetReconstructedEvent()
virtual Bool_t CheckTaskVariables(void)
Checks the task variables.
virtual void WriteBatchEnvFile(const Char_t *, Bool_t sav=kTRUE)
virtual KVNumberList PrintAvailableRuns(KVString &datatype)
virtual Bool_t CheckIfUserClassIsValid(const KVString &="")
Event reconstructed from energy losses in INDRA multidetector.
Set trigger conditions for analysis of reconstructed INDRA data.
void SetTriggerConditionsForRun(KVEventSelector *, int)
void SetMinimumOKMultiplicity(KVEvent *) const
Definition: KVINDRA.cpp:1312
const KVSeqCollection * GetACQParams() const
virtual void SetParameters(UInt_t n, Bool_t physics_parameters_only=kFALSE)
static KVMultiDetArray * MakeMultiDetector(const Char_t *dataset_name, Int_t run=-1, TString classname="KVMultiDetArray")
KVACQParam * GetACQParam(const Char_t *name) const
Strings used to represent a set of ranges of values.
Definition: KVNumberList.h:83
Bool_t End(void) const
Definition: KVNumberList.h:196
void Begin(void) const
Int_t Next(void) const
Base class for user analysis of reconstructed data.
void SetCurrentRun(KVDBRun *r)
Physical event reconstructed from data measured with a detector array using implemented identificatio...
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
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
const char * Data() const
Bool_t IsNull() const
void Form(const char *fmt,...)
virtual const char * BaseName(const char *pathname)
long long Long64_t
void Add(RHist< DIMENSIONS, PRECISION, STAT_TO... > &to, const RHist< DIMENSIONS, PRECISION, STAT_FROM... > &from)
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()
auto * a