--- DarthVader/src/R2-D2.cpp 2006/06/14 09:53:26 1.2 +++ DarthVader/src/R2-D2.cpp 2006/09/25 11:49:39 1.10 @@ -30,11 +30,17 @@ printf("\n --version print informations about compilation and exit\n"); printf("\n -h | --help print this help and exit \n"); printf("\n -v | --verbose be verbose [default: print nothing on STDOUT]\n"); - printf("\n -idRun ID_RUN: ID number of the run \n"); - printf("\n -filename output yoda filename \n"); - printf("\n -host name for the host [default = mysql://localhost/pamelaprod]\n"); - printf("\n -user username for the DB [default = anonymous] \n"); - printf("\n -psw password for the DB [default = \"\"]\n"); + printf("\n -idRun run ID_RUN: ID number of the run \n"); + printf("\n -filename file output yoda filename \n"); + printf("\n -host name for the host [default = $PAM_DBHOST or mysql://localhost/pamelaprod]\n"); + printf("\n -user username for the DB [default = $PAM_DBUSER or \"anonymous\"] \n"); + printf("\n -psw password for the DB [default = $PAM_DBPSW or \"\"]\n"); + printf("\n -tzone timezone the time zone: UTC,GMT,MSK,MSD,CET,CEST are accepted \n"); + printf("\n -convert dbtime convert the dbtime given in seconds (from the DB) to a string\n"); + printf("\n -runat time returns run number which contains the given date,\n"); + printf("\n for time use the SQL format \"yyyy-mm-dd hh:mm:ss\" \n"); + printf("\n -tsfile file yoda filename for the time sync (to be used with -obt)\n"); + printf("\n -obt OBT OBT in ms returns a date (to be used with -tsfile)\n"); printf("\nExamples: \n"); printf("\n R2-D2 -idRun 1085 \n"); printf("\n R2-D2 -filename DW_050208_00900.root \n"); @@ -50,7 +56,7 @@ TString message; Int_t error = 0; // - ULong64_t run = 0ULL; + UInt_t run = 0ULL; // TString filename = ""; // @@ -59,6 +65,34 @@ TString user = "anonymous"; TString psw = ""; // + const char *pamdbhost=gSystem->Getenv("PAM_DBHOST"); + const char *pamdbuser=gSystem->Getenv("PAM_DBUSER"); + const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW"); + if ( !pamdbhost ) pamdbhost = ""; + if ( !pamdbuser ) pamdbuser = ""; + if ( !pamdbpsw ) pamdbpsw = ""; + if ( strcmp(pamdbhost,"") ) host = pamdbhost; + if ( strcmp(pamdbuser,"") ) user = pamdbuser; + if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw; + // + // printf(" host %s user %s psw %s \n",host.Data(),user.Data(),psw.Data()); + // + TString tzone = "UTC"; + TString runtime = "1970-01-01 00:00:00"; + UInt_t dbti = 0; + Bool_t convert = false; + Bool_t ruti = false; + Bool_t convobt = false; + Bool_t convobtts = false; + // + UInt_t obt = 0; + TString tsfile = ""; + // + TSQLResult *pResult; + TSQLRow *Row; + int t; + int r; + stringstream myquery; // // Checking input parameters // @@ -78,8 +112,7 @@ r2d2usage(); exit(-3); }; - char *pEnd; - run = strtoull(inps[i+1],&pEnd,0); + run = (UInt_t)atoll(inps[i+1]); }; if ( !strcmp(inps[i],"-filename") ) { if ( numinp-1 < i+1 ){ @@ -109,6 +142,52 @@ }; psw = (TString)inps[i+1]; }; + // + if ( !strcmp(inps[i],"-tsfile") ) { + convobtts = true; + if ( numinp-1 < i+1 ){ + r2d2usage(); + exit(-3); + }; + tsfile = (TString)inps[i+1]; + }; + // + if ( !strcmp(inps[i],"-obt") ) { + convobt = true; + if ( numinp-1 < i+1 ){ + r2d2usage(); + exit(-3); + }; + obt = (UInt_t)atoll(inps[i+1]); + }; + // + // + if ( !strcmp(inps[i],"-tzone") ) { + if ( numinp-1 < i+1 ){ + r2d2usage(); + exit(-3); + }; + tzone = (TString)inps[i+1]; + }; + // + if ( !strcmp(inps[i],"-convert") ) { + convert = true; + if ( numinp-1 < i+1 ){ + r2d2usage(); + exit(-3); + }; + dbti = (UInt_t)atoll(inps[i+1]); + }; + // + if ( !strcmp(inps[i],"-runat") ) { + ruti = true; + if ( numinp-1 < i+1 ){ + r2d2usage(); + exit(-3); + }; + runtime = (TString)inps[i+1]; + }; + // i++; }; // @@ -134,22 +213,91 @@ // GL_ROOT *glroot = new GL_ROOT(); GL_RUN *glrun = new GL_RUN(); + GL_TIMESYNC *dbtime = new GL_TIMESYNC(); + // + // At which date correspond the DB time "dbti"? + // + if ( convert ){ + printf("\n DB time %u is %s %s \n",dbti,dbtime->ConvertTime(tzone,dbti).Data(),tzone.Data()); + }; + // + // + // + if ( convobt && convobtts ){ + UInt_t id = 0; + myquery.str(""); + myquery << "select "; + myquery << " ID"; + myquery << " from GL_ROOT where NAME=\"" << tsfile.Data() << "\";"; + pResult = dbc->Query(myquery.str().c_str()); + if ( pResult ){ + Row = pResult->Next(); + if ( Row ){ + id = (UInt_t)atoll(Row->GetField(0)); + delete pResult; + GL_TIMESYNC *ctime = new GL_TIMESYNC(id,"ID",dbc); + UInt_t abtime = ctime->DBabsTime(obt); + TString UTC="UTC"; + TString thetime = dbtime->ConvertTime(tzone,abtime); + printf("\n OBT %u in the file %s corresponds to DBtime %u and date %s %s \n",obt,tsfile.Data(),abtime,thetime.Data(),tzone.Data()); + delete ctime; + }; + }; + }; + if ( (convobt && !convobtts) || (convobtts && !convobt) ){ + printf("\n To convert a OBT to a date you must provide both OBT and file to be used for time sync \n"); + }; + // + // Which run contains the date "runtime"? + // + if ( ruti ){ + // + TDatime *time = new TDatime(runtime.Data()); + UInt_t dbti = time->Convert(); + // + TString thetime = dbtime->UnConvertTime(tzone,dbti); + // + TDatime *time2 = new TDatime(thetime.Data()); + UInt_t mytime = time2->Convert(); + // + myquery.str(""); + myquery << "select "; + myquery << " ID "; + myquery << " from GL_RUN where RUNHEADER_TIME<=" << mytime << " AND " + << " RUNTRAILER_TIME>=" << mytime << " ;"; + // printf("myquery is %s \n",myquery.str().c_str()); + pResult = dbc->Query(myquery.str().c_str()); + for( r=0; r < 1000; r++){ + Row = pResult->Next(); + if ( !r && !Row ){ + printf("\n No run contains date %s %s (DB time %u )\n",runtime.Data(),tzone.Data(),mytime); + }; + if( Row == NULL ) break; + printf("\n Date %s %s (DB time %u ) is contained in run %u \n",runtime.Data(),tzone.Data(),mytime,(UInt_t)atoll(Row->GetField(0))); + }; + }; // - if ( run != 0ULL ){ + // To which file the run "run" belongs? + // + if ( run != 0 ){ + glrun->Clear(); error = glrun->Query_GL_RUN(run,dbc); - error = glroot->Query_GL_ROOT(glrun->ID_REG_RUN,dbc); - if ( error ){ - printf(" Error querying the DB! \n"); - exit(-4); + glroot->Clear(); + error = glroot->Query_GL_ROOT(glrun->ID_ROOT_L0,dbc); + if ( !glrun->ID_ROOT_L0 ){ + printf("\n No run with ID=%u in the DB!\n",run); + } else { + if ( error ){ + printf(" Error querying the DB! \n"); + exit(-4); + }; + printf("\n Run %u belongs to file %s \n",run,(glroot->PATH+glroot->NAME).Data()); }; - printf("\n Run %llu belongs to file %s \n",run,(glroot->PATH+glroot->NAME).Data()); }; + // + // Which runs are contained in the file "filename"? + // if ( strcmp(filename.Data(),"") ){ - TSQLResult *pResult; - TSQLRow *Row; - int t; - int r; - stringstream myquery; // ---------------- Int_t ID = 0; Int_t ID_RAW = 0; @@ -165,7 +313,6 @@ myquery << ",NAME"; myquery << " from GL_ROOT where NAME=\"" << filename.Data() << "\";"; pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount()) return (-51); for( r=0; r < 1000; r++){ Row = pResult->Next(); if( Row == NULL ) break; @@ -175,37 +322,41 @@ }; }; delete pResult; - printf("\n File %s contains the following runs: \n",filename.Data()); - myquery.str(""); - myquery << "select "; - myquery << " ID"; - myquery << " from GL_RUN where ID_REG_RUN=" << ID << ";"; - pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount()) return (-51); - for( r=0; r < 1000; r++){ - Row = pResult->Next(); - if( Row == NULL ) break; - for( t = 0; t < pResult->GetFieldCount(); t++){ - if (t==0) printf(" => %i \n",atoi(Row->GetField(t))); + if ( !ID && !ID_RAW ){ + printf("\n No file with name %s in the DB!\n",filename.Data()); + } else { + myquery.str(""); + myquery << "select "; + myquery << " ID,RUNHEADER_TIME,RUNTRAILER_TIME "; + myquery << " from GL_RUN where ID_ROOT_L0=" << ID << ";"; + pResult = dbc->Query(myquery.str().c_str()); + for( r=0; r < 1000; r++){ + Row = pResult->Next(); + if ( !r && !Row ){ + printf("\n No run associated to the file %s \n",filename.Data()); + }; + if( Row == NULL ) break; + if ( !r ) printf("\n File %s contains the following runs: \n\n",filename.Data()); + TString UTC="UTC"; + printf(" => ID = %i _-_-_ the run started at %s UTC ended at %s UTC \n\n",(UInt_t)atoll(Row->GetField(0)),dbtime->ConvertTime(UTC,(UInt_t)atoll(Row->GetField(1))).Data(),dbtime->ConvertTime(UTC,(UInt_t)atoll(Row->GetField(2))).Data()); }; - }; - delete pResult; - myquery.str(""); - myquery << "select "; - myquery << " PATH,NAME"; - myquery << " from GL_RAW where ID=" << ID_RAW << ";"; - pResult = dbc->Query(myquery.str().c_str()); - if(!pResult->GetRowCount()) return (-51); - for( r=0; r < 1000; r++){ - Row = pResult->Next(); - if( Row == NULL ) break; - for( t = 0; t < pResult->GetFieldCount(); t++){ - if(t==0) rawpath = Row->GetField(t); - if(t==1) rawname = Row->GetField(t); + delete pResult; + myquery.str(""); + myquery << "select "; + myquery << " PATH,NAME"; + myquery << " from GL_RAW where ID=" << ID_RAW << ";"; + pResult = dbc->Query(myquery.str().c_str()); + for( r=0; r < 1000; r++){ + Row = pResult->Next(); + if( Row == NULL ) break; + for( t = 0; t < pResult->GetFieldCount(); t++){ + if(t==0) rawpath = Row->GetField(t); + if(t==1) rawname = Row->GetField(t); + }; }; + delete pResult; + printf("\n File %s belongs to raw data file %s/%s \n",filename.Data(),rawpath,rawname); }; - delete pResult; - printf(" and belongs to raw data file %s/%s \n",rawpath,rawname); }; // // Close the DB connection