GSI Object Oriented Online Offline (Go4)  GO4-5.3.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
TGo4FileStore.cxx
Go to the documentation of this file.
1 // $Id: TGo4FileStore.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 "TGo4FileStore.h"
15 
16 #include "TFolder.h"
17 #include "TFile.h"
18 #include "TTree.h"
19 
20 #include "TGo4Log.h"
21 #include "TGo4EventElement.h"
22 #include "TGo4CompositeEvent.h"
23 #include "TGo4Parameter.h"
24 #include "TGo4Condition.h"
25 #include "TGo4Fitter.h"
26 #include "TGo4FileStoreParameter.h"
27 
28 const char* TGo4FileStore::fgcTREESUF = "xTree";
29 const char* TGo4FileStore::fgcFILESUF = ".root";
30 const char* TGo4FileStore::fgcEVBRANCHNAME = "Go4EventBranch.";
31 Long64_t TGo4FileStore::fgiFILESPLITSIZE = 1900000000;
32 
33 
35  TGo4EventStore("Go4 Default File Store"),
36  fxFile(0),
37  fxTree(0),
38  fbBranchExists(kFALSE),
39  fxEvent(0),
40  fiSplit(1),
41  fiBufsize(0),
42  fiFillCount(0)
43 {
44  GO4TRACE((15,"TGo4FileStore::TGo4FileStore()", __LINE__, __FILE__));
45  // public default ctor for streamer
46  TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
47 
48 }
49 
51  Int_t splitlevel,
52  Int_t compression,
53  Bool_t overwrite,
54  Int_t autosavesize,
55  Int_t bufsize) :
56  TGo4EventStore(name),
57  fxFile(0),
58  fxTree(0),
59  fbBranchExists(kFALSE),
60  fxEvent(0),
61  fiSplit(splitlevel),
62  fiBufsize(bufsize),
63  fiFillCount(0)
64 {
65  GO4TRACE((15,"TGo4FileStore::TGo4FileStore(char*,...)", __LINE__, __FILE__));
66  TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
67  TString buffer(name);
68  if(strstr(buffer.Data(), fgcFILESUF)==0) buffer.Append(fgcFILESUF);
69  if(overwrite) {
70  fxFile = TFile::Open(buffer.Data(), "RECREATE", "Go4 file store", compression);
71  TGo4Log::Info("TGo4FileStore: Open file %s RECREATE", buffer.Data());
72  } else {
73  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 file store", compression);
74  TGo4Log::Info("TGo4FileStore: Open file %s UPDATE", buffer.Data());
75  }
76 
77  // strip any path information from treename:
78  const char* lastname = name;
79  const char* oldname = name;
80  lastname = strstr(oldname,"/");
81  while(lastname!=0) {
82  oldname=lastname+1;
83  lastname=strstr(oldname,"/");
84  }
85  buffer = oldname;
86  buffer += fgcTREESUF;
87  fxTree = dynamic_cast<TTree*> (fxFile->Get(buffer.Data()));
88  if(fxTree)
89  {
90  TGo4Log::Debug(" Tree %s has been found in file %s ",buffer.Data(), fxFile->GetName());
91  fiFillCount = (Int_t) (fxTree->GetEntries());
92  }
93  else
94  {
95  fxTree = new TTree(buffer.Data(), "Go4FileStore");
96  fxTree->SetAutoSave(autosavesize);
97  fxTree->Write();
98  TGo4Log::Debug(" Tree %s has been created in file %s ",buffer.Data(), fxFile->GetName());
99  }
100 }
101 
102 
104  TGo4EventStore("dummy"),
105  fbBranchExists(kFALSE),
106  fxEvent(0),
107  fiSplit(1),
108  fiBufsize(0),
109  fiFillCount(0)
110 {
111  GO4TRACE((15,"TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par)", __LINE__, __FILE__));
112 
113  if (par==0) {
114  TGo4Log::Error("TGo4FileStore::TGo4FileStore(.., TGo4FileStoreParameter* is not specified");
115  return;
116  }
117 
118  fiSplit = par->GetSplitlevel();
119  fiBufsize = par->GetBufsize();
120 
121  TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
122 
123  TString buffer = par->GetName();
124  SetName(buffer.Data());
125  if(!buffer.Contains(fgcFILESUF)) buffer.Append(fgcFILESUF);
126  if(par->IsOverwriteMode()) {
127  fxFile = TFile::Open(buffer.Data(), "RECREATE", "Go4 file store", par->GetCompression());
128  TGo4Log::Info("TGo4FileStore: Open file %s RECREATE", buffer.Data());
129  } else {
130  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 file store", par->GetCompression());
131  TGo4Log::Info("TGo4FileStore: Open file %s UPDATE", buffer.Data());
132  }
133 
134  // strip any path information from treename (could be identical with filename!)
135  const char* lastname = par->GetTitle();
136  const char* oldname=lastname;
137  lastname=strstr(oldname,"/");
138  while(lastname!=0) {
139  oldname=lastname+1;
140  lastname=strstr(oldname,"/");
141  }
142 
143 
144  buffer = oldname;
145  buffer += fgcTREESUF;
146  fxTree = dynamic_cast<TTree*> (fxFile->Get(buffer.Data()));
147  if(fxTree) {
148  TGo4Log::Debug(" Tree %s has been found in file %s ", buffer.Data(), fxFile->GetName());
149  fiFillCount= (Int_t) (fxTree->GetEntries());
150  } else {
151  fxTree = new TTree(buffer.Data(), "Go4FileStore");
152  fxTree->SetAutoSave(par->GetAutosaveSize());
153  fxTree->Write();
154  TGo4Log::Debug(" Tree %s has been created in file %s ",buffer.Data(), fxFile->GetName());
155  }
156 }
157 
158 
160 {
161  GO4TRACE((15,"TGo4FileStore::~TGo4FileStore()", __LINE__, __FILE__));
162  if(fxFile) {
163  fxFile = fxTree->GetCurrentFile(); // for file split after 1.8 Gb!
164  fxFile->cd();
165  fxTree->Write(0, TObject::kOverwrite);
166  delete fxFile; // closes File, fxTree is removed from memory then
167  fxFile = 0;
168  }
169 }
170 
171 
172 void TGo4FileStore::SetAutoSave(Int_t bytesinterval)
173 {
174  GO4TRACE((15,"TGo4FileStore::SetAutoSave(Int_t)", __LINE__, __FILE__));
175  fxTree->SetAutoSave(bytesinterval);
176 }
177 
179 {
180  GO4TRACE((12,"TGo4FileStore::SetCompression(Int_t)", __LINE__, __FILE__));
181  fxFile->SetCompressionLevel(comp);
182 }
183 
185 {
186  GO4TRACE((12,"TGo4FileStore::Store(TGo4EventElement*)", __LINE__, __FILE__));
187 
188  fxEvent = event; // address of next event into event pointer
189  if(!fbBranchExists) {
190  // first call of Store, create new branch
191  //std::cout << "**********************Creating new branch!"<< std::endl;
192  if(fxEvent) {
193  TString topbranchname = TString::Format("%s.", fxEvent->GetName());
194  TBranch* go4branch = fxTree->GetBranch(topbranchname.Data());
195  if(go4branch) {
196  // tree already had branch of our name, check it
197  TGo4Log::Debug(" FileStore: Found existing branch %s , continue filling ", fgcEVBRANCHNAME);
198  // here we might check the classname of the stored events inbranch
199  go4branch->SetAddress(&fxEvent);
200  fbBranchExists=kTRUE;
201  } else {
202  // no such branch existing, create a new one
203  TBranch *topbranch=
204  fxTree->Branch(topbranchname.Data(), fxEvent->ClassName(), &fxEvent, fiBufsize, fiSplit);
205  TGo4Log::Debug(" FileStore: Created new branch %s ", topbranchname.Data());
206  fbBranchExists = kTRUE;
207  fxEvent->makeBranch(topbranch);
208  } // if(go4branch)
209  }
210  else
211  {
212  // this is an error....
213  }
214  } // if(!fbEventBranchExists)
215 
216  fxTree->Fill();
217  fiFillCount++;
218  return 0;
219 }
220 
222 {
223  WriteToStore(cali);
224  return 0;
225 }
226 
228 {
229  WriteToStore(conny);
230  return 0;
231 }
232 
234 {
235  WriteToStore(fitter);
236  return 0;
237 }
238 
239 Int_t TGo4FileStore::Store(TFolder* fold)
240 {
241  WriteToStore(fold);
242  return 0;
243 }
244 
245 
246 
248 {
249  if (ob==0) return;
250 
251  TDirectory* dsav=gDirectory;
252  TString oldname = ob->GetName();
253  ob->SetName(Form("%s_%d" , oldname.Data(), fiFillCount));
254  if(fxTree) fxFile = fxTree->GetCurrentFile();
255  if (fxFile) fxFile->cd();
256  ob->Write(0, TObject::kOverwrite);
257  ob->SetName(oldname.Data());
258  if (dsav) dsav->cd();
259 }
260 
262 {
263  fgiFILESPLITSIZE = sz;
264 }
265 
267 {
268  return fgiFILESPLITSIZE;
269 }
270 
static Long64_t fgiFILESPLITSIZE
Definition: TGo4FileStore.h:90
virtual void makeBranch(TBranch *parent)
virtual ~TGo4FileStore()
void WriteToStore(TNamed *ob)
static void SetMaxTreeSize(Long64_t sz)
void SetCompression(Int_t comp)
Bool_t fbBranchExists
TGo4EventElement * fxEvent
void SetAutoSave(Int_t interval)
virtual Int_t Store(TGo4EventElement *event)
static const char * fgcTREESUF
Definition: TGo4FileStore.h:87
#define GO4TRACE(X)
Definition: TGo4Log.h:26
static const char * fgcFILESUF
Definition: TGo4FileStore.h:84
static Long64_t GetMaxTreeSize()
static const char * fgcEVBRANCHNAME
Definition: TGo4FileStore.h:81
static void Error(const char *text,...)
Definition: TGo4Log.cxx:309
static void Info(const char *text,...)
Definition: TGo4Log.cxx:283
static void Debug(const char *text,...)
Definition: TGo4Log.cxx:270