GSI Object Oriented Online Offline (Go4)  GO4-6.3.0
TGo4FileStore.cxx
Go to the documentation of this file.
1 // $Id$
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(nullptr),
36  fxTree(nullptr),
37  fbBranchExists(kFALSE),
38  fxEvent(nullptr),
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(nullptr),
57  fxTree(nullptr),
58  fbBranchExists(kFALSE),
59  fxEvent(nullptr),
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))
68  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) {
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  TGo4Log::Debug(" Tree %s has been found in file %s ", buffer.Data(), fxFile->GetName());
90  fiFillCount = (Int_t)(fxTree->GetEntries());
91  } else {
92  fxTree = new TTree(buffer.Data(), "Go4FileStore");
93  fxTree->SetAutoSave(autosavesize);
94  fxTree->Write();
95  TGo4Log::Debug(" Tree %s has been created in file %s ", buffer.Data(), fxFile->GetName());
96  }
97 }
98 
100  TGo4EventStore("dummy"),
101  fbBranchExists(kFALSE),
102  fxEvent(nullptr),
103  fiSplit(1),
104  fiBufsize(0),
105  fiFillCount(0)
106 {
107  GO4TRACE((15,"TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par)", __LINE__, __FILE__));
108 
109  if (!par) {
110  TGo4Log::Error("TGo4FileStore::TGo4FileStore(.., TGo4FileStoreParameter* is not specified");
111  return;
112  }
113 
114  fiSplit = par->GetSplitlevel();
115  fiBufsize = par->GetBufsize();
116 
117  TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
118 
119  TString buffer = par->GetName();
120  SetName(buffer.Data());
121  if(!buffer.Contains(fgcFILESUF)) buffer.Append(fgcFILESUF);
122  if(par->IsOverwriteMode()) {
123  fxFile = TFile::Open(buffer.Data(), "RECREATE", "Go4 file store", par->GetCompression());
124  TGo4Log::Info("TGo4FileStore: Open file %s RECREATE", buffer.Data());
125  } else {
126  fxFile = TFile::Open(buffer.Data(), "UPDATE", "Go4 file store", par->GetCompression());
127  TGo4Log::Info("TGo4FileStore: Open file %s UPDATE", buffer.Data());
128  }
129 
130  // strip any path information from treename (could be identical with filename!)
131  const char *lastname = par->GetTitle();
132  const char *oldname=lastname;
133  lastname = strstr(oldname,"/");
134  while(lastname) {
135  oldname = lastname+1;
136  lastname = strstr(oldname,"/");
137  }
138 
139  buffer = oldname;
140  buffer += fgcTREESUF;
141  fxTree = dynamic_cast<TTree *> (fxFile->Get(buffer.Data()));
142  if(fxTree) {
143  TGo4Log::Debug(" Tree %s has been found in file %s ", buffer.Data(), fxFile->GetName());
144  fiFillCount = (Int_t) fxTree->GetEntries();
145  } else {
146  fxTree = new TTree(buffer.Data(), "Go4FileStore");
147  fxTree->SetAutoSave(par->GetAutosaveSize());
148  fxTree->Write();
149  TGo4Log::Debug(" Tree %s has been created in file %s ",buffer.Data(), fxFile->GetName());
150  }
151 }
152 
153 
155 {
156  GO4TRACE((15,"TGo4FileStore::~TGo4FileStore()", __LINE__, __FILE__));
157  if(fxFile && fxTree) {
158  fxFile = fxTree->GetCurrentFile(); // for file split after 1.8 Gb!
159  fxFile->cd();
160  fxTree->Write(nullptr, TObject::kOverwrite);
161  delete fxFile; // closes File, fxTree is removed from memory then
162  fxFile = nullptr;
163  }
164 }
165 
166 
167 void TGo4FileStore::SetAutoSave(Int_t bytesinterval)
168 {
169  GO4TRACE((15,"TGo4FileStore::SetAutoSave(Int_t)", __LINE__, __FILE__));
170  fxTree->SetAutoSave(bytesinterval);
171 }
172 
174 {
175  GO4TRACE((12,"TGo4FileStore::SetCompression(Int_t)", __LINE__, __FILE__));
176  fxFile->SetCompressionLevel(comp);
177 }
178 
180 {
181  GO4TRACE((12,"TGo4FileStore::Store(TGo4EventElement *)", __LINE__, __FILE__));
182 
183  fxEvent = event; // address of next event into event pointer
184  if(!fbBranchExists) {
185  // first call of Store, create new branch
186  if(fxEvent) {
187  TString topbranchname = TString::Format("%s.", fxEvent->GetName());
188  TBranch *go4branch = fxTree->GetBranch(topbranchname.Data());
189  if(go4branch) {
190  // tree already had branch of our name, check it
191  TGo4Log::Debug(" FileStore: Found existing branch %s, continue filling ", fgcEVBRANCHNAME);
192  // here we might check the classname of the stored events inbranch
193  go4branch->SetAddress(&fxEvent);
194  fbBranchExists=kTRUE;
195  } else {
196  // no such branch existing, create a new one
197  TBranch *topbranch=
198  fxTree->Branch(topbranchname.Data(), fxEvent->ClassName(), &fxEvent, fiBufsize, fiSplit);
199  TGo4Log::Debug(" FileStore: Created new branch %s ", topbranchname.Data());
200  fbBranchExists = kTRUE;
201  fxEvent->makeBranch(topbranch);
202  } // if(go4branch)
203  }
204  else
205  {
206  // this is an error....
207  }
208  } // if(!fbEventBranchExists)
209 
210  fxTree->Fill();
211  fiFillCount++;
212  return 0;
213 }
214 
216 {
217  WriteToStore(cali);
218  return 0;
219 }
220 
222 {
223  WriteToStore(conny);
224  return 0;
225 }
226 
228 {
229  WriteToStore(fitter);
230  return 0;
231 }
232 
233 Int_t TGo4FileStore::Store(TFolder *fold)
234 {
235  WriteToStore(fold);
236  return 0;
237 }
238 
240 {
241  if (!ob) return;
242 
243  auto dsav = gDirectory;
244  TString oldname = ob->GetName();
245  ob->SetName(TString::Format("%s_%d", oldname.Data(), fiFillCount).Data());
246  if(fxTree) fxFile = fxTree->GetCurrentFile();
247  if (fxFile) fxFile->cd();
248  ob->Write(nullptr, TObject::kOverwrite);
249  ob->SetName(oldname.Data());
250  if (dsav) dsav->cd();
251 }
252 
254 {
255  fgiFILESPLITSIZE = sz;
256 }
257 
259 {
260  return fgiFILESPLITSIZE;
261 }
static Long64_t fgiFILESPLITSIZE
Definition: TGo4FileStore.h:90
virtual void makeBranch(TBranch *parent)
virtual ~TGo4FileStore()
void WriteToStore(TNamed *ob)
static void Info(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:294
static void SetMaxTreeSize(Long64_t sz)
void SetCompression(Int_t comp)
Int_t Store(TGo4EventElement *event) override
Bool_t fbBranchExists
static void Debug(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:281
TGo4EventElement * fxEvent
void SetAutoSave(Int_t interval)
static const char * fgcTREESUF
Definition: TGo4FileStore.h:87
static void Error(const char *text,...) GO4_PRINTF_ARGS
Definition: TGo4Log.cxx:320
#define GO4TRACE(X)
Definition: TGo4Log.h:25
static const char * fgcFILESUF
Definition: TGo4FileStore.h:84
static Long64_t GetMaxTreeSize()
static const char * fgcEVBRANCHNAME
Definition: TGo4FileStore.h:81