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