KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVReconDataAnalyser.cpp
Go to the documentation of this file.
1 //Created by KVClassFactory on Thu Jul 19 2018
2 //Author: John Frankland
3 
4 #include "KVBase.h"
5 #include "KVReconDataAnalyser.h"
6 #include "KVDBRun.h"
7 #include "KVMultiDetArray.h"
8 #include "KVDataAnalysisTask.h"
9 #include "KVDataSet.h"
10 #include "TChain.h"
11 #include "TObjString.h"
12 #include "TChain.h"
13 #include "KVAvailableRunsFile.h"
14 #include "TProof.h"
15 
16 #include <KVClassFactory.h>
17 #include <KVTriggerConditions.h>
18 
19 using namespace std;
20 
22 
23 
24 
27  : fSelector(nullptr), theChain(nullptr)
28 {
29 }
30 
31 
32 
35 
37 {
38  //Reset task variables
40  theChain = nullptr;
41  fSelector = nullptr;
42  TotalEntriesToRead = 0;
43 }
44 
45 
46 
49 
50 KVReconDataAnalyser::~KVReconDataAnalyser()
51 {
52  //Destructor
53  SafeDelete(fSelector);
54 }
55 
56 
57 
58 
61 
63 {
64  // Checks the task variables
65 
67 
68  cout << "============> Analysis summary <=============" << endl;
69  cout << "Analysis of runs " << GetRunList().GetList() << " with the class ";
70  cout << "\"" << GetUserClass() << "\"." << endl;
71  if (GetNbEventToRead()) {
72  cout << GetNbEventToRead() << " events will be processed." << endl;
73  }
74  else {
75  cout << "All events will be processed." << endl;
76  }
77  cout << "=============================================" << endl;
78 
79  return kTRUE;
80 }
81 
82 
83 
84 
89 
91 {
92  //Run the interactive analysis
93 
94  //make the chosen dataset the active dataset ( = gDataSet; note this also opens database
95  //and positions gDataBase & gExpDB).
96  GetDataSet()->cd();
97  fSelector = nullptr;
98 
99  theChain = new TChain("ReconEvents");
100  theChain->SetDirectory(0); // we handle delete
101 
102  GetRunList().Begin();
103  Int_t run;
104 
105  // open and add to TChain all required files
106  // we force the opening of the files to avoid problems with xrootd which sometimes
107  // seems to have a little difficulty
108  while (!GetRunList().End()) {
109  run = GetRunList().Next();
110  TString fullPathToRunfile = gDataSet->GetFullPathToRunfile(GetDataType(), run);
111  cout << "Opening file " << fullPathToRunfile << endl;
113  cout << "Adding file " << fullPathToRunfile;
114  cout << " to the TChain." << endl;
115  dynamic_cast<TChain*>(theChain)->Add(fullPathToRunfile);
116  if (f && !f->IsZombie()) {
117  // update run infos in available runs file if necessary
119  if (ARF->InfosNeedUpdate(run, gSystem->BaseName(fullPathToRunfile))) {
120  if (!((TTree*)f->Get("ReconEvents"))) {
121  Error("SubmitTask", "No tree named ReconEvents is present in the current file");
122  delete theChain;
123  return;
124  }
125  TEnv* treeInfos = (TEnv*)((TTree*)f->Get("ReconEvents"))->GetUserInfo()->FindObject("TEnv");
126  if (treeInfos) {
127  TString kvversion = treeInfos->GetValue("KVBase::GetKVVersion()", "");
128  TString username = treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "");
129  if (kvversion != "") ARF->UpdateInfos(run, gSystem->BaseName(fullPathToRunfile), kvversion, username);
130  }
131  else {
132  Info("SubmitTask", "No TEnv object associated to the tree");
133  }
134  }
135  }
136  }
137  TotalEntriesToRead = theChain->GetEntries();
138  TString option = Form("EventsReadInterval=%lld,", GetAnalysisTask()->GetStatusUpdateInterval());
139  option += Form("FullRunList=%s", GetFullRunList().GetList());
140 
141  // Add any user-defined options
142  if (GetUserClassOptions() != "") {
143  option += ",";
144  option += GetUserClassOptions();
145  }
146 
147  TObject* new_selector = GetInstanceOfUserClass();
148 
149  if (!new_selector || !new_selector->InheritsFrom("TSelector")) {
150  cout << "The selector \"" << GetUserClass() << "\" is not valid." << endl;
151  cout << "Process aborted." << endl;
152  SafeDelete(new_selector);
153  }
154  else {
155  SafeDelete(new_selector);
156  Info("SubmitTask", "Beginning TChain::Process...");
157 #ifdef WITH_CPP11
158  if (GetProofMode() != KVDataAnalyser::EProofMode::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
159 #else
160  if (GetProofMode() != KVDataAnalyser::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
161 #endif
162  TString analysis_class;
163  if (GetAnalysisTask()->WithUserClass()) analysis_class.Form("%s%s", GetUserClassImp().Data(), GetACliCMode());
164  else analysis_class = GetUserClass();
165 
166  if (GetNbEventToRead()) {
167  theChain->Process(analysis_class, option.Data(), GetNbEventToRead());
168  }
169  else {
170  theChain->Process(analysis_class, option.Data());
171  }
172  }
173  delete theChain;
174  fSelector = nullptr; //deleted by TChain/TTreePlayer
175 }
176 
177 
178 
179 
185 
187 {
188  //Save (in the TEnv fBatchEnv) all necessary information on analysis task which can be used to execute it later
189  //(i.e. when batch processing system executes the job).
190  //If save=kTRUE (default), write the information in a file whose name is given by ".jobname"
191  //where 'jobname' is the name of the job as given to the batch system.
192 
194  if (save) GetBatchInfoFile()->SaveLevel(kEnvUser);
195 }
196 
197 
198 
208 
210 {
211  // Called by currently-processed KVSelector before user's InitAnalysis() method.
212  // We build the multidetector for the current dataset in case informations on
213  // detector are needed e.g. to define histograms in InitAnalysis().
214  // Note that at this stage we are not analysing a given run, so the parameters
215  // of the array are not set (they will be set in preInitRun()).
216  //
217  // Note for PROOF: as this will be called both on master and on slave workers,
218  // in order to reduce memory footprint we only build multidetector on the slaves
219 
220  if (!gProof || !gProof->IsMaster()) {
221  if (!gMultiDetArray) KVMultiDetArray::MakeMultiDetector(GetDataSet()->GetName());
222  }
223 }
224 
225 
226 
238 
240 {
241  // Called by currently-processed TSelector when a new file in the TChain is opened.
242  //
243  // We call gMultiDetArray->SetParameters for the current run. By default only physics parameters
244  // are set, not the full calibrations and identifications, unless the user analysis class option
245  //~~~~
246  // WithCalibInfos=yes
247  //~~~~
248  // is given.
249  //
250  // Infos on currently read file/tree are printed.
251 
252  Bool_t physics_parameters_only = kTRUE;
253  if (fSelector->IsOptGiven("WithCalibInfos"))
254  physics_parameters_only = (fSelector->GetOpt("WithCalibInfos") != "yes");
255 
256  Int_t run = GetRunNumberFromFileName(theChain->GetCurrentFile()->GetName());
257  gMultiDetArray->SetParameters(run, physics_parameters_only);
258  KVDBRun* CurrentRun = gExpDB->GetDBRun(run);
259  SetCurrentRun(CurrentRun);
260  fSelector->SetCurrentRun(CurrentRun);
261 
262  cout << endl << " =================== New Run =================== " <<
263  endl << endl;
264 
265  CurrentRun->Print();
266  if (CurrentRun->GetSystem()) {
267  if (CurrentRun->GetSystem()->GetKinematics())
268  CurrentRun->GetSystem()->GetKinematics()->Print();
269  }
270 
271  cout << endl << " ================================================= " <<
272  endl << endl;
273 
274  PrintTreeInfos();
275  Info("preInitRun", "Data written with series %s, release %d", GetDataSeries().Data(),
276  GetDataReleaseNumber());
277  fRustines.InitializePatchList(GetDataSet()->GetName(), GetDataType(), run, GetDataSeries(),
278  GetDataReleaseNumber(), theChain->GetCurrentFile()->GetStreamerInfoCache());
279  fRustines.Print();
280 }
281 
282 
283 
286 
288 {
289  // Set minimum (trigger) multiplicity for array
290 
291  gMultiDetArray->SetMinimumOKMultiplicity(fSelector->GetEvent());
292 
293  // apply any required patches to data
294  if (fRustines.HasActivePatches()) fRustines.Apply(fSelector->GetEvent());
295 }
296 
297 
298 
300 
302 {
303  return (TEnv*)theChain->GetTree()->GetUserInfo()->FindObject("TEnv");
304 }
305 
306 
307 #ifdef USING_ROOT6
308 
319 
321 {
322  // When called from the InitRun() method of a user's analysis class, this method will ensure that only data
323  // compatible with the experimental trigger will be provided for analysis in the user's Analysis() method.
324  //
325  // This will be done by searching for a KVTriggerConditions plugin class defined for the currently-analysed
326  // dataset, defined like so:
327  //
328  //~~~~
329  //+Plugin.KVTriggerConditions: [dataset] [classname] [libname] "[default constructor]()"
330  //~~~~
331 
332  TPluginHandler* ph = KVBase::LoadPlugin("KVTriggerConditions", GetDataSet()->GetName());
333  if (!ph) {
334  Info("SetTriggerConditionsForRun",
335  "No definition of trigger conditions available for dataset %s",
336  GetDataSet()->GetName());
337  return;
338  }
339  std::unique_ptr<KVTriggerConditions> trig((KVTriggerConditions*) ph->ExecPlugin(0));
340  trig->SetTriggerConditionsForRun(fSelector, run);
341 }
342 
343 #endif
344 
345 
348 
350 {
351  // Print informations on currently analysed TTree
352  TEnv* treeInfos = GetReconDataTreeInfos();
353  if (!treeInfos) return;
354  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
355  cout << "INFORMATIONS ON VERSION OF KALIVEDA USED TO GENERATE FILE:" << endl << endl;
356  fDataVersion = treeInfos->GetValue("KVBase::GetKVVersion()", "(unknown)");
357  cout << "version = " << fDataVersion << endl ;
358  cout << "build date = " << treeInfos->GetValue("KVBase::GetKVBuildDate()", "(unknown)") << endl ;
359  cout << "source directory = " << treeInfos->GetValue("KVBase::GetKVSourceDir()", "(unknown)") << endl ;
360  cout << "KVROOT = " << treeInfos->GetValue("KVBase::GetKVRoot()", "(unknown)") << endl ;
361  if (strcmp(treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)"), "(unknown)")) {
362  cout << "BZR branch name = " << treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)") << endl ;
363  cout << "BZR revision #" << treeInfos->GetValue("KVBase::bzrRevisionNumber()", "(unknown)") << endl ;
364  cout << "BZR revision ID = " << treeInfos->GetValue("KVBase::bzrRevisionId()", "(unknown)") << endl ;
365  cout << "BZR revision date = " << treeInfos->GetValue("KVBase::bzrRevisionDate()", "(unknown)") << endl ;
366  }
367  else if (strcmp(treeInfos->GetValue("KVBase::gitBranch()", "(unknown)"), "(unknown)")) {
368  cout << "git branch = " << treeInfos->GetValue("KVBase::gitBranch()", "(unknown)") << endl;
369  cout << "git commit = " << treeInfos->GetValue("KVBase::gitCommit()", "(unknown)") << endl;
370  }
371  cout << endl << "INFORMATIONS ON GENERATION OF FILE:" << endl << endl;
372  cout << "Generated by : " << treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "(unknown)") << endl ;
373  cout << "Analysis task : " << treeInfos->GetValue("AnalysisTask", "(unknown)") << endl ;
374  cout << "Job name : " << treeInfos->GetValue("BatchSystem.JobName", "(unknown)") << endl ;
375  cout << "Job submitted from : " << treeInfos->GetValue("LaunchDirectory", "(unknown)") << endl ;
376  cout << "Runs : " << treeInfos->GetValue("Runs", "(unknown)") << endl ;
377  cout << "Number of events requested : " << treeInfos->GetValue("NbToRead", "(unknown)") << endl ;
378  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
379 
380  // if possible, parse fDataVersion into series and release number
381  // e.g. if fDataVersion = "1.8.10":
382  // => fDataSeries = "1.8" fDataReleaseNum = 10
383  // e.g. if fDataVersion = "1.11/00":
384  // => fDataSeries = "1.11" fDataReleaseNum = 0
385  Int_t a, b, c;
386  if (fDataVersion != "(unknown)") {
387  if (fDataVersion.Contains("/")) {
388  if (sscanf(fDataVersion.Data(), "%d.%d/%d", &a, &b, &c) == 3) {
389  fDataSeries.Form("%d.%d", a, b);
390  fDataReleaseNum = c;
391  }
392  }
393  else {
394  if (sscanf(fDataVersion.Data(), "%d.%d.%d", &a, &b, &c) == 3) {
395  fDataSeries.Form("%d.%d", a, b);
396  fDataReleaseNum = c;
397  }
398  }
399  }
400  else {
401  fDataSeries = "";
402  fDataReleaseNum = -1;
403  }
404 }
405 
406 
int Int_t
KVDataSet * gDataSet
Definition: KVDataSet.cpp:29
KVExpDB * gExpDB
Definition: KVExpDB.cpp:13
KVMultiDetArray * gMultiDetArray
ClassImp(KVPartitionList) void KVPartitionList
Initialisation.
#define SafeDelete(p)
#define b(i)
#define f(i)
#define c(i)
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
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)
static TPluginHandler * LoadPlugin(const Char_t *base, const Char_t *uri="0")
Definition: KVBase.cpp:793
Description of an experimental run in database ,.
Definition: KVDBRun.h:35
KVDBSystem * GetSystem() const
Definition: KVDBRun.cpp:242
virtual void Print(Option_t *option="") const
Definition: KVDBRun.cpp:69
KV2Body * GetKinematics()
Definition: KVDBSystem.cpp:80
virtual Bool_t CheckTaskVariables()
void WriteBatchEnvFile(const Char_t *jobname, Bool_t save=kTRUE)
KVAvailableRunsFile * GetAvailableRunsFile(const Char_t *type) const
Definition: KVDataSet.cpp:48
FileType * OpenRunfile(const Char_t *type, Int_t run)
Definition: KVDataSet.h:338
TString GetFullPathToRunfile(const Char_t *type, Int_t run) const
Definition: KVDataSet.cpp:888
KVDBRun * GetDBRun(Int_t number) const
Definition: KVExpDB.h:75
virtual void SetMinimumOKMultiplicity(KVEvent *) 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")
Manages user analysis of reconstructed experimental data.
virtual void SetTriggerConditionsForRun(int)
TEnv * GetReconDataTreeInfos() const
void PrintTreeInfos()
Print informations on currently analysed TTree.
void preAnalysis()
Set minimum (trigger) multiplicity for array.
virtual void Reset()
Reset task variables.
virtual void WriteBatchEnvFile(const Char_t *, Bool_t sav=kTRUE)
virtual Bool_t CheckTaskVariables(void)
Checks the task variables.
Set trigger conditions for analysis of reconstructed data.
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
Longptr_t ExecPlugin(int nargs, const T &... params)
Bool_t IsMaster() const
const char * Data() const
void Form(const char *fmt,...)
virtual const char * BaseName(const char *pathname)
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 End()
auto * a