KaliVeda  1.13/01
Heavy-Ion Analysis Toolkit
KVEventSelector.h
Go to the documentation of this file.
1 #ifndef KVEventSelector_h
2 #define KVEventSelector_h
3 
4 #include <TROOT.h>
5 #include <TChain.h>
6 #include <TFile.h>
7 #include <TSelector.h>
8 #include "KVNucleusEvent.h"
9 #include "KVGVList.h"
10 #include "KVString.h"
11 #include "KVParticleCondition.h"
12 #include "TDatime.h"
13 #include "KVHashList.h"
14 #include <TH3.h>
15 #include <TH2.h>
16 #include <TProfile2D.h>
18 #include "TProofOutputFile.h"
19 #include "KVDataAnalyser.h"
20 
153 class KVEventSelector : public TSelector {
154 
155 protected :
158 
162 
165 
169 
171 
174 
176 
180 
183 
186 
189 
191  TFile* writeFile = nullptr;
193 
194  void add_histo(TH1* histo);
195  void add_tree(TTree* tree);
196  void FillTH1(TH1* h1, Double_t one, Double_t two);
197  void FillTProfile(TProfile* h1, Double_t one, Double_t two, Double_t three);
198  void FillTH2(TH2* h2, Double_t one, Double_t two, Double_t three);
199  void FillTProfile2D(TProfile2D* h2, Double_t one, Double_t two, Double_t three, Double_t four);
200  void FillTH3(TH3* h3, Double_t one, Double_t two, Double_t three, Double_t four);
201 
202  void SetUpAuxEventChain();
203  void SetCombinedOutputFile(const TString& filename)
204  {
219  fCombinedOutputFile = filename;
220 
223  }
224  void create_output_file();
225 
226 public:
227  Bool_t CreateTreeFile(const Char_t* filename = "");
228 
229  virtual void ParseOptions();
230 
231  KVEventSelector(TTree* /*tree*/ = 0) : fChain(0), fAuxChain(0), fBranchName("data"), fFirstEvent(kTRUE),
233  {
235  }
237  {
239  }
240  virtual Int_t Version() const
241  {
242  return 3;
243  }
244  virtual void Begin(TTree* tree);
245  virtual void SlaveBegin(TTree* tree);
246  virtual void Init(TTree* tree);
247  void InitFriendTree(TTree* tree, const TString& branchname);
248  virtual Bool_t Notify();
249  virtual Bool_t Process(Long64_t entry);
250  virtual void CheckEndOfRun();
251  virtual Int_t GetEntry(Long64_t entry, Int_t getall = 0)
252  {
253  return fChain ? fChain->GetTree()->GetEntry(entry, getall) : 0;
254  }
256  {
257  return fAuxChain ? fAuxChain->GetTree()->GetEntry(entry, getall) : 0;
258  }
260  {
261  return AuxEvent;
262  }
263  virtual void SetObject(TObject* obj)
264  {
265  fObject = obj;
266  }
267  virtual void SetInputList(TList* input)
268  {
269  fInput = input;
270  }
271  virtual TList* GetOutputList() const
272  {
273  return fOutput;
274  }
275  virtual void SlaveTerminate();
276  virtual void Terminate();
277 
278  void SetBranchName(const Char_t* n)
279  {
280  fBranchName = n;
281  }
282  const Char_t* GetBranchName() const
283  {
284  return fBranchName.Data();
285  }
286  virtual void SetAnalysisFrame()
287  {
293  }
294  virtual void SetCurrentRun(KVDBRun*) {}
295 
296  KVEvent* GetEvent() const
297  {
298  return Event;
299  }
300  void SetEvent(KVEvent* e)
301  {
302  Event = e;
303  }
305  {
306  if (!GetEvent()) {
307  Error("GetEventNumber", "No event defined!!!");
308  return -1;
309  }
310  return GetEvent()->GetNumber();
311  }
312 
313  /* user entry points */
314  virtual void InitAnalysis()
315  {
316  AbstractMethod("InitAnalysis");
317  }
318  virtual void InitRun()
319  {
320  AbstractMethod("InitRun");
321  }
322  virtual Bool_t Analysis()
323  {
325  return kTRUE;
326  }
327  virtual void EndRun()
328  {
329  AbstractMethod("EndRun");
330  }
331  virtual void EndAnalysis()
332  {
333  AbstractMethod("EndAnalysis");
334  }
335  KVGVList* AddGVList(const KVString& list_name, const KVParticleCondition& selection = KVParticleCondition())
336  {
342 
343  auto gvl = new KVGVList(list_name, selection);
344  fGlobalVariables.Add(gvl);
345  return gvl;
346  }
347  KVGVList* GetGVList(const KVString& list_name = "default")
348  {
354 
355  if (list_name == "default" && fGlobalVariables.IsEmpty()) {
356  auto l = new KVGVList;
358  return l;
359  }
360  return (KVGVList*)fGlobalVariables.FindObject(list_name);
361  }
362  const KVGVList* GetGVList(const KVString& list_name = "default") const
363  {
365  return (KVGVList*)fGlobalVariables.FindObject(list_name);
366  }
367  void AddGV(KVVarGlob* vg)
368  {
372  if (!vg)
373  Error("AddGV(KVVarGlob*)", "KVVarGlob pointer is null");
374  else
375  GetGVList()->Add(vg);
376  }
377  KVVarGlob* AddGV(const Char_t* class_name, const Char_t* name);
378  KVVarGlob* GetGV(const Char_t* name) const
379  {
383 
384  auto gv = GetGVList()->GetGV(name);
385  if (!gv) {
386  Error("GetGV(const Char_t* name)", "Request for undefined global variable name=%s", name);
387  }
388  return gv;
389  }
390  virtual void RecalculateGlobalVariables();
392  {
394  return (fTreeEntry + 1 == fChain->GetTree()->GetEntries());
395  }
396 
397  void SetParticleConditions(const KVParticleCondition&, const KVString& = "");
399  {
400  fPartName = t;
401  }
402 
403  void AddHisto(TH1* histo)
404  {
405  Deprecate(Form("You should use e.g. 'auto h = AddHisto<%s>(\"%s\",\"%s\",%d,...);' to add histograms to your analysis.",
406  histo->ClassName(), histo->GetName(), histo->GetTitle(), histo->GetNbinsX()));
407  add_histo(histo);
408  }
409 
410  template<typename HistoType, typename... Args>
411  HistoType* AddHisto(Args&& ... args)
412  {
425 
426  auto h = new HistoType(std::forward<Args>(args)...);
427  add_histo(h);
428  return h;
429  }
430 
432  {
433  Deprecate(Form("You should use e.g. 'auto t = AddTree(\"%s\", \"%s\");' to add a TTree to your analysis",
434  tree->GetName(), tree->GetTitle()));
435  add_tree(tree);
436  }
437  TTree* AddTree(const TString& name, const TString& title = "");
438 
439  void FillHisto(const Char_t* sname, Double_t one, Double_t two = 1, Double_t three = 1, Double_t four = 1);
440  void FillHisto(const Char_t* sname, const Char_t* label, Double_t weight = 1);
441  void FillTree(const Char_t* sname = "");
442 
443  const KVHashList* GetHistoList() const;
444  const KVHashList* GetTreeList() const;
445 
446  TH1* GetHisto(const Char_t* name) const;
447  TTree* GetTree(const Char_t* name) const;
448 
449  virtual void SaveHistos(const Char_t* = "", Option_t* = "recreate", Bool_t = kFALSE)
450  {
451  Error("SaveHistos", "Method no longer implemented. Does nothing.");
452  }
453 
454  void SetOpt(const Char_t* option, const Char_t* value)
455  {
456  fOptionList.SetOpt(option, value);
457  }
458  Bool_t IsOptGiven(const Char_t* option)
459  {
460  return fOptionList.IsOptGiven(option);
461  }
462  TString GetOpt(const Char_t* option) const
463  {
464  return fOptionList.GetOpt(option);
465  }
466  void UnsetOpt(const Char_t* opt)
467  {
468  fOptionList.UnsetOpt(opt);
469  }
470 
472  {
475  }
476 
477  void SetJobOutputFileName(const TString& filename)
478  {
484 
485  if (KVDataAnalyser::IsRunningBatchAnalysis() && (gDataAnalyser->GetProofMode() == KVDataAnalyser::EProofMode::None))
487  else
488  SetCombinedOutputFile(filename);
489  }
490  void SetTriggerConditionsForRun(int);
491  ClassDef(KVEventSelector, 0)//General purpose analysis class for TTrees containing KVEvent objects
492 };
493 
498 #define AddVar(var,type) Branch(dadastr(var), &var, didixstr(duduvartype(var,type)))
499 #define AddVarBranch(var,branch,type) Branch(dadastr(branch), &var, didixstr(duduvartype(branch,type)))
500 #define duduvartype(var,type) var/type
501 #define didixstr(s) dadastr(s)
502 #define dadastr(s) #s
503 
504 #endif
int Int_t
KVDataAnalyser * gDataAnalyser
KVTemplateParticleCondition< KVNucleus > KVParticleCondition
#define e(i)
char Char_t
const Bool_t kFALSE
bool Bool_t
double Double_t
const Bool_t kTRUE
const char Option_t
#define ClassDef(name, id)
#define N
char * Form(const char *fmt,...)
TVector3 GetCMVelocity() const
Return vector velocity of centre of mass of reaction (units: cm/ns)
Definition: KV2Body.cpp:573
UInt_t GetNumber() const
Definition: KVBase.h:219
virtual const Char_t * GetJobName() const
Description of an experimental run in database ,.
Definition: KVDBRun.h:35
virtual const KVBatchSystem * GetBatchSystem()
virtual const KV2Body * GetKinematics() const
EProofMode GetProofMode() const
static Bool_t IsRunningBatchAnalysis()
General purpose analysis class for TTree containing KVEvent objects.
virtual void SetInputList(TList *input)
void FillTProfile2D(TProfile2D *h2, Double_t one, Double_t two, Double_t three, Double_t four)
virtual Int_t Version() const
virtual void SaveHistos(const Char_t *="", Option_t *="recreate", Bool_t=kFALSE)
void AddGV(KVVarGlob *vg)
const KVHashList * GetTreeList() const
return the list of created trees
KVVarGlob * GetGV(const Char_t *name) const
Bool_t fDisableCreateTreeFile
used with PROOF
KVEventSelector(TTree *=0)
Bool_t fFirstEvent
set to kFALSE after first event is read
Int_t GetFriendTreeEntry(Long64_t entry, Int_t getall=0)
TH1 * GetHisto(const Char_t *name) const
TProofOutputFile * mergeFile
output file for trees and histos
KVHashList lhisto
list of histograms added with AddHisto
void SetParticleConditions(const KVParticleCondition &, const KVString &="")
const Char_t * GetBranchName() const
void FillTree(const Char_t *sname="")
virtual void InitAnalysis()
KVHashList ltree
list of trees added with AddTree
void InitFriendTree(TTree *tree, const TString &branchname)
void SetOpt(const Char_t *option, const Char_t *value)
virtual void EndRun()
TTree * fAuxChain
[optional] pointer to another TTree or TChain which may be used during analysis
const KVHashList * GetHistoList() const
return the list of created trees
void SetCombinedOutputFile(const TString &filename)
virtual Bool_t Analysis()
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
KVEvent * Event
Declaration of leaf types.
virtual void SetAnalysisFrame()
TTree * GetTree(const Char_t *name) const
return the tree named tree_name
Long64_t fEventsRead
number of events read
virtual void CheckEndOfRun()
Testing whether EndRun() should be called.
void FillTH2(TH2 *h2, Double_t one, Double_t two, Double_t three)
virtual void Begin(TTree *tree)
Long64_t fTreeEntry
current tree entry number
TTree * fChain
pointer to the analyzed TTree or TChain
Bool_t CreateTreeFile(const Char_t *filename="")
KVString fCombinedOutputFile
name for ROOT file containing histos and/or trees produced by analysis
void AddTree(TTree *tree)
KVString fBranchName
name of branch which contains events to analyse
virtual void SlaveBegin(TTree *tree)
virtual void SlaveTerminate()
virtual void Terminate()
virtual Bool_t Notify()
void SetEventsReadInterval(Long64_t N)
void SetJobOutputFileName(const TString &filename)
const KVGVList * GetGVList(const KVString &list_name="default") const
void add_histo(TH1 *histo)
for merging with PROOF
void AddHisto(TH1 *histo)
void FillHisto(const Char_t *sname, Double_t one, Double_t two=1, Double_t three=1, Double_t four=1)
Bool_t AtEndOfRun(void)
KVUniqueNameList fGlobalVariables
Lists of global variables with different selection criteria.
TBranch * b_Event
List of branches.
void SetBranchName(const Char_t *n)
KVGVList * GetGVList(const KVString &list_name="default")
virtual void SetAdditionalBranchAddress()
Bool_t IsOptGiven(const Char_t *option)
void FillTProfile(TProfile *h1, Double_t one, Double_t two, Double_t three)
virtual void SetCurrentRun(KVDBRun *)
KVEvent * GetFriendEvent() const
KVParticleCondition fPartCond
(optional) conditions for selecting particles
KVEvent * GetEvent() const
void SetTriggerConditionsForRun(int)
void FillTH1(TH1 *h1, Double_t one, Double_t two)
virtual void SetObject(TObject *obj)
virtual void Init(TTree *tree)
void SetParticleConditionsParticleClassName(const KVString &t)
void add_tree(TTree *tree)
virtual void RecalculateGlobalVariables()
KVGVList * AddGVList(const KVString &list_name, const KVParticleCondition &selection=KVParticleCondition())
virtual Bool_t Process(Long64_t entry)
Bool_t fNotifyCalled
avoid multiple calls to Notify/InitRun
TString GetOpt(const Char_t *option) const
virtual void EndAnalysis()
virtual void ParseOptions()
void SetEvent(KVEvent *e)
Bool_t fGlobalVariableAbortEventAnalysis
KVEvent * AuxEvent
[optional] events in fAuxChain
Long64_t fEventsReadInterval
interval at which to print number of events read
HistoType * AddHisto(Args &&... args)
virtual void InitRun()
Int_t GetEventNumber() const
void FillTH3(TH3 *h3, Double_t one, Double_t two, Double_t three, Double_t four)
virtual TList * GetOutputList() const
KVString fPartName
(optional) classname for upcasting in KVParticleCondition::Optimize
void UnsetOpt(const Char_t *opt)
KVUserAnalysisOptionList fOptionList
parsed list of options given to TTree::Process
Abstract base class container for multi-particle events.
Definition: KVEvent.h:66
virtual void SetFrame(const Char_t *, const KVFrameTransform &)=0
Manage a list of global variables.
Definition: KVGVList.h:122
virtual void Add(TObject *obj)
Definition: KVGVList.cpp:279
KVVarGlob * GetGV(const Char_t *nom) const
Return pointer to global variable in list with name 'nom'.
Definition: KVGVList.cpp:262
Extended version of ROOT THashList.
Definition: KVHashList.h:28
virtual void SetOwner(Bool_t enable=kTRUE)
virtual TObject * FindObject(const char *name) const
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:72
Optimised list in which named objects can only be placed once.
virtual void Add(TObject *obj)
Handle list of options and input parameters for user analyis.
void SetOpt(const Char_t *option, const Char_t *value)
TString GetOpt(const Char_t *opt) const
Bool_t IsOptGiven(const Char_t *opt)
void UnsetOpt(const Char_t *opt)
Base class for all global variable implementations.
Definition: KVVarGlob.h:231
virtual Bool_t IsEmpty() const
virtual Int_t GetNbinsX() const
virtual const char * GetName() const
virtual const char * GetTitle() const
void AbstractMethod(const char *method) const
virtual const char * ClassName() const
virtual void Error(const char *method, const char *msgfmt,...) const
TList * fInput
TSelectorList * fOutput
TObject * fObject
const char * Data() const
virtual Int_t GetEntry(Long64_t entry, Int_t getall=0)
virtual TTree * GetTree() const
virtual Long64_t GetEntries() const
long long Long64_t
const Int_t n
TH1 * h
auto * l