KaliVeda
Toolkit for HIC analysis
KVEventSelector.h
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 "KVEvent.h"
9 #include "KVGVList.h"
10 #include "KVString.h"
11 #include "KVParticleCondition.h"
12 #include "KVHashList.h"
13 #include <TH3.h>
14 #include <TH2.h>
15 #include <TProfile2D.h>
16 #include "KVUserAnalysisOptionList.h"
17 #ifdef WITH_PROOF
18 #include "TProofOutputFile.h"
19 #endif
20 #include "KVDataAnalyser.h"
21 
193 class KVEventSelector : public TSelector {
194 
195 protected :
198 
202 
205 
209 
211 
213 
215 
219 
222 
225 
228 
230  TFile* writeFile = nullptr;
231 #ifdef WITH_PROOF
232  TProofOutputFile* mergeFile = nullptr;
233 #endif
234  void add_histo(TH1* histo);
235  void add_tree(TTree* tree);
236  void FillTH1(TH1* h1, Double_t one, Double_t two);
237  void FillTProfile(TProfile* h1, Double_t one, Double_t two, Double_t three);
238  void FillTH2(TH2* h2, Double_t one, Double_t two, Double_t three);
239  void FillTProfile2D(TProfile2D* h2, Double_t one, Double_t two, Double_t three, Double_t four);
240  void FillTH3(TH3* h3, Double_t one, Double_t two, Double_t three, Double_t four);
241 
242  void SetUpAuxEventChain();
243  void InitFriendTree(TTree* tree, const TString& branchname);
244  void SetCombinedOutputFile(const TString& filename)
245  {
261 
264  }
265  void create_output_file();
266  virtual void CheckEndOfRun();
267  void SetEvent(KVEvent* e)
268  {
269  Event = e;
270  }
271  virtual void ParseOptions();
273  {
275  return (fTreeEntry + 1 == fChain->GetTree()->GetEntries());
276  }
279  {
283  if (gDataAnalyser->GetKinematics())
284  GetEvent()->SetFrame("CM", gDataAnalyser->GetKinematics()->GetCMVelocity());
285  }
286 
287 
288 public:
289  Bool_t CreateTreeFile(const Char_t* filename = "");
290 
291  KVEventSelector(TTree* /*tree*/ = 0) : fChain(0), fAuxChain(0), fBranchName("data"), fFirstEvent(kTRUE),
293  {
295  }
297  {
299  }
300  Int_t Version() const override
301  {
302  return 3;
303  }
304  void Begin(TTree* tree) override;
305  void SlaveBegin(TTree* tree) override;
306  void Init(TTree* tree) override;
307 
308  Bool_t Notify() override;
309  Bool_t Process(Long64_t entry) override;
310  Int_t GetEntry(Long64_t entry, Int_t getall = 0) override
311  {
312  return fChain ? fChain->GetTree()->GetEntry(entry, getall) : 0;
313  }
315  {
316  return fAuxChain ? fAuxChain->GetTree()->GetEntry(entry, getall) : 0;
317  }
319  {
320  return AuxEvent;
321  }
322  void SetObject(TObject* obj) override
323  {
324  fObject = obj;
325  }
326  void SetInputList(TList* input) override
327  {
328  fInput = input;
329  }
330  TList* GetOutputList() const override
331  {
332  return fOutput;
333  }
334  void SlaveTerminate() override;
335  void Terminate() override;
336 
337  void SetBranchName(const Char_t* n)
338  {
339  fBranchName = n;
340  }
341  const Char_t* GetBranchName() const
342  {
343  return fBranchName.Data();
344  }
345  virtual void SetCurrentRun(KVDBRun*) {}
346 
352  KVEvent* GetEvent() const
353  {
354  return Event;
355  }
360  {
361  if (!GetEvent()) {
362  Error("GetEventNumber", "No event defined!!!");
363  return -1;
364  }
365  return GetEvent()->GetNumber();
366  }
367 
400  virtual void InitAnalysis()
401  {
402  AbstractMethod("InitAnalysis");
403  }
421  virtual void InitRun()
422  {
423  AbstractMethod("InitRun");
424  }
456  virtual Bool_t Analysis()
457  {
458  return kTRUE;
459  }
460  virtual void EndRun()
461  {
462  AbstractMethod("EndRun");
463  }
464  virtual void EndAnalysis()
465  {
466  AbstractMethod("EndAnalysis");
467  }
468 
476  KVGVList* AddGVList(const KVString& list_name, const KVParticleCondition& selection = KVParticleCondition())
477  {
478  if (list_name != "" && GetGVList(list_name)) {
479  Error("AddGVList", "You tried to add a list with the same name '%s' as an existing list: no list created",
480  list_name.Data());
481  return nullptr;
482  }
483  auto gvl = new KVGVList(list_name, selection);
484  fGlobalVariables.Add(gvl);
485  return gvl;
486  }
502  KVGVList* GetGVList(const KVString& list_name = "default")
503  {
504  auto l = (KVGVList*)fGlobalVariables.FindObject(list_name);
505  if (!l) {
506  if (list_name == "default") {
507  l = new KVGVList("default");
509  }
510  else if (list_name == "__internal__") {
511  l = new KVGVList("__internal__", KVParticleCondition());
513  }
514  }
515  return l;
516  }
517 
522  const KVGVList* GetGVList(const KVString& list_name = "default") const
523  {
524  return (KVGVList*)fGlobalVariables.FindObject(list_name);
525  }
533  void AddGV(KVVarGlob* vg)
534  {
535  if (!vg)
536  Error("AddGV(KVVarGlob*)", "KVVarGlob pointer is null");
537  else
538  GetGVList()->Add(vg);
539  }
548  KVVarGlob* AddGV(const Char_t* class_name, const Char_t* name)
549  {
550  return GetGVList()->AddGV(class_name, name);
551  }
552 
561  KVVarGlob* GetGV(const Char_t* name) const
562  {
563  auto gv = GetGVList()->GetGV(name);
564  if (!gv) {
565  Error("GetGV(const Char_t* name)", "Request for undefined global variable name=%s", name);
566  }
567  return gv;
568  }
569 
579  {
580  fPartCond = cond;
581  }
582 
583  void AddHisto(TH1* histo)
584  {
585  Deprecate(Form("You should use e.g. 'auto h = AddHisto<%s>(\"%s\",\"%s\",%d,...);' to add histograms to your analysis.",
586  histo->ClassName(), histo->GetName(), histo->GetTitle(), histo->GetNbinsX()));
587  add_histo(histo);
588  }
589 
605  template<typename HistoType, typename... Args>
606  HistoType* AddHisto(Args&& ... args)
607  {
608  auto h = new HistoType(std::forward<Args>(args)...);
609  add_histo(h);
610  return h;
611  }
612 
614  {
615  Deprecate(Form("You should use e.g. 'auto t = AddTree(\"%s\", \"%s\");' to add a TTree to your analysis",
616  tree->GetName(), tree->GetTitle()));
617  add_tree(tree);
618  }
619 
628  TTree* AddTree(const TString& name, const TString& title = "")
629  {
630  auto t = new TTree(name, title);
631  add_tree(t);
632  return t;
633  }
634 
635  void FillHisto(const Char_t* sname, Double_t one, Double_t two = 1, Double_t three = 1, Double_t four = 1);
636  void FillHisto(const Char_t* sname, const Char_t* label, Double_t weight = 1);
637 
643  void FillTree(const Char_t* tree_name = "");
644 
648  const KVHashList* GetHistoList() const
649  {
650  return &lhisto;
651  }
652 
657  const KVHashList* GetTreeList() const
658  {
659  return &ltree;
660  }
661 
666  TH1* GetHisto(const Char_t* name) const
667  {
668  return lhisto.get_object<TH1>(name);
669  }
670 
675  TTree* GetTree(const Char_t* name) const
676  {
678  if (!t) Fatal("GetTree", "Tree %s not found: is this the right name?", name);
679  return t;
680  }
681 
682  virtual void SaveHistos(const Char_t* = "", Option_t* = "recreate", Bool_t = kFALSE)
683  {
684  Error("SaveHistos", "Method no longer implemented. Does nothing.");
685  }
686 
687  void SetOpt(const Char_t* option, const Char_t* value)
688  {
690  }
691  Bool_t IsOptGiven(const Char_t* option)
692  {
693  return fOptionList.IsOptGiven(option);
694  }
695  TString GetOpt(const Char_t* option) const
696  {
697  return fOptionList.GetOpt(option);
698  }
699  void UnsetOpt(const Char_t* opt)
700  {
701  fOptionList.UnsetOpt(opt);
702  }
703 
711  {
712  }
713 
723  void SetJobOutputFileName(const TString& filename)
724  {
725  if (KVDataAnalyser::IsRunningBatchAnalysis() && (gDataAnalyser->GetProofMode() == KVDataAnalyser::EProofMode::None))
726  SetCombinedOutputFile(Form("%s.root", gDataAnalyser->GetBatchSystem()->GetJobName()));
727  else
729  }
730 
738  {
739  gDataAnalyser->SetTriggerConditionsForRun(run);
740  }
741  ClassDefOverride(KVEventSelector, 0)//General purpose analysis class for TTrees containing KVEvent objects
742 };
743 
748 #define AddVar(var,type) Branch(dadastr(var), &var, didixstr(duduvartype(var,type)))
749 #define AddVarBranch(var,branch,type) Branch(dadastr(branch), &var, didixstr(duduvartype(branch,type)))
750 #define duduvartype(var,type) var/type
751 #define didixstr(s) dadastr(s)
752 #define dadastr(s) #s
753 
754 #endif
int Int_t
#define e(i)
bool Bool_t
char Char_t
constexpr Bool_t kFALSE
double Double_t
constexpr Bool_t kTRUE
const char Option_t
#define ClassDefOverride(name, id)
#define N
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
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 Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char filename
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void value
char name[80]
char * Form(const char *fmt,...)
TVector3 GetCMVelocity() const
Return vector velocity of centre of mass of reaction (units: cm/ns)
Definition: KV2Body.cpp:572
UInt_t GetNumber() const
Definition: KVBase.h:219
virtual const Char_t * GetJobName() const
Description of an experimental run in database ,,.
Definition: KVDBRun.h:41
virtual void SetTriggerConditionsForRun(int)
virtual const KVBatchSystem * GetBatchSystem()
virtual const KV2Body * GetKinematics() const
EProofMode GetProofMode() const
static Bool_t IsRunningBatchAnalysis()
General purpose analysis base class for TTree containing KVEvent objects.
Bool_t Process(Long64_t entry) override
void FillTProfile2D(TProfile2D *h2, Double_t one, Double_t two, Double_t three, Double_t four)
virtual void SaveHistos(const Char_t *="", Option_t *="recreate", Bool_t=kFALSE)
void AddGV(KVVarGlob *vg)
KVVarGlob * GetGV(const Char_t *name) const
Bool_t fDisableCreateTreeFile
used with PROOF
void SetObject(TObject *obj) override
KVEventSelector(TTree *=0)
Bool_t fFirstEvent
set to kFALSE after first event is read
Int_t GetFriendTreeEntry(Long64_t entry, Int_t getall=0)
KVHashList lhisto
list of histograms added with AddHisto
const Char_t * GetBranchName() const
virtual void InitAnalysis()
KVHashList ltree
list of trees added with AddTree
void InitFriendTree(TTree *tree, const TString &branchname)
TTree * AddTree(const TString &name, const TString &title="")
void SetParticleConditions(const KVParticleCondition &cond)
void SetOpt(const Char_t *option, const Char_t *value)
virtual void EndRun()
void Init(TTree *tree) override
TTree * fAuxChain
[optional] pointer to another TTree or TChain which may be used during analysis
void SetCombinedOutputFile(const TString &filename)
virtual Bool_t Analysis()
KVEvent * Event
Declaration of leaf types.
Long64_t fEventsRead
number of events read
virtual void CheckEndOfRun()
void FillTH2(TH2 *h2, Double_t one, Double_t two, Double_t three)
Long64_t fTreeEntry
current tree entry number
TTree * fChain
pointer to the analyzed TTree or TChain
void SetInputList(TList *input) override
Bool_t Notify() override
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)
Int_t GetEntry(Long64_t entry, Int_t getall=0) override
KVString fBranchName
name of branch which contains events to analyse
Int_t Version() const override
TH1 * GetHisto(const Char_t *name) const
void SetEventsReadInterval(Long64_t N)
void SetJobOutputFileName(const TString &filename)
const KVGVList * GetGVList(const KVString &list_name="default") const
void add_histo(TH1 *histo)
output file for trees and histos
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)
const KVHashList * GetTreeList() const
GetTreeList.
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 SlaveTerminate() override
void FillTH1(TH1 *h1, Double_t one, Double_t two)
void add_tree(TTree *tree)
void SlaveBegin(TTree *tree) override
void RecalculateGlobalVariables()
void SetTriggerConditionsForRun(int run)
KVVarGlob * AddGV(const Char_t *class_name, const Char_t *name)
TTree * GetTree(const Char_t *name) const
const KVHashList * GetHistoList() const
KVGVList * AddGVList(const KVString &list_name, const KVParticleCondition &selection=KVParticleCondition())
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)
TList * GetOutputList() const override
virtual void InitRun()
Int_t GetEventNumber() const
void FillTH3(TH3 *h3, Double_t one, Double_t two, Double_t three, Double_t four)
void FillTree(const Char_t *tree_name="")
void Terminate() override
void Begin(TTree *tree) override
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:67
virtual void SetFrame(const Char_t *, const KVFrameTransform &)=0
#define KVGVLIST_OPTIMIZE_GVLIST
Definition: KVGVList.h:227
void Add(TObject *obj) override
Definition: KVGVList.cpp:336
KVVarGlob * AddGV(const Char_t *class_name, const Char_t *name)
Definition: KVGVList.cpp:705
KVVarGlob * GetGV(const Char_t *nom) const
Return pointer to global variable in list with name 'nom'.
Definition: KVGVList.cpp:319
Extended version of ROOT THashList.
Definition: KVHashList.h:29
Implement selections for KVNucleus objects.
T * get_object(const TString &name) const
TObject * FindObject(const char *name) const override
void SetOwner(Bool_t enable=kTRUE) override
Extension of ROOT TString class which allows backwards compatibility with ROOT v3....
Definition: KVString.h:73
Optimised list in which named objects can only be placed once.
void AddFirst(TObject *obj) override
void Add(TObject *obj) override
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:233
virtual Int_t GetNbinsX() const
const char * GetName() const override
const char * GetTitle() const override
void AbstractMethod(const char *method) const
virtual const char * ClassName() const
virtual void Error(const char *method, const char *msgfmt,...) const
virtual void Fatal(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 Long64_t GetEntries() const
virtual TTree * GetTree() const
long long Long64_t
const Int_t n
TH1 * h
TLine l