PREAN/TFndPreanMan.cxx

00001 // @(#)fROOT/PREAN:$Name:  $:$Id: TFndPreanMan.cxx,v 1.57 2007/10/04 14:10:26 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2005/06/24
00003 
00005 //  This class is designed in order to manage any preanalysis process          //
00006 //  based or not on the FINUDA official reconstruction code.                   //
00007 //  You should never use more than one object of this class within the         //
00008 //  same preanalysis process (even if you are performing a loop on             //
00009 //  several runs).                                                             //
00010 //                                                                             //
00011 //  The Preanalysis-Manager is written to work both in online and offline      //
00012 //  mode, depending on the arguments passed to the InitNewPreanRun() method:   //
00013 //            ===> (0,0) means online (default arguments)                      //
00014 //            ===> ("FINU",10) means offline processing of FINU00010.raw       //
00015 //                             (both arguments must be specified)              //
00016 //                                                                             //
00017 //  You can call InitNewPreanRun() only if no other process is running, i.e.   //
00018 //  you need to call FinishRun() first; the access to  InitNewPreanRun() and   //
00019 //  is controlled through the fProcessing data member.                         //
00020 //                                                                             //
00021 //  The fidarc<->froot interface is handled by the TFndProcessRec class        //
00022 //  through the fProcessRec data member; the main addresses for ZEBRA banks    //
00023 //  access are stored (and re-initialized by InitNewPreanRun()) in the         //
00024 //  fJFGES and fJFDST data members.                                            //
00025 //  Debug on TFndProcessRec is "hard coded"                                    //
00026 //  ("processrec_debug" in the source code of the "InitNewPreanRun" method)    //
00027 //                                                                             //
00028 //                                                                             //
00029 //  The SINGLE-EVENT-PREANALYSIS is splitted into three steps:                 //
00030 //     1) - Get raw event (in FROOT code) and perform pre-analysis             //
00031 //           (eventually you can perform also a custom analysis)               //
00032 //     2) - raw event --=> zebra --=> reconstructed hits                       //
00033 //        - ROOT event reconstruction from JFGES (AnalyzeJFGES() method)       //
00034 //     3) - fidarc standard reconstruction                                     //
00035 //        - ROOT zebra-dst analysis from JFDST (AnalyzeJFDST() method)         //
00036 //  NOTE: the ROOT-dst tree is completely independent on the pre-analysis.     //
00037 //                                                                             //
00039 
00040 #include <limits>
00041 #include <TROOT.h>
00042 #include <TSystem.h>
00043 #include <TApplication.h>
00044 #include <TMath.h>
00045 #include <TThread.h>
00046 
00047 #include "TFndProcessRec.h"
00048 #include "TFndPrean.h"
00049 #include "TFndPreanHistos.h"
00050 #include "TFndEnv.h"
00051 #include "TFndPreanMan.h"
00052 
00053 ClassImp(TFndPreanMan)
00054 
00055 //_____________________________
00056 TFndPreanMan::TFndPreanMan(TString msqlhost, TString data_path, Int_t debug_lev)
00057 : TFndRun(),
00058   fNumOfProcessedFidaEvents(), fJFGES(), fJFDST(), fCurPreanFile()
00059 {
00060   TFndEnv *env = TFndEnv::Instance();
00061 
00062   // Check existence of needed files (fida*.dat; fida*.so; etc...)
00063   fDebug = debug_lev;
00064   fMaxNofEvents = std::numeric_limits<Int_t>::max();
00065   
00066   if (msqlhost) fDBHost = ExpandPathName(msqlhost);
00067   else fDBHost = env->GetValue("froot.PreAnalysisManager.MsqlDbHost", "localhost", "MSQL_DB_HOST");
00068 
00069   if (data_path) fDataPath = data_path;
00070   else fDataPath = env->GetValue("froot.PreAnalysisManager.DataPath", "/data01", "RDT");
00071 
00072   fPrean = new TFndPrean(debug_lev);    // Preanalysis calculations
00073   fPrean->SetDeamonBehaviour();
00074 }
00075 
00076 //_____________________________
00077 TFndPreanMan::~TFndPreanMan(){
00078 
00079   delete fProcessRec; // why not?
00080   delete fPrean;
00081 }
00082 
00083 
00084 //_____________________________
00085 Int_t TFndPreanMan::CloseInterface(){
00086   // close shared libraries and delete fProcessRec
00087   // (fProcessRec is also resetted in order to allow deletion in dtor)
00088   // this method must be called before dtor
00089   // return value: exit code of the "dlclose" function
00090 
00091   if(!fProcessRec) return 0;
00092   Int_t dlclose_err = fProcessRec->CloseInterface();
00093   delete fProcessRec; fProcessRec = 0;
00094   return dlclose_err;
00095 }
00096 
00097 //_____________________________
00098 Int_t TFndPreanMan::InitNewPreanRun(TString runtype,Int_t runnum){
00099   // initialize interface for new run (default: online)
00100   
00101   Int_t ret_val = InitNewRun(runtype,runnum,kTRUE); // skip TFndRun event management
00102   if(ret_val) return ret_val;
00103 
00104   if(fOnlineFlag == ONL_ALL) fPrean->Init(kTRUE); 
00105   else fPrean->Init(kFALSE);
00106 
00107   if(fDebug) Info("InitNewPreanRun", "...initializing fidarc...");
00108   Bool_t processrec_debug = kFALSE;
00109   if (!fProcessRec) {
00110     if(processrec_debug) Printf("creating new process_rec");
00111     fProcessRec = new TFndProcessRec(fRunType.Data(), fRunNumber, fDataPath.Data(), fDBHost.Data(), processrec_debug);
00112   }
00113   else {
00114     if(fProcessRec->GetDebugFlag()) Printf("using existing process_rec");
00115     fProcessRec->InitNewRun(fRunType.Data(), fRunNumber, fDataPath.Data(), fDBHost.Data(), processrec_debug);
00116   }
00117   if(!fProcessRec) TerminateFroot(0,"TFndPreanMan::InitNewPreanRun ---> fProcessRec not available");
00118   
00119   if(fDebug) Info("InitNewPreanRun", "...fidarc initialized successfully.");
00120 
00121   return ret_val;
00122 }
00123 
00124 //_____________________________
00125 Int_t TFndPreanMan::FinishPreanRun(Bool_t last,Bool_t create_outfile){
00126   // reset interface and data members
00127   // in case of last run the interface is also closed
00128   // 
00129   // Current root-file with pre-analysis trees is closed
00130   // fPrean members are not reset until a new run is initialized
00131   
00132   TString runtype = fRunType; // will be reset by TFndRun::FinishRun
00133   Int_t runnum = fRunNumber;  // will be reset by TFndRun::FinishRun
00134 
00135   Int_t ret_val = FinishRun(last,kTRUE); // skip TFndRun close operations
00136   if(ret_val) return ret_val; // stop method here in case of errors
00137   
00138   fJFGES = 0;
00139   fJFDST = 0;
00140   
00141   if(!fProcessRec){
00142     Error("FinishRun", "The process seems to be running without fProcessRec");
00143     return -1;
00144   }
00145 
00146   if(fDebug) Info("FinishRun", "Closing run in fProcessRec (is last: %u)",last);
00147   fProcessRec->CloseRun(last);
00148 
00149   // --- create output file and save  
00150   if(create_outfile){
00151     TString cpth = "$FND_PREAN_TREES/";
00152     cpth+=FIN_PHYS::FidaVer_Name();
00153     TString start_pth=ExpandPathName(cpth);  
00154     
00155 #if defined _FND_OUTPUTS_USE_SUBDIR
00156     CreateGrpSubDir(runtype,runnum,start_pth);
00157     TString frunnam = BuildRunName(runtype,runnum,kTRUE);
00158 #else
00159     TString frunnam = BuildRunName(runtype,runnum);
00160 #endif
00161     
00162     TString fnam;
00163     fnam.Form("%s/%s.prean.root",
00164               start_pth.Data(),
00165               frunnam.Data() );
00166     
00167     fCurPreanFile = new TFile(fnam,"RECREATE");
00168     fCurPreanFile->SetCompressionLevel(1);
00169     if( fPrean->SaveTrees(fCurPreanFile) != -1 ){
00170       fCurPreanFile->Close();
00171       delete fCurPreanFile;
00172       fCurPreanFile = 0;
00173     }
00174   }
00175   // ---
00176   if(last){
00177     fPrean->Finish(); // perform ending preanalysis operations at the end of the last run
00178     fNumOfProcessedFidaEvents = 0;
00179     CloseInterface();
00180   }
00181   return ret_val;
00182 }
00183 
00184 //_____________________________
00185 // UInt_t TFndPreanMan::UpdateScalers(){
00186 //   // the elepsed time from last update is returned
00187 
00188 //     if (fDebug) Info("EvalLuminosity","Scalers Event number %u",GetEvNum_Raw());
00189 //     fPrean->ReadScalers(fRawEvent);
00190 // }
00191 
00192 //_____________________________
00193 
00194 // Int_t TFndPreanMan::EvalLuminosity(Int_t Lumin){
00195 //   // pre-analysis of the current raw event (ROOT)
00196 //   // arguments:
00197 //   //        - Scalers: true ==> Read scalers and text-files generation enabled
00198 //   //        - Lumin  : bit-mask for evaluation methods
00199 //   //                   1 (LUMIN_BHABHA)  ==> Bhabha lumin evaluation method
00200 //   //                   2 (LUMIN_KPKM)    ==> K+/K- lumin evaluation method
00201 //   //                   4 (LUMIN_KLKS)    ==> KL/KS lumin evaluatino method
00202 //   //                   7 (LUMIN_DEFAULT) ==> All above methods combined (default)
00203 //   //
00204 //   // this method has been re-written by H. So (July 2006)
00205   
00206 //   if(Lumin == LUMIN_NONE){
00207 //     Warning("EvalLuminosity","No method has been selected: nothing will happen...");
00208 //     return 0;
00209 //   }
00210 //   if(fProcessRec->GetBhabhaRecMode() != TFndProcessRec::E_SINGLE_HELIX){
00211 //     Warning("EvalLuminosity","Are you sure you want to use double-helix procedure for bhabha events?");
00212 //     gSystem->Sleep(500);
00213 //   }
00214   
00215   
00216 //   if (Lumin & LUMIN_UNUSED) Warning("EvalLuminosity", "unused bits are on (0x%x)\n", Lumin);
00217 //   if (Lumin & LUMIN_BHABHA) fPrean->EvalLuminBhabha(fProcessRec);
00218 //   if (Lumin & LUMIN_KPKM)   fPrean->EvalLuminKPKM(fProcessRec);
00219 //   if (Lumin & LUMIN_KLKS)   fPrean->EvalLuminKLKS(fProcessRec);
00220 
00221 //   /*
00222 //     finuda_lum.fast and raw format:
00223     
00224 //     | from epoch (unix time) |
00225 //     | to epoch (unix time) |
00226 //     | time bin size (seconds) |   // may be removed
00227 //     | instantaneous luminosity from bhabha (10^33 cm-2 s-1) |
00228 //     | instantaneous luminosity from K_short (10^33 cm-2 s-1) |
00229 //     | instantaneous luminosity from K stopped (10^33 cm-2 s-1) |
00230 //     | instantaneous luminosity integrated over 60 s (nbarn) | // to be understood
00231 //     | luminosity integrated over the time of the experiment (nbarn) | // to be understood
00232 //     | momentum mean value (GeV/c) |
00233 //     | momentum st dev (GeV/c) |
00234 //     | X mean value (cm) |
00235 //     | X st dev (cm) |
00236 //     | Z mean value (cm) |
00237 //     | Z st dev (cm) |
00238 //     | triggered events |
00239 //     | analyzed events |
00240 //     | reconstructed evented |
00241 //     | recognized bhabha events |
00242 //     //
00243 //     //
00244 //     //                            (recognized bhabha events * triggered events)
00245 //     //                            ---------------------------------------------
00246 //     //                                 (analyzed events * time bin size)
00247 //     Instantaneous Luminosity = -------------------------------------------------
00248 //     //                                     cross section * efficiency
00249     
00250 //     finuda_point.fast:
00251     
00252 //     | Mom value | X value | Z value |
00253 //   */
00254 //   return 0;
00255 // }
00256 
00257 
00258 //_____________________________
00259 Int_t TFndPreanMan::GetNextFidaEvent()
00260 {
00261   // --- fidarc event management (read_lib and JFGES zebra)
00262   //
00263   // return value:
00264   //              0 -> good event
00265   //              1 -> run completed
00266 
00267   fNumOfProcessedFidaEvents++;
00268   if (fDebug) Info("GetNextFidaEvent", "getting fida event %d (local counter)", fNumOfProcessedFidaEvents);
00269   Int_t FidaResult = 0;
00270 
00271   fPrean->ResetDataVariables();
00272 
00273   FidaResult = fProcessRec->FillJFGES();
00274   if (fNumOfProcessedFidaEvents == 1)
00275     fProcessRec->SetRuninf();   // set the common address one time only
00276   fJFGES = fProcessRec->GetJFGES();
00277   fJFDST = 0;                   // contains data relative to the previous event
00278 
00279   bzero(fRawEvent,sizeof(fRawEvent));
00280   memcpy(fRawEvent, fProcessRec->GetRawEvPointer(), sizeof(fRawEvent));
00281   //  fRawEvent = fProcessRec->GetRawEvPointer();
00282   return FidaResult;
00283 }
00284 
00285 //_____________________________
00286 Int_t TFndPreanMan::ReconstructFidaEvent(){
00287   // process fidarc reconstruction
00288   // and fill JFDST zebra banks
00289   // (thus loosing informations in JFDST)
00290   // This method must be called only if GetNextEvent()
00291   // was called previousely
00292   //
00293   // return value:
00294   //               0 -> good event
00295   //               1 -> run completed
00296   //              -1 -> problems
00297 
00298   Int_t result = fProcessRec->Reconstruct();
00299   fJFDST = fProcessRec->GetJFDST();
00300   fJFGES = 0; // not useful anymore (banks dropped by fidarc reconstruction)
00301   return result;
00302 }
00303 
00304 // //_____________________________
00305 // void TFndPreanMan::InitHistos(){
00306 //   // initialize fHistos and build histograms
00307 
00308 //   if (fDebug) Info("InitHistos", "Initializing histograms");
00309 //   fHistos = new TFndPreanHistos(fDebug);
00310 //   fHistos->BuildHistos();
00311 //   if (fDebug) Info("InitHistos", "done");
00312 // }
00313 
00314 //_____________________________
00315 void TFndPreanMan::AnalyzeJFGES(){
00316   // analyze JFGES zebra banks before processing fidarc event reconstruction
00317   // call this method only after GetNextEvent() and before ReconstructEvent()
00318 
00319   fPrean->ReadScalers(fRawEvent); // fidarc not involved
00320   fPrean->InspectGES(fProcessRec);
00321   fPrean->CheckTofinoCFDs(fProcessRec);
00322 }
00323 
00324 //_____________________________
00325 void TFndPreanMan::AnalyzeJFDST(){
00326   // analyze JFDST zebra banks
00327   // call this method only after ReconstructEvent()
00328  
00329   fPrean->AnalyzeBhabha(fProcessRec);
00330   fPrean->AnalyzeHype(fProcessRec);
00331   fPrean->UpdateGESTree();
00332 
00333 }
00334 
00335 //_______________________________________________________________________
00336 void TFndPreanMan::OpenLogFile(const TString &fdir, const TString &fnam){
00337 
00338   CloseLogFile();
00339 
00340   TString path_str;
00341   path_str.Form("%s/%s",ExpandPathName(fdir).Data(),fnam.Data());
00342 
00343   fCurLogFile = new ofstream();
00344 
00345   fCurLogFile->open(path_str.Data(),ofstream::out | ofstream::app);
00346 
00347   fPrean->SetLogFile(fCurLogFile);
00348 }

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