STEER/TFndRun.cxx

00001 // @(#)fROOT/STEER:$Name:  $:$Id: TFndRun.cxx,v 1.92 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Revision Author: Diego Faso <mailto:faso@to.infn.it>, 2005/06/24
00003 // Original Author: P. Cerello <mailto:cerello@to.infn.it>, 2000
00004 
00006 //                                                               //
00007 //  Control class for FINUDA C++ run processing                  //
00008 //  Only one single instance of this class exists.               //
00009 //  The object is pointed by the global fndrun.                  //
00010 //                                                               //
00011 //   -Supports the list of all FINUDA Detectors (fDetectors).    //
00012 //                                                               //
00013 //                                                               //
00014 //Begin_Html
00015 //  <img src="../froot_images/fnd_logo.gif">
00016 //End_Html
00017 //                                                               //
00019 
00020 //#include "FROOT.h"
00021 #include "TFndRun.h"
00022 
00023 #include "TFndGtsMon.h"
00024 #include "TFndTofMon.h"
00025 #include "TFndSilMon.h"
00026 #include "TFndLmdMon.h"
00027 #include "TFndStbMon.h"
00028 
00029 // socket includes management depends on CINT
00030 #if defined(__CINT__)
00031 #include <netinet/in.h>
00032 #endif
00033 #include <sys/ioctl.h>
00034 #include <sys/socket.h>
00035 // ---
00036 
00037 #include "msql.h" // to be removed as soon as possible
00038 
00039 #include "Bytes.h"
00040 
00041 TFndRun *fndrun = 0;
00042 
00043 ClassImp(TFndRun)
00044 
00045 TFndHdt *TFndRun::fgFinHdt = 0;
00046 //___________________________________________
00047 TFndRun::TFndRun(const TString &name, const TString &title):
00048   TNamed(name,title)
00049 {
00050   // default constructor:
00051   
00052   if(fndrun){// only 1 object of TFndRun allowed
00053     Error("TFndRun", "cannot create 2 objects of TFndRun!");
00054     TerminateFroot();
00055   }
00056   // --- initialization to be optimized
00057   fRunConfiguration = 0;
00058   fFeeMap = 0;
00059 
00060   fGenInfo = 0;
00061 
00062   fGtsMonFrame = 0;
00063   fTofMonFrame = 0;
00064   fSilMonFrame = 0;
00065   fLmdMonFrame = 0;
00066   fStbMonFrame = 0;
00067 
00068   fGtsMon = 0;
00069   fTofMon = 0;
00070   fIsmMon = 0;
00071   fOsmMon = 0;
00072   fLmdMon = 0;
00073   fStbMon = 0;
00074   
00075   fProcessRec = 0;
00076   // ---------------------
00077   
00078   fFinRdt = 0;
00079   if(name.IsNull()) SetName(name);  
00080   if(title.IsNull()) SetTitle(title);  
00081 
00082   Init();
00083 }
00084 
00085 //___________________________________________
00086 TFndRun::~TFndRun(){
00087 
00088   if (fndrun != this) fndrun = this;
00089 
00090   Info("~TFndRun","Cleaning memory in TFndRun...");
00091   delete fRunConfiguration;
00092   delete fFeeMap;
00093   delete fGenInfo;
00094 
00095 
00096   Info("~TFndRun","Deleting monitor producers");
00097   Info("~TFndRun"," (GTS)");
00098   delete fGtsMon;
00099   Info("~TFndRun"," (TOF)");
00100   delete fTofMon;
00101   Info("~TFndRun"," (ISM)");
00102   delete fIsmMon;
00103   Info("~TFndRun"," (OSM)");
00104   delete fOsmMon;
00105   Info("~TFndRun"," (LMD)");
00106   delete fLmdMon;
00107   Info("~TFndRun"," (STB)");
00108   delete fStbMon;
00109 
00110   if(fOnlineFlag == ONL_OFF){
00111     Info("~TFndRun","Deleting monitor consumers");
00112     delete fGtsMonFrame;
00113     delete fTofMonFrame;
00114     delete fSilMonFrame;
00115     delete fLmdMonFrame;
00116     delete fStbMonFrame;
00117   }
00118   
00119   Info("~TFndRun","Deleting hits buffers");
00120   delete fFinRdt;
00121   delete fFinHdt;
00122 
00124   fndrun = 0;
00125 
00127   //  delete fProcessRec; // handled by TFndPreanMan
00128   Info("~TFndRun","Memory allocated for TFndRun cleaned.");
00129 
00130 }
00131 
00132 //___________________________________________
00133 Int_t TFndRun::Init(){
00134   // private method called by ctors
00135   // (some operations are shared between different constructors)
00136 
00137   fndrun = this;
00138   fRunConfiguration = new TFndRunConfiguration(); 
00139 
00140   fIsNewRun = kFALSE;
00141   SetDBHost("$MSQL_DB_HOST");
00142   SetDataPath("$RDT");
00143   fMaxNofEvents = std::numeric_limits<Int_t>::max();
00144 
00145   fFeeMap = new TFndFeeMap();
00146   fRunTypeID == -1;
00147   fNumOfProcessedRuns = 0;
00148   fNumOfProcessedEvents = 0;
00149 
00150   fGenInfo = new TFndGenInfo();
00151 
00152   fSockLength = FIN_RAW_DATA_SOCK_LEN;
00153   request_counter = 0;
00154   fUdpSd = -1; // means error
00155   
00156   if (!fgFinHdt) fgFinHdt = new TFndHdt();
00157   fFinHdt = fgFinHdt;
00158   //  fFinHdt = new TFndHdt();
00159   fFinRdt = new TFndRdt();
00160 
00161   fCurLogFile = 0;
00162   for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) fMustFillMonitor[i] = kTRUE;
00163 
00164 }
00165 
00166 //_____________________________
00167 Int_t TFndRun::InitNewRun(TString runtype,const Int_t &runnum,const Bool_t &UsedByPrean){
00168   // return values:
00169   //                -  1: nothing to do: some other process is running
00170   //                -  0: ok (can start process)
00171   //                - -1: raw file not found (or not stat'ed - see TSystem::GetPathInfo -)
00172   //                - -2: raw file too little
00173   //                - -3: user error (wrong run type, etc...)
00174   //
00175   // NOTE: while using TFndPreanMan only the 
00176   //       following return value are possible:
00177   //                -  1: nothing to do: some other process is running
00178   //                -  0: ok (can start process) (not checking "-1" and "-2" cases)
00179   //                - -3: user error (wrong run type, etc...)
00180   //
00181   // maybe a check on run type/number should be added:
00182   // (now it is possible to make mistakes calling ONLM00000 and so running offline
00183   //  while the user thinks to run online...)
00184 
00185   fRunNumber = runnum;
00186   fGebIp = FROOT::Ip2Int(gSystem->Getenv("FND_GEBIP"));
00187 
00188   if (fProcessing) {
00189     Warning("InitNewRun", "Nothing to do: already processing");
00190     return 1;
00191   }
00192 
00193   if(SetRunType(runtype) == -1) return -3;
00194   
00195   if(!UsedByPrean){ // note that this is the default behaviour
00196     Info("InitNewRun", "New process starting");
00197     
00198     // raw-file/socket raw event management
00199     if (runnum == -1) {             // ONLINE MODE
00200       Info("InitNewRun", "Initializing new run in online mode");
00201       fOnlineFlag = ONL_ALL;
00202       fRunType = "ONLM";       // override any other name (maybe it is better to chek previousely)
00203       RecreateSocketToDAQ();
00204     }
00205     else {                          // OFFLINE MODE
00206       fOnlineFlag = ONL_OFF;
00207       
00208       fRunFilename =  BuildRunName(fRunType,fRunNumber);
00209       Info("InitNewRun", "Initializing new run in offline mode (run \"%s\")",fRunFilename.Data());
00210       Long_t raw_file_size = OpenRawFile(fDataPath, fRunFilename);
00211       if (raw_file_size == -1) {
00212         if(fDebug) Error("InitNewRun", "raw file not found");
00213         return -1;
00214       }
00215       else if (raw_file_size < k_MinRawFileSize) {
00216         Error("InitNewRun", "raw file too small (%ld)", raw_file_size);
00217         return -2;
00218       }
00219     }
00220   }
00221   else{ // performed when called by TFndPrean (daughter)
00222     if (runnum == -1) {             // ONLINE MODE
00223       Info("InitNewRun", "Initializing new run in online mode");
00224       fOnlineFlag = ONL_ALL;
00225       fRunType = "ONLM";
00226     }
00227     else { // OFFLINE MODE
00228       fOnlineFlag = ONL_OFF;
00229       
00230       fRunFilename =  BuildRunName(fRunType,fRunNumber);
00231       Info("InitNewRun", "Initializing new run in offline mode (run \"%s\")",fRunFilename.Data());
00232       Long_t raw_file_size = OpenRawFile(fDataPath, fRunFilename,kTRUE); // do not open file
00233       if (raw_file_size == -1) {
00234         if(fDebug) Error("InitNewRun", "raw file not found");
00235         return -1;
00236       }
00237       else if (raw_file_size < k_MinRawFileSize) {
00238         Error("InitNewRun", "raw file too small (%ld)", raw_file_size);
00239         return -2;
00240       }
00241     }
00242   }
00243   
00244   // --- it is safe to check here the online flag
00245   if(fOnlineFlag < (Int_t)(ONL_OFF) || fOnlineFlag > (Int_t)(ONL_ALL)){
00246     Error("GetNextRawEvent","You have just found a BUG! (fOnlineFlag = %d)",fOnlineFlag);
00247     TerminateFroot(0,"BUG detected: Requested online flag value not available");
00248   }
00249   
00250   fCurRunType = "";
00251   fCurRunNum = 0;
00252   fIsNewRun = kTRUE;
00253   fProcessing = kTRUE;  
00254   fNumOfProcessedRuns++;
00255   return 0;
00256 }
00257 
00258 //_____________________________
00259 Int_t TFndRun::FinishRun(Bool_t last,const Bool_t &UsedByPrean){
00260   // reset interface and data members
00261   // if "last=true" close fortran (can not restart!)
00262   // return values:
00263   //                -  1: nothing to do: some other process is running
00264   //                -  0: ok
00265   //                - -1: error
00266   //
00267   // Init/Finish process tested up to 3000 times
00268 
00269   if(!fProcessing){
00270     if(fDebug) Warning("FinishRun", "Nothing to do: no process running");
00271     return 1;
00272   }
00273 
00274   if(fOnlineFlag){
00275     Info("FinishRun", "Terminating online process");
00276     ShutdownSocketToDAQ();
00277   }
00278   else{
00279     Info("FinishRun", "Terminating process for run \"%s\" number %d", fRunType.Data(), fRunNumber);
00280     if(fRawFile) fclose(fRawFile);
00281   }
00282 
00283   fRunNumber = 0;
00284 
00285   fProcessing = kFALSE; // now ready to restart
00286   
00287   if(!UsedByPrean){
00288     if(fDebug) Info("FinishRun", "performing end-of-run operations.");
00289     WriteMonitors(kTRUE);
00290   }
00291   
00292   if (!last){
00293     Info("FinishRun", "Current run completed: ready to start a new run.");
00294   }
00295   else{
00296     Info("FinishRun", "Process terminated: performing end-of-process operations.");
00297     fNumOfProcessedRuns = 0;
00298     fNumOfProcessedEvents = 0;
00299     CleanEvent();
00300   }
00301   
00302   return 0;
00303 }
00304 
00305 //___________________________________________
00306 Int_t TFndRun::SetRunType(TString runtype){
00307   // return value:
00308   //              0: ok
00309   //             -1: selected run type not available
00310 
00311   //  Info("SetRunType","received string: \"%s\"",runtype.Data());
00312   fRunTypeID = -1; 
00313   for(Int_t i=(Int_t)(RT_FINU);i<=(Int_t)(RT_ONLM);i++){
00314     if((Int_t)(runtype.CompareTo(RunType_Name(i))) == 0){ fRunTypeID = i;  break; }
00315   }
00316   if(fRunTypeID == -1){
00317     Warning("SetRunType","runtype \"%s\" not accepted",runtype.Data());
00318     fRunType = "";
00319     return -1;
00320   }
00321   fRunType = runtype;
00322   //  Info("SetRunType","setting completed: fRunType = \"%s\", fRunTypeID = %d",fRunType.Data(),fRunTypeID);
00323   return 0;
00324 }
00325 //___________________________________________
00326 void TFndRun::SetRunType(Int_t runtype){
00327 
00328   if(runtype>(Int_t)(RT_ONLM)) Warning("SetRunType","runtype %d not accepted",runtype);
00329   fRunTypeID = runtype;
00330   fRunType = RunType_Name(runtype);
00331 }
00332 
00333 //___________________________________________
00334 void TFndRun::ClearEvent(){
00335   // method called at the end of every processed event
00336   
00337   if(fDebug){
00338     cout << "TFndRun::ClearEvent Info: Clearing event " << endl;
00339     cout << "TFndRun::ClearEvent Info: rdt: " << fFinRdt << endl;
00340   }  
00341   fFinRdt->Reset();
00342   fFinHdt->Reset();
00343 
00344   //  if(fOnlineFlag = ONL_OFF && fRawEvent != 0) { delete [] fRawEvent; fRawEvent = 0; }
00345   bzero(fRawEvent,sizeof(fRawEvent));
00346   //  if(fRawEvent != 0) { delete [] fRawEvent; fRawEvent = 0; }
00347 
00348   if(fDebug) cout << "TFndRun::ClearEvent Info: Event cleared" << endl;
00349 }
00350 
00351 //___________________________________________
00352 void TFndRun::CleanEvent(){
00353   // method called at the end of event processing
00354   // this method is 'protected' since it is needed only at the end
00355   // of every processed run.
00356 
00357   if(fDebug){
00358     cout << "TFndRun::CleanEvent Info: Cleaning event " << endl;
00359     cout << "TFndRun::CleanEvent Info: rdt: " << fFinRdt << endl;
00360   }
00361   delete fFinRdt; fFinRdt = NULL;
00362   delete fFinHdt; fFinHdt = NULL; 
00363   //  fFinHdt->ClearArrays(); // can not delete fFinHdt now
00364   //  if(fOnlineFlag = ONL_OFF && fRawEvent != 0) delete [] fRawEvent;  
00365 
00366   if(fDebug) cout << "TFndRun::CleanEvent Info: Event cleaned" << endl;
00367 }
00368  
00369 //_____________________________
00370 Int_t TFndRun::CheckMsqlConnection(){
00371   // return the output of msqlConnect()
00372 
00373   if(fDebug) Info("CheckMsqlConnection", " Checking connection to msql: host \"%s\"", ExpandPathName(fDBHost).Data());
00374 
00375   Int_t ntry = 0;
00376   Int_t nmaxtry = 30;
00377   Int_t sd = -1;
00378   
00379   while(1){
00380     sd = msqlConnect((Char_t *)fDBHost.Data());
00381     if(sd != -1){
00382       gSystem->Sleep(50);
00383       msqlClose(sd);
00384       gSystem->Sleep(50);
00385       return sd;
00386     }
00387     if(ntry){
00388       Info("CheckMsqlConnection", "No connection to DB on \"%s\": retrying (%d).", fDBHost.Data(), ntry);
00389       Warning("CheckMsqlConnection", "msql connection not available: retrying...");
00390     }
00391     ntry++;
00392     if(ntry>=nmaxtry){
00393       Warning("CheckMsqlConnection", "msql connection Maximum number of attempts reached");
00394       return sd;
00395     }
00396     gSystem->ProcessEvents();
00397     gSystem->Sleep(1000);
00398   }
00399   
00400   if(fDebug) Info("CheckMsqlConnection", " Socket to msql opened with identifier %d on \"%s\"", sd, fDBHost.Data());
00401   msqlClose(sd);
00402 
00403   return sd;
00404 }
00405 
00406 //___________________________________________
00407 Int_t TFndRun::ConnectToDB(){
00408   // return value:
00409   //              -1: DB internal error
00410   //              -2: Pointer to Database Interface not defined
00411   //              -3: Already connected
00412   //              -4: DB-connection not available
00413   //         (other): DB-connection socket descriptor
00414   //
00415   // TFndFeeMap is becoming deprecated!
00416   // support for mysql is coming soon...
00417   
00418   if(!fFeeMap){
00419     Error("ConnectToDB","Pointer to Database Interface not defined");
00420     return -2;
00421   }
00422   if(fFeeMap->IsConnected()){
00423     Error("ConnectToDB","Already connected to DB");
00424     return -3;
00425   }
00426   if (CheckMsqlConnection() < 0) {
00427     Error("InitNewRun", "No connection to database");
00428     return -4;
00429   }
00430 
00431   Info("ConnectToDB", "Connecting to database (DB-host: \"%s\")",fDBHost.Data());
00432   Int_t sd = fFeeMap->ConnectToHost(fDBHost);
00433   if(sd == -1) Warning("ConnectToDB","No connection to database (DB internal error)");
00434   return sd;
00435 }
00436 
00437 //___________________________________________
00438 Int_t TFndRun::DisconnectFromDB(){
00439   // return value:
00440   //               0: ok (connection succesfully closed)
00441   //              -1: no poiter to DB Interface: doing nothing
00442   // 
00443   // TFndFeeMap is becoming deprecated!
00444   // support for mysql is coming soon...
00445   
00446   if(!fFeeMap){
00447     Warning("DisconnectFromDB","Pointer to Database Interface not found");
00448     return -1;
00449   }
00450 
00451   fFeeMap->DisconnectFromHost();
00452   return 0;
00453 }
00454 
00455 //___________________________________________
00456 Int_t TFndRun::FetchDBinfo(Int_t source,Bool_t verbose){
00457   //
00458   // available values for "source" (see FROOT.h::EFndDBFetchSource):
00459   //    - K_DB_SOURCE_HOST (raw-event runtime is used to fetch information)
00460   //    - K_DB_SOURCE_FILE (previousley fetched info are grabbed from current HDT root-file)
00461   //
00462   // return value:
00463   //               0: ok
00464   //              -1: DB internal error
00465   //              -2: Pointer to Database Interface not defined
00466   //              -3: Current interface to DB not connected
00467   //
00468   // TFndFeeMap is becoming deprecated!
00469   // support for mysql is coming soon...
00470 
00471   
00472   //   if(source == (Int_t)(K_DB_SOURCE_FILE)){    
00473   //     fFeeMap = (TFndFeeMap*) fHdtRootFile->Get("TFndFeeMap");
00474   //     return 0;
00475   //   }
00476   //   if(source != (Int_t)(K_DB_SOURCE_HOST)){
00477   //     Error("FetchDBinfo","Wrong call (source not available)");
00478   //     TerminateFroot();
00479   //   } 
00480   // previous lines shall be replaced with a "switch" in case of more than two sources
00481   
00482   // still thinking about geting DB info from a previousely saved root-file
00483   // (in case of source==K_DB_SOURCE_FILE the complete path to the hdt-root-file shall be provided)
00484 
00485   if(!fFeeMap){
00486     Error("FetchDBinfo","Pointer to Database Interface not defined");
00487     return -2;
00488   }
00489   if(!fFeeMap->IsConnected()){
00490     Error("FetchDBinfo","Not connected to DB");
00491     return -3;
00492   }
00493   
00494   Info("FetchDBinfo","Fetching info from DB host");
00495   Int_t ierr = fFeeMap->FetchRunInfo(verbose);
00496   if(fDebug) Info("FetchDBinfo","ierr = %d",ierr);
00497 
00498   // --- if no database has been loaded yet...
00499   if(!fFeeMap){
00500     Error("FetchDBinfo","Why not connected yet?");
00501     TerminateFroot();
00502   }
00503 
00504   // --- debugging info
00505   //   fFeeMap->PrintTofCalibInfo();
00506   //   fFeeMap->PrintLmdCalibInfo();
00507   //   fFeeMap->PrintStbCalibInfo();
00508   //
00509 
00510   if(fDebug ==0) Info("FetchDBinfo","done");  
00511   return 0; // return value will take into acount the DB-connection status
00512   //--- new connection to mysql coming soon
00513 }
00514 
00515 //___________________________________________
00516 Int_t TFndRun::CreateSocketToDAQ(E_Fnd_DaqRaw_Udp_Ports det_port){
00517 
00518   fUdpSd= socket(AF_INET, SOCK_DGRAM, 0);
00519   // reset (and set) the socket
00520   bzero((char*)&fLocSockAddr,sizeof(fLocSockAddr));
00521   fLocSockAddr.sin_family      = AF_INET;
00522   fLocSockAddr.sin_addr.s_addr = INADDR_ANY;  
00523   fLocSockAddr.sin_port        = 0; 
00524   
00525   Info("CreateSocketToDAQ","new socket created with socket descriptor %d; now binding...",fUdpSd);
00526   int bind_err = bind(fUdpSd, (struct sockaddr *) &fLocSockAddr, sizeof(fLocSockAddr)); 
00527   if(bind_err == -1) TerminateFroot(0,"TFndRun::CreateSocketToDAQ ---> Bind operation failed!");
00528   Info("CreateSocketToDAQ","...done (bind_err = %d)",bind_err);
00529   
00531   // The initialization of fShrSockAddr is required in order to send requests   //
00532   // The process, anyway, must be binded to fLocSockAddr: can not send/receive  //
00533   // data by using the binded structure!                                        //
00535   
00536   bzero((char*)&fShrSockAddr,sizeof(fShrSockAddr));
00537   fShrSockAddr.sin_family      = AF_INET;
00538   fShrSockAddr.sin_addr.s_addr = htonl(fGebIp);
00539   fShrSockAddr.sin_port        = htons(det_port); 
00540   fShrSockAddr_size    = sizeof(fShrSockAddr);;
00541   
00542   // set the current socket (through the descriptor) to receive events
00543   int sock_err = setsockopt(fUdpSd,SOL_SOCKET,SO_RCVBUF,&fSockLength,sizeof(fSockLength));
00544   if(sock_err==-1) TerminateFroot(0,"TFndRun::CreateSocketToDAQ ---> setsockopt failed failed!");
00545   return 0;
00546 
00547 }
00548 
00549 //___________________________________________
00550 Int_t TFndRun::CheckSocketToDAQ(){
00551   // set according to a minimum rate of 1ev/3s
00552 
00553   Int_t check_result=0;
00554   Int_t nn = 0;
00555   while(nn < 50){ // was 10
00556     nn++;
00557     //    cout << "check socket " << nn << endl;
00558     ioctl(fUdpSd,FIONREAD,(Int_t *)&check_result);
00559     if(check_result != 0) break;
00560     usleep(3000); // according to maximum rate required (was 5000)
00561     // slow down depending on answer time (DAQ rate)
00562     if(nn >  2) usleep(10000);
00563     if(nn >  3) usleep(40000);
00564     if(nn >  5) usleep(500000);
00565     if(nn >  7) usleep(500000);
00566     if(nn >  9) usleep(100000);
00567     //     if(nn > 11) usleep(100000);
00568     //     if(nn > 13) usleep(100000);
00569     //     if(nn > 15) usleep(100000);
00570     //     if(nn > 17) usleep(100000);
00571     //     if(nn > 19) usleep(500000);
00572   }
00573   return check_result;
00574 }
00575 
00576 //___________________________________________
00577 Int_t TFndRun::ShutdownSocketToDAQ(){
00578   return shutdown(fUdpSd,SHUT_RDWR);
00579 }
00580 
00581 //___________________________________________
00582 UInt_t *TFndRun::GetNextRawEventFromSocket(int &exit_val){
00583   // get the next raw-event (from socket)
00584   // send the request
00585   // NOTE:
00586   //   A loop of requests is required, unless you are sure
00587   //   that the server is already running and witing for client requests
00588   //    "ioctl" is used to know if the request has been received by the server
00589   //
00590   // tested with simulated DAQ rate in the range [0.3 - 200] ev/s
00591   //
00592   // exit_val value: 
00593   //                0: ok
00594   //               -2: must re-create socket
00595   
00596   bzero(fRawEvent,sizeof(fRawEvent));
00597   //   char request_buf[100];
00598   //   sprintf(request_buf,"send me an event");
00599 
00600   Int_t request_buf=1;
00601   
00602   Int_t n_max_requests = 50; // after 50 requests recreate socket (was 10)
00603   
00604   int n_trials = 0;
00605   while(1){
00606     n_trials++; if(n_trials> n_max_requests){
00607       exit_val = -2;
00608       return 0;
00609     }
00610     request_counter++;
00611     //Info("GetNextRawEventFromSocket","sending request to the server (%d)",request_counter);
00612     //int send_err = sendto(fUdpSd,(char*)request_buf ,sizeof(request_buf) ,0 ,(struct sockaddr *) &fShrSockAddr,fShrSockAddr_size);
00613     int send_err = sendto(fUdpSd,(char*)&request_buf ,sizeof(Int_t) ,0 ,(struct sockaddr *) &fShrSockAddr,fShrSockAddr_size);    
00614     if(send_err==-1){
00615       Error("GetNextRawEventFromSocket","send-request operation failed!");
00616       sleep(1);
00617       continue;
00618     }
00619 
00620     int check_result=CheckSocketToDAQ(); // wait for answer
00621     // cout << "check_result: " << check_result << endl;
00622     if(check_result) break;
00623     else Info("GetNextRawEventFromSocket","...server not ready: trying again");
00624   }
00625   // the data fRawEvent is sent from the server to the "sockaddr_in" who sent the request (so it is used to receive)
00626   Int_t nread = recvfrom(fUdpSd ,(char*)fRawEvent ,sizeof(fRawEvent) ,0 ,(struct sockaddr*)&fShrSockAddr , (socklen_t *) &fShrSockAddr_size);
00627 
00628   if(fDebug > 0) printf("UDP fRawEvent received from server: nread = %d; event = \"%x\".\n",nread,fRawEvent);
00629 
00631   if(nread == -1){
00632     Error("GetNextRawEventFromSocket","nread = -1");
00633     exit_val = -2;
00634     return 0;
00635   }
00636   else if(nread == 0){
00637     Error("GetNextRawEventFromSocket","connection closed by GEB");
00638     exit_val = -2;
00639     return 0;
00640   }
00641 
00642   Int_t u_length = nread / sizeof(fRawEvent[0]);
00643   if( u_length >= sizeof(fRawEvent) ){
00644     Warning("GetNextRawEventFromSocket","received buffer size greater than local buf-size");
00645     exit_val = -2;
00646     return 0;
00647   }
00648   if( u_length != fRawEvent[0] ){
00649     Warning("GetNextRawEventFromSocket","received buffer size different from event size");    
00650     exit_val = -2;
00651     return 0;
00652   }
00653 
00654   if(fDebug > 0) if(u_length == fRawEvent[0]) Info("GetNextRawEventFromSocket","received event OK");
00655   // --- CHECKS completed
00656 
00657   if(fDebug > 1){
00658     for(unsigned int j=0; j<5; j++){
00659       if(fRawEvent[j] != 0) cout << "fRawEvent[" << j << "] = " << fRawEvent[j] << endl;
00660     }
00661   }
00662   else if(fDebug > 2){
00663     unsigned int len_test = fRawEvent[0];
00664     for(unsigned int j=0; j<len_test; j++){
00665       if(fRawEvent[j] != 0) cout << "fRawEvent[" << j << "] = " << fRawEvent[j] << endl;
00666     }
00667   }
00669  
00670   CheckRawSwapping();
00671 
00672   exit_val = 0;
00673   return fRawEvent;  
00674 }
00675 
00676 //___________________________________________
00677 UInt_t *TFndRun::GetNextRawEventFromFile(int &exit_val){
00678   // get the next raw-event (from file)
00679 
00680   UInt_t evlen;
00681   if(fDebug > 1) Info("GetNextRawEventFromFile", "Get event from disk");
00682   if(fread(&evlen, sizeof(UInt_t), 1 , fRawFile)!=1) {
00683     Info("GetNextRawEventFromFile", "End of Run reached");
00684     exit_val = 2;
00685     return 0; 
00686   }
00687   bzero(fRawEvent,sizeof(fRawEvent));
00688   fRawEvent[0] = (UInt_t) evlen;
00689   Int_t read_length = 1 + fread(fRawEvent+1, sizeof(UInt_t), evlen-1, fRawFile);
00690   if(read_length != evlen) Error("GetNextRawEventFromFile","wrong length from freed"); 
00691 
00692   CheckRawSwapping();
00693   return fRawEvent;  
00694 }
00695 
00696 //___________________________________________
00697 UInt_t *TFndRun::GetNextRawEvent(Int_t &exit_value){
00698   // This method can be used by any application.
00699   //
00700   // Get the pointer to the next raw event and
00701   // store it into the relative data member
00702   //
00703   // exit_value:
00704   //             0: good event: go on
00705   //            -2: No answer received from DAQ (online mode only)
00706   //            -1: (not used here)
00707   //             1: requested number of events reached
00708   //             2: end of run reached
00709 
00710   exit_value = 0; 
00711 
00712   if(fNumOfProcessedEvents >= fMaxNofEvents) {
00713     Info("GetNextRawEvent", "Requested number of events reached: process completed");
00714     exit_value = 1;
00715     return 0; 
00716   }
00717   if(fDebug) Info("GetNextRawEvent", "fOnlineFlag: %d", fOnlineFlag);
00718   
00720   if(fOnlineFlag == (Int_t)(ONL_OFF)){
00721     GetNextRawEventFromFile(exit_value);
00722   }
00723   else{ // online mode    
00724     GetNextRawEventFromSocket(exit_value);
00725     if(exit_value == -2){
00726       RecreateSocketToDAQ();
00727       return 0;
00728     }
00729   }
00730   
00731   //  if(GetEvNum_Raw() % 100 ==0) Printf("GetNextRawEvent ---> Event: %u; exit_val = %d",GetEvNum_Raw(),exit_value);
00732 
00733   fNumOfProcessedEvents++;
00734   return fRawEvent;
00735 
00736 }
00737 
00738 //___________________________________________
00739 void TFndRun::CheckRawSwapping(){
00740   // bytes swapping is applied to current raw event if needed
00741 
00742   UInt_t evlen = fRawEvent[0];
00743   //evlen = fRawEvent[0];
00744   Int_t unswpd_len = 0;
00745   Int_t swpd_len = 0;
00746   Int_t swap = 0;
00747   unswpd_len = evlen;
00748   swpd_len = host2net(evlen);
00749   swap = 0;
00750   //  Printf("Checking swap  ---> unswapped length: %d", unswpd_len);
00751   //  Printf("Checking swap  ---> swapped length: %d", swpd_len);
00752   if(unswpd_len <= 0) {
00753     evlen = swpd_len;
00754     swap = 1;
00755   }
00756   else if(swpd_len <= 0) {
00757     evlen = unswpd_len;
00758     swap = 0;
00759   }
00760   else {
00761     if(swpd_len < unswpd_len) {
00762       evlen = swpd_len;
00763       swap = 1; 
00764     }
00765     else {
00766       evlen = unswpd_len;
00767       swap = 0;
00768     }
00769   }
00770   //  Printf("Checking swap  ---> event length: %u; swap flag: %d", evlen,swap);
00771   if(swap == 1) for (UInt_t j=1; j<evlen;  j++) fRawEvent[j] = host2net(fRawEvent[j]);
00772 }
00773 
00774 //___________________________________________
00775 Bool_t TFndRun::IsNewRun(){
00776   
00777   if( fCurRunType.IsNull() || 
00778       fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) )!=0 || 
00779       fRawEvent[4] != fCurRunNum)
00780     {
00781       fIsNewRun = kTRUE;
00782       if(fDebug) Info("IsNewRun"," New run set to true (fRunType: \"%s\"; raw: \"%s\")",fCurRunType.Data(),DecodeRunType(fRawEvent[3]).Data());
00783       if(fDebug > 1){
00784         Info("IsNewRun","fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) ): %d",fCurRunType.CompareTo(DecodeRunType(fRawEvent[3]) ) );
00785         Info("IsNewRun","fRawEvent[4] = %u ; fCurRunNum = %u", fRawEvent[4],fCurRunNum );
00786       }
00787     }
00788   return fIsNewRun;
00789 }
00790 
00791 //___________________________________________
00792 Int_t TFndRun::ReadRawEvent(Bool_t using_db){
00793   // read and unpack current raw event filling the HDT structure
00794   // Use this methoid after "GetNextRawEvent" only!
00795   // returned values:
00796   //                 -1: DB-error
00797   //                  0: good event: go on (also for first event of a new run)
00798   //
00799   // NOTE: if you call this method you will fill HDT structure,
00800   //       since the decoding is useless...
00801   
00802   if(fDebug) Info("ReadRawEvent", "entering method: online flag = %d",fOnlineFlag);
00803   
00804   if(fOnlineFlag == ONL_ALL) IsNewRun();
00805   
00806   if(fRawEvent[13] != 0) Info("ReadRawEvent", "event 0 at event: %d, location: %u", fRawEvent[2], fRawEvent[13]); // next cicle if "Event 0"
00807   
00808   if(fDebug && (fOnlineFlag == ONL_ALL || fOnlineFlag == ONL_OFF)) Info("ReadRawEvent", "entering method: Run  %u; Event %u", fRawEvent[4],fRawEvent[2]);
00809   
00810   if(fIsNewRun == kTRUE) { // new run (first event) operations
00811     Info("ReadRawEvent",
00812          "Performing new run operations (run type: \"%s\"; run number = %u; event number = %u",
00813          DecodeRunType(fRawEvent[3]).Data(),
00814          fRawEvent[4],
00815          fRawEvent[2]);
00816     delete fGenInfo;
00817     fGenInfo = new TFndGenInfo(fRawEvent);
00818     fRunTime = fRawEvent[5];
00819     
00820     // DB info must be fetched from the official database if reading raw-data
00821     if(using_db){
00822       Info("ReadRawEvent","Fetching information from DB");
00823       if(!fFeeMap->IsConnected()){
00824         if(ConnectToDB() < 0) return -1;  
00825       }
00826       if(FetchDBinfo(K_DB_SOURCE_HOST) != 0) TerminateFroot();
00827       DisconnectFromDB();
00828       fFinRdt->Reload();  // very important!
00829     }  
00830     // ---
00831     fIsNewRun = kFALSE;
00832     fCurRunType = DecodeRunType(fRawEvent[3]);
00833     fCurRunNum = fRawEvent[4];
00834     Info("ReadRawEvent","New run operations succesfully performed for run %u",fCurRunNum);
00835     if(fDebug) Info("ReadRawEvent","New run set to false");
00836     if(fOnlineFlag == ONL_ALL){
00837       Info("ReadRawEvent","\n\n\n");
00838       Info("ReadRawEvent"," -----------------------------------");
00839       Info("ReadRawEvent","   ---- NEW RUN started (%s %u)",fCurRunType.Data(),fCurRunNum);
00840       Info("ReadRawEvent"," -----------------------------------\n\n\n");
00841       // Histograms reset can be added if needed
00842       fFinHdt->Reset(); // created once and then recycled
00843       fFinHdt->SetEventProperties(fRawEvent[4],fRawEvent[2]);     
00844       return 0;
00845     }
00846   }
00847   
00848   if(!fFinRdt) TerminateFroot(0,"TFndRun::ReadRawEvent ---> fFinRdt not present!");
00849   
00850   fFinHdt->Reset(); // created once and then recycled
00851   fFinHdt->SetEventProperties(fRawEvent[4],fRawEvent[2]);
00852 
00853   if(fDebug) Info("ReadRawEvent", "mapping RDT to HDT");
00854   if(!using_db) return 0;
00855   
00856   if(using_db){  
00857     //    Printf("fFinHdt in TFndRun: 0x%x",fFinHdt);
00858     fFinRdt->RdtToHdt(fRawEvent,0);
00859     fFinHdt->EvalHits();
00860     FillMonitors();
00861   }
00862   
00863   if(fDebug) Info("ReadRawEvent", "exiting method");
00864   return 0;
00865 }
00866 
00867 //___________________________________________
00868 Int_t TFndRun::GetNextEvent(Bool_t fill_hdt){
00869   // This method is used by the online monitor only
00870   //
00871   // return values:
00872   //                 -2: No answer received from DAQ (online mode only)
00873   //                 -1: DB-error
00874   //                  0: good event: go on
00875   //                  1: requested number of events reached
00876   //                  2: end of run reached
00877   
00878   if(fDebug) Info("GetNextEvent", "entering method");
00879   Int_t istop = 0;
00880 
00881   if(fDebug) cerr << "getting new event; last processed event: " << fNumOfProcessedEvents << endl;
00882   // // remember to call ClearEvent() when you are using directly "GetNextRawEvent"
00883   // --------------------------------------------------------
00884   if(fRawEvent && fRawEvent[0]!=0){
00885     Warning("GetNextEvent","You forgot to call TFndRun::ClearEvent! forcing Clear operation (ev: %u).",GetEvNum_Raw());
00886     ClearEvent();
00887   }
00888   GetNextRawEvent(istop);
00889   if(istop) return istop;
00890   // --------------------------------------------------  
00891   if(fDebug) cerr << "event " << GetEvNum_Raw() << " got" << " (fNumOfProcessedEvents: " << fNumOfProcessedEvents << endl;
00892 
00893   if(fill_hdt) istop = ReadRawEvent();
00894 
00895   if(fDebug) cerr << "event " << fNumOfProcessedEvents  << " completed" << endl;
00896   return istop;
00897 }
00898 
00899 //_____________________________
00900 Int_t TFndRun::ReconstructEvent(){
00901   // Custom reconstruction
00902   // of the current raw event (fRawEvent)
00903 
00904   Info("ReconstructEvent", "Global Event address        : %p", fRawEvent);
00905   Info("ReconstructEvent", "Global Event Length         : %u", fRawEvent[0]);
00906   Info("ReconstructEvent", "Raw event number (hardware) : %u", fRawEvent[1]);
00907   Info("ReconstructEvent", "Raw event number (software) : %u", fRawEvent[2]);
00908 
00909   return 0;
00910 }
00911 
00912 //___________________________________________
00913 void TFndRun::StartMonitors(){
00914   // Start all TFnd*Mon in Producer mode ---- check ---
00915   // please do not change the order!
00916   
00917   Bool_t at_least_one = kFALSE;
00918   for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) at_least_one = at_least_one || fMustFillMonitor[i];
00919   if(!at_least_one){
00920     Info("StartMonitors","No monitor will start (histograms)");
00921     return;
00922   }
00923   
00924   if( !fRunFilename.IsNull() ){
00925     fMonHisFilename=fRunFilename;
00926     TString def_pth= ExpandPathName("$FND_MON_HISTOS");
00927     if( !def_pth.IsNull() && gSystem->AccessPathName(def_pth)==0 ) fMonHisFilename.Form("%s/%s",def_pth.Data(),fRunFilename.Data());
00928   }
00929   
00930   if( fMonHisFilename.IsNull() )  Info("StartMonitors", "Start Monitors in online mode");
00931   else Info("StartMonitors", "Start Monitors with fMonHisFilename: %s", fMonHisFilename.Data());
00932 
00933   if(fMustFillMonitor[(Int_t)D_GTS] ){
00934     Info("StartMonitors","Starting GTS");
00935     fGtsMon = new TFndGtsMon(fMonHisFilename.Data());
00936   }
00937 
00938   if(fMustFillMonitor[(Int_t)D_TOF] ){
00939     Info("StartMonitors","Starting TOF");
00940     StartTofMonitor();
00941   }
00942 
00943   if(fMustFillMonitor[(Int_t)D_SIL] ){
00944     Info("StartMonitors","Starting ISIM");
00945     StartIsmMonitor();
00946     
00947     Info("StartMonitors","Starting OSIM");
00948     StartOsmMonitor();
00949   }
00950   
00951   if(fMustFillMonitor[(Int_t)D_LMD] ){
00952     Info("StartMonitors","Starting LMD");
00953     StartLmdMonitor();
00954   }
00955   
00956   if(fMustFillMonitor[(Int_t)D_STB] ){  
00957     Info("StartMonitors","Starting STB");
00958     StartStbMonitor();
00959   }
00960   
00961   Info("StartMonitors","all monitors started ");
00962   return;
00963 }
00964 //___________________________________________
00965 void TFndRun::StartGtsMonitor(){
00966   // Start TFndGtsMon in Producer mode
00967 
00968   fGtsMon = new TFndGtsMon(fMonHisFilename.Data());
00969   return;
00970 }
00971 
00972 //___________________________________________
00973 void TFndRun::StartTofMonitor(Int_t adc_bins,Float_t adc_min,Float_t adc_max,Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
00974   // Start TFndTofMon in Producer mode
00975   if(!fRunConfiguration){
00976     Warning("StartTofMonitor","Run Configuration file not loaded: can not start monitor");
00977     return;
00978   }
00979   if(adc_bins > K_MAX_BINS_ADC || adc_bins <= 0) fRunConfiguration->GetAdcRange(D_TOF,adc_bins,adc_min,adc_max);
00980   if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_TOF,tdc_bins,tdc_min,tdc_max);
00981 
00982   fTofMon = new TFndTofMon(fMonHisFilename.Data(),adc_bins,adc_min,adc_max,tdc_bins,tdc_min,tdc_max);
00983   
00984   return;
00985     
00986 }
00987 
00988 //___________________________________________
00989 void TFndRun::StartIsmMonitor(){
00990 
00991   fIsmMon = new TFndSilMon(fMonHisFilename.Data(),1);
00992   return;
00993 }
00994 
00995 //___________________________________________
00996 void TFndRun::StartOsmMonitor(){
00997 
00998   fOsmMon = new TFndSilMon(fMonHisFilename.Data(),2);
00999   return;
01000 }
01001 
01002 //___________________________________________
01003 void TFndRun::StartLmdMonitor(Int_t adc_bins,Float_t adc_min,Float_t adc_max,Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
01004   // Start TFndLmdMon in Producer mode
01005   if(!fRunConfiguration){
01006     Warning("StartLmdMonitor","Run Configuration file not loaded: can not start monitor");
01007     return;
01008   }
01009   if(adc_bins > K_MAX_BINS_ADC || adc_bins <= 0) fRunConfiguration->GetAdcRange(D_LMD,adc_bins,adc_min,adc_max);
01010   if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_LMD,tdc_bins,tdc_min,tdc_max);
01011   fLmdMon = new TFndLmdMon(fMonHisFilename.Data(),adc_bins,adc_min,adc_max,tdc_bins,tdc_min,tdc_max);
01012   return;
01013 }
01014 
01015 //___________________________________________
01016 void TFndRun::StartStbMonitor(Int_t tdc_bins,Float_t tdc_min,Float_t tdc_max){
01017   // Start TFndStbMon in Producer mode
01018   if(!fRunConfiguration){
01019     Warning("StartStbMonitor","Run Configuration file not loaded: can not start monitor");
01020     return;
01021   }
01022   if(tdc_bins > K_MAX_BINS_TDC || tdc_bins <= 0) fRunConfiguration->GetTdcRange(D_STB,tdc_bins,tdc_min,tdc_max);
01023   
01024   fStbMon = new TFndStbMon(fMonHisFilename.Data(),tdc_bins,tdc_min,tdc_max);
01025   return;
01026 }
01027 
01028 //___________________________________________
01029 void TFndRun::StartGtsMonitor(TString *str){
01030   // Start TFndGtsMon in read mode:
01031   // from a ".root" file (previously produced)
01032 
01033   if(!fGtsMon) {
01034     if(fOnlineFlag == ONL_OFF) {
01035       Info("StartGtsMonitor", "Start Gts monitor in offline mode.");
01036       fGtsMon = new TFndGtsMon(str);
01037     }
01038     else {
01039       Info("StartGtsMonitor", "Start Gts monitor in online mode.");
01040       fGtsMon = new TFndGtsMon();    
01041     }
01042   }
01043   return;
01044 }
01045 
01046 //___________________________________________
01047 void TFndRun::StartTofMonitor(TString *str){
01048   // Start TFndTofMon in read mode:
01049   // from a ".root" file (previously produced)
01050 
01051   if(!fTofMon) {
01052     if(fOnlineFlag == ONL_OFF) {
01053       Info("StartTofMonitor", "Start Tof monitor in offline mode");
01054       fTofMon = new TFndTofMon(str);
01055     } 
01056     else {
01057       Info("StartTofMonitor", "Start Tof monitor in online mode");
01058       fTofMon = new TFndTofMon();    
01059     }
01060   }
01061   return;
01062 }
01063 
01064 //___________________________________________
01065 void TFndRun::StartIsmMonitor(TString *str){
01066   // Start TFndIsmMon in read mode:
01067   // from a ".root" file (previously produced)
01068 
01069   if(!fIsmMon) {
01070     if(fOnlineFlag == ONL_OFF){
01071       Info("StartIsmMonitor", "Start Ism monitor in offline mode");
01072       fIsmMon = new TFndSilMon(str->Data(),1);
01073     }
01074     else {
01075       Info("StartIsmMonitor", "Start Ism monitor in online mode");
01076       fIsmMon = new TFndSilMon();
01077     }
01078   }
01079   return;
01080 }
01081 
01082 //___________________________________________
01083 void TFndRun::StartOsmMonitor(TString *str){
01084   // Start TFndOsmMon in read mode:
01085   // from a ".root" file (previously produced)
01086   
01087   if(!fOsmMon) {
01088     if(fOnlineFlag == ONL_OFF){
01089       Info("StartOsmMonitor", "Start Osm monitor in offline mode");
01090       fOsmMon = new TFndSilMon(str->Data(),2);
01091     }
01092     else {
01093       Info("StartOsmMonitor", "Start Osm monitor in online mode");
01094       fOsmMon = new TFndSilMon();
01095     }
01096   }
01097   return;
01098 }
01099 
01100 //___________________________________________
01101 void TFndRun::StartLmdMonitor(TString *str){
01102   // Start TFndLmdMon in read mode:
01103   // from a ".root" file (previously produced)
01104 
01105   if(!fLmdMon) {
01106     if(fOnlineFlag == ONL_OFF) {
01107       Info("StartLmdMonitor", "Start Lmd monitor in offline mode");
01108       fLmdMon = new TFndLmdMon(str);
01109     } 
01110     else {
01111       Info("StartLmdMonitor", "Start Lmd monitor in online mode");
01112       fLmdMon = new TFndLmdMon();
01113     }
01114   }
01115   return;
01116 }
01117 
01118 //___________________________________________
01119 void TFndRun::StartStbMonitor(TString *str){
01120   // Start TFndStbMon in read mode:
01121   // from a ".root" file (previously produced)
01122 
01123   if(!fStbMon) {
01124     if(fOnlineFlag == ONL_OFF) {
01125       Info("StartStbMonitor", "Start Stb monitor in offline mode");
01126       fStbMon = new TFndStbMon(str);
01127     } 
01128     else {
01129       Info("StartStbMonitor", "Start Stb monitor in online mode");
01130       fStbMon = new TFndStbMon();
01131     }
01132   }
01133   return;
01134 }
01135 
01136 //___________________________________________
01137 Int_t TFndRun::StartMonFrame(Int_t det,TString RootFileName,TString RootFileName2){
01138   // Start the monitor-frame (consumer-GUI) for the selected detector
01139   //    (see FROOT:: EFndMonDetectors)
01140   //
01141   // If RootFileName and RootFileName_2 is not passed, then online-mode is selected
01142   //   (RootFileName_2 is used for SIL only)
01143   // 
01144   // return value:
01145   //               0 : ok
01146   //               1 : deleting old one
01147   //              -1 : detector selection error
01148 
01149   Int_t ret_val = 0;
01150 
01151   if(GetMonFrame(det)){
01152     DeleteMonFrame(det);
01153     ret_val = 1;
01154   }
01155 
01156   switch(det){
01157   case D_GTS:
01158     fGtsMonFrame = new TFndGtsMonFrame(RootFileName);
01159     fGtsMonFrame->Open();
01160     break; 
01161   case D_TOF: 
01162     fTofMonFrame = new TFndTofMonFrame(RootFileName);
01163     fTofMonFrame->Open();
01164     break; 
01165   case D_SIL: 
01166     fSilMonFrame = new TFndSilMonFrame(RootFileName,RootFileName2);
01167     fSilMonFrame->Open();
01168     break; 
01169   case D_LMD:
01170     fLmdMonFrame = new TFndLmdMonFrame(RootFileName);
01171     fLmdMonFrame->Open();
01172     break; 
01173   case D_STB:
01174     fStbMonFrame = new TFndStbMonFrame(RootFileName);
01175     fStbMonFrame->Open();
01176     break; 
01177   default:
01178     Error("StartMonFrame","det \"%d\" not accepted",det);
01179     return -1;
01180   }
01181   return ret_val;
01182 }
01183 
01184 //___________________________________________
01185 TFndMonFrame *TFndRun::GetMonFrame(Int_t det){
01186   // Get current monitor-frame (consumer-GUI) for the selected detector (det)
01187   //
01188   // Remember to use casting to the needed class:
01189   //    (TFndGtsMonFrame *)fnrun->GetMonFrame((Int_t)(D_GTS))
01190   //    (TFndTofMonFrame *)fnrun->GetMonFrame((Int_t)(D_TOF))
01191   //    (TFndSilMonFrame *)fnrun->GetMonFrame((Int_t)(D_SIL))
01192   //    (TFndLmdMonFrame *)fnrun->GetMonFrame((Int_t)(D_LMD))
01193   //    (TFndStbMonFrame *)fnrun->GetMonFrame((Int_t)(D_STB))
01194 
01195   switch(det){
01196   case D_GTS: return (TFndMonFrame *) fGtsMonFrame; 
01197   case D_TOF: return (TFndMonFrame *) fTofMonFrame; 
01198   case D_SIL: return (TFndMonFrame *) fSilMonFrame; 
01199   case D_LMD: return (TFndMonFrame *) fLmdMonFrame; 
01200   case D_STB: return (TFndMonFrame *) fStbMonFrame; 
01201   default:
01202     Error("StartMonFrame","det \"%d\" not accepted",det);
01203     return 0;
01204   }
01205   return 0; // should never reach this line!
01206 }
01207 
01208 //___________________________________________
01209 Int_t TFndRun::DeleteMonFrame(Int_t det){
01210   // Delete current monitor-frame (consumer-GUI) for the selected detector
01211   //    (see FROOT:: EFndMonDetectors)
01212   // 
01213   // return value:
01214   //               0 : ok
01215   //              -1 : detector selection error
01216   
01217   switch(det){
01218   case D_GTS: delete fGtsMonFrame; fGtsMonFrame = 0;  break; 
01219   case D_TOF: delete fTofMonFrame; fTofMonFrame = 0;  break; 
01220   case D_SIL: delete fSilMonFrame; fSilMonFrame = 0;  break; 
01221   case D_LMD: delete fLmdMonFrame; fLmdMonFrame = 0;  break; 
01222   case D_STB: delete fStbMonFrame; fStbMonFrame = 0;  break; 
01223   default:
01224     Error("StartMonFrame","det \"%d\" not accepted",det);
01225     return -1;
01226   }
01227   return 0;
01228 }
01229 
01230 //___________________________________________
01231 void TFndRun::FillMonitors(){
01232   // Fill producer histograms
01233 
01234   if(fDebug > 0) Info("FillMonitors", "entering method");
01235   if(fGtsMon) fGtsMon->Fill();
01236   if(fTofMon) fTofMon->Fill();
01237   if(fIsmMon) fIsmMon->Fill();
01238   if(fOsmMon) fOsmMon->Fill();
01239   if(fLmdMon) fLmdMon->Fill();
01240   if(fStbMon) fStbMon->Fill();
01241   if(fDebug > 0) Info("FillMonitors", "exiting method");
01242   return;
01243 }
01244 
01245 //___________________________________________
01246 void TFndRun::WriteMonitors(Bool_t close){
01247   // Write producer histograms to the relative root-files
01248   // (see "TFnd*Mon" classes)
01249   
01250   Info("WriteMonitors", "Writing Histograms To file (close: %d)",close);
01251   if(fGtsMon) close ? fGtsMon->WriteToFile() : fGtsMon->Update();
01252   //  cout << "DEBUG 1" << endl;
01253   if(fTofMon) close ? fTofMon->WriteToFile() : fTofMon->Update();
01254   //  cout << "DEBUG 2" << endl;
01255   if(fIsmMon) close ? fIsmMon->WriteToFile() : fIsmMon->Update();
01256   //  cout << "DEBUG 3" << endl;
01257   if(fOsmMon) close ? fOsmMon->WriteToFile() : fOsmMon->Update();
01258   //  cout << "DEBUG 4" << endl;
01259   if(fLmdMon) close ? fLmdMon->WriteToFile() : fLmdMon->Update();
01260   //  cout << "DEBUG 5" << endl;
01261   if(fStbMon) close ? fStbMon->WriteToFile() : fStbMon->Update();
01262   Info("WriteMonitors", "done");
01263   return;
01264 }
01265 
01266 //_______________________________________________________________________
01267 void TFndRun::ReadConfigFile(TString ConfFileName){
01268   // calling this method will change the value of fDebug.
01269 
01270   TString ConfFuncName = "RunConfig()";
01271   
01272   if(gSystem->AccessPathName(ConfFileName)){
01273     Warning("ReadConfigFile", "file \"%s\" not found.", ConfFileName.Data());
01274     Warning("ReadConfigFile", "Configuration file \"%s\" not found in current directory: using defaults.", ConfFileName.Data());
01275     ConfFileName = FROOT::ExpandPathName("$FROOTSYS/mcr_called/RunConfig.C");
01276     Info("ReadConfigFile","The default configuration file is \"%s\"",(Char_t *)ConfFileName.Data());
01277   }
01278   if(gSystem->AccessPathName(ConfFileName)){
01279     Error("ReadConfigFile","Default configuration file \"%s\" not found! exiting program.",(Char_t *)ConfFileName.Data());
01280     TerminateFroot();
01281   }
01282   
01283   if(fDebug > 1) Info("ReadConfigFile", "Loading Macro %s", ConfFileName.Data());
01284   gROOT->LoadMacro(ConfFileName);
01285   if(fDebug > 1) Info("ReadConfigFile", "Loading configuration method \"%s\"", ConfFileName.Data());
01286   Info("ReadConfigFile", " Loading \"%s\"", (Char_t *) (ConfFileName.Data()));
01287   printf("TFndRun::ReadConfigFile Info:  Loading \"%s\"\n", (Char_t *) (ConfFileName.Data()));
01288   gInterpreter->ProcessLine(ConfFuncName);
01289 
01290   fDebug = fRunConfiguration->Debug();
01291   fDebugRawEv = fRunConfiguration->DebugRaw();
01292 
01293   for(Int_t i= (Int_t)D_GTS; i<= (Int_t)D_STB; i++) fMustFillMonitor[i] = fRunConfiguration->GetMustFillMonitor(i);
01294 
01295   //  cout << " ====================> " << fRunConfiguration->Debug() << " ; " << fDebug << endl;
01296   return;
01297 }
01298 //_______________________________________________________________________
01299 Long_t TFndRun::OpenRawFile(TString fdir, TString fnam,Bool_t check_only){
01300   // Open raw file (or gzipped raw file)
01301   // and set the fRawFile pointer
01302   // return value: file size (-1 in case of problems)
01303   // if the "check_only" argument is true, then the file will not be open
01304 
01305   Bool_t is_zipped = kFALSE;  
01306 
01307   TString gzip  = TString("gzip -cd ");
01308   TString sufx  = TString(".gz");
01309   TString fname = FROOT::ExpandPathName(fdir);
01310   fname+="/";
01311   fname+=fnam;
01312   fname+=".raw";
01313   // -- file existence check
01314   //   if(!CheckFileExistence(fname)){
01315   //     if(fDebug > 0) Warning("OpenRawFile", "File not found");
01316   //     return -1;
01317   //   }
01318   // file existence check completed ---
01319   
01320   if(!check_only){ 
01321     // performed by default 
01322     gzip+=fname.Data();
01323     gzip+=sufx.Data();
01324     if(fDebug > 0) cout << "TFndRun::OpenRawFile Info: Opening " << fname.Data() << endl;
01325     fRawFile = fopen(fname.Data(),"rb");
01326     if (fRawFile==NULL) {
01327       if(fDebug) cout << "TFndRun::OpenRawFile Info: Opening zipped file" << endl;
01328       is_zipped = kTRUE;
01329       fRawFile = popen(gzip.Data(),"r");
01330       if (fRawFile==NULL) {   
01331         printf("TFndRun::OpenRawFile Warning: file %s does not exist\n", fname.Data());
01332         return -1;
01333       }
01334     }
01335     //
01336     TString msg_sf = is_zipped ? ".gz" : "";
01337       // fname+=sufx.Data();
01338     if(fDebug > 1 && !check_only) printf("TFndRun::OpenRawFile Info: file %s%s opened.\n", fname.Data(), msg_sf.Data());
01339   }
01340   
01341 
01342   // --- last check: file size (File stats check)
01343   // Get info about a file: id, size, flags, modification time.
01344   is_zipped = kFALSE;
01345   Long_t id = 0;
01346   Long_t size = 0;
01347   Long_t flags = 0;
01348   Long_t modtime = 0;
01349   Int_t InfoRes = -1; // zero means "success"
01350   
01351   InfoRes = gSystem->GetPathInfo(fname.Data(),&id,&size,&flags,&modtime);
01352   if(InfoRes){
01353     is_zipped = kTRUE;
01354     fname+=sufx;
01355     InfoRes = gSystem->GetPathInfo(fname.Data(),&id,&size,&flags,&modtime);
01356   }
01357   //
01358   if(!InfoRes){
01359     if(fDebug > 1)
01360       printf("TFndRun::OpenRawFile Info: file size: %d (zipped: %d)\n", size, is_zipped);
01361     if(fDebug > 2)
01362       printf("TFndRun::OpenRawFile Info: name: \"%s\"; id: %ld; size: %ld; flags: %ld; mod time\n", 
01363              fname.Data(), id, size, flags, modtime);
01364     
01365     return size;
01366   }
01367   else{
01368     printf("TFndRun::OpenRawFile Info: can not stat file \"%s\"\n",fname.Data());
01369     return -1;
01370   }
01371 }
01372 
01374 
01375 //_______________________________________________________________________
01376 void TFndRun::OpenLogFile(const TString &fdir, const TString &fnam){
01377 
01378   CloseLogFile();
01379 
01380   TString path_str;
01381   path_str.Form("%s/%s",ExpandPathName(fdir).Data(),fnam.Data());
01382 
01383   fCurLogFile = new ofstream();
01384 
01385   fCurLogFile->open(path_str.Data(),ofstream::out | ofstream::app);
01386 
01387 }
01388 
01389 //_______________________________________________________________________
01390 void TFndRun::CloseLogFile(){
01391 
01392   if(!fCurLogFile) return;
01393   fCurLogFile->close();
01394   delete fCurLogFile;
01395   fCurLogFile = 0;
01396 }
01397 
01398 //_______________________________________________________________________
01399 void TFndRun::WriteLogFile(const TString &line){
01400 
01401   if(!fCurLogFile){
01402     
01403     return;
01404   }
01405   *fCurLogFile << line.Data() << endl;
01406 }
01407    

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