GSI Object Oriented Online Offline (Go4)  GO4-5.3.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4EventElement.cxx
Go to the documentation of this file.
1 // $Id: TGo4EventElement.cxx 1488 2015-06-08 11:32:46Z linev $
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 f�r 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 "TTree.h"
17 #include "TDirectory.h"
18 #include "TROOT.h"
19 #include "Riostream.h"
20 
21 #include "TGo4Log.h"
22 #include "TGo4EventSource.h"
23 
26 #if ROOT_VERSION_CODE <= ROOT_VERSION(5,34,19)
27 R__EXTERN TTree *gTree;
28 #endif
29 
31  TNamed("Go4Element","This is a Go4 EventElement"),
32  fbIsValid(kTRUE),
33  fxParent(0),
34  fxEventSource(0),
35  fIdentifier(-1),
36  fDebug(kFALSE),
37  fbKeepContents(kFALSE)
38 {
39 
40  GO4TRACE((15,"TGo4EventElement::TGo4EventElement()",__LINE__, __FILE__));
41 }
42 
44  TNamed(name,"This is a Go4 EventElement"),
45  fbIsValid(kTRUE),
46  fxParent(0),
47  fxEventSource(0),
48  fIdentifier(-1),
49  fDebug(kFALSE),
50  fbKeepContents(kFALSE)
51 {
52  GO4TRACE((15,"TGo4EventElement::TGo4EventElement(const char*)",__LINE__, __FILE__));
53 }
54 
55 TGo4EventElement::TGo4EventElement(const char* aName, const char* aTitle, Short_t aBaseCat) :
56  TNamed(aName,aTitle),
57  fbIsValid(kTRUE),
58  fxParent(0),
59  fxEventSource(0),
60  fIdentifier(aBaseCat),
61  fDebug(kFALSE)
62 {
63 }
64 
66 {
67  GO4TRACE((15,"TGo4EventElement::~TGo4EventElement()",__LINE__, __FILE__));
68 }
69 
70 Bool_t TGo4EventElement::CheckEventSource(const char* classname)
71 {
72  GO4TRACE((12,"TGo4EventElement::CheckEventSource(const char*)",__LINE__, __FILE__));
73  return fxEventSource ? fxEventSource->InheritsFrom(classname) : kFALSE;
74 }
75 
77 {
78  GO4TRACE((12,"TGo4EventElement::PrintEvent()",__LINE__, __FILE__));
79 
80  TGo4Log::Info("EventElement printout: Name=%s IsValid=%s ", GetName(), fbIsValid ? "true" : "false");
81  if(fxEventSource)
82  TGo4Log::Info( "EventSource: %s of class %s", fxEventSource->GetName(), fxEventSource->ClassName() );
83  else
84  TGo4Log::Info( "NO EventSource");
85 }
86 
87 void TGo4EventElement::Print(Option_t* option) const
88 {
89  ((TGo4EventElement*)this) -> PrintEvent();
90 }
91 
93 {
94  if ((name==0) || (strlen(name)==0)) return this;
95 
96  if (strcmp(name,".")==0) return this;
97 
98  if (strcmp(name,"..")==0) return GetParent();
99 
100  return 0;
101 }
102 
103 
105 {
106  if (tree==0) return;
107 
108  TBranch* topb = 0;
109  TString searchname = GetName();
110  if (searchname.Length()>0) {
111  searchname += ".";
112  topb = tree->FindBranch(searchname.Data());
113  }
114 
115  // if no appropriate branches found, use first branch for the event
116  // TODO: should we check class name of the branch?
117  if (topb==0) topb = (TBranch*) tree->GetListOfBranches()->First();
118 
119  Int_t index = tree->GetListOfBranches()->IndexOf(topb);
120 
121  // FIXME SL: should we activate other branches, reading not working when all branches disabled in the beginning
122  // note: only deactivate subleafs _after_ address of top branch is set!
123  // tree->SetBranchStatus("*",0);
124 
125  activateBranch(topb, index, var_ptr);
126 }
127 
128 Int_t TGo4EventElement::activateBranch(TBranch *branch, Int_t init, TGo4EventElement** var_ptr)
129 {
130  if (branch==0) return 0;
131 
132  TString cad = branch->GetName();
133 
134  TTree* tree = branch->GetTree();
135 
136  if (var_ptr!=0) {
137  // SL 23.08.2013 - seems to be, at some point TTree::SetBranchAddress() signature was changed
138  // now one other need to specify pointer on actual class like
139  // UserEvent* ev = (UserEvent*) GetInputEvent();
140  // tree->SetBranchAddress("branch.", &ev);
141  // or one need explicitly specify which class we want to set to the branch
142  // No idea when it happens, but newest 5.34 ROOT no longer working correctly.
143 
144  TClass* cl = *var_ptr ? (*var_ptr)->IsA() : 0;
145  tree->SetBranchAddress(cad.Data(), var_ptr, 0, cl, kOther_t, true);
146  //tree->SetBranchAddress(cad.Data(), (void**) var_ptr);
147  }
148 
149  tree->SetBranchStatus(cad.Data(), 1);
150  cad+="*";
151  tree->SetBranchStatus(cad.Data(), 1);
152 
153  return 0;
154 }
155 
157 {
158  TString name = GetName();
159 #if ROOT_VERSION_CODE <= ROOT_VERSION(5,34,19)
160  name+=".";
161  gTree->SetBranchStatus(name.Data(), 0);
162  name+="*";
163  gTree->SetBranchStatus(name.Data(), 0);
164  TGo4Log::Info("-I- Deactivating elements at location : %s", name.Data());
165 #else
166  TGo4Log::Warn("-W- Could not deactivate() event element %s in this ROOT Version, do not use!", name.Data());
167 #endif
168 }
169 
171 {
172  TString name=GetName();
173  #if ROOT_VERSION_CODE <= ROOT_VERSION(5,34,19)
174  name+=".";
175  gTree->SetBranchStatus(name.Data(), 1);
176  name+="*";
177  gTree->SetBranchStatus(name.Data(), 1);
178  TGo4Log::Info("-I- Activating elements at location : %s", name.Data());
179 #else
180  TGo4Log::Warn("-W- Could not activate() element %s in this ROOT Version, do not use!", name.Data());
181 #endif
182 
183 }
184 
185 void TGo4EventElement::Clear(Option_t *)
186 {
187 }
188 
190 {
191  Int_t res(0);
192  Clear();
193  SetValid(kTRUE);
194  if (fxEventSource) {
195  TGo4Log::Info("Event %s has source %s class: %s", GetName(), fxEventSource->GetName(), fxEventSource->ClassName());
196  if (!fxEventSource->CheckEventClass(IsA())) {
197  TGo4Log::Error("Event %s, mismatch between event source and event class", GetName());
198  res = 1;
199  }
200  } else {
201  TGo4Log::Error("Event %s has no data source", GetName());
202  res = 1;
203  }
204  return res;
205 }
206 
208 {
209  if (!fbKeepContents) Clear();
210  fbKeepContents = kFALSE;
211  if (fxEventSource==0) { SetValid(kFALSE); return 1; }
212 
213  if (fxEventSource->BuildEvent(this)) {
214  //SetValid(kTRUE); // JAM: do not override event validity as specified by user
215  return 0;
216  }
217 
218  Int_t res = fxEventSource->GetEventStatus();
219 
220  SetValid(kFALSE);
221 
222  return res==0 ? 1 : res;
223 }
224 
225 
227 {
228  // create sample tree with event element as entry
229  // to be able use such tree later, one should provide 'sample' pointer to keep
230  // event instance associated with tree branch
231 
232  TDirectory* filsav = gDirectory;
233  gROOT->cd();
234  if (sample!=0) delete *sample;
235  TGo4EventElement* clone = (TGo4EventElement*) Clone();
236  TTree* thetree = new TTree(clone->GetName(), "Single Event Tree");
237  thetree->SetDirectory(0);
238  if (sample) *sample = clone;
239  thetree->Branch("Go4EventSample", clone->ClassName(), sample ? sample : &clone, 64000, 99);
240  thetree->Fill();
241  filsav->cd();
242  if (sample==0) delete clone;
243  return thetree;
244 }
245 
247 {
248  TGo4EventElement* sample = 0;
249 
250  TTree* tr = CreateSampleTree(&sample);
251 
252  if (tr) tr->Show(0);
253  std::cout << std::endl;
254  delete tr;
255  delete sample;
256 }
257 
virtual TGo4EventElement * GetChild(const char *name)
void SetValid(Bool_t on)
R__EXTERN TTree * gTree
virtual Bool_t CheckEventClass(TClass *cl)
virtual ~TGo4EventElement(void)
static void Warn(const char *text,...)
Definition: TGo4Log.cxx:296
virtual void deactivate()
virtual void PrintEvent()
Bool_t CheckEventSource(const char *classname)
virtual Int_t Fill()
virtual Bool_t BuildEvent(TGo4EventElement *dest)
virtual void Clear(Option_t *t="")
virtual TTree * CreateSampleTree(TGo4EventElement **sample=0)
TGo4EventElement * GetParent() const
Int_t GetEventStatus() const
virtual void synchronizeWithTree(TTree *tree, TGo4EventElement **var_ptr=0)
virtual Int_t Init()
#define GO4TRACE(X)
Definition: TGo4Log.h:26
virtual void activate()
virtual void Print(Option_t *option="") const
Bool_t fbKeepContents
Debug level.
TGo4EventSource * fxEventSource
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
static void Info(const char *text,...)
Definition: TGo4Log.cxx:283
virtual Int_t activateBranch(TBranch *branch, Int_t index=0, TGo4EventElement **var_ptr=0)