--- YodaProfiler/src/GLTables.cpp 2006/09/05 10:48:58 1.3 +++ YodaProfiler/src/GLTables.cpp 2006/10/20 11:39:37 1.13 @@ -6,6 +6,7 @@ */ // #include +#include // #include #include @@ -13,6 +14,7 @@ #include // #include +#include // ClassImp(GL_TRK_CALIB); ClassImp(GL_RUN); @@ -21,6 +23,7 @@ ClassImp(GL_S4_CALIB); ClassImp(GL_CALO_CALIB); ClassImp(GL_TIMESYNC); +ClassImp(GL_TLE); // using namespace std; @@ -58,6 +61,7 @@ VALIDATION = 0; } + void GL_RUN::Clear() { ID = 0; ID_RUN_FRAG = 0; @@ -122,6 +126,8 @@ OBT2 = 0; PKT1 = 0; PKT2 = 0; + BOOT_NUMBER = 0; + VALIDATION = 0; } GL_CALO_CALIB::GL_CALO_CALIB(){ @@ -152,8 +158,12 @@ OBT0 = 0; TIMESYNC = 0; TYPE = 0; -}// **************************************************** +} +GL_TLE::GL_TLE(){ +} + +// **************************************************** void GL_RUN::SetEV_FROM(UInt_t evfrom){ EV_FROM = evfrom; @@ -327,6 +337,42 @@ CAL_DSP_MASK = 0; }; +void GL_RUN::Set_GL_RUN(TSQLRow *Row){ + for( Int_t t = 0; t < 30; 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) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t)); + if (t==11) EV_FROM = (UInt_t)atoll(Row->GetField(t)); + if (t==12) EV_TO = (UInt_t)atoll(Row->GetField(t)); + if (t==13) NEVENTS = (UInt_t)atoll(Row->GetField(t)); + if (t==14) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t)); + if (t==15) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t)); + if (t==16) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t)); + if (t==17) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); + if (t==18) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t)); + if (t==19) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t)); + if (t==20) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t)); + if (t==21) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t)); + if (t==22) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t)); + if (t==23) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t)); + if (t==24) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t)); + if (t==25) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t)); + if (t==26) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t)); + if (t==27) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); + if (t==28) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t)); + if (t==29) VALIDATION = (UInt_t)atoll(Row->GetField(t)); + }; + +} + /** * Function to fill the GL_RUN table of the DB. @@ -537,50 +583,52 @@ myquery << ",VALIDATION"; myquery << " from GL_RUN where ID=" << run << ";"; // + // printf(" myquery is %s \n",myquery.str().c_str()); pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount())return(-50); + // + // printf(" getrowcount %i \n",pResult->GetRowCount()); + // + if( !pResult->GetRowCount() ) return(-50); + // for( r=0; r < 1000; r++){ Row = pResult->Next(); 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; - - return 0; - + // delete pResult; + return(0); }; /** @@ -720,6 +768,7 @@ * Function to query the GL_TRK_CALIB table of the DB. * * \param run starting time + * \param dbc DB server * \return struct of type GL_TRK_CALIB_data, which stores the query result */ Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(UInt_t time, TSQLServer *dbc){ @@ -731,7 +780,7 @@ stringstream myquery; // ---------------- myquery.str(""); - myquery << "select ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,OBT2,PKT1,PKT2 from GL_TRK_CALIB where FROM_TIME <= "<< time; + 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 << " ORDER BY FROM_TIME DESC LIMIT 1;"; pResult = dbc->Query(myquery.str().c_str()); @@ -743,22 +792,25 @@ stringstream row; row.str(""); row << "0" << Row->GetField(t); // add leading 0 since we have two fields that could be "null" and we want to avoid crashes due to atoll - if (t==0) ID = (UInt_t)atoll(Row->GetField(t)); - if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(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_CALIBTRK1 = (UInt_t)atoll(row.str().c_str()); if (t==3) EV_ROOT_CALIBTRK2 = (UInt_t)atoll(row.str().c_str()); - if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); - if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t)); + if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t)); // - if (t==6) OBT1 = (UInt_t)atoll(Row->GetField(t)); - if (t==7) OBT2 = (UInt_t)atoll(Row->GetField(t)); - if (t==8) PKT1 = (UInt_t)atoll(Row->GetField(t)); - if (t==9) PKT2 = (UInt_t)atoll(Row->GetField(t)); - }; + if (t==6) OBT1 = (UInt_t)atoll(Row->GetField(t)); + if (t==7) OBT2 = (UInt_t)atoll(Row->GetField(t)); + if (t==8) PKT1 = (UInt_t)atoll(Row->GetField(t)); + if (t==9) PKT2 = (UInt_t)atoll(Row->GetField(t)); + if (t==10) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t)); + if (t==11) VALIDATION = (UInt_t)atoll(Row->GetField(t)); + }; }; - delete pResult; + delete pResult; + // +// if ( TO_TIME < time ) return(51); // - if(TO_TIME < time)return(51); if ( (!OBT1 && !PKT1 ) || (!OBT2 && !PKT2) ) return(52); // ONE CALIBRATION PACKET IS MISSING! // return 0; @@ -771,12 +823,13 @@ * \param run starting time * \return struct of type GL_CALO_CALIB_data, which stores the query result */ -Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB(UInt_t time, UInt_t section, TSQLServer *dbc){ +Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB(UInt_t time, UInt_t &uptime, UInt_t section, TSQLServer *dbc){ // MySQL variables TSQLResult *pResult; TSQLRow *Row; int t; stringstream myquery; + uptime = 0; // // select the correct calibration // @@ -784,14 +837,21 @@ 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 TO_TIME > " << time; - myquery << " and VALIDATION=1;"; + myquery << " ;"; + //myquery << " and VALIDATION=1;"; // pResult = dbc->Query(myquery.str().c_str()); + // printf(" mysquery is %s\n",myquery.str().c_str()); + // + if( !pResult->GetRowCount() ) return(-54); + Row = pResult->Next(); + if( Row == NULL ) return (-54); + // + uptime = (UInt_t)atoll(Row->GetField(2)); // // if it is corrupted validation is 0 and we have no results from the query... // - if( !pResult->GetRowCount() ){ - // pResult->Delete(); + if( atoi(Row->GetField(4)) == 0 ){ // if validation = 0 // // in this case take relax the conditions and take the valid calibration that preceed the correct one // @@ -800,15 +860,18 @@ myquery << " and FROM_TIME <= " << time; myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; pResult = dbc->Query(myquery.str().c_str()); + // printf(" mysquery is %s\n",myquery.str().c_str()); // // if no results yet quit with error // if( !pResult->GetRowCount() ) return (-54); + // + Row = pResult->Next(); + // }; // // store infos and exit // - Row = pResult->Next(); if( Row == NULL ) return (-54); for( t = 0; t < pResult->GetFieldCount(); t++){ if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t)); @@ -836,7 +899,7 @@ // ---------------- myquery.str(""); myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time; - myquery << " AND VALIDATION = 1 ORDER BY FROM_TIME DESC LIMIT 1;"; + myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;"; pResult = dbc->Query(myquery.str().c_str()); if(!pResult->GetRowCount())return (-55);//throw -55; @@ -1049,3 +1112,204 @@ return(pkt_num); // }; + +/* + * + * Convert the time in the DB from UInt_t to a string + * + * @param dbt time in the DB + * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK + * + */ +TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){ + // + TDatime *time = new TDatime(); + 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); + 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); + 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); + 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); + found = true; + // + }; + // + if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){ + printf("\n Unknown time zone %s using UTC \n",tzone.Data()); + tzone = "UTC"; + }; + // + rtime = time->AsSQLString(); + // + return(rtime); +} + +/* + * + * Convert the time in the DB from UInt_t to a string + * + * @param dbt time in the DB + * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK + * + */ +TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){ + // + TDatime *time = new TDatime(); + 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); + 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); + 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); + 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); + found = true; + // + }; + // + if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){ + // + printf("\n Unknown time zone %s using UTC \n",tzone.Data()); + tzone = "UTC"; + }; + // + rtime = time->AsSQLString(); + // + 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){ + 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;"; + + + return Query_GL_TLE_go(myquery.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){ + stringstream myquery; + myquery.str(""); + + myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE " + << "WHERE FROM_TIME < '" << date.Data() << "' ORDER BY FROM_TIME DESC LIMIT 1;"; + + + return Query_GL_TLE_go(myquery.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 + TSQLResult *result; + TSQLRow *row; + + result = dbc->Query(myquery.Data()); + if(! result->GetRowCount() ) return NULL; + + row = result->Next(); + tle1 = row->GetField(0); + tle2 = row->GetField(1); + tle3 = row->GetField(2); + + tle = new cTle(tle1, tle2, tle3); + + delete result; + delete row; + + return tle; +}