KaliVeda
Toolkit for HIC analysis
KVReconDataAnalyser.cpp
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 
15 #include <KVClassFactory.h>
16 #include <KVTriggerConditions.h>
17 
18 using namespace std;
19 
21 
22 
23 
26  : fSelector(nullptr), theChain(nullptr)
27 {
28 }
29 
30 
31 
34 
36 {
37  //Reset task variables
39  theChain = nullptr;
40  fSelector = nullptr;
41  TotalEntriesToRead = 0;
42 }
43 
44 
45 
46 
47 
48 
51 
53 {
54  // Checks the task variables
55 
57 
58  cout << "============> Analysis summary <=============" << endl;
59  cout << "Analysis of runs " << GetRunList().GetList() << " with the class ";
60  cout << "\"" << GetUserClass().name << "\"." << endl;
61  if (GetNbEventToRead()) {
62  cout << GetNbEventToRead() << " events will be processed." << endl;
63  }
64  else {
65  cout << "All events will be processed." << endl;
66  }
67  cout << "=============================================" << endl;
68 
69  return kTRUE;
70 }
71 
72 
73 
74 
79 
81 {
82  //Run the interactive analysis
83 
84  //make the chosen dataset the active dataset ( = gDataSet; note this also opens database
85  //and positions gDataBase & gExpDB).
86  GetDataSet()->cd();
87  fSelector = nullptr;
88 
89  theChain = new TChain("ReconEvents");
90  theChain->SetDirectory(0); // we handle delete
91 
92  // open and add to TChain all required files
93  // we force the opening of the files to avoid problems with xrootd which sometimes
94  // seems to have a little difficulty
95  for (auto& run : GetRunList()) {
96  TString fullPathToRunfile = gDataSet->GetFullPathToRunfile(GetDataType(), run);
97  cout << "Opening file " << fullPathToRunfile << endl;
98  TFile* f = gDataSet->OpenRunfile<TFile>(GetDataType(), run);
99  cout << "Adding file " << fullPathToRunfile;
100  cout << " to the TChain." << endl;
101  dynamic_cast<TChain*>(theChain)->Add(fullPathToRunfile);
102  if (f && !f->IsZombie()) {
103  // update run infos in available runs file if necessary
105  if (ARF->InfosNeedUpdate(run, gSystem->BaseName(fullPathToRunfile))) {
106  if (!((TTree*)f->Get("ReconEvents"))) {
107  KVError::Error(this, "SubmitTask", "No tree named ReconEvents is present in the current file");
108  delete theChain;
109  return;
110  }
111  TEnv* treeInfos = (TEnv*)((TTree*)f->Get("ReconEvents"))->GetUserInfo()->FindObject("TEnv");
112  if (treeInfos) {
113  TString kvversion = treeInfos->GetValue("KVBase::GetKVVersion()", "");
114  TString username = treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "");
115  if (kvversion != "") ARF->UpdateInfos(run, gSystem->BaseName(fullPathToRunfile), kvversion, username);
116  }
117  else {
118  Info("SubmitTask", "No TEnv object associated to the tree");
119  }
120  }
121  }
122  }
123  TotalEntriesToRead = theChain->GetEntries();
124  TString option = Form("EventsReadInterval=%lld,", GetAnalysisTask()->GetStatusUpdateInterval());
125  option += Form("FullRunList=%s", GetFullRunList().GetList().Data());
126 
127  // Add any user-defined options
128  if (GetUserClassOptions() != "") {
129  option += ",";
130  option += GetUserClassOptions();
131  }
132 
133  TObject* new_selector = GetInstanceOfUserClass();
134 
135  if (!new_selector || !new_selector->InheritsFrom("TSelector")) {
136  cout << "The selector \"" << GetUserClassName() << "\" is not valid." << endl;
137  cout << "Process aborted." << endl;
138  SafeDelete(new_selector);
139  }
140  else {
141  SafeDelete(new_selector);
142  Info("SubmitTask", "Beginning TChain::Process...");
143 #ifdef WITH_PROOF
144 #ifdef WITH_CPP11
145  if (GetProofMode() != KVDataAnalyser::EProofMode::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
146 #else
147  if (GetProofMode() != KVDataAnalyser::None) dynamic_cast<TChain*>(theChain)->SetProof(kTRUE);
148 #endif
149 #endif
150  TString analysis_class;
151  if (GetAnalysisTask()->WithUserClass())
152  analysis_class.Form("%s%s", GetUserClass().full_path_imp().Data(), GetACliCMode());
153  else analysis_class = GetUserClassName();
154 
155  if (GetNbEventToRead()) {
156  theChain->Process(analysis_class, option.Data(), GetNbEventToRead());
157  }
158  else {
159  theChain->Process(analysis_class, option.Data());
160  }
161  }
162  delete theChain;
163  fSelector = nullptr; //deleted by TChain/TTreePlayer
164 }
165 
166 
167 
196 
198 {
199  // Called by currently-processed TSelector when a new file in the TChain is opened.
200  //
201  // We call gMultiDetArray->SetParameters() for the current run.
202  //
203  // Whether or not only physics parameters are set, or the full set of calibrations and identifications
204  // for each detector/identification telescope is determined by the environment variable
205  //
206  //~~~
207  // [dataset].ReconAnalysis.WithCalibInfos: [yes/no]
208  //~~~
209  //
210  // This can be overridden for any individual analysis by setting the analysis class option
211  //
212  //~~~~
213  // WithCalibInfos=yes
214  //~~~~
215  //
216  // Infos on currently read file/tree are printed.
217  //
218  // Any data patches (see KVDataPatch and KVDataPatchList) which are defined for the currently analysed
219  // data will be applied, unless the variable
220  //
221  //~~~
222  //[dataset].DataAnalysis.ApplyDataPatches
223  //~~~
224  //
225  // has been set to 'off', 'no', 'false' (see KVDataPatchList::InitializePatchList())
226 
227  Bool_t physics_parameters_only = !gDataSet->GetDataSetEnv("ReconAnalysis.WithCalibInfos", kTRUE);
228  if (fSelector->IsOptGiven("WithCalibInfos"))
229  physics_parameters_only = (fSelector->GetOpt("WithCalibInfos") != "yes");
230 
231  auto run_file = gDataSet->GetRunNumberFromFileName(GetDataType(), theChain->GetCurrentFile()->GetName());
232  Int_t run = run_file.value().run();
234  KVMultiDetArray::MakeMultiDetector(GetDataSet()->GetName(), run);
235 
236  KVDBRun* CurrentRun = gExpDB->GetDBRun(run);
237  SetCurrentRun(CurrentRun);
238  fSelector->SetCurrentRun(CurrentRun);
239  SetCurrentRunFile(&gExpDB->GetDBRunFile(run_file.value()));
240 
241  cout << endl << " ================= New Run File ================= " <<
242  endl << endl;
243 
244  GetAnalysedRunFile()->Print();
245  cout << endl;
246  CurrentRun->Print();
247  if (CurrentRun->GetSystem()) {
248  if (CurrentRun->GetSystem()->GetKinematics())
249  CurrentRun->GetSystem()->GetKinematics()->Print();
250  }
251 
252  cout << endl << " ================================================= " <<
253  endl << endl;
254 
255  PrintTreeInfos();
256  Info("preInitRun", "Data written with series %s, release %d", GetDataSeries().Data(),
257  GetDataReleaseNumber());
258  fRustines.InitializePatchList(GetDataSet()->GetName(), GetDataType(), run, GetDataSeries(),
259  GetDataReleaseNumber(), theChain->GetCurrentFile()->GetStreamerInfoCache());
260  fRustines.Print();
261 }
262 
263 
264 
267 
269 {
270  // apply any required patches to data
271  if (fRustines.HasActivePatches()) fRustines.Apply(fSelector->GetEvent());
272 }
273 
274 
275 
277 
279 {
280  return (TEnv*)theChain->GetTree()->GetUserInfo()->FindObject("TEnv");
281 }
282 
283 
284 
300 
302 {
303  // When called from the InitRun() method of a user's analysis class, this method will ensure that only data
304  // compatible with the experimental trigger will be provided for analysis in the user's Analysis() method.
305  //
306  // This will be done by searching for a KVTriggerConditions plugin class defined for the currently-analysed
307  // dataset, defined like so:
308  //
309  //~~~~
310  //+Plugin.KVTriggerConditions: [dataset] [classname] [libname] "[default constructor]()"
311  //~~~~
312  //
313  // An object of the plugin class will be instantiated, and then its overridden
314  // KVTriggerConditions::SetTriggerConditionsForRun() method will be called with 2 arguments:
315  // - a pointer to the user's analysis class derived from KVReconEventSelector;
316  // - the number of the run currently being analysed
317 
318  TPluginHandler* ph = KVBase::LoadPlugin("KVTriggerConditions", GetDataSet()->GetName());
319  if (!ph) {
320  Info("SetTriggerConditionsForRun",
321  "No definition of trigger conditions available for dataset %s",
322  GetDataSet()->GetName());
323  return;
324  }
325  std::unique_ptr<KVTriggerConditions> trig((KVTriggerConditions*) ph->ExecPlugin(0));
326  trig->SetTriggerConditionsForRun(fSelector, run);
327 }
328 
329 
330 
333 
335 {
336  // Print informations on currently analysed TTree
337  TEnv* treeInfos = GetReconDataTreeInfos();
338  if (!treeInfos) return;
339  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
340  cout << "INFORMATIONS ON VERSION OF KALIVEDA USED TO GENERATE FILE:" << endl << endl;
341  fDataVersion = treeInfos->GetValue("KVBase::GetKVVersion()", "(unknown)");
342  cout << "version = " << fDataVersion << endl ;
343  cout << "build date = " << treeInfos->GetValue("KVBase::GetKVBuildDate()", "(unknown)") << endl ;
344  cout << "source directory = " << treeInfos->GetValue("KVBase::GetKVSourceDir()", "(unknown)") << endl ;
345  cout << "KVROOT = " << treeInfos->GetValue("KVBase::GetKVRoot()", "(unknown)") << endl ;
346  if (strcmp(treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)"), "(unknown)")) {
347  cout << "BZR branch name = " << treeInfos->GetValue("KVBase::bzrBranchNick()", "(unknown)") << endl ;
348  cout << "BZR revision #" << treeInfos->GetValue("KVBase::bzrRevisionNumber()", "(unknown)") << endl ;
349  cout << "BZR revision ID = " << treeInfos->GetValue("KVBase::bzrRevisionId()", "(unknown)") << endl ;
350  cout << "BZR revision date = " << treeInfos->GetValue("KVBase::bzrRevisionDate()", "(unknown)") << endl ;
351  }
352  else if (strcmp(treeInfos->GetValue("KVBase::gitBranch()", "(unknown)"), "(unknown)")) {
353  cout << "git branch = " << treeInfos->GetValue("KVBase::gitBranch()", "(unknown)") << endl;
354  cout << "git commit = " << treeInfos->GetValue("KVBase::gitCommit()", "(unknown)") << endl;
355  }
356  cout << endl << "INFORMATIONS ON GENERATION OF FILE:" << endl << endl;
357  cout << "Generated by : " << treeInfos->GetValue("gSystem->GetUserInfo()->fUser", "(unknown)") << endl ;
358  cout << "Analysis task : " << treeInfos->GetValue("AnalysisTask", "(unknown)") << endl ;
359  cout << "Job name : " << treeInfos->GetValue("BatchSystem.JobName", "(unknown)") << endl ;
360  cout << "Job submitted from : " << treeInfos->GetValue("LaunchDirectory", "(unknown)") << endl ;
361  cout << "Runs : " << treeInfos->GetValue("Runs", "(unknown)") << endl ;
362  cout << "Number of events requested : " << treeInfos->GetValue("NbToRead", "(unknown)") << endl ;
363  cout << endl << "----------------------------------------------------------------------------------------------------" << endl;
364 
365  // if possible, parse fDataVersion into series and release number
366  // e.g. if fDataVersion = "1.8.10":
367  // => fDataSeries = "1.8" fDataReleaseNum = 10
368  // e.g. if fDataVersion = "1.11/00":
369  // => fDataSeries = "1.11" fDataReleaseNum = 0
370  Int_t a, b, c;
371  if (fDataVersion != "(unknown)") {
372  if (fDataVersion.Contains("/")) {
373  if (sscanf(fDataVersion.Data(), "%d.%d/%d", &a, &b, &c) == 3) {
374  fDataSeries.Form("%d.%d", a, b);
375  fDataReleaseNum = c;
376  }
377  }
378  else {
379  if (sscanf(fDataVersion.Data(), "%d.%d.%d", &a, &b, &c) == 3) {
380  fDataSeries.Form("%d.%d", a, b);
381  fDataReleaseNum = c;
382  }
383  }
384  }
385  else {
386  fDataSeries = "";
387  fDataReleaseNum = -1;
388  }
389 }
390 
391 
int Int_t
#define SafeDelete(p)
#define f(i)
#define c(i)
bool Bool_t
constexpr Bool_t kFALSE
constexpr Bool_t kTRUE
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
char * Form(const char *fmt,...)
R__EXTERN TSystem * gSystem
void Print(Option_t *opt="") const override
Definition: KV2Body.cpp:874
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)
static TPluginHandler * LoadPlugin(const Char_t *base, const Char_t *uri="0")
Definition: KVBase.cpp:795
Description of an experimental run in database ,,.
Definition: KVDBRun.h:41
void Print(Option_t *option="") const override
Definition: KVDBRun.cpp:61
KVDBSystem * GetSystem() const
Definition: KVDBRun.cpp:240
KV2Body * GetKinematics()
Definition: KVDBSystem.cpp:103
virtual Bool_t CheckTaskVariables()
void Reset() override
TString GetFullPathToRunfile(const KVString &type, const run_index_t &run) const
Definition: KVDataSet.cpp:885
KVAvailableRunsFile * GetAvailableRunsFile(const TString &type) const
Definition: KVDataSet.cpp:49
std::optional< run_index_t > GetRunNumberFromFileName(const TString &datatype, const TString &filename)
Definition: KVDataSet.cpp:978
FileType * OpenRunfile(const KVString &type, const run_index_t &run)
Definition: KVDataSet.h:285
ValType GetDataSetEnv(const Char_t *type, const ValType &defval={}) const
Definition: KVDataSet.h:270
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)
static void SetMakeMultiDetectorPhysicsParametersOnly(Bool_t on=kTRUE)
Manages user analysis of reconstructed experimental data.
void preAnalysis() override
apply any required patches to data
TEnv * GetReconDataTreeInfos() const
void PrintTreeInfos()
Print informations on currently analysed TTree.
Bool_t CheckTaskVariables(void) override
Checks the task variables.
virtual void SetTriggerConditionsForRun(int) override
void SubmitTask() override
void Reset() override
Reset 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)
void Form(const char *fmt,...)
virtual const char * BaseName(const char *pathname)
void Error(UserClass p, const char *location, const char *va_(fmt),...)
Definition: KVError.h:116
BinData::ErrorType GetDataType(const TGraph *gr, DataOptions &fitOpt)
void Info(const char *location, const char *fmt,...)
Add
TArc a
ClassImp(TPyArg)