00001
00002
00004
00005 #include <Riostream.h>
00006 #include "TROOT.h"
00007 #include "TSystem.h"
00008 #include "TApplication.h"
00009
00010 #include "TString.h"
00011
00012 #include "TFndHdt.h"
00013 #include "TFndPreanMan.h"
00014 #include "TFndPrean.h"
00015
00016 class TFndPreanProcess: public TObject{
00017
00018 public:
00019 enum E_FollowControl{
00020 E_Follow_NotUsed = -1,
00021 E_Follow_GoToNext = 0,
00022 E_Follow_RetryThis = 1,
00023 E_Follow_ProcessThis = 2,
00024 };
00025
00026
00027 private:
00028 TFndPreanMan *fPreanMan;
00029
00030 TString fDBhost;
00031 TString fDataPath;
00032 TString fRunType;
00033
00034 Int_t fFirstRun;
00035 Int_t fLastRun;
00036 Bool_t fIsLastRun;
00037
00038 Int_t fDebugLevel;
00039
00040 Int_t fTotEvCount;
00041 Int_t fTotEvTgt;
00042 Int_t fEvPerRunTgt;
00043 Int_t fCurEv;
00044 Int_t fIncrement;
00045 Int_t fCurrentRun;
00046 TString fLogFileName;
00047 TString fLogMsg;
00048
00049
00050 TDatime fCheckTime_Now;
00051 Int_t fStartTime;
00052 Int_t fStopTime;
00053
00054 private:
00055 Int_t RunLoop();
00056 Int_t FollowDAQ();
00057
00058 public:
00059
00060 TFndPreanProcess();
00061 ~TFndPreanProcess();
00062
00063 void SetDebugLevel(const Int_t &debug_lev) { fDebugLevel = debug_lev; }
00064
00065 void SetDBhost(const TString &db_host) { fDBhost = db_host; }
00066 void SetDataPath(const TString &data_path) { fDataPath = data_path; }
00067 void SetRunType(const TString &run_type) { fRunType = run_type; }
00068
00069 void SetCurrentRun(const Int_t &run_num) { fCurrentRun = run_num; }
00070
00071 void SetRunEventTarget(const Int_t &nevt) { fEvPerRunTgt = nevt; }
00072 void SetTotEventTarget(const Int_t &nevt) { fTotEvTgt = nevt; }
00073
00074 void SetFirstRun(const Int_t &num) { fFirstRun = num; }
00075 void SetLastRun(const Int_t &num) { fLastRun = num; }
00076
00077 Int_t ProcessPreanalysis();
00078
00079 void StartTimeCheck();
00080 void StopTimeCheck();
00081 void EventLoop();
00082 Int_t ProcessSingleRun();
00083
00084 Int_t CheckRuns();
00085
00086 ClassDef(TFndPreanProcess,0)
00087 };
00088
00089 ClassImp(TFndPreanProcess)
00090
00091
00092 TFndPreanProcess::TFndPreanProcess():
00093 fPreanMan(),fDBhost(),fDataPath(),fRunType(),
00094 fFirstRun(),fLastRun(),fIsLastRun(),
00095 fDebugLevel(),
00096 fTotEvCount(),fTotEvTgt(),fEvPerRunTgt(),fCurEv(),fIncrement(),fCurrentRun(),fLogFileName(),fLogMsg(),
00097 fCheckTime_Now(),fStartTime(),fStopTime()
00098 {
00099
00100 fStartTime = 0;
00101 fStopTime = 0;
00102 fTotEvCount = 1;
00103
00104 }
00105
00106
00107 TFndPreanProcess::~TFndPreanProcess(){
00108
00109 cout << endl << endl << " -> Destroying the preanalysis manager..." << endl;
00110 delete fPreanMan;
00111 }
00112
00113
00114
00115 void TFndPreanProcess::StartTimeCheck(){
00116
00117 fCheckTime_Now.Set();
00118 fStartTime = fCheckTime_Now.Convert();
00119 }
00120
00121
00122 void TFndPreanProcess::StopTimeCheck(){
00123
00124 fCheckTime_Now.Set();
00125 fStopTime = fCheckTime_Now.Convert();
00126 Int_t rate = Int_t ( (Float_t)fCurEv / (Float_t)(fStopTime - fStartTime) );
00127
00128 cout << endl << " --- --- --- --- --- --- --- --- --- --- --- ---" << endl;
00129 if(fCurEv >0) fLogMsg.Form("Performances test: %d events processed in %d s ===> %d ev/s",fCurEv, fStopTime - fStartTime, rate );
00130 else fLogMsg.Form("Performances test: elapsed time: %d s",fStopTime - fStartTime );
00131 cout << fLogMsg << endl;
00132 cout << " --- --- --- --- --- --- --- --- --- --- --- ---" << endl << endl;
00133 fPreanMan->WriteLogFile(fLogMsg);
00134 fPreanMan->WriteLogFile("\n --- --- --- --- --- --- --- \n\n");
00135 }
00136
00137
00138 void TFndPreanProcess::EventLoop(){
00139
00140 while(1){
00141 if( fCurEv % 300 == 0 ) gROOT->Info("FinudaPreanalysis","Processing run %s-%d ===> event %d",fRunType.Data(),fCurrentRun,fCurEv);
00142 if(fTotEvTgt != -1 && fTotEvCount > fTotEvTgt){
00143 Printf("PreanalyzeRun ---> maximum number of events reached for current preanalysis process");
00144 return;
00145 }
00146 if(fEvPerRunTgt != -1 && fCurEv > fEvPerRunTgt){
00147 Printf("PreanalyzeRun ---> maximum number of events reached for current run");
00148 return;
00149 }
00150 if( fPreanMan->GetNextFidaEvent() != 0) return;
00151 fPreanMan->AnalyzeJFGES();
00152 if( fPreanMan->ReconstructFidaEvent() !=0) return;
00153 fPreanMan->AnalyzeJFDST();
00154 fCurEv++;
00155 fTotEvCount++;
00156 }
00157
00158 }
00159
00160
00161 Int_t TFndPreanProcess::ProcessPreanalysis(){
00162
00163
00164
00165
00166 if(fPreanMan){
00167 Error("ProcessPreanalysis","fPreanMan should not be defined yet!");
00168 delete fPreanMan;
00169 TerminateFroot();
00170 }
00171 fPreanMan = new TFndPreanMan(fDBhost,fDataPath,fDebugLevel);
00172
00173 if (fFirstRun == 0 && fLastRun == 0){
00174 Warning("ProcessPreanalysis()","Can not determine process mode");
00175 return -1;
00176 }
00177
00178 Int_t ret_val =
00179 (fLastRun == -1) ?
00180 FollowDAQ() :
00181 RunLoop();
00182
00183 return ret_val;
00184 }
00185
00186
00187
00188 Int_t TFndPreanProcess::ProcessSingleRun(){
00189
00190
00191
00192
00193 Int_t initval = fPreanMan->InitNewPreanRun(fRunType,fCurrentRun);
00194 if(initval!=0){
00195
00196 fPreanMan->FinishPreanRun(kFALSE);
00197 fCurrentRun += fIncrement;
00198 Printf("\n --- --- --- --- \n");
00199 return -1;
00200 }
00201
00202 fLogFileName.Form("%s_prean.log",BuildRunName(fRunType,fCurrentRun).Data());
00203 fPreanMan->OpenLogFile("$PWD",fLogFileName);
00204 Printf("New run started (%s %d)",fRunType.Data(),fCurrentRun);
00205
00206 fCurEv = 1;
00207 StartTimeCheck();
00208
00209 fLogMsg.Form("Process Start time: %s",fCheckTime_Now.AsString() );
00210 fPreanMan->WriteLogFile(fLogMsg);
00211 fLogMsg.Form(" FIDARC release: \"%s\"\n",FIN_PHYS::FidaVer_Name().Data() );
00212 fPreanMan->WriteLogFile(fLogMsg);
00213
00214
00215 EventLoop();
00216
00217
00218 fPreanMan->FinishPreanRun(fIsLastRun);
00219
00220
00221 fLogMsg.Form("\nProcess end time: %s",fCheckTime_Now.AsString());
00222 fPreanMan->WriteLogFile(fLogMsg);
00223 StopTimeCheck();
00224 fPreanMan->CloseLogFile();
00225
00226
00227 Printf("\n --- --- --- --- \n");
00228
00229 return 0;
00230 }
00231
00232
00233
00234 Int_t TFndPreanProcess::RunLoop(){
00235
00236 if(!fPreanMan){
00237 Error("RunLoop","fPreanMan not defined");
00238 TerminateFroot();
00239 }
00240
00241 Info("RunLoop","Starting loop on runs (%d -> %d)",fFirstRun,fLastRun);
00242
00243 fCurrentRun = fFirstRun;
00244 fIncrement = (fLastRun >= fFirstRun) ? +1 : -1;
00245
00246 while(1){
00247 if(fLastRun >= fFirstRun && fCurrentRun > fLastRun) break;
00248 else if(fLastRun < fFirstRun && fCurrentRun < fLastRun) break;
00249
00250 if(fTotEvTgt != -1 && fTotEvCount >= fTotEvTgt){
00251 Printf("RunLoop ---> maximum number of events reached for current preanalysis process");
00252 break;
00253 }
00254
00255 fIsLastRun = (fCurrentRun == fLastRun)? kTRUE : kFALSE;
00256 if(ProcessSingleRun() == -1) continue;
00257
00258 if(fCurrentRun == -1) break;
00259
00260
00261 fCurrentRun += fIncrement;
00262 }
00263
00264 fPreanMan->CloseInterface();
00265
00266 return 0;
00267
00268 }
00269
00270
00271 Int_t TFndPreanProcess::CheckRuns(){
00272
00273
00274
00275
00276
00277
00278 TString chk_start;
00279 TString chk_raw;
00280 TString chk_raw_gz;
00281 TString chk_form;
00282
00283
00284 Bool_t exists[4];
00285 Bool_t form_exists[4];
00286
00287 for(Int_t i=0;i<4;i++){
00288 exists[i] = kFALSE;
00289 form_exists[i] = kFALSE;
00290
00291 chk_start.Form("%s/%s",
00292 ExpandPathName(fDataPath).Data(),BuildRunName(fRunType,fCurrentRun+i).Data());
00293
00294 chk_raw.Form("%s.raw",chk_start.Data());
00295 chk_raw_gz.Form("%s.raw.gz",chk_start.Data());
00296 chk_form.Form("%s.raw_form.txt",chk_start.Data());
00297
00298
00299
00300
00301 if( !gSystem->AccessPathName(chk_raw) || !gSystem->AccessPathName(chk_raw_gz) ) exists[i] = kTRUE;
00302 if( !gSystem->AccessPathName(chk_form) ) form_exists[i] = kTRUE;
00303
00304
00305
00306 }
00307
00308 Int_t ret_val = -1;
00309
00310 if(!exists[0] && ( exists[1] || exists[2] || exists[3] ) ) ret_val = (Int_t) E_Follow_GoToNext;
00311 else if( !exists[1] && !exists[2] && !exists[3] ) ret_val = (Int_t) E_Follow_RetryThis;
00312 else{
00313 ret_val = (Int_t) E_Follow_ProcessThis;
00314
00315 if(!form_exists[0]){
00316 Warning("CheckRuns","Form-file does not exist");
00317 }
00318 }
00319
00320 return ret_val;
00321 }
00322
00323
00324 Int_t TFndPreanProcess::FollowDAQ(){
00325
00326
00327
00328 if(!fPreanMan){
00329 Error("FollowDAQ","fPreanMan not defined");
00330 TerminateFroot();
00331 }
00332
00333 Info("FollowDAQ","Starting in \"DAQ-follow\" mode");
00334
00335
00336 fCurrentRun = fFirstRun;
00337
00338 E_FollowControl pr_fl = E_Follow_NotUsed;
00339 while(1){
00340
00341 if(fTotEvTgt != -1 && fTotEvCount >= fTotEvTgt){
00342 Printf("FollowDAQ ---> maximum number of events reached for current preanalysis process");
00343 break;
00344 }
00345
00346 pr_fl = (E_FollowControl) CheckRuns();
00347
00348 switch(pr_fl){
00349 case E_Follow_GoToNext:
00350 fCurrentRun++;
00351 Info("FollowDAQ","switching to run number %d",fCurrentRun);
00352 break;
00353 case E_Follow_RetryThis:
00354 Info("FollowDAQ","waiting for run number %d to be completed",fCurrentRun);
00355 gSystem->Sleep(3000);
00356 break;
00357 case E_Follow_ProcessThis:
00358 Info("FollowDAQ","ready to process run number %d",fCurrentRun);
00359 ProcessSingleRun();
00360 fCurrentRun++;
00361 break;
00362 default:
00363 Error("FollowDAQ","Don't know what to do!");
00364 TerminateFroot();
00365 }
00366
00367 }
00368
00369
00370 return 0;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404