00001
00002
00003
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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();
00055 DisconnectFromHost();
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
00065
00066
00067
00068
00069
00070
00071
00072 DisconnectFromHost();
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
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
00093
00094
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
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
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
00148
00149
00150
00151 cur_row_str += (Int_t)TString(fCurRow->GetField(f)).Atoi();
00152
00153
00154
00155
00156
00157
00158
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
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188 if(!fDBServ){
00189 Warning("FetchRunInfo","No interface to DB server loaded");
00190 return -1;
00191 }
00192
00193
00194
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
00205
00206
00207
00208
00209
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
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
00232
00233
00234
00235
00236
00237
00238 Int_t rows = 0;
00239
00240
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
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
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
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
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314 if(!fDBServ){
00315 Warning("LoadAllInfo","No interface to DB server loaded");
00316 return -1;
00317 }
00318
00319
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 }