mcr/prean/FinudaPreanalysis.C

00001 
00002 //   Author: Diego Faso                                                                 //
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, // init only
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(); // alternative to RunLoop()
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(); // mode handled depending on first/last run
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; // exit loop if run completed
00151     fPreanMan->AnalyzeJFGES();
00152     if( fPreanMan->ReconstructFidaEvent() !=0) return; // exit loop in case of problems
00153     fPreanMan->AnalyzeJFDST();
00154     fCurEv++;
00155     fTotEvCount++;
00156   } // event loop completed
00157   
00158 }
00159 
00160 //___________________________________
00161 Int_t TFndPreanProcess::ProcessPreanalysis(){
00162   // return value:
00163   //              0: ok
00164   //             -1: process mode undefined
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   // return value:
00190   //              0: ok
00191   //             -1: run initialization not possible 
00192 
00193   Int_t initval = fPreanMan->InitNewPreanRun(fRunType,fCurrentRun); // offline mode
00194   if(initval!=0){
00195     //      Printf("initval: %d",initval);
00196     fPreanMan->FinishPreanRun(kFALSE);
00197     fCurrentRun += fIncrement;
00198     Printf("\n --- --- --- --- \n"); // simply nice ;)
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; // reset single run event counter
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); // last run completed
00219 
00220   // ---
00221   fLogMsg.Form("\nProcess end time: %s",fCheckTime_Now.AsString());
00222   fPreanMan->WriteLogFile(fLogMsg);
00223   StopTimeCheck(); // will add statistics to current log file
00224   fPreanMan->CloseLogFile();
00225   // ---
00226   
00227   Printf("\n --- --- --- --- \n"); // simply nice ;)
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){ // run loop
00247     if(fLastRun >= fFirstRun && fCurrentRun > fLastRun) break;    
00248     else if(fLastRun < fFirstRun && fCurrentRun < fLastRun) break; // allow reverse processing
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; // one initialization only in online-mode
00259     
00260     //
00261     fCurrentRun += fIncrement;
00262   } // run loop completed
00263   
00264   fPreanMan->CloseInterface(); // close used reconstruction (dynamic) library
00265   
00266   return 0;
00267   
00268 }
00269 
00270 //___________________________________
00271 Int_t TFndPreanProcess::CheckRuns(){
00272   // return value:
00273   //       E_Follow_GoToNext
00274   //       E_Follow_RetryThis
00275   //       E_Follow_ProcessThis
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     // if !cur && ( cur+1 || cur+2 || cur+3 ) ===> next
00300     // if (!cur+1 && !cur+2 && !cur+3) ===> retry
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     //    cout << "chk_start: \"" << chk_start << "\" ---> " << exists[i] << " ===> " << form_exists[i] << endl;
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   // alternative to RunLoop()
00326   //  used to follow DAQ closed runs
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; // process flag
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;// will continue loop incrementing run-number
00353     case E_Follow_RetryThis:
00354       Info("FollowDAQ","waiting for run number %d to be completed",fCurrentRun);
00355       gSystem->Sleep(3000);
00356       break; // will continue loop without incrementing run-number
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 // void FinudaPreanalysis(TString run_type="FINU",Int_t first_run = -1,Int_t last_run = -1,Int_t n_evs_run=-1,Int_t n_evs_tot=-1){
00375 //   // NOTE:
00376 //   //      in order to follow DAQ close run use "last_run = -1"
00377 
00378 //   Int_t debug_lev = 0;
00379 
00380 //   TFndPreanProcess *PreanProc = new TFndPreanProcess();
00381 
00382 //   PreanProc->SetDBhost("$MSQL_DB_HOST");
00383 //   PreanProc->SetDataPath("$RDT");
00384 //   PreanProc->SetRunType(run_type);
00385 //   PreanProc->SetRunEventTarget(n_evs_run);
00386 //   PreanProc->SetTotEventTarget(n_evs_tot);
00387 //   PreanProc->SetDebugLevel(debug_lev);
00388 
00389 //   PreanProc->SetFirstRun(first_run);
00390 //   PreanProc->SetLastRun(last_run);
00391 
00392 
00393 //   cout << " -> FinudaPreanalysis starting ..." << endl;
00394   
00395 //   PreanProc->ProcessPreanalysis();
00396 
00397   
00398   
00399 //   // ---
00400 //   delete PreanProc; // free used memory
00401   
00402 //   cout << "    -> done!" << endl;
00403 //   cout << " -> FinudaPreanalysis completed!" << endl;
00404 // }

Generated on Tue Oct 16 15:40:47 2007 by  doxygen 1.5.2