--- YodaProfiler/src/GLTables.cpp 2006/09/08 14:42:48 1.9 +++ YodaProfiler/src/GLTables.cpp 2006/10/31 15:36:04 1.15 @@ -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; @@ -155,8 +158,12 @@ OBT0 = 0; TIMESYNC = 0; TYPE = 0; -}// **************************************************** +} + +GL_TLE::GL_TLE(){ +} +// **************************************************** void GL_RUN::SetEV_FROM(UInt_t evfrom){ EV_FROM = evfrom; @@ -366,6 +373,272 @@ } +/** + * This method delete the run from the "FromTable" table and store it in the GL_RUN_TRASH table + * If IDRUN is 0 "this->ID" run is used. + * + **/ +Int_t GL_RUN::DeleteRun(TSQLServer *dbc,UInt_t IDRUN,TString FromTable){ + // MySQL variables + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + if ( !IDRUN ) IDRUN = ID; + if ( !IDRUN ) return 1; + // ---------------- + myquery.str(""); + myquery << " INSERT INTO GL_RUN_TRASH ("; + myquery << "ID"; + myquery << ",ID_RUN_FRAG"; + myquery << ",ID_ROOT_L0"; + myquery << ",ID_ROOT_L2"; + myquery << ",RUNHEADER_TIME"; + myquery << ",RUNTRAILER_TIME"; + myquery << ",RUNHEADER_OBT"; + myquery << ",RUNTRAILER_OBT"; + myquery << ",RUNHEADER_PKT"; + myquery << ",RUNTRAILER_PKT"; + myquery << ",BOOT_NUMBER"; + myquery << ",EV_FROM"; + myquery << ",EV_TO"; + myquery << ",NEVENTS"; + myquery << ",PKT_COUNTER"; + myquery << ",PKT_READY_COUNTER"; + myquery << ",COMPILATIONTIMESTAMP"; + myquery << ",FAV_WRK_SCHEDULE"; + myquery << ",EFF_WRK_SCHEDULE"; + myquery << ",PRH_VAR_TRG_MODE_A"; + myquery << ",PRH_VAR_TRG_MODE_B"; + myquery << ",ACQ_BUILD_INFO"; + myquery << ",ACQ_VAR_INFO"; + myquery << ",RM_ACQ_AFTER_CALIB"; + myquery << ",RM_ACQ_SETTING_MODE"; + myquery << ",TRK_CALIB_USED"; + myquery << ",CAL_DSP_MASK"; + myquery << ",LAST_TIMESYNC"; + myquery << ",OBT_TIMESYNC"; + myquery << ",VALIDATION"; + myquery << ",INSERT_TIME"; + myquery << ") SELECT * FROM "; + myquery << FromTable.Data(); + myquery << " WHERE ID="; + myquery << (UInt_t)IDRUN << ";"; + // + // printf("1myquery is %s \n",myquery.str().c_str()); + // + 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"; + pResult = dbc->Query(myquery.str().c_str()); + // + UInt_t idtrash = 0; + UInt_t idl0 = 0; + UInt_t idl2 = 0; + // + Row = pResult->Next(); + if( Row != NULL ){ + idtrash = (UInt_t)atoll(Row->GetField(0)); + idl0 = (UInt_t)atoll(Row->GetField(1)); + idl2 = (UInt_t)atoll(Row->GetField(2)); + }; + // + TString fileL0 = ""; + TString fileL2 = ""; + myquery.str(""); + myquery << " SELECT NAME FROM GL_ROOT WHERE ID="; + myquery << idl0 << ";"; + // + // printf("2myquery is %s \n",myquery.str().c_str()); + // + pResult = dbc->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( Row != NULL ){ + fileL0 = (TString)Row->GetField(0); + }; + // + // + // + myquery.str(""); + myquery << " SELECT NAME FROM GL_ROOT WHERE ID="; + myquery << idl2 << ";"; + // + // printf("3myquery is %s \n",myquery.str().c_str()); + // + pResult = dbc->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( Row != NULL ){ + fileL2 = (TString)Row->GetField(0); + }; + // + // + // + myquery.str(""); + myquery << " UPDATE GL_RUN_TRASH SET FILENAMEL0='"; + myquery << fileL0.Data() << "' where ID_TRASH="; + myquery << idtrash << ";"; + // + // printf("4myquery is %s \n",myquery.str().c_str()); + // + dbc->Query(myquery.str().c_str()); + // + myquery.str(""); + myquery << " UPDATE GL_RUN_TRASH SET FILENAMEL2='"; + myquery << fileL2.Data() << "' where ID_TRASH="; + myquery << idtrash << ";"; + // + // printf("4myquery is %s \n",myquery.str().c_str()); + // + dbc->Query(myquery.str().c_str()); + // + myquery.str(""); + myquery << " UPDATE GL_RUN_TRASH SET BELONGED_TO='"; + myquery << FromTable.Data() << "' where ID_TRASH="; + myquery << idtrash << ";"; + // + // printf("4myquery is %s \n",myquery.str().c_str()); + // + // + dbc->Query(myquery.str().c_str()); + // + myquery.str(""); + myquery << " DELETE FROM "; + myquery << FromTable.Data() << " where ID="; + myquery << IDRUN << ";"; + // + // printf("5myquery is %s \n",myquery.str().c_str()); + // + // + dbc->Query(myquery.str().c_str()); + // + return 0; +}; + + + +/** + * This method restore a run from the GL_RUN_TRASH table. + * If ID is 0 "this->ID" is used; if "ToTable" is empty BELONG_TO field of GL_RUN_TRASH is used. + * + **/ +Int_t GL_RUN::RestoreRun(TSQLServer *dbc,UInt_t IDRUN,TString ToTable){ + // insert into GL_RUN_FRAGMENTS select * FROM GL_RUN where ID=11; + //insert into GL_RUN_TRASH VALUES (ID , ID_RUN_FRAG , ID_ROOT_L0 , ID_ROOT_L2 , RUNHEADER_TIME , RUNTRAILER_TIME , RUNHEADER_OBT , RUNTRAILER_OBT , RUNHEADER_PKT , RUNTRAILER_PKT , BOOT_NUMBER , EV_FROM , EV_TO , NEVENTS , PKT_COUNTER , PKT_READY_COUNTER , COMPILATIONTIMESTAMP , FAV_WRK_SCHEDULE , EFF_WRK_SCHEDULE , PRH_VAR_TRG_MODE_A , PRH_VAR_TRG_MODE_B , ACQ_BUILD_INFO , ACQ_VAR_INFO , RM_ACQ_AFTER_CALIB , RM_ACQ_SETTING_MODE, TRK_CALIB_USED,CAL_DSP_MASK, LAST_TIMESYNC, OBT_TIMESYNC, VALIDATION, INSERT_TIME) select * FROM GL_RUN where ID=11; + // MySQL variables + TSQLResult *pResult; + TSQLRow *Row; + stringstream myquery; + // + if ( !IDRUN ) IDRUN = ID; + if ( !IDRUN ) return 1; + // + if ( !strcmp(ToTable.Data(),"") ){ + // + myquery.str(""); + myquery << " SELECT BELONGED_TO FROM GL_RUN_TRASH WHERE ID="; + myquery << (UInt_t)IDRUN << ";"; + // + printf(" qui? myquery is %s \n",myquery.str().c_str()); + // + pResult = dbc->Query(myquery.str().c_str()); + // + Row = pResult->Next(); + if( Row != NULL ){ + ToTable = (TString)Row->GetField(0); + } else { + return 1; + }; + }; + // ---------------- + myquery.str(""); + myquery << " INSERT INTO "; + myquery << ToTable.Data(); + myquery << " ("; + myquery << "ID"; + myquery << ",ID_RUN_FRAG"; + myquery << ",ID_ROOT_L0"; + myquery << ",ID_ROOT_L2"; + myquery << ",RUNHEADER_TIME"; + myquery << ",RUNTRAILER_TIME"; + myquery << ",RUNHEADER_OBT"; + myquery << ",RUNTRAILER_OBT"; + myquery << ",RUNHEADER_PKT"; + myquery << ",RUNTRAILER_PKT"; + myquery << ",BOOT_NUMBER"; + myquery << ",EV_FROM"; + myquery << ",EV_TO"; + myquery << ",NEVENTS"; + myquery << ",PKT_COUNTER"; + myquery << ",PKT_READY_COUNTER"; + myquery << ",COMPILATIONTIMESTAMP"; + myquery << ",FAV_WRK_SCHEDULE"; + myquery << ",EFF_WRK_SCHEDULE"; + myquery << ",PRH_VAR_TRG_MODE_A"; + myquery << ",PRH_VAR_TRG_MODE_B"; + myquery << ",ACQ_BUILD_INFO"; + myquery << ",ACQ_VAR_INFO"; + myquery << ",RM_ACQ_AFTER_CALIB"; + myquery << ",RM_ACQ_SETTING_MODE"; + myquery << ",TRK_CALIB_USED"; + myquery << ",CAL_DSP_MASK"; + myquery << ",LAST_TIMESYNC"; + myquery << ",OBT_TIMESYNC"; + myquery << ",VALIDATION"; + myquery << ",INSERT_TIME"; + myquery << ") SELECT "; + myquery << "ID"; + myquery << ",ID_RUN_FRAG"; + myquery << ",ID_ROOT_L0"; + myquery << ",ID_ROOT_L2"; + myquery << ",RUNHEADER_TIME"; + myquery << ",RUNTRAILER_TIME"; + myquery << ",RUNHEADER_OBT"; + myquery << ",RUNTRAILER_OBT"; + myquery << ",RUNHEADER_PKT"; + myquery << ",RUNTRAILER_PKT"; + myquery << ",BOOT_NUMBER"; + myquery << ",EV_FROM"; + myquery << ",EV_TO"; + myquery << ",NEVENTS"; + myquery << ",PKT_COUNTER"; + myquery << ",PKT_READY_COUNTER"; + myquery << ",COMPILATIONTIMESTAMP"; + myquery << ",FAV_WRK_SCHEDULE"; + myquery << ",EFF_WRK_SCHEDULE"; + myquery << ",PRH_VAR_TRG_MODE_A"; + myquery << ",PRH_VAR_TRG_MODE_B"; + myquery << ",ACQ_BUILD_INFO"; + myquery << ",ACQ_VAR_INFO"; + myquery << ",RM_ACQ_AFTER_CALIB"; + myquery << ",RM_ACQ_SETTING_MODE"; + myquery << ",TRK_CALIB_USED"; + myquery << ",CAL_DSP_MASK"; + myquery << ",LAST_TIMESYNC"; + myquery << ",OBT_TIMESYNC"; + myquery << ",VALIDATION"; + myquery << ",INSERT_TIME"; + myquery << " FROM GL_RUN_TRASH "; + myquery << " WHERE BELONGED_TO='GL_RUN_FRAGMENTS' AND ID="; + myquery << (UInt_t)IDRUN << ";"; + // + // printf("5myquery is %s \n",myquery.str().c_str()); + // + dbc->Query(myquery.str().c_str()); + // + // + myquery.str(""); + myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID="; + myquery << IDRUN << ";"; + // + dbc->Query(myquery.str().c_str()); + // + return 0; +}; /** * Function to fill the GL_RUN table of the DB. @@ -992,6 +1265,8 @@ TSQLResult *pResult; TSQLRow *Row; stringstream myquery; + stringstream rname; + rname.str(""); // ---------------- myquery.str(""); myquery << "select "; @@ -1008,6 +1283,7 @@ stringstream fname; fname.str(""); fname << 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)); }; @@ -1029,20 +1305,63 @@ // UInt_t t0 = 0; // - 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 ){ + // + stringstream oss; + 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()); + 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()); + // + if ( !Row ){ + 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()); + pResult = dbc->Query(oss.str().c_str()); Row = pResult->Next(); - if ( Row ){ - // - t0 = (UInt_t)TDatime(Row->GetField(0)).Convert(); - // + 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(); + // // + // }; + // }; + // // // look for the correct timesync entry // @@ -1114,41 +1433,51 @@ * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK * */ -TString GL_TIMESYNC::ConvertTime(TString tzone, UInt_t dbt){ +TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){ // TDatime *time = new TDatime(); TString rtime; + Bool_t found = false; // - time->Set(dbt,false); // MSK = Moscow Winter Time + time->Set(dbt,false); // UTC (Coordinated Universal Time) // - if ( !strcmp(tzone.Data(),"UTC") || !strcmp(tzone.Data(),"GMT") ){ + if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){ // - UInt_t timeUTC = time->Convert() - 60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3 hs - time->Set(timeUTC,false); + 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*2; // CET (Central European Time) = Moscow Winter Time - 2 hs + 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*1; // CEST (Central European Summer Time) = Moscow Winter Time - 1 h + 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") ){ + if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){ // - UInt_t timeMSD = time->Convert() + 60*60*1; // MSD (Moscow Summer Time) = Moscow Winter Time + 1 h + 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); @@ -1162,42 +1491,137 @@ * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK * */ -TString GL_TIMESYNC::UnConvertTime(TString tzone, UInt_t dbt){ +TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){ // TDatime *time = new TDatime(); TString rtime; // - time->Set(dbt,false); // MSK = Moscow Winter Time + Bool_t found = false; + // + time->Set(dbt,false); // tzone // - if ( !strcmp(tzone.Data(),"UTC") || !strcmp(tzone.Data(),"GMT") ){ + if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){ // - UInt_t timeUTC = time->Convert() + 60*60*3; // UTC (Coordinated Universal Time) +3 hs = Moscow Winter Time + 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*2; // CET (Central European Time) + 2 hs = Moscow Winter Time + 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*1; // CEST (Central European Summer Time) + 1 h = Moscow Winter Time + 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") ){ + if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){ // - UInt_t timeMSD = time->Convert() - 60*60*1; // MSD (Moscow Summer Time) - 1 h = Moscow Winter Time + 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; +}