PREAN/TFndPreanProcess.cxx

00001 // @(#)fROOT/PREAN:$Name:  $:$Id: TFndPreanProcess.cxx,v 1.4 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2007/01/15
00003 
00005 //  This class is designed in order to drive the preanalysis process.          //
00006 //  TFndPreanMan is used and the support for multi-run processing is provided. //
00007 //                                                                             //
00008 //  TFndPreanMan could be used directly, by the way this class simplify the    //
00009 //  run-processing: three run-processing modes are available:                  //
00010 //          1) standard mode:                                                  //
00011 //               Increasing run-number (first-run < last-run)                  //
00012 //                                                                             //
00013 //          2) reverse mode:                                                   //
00014 //               Decreasing run-number (first-run > last-run)                  //
00015 //                                                                             //
00016 //          3) DAQ-follow mode:                                                //
00017 //               A check of the last acquired run is performed, in order to    //
00018 //               follow the DAQ, without the need of users interacation        //
00019 //                                                                             //
00020 //  Pre-analysis log files and benchmarks are also provided by this class.     //
00021 //                                                                             //
00022 //  This class is directly used by the "PreanProcFinuda" executable            //
00023 //  (official pre-analysis producer)                                           //
00024 //                                                                             //
00026 
00027 #include "TFndPreanProcess.h"
00028 
00029 
00030 ClassImp(TFndPreanProcess)
00031 
00032 //___________________________________
00033 TFndPreanProcess::TFndPreanProcess():
00034   fPreanMan(),fDBhost(),fDataPath(),fRunType(),
00035   fFirstRun(),fLastRun(),fIsLastRun(),
00036   fDebugLevel(),
00037   fTotEvCount(),fTotEvTgt(),fEvPerRunTgt(),fCurEv(),fIncrement(),fCurrentRun(),fLogFileName(),fLogMsg(),
00038   fCheckTime_Now(),fStartTime(),fStopTime()
00039 {
00040   
00041   fStartTime = 0;
00042   fStopTime = 0;
00043   fTotEvCount = 1;
00044 
00045 }
00046 
00047 //___________________________________
00048 TFndPreanProcess::~TFndPreanProcess(){
00049 
00050   Info("~TFndPreanProcess","Destroying the preanalysis manager");
00051   delete fPreanMan;
00052 }
00053 
00054 //___________________________________
00055 void TFndPreanProcess::InitPreanalysis(){
00056   
00057   if(fPreanMan){
00058     Error("InitPreanalysis","fPreanMan should not be defined yet!");
00059     delete fPreanMan;
00060     TerminateFroot();
00061   }
00062   fPreanMan = new TFndPreanMan(fDBhost,fDataPath,fDebugLevel);
00063 }
00064 
00065 //___________________________________
00066 void TFndPreanProcess::StartTimeCheck(){
00067 
00068   fCheckTime_Now.Set();
00069   fStartTime = fCheckTime_Now.Convert();
00070 }
00071 
00072 //___________________________________
00073 void TFndPreanProcess::StopTimeCheck(){
00074 
00075   fCheckTime_Now.Set();
00076   fStopTime = fCheckTime_Now.Convert();
00077   Int_t rate = Int_t ( (Float_t)fCurEv / (Float_t)(fStopTime - fStartTime) );
00078 
00079   cout << endl << " --- --- --- --- --- --- --- --- --- --- --- ---" << endl;
00080   if(fCurEv >0) fLogMsg.Form("Performances test: %d events processed in %d s ===> %d ev/s",fCurEv, fStopTime - fStartTime, rate );
00081   else fLogMsg.Form("Performances test: elapsed time: %d s",fStopTime - fStartTime );
00082   cout << fLogMsg << endl;  
00083   cout << " --- --- --- --- --- --- --- --- --- --- --- ---" << endl << endl;
00084   fPreanMan->WriteLogFile(fLogMsg);
00085   fPreanMan->WriteLogFile("\n --- --- --- --- --- --- --- \n\n");
00086 }
00087 
00088 //___________________________________
00089 void TFndPreanProcess::EventLoop(){
00090   
00091   while(1){
00092     if( fCurEv % 300 == 0 ) gROOT->Info("FinudaPreanalysis","Processing run %s-%d ===> event %d",fRunType.Data(),fCurrentRun,fCurEv);
00093     if(fTotEvTgt != -1 && fTotEvCount > fTotEvTgt){
00094       Printf("PreanalyzeRun ---> maximum number of events reached for current preanalysis process");
00095       return;
00096     }
00097     if(fEvPerRunTgt != -1 && fCurEv > fEvPerRunTgt){
00098       Printf("PreanalyzeRun ---> maximum number of events reached for current run");
00099       return;
00100     }
00101     if( fPreanMan->GetNextFidaEvent() != 0) return; // exit loop if run completed
00102     fPreanMan->AnalyzeJFGES();
00103     if( fPreanMan->ReconstructFidaEvent() !=0) return; // exit loop in case of problems
00104     fPreanMan->AnalyzeJFDST();
00105     fCurEv++;
00106     fTotEvCount++;
00107   } // event loop completed
00108   
00109 }
00110 
00111 //___________________________________
00112 Int_t TFndPreanProcess::ProcessPreanalysis(){
00113   // return value:
00114   //              0: ok
00115   //             -1: process mode undefined
00116   
00117 
00118   InitPreanalysis();
00119 
00120   if (fFirstRun == 0 && fLastRun == 0){
00121     Warning("ProcessPreanalysis()","Can not determine process mode");
00122     return -1;
00123   }
00124   
00125   Int_t ret_val = 
00126     (fLastRun == -1) ? 
00127     FollowDAQ() :
00128     RunLoop();
00129 
00130   return ret_val;
00131 }
00132 
00133 
00134 //___________________________________
00135 Int_t TFndPreanProcess::ProcessSingleRun(){
00136   // return value:
00137   //              0: ok
00138   //             -1: run initialization not possible 
00139 
00140   Int_t initval = fPreanMan->InitNewPreanRun(fRunType,fCurrentRun); // offline mode
00141   if(initval!=0){
00142     //      Printf("initval: %d",initval);
00143     fPreanMan->FinishPreanRun(kFALSE);
00144     fCurrentRun += fIncrement;
00145     Printf("\n --- --- --- --- \n"); // simply nice ;)
00146     return -1;
00147   }
00148   
00149   fLogFileName.Form("%s_prean.log",BuildRunName(fRunType,fCurrentRun).Data());
00150   fPreanMan->OpenLogFile("$PWD",fLogFileName);
00151   Printf("New run started (%s %d)",fRunType.Data(),fCurrentRun);
00152   
00153   fCurEv = 1; // reset single run event counter
00154   StartTimeCheck(); //
00155   
00156   fLogMsg.Form("Process Start time: %s",fCheckTime_Now.AsString() );
00157   fPreanMan->WriteLogFile(fLogMsg);
00158   fLogMsg.Form(" FIDARC release: \"%s\"\n",FIN_PHYS::FidaVer_Name().Data() );
00159   fPreanMan->WriteLogFile(fLogMsg);
00160   
00161   // ---
00162   EventLoop();
00163   // ---
00164   
00165   fPreanMan->FinishPreanRun(fIsLastRun); // last run completed
00166 
00167   // ---
00168   fLogMsg.Form("\nProcess end time: %s",fCheckTime_Now.AsString());
00169   fPreanMan->WriteLogFile(fLogMsg);
00170   StopTimeCheck(); // will add statistics to current log file
00171   fPreanMan->CloseLogFile();
00172   // ---
00173   
00174   Printf("\n --- --- --- --- \n"); // simply nice ;)
00175   
00176   return 0;
00177 }
00178 
00179 
00180 //___________________________________
00181 Int_t TFndPreanProcess::RunLoop(){
00182   
00183   if(!fPreanMan){
00184     Error("RunLoop","fPreanMan not defined");
00185     TerminateFroot();
00186   }
00187 
00188   Info("RunLoop","Starting loop on runs (%d -> %d)",fFirstRun,fLastRun);
00189 
00190   fCurrentRun = fFirstRun;
00191   fIncrement = (fLastRun >= fFirstRun) ? +1 : -1;
00192   
00193   while(1){ // run loop
00194     if(fLastRun >= fFirstRun && fCurrentRun > fLastRun) break;    
00195     else if(fLastRun < fFirstRun && fCurrentRun < fLastRun) break; // allow reverse processing
00196     
00197     if(fTotEvTgt != -1 && fTotEvCount >= fTotEvTgt){
00198       Printf("RunLoop ---> maximum number of events reached for current preanalysis process");
00199       break;
00200     }
00201     
00202     fIsLastRun = (fCurrentRun == fLastRun)? kTRUE : kFALSE;
00203     if(ProcessSingleRun() == -1) continue;
00204     
00205     if(fCurrentRun == -1) break; // one initialization only in online-mode
00206     
00207     //
00208     fCurrentRun += fIncrement;
00209   } // run loop completed
00210   
00211   //  fPreanMan->CloseInterface(); // close used reconstruction (dynamic) library
00212   
00213   return 0;
00214   
00215 }
00216 
00217 //___________________________________
00218 Int_t TFndPreanProcess::CheckRuns(){
00219   // return value:
00220   //       E_Follow_GoToNext
00221   //       E_Follow_RetryThis
00222   //       E_Follow_ProcessThis
00223 
00224   // ---  
00225   TString chk_start;
00226   TString chk_raw;
00227   TString chk_raw_gz;
00228   TString chk_form;
00229   // ---
00230 
00231   Bool_t exists[4];
00232   Bool_t form_exists[4];
00233 
00234   for(Int_t i=0;i<4;i++){
00235     exists[i] = kFALSE;
00236     form_exists[i] = kFALSE;
00237     
00238     chk_start.Form("%s/%s",
00239                    ExpandPathName(fDataPath).Data(),BuildRunName(fRunType,fCurrentRun+i).Data());
00240     
00241     chk_raw.Form("%s.raw",chk_start.Data());
00242     chk_raw_gz.Form("%s.raw.gz",chk_start.Data());
00243     chk_form.Form("%s.raw_form.txt",chk_start.Data());
00244     // ---  
00245     
00246     // if !cur && ( cur+1 || cur+2 || cur+3 ) ===> next
00247     // if (!cur+1 && !cur+2 && !cur+3) ===> retry
00248     if( !gSystem->AccessPathName(chk_raw) || !gSystem->AccessPathName(chk_raw_gz) ) exists[i] = kTRUE;
00249     if( !gSystem->AccessPathName(chk_form) ) form_exists[i] = kTRUE;
00250 
00251     //    cout << "chk_start: \"" << chk_start << "\" ---> " << exists[i] << " ===> " << form_exists[i] << endl;
00252 
00253   }
00254   
00255   Int_t ret_val = -1;
00256 
00257   if(!exists[0] && ( exists[1] ||  exists[2] ||  exists[3] ) ) ret_val = (Int_t) E_Follow_GoToNext;
00258   else if( !exists[1] && !exists[2] && !exists[3] ) ret_val = (Int_t) E_Follow_RetryThis;
00259   else{
00260     ret_val = (Int_t) E_Follow_ProcessThis;
00261     
00262     if(!form_exists[0]){
00263       Warning("CheckRuns","Form-file does not exist");
00264     }
00265   }
00266 
00267   return ret_val;
00268 }
00269 
00270 //___________________________________
00271 Int_t TFndPreanProcess::FollowDAQ(){
00272   // alternative to RunLoop()
00273   //  used to follow DAQ closed runs
00274 
00275   if(!fPreanMan){
00276     Error("FollowDAQ","fPreanMan not defined");
00277     TerminateFroot();
00278   }
00279 
00280   Info("FollowDAQ","Starting in \"DAQ-follow\" mode");
00281 
00282   
00283   fCurrentRun = fFirstRun;
00284 
00285   E_FollowControl pr_fl = E_Follow_NotUsed; // process flag
00286   while(1){
00287     // ---
00288     if(fTotEvTgt != -1 && fTotEvCount >= fTotEvTgt){
00289       Printf("FollowDAQ ---> maximum number of events reached for current preanalysis process");
00290       break;
00291     }
00292     
00293     pr_fl = (E_FollowControl) CheckRuns();
00294     
00295     switch(pr_fl){
00296     case E_Follow_GoToNext:
00297       fCurrentRun++;
00298       Info("FollowDAQ","switching to run number %d",fCurrentRun);
00299       break;// will continue loop incrementing run-number
00300     case E_Follow_RetryThis:
00301       Info("FollowDAQ","waiting for run number %d to be completed",fCurrentRun);
00302       gSystem->Sleep(3000);
00303       break; // will continue loop without incrementing run-number
00304     case E_Follow_ProcessThis:
00305       Info("FollowDAQ","ready to process run number %d",fCurrentRun);
00306       ProcessSingleRun();
00307       fCurrentRun++;
00308       break;
00309     default:
00310       Error("FollowDAQ","Don't know what to do!");
00311       TerminateFroot();
00312     }
00313     
00314   }
00315 
00316   
00317   return 0;
00318 }

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