STEER/TFndDB.cxx

00001 // @(#)fROOT/STEER:$Name:  $:$Id: TFndDB.cxx,v 1.10 2007/09/24 07:32:41 Diego_Faso Exp $
00002 // Author: Diego Faso <mailto:faso@to.infn.it>, 2006/07/05
00003 
00005 //                                                                          //
00006 //                               TFndFeeDB                                  //
00007 //                                                                          //
00008 // Interface to the FINUDA official database.                               //
00009 //                                                                          //
00010 //                                                                          //
00011 //  The  fDBServ server-interface is used in order to establish the         //
00012 //   connection with the remote-or-local mysql database                     //
00013 //                                                                          //
00014 //  The complete content of the whole database can be loaded by calling the //
00015 //  "LoadAllInfo" method: in this case the whole content of the database is //
00016 //  fetched and stored into TTreeSQL objects (fSqlTree[]).                  //
00017 //  fSqlTree[] are available by calling "GetSqlTree(<EFndDbTables>)"        //
00018 //                                                                          //
00019 //  On the other hand, the selected information (according to the run-time) //
00020 //  is stored into data-members by the "FetchRunInfo" method.               //
00021 //  all these data-members are available ("Get<detector><spec>" methods)    //
00022 //                                                                          //
00023 //  Fetching the complete content of every table allows to manage every     //
00024 //  table within this class.                                                //
00025 //  The memory used to store fSqlTree[] can be released by calling the      //
00026 //  "UnloadAllInfo"
00027 //  after having called  DisconnectFromHost() only the selected information //
00028 //  will be available.                                                      //
00029 //                                                                          //
00030 //  While filling data-members with the selected info fCurRow is used.      //
00031 //                                                                          //
00033 
00034 #include "TThread.h"
00035 #include "TSQLTableInfo.h"
00036 #include "TSQLColumnInfo.h"
00037 
00038 #include "TFndRun.h"
00039 
00040 #include "TFndDB.h"
00041 
00042 ClassImp(TFndDB)
00043 
00044 //_______________________________________
00045 TFndDB::TFndDB():
00046   fSqlTree()
00047 {
00048 
00049 }
00050 
00051 //_______________________________________
00052 TFndDB::~TFndDB(){
00053 
00054   UnloadAllInfo(); // release used memory (if needed)
00055   DisconnectFromHost(); // prevent from user mistakes
00056 
00057   delete fCurRow;
00058   delete fCurRes;
00059   delete fDBServ;
00060 }
00061 
00062 //_______________________________________
00063 Int_t TFndDB::ConnectToHost(const TString &server,TString user,TString passwd,Int_t timeout,Int_t n_trials){
00064   // - timeout is the connection timeout in seconds
00065   // - n_trials is the number of times the connection will be tried
00066   //   before exiting with error (return -1)
00067   //
00068   // return value:
00069   //              0: ok
00070   //             -1: no connection to DB host (or DB not available)
00071   
00072   DisconnectFromHost(); // do not allow the user to open more than one connection at a time
00073   fIsThere = kFALSE;
00074   TString host_str;
00075   host_str.Form("mysql://%s/?timeout=%d:%s",server.Data(),timeout,gSystem->Getenv("MYSQL_TCP_PORT"));
00076   // TSQLServer::Connect("mysql://host.domain/test?timeout=10","user","passwd")
00077   
00078   for(Int_t nt=1;nt<=n_trials;nt++){
00079     fDBServ = (TSQLServer *) TSQLServer::Connect(host_str, user, passwd);
00080     if(fDBServ) break;
00081     else Warning("ConnectToHost","Connection to DB failed (timeout=%d): retrying (%d)",timeout,nt);
00082   }
00083   if(!fDBServ){
00084     Error("ConnectToHost","Database not available after %d trials: check connection to DB-host",n_trials);
00085     return -1;
00086   }
00087   return 0;
00088 }
00089 
00090 //_______________________________________
00091 void TFndDB::DisconnectFromHost(){
00092   // by calling this method the memory used for fetching
00093   // the complete content of all tables is automatically released:
00094   // all fSqlTree[] are deleted.
00095  
00096   if(fDBServ) if(fDBServ->IsConnected()) fDBServ->Close();
00097   delete fDBServ; fDBServ = 0;
00098 }
00099 
00100 //_______________________________________
00101 Int_t TFndDB::UnloadAllInfo(){
00102   
00103 
00104   for(Int_t i=0;i<K_N_DbTables;i++){
00105     delete fSqlTree[i];
00106     fSqlTree[i] = 0;
00107   }
00108 
00109 }
00110 
00111 //_______________________________________
00112 TSQLResult* TFndDB::Query(TString DB_name, TString QueryStr,Int_t print_mode){
00113   // return a TSQLResult pointer withe the query result
00114   //
00115 
00116   if(!fDBServ){
00117     Warning("Query","can not perform any query: DB server not available");
00118     return 0;
00119   }
00120 
00121   fDBServ->SelectDataBase(DB_name);
00122   fCurRes = (TSQLResult *) fDBServ->Query(QueryStr);
00123   if(!fCurRes){
00124     Error("Query","Failure while querying from DB (\"%s\")",DB_name.Data());
00125     return 0;
00126   }
00127 
00128   if(print_mode == (Int_t )Q_SILENT) return fCurRes;
00129   
00130   for(Int_t r=0;r<fCurRes->GetRowCount();r++){
00131     fCurRow = (TSQLRow *) fCurRes->Next();
00132     PrintCurRow(r,print_mode);
00133   }
00134 
00135   return fCurRes;
00136 }
00137 
00138 //_______________________________________
00139 void TFndDB::PrintCurRow(Int_t row_num,Int_t print_mode){
00140   //  { Q_SILENT = 0, Q_GENINFO, Q_COMPACT, Q_DETAILED, Q_MYSQL_STYLE };
00141   
00142   Int_t conv_type = -1;
00143 
00144   TString cur_row_str = "";
00145   for(Int_t f=0;f<fCurRes->GetFieldCount();f++){
00146     cur_row_str += fCurRes->GetFieldName(f);
00147     //    TSQLColumnInfo cinf = TSQLColumnInfo(fCurRow->GetField(f));
00148     //    Printf("Type[%d]: \"%d\"",f,cinf.GetSQLType() );
00149     //    switch(conv_type){
00150     //    case (Int_t) FINTYPE_INT:
00151     cur_row_str += (Int_t)TString(fCurRow->GetField(f)).Atoi();
00152     //       break;
00153     //     case (Int_t) FINTYPE_FLOAT:
00154     //    cur_row_str += (Float_t)TString(fCurRow->GetField(f)).Atof();
00155     //       break;
00156     //     case (Int_t) FINTYPE_CHAR:
00157     //       cur_row_str += (Char_t *)TString(fCurRow->GetField(f)).Data();
00158     //       break;
00159     
00160     //    }
00161     cur_row_str += "; ";
00162   }
00163   cur_row_str += ".";
00164   Printf("row %d: %s",row_num,cur_row_str.Data());
00165 
00166 }
00167 
00168 //_______________________________________
00169 Int_t TFndDB::FetchRunInfo(Bool_t verbose,UInt_t run_time,Bool_t also_calib){
00170   // The selected information (according to the run-time)
00171   // is stored into standard arrays (saved to HDT root-file)
00172   //
00173   // If fndrun is not available or the run-time
00174   // is not defined, then:
00175   //           - TofDB2 is selected by default
00176   //           - No selection will be performed on tables content
00177   //           - fIsThere = kFALSE;
00178   //
00179   // The run-time can be passed as argument directly to this method:
00180   //  in this case fndrun->GetRunTime will not be considered
00181   //  (this is for people who needs to inspect the database content)
00182   //
00183   // return value:
00184   //               0: fetching performed succesfully
00185   //              -1: no interface loaded
00186   //              -2: run-time not defined (no selected information)
00187 
00188   if(!fDBServ){
00189     Warning("FetchRunInfo","No interface to DB server loaded");
00190     return -1;
00191   } 
00192   
00193   // Get run time from argument or update local reference to the time of the run
00194   //  then select the correct table for TOF
00195   UInt_t RunTime = run_time;
00196   if(RunTime == 0){
00197     if(fndrun) RunTime = fndrun->GetRunTime();
00198     else {
00199       Error("FetchRunInfo","fndrun not defined: RunTime will not be available");
00200       return -2;
00201     }
00202   }
00203   
00204   //   if(RunTime == 0){
00205   //     Error("FetchRunInfo","RunTime not defined: selected information not available");
00206   //     return -2;
00207   //   }
00208   
00209   // FETCH HERE !!!
00210   Int_t fetch_err = 0;
00211  
00212   fetch_err += FetchTofRunInfo(verbose,RunTime);
00213   fetch_err += FetchSilRunInfo(verbose,RunTime);
00214   fetch_err += FetchLmdRunInfo(verbose,RunTime);
00215   fetch_err += FetchStbRunInfo(verbose,RunTime);
00216 
00217   if(also_calib){
00218     // --- CALIBRATION
00219     fetch_err += FetchTofCalibInfo(verbose);
00220     fetch_err += FetchLmdCalibInfo(verbose);
00221     fetch_err += FetchStbCalibInfo(verbose);
00222   }
00223   
00224 
00225   fIsThere = kTRUE;
00226   return fetch_err;
00227 }
00228 
00229 //_______________________________________
00230 Int_t TFndDB::FetchTofRunInfo(Bool_t verbose,const Int_t &run_time){
00231   // return value:
00232   //              0: ok
00233   //             -1: failure while querying
00234   //
00235   //  sprintf(qry,"SELECT w,ap,ae,tp,te,tm,q0p,q0e,q1p,q1e,t0p,t0e,t0m,cnp,cne,cnm FROM fee %s ORDER BY w",where);
00236   //  sprintf(qry,"SELECT w,A,Z,VER,offtime FROM offset %s AND VER= %d ORDER BY w",where,offsetver_.ofstver);
00237   
00238   Int_t rows = 0; // used for fetching rows
00239   
00240   // SELECT ACCORDING TO THE RUN-TIME
00241   TString TofDB_str = "TofDB";
00242   if(run_time > FROOT::DTak_2006_SftwStartTime) TofDB_str = "TofDB2";
00243   if(verbose) Printf("TFndDB::FetchTofRunInfo ---> Selected DB for TOF is \"%s\"",TofDB_str.Data());
00244   
00245   
00246   fDBServ->SelectDataBase(TofDB_str);
00247   TString QueryStr;
00248   
00249   // --- FEE info
00250   QueryStr.Form("select w, ap, ae, tp, te, tm from fee where A< %d and Z> %d order by w",run_time,run_time);
00251   Printf("Query string: \"%s\"",QueryStr.Data());
00252   fCurRes = (TSQLResult *) fDBServ->Query(QueryStr);
00253   
00254   if(!fCurRes){
00255     Error("FetchTofRunInfo","Failure while querying from table \"fee\" (\"%s\")",TofDB_str.Data());
00256     return -1;
00257   }
00258 
00259   if(verbose){
00260     Printf("Number of selected rows: %d",fCurRes->GetRowCount());
00261     Printf("Number of fields / row : %d",fCurRes->GetFieldCount());
00262   }
00263   //
00264   fCurRow = 0;
00265   
00266   Int_t imodule_adc = 0;
00267   Int_t ichannel_adc = 0;
00268   Int_t imodule_tdc = 0;
00269   Int_t ichannel_tdc = 0;
00270   Int_t imodule_mt = 0;
00271   Int_t ichannel_mt = 0;
00272 
00273   // Fetch row-by-row (use ROOT iterator "Next()")
00274   for(Int_t r=0;r<fCurRes->GetRowCount();r++){
00275     fCurRow = (TSQLRow *) fCurRes->Next();
00276     if(verbose) PrintCurRow(r,(Int_t)Q_COMPACT);
00277     // fetch field-by-field from current raw
00278     Int_t islab = TString(fCurRow->GetField(0)).Atoi();
00279 
00280   }
00281   fCurRes = 0;  
00282   return 0;
00283 }
00284 
00285 //_______________________________________
00286 Int_t TFndDB::FetchSilRunInfo(Bool_t verbose,Int_t run_time){
00287 
00288 }
00289 
00290 //_______________________________________
00291 Int_t TFndDB::FetchLmdRunInfo(Bool_t verbose,Int_t run_time){
00292 
00293 }
00294 
00295 //_______________________________________
00296 Int_t TFndDB::FetchStbRunInfo(Bool_t verbose,Int_t run_time){
00297 
00298 }
00299 
00300 //_______________________________________
00301 Int_t TFndDB::LoadAllInfo(Bool_t verbose,TString TofDB_str){
00302   // The complete (not selected) information
00303   // is stored (but not saved) into fSqlTree[]
00304   //
00305   // Concernong the TOF database:
00306   //   TofDB2 is the dafault:
00307   //   if you want to load TofDB you must pass "TofDB" as second argument
00308   //
00309   // return value:
00310   //               0: fetching and loading performed succesfully
00311   //              -1: no interface loaded
00312   //              -2: Error while getting  (no selected information)
00313 
00314   if(!fDBServ){
00315     Warning("LoadAllInfo","No interface to DB server loaded");
00316     return -1;
00317   } 
00318 
00319   // (FETCH and then CHECK)
00320   if(verbose) Printf("TFndDB::LoadAllInfo ---> Selected DB for TOF is \"%s\"",TofDB_str.Data());
00321   delete fSqlTree[DBTAB_TOF_FEE];    fSqlTree[DBTAB_TOF_FEE]    =  new  TTreeSQL(fDBServ,TofDB_str,"fee");
00322   delete fSqlTree[DBTAB_TOF_CALIB];  fSqlTree[DBTAB_TOF_CALIB]  =  new  TTreeSQL(fDBServ,TofDB_str,"calib");
00323   delete fSqlTree[DBTAB_TOF_OFFSET]; fSqlTree[DBTAB_TOF_OFFSET] =  new  TTreeSQL(fDBServ,TofDB_str,"offset");
00324   delete fSqlTree[DBTAB_SIL_FEE];    fSqlTree[DBTAB_SIL_FEE]    =  new  TTreeSQL(fDBServ,"SilDB","fee");
00325   delete fSqlTree[DBTAB_LMD_FEE];    fSqlTree[DBTAB_LMD_FEE]    =  new  TTreeSQL(fDBServ,"LmdDB","fee");
00326   delete fSqlTree[DBTAB_STB_FEE];    fSqlTree[DBTAB_STB_FEE]    =  new  TTreeSQL(fDBServ,"StbDB","fee");
00327   
00328   Bool_t AllLoaded = kTRUE;
00329   for(Int_t i=0;i<K_N_DbTables;i++){
00330     if(i!=DBTAB_TOF_CALIB) AllLoaded = AllLoaded && ((Bool_t)(fSqlTree[i]->GetEntries()));
00331     if(verbose) Printf("             entries of table[%d] (see TFndDB::EFndDbTables): %d",i,fSqlTree[i]->GetEntries());
00332   }
00333   if(TofDB_str.CompareTo("TofDB2") ==0) AllLoaded && ((Bool_t)(fSqlTree[DBTAB_TOF_CALIB]->GetEntries()));
00334   
00335   if(!AllLoaded){
00336     Error("LoadAllInfo","Errors while fetching information from database");
00337     return -2;
00338   }
00339   return 0;
00340 }
00341 
00343 
00344 //_______________________________________
00345 Int_t TFndDB::FetchTofCalibInfo(const Bool_t &verbose){
00346 
00347   if(verbose) Printf("TFndDB::FetchTofCalibInfo ---> entering method");
00348 
00349   // ---
00350   if(verbose) Printf("TFndDB::FetchTofCalibInfo ---> exiting method");
00351   return 0;
00352 }
00353 
00354 
00355 //_______________________________________
00356 Int_t TFndDB::FetchLmdCalibInfo(const Bool_t &verbose){
00357 
00358   if(verbose) Printf("TFndDB::FetchLmdCalibInfo ---> entering method");
00359 
00360   // ---
00361   if(verbose) Printf("TFndDB::FetchLmdCalibInfo ---> exiting method");
00362   return 0;  
00363 }
00364 
00365 //_______________________________________
00366 Int_t TFndDB::FetchStbCalibInfo(const Bool_t &verbose){
00367   
00368   if(verbose) Printf("TFndDB::FetchStbCalibInfo ---> entering method");
00369 
00370   // ---
00371   if(verbose) Printf("TFndDB::FetchStbCalibInfo ---> exiting method");
00372   return 0;
00373 }

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