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