00001 #ifndef FROOT_HDT_ACCESS
00002 #define FROOT_HDT_ACCESS
00003
00004 #include <Riostream.h>
00005 #include "TROOT.h"
00006 #include "TSystem.h"
00007 #include "TApplication.h"
00008
00009 #include "TString.h"
00010 #include "TObject.h"
00011
00012 #include "TFndHdt.h"
00013
00014
00015 class THdtAccess: public TObject{
00016
00017 private:
00018
00019 Int_t fVerboseLevel;
00020 Int_t fUpdateEvGap;
00021
00022 TFile *fCurHdtFile;
00023 TTree *fCurHdtTree;
00024 TFndGenInfo *fCurRunInfo;
00025
00026 TFndHdt *fCurHdtEv;
00027
00028 TString fDBhost;
00029 TString fDataPath;
00030 TString fRunType;
00031
00032 Int_t fFirstRun;
00033 Int_t fLastRun;
00034 Bool_t fIsLastRun;
00035
00036 Int_t fDebugLevel;
00037
00038 Int_t fTotEvCount;
00039 Int_t fTotEvTgt;
00040 Int_t fEvPerRunTgt;
00041 Int_t fCurEv;
00042 Int_t fIncrement;
00043 Int_t fCurrentRun;
00044
00045 Int_t fCurTotEntries;
00046
00047 Int_t InitNewRun();
00048 Int_t FinishRun();
00049
00050 public:
00051
00052 THdtAccess();
00053 ~THdtAccess();
00054
00055 void SetUpdateEvGap(const Int_t &ev_gap) { fUpdateEvGap = ev_gap; }
00056 Int_t GetUpdateEvGap() { return fUpdateEvGap; }
00057
00058 void SetVerboseLevel(const Int_t &lev) { fVerboseLevel = lev; }
00059 Int_t GetVerboseLevel() { return fVerboseLevel; }
00060
00061 virtual void Init(const TString &db_host,const TString &data_path,const TString &run_type,Int_t first_run,Int_t last_run,Int_t n_evs_run,Int_t n_evs_tot,Int_t debug_lev);
00062
00063 virtual void UseHdtEvent()=0;
00064 virtual void EventLoop();
00065 virtual Int_t ProcessSingleRun();
00066 virtual Int_t RunLoop();
00067
00068 virtual void Pre_Run_Operation() = 0;
00069 virtual void Post_Run_Operation() = 0;
00070
00071
00072 };
00073
00074 #endif // FROOT_HDT_ACCESS
00075
00076
00077
00078
00079 THdtAccess::THdtAccess():
00080 fVerboseLevel(1),fUpdateEvGap(300),
00081 fCurHdtFile(),fCurHdtTree(),fCurRunInfo(),
00082 fCurHdtEv(),
00083 fDBhost(),fDataPath(),fRunType(),
00084 fFirstRun(),fLastRun(),fDebugLevel(),
00085 fTotEvCount(),fTotEvTgt(),fEvPerRunTgt(),
00086 fCurEv(),fIncrement(),fCurrentRun(),fCurTotEntries()
00087 {
00088
00089 }
00090
00091
00092 THdtAccess::~THdtAccess(){
00093
00094 }
00095
00096
00097 void THdtAccess::Init(const TString &db_host,const TString &data_path,const TString &run_type,Int_t first_run,Int_t last_run,Int_t n_evs_run,Int_t n_evs_tot,Int_t debug_lev){
00098
00099 fDBhost = db_host;
00100 fDataPath = data_path;
00101 fRunType = run_type;
00102 fEvPerRunTgt = n_evs_run;
00103 fTotEvTgt = n_evs_tot;
00104 fDebugLevel = debug_lev;
00105
00106 fFirstRun = first_run;
00107 fLastRun = last_run;
00108
00109 fCurTotEntries = 0;
00110 }
00111
00112
00113
00114 Int_t THdtAccess::InitNewRun(){
00115
00116
00117
00118
00119
00120
00121
00122
00123 if(fVerboseLevel > 2) Printf("InitNewRun: fCurrentRun = %u",fCurrentRun);
00124
00125 FinishRun();
00126
00127
00128 #if defined _FND_OUTPUTS_USE_SUBDIR
00129 TString hdt_rnam = BuildRunName(fRunType,fCurrentRun,kTRUE);
00130 #else
00131 TString hdt_rnam = BuildRunName(fRunType,fCurrentRun);
00132 #endif
00133
00134 hdt_rnam+=".hdt.root";
00135 TString cpth = "";
00136 cpth.Form("%s/%s",ExpandPathName("$FND_HDT").Data(),hdt_rnam.Data());
00137
00138 if( gSystem->AccessPathName(cpth) ){
00139 Printf("file \"%s\" not found",cpth.Data());
00140 return -1;
00141 }
00142
00143 fCurHdtFile = new TFile(cpth,"OPEN");
00144 if(!fCurHdtFile){
00145 Printf("problems while trying to get file \"%s\"",cpth.Data());
00146 return -1;
00147 }
00148
00149
00150
00151 fCurHdtTree = (TTree *) ( fCurHdtFile->Get("FIN_HDT_TREE") );
00152 if(!fCurHdtTree){
00153 Printf("tree \"FIN_HDT_TREE\" not found in file \"%s\"",fCurHdtFile->GetName());
00154 return -2;
00155 }
00156 if(fCurHdtTree->GetEntries() < 1){
00157 Printf("tree \"FIN_HDT_TREE\" found, but empty");
00158 return -3;
00159 }
00160
00161
00162
00163 if(! fCurHdtTree->GetBranch("fndhdt") ){
00164 Printf("can not assign HDT-branch from hdt tree");
00165 return -4;
00166 }
00167 fCurHdtTree->SetBranchAddress("fndhdt",&fCurHdtEv);
00168 fCurTotEntries = fCurHdtTree->GetEntries();
00169
00170
00171
00172 fCurRunInfo = (TFndGenInfo *)( fCurHdtFile->Get("TFndGenInfo") );
00173 if(!fCurRunInfo){
00174 Printf("Pointer to TFndGenInfo not found in file \"%s\"",fCurHdtFile->GetName());
00175 return -5;
00176 }
00177
00178 return 0;
00179 }
00180
00181
00182 Int_t THdtAccess::FinishRun(){
00183
00184
00185
00186
00187 if(fVerboseLevel > 2) Printf("FinishRun: fCurrentRun = %u",fCurrentRun);
00188
00189 if(!fCurHdtFile && !fCurHdtTree && !fCurHdtEv ){
00190
00191 return 1;
00192 }
00193
00194 if(fCurHdtEv){
00195 delete fCurHdtEv;
00196 fCurHdtEv = 0;
00197 }
00198 if(fCurHdtTree){
00199 fCurHdtTree->Reset();
00200 delete fCurHdtTree;
00201 fCurHdtTree = 0;
00202 }
00203
00204 if(fCurRunInfo){
00205 delete fCurRunInfo;
00206 fCurRunInfo = 0;
00207 }
00208
00209
00210 if( fCurHdtFile ){
00211 if( fCurHdtFile->IsOpen() ) fCurHdtFile->Close();
00212 delete fCurHdtFile;
00213 fCurHdtFile = 0;
00214 }
00215
00216 fCurTotEntries = 0;
00217
00218 return 0;
00219 }
00220
00221
00222 void THdtAccess::EventLoop(){
00223
00224 while(1){
00225 if(fCurEv > fCurTotEntries){
00226 if(fVerboseLevel > 0) Printf("EventLoop ---> run completed (%d events)",fCurEv-1);
00227 return;
00228 }
00229 else{
00230 if(fTotEvTgt != -1 && fTotEvCount > fTotEvTgt){
00231 if(fVerboseLevel > 0) Printf("EventLoop ---> maximum number of events reached for current process");
00232 return;
00233 }
00234 if(fEvPerRunTgt != -1 && fCurEv > fEvPerRunTgt){
00235 if(fVerboseLevel > 0) Printf("EventLoop ---> maximum number of events reached for current run");
00236 return;
00237 }
00238 }
00239
00240 if( fCurEv % fUpdateEvGap == 0 && fVerboseLevel > 0 )
00241 gROOT->Info("EventLoop","Processing run %s-%d ===> event %d",fRunType.Data(),fCurrentRun,fCurEv);
00242
00243
00244 fCurHdtTree->GetEntry(fCurEv-1);
00245 UseHdtEvent();
00246
00247 fCurEv++;
00248 fTotEvCount++;
00249 }
00250
00251 }
00252
00253
00254 Int_t THdtAccess::ProcessSingleRun(){
00255
00256
00257
00258
00259 Pre_Run_Operation();
00260
00261 Int_t initval = InitNewRun();
00262 if(initval!=0){
00263 Printf("initval: %d",initval);
00264 FinishRun();
00265 return -1;
00266 }
00267
00268 if(fVerboseLevel > 1) Printf("New run started (%s %d)",fRunType.Data(),fCurrentRun);
00269
00270 fCurEv = 1;
00271
00272
00273 EventLoop();
00274
00275 Post_Run_Operation();
00276 FinishRun();
00277 if(fVerboseLevel > 1) Printf("\n --- --- --- --- \n");
00278
00279 return 0;
00280 }
00281
00282
00283 Int_t THdtAccess::RunLoop(){
00284
00285
00286 if(fVerboseLevel > 0) gROOT->Info("RunLoop","Starting loop on runs (%d -> %d)",fFirstRun,fLastRun);
00287
00288 Int_t retval = 0;
00289
00290 fCurrentRun = fFirstRun;
00291 fIncrement = (fLastRun >= fFirstRun) ? +1 : -1;
00292
00293 while(1){
00294 if(fLastRun >= fFirstRun && fCurrentRun > fLastRun) break;
00295 else if(fLastRun < fFirstRun && fCurrentRun < fLastRun) break;
00296
00297 if(fTotEvTgt != -1 && fTotEvCount >= fTotEvTgt){
00298 Printf("RunLoop ---> maximum number of events reached for current process");
00299 break;
00300 }
00301
00302 fIsLastRun = (fCurrentRun == fLastRun)? kTRUE : kFALSE;
00303 if(ProcessSingleRun() == -1){
00304 fCurrentRun += fIncrement;
00305 retval++;
00306 continue;
00307 }
00308
00309 if(fCurrentRun == -1) break;
00310
00311
00312
00313 fCurrentRun += fIncrement;
00314 }
00315
00316 return retval;
00317
00318 }