GSI Object Oriented Online Offline (Go4)  GO4-6.1.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4TreeStore.cxx
Go to the documentation of this file.
1 // $Id: TGo4TreeStore.cxx 2748 2020-04-16 09:50:56Z 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 "TGo4TreeStore.h"
15 
16 #include "TFile.h"
17 #include "TTree.h"
18 #include "TBranch.h"
19 
20 #include "TGo4Log.h"
21 #include "TGo4EventElement.h"
22 #include "TGo4Parameter.h"
23 #include "TGo4TreeStoreParameter.h"
24 #include "TGo4MainTree.h"
25 
26 const char* TGo4TreeStore::fgcFILESUF = ".root";
27 
29  TGo4EventElement* event,
30  Int_t splitlevel,
31  Int_t bufsize,
32  const char* filename,
33  Int_t compression) :
34  TGo4EventStore(name),
35  fxBranch(0), fxEvent(event), fiSplit(splitlevel), fiBufsize(bufsize)
36 {
37  GO4TRACE((15,"TGo4TreeStore::TGo4TreeStore(const char*,...)", __LINE__, __FILE__));
38 
39  TString buffer = TString::Format("%s.", name); // for branches containing the same event structure
41  if(fxEvent) {
42  fcEventClass = fxEvent->ClassName();
44  fxBranch = fxTree->GetBranch(buffer.Data());
45  if(fxBranch) {
46  // tree already had branch of our name, check it
47  TGo4Log::Debug(" TreeStore: Found existing branch %s , reset it ", name);
48  fxBranch->Reset();
49  } else {
50  // no such branch existing, create a new one
51  fxBranch = fxTree->Branch(buffer, fcEventClass.Data(), &fxEvent, fiBufsize, fiSplit);
52  TGo4Log::Debug(" TreeStore: Created new branch %s ", name);
53  } // if(fxBranch)
54  if(filename) {
55  buffer = TString::Format("%s%s", filename, fgcFILESUF);
56  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 tree store", compression);
57  fxBranch->SetFile(fxFile);
58  TGo4Log::Debug(" TreeStore: Set branch to file %s ", filename);
59  TGo4Log::Info("TGo4TreeStore: Open file tree %s UPDATE", buffer.Data());
60  } else {
61  fxFile=0;
62  } // if(filename)
63  }
64  else
65  {
66  TGo4Log::Debug(" !!! TreeStore creation ERROR: no event structure !!! ");
67  } // if(fxEvent)
68 }
69 
71 : TGo4EventStore("dummy"),
72  fxBranch(0), fxEvent(event), fiSplit(par->fiSplit), fiBufsize(par->fiBufsize)
73 {
74  GO4TRACE((15,"TGo4TreeStore::TGo4TreeStore(const char*,...)", __LINE__, __FILE__));
75 
76  SetName(par->GetName());
77  TString buffer = TString::Format("%s.", par->GetName());
79  if(fxEvent) {
80  fcEventClass = fxEvent->ClassName();
81  fxTree=fxSingletonTree->GetTree();
82  fxBranch= fxTree->GetBranch(buffer.Data());
83  if(fxBranch) {
84  // tree already had branch of our name, check it
85  TGo4Log::Debug(" TreeStore: Found existing branch %s , reset it ", buffer.Data());
86  fxBranch->Reset();
87  } else {
88  // no such branch existing, create a new one
89  fxBranch = fxTree->Branch(buffer.Data(), fcEventClass.Data(), &fxEvent, fiBufsize, fiSplit);
90  TGo4Log::Debug(" TreeStore: Created new branch %s ", buffer.Data());
91  } // if(fxBranch)
92  if(!par->fxBranchFile.IsNull())
93  {
94  buffer = TString::Format("%s%s", par->fxBranchFile.Data(), fgcFILESUF);
95  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 tree store", par->fiCompression);
96  fxBranch->SetFile(fxFile);
97  TGo4Log::Debug(" TreeStore: Set branch to file %s ", buffer.Data());
98  TGo4Log::Info("TGo4TreeStore: Open file tree %s UPDATE", buffer.Data());
99  }
100  else
101  {
102  fxFile=0;
103  } // if(filename)
104 
105  }
106  else
107  {
108  TGo4Log::Debug(" !!! TreeStore creation ERROR: no event structure !!! ");
109  } // if(fxEvent)
110 }
111 
113 :TGo4EventStore("Go4 Default Tree Store"),
114  fxBranch(0), fxEvent(0), fiSplit(0), fiBufsize(0)
115 {
116  GO4TRACE((15,"TGo4TreeStore::TGo4TreeStore()", __LINE__, __FILE__));
117  // public default ctor for streamer
118 }
119 
120 
122 {
123  GO4TRACE((15,"TGo4TreeStore::~TGo4TreeStore()", __LINE__, __FILE__));
124  //delete fxFile; // closes File <- done by maintree dtor
125 }
126 
127 
128 
130 {
131  GO4TRACE((12,"TGo4TreeStore::Store(TGo4EventElement*)", __LINE__, __FILE__));
132 
133  Int_t rev=-1;
134  // check if new event is of the right class
135  if(fcEventClass == event->ClassName()) {
136  // event class is matching, we fill it into our tree
137  fxEvent = event;
138  fxBranch->SetAddress(&fxEvent);
139  fxBranch->Fill();
140  rev=0;
141  } else {
142  // new event does not match the old ones, we ignore it
143  TGo4Log::Debug(" !!! TreeStore::Store ERROR: this event does not match !!! ");
144  fxEvent->Clear();
145  fxEvent->SetValid(kFALSE);
146  fxBranch->SetAddress(&fxEvent);
147  fxBranch->Fill(); // we fill dummy event for consistency
148  rev=1;
149  }
150  //fxBranch->Write();
151  return rev;
152 }
153 
154 
156 {
157  GO4TRACE((12,"TGo4TreeStore::Store(TGo4Parameter*)", __LINE__, __FILE__));
158  if(cali) {
159  TString oldname = cali->GetName();
160  cali->SetName(Form("%s_%d", oldname.Data(), fxSingletonTree->GetCurrentIndex()));
161  fxSingletonTree->fxFile->cd(); // we are main tree's friend
162  cali->Write(0, TObject::kOverwrite);
163  cali->SetName(oldname.Data());
164  }
165 
166  return 0;
167 }
168 
170 {
171  GO4TRACE((12,"TGo4TreeStore::SetCompression(Int_t)", __LINE__, __FILE__));
172  fxFile->SetCompressionLevel(comp);
173 }
static TGo4MainTree * Instance()
void SetValid(Bool_t on)
TGo4MainTree * fxSingletonTree
Definition: TGo4TreeStore.h:81
TTree * GetTree()
Definition: TGo4MainTree.h:39
TFile * fxFile
Definition: TGo4TreeStore.h:76
TTree * fxTree
Definition: TGo4TreeStore.h:84
TFile * fxFile
Definition: TGo4MainTree.h:93
void SetCompression(Int_t comp)
static const char * fgcFILESUF
Definition: TGo4TreeStore.h:70
virtual void Clear(Option_t *t="")
Int_t GetCurrentIndex() const
Definition: TGo4MainTree.h:66
TString fcEventClass
Definition: TGo4TreeStore.h:96
virtual Int_t Store(TGo4EventElement *event)
TBranch * fxBranch
Definition: TGo4TreeStore.h:87
#define GO4TRACE(X)
Definition: TGo4Log.h:26
TGo4EventElement * fxEvent
Definition: TGo4TreeStore.h:90
static void Info(const char *text,...)
Definition: TGo4Log.cxx:287
virtual ~TGo4TreeStore()
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:274