00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4FileStore.h"
00017
00018 #include "Riostream.h"
00019
00020 #include "TFolder.h"
00021 #include "TFile.h"
00022 #include "TTree.h"
00023 #include "snprintf.h"
00024
00025 #include "TGo4Log.h"
00026 #include "TGo4EventElement.h"
00027 #include "TGo4CompositeEvent.h"
00028 #include "TGo4Parameter.h"
00029 #include "TGo4Condition.h"
00030 #include "TGo4Fitter.h"
00031 #include "TGo4FileStoreParameter.h"
00032
00033 const Text_t TGo4FileStore::fgcTREESUF[] = "xTree";
00034 const Text_t TGo4FileStore::fgcFILESUF[] = ".root";
00035 const Text_t TGo4FileStore::fgcEVBRANCHNAME[] = "Go4EventBranch.";
00036 const Int_t TGo4FileStore::fgiFILESPLITSIZE = 1000000000;
00037
00038
00039 TGo4FileStore::TGo4FileStore() :
00040 TGo4EventStore("Go4 Default File Store"),
00041 fbBranchExists(kFALSE), fxEvent(0),
00042 fiSplit(1),
00043 fiBufsize(0),
00044 fiFillCount(0)
00045 {
00046 TRACE((15,"TGo4FileStore::TGo4FileStore()", __LINE__, __FILE__));
00047
00048 fxFile=0;
00049 fxTree=0;
00050 TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00051
00052 }
00053
00054 TGo4FileStore::TGo4FileStore(const char* name,
00055 Int_t splitlevel,
00056 Int_t compression,
00057 Bool_t overwrite,
00058 Int_t autosavesize,
00059 Int_t bufsize) :
00060 TGo4EventStore(name),
00061 fbBranchExists(kFALSE),
00062 fxEvent(0),
00063 fiSplit(splitlevel),
00064 fiBufsize(bufsize),
00065 fiFillCount(0)
00066 {
00067 TRACE((15,"TGo4FileStore::TGo4FileStore(Text_t*,...)", __LINE__, __FILE__));
00068 TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00069 TString buffer(name);
00070 if(strstr(buffer.Data(),fgcFILESUF)==0)
00071 buffer+=fgcFILESUF;
00072 if(overwrite)
00073 {
00074 fxFile = new TFile(buffer.Data(), "RECREATE");
00075 }
00076 else
00077 {
00078 fxFile = new TFile(buffer.Data(), "UPDATE");
00079 }
00080 fxFile->SetCompressionLevel(compression);
00081
00082
00083 const char* lastname=name;
00084 const char* oldname=name;
00085 lastname=strstr(oldname,"/");
00086 while(lastname!=0)
00087 {
00088 oldname=lastname+1;
00089 lastname=strstr(oldname,"/");
00090 }
00091 buffer = oldname;
00092 buffer += fgcTREESUF;
00093 fxTree= dynamic_cast<TTree*> (fxFile->Get(buffer.Data()));
00094 if(fxTree)
00095 {
00096 TGo4Log::Debug(" Tree %s has been found in file %s ",buffer.Data(), fxFile->GetName());
00097 fiFillCount=(Int_t) (fxTree->GetEntries());
00098 }
00099 else
00100 {
00101 fxTree = new TTree(buffer.Data(), "Go4FileStore");
00102 fxTree->SetAutoSave(autosavesize);
00103 fxTree->Write();
00104 TGo4Log::Debug(" Tree %s has been created in file %s ",buffer.Data(), fxFile->GetName());
00105 }
00106 }
00107
00108
00109 TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par) :
00110 TGo4EventStore("dummy"),
00111 fbBranchExists(kFALSE),
00112 fxEvent(0),
00113 fiSplit(1),
00114 fiBufsize(0),
00115 fiFillCount(0)
00116 {
00117 TRACE((15,"TGo4FileStore::TGo4FileStore(TGo4FileStoreParameter* par)", __LINE__, __FILE__));
00118
00119 if (par==0) {
00120 TGo4Log::Error("TGo4FileStore::TGo4FileStore(.., TGo4FileStoreParameter* is not specified");
00121 return;
00122 }
00123
00124 fiSplit = par->GetSplitlevel();
00125 fiBufsize = par->GetBufsize();
00126
00127 TTree::SetMaxTreeSize(fgiFILESPLITSIZE);
00128 Text_t buffer[TGo4EventStore::fguTXTLEN];
00129 snprintf(buffer,TGo4EventStore::fguTXTLEN -20, "%s", par->GetName());
00130 SetName(buffer);
00131 if(!strstr(buffer,fgcFILESUF))
00132 strcat(buffer,fgcFILESUF);
00133 if(par->IsOverwriteMode())
00134 {
00135 fxFile = new TFile(buffer, "RECREATE");
00136 }
00137 else
00138 {
00139 fxFile = new TFile(buffer, "UPDATE");
00140 }
00141 fxFile->SetCompressionLevel(par->GetCompression());
00142
00143
00144 const char* lastname = par->GetTitle();
00145 const char* oldname=lastname;
00146 lastname=strstr(oldname,"/");
00147 while(lastname!=0)
00148 {
00149 oldname=lastname+1;
00150 lastname=strstr(oldname,"/");
00151 }
00152 snprintf(buffer, TGo4EventStore::fguTXTLEN -10 ,"%s", oldname);
00153 strcat(buffer,fgcTREESUF);
00154 fxTree= dynamic_cast<TTree*> (fxFile->Get(buffer));
00155 if(fxTree)
00156 {
00157 TGo4Log::Debug(" Tree %s has been found in file %s ",buffer, fxFile->GetName());
00158 fiFillCount= (Int_t) (fxTree->GetEntries());
00159 }
00160 else
00161 {
00162 fxTree = new TTree(buffer, "Go4FileStore");
00163 fxTree->SetAutoSave(par->GetAutosaveSize());
00164 fxTree->Write();
00165 TGo4Log::Debug(" Tree %s has been created in file %s ",buffer, fxFile->GetName());
00166 }
00167 }
00168
00169
00170 TGo4FileStore::~TGo4FileStore()
00171 {
00172 TRACE((15,"TGo4FileStore::~TGo4FileStore()", __LINE__, __FILE__));
00173 if(fxFile)
00174 {
00175 fxFile=fxTree->GetCurrentFile();
00176 fxFile->cd();
00177 fxTree->Write(0, TObject::kOverwrite);
00178 delete fxFile;
00179 }
00180 else
00181 { }
00182 }
00183
00184
00185
00186
00187 void TGo4FileStore::SetAutoSave(Int_t bytesinterval)
00188 {
00189 TRACE((15,"TGo4FileStore::SetAutoSave(Int_t)", __LINE__, __FILE__));
00190 fxTree->SetAutoSave(bytesinterval);
00191 }
00192 void TGo4FileStore::SetCompression(Int_t comp)
00193 {
00194 TRACE((12,"TGo4FileStore::SetCompression(Int_t)", __LINE__, __FILE__));
00195 fxFile->SetCompressionLevel(comp);
00196 }
00197
00198 Int_t TGo4FileStore::Store(TGo4EventElement* event)
00199 {
00200 TRACE((12,"TGo4FileStore::Store(TGo4EventElement*)", __LINE__, __FILE__));
00201
00202 Int_t rev=-1;
00203 TGo4EventElement* lastevent= fxEvent;
00204 fxEvent=event;
00205 if(!fbBranchExists)
00206 {
00207
00208
00209 if(fxEvent)
00210 {
00211 Text_t topbranchname [TGo4EventStore::fguTXTLEN];
00212 snprintf(topbranchname,TGo4EventStore::fguTXTLEN -20,"%s.", fxEvent->GetName());
00213 TBranch* go4branch= fxTree->GetBranch(topbranchname);
00214 if(go4branch)
00215 {
00216
00217 TGo4Log::Debug(" FileStore: Found existing branch %s , continue filling ", fgcEVBRANCHNAME);
00218
00219 go4branch->SetAddress(&fxEvent);
00220 fbBranchExists=kTRUE;
00221 }
00222 else
00223 {
00224
00225 TBranch *topbranch=
00226 fxTree->Branch(topbranchname, fxEvent->ClassName(), &fxEvent, fiBufsize, fiSplit);
00227 TGo4Log::Debug(" FileStore: Created new branch %s ", topbranchname);
00228 fbBranchExists=kTRUE;
00229 if (fxEvent->InheritsFrom(TGo4CompositeEvent::Class()))
00230 dynamic_cast<TGo4CompositeEvent*> (fxEvent)->makeBranch(topbranch);
00231 }
00232 lastevent=event;
00233 }
00234 else
00235 {
00236
00237 rev=1;
00238 }
00239 }
00240 else
00241 {
00242
00243 }
00244
00245
00246
00247
00248 fxTree->Fill();
00249 fiFillCount++;
00250 rev=0;
00251
00252
00253
00254
00255
00256
00257
00258 return rev;
00259 }
00260
00261 Int_t TGo4FileStore::Store(TGo4Parameter* cali)
00262 {
00263 WriteToStore(cali);
00264 return 0;
00265 }
00266
00267 Int_t TGo4FileStore::Store(TGo4Condition* conny)
00268 {
00269 WriteToStore(conny);
00270 return 0;
00271 }
00272
00273 Int_t TGo4FileStore::Store(TGo4Fitter* fitter)
00274 {
00275 WriteToStore(fitter);
00276 return 0;
00277 }
00278
00279 Int_t TGo4FileStore::Store(TFolder* fold)
00280 {
00281 WriteToStore(fold);
00282 return 0;
00283 }
00284
00285
00286
00287 void TGo4FileStore::WriteToStore(TNamed* ob)
00288 {
00289 if(ob)
00290 {
00291 TDirectory* dsav=gDirectory;
00292 Text_t buf[TGo4EventStore::fguTXTLEN];
00293 Text_t oldname[TGo4EventStore::fguTXTLEN];
00294 strncpy(oldname,ob->GetName(), TGo4EventStore::fguTXTLEN -1);
00295 snprintf(buf,
00296 TGo4EventStore::fguTXTLEN-1,
00297 "%s_%d" , oldname, fiFillCount);
00298 ob->SetName(buf);
00299 if(fxTree) fxFile=fxTree->GetCurrentFile();
00300 fxFile->cd();
00301 ob->Write(0, TObject::kOverwrite);
00302 ob->SetName(oldname);
00303 dsav->cd();
00304 }
00305
00306 }
00307
00308
00309
00310