--- YodaProfiler/src/GLTables.cpp 2006/10/31 15:36:04 1.15 +++ YodaProfiler/src/GLTables.cpp 2007/11/26 07:58:00 1.36 @@ -10,23 +10,133 @@ // #include #include +#include #include #include // #include #include // +ClassImp(GL_TABLES); ClassImp(GL_TRK_CALIB); ClassImp(GL_RUN); ClassImp(GL_ROOT); ClassImp(GL_PARAM); ClassImp(GL_S4_CALIB); ClassImp(GL_CALO_CALIB); +ClassImp(GL_CALOPULSE_CALIB); ClassImp(GL_TIMESYNC); ClassImp(GL_TLE); // using namespace std; +GL_TABLES::GL_TABLES(){ +}; + +GL_TABLES::GL_TABLES(TString host, TString user, TString psw){ + fHost = new TString(host.Data()); + fUser = new TString(user.Data()); + fPsw = new TString(psw.Data()); + fSet = true; + fNquery = 0; + mh = host.Data(); + mu = user.Data(); + mp = psw.Data(); +}; + + +void GL_TABLES::Set(TString host, TString user, TString psw){ + fHost = new TString(host.Data()); + fUser = new TString(user.Data()); + fPsw = new TString(psw.Data()); + fSet = true; + fNquery = 0; + mh = host.Data(); + mu = user.Data(); + mp = psw.Data(); +}; + +Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){ + // + // + // + if ( !fSet ){ + return true; + }; + // + // printf(" host is %s \n",fHost->Data()); + // + stringstream myquery; + myquery.str(""); + myquery << "show databases;"; + if ( dbc ){ + if ( dbc->IsConnected() ){ + dbc->Query(myquery.str().c_str()); + fNquery++; + if ( !(dbc->GetErrorCode()) ){ + // printf("ok\n"); + // + // if ( (dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006) ){ + // if ( !(dbc->GetErrorCode()) ){ + // is connected + return true; + // }; + }; + }; + }; + // + if ( true ) { + // + printf(" WARNING: Lost connection to DB! try to recover... \n"); + // + TString host = fHost->Data(); + TString user = fUser->Data(); + TString psw = fPsw->Data(); + dbc->Close(); + delete dbc; + dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); + // + myquery.str(""); + myquery << "show databases;"; + dbc->Query(myquery.str().c_str()); + fNquery++; + // if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){ + if ( !(dbc->GetErrorCode()) ){ + // + printf(" ...connection recovered, I can continue! \n"); + // + myquery.str(""); + myquery << "SET time_zone='+0:00'"; + dbc->Query(myquery.str().c_str()); + fNquery++; + myquery.str(""); + myquery << "SET wait_timeout=173000;"; + dbc->Query(myquery.str().c_str()); + fNquery++; + return true; + }; + }; + // + printf(" GLTABLES: connection is gone away, query will fail\n"); + // + return false; + // +}; + +void GL_TABLES::ResetCounters(){ + fNquery = 0; +}; + +void GL_TABLES::AddQ(){ + if ( fSet ) fNquery++; +}; + +UInt_t GL_TABLES::GetNqueries(){ + UInt_t rn = 0; + rn += (UInt_t&)fNquery; + return(rn); +}; + GL_RUN::GL_RUN() { ID = 0; ID_RUN_FRAG = 0; @@ -62,7 +172,7 @@ } -void GL_RUN::Clear() { +void GL_RUN::Clear(Option_t *t) { ID = 0; ID_RUN_FRAG = 0; ID_ROOT_L0 = 0; @@ -137,6 +247,25 @@ FROM_TIME = 0; TO_TIME = 0; SECTION = 0; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; + VALIDATION = 0; +} + +GL_CALOPULSE_CALIB::GL_CALOPULSE_CALIB(){ + ID = 0; + ID_ROOT_L0 = 0; + EV_ROOT = 0; + FROM_TIME = 0; + TO_TIME = 0; + SECTION = 0; + PULSED_STRIP = 0; + PULSE_AMPLITUDE = 0; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; + VALIDATION = 0; } GL_S4_CALIB::GL_S4_CALIB(){ @@ -145,14 +274,15 @@ EV_ROOT = 0; FROM_TIME = 0; TO_TIME = 0; - PARAM_FIT0 = 0.; - PARAM_FIT1 = 0.; + OBT = 0; + PKT = 0; + BOOT_NUMBER = 0; } GL_TIMESYNC::GL_TIMESYNC(){ obtfirst = 0; pktfirst = 0; - toffset = 0; + ID_RESURS_OFFSET = 0; ID = 0; ID_RAW = 0; OBT0 = 0; @@ -160,9 +290,6 @@ TYPE = 0; } -GL_TLE::GL_TLE(){ -} - // **************************************************** void GL_RUN::SetEV_FROM(UInt_t evfrom){ @@ -427,12 +554,16 @@ // // printf("1myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // // retrieve this ID_TRASH // myquery.str(""); myquery << " SELECT ID_TRASH,ID_ROOT_L0,ID_ROOT_L2 FROM GL_RUN_TRASH ORDER BY ID_TRASH DESC LIMIT 1"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // UInt_t idtrash = 0; @@ -454,6 +585,8 @@ // // printf("2myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -469,6 +602,8 @@ // // printf("3myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -485,6 +620,8 @@ // // printf("4myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -494,6 +631,8 @@ // // printf("4myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -504,6 +643,8 @@ // printf("4myquery is %s \n",myquery.str().c_str()); // // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // myquery.str(""); @@ -514,6 +655,8 @@ // printf("5myquery is %s \n",myquery.str().c_str()); // // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -545,6 +688,8 @@ // printf(" qui? myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // Row = pResult->Next(); @@ -628,6 +773,8 @@ // // printf("5myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // // @@ -635,6 +782,8 @@ myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID="; myquery << IDRUN << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -717,6 +866,8 @@ // // printf("myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -793,6 +944,8 @@ // // printf("myquery is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); dbc->Query(myquery.str().c_str()); // return 0; @@ -850,6 +1003,8 @@ myquery << " from GL_RUN where ID=" << run << ";"; // // printf(" myquery is %s \n",myquery.str().c_str()); + if ( !this->IsConnected(dbc) ) return -57; + this->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // // printf(" getrowcount %i \n",pResult->GetRowCount()); @@ -861,36 +1016,36 @@ if( Row == NULL ) break; // Set_GL_RUN(Row); for( t = 0; t < pResult->GetFieldCount(); t++){ - if (t== 0) ID = (UInt_t)atoll(Row->GetField(t)); - if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t)); - if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); - if (t== 3) ID_ROOT_L2 = (UInt_t)atoll(Row->GetField(t)); - if (t== 4) RUNHEADER_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t== 5) RUNTRAILER_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t== 6) RUNHEADER_OBT = (UInt_t)atoll(Row->GetField(t)); - if (t== 7) RUNTRAILER_OBT = (UInt_t)atoll(Row->GetField(t)); - if (t== 8) RUNHEADER_PKT = (UInt_t)atoll(Row->GetField(t)); - if (t== 9) RUNTRAILER_PKT = (UInt_t)atoll(Row->GetField(t)); - if (t==10) EV_FROM = (UInt_t)atoll(Row->GetField(t)); - if (t==11) EV_TO = (UInt_t)atoll(Row->GetField(t)); - if (t==12) NEVENTS = (UInt_t)atoll(Row->GetField(t)); - if (t==13) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); - if (t==14) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); - if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t)); - if (t==16) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); - if (t==17) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); - if (t==18) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t)); - if (t==19) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t)); - if (t==20) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t)); - if (t==21) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t)); - if (t==22) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t)); - if (t==23) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t)); - if (t==24) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t)); - if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t)); - if (t==26) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t)); - if (t==27) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t)); - if (t==28) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t)); - if (t==29) VALIDATION = (UInt_t)atoll(Row->GetField(t)); + if (t== 0) ID = (UInt_t)atoll(Row->GetField(t)); + if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t)); + if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); + if (t== 3) ID_ROOT_L2 = (UInt_t)atoll(Row->GetField(t)); + if (t== 4) RUNHEADER_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t== 5) RUNTRAILER_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t== 6) RUNHEADER_OBT = (UInt_t)atoll(Row->GetField(t)); + if (t== 7) RUNTRAILER_OBT = (UInt_t)atoll(Row->GetField(t)); + if (t== 8) RUNHEADER_PKT = (UInt_t)atoll(Row->GetField(t)); + if (t== 9) RUNTRAILER_PKT = (UInt_t)atoll(Row->GetField(t)); + if (t==10) EV_FROM = (UInt_t)atoll(Row->GetField(t)); + if (t==11) EV_TO = (UInt_t)atoll(Row->GetField(t)); + if (t==12) NEVENTS = (UInt_t)atoll(Row->GetField(t)); + if (t==13) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); + if (t==14) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); + if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t)); + if (t==16) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); + if (t==17) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); + if (t==18) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t)); + if (t==19) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t)); + if (t==20) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t)); + if (t==21) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t)); + if (t==22) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t)); + if (t==23) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t)); + if (t==24) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t)); + if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t)); + if (t==26) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t)); + if (t==27) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t)); + if (t==28) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t)); + if (t==29) VALIDATION = (UInt_t)atoll(Row->GetField(t)); }; }; // delete pResult; @@ -948,6 +1103,8 @@ // // printf(" query is %s \n",myquery.str().c_str()); // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return(-50); for( r=0; r < 1000; r++){ @@ -1013,6 +1170,8 @@ myquery << ",NAME"; myquery << " from GL_ROOT where ID=" << id << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-51); for( r=0; r < 1000; r++){ @@ -1022,7 +1181,7 @@ if(t==0) ID = (UInt_t)atoll(Row->GetField(t)); if(t==1) ID_RAW = (UInt_t)atoll(Row->GetField(t)); if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); - if(t==3) PATH = Row->GetField(t); + if(t==3) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/'; if(t==4) NAME = Row->GetField(t); }; }; @@ -1047,8 +1206,12 @@ // ---------------- myquery.str(""); myquery << "select ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,OBT2,PKT1,PKT2,BOOT_NUMBER,VALIDATION from GL_TRK_CALIB where FROM_TIME <= "<< time; - myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; +// myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; // RIVEDERE LA VALIDAZIONE!!! + myquery << " and EV_ROOT_CALIBTRK1>=0 and EV_ROOT_CALIBTRK2>=0 "; // EM! SE MANCA UN PACCHETTO DEI DUE IL PROCESSAMENTO CRASHA... DA RIVEDERE LA VALIDAZIONE + myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-53); for( r=0; r < 1000; r++){ @@ -1106,6 +1269,8 @@ myquery << " ;"; //myquery << " and VALIDATION=1;"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // @@ -1125,6 +1290,8 @@ myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT,VALIDATION from GL_CALO_CALIB where SECTION=" << section; myquery << " and FROM_TIME <= " << time; myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); // printf(" mysquery is %s\n",myquery.str().c_str()); // @@ -1148,6 +1315,54 @@ pResult->Delete(); return 0; }; + + +// **************************************************** +/** + * Function to query the GL_CALOPULSE_CALIB table of the DB. + * + * \param run starting time + * \return struct of type GL_CALOPULSE_CALIB_data, which stores the query result + */ +Int_t GL_CALOPULSE_CALIB::Query_GL_CALOPULSE_CALIB(UInt_t time, UInt_t section, UInt_t pampli, TSQLServer *dbc){ + // MySQL variables + TSQLResult *pResult; + TSQLRow *Row; + int t; + stringstream myquery; + // + // select the correct calibration i.e. the closest to our time + // + myquery.str(""); + myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT from GL_CALOPULSE_CALIB where SECTION=" << section; + myquery << " and PULSE_AMPLITUDE=" << pampli; + myquery << " and VALIDATION=1 and (PULSED_STRIP IS NULL OR PULSED_STRIP<100) ORDER BY ABS(" << time << "-FROM_TIME) asc limit 1;"; + // printf(" myq is %s \n",myquery.str().c_str()); + // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); + pResult = dbc->Query(myquery.str().c_str()); + // + if( !pResult ) return(-54); + // + Row = pResult->Next(); + // + if( !Row ) return (-54); + // + // store infos and exit + // + for( t = 0; t < pResult->GetFieldCount(); t++){ + // printf(" field %i is %s \n",t,Row->GetField(t)); + if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); + if (t==1) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==2) TO_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); + }; + pResult->Delete(); + return 0; +}; + + // **************************************************** /** * Function to query the GL_S4_CALIB table of the DB. @@ -1164,9 +1379,11 @@ stringstream myquery; // ---------------- myquery.str(""); - myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time; + myquery << "select ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME from GL_S4_CALIB where FROM_TIME <= "<< time; myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-55);//throw -55; for( r=0; r < 1000; r++){ @@ -1174,12 +1391,10 @@ if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ if (t==0) ID = (UInt_t)atoll(Row->GetField(t)); - if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); - if (t==2) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); - if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==5) PARAM_FIT0 = atof(Row->GetField(t)); - if (t==6) PARAM_FIT1 = atof(Row->GetField(t)); + if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); + if (t==2) EV_ROOT = (UInt_t)atoll(Row->GetField(t)); + if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t)); }; }; delete pResult; @@ -1207,12 +1422,14 @@ // ---------------- myquery.str(""); myquery << " select "; - myquery << " ID, PATH, NAME, DESCR, TYPE, FROM_TIME,TO_TIME "; + myquery << " ID, PATH, NAME, DESCR, FROM_TIME,TO_TIME, TYPE "; myquery << " from GL_PARAM "; myquery << " where TYPE = '"<GetGLTABLES()->IsConnected(dbc) ) return -57; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-52); for( r=0; r < 1000; r++){ @@ -1220,7 +1437,7 @@ if( Row == NULL ) break; for( t = 0; t < pResult->GetFieldCount(); t++){ if (t==0) ID = (UInt_t)atoll(Row->GetField(t)); - if (t==1) PATH = Row->GetField(t);// put in fpath the path to that file + if (t==1) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/';// put in fpath the path to that file if (t==2) NAME = Row->GetField(t); if (t==3) DESCR = Row->GetField(t); if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); @@ -1266,6 +1483,9 @@ TSQLRow *Row; stringstream myquery; stringstream rname; + // pcksList packetsNames; + // pcksList::iterator Iter; + // getPacketsNames(packetsNames); rname.str(""); // ---------------- myquery.str(""); @@ -1276,13 +1496,15 @@ myquery << type.Data(); myquery << "=" << id << ";"; // + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if( pResult->GetRowCount() ){ Row = pResult->Next(); if( Row ){ stringstream fname; fname.str(""); - fname << Row->GetField(0) << "/" << Row->GetField(1); + fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1); rname << Row->GetField(1); file = new TFile(fname.str().c_str(),"READ"); idraw = (UInt_t)atoll(Row->GetField(2)); @@ -1299,81 +1521,140 @@ ph = eh->GetPscuHeader(); pktfirst = ph->GetCounter(); obtfirst = ph->GetOrbitalTime(); + // +// code = eh->GetCounter(); +// UInt_t en = 0; +// for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){ +// en = code->Get(GetPacketType(*Iter)); +// if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){ +// // +// TTree *TC = 0; +// TC = (TTree*)file->Get("CalibCalPed"); +// if ( !TC || TC->IsZombie() ) return; +// EventHeader *ehc = 0; +// PscuHeader *phc = 0; +// TC->SetBranchAddress("Header", &ehc); +// TC->GetEntry(0); +// phc = ehc->GetPscuHeader(); +// pktfirst = phc->GetCounter(); +// obtfirst = phc->GetOrbitalTime(); +// // +// }; +// }; + // }; // // look for Resurs offset // - UInt_t t0 = 0; + T0 = 0; // // stringstream oss; + TString frn = rname.str().c_str(); + frn = frn.ReplaceAll(".root",5,".pam",4); oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" - << rname.str().c_str() << "';"; - // if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='" + << frn.Data() << "';"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); // TString name=rname.str().c_str(); UInt_t dworbit = 0; Int_t nlength = name.Length(); - if ( nlength < 5 ) return; - TString dwo = 0; - for (Int_t i = 0; i<5; i++){ - dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ){ - dworbit = (UInt_t)dwo.Atoi(); - } else { - dwo=""; - for (Int_t i = 8; i<13; i++){ - dwo.Append(name[i],1); - }; - if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi(); - }; - // if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data()); + // + // Is not a special file // if ( !Row ){ + delete pResult; + // + // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset + // oss.str(""); - oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " - << dworbit << " order by FROM_ORBIT desc limit 1;"; - // if ( IsDebug() ) printf(" %s \n",oss.str().c_str()); + oss << "SELECT ID_RESURS_OFFSET from GL_TIMESYNC where ID_RAW=" << idraw <<";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(oss.str().c_str()); - Row = pResult->Next(); - if ( !Row ){ - printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); - return; + Bool_t fndit = false; + if ( pResult ){ + Row = pResult->Next(); + if ( Row ){ + oss.str(""); + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE ID=" + << Row->GetField(0) << ";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); + pResult = dbc->Query(oss.str().c_str()); + if ( pResult ){ + Row = pResult->Next(); + if ( Row ){ + // printf(" GREAT! the DB structure is the new one! \n"); + fndit = true; + dworbit = 1; + }; + }; + }; + }; + if ( !fndit ){ + delete pResult; + // + printf(" OK, you got an error because this is the old database\n Using backward compability code, hence you can continue safetly \n"); + // + // Old code, we must trust the filename + // + if ( nlength < 5 ) return; + TString dwo = 0; + for (Int_t i = 0; i<5; i++){ + dwo.Append(name[i],1); + }; + if ( dwo.IsDigit() ){ + dworbit = (UInt_t)dwo.Atoi(); + } else { + dwo=""; + for (Int_t i = 8; i<13; i++){ + dwo.Append(name[i],1); + }; + if ( dwo.IsDigit() ){ + dworbit = (UInt_t)dwo.Atoi(); + } else { + dworbit = 1; + }; + }; + // + oss.str(""); + oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< " + << dworbit << " order by FROM_ORBIT desc limit 1;"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); + pResult = dbc->Query(oss.str().c_str()); + Row = pResult->Next(); + if ( !Row ){ + printf(" ERROR FROM GLTables! cannot determine Resurs offset \n"); + return; + }; }; }; // - t0 = (UInt_t)TDatime(Row->GetField(0)).Convert(); - // - // myquery.str(""); - // myquery << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= " - // << idraw << " AND TO_ID_RAW >= " - // << idraw << ";"; - // pResult = dbc->Query(myquery.str().c_str()); - // if ( pResult ){ - // Row = pResult->Next(); - // if ( Row ){ - // // - // t0 = (UInt_t)TDatime(Row->GetField(0)).Convert(); - // // - // }; - // }; - // + TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0); + T0 = (UInt_t)tu.GetSec(); // // look for the correct timesync entry // myquery.str(""); - myquery << " SELECT OBT0,TIMESYNC FROM GL_TIMESYNC " + myquery << " SELECT OBT0,TIMESYNC,TYPE FROM GL_TIMESYNC " << " WHERE ID_RAW = " << idraw << ";"; + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return; + this->GetGLTABLES()->AddQ(); pResult = dbc->Query(myquery.str().c_str()); if ( pResult ){ Row = pResult->Next(); if ( (Row != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){ - toffset = (UInt_t)atoll(Row->GetField(1)) - (UInt_t)(this->DBobt((UInt_t)atoll(Row->GetField(0)))/1000) + t0; + OBT0 = (UInt_t)atoll(Row->GetField(0)); + TIMESYNC = (UInt_t)atoll(Row->GetField(1)); + TYPE = (UInt_t)atoll(Row->GetField(2)); + toffset = (UInt_t)atoll(Row->GetField(1)) - (UInt_t)(this->DBobt((UInt_t)atoll(Row->GetField(0)))/1000) + T0; }; }; // @@ -1392,39 +1673,83 @@ // }; +/** + * + * Returns the Resurs time given the OBT needed to process inclination and orbital infos + * + */ +UInt_t GL_TIMESYNC::ResursTime(UInt_t OBT){ + // + return(((UInt_t)((Int_t)(this->DBobt(OBT)-this->DBobt(OBT0))/1000)+TIMESYNC)); + // +}; -ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){ + +/** + * Return the correct packet number if we went back to zero + */ +Long64_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ // - if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((ULong64_t)(obt+numeric_limits::max())); + if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ){ + return((Long64_t)pkt_num+16777215LL); + }; // - if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ - if ( (obt-numeric_limits::max()) < 0 ){ - return((ULong64_t)(numeric_limits::max()-obt)); - } else { - return((ULong64_t)(obt-numeric_limits::max())); - }; + if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ + return((Long64_t)pkt_num-16777215LL); }; // - return((ULong64_t)obt); + return((Long64_t)pkt_num); // }; -UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ +/** + * Return the correct On Board Time if we went back to zero + */ +Long64_t GL_TIMESYNC::DBobt(UInt_t obt){ // - if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215)); + if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((Long64_t)(obt+numeric_limits::max())); // - if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ - if ( (pkt_num-16777215) < 0 ){ - return((16777215-pkt_num)); - } else { - return((pkt_num-16777215)); - }; + if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ + return((Long64_t)obt-(Long64_t)numeric_limits::max()); }; // - return(pkt_num); - // + return((Long64_t)obt); }; + + +// ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){ +// // +// if ( obt < (obtfirst/2) && obtfirst > (numeric_limits::max()/2) ) return((ULong64_t)(obt+numeric_limits::max())); +// // +// if ( obt > (obtfirst*2) && obt > (numeric_limits::max()/2) ){ +// if ( (obt-numeric_limits::max()) < 0 ){ +// return((ULong64_t)(numeric_limits::max()-obt)); +// } else { +// return((ULong64_t)(obt-numeric_limits::max())); +// }; +// }; +// // +// return((ULong64_t)obt); +// // +// }; + +// UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){ +// // +// if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215)); +// // +// if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){ +// if ( (pkt_num-16777215) < 0 ){ +// return((16777215-pkt_num)); +// } else { +// return((pkt_num-16777215)); +// }; +// }; +// // +// return(pkt_num); +// // +// }; + /* * * Convert the time in the DB from UInt_t to a string @@ -1435,40 +1760,34 @@ */ TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){ // - TDatime *time = new TDatime(); + Int_t offset = 0; TString rtime; Bool_t found = false; // - time->Set(dbt,false); // UTC (Coordinated Universal Time) - // if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){ // - UInt_t timeMSK = time->Convert() + 60*60*3; // UTC (Coordinated Universal Time) + 3hs = Moscow Winter Time - time->Set(timeMSK,false); + offset = 60*60*3; // UTC (Coordinated Universal Time) + 3hs = Moscow Winter Time found = true; // }; // if ( !strcmp(tzone.Data(),"CET") ){ // - UInt_t timeCET = time->Convert() + 60*60*1; // CET (Central European Time) = UTC + 1 hs - time->Set(timeCET,false); + offset = 60*60*1; // CET (Central European Time) = UTC + 1 hs found = true; // }; // if ( !strcmp(tzone.Data(),"CEST") ){ // - UInt_t timeCEST = time->Convert() + 60*60*2; // CEST (Central European Summer Time) = UTC + 2 h - time->Set(timeCEST,false); + offset = 60*60*2; // CEST (Central European Summer Time) = UTC + 2 h found = true; // }; // if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){ // - UInt_t timeMSD = time->Convert() + 60*60*4; // MSD (Moscow Summer Time) = UTC + 4 h - time->Set(timeMSD,false); + offset = 60*60*4; // MSD (Moscow Summer Time) = UTC + 4 h found = true; // }; @@ -1478,14 +1797,20 @@ tzone = "UTC"; }; // - rtime = time->AsSQLString(); + dbt += offset; + // + TTimeStamp *time = new TTimeStamp((time_t)dbt,0); + // + rtime = time->AsString("s"); + // + delete time; // return(rtime); } /* * - * Convert the time in the DB from UInt_t to a string + * Convert the time from TZONE to UTC * * @param dbt time in the DB * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK @@ -1493,135 +1818,162 @@ */ TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){ // - TDatime *time = new TDatime(); + Int_t offset = 0; TString rtime; // Bool_t found = false; // - time->Set(dbt,false); // tzone - // if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){ // - UInt_t timeUTC = time->Convert() - 60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3hs - time->Set(timeUTC,false); + offset = -60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3hs found = true; // }; // if ( !strcmp(tzone.Data(),"CET") ){ // - UInt_t timeCET = time->Convert() - 60*60*1; // CET (Central European Time) - 1 hs = UTC - time->Set(timeCET,false); + offset = -60*60*1; // CET (Central European Time) - 1 hs = UTC found = true; // }; // if ( !strcmp(tzone.Data(),"CEST") ){ // - UInt_t timeCEST = time->Convert() - 60*60*2; // CEST (Central European Summer Time) - 2 h = UTC - time->Set(timeCEST,false); + offset = -60*60*2; // CEST (Central European Summer Time) - 2 h = UTC found = true; // }; // if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){ // - UInt_t timeMSD = time->Convert() - 60*60*4; // MSD (Moscow Summer Time) - 4 h = UTC - time->Set(timeMSD,false); + offset = -60*60*4; // MSD (Moscow Summer Time) - 4 h = UTC found = true; // }; // if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){ // + offset = 0; printf("\n Unknown time zone %s using UTC \n",tzone.Data()); tzone = "UTC"; }; // - rtime = time->AsSQLString(); + dbt += offset; + TTimeStamp *time = new TTimeStamp((time_t)dbt,0); + // + rtime = time->AsString("s"); // return(rtime); } -// **************************************************** -/** - * Function to query the GL_TLE table of the DB. - * - * time is the unix time for which a good tle is requested. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE(UInt_t time, TSQLServer *dbc){ +// +// Build a query and call DoQuery. +// +// date it's an SQL datetime date and dbc is the connection to be +// used. It will query for the tle with the nearest but previous date +// and the immediatly next one. +// +Int_t GL_TLE::Query(TString date, TSQLServer *dbc){ stringstream myquery; myquery.str(""); - myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE " - << "WHERE FROM_TIME < FROM_UNIXTIME(" << time << ") ORDER BY FROM_TIME DESC LIMIT 1;"; - + myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME <= '" << date.Data() + << "' ORDER BY FROM_TIME DESC LIMIT 1) " + << "UNION " + << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME > '" << date.Data() + << "' ORDER BY FROM_TIME ASC LIMIT 1)"; - return Query_GL_TLE_go(myquery.str(), dbc); + return DoQuery(myquery.str().c_str(), dbc); } -// **************************************************** -/** - * Function to query the GL_TLE table of the DB. - * - * date is a datetime format YYYY-MM-DD hh:mm:ss for which a good tle - * is requested. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE(TString date, TSQLServer *dbc){ +// +// Build a query and call DoQuery. +// +// time is the UTC date in unix time (UTC) and dbc is the connection +// to be used. It will query for the tle with the nearest but +// previous date and the immediatly next one. +// +// Returns the value returned by DoQuery(). +// +Int_t GL_TLE::Query(UInt_t time, TSQLServer *dbc){ stringstream myquery; myquery.str(""); - myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE " - << "WHERE FROM_TIME < '" << date.Data() << "' ORDER BY FROM_TIME DESC LIMIT 1;"; + myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME <= FROM_UNIXTIME('" << time + << "') ORDER BY FROM_TIME DESC LIMIT 1) " + << "UNION " + << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE " + << "WHERE FROM_TIME > FROM_UNIXTIME('" << time + << "') ORDER BY FROM_TIME ASC LIMIT 1)"; - - return Query_GL_TLE_go(myquery.str(), dbc); + return DoQuery(myquery.str().c_str(), dbc); } -// **************************************************** -/** - * Private function used by Query_GL_TLE methods. - * - * myquery is the query string. - * - * Return the pointer to a cTle object that has the closest and - * previous date compared with time. - * - * If errors occurs it returns NULL. - */ -cTle* GL_TLE::Query_GL_TLE_go(TString myquery, TSQLServer *dbc){ - cTle *tle; - string tle1, tle2, tle3; - - // MySQL variables +// +// Do the query myquery on the connectio dbc. Initialize tle, +// tleFromTime and tleToTime. +// +// We should have two rows (unless the table is old). From the last +// one we only take tleToTime. +// +// Returns 0 for success, 1 for failure. +// +Int_t GL_TLE::DoQuery(TString myquery, TSQLServer *dbc){ TSQLResult *result; TSQLRow *row; + // Set the right time_zone (otherwise horrible things will occur! :) + if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57; + dbc->Query("SET time_zone = '+0:00'"); + + // Do the query + this->GetGLTABLES()->AddQ(); result = dbc->Query(myquery.Data()); - if(! result->GetRowCount() ) return NULL; + if(! result->GetRowCount() ) { + cerr << "GL_TLE: query failed: " << myquery.Data() << endl; + return 1; + } + + // Get results + row = result->Next(); // first tle row + tle = GiveTle(row); + + tleFromTime = strtol(row->GetField(4), NULL, 10); + + row = result->Next(); // second tle row + if(row) + tleToTime = strtol(row->GetField(4), NULL, 10); + else { + cerr << "GL_TLE: Warning: using last avaible TLE. Please update GL_TLE table!\n"; + tleToTime = UINT_MAX; + } + + delete row; + delete result; - row = result->Next(); - tle1 = row->GetField(0); - tle2 = row->GetField(1); - tle3 = row->GetField(2); + return 0; +} - tle = new cTle(tle1, tle2, tle3); - delete result; - delete row; +// +// Build a cTle object from the GL_TLE row. +// +cTle* GL_TLE::GiveTle(TSQLRow *row) { + cTle *thistle = NULL; + string tle1, tle2, tle3; + + // Build cTle object + tle1 = row->GetField(1); + tle2 = row->GetField(2); + tle3 = row->GetField(3); + + thistle = new cTle(tle1, tle2, tle3); - return tle; + return thistle; }