GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4EventElement.cxx
Go to the documentation of this file.
1 // $Id$
2 //-----------------------------------------------------------------------
3 // The GSI Online Offline Object Oriented (Go4) Project
4 // Experiment Data Processing at EE department, GSI
5 //-----------------------------------------------------------------------
6 // Copyright (C) 2000- GSI Helmholtzzentrum fuer Schwerionenforschung GmbH
7 // Planckstr. 1, 64291 Darmstadt, Germany
8 // Contact: http://go4.gsi.de
9 //-----------------------------------------------------------------------
10 // This software can be used under the license agreements as stated
11 // in Go4License.txt file which is part of the distribution.
12 //-----------------------------------------------------------------------
13 
14 #include "TGo4EventElement.h"
15 
16 #include <iostream>
17 
18 #include "TTree.h"
19 #include "TDirectory.h"
20 #include "TROOT.h"
21 #include "TBranch.h"
22 
23 #include "TGo4Log.h"
24 #include "TGo4EventSource.h"
25 
27  TNamed("Go4Element","This is a Go4 EventElement")
28 {
29  GO4TRACE((15,"TGo4EventElement::TGo4EventElement()",__LINE__, __FILE__));
30 }
31 
33  TNamed(name,"This is a Go4 EventElement")
34 {
35  GO4TRACE((15,"TGo4EventElement::TGo4EventElement(const char *)",__LINE__, __FILE__));
36 }
37 
38 TGo4EventElement::TGo4EventElement(const char *aName, const char *aTitle, Short_t aBaseCat) :
39  TNamed(aName,aTitle),
40  fIdentifier(aBaseCat)
41 {
42 }
43 
45 {
46  GO4TRACE((15,"TGo4EventElement::~TGo4EventElement()",__LINE__, __FILE__));
47 }
48 
49 Bool_t TGo4EventElement::CheckEventSource(const char *classname)
50 {
51  GO4TRACE((12,"TGo4EventElement::CheckEventSource(const char *)",__LINE__, __FILE__));
52  return fxEventSource ? fxEventSource->InheritsFrom(classname) : kFALSE;
53 }
54 
56 {
57  GO4TRACE((12,"TGo4EventElement::PrintEvent()",__LINE__, __FILE__));
58 
59  TGo4Log::Info("EventElement printout: Name=%s IsValid=%s ", GetName(), fbIsValid ? "true" : "false");
60  if(fxEventSource)
61  TGo4Log::Info( "EventSource: %s of class %s", fxEventSource->GetName(), fxEventSource->ClassName() );
62  else
63  TGo4Log::Info( "NO EventSource");
64 }
65 
66 void TGo4EventElement::Print(Option_t *option) const
67 {
68  const_cast<TGo4EventElement *>(this)->PrintEvent();
69 }
70 
72 {
73  if (!name || (strlen(name) == 0)) return this;
74 
75  if (strcmp(name,".") == 0) return this;
76 
77  if (strcmp(name,"..") == 0) return GetParent();
78 
79  return nullptr;
80 }
81 
82 
84 {
85  if (!tree) return;
86 
87  TBranch *topb = nullptr;
88  TString searchname = GetName();
89  if (searchname.Length()>0) {
90  searchname += ".";
91  topb = tree->FindBranch(searchname.Data());
92  }
93 
94  // if no appropriate branches found, use first branch for the event
95  // TODO: should we check class name of the branch?
96  if (!topb) topb = (TBranch *) tree->GetListOfBranches()->First();
97 
98  Int_t index = tree->GetListOfBranches()->IndexOf(topb);
99 
100  // FIXME SL: should we activate other branches, reading not working when all branches disabled in the beginning
101  // note: only deactivate subleafs _after_ address of top branch is set!
102  // tree->SetBranchStatus("*",0);
103 
104  activateBranch(topb, index, var_ptr);
105 }
106 
107 Int_t TGo4EventElement::activateBranch(TBranch *branch, Int_t init, TGo4EventElement **var_ptr)
108 {
109  if (!branch) return 0;
110 
111  TString cad = branch->GetName();
112 
113  TTree *tree = branch->GetTree();
114 
115  if (var_ptr) {
116  // SL 23.08.2013 - seems to be, at some point TTree::SetBranchAddress() signature was changed
117  // now one other need to specify pointer on actual class like
118  // UserEvent* ev = (UserEvent*) GetInputEvent();
119  // tree->SetBranchAddress("branch.", &ev);
120  // or one need explicitly specify which class we want to set to the branch
121  // No idea when it happens, but newest 5.34 ROOT no longer working correctly.
122 
123  TClass *cl = *var_ptr ? (*var_ptr)->IsA() : nullptr;
124  tree->SetBranchAddress(cad.Data(), var_ptr, nullptr, cl, kOther_t, true);
125  //tree->SetBranchAddress(cad.Data(), (void **) var_ptr);
126  }
127 
128  tree->SetBranchStatus(cad.Data(), 1);
129  cad += "*";
130  tree->SetBranchStatus(cad.Data(), 1);
131 
132  return 0;
133 }
134 
136 {
137  TGo4Log::Warn("-W- Could not deactivate() event element %s in this ROOT Version, do not use!", GetName());
138 }
139 
141 {
142  TGo4Log::Warn("-W- Could not activate() element %s in this ROOT Version, do not use!", GetName());
143 }
144 
145 void TGo4EventElement::Clear(Option_t *)
146 {
147 }
148 
150 {
151  Int_t res = 0;
152  Clear();
153  SetValid(kTRUE);
154  if (fxEventSource) {
155  TGo4Log::Info("Event %s has source %s class: %s", GetName(), fxEventSource->GetName(), fxEventSource->ClassName());
156  if (!fxEventSource->CheckEventClass(IsA())) {
157  TGo4Log::Error("Event %s, mismatch between event source and event class", GetName());
158  res = 1;
159  }
160  } else {
161  TGo4Log::Error("Event %s has no data source", GetName());
162  res = 1;
163  }
164  return res;
165 }
166 
168 {
169  if (!fbKeepContents) Clear();
170  fbKeepContents = kFALSE;
171  if (!fxEventSource) { SetValid(kFALSE); return 1; }
172 
173  if (fxEventSource->BuildEvent(this)) {
174  //SetValid(kTRUE); // JAM: do not override event validity as specified by user
175  return 0;
176  }
177 
178  Int_t res = fxEventSource->GetEventStatus();
179 
180  SetValid(kFALSE);
181 
182  return res == 0 ? 1 : res;
183 }
184 
185 
187 {
188  // create sample tree with event element as entry
189  // to be able use such tree later, one should provide 'sample' pointer to keep
190  // event instance associated with tree branch
191 
192  TDirectory *filsav = gDirectory;
193  gROOT->cd();
194  if (sample) {
195  delete *sample;
196  *sample = nullptr;
197  }
198  TGo4EventElement *clone = (TGo4EventElement *) Clone();
199  TTree *thetree = new TTree(clone->GetName(), "Single Event Tree");
200  thetree->SetDirectory(nullptr);
201  if (sample) *sample = clone;
202  thetree->Branch("Go4EventSample", clone->ClassName(), sample ? sample : &clone, 64000, 99);
203  thetree->Fill();
204  filsav->cd();
205  if (!sample) delete clone;
206  return thetree;
207 }
208 
210 {
211  TGo4EventElement *sample = nullptr;
212 
213  TTree *tr = CreateSampleTree(&sample);
214 
215  if (tr) tr->Show(0);
216  std::cout << std::endl;
217  delete tr;
218  delete sample;
219 }
220 
virtual TGo4EventElement * GetChild(const char *name)
void SetValid(Bool_t on)
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
virtual Bool_t CheckEventClass(TClass *cl)
virtual TTree * CreateSampleTree(TGo4EventElement **sample=nullptr)
virtual void deactivate()
virtual void PrintEvent()
virtual void synchronizeWithTree(TTree *tree, TGo4EventElement **var_ptr=nullptr)
Bool_t CheckEventSource(const char *classname)
virtual Int_t Fill()
virtual Bool_t BuildEvent(TGo4EventElement *dest)
TGo4EventElement * GetParent() const
void Clear(Option_t *opt="") override
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
void Print(Option_t *option="") const override
virtual Int_t Init()
#define GO4TRACE(X)
Definition: TGo4Log.h:25
virtual void activate()
Bool_t fbKeepContents
Debug level.
TGo4EventSource * fxEventSource
Int_t GetEventStatus() const
static void Warn(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:307
virtual Int_t activateBranch(TBranch *branch, Int_t index=0, TGo4EventElement **var_ptr=nullptr)
virtual ~TGo4EventElement()