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