| 1 | // | 
| 2 | // C/C++ headers | 
| 3 | // | 
| 4 | #include <iostream> | 
| 5 | #include <sstream> | 
| 6 | // | 
| 7 | // ROOT headers | 
| 8 | // | 
| 9 | #include <TString.h> | 
| 10 | #include <TSQLServer.h> | 
| 11 | #include <TFile.h> | 
| 12 | #include <TSystem.h> | 
| 13 | // | 
| 14 | // Detector's package headers | 
| 15 | // | 
| 16 | #include <GLTables.h> | 
| 17 | // | 
| 18 | using namespace std; | 
| 19 | // | 
| 20 | // | 
| 21 | // | 
| 22 | #include <DarthVaderVerl2.h> | 
| 23 | // | 
| 24 | // Usage subroutine | 
| 25 | // | 
| 26 | void r2d2usage(){ | 
| 27 | printf("\nUsage:\n"); | 
| 28 | printf("\n DarthVader [-h | --help] [--version] [-idRun ID_RUN] [-filename filename]\n"); | 
| 29 | printf("\n            [-host host] [-user username] [-psw password] \n"); | 
| 30 | printf("\n --version       print informations about compilation and exit\n"); | 
| 31 | printf("\n -h | --help     print this help and exit \n"); | 
| 32 | printf("\n -v | --verbose  be verbose [default: print nothing on STDOUT]\n"); | 
| 33 | printf("\n -idRun          ID_RUN: ID number of the run \n"); | 
| 34 | printf("\n -filename       output yoda filename \n"); | 
| 35 | printf("\n -host           name for the host [default = $PAM_DBHOST or mysql://localhost/pamelaprod]\n"); | 
| 36 | printf("\n -user           username for the DB [default = $PAM_DBUSER or \"anonymous\"] \n"); | 
| 37 | printf("\n -psw            password for the DB [default = $PAM_DBPSW or \"\"]\n"); | 
| 38 | printf("\n -convert [ -tzone timezone -dbtime dbtime ] \n"); | 
| 39 | printf("\n                 convert the dbtime given in seconds (from the DB) to a string for\n"); | 
| 40 | printf("\n                 the given time zone (UTC,GMT,MSK,MSD,CET,CEST are accepted) \n"); | 
| 41 | printf("\n -runat [ -tzone timezone -time \"yyyy-mm-dd hh:mm:ss\" ] \n"); | 
| 42 | printf("\n                 returns run number which contains the given date in the given time zone \n"); | 
| 43 | printf("\nExamples: \n"); | 
| 44 | printf("\n R2-D2 -idRun 1085 \n"); | 
| 45 | printf("\n R2-D2 -filename DW_050208_00900.root \n"); | 
| 46 | printf("\n R2-D2 -idRun 1085 -filename DW_050208_00900.root \n"); | 
| 47 | }; | 
| 48 | // | 
| 49 | // Here the main | 
| 50 | // | 
| 51 | int main(int numinp, char *inps[]){ | 
| 52 | // | 
| 53 | // Variables booking | 
| 54 | // | 
| 55 | TString message; | 
| 56 | Int_t error = 0; | 
| 57 | // | 
| 58 | UInt_t run = 0ULL; | 
| 59 | // | 
| 60 | TString filename = ""; | 
| 61 | // | 
| 62 | TSQLServer *dbc = 0; | 
| 63 | TString host = "mysql://localhost/pamelaprod"; | 
| 64 | TString user = "anonymous"; | 
| 65 | TString psw = ""; | 
| 66 | // | 
| 67 | const char *pamdbhost=gSystem->Getenv("PAM_DBHOST"); | 
| 68 | const char *pamdbuser=gSystem->Getenv("PAM_DBUSER"); | 
| 69 | const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW"); | 
| 70 | if ( strcmp(pamdbhost,"") ) host = pamdbhost; | 
| 71 | if ( strcmp(pamdbuser,"") ) user = pamdbuser; | 
| 72 | if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw; | 
| 73 | // | 
| 74 | //  printf(" host %s user %s psw %s \n",host.Data(),user.Data(),psw.Data()); | 
| 75 | // | 
| 76 | TString tzone = "MSK"; | 
| 77 | TString tzone2 = "MSK"; | 
| 78 | TString runtime = "1970-01-01 00:00:00"; | 
| 79 | UInt_t dbti = 0; | 
| 80 | Bool_t convert = false; | 
| 81 | Bool_t ruti = false; | 
| 82 | // | 
| 83 | TSQLResult *pResult; | 
| 84 | TSQLRow *Row; | 
| 85 | int t; | 
| 86 | int r; | 
| 87 | stringstream myquery; | 
| 88 | // | 
| 89 | // Checking input parameters | 
| 90 | // | 
| 91 | Int_t i = 0; | 
| 92 | if ( numinp > 1 ){ | 
| 93 | while ( i < numinp ){ | 
| 94 | if ( !strcmp(inps[i],"--version") ){ | 
| 95 | DarthVaderInfo(true); | 
| 96 | exit(0); | 
| 97 | }; | 
| 98 | if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){ | 
| 99 | r2d2usage(); | 
| 100 | exit(0); | 
| 101 | }; | 
| 102 | if ( !strcmp(inps[i],"-idRun") ) { | 
| 103 | if ( numinp-1 < i+1 ) { | 
| 104 | r2d2usage(); | 
| 105 | exit(-3); | 
| 106 | }; | 
| 107 | run = (UInt_t)atoll(inps[i+1]); | 
| 108 | }; | 
| 109 | if ( !strcmp(inps[i],"-filename") ) { | 
| 110 | if ( numinp-1 < i+1 ){ | 
| 111 | r2d2usage(); | 
| 112 | exit(-3); | 
| 113 | }; | 
| 114 | filename = (TString)inps[i+1]; | 
| 115 | }; | 
| 116 | if ( !strcmp(inps[i],"-host") ) { | 
| 117 | if ( numinp-1 < i+1 ){ | 
| 118 | r2d2usage(); | 
| 119 | exit(-3); | 
| 120 | }; | 
| 121 | host = (TString)inps[i+1]; | 
| 122 | }; | 
| 123 | if ( !strcmp(inps[i],"-user") ) { | 
| 124 | if ( numinp-1 < i+1 ){ | 
| 125 | r2d2usage(); | 
| 126 | exit(-3); | 
| 127 | }; | 
| 128 | user = (TString)inps[i+1]; | 
| 129 | }; | 
| 130 | if ( !strcmp(inps[i],"-psw") ) { | 
| 131 | if ( numinp-1 < i+1 ){ | 
| 132 | r2d2usage(); | 
| 133 | exit(-3); | 
| 134 | }; | 
| 135 | psw = (TString)inps[i+1]; | 
| 136 | }; | 
| 137 | // | 
| 138 | if ( !strcmp(inps[i],"-convert") ) { | 
| 139 | convert = true; | 
| 140 | if ( numinp >= i+2 && !strcmp(inps[i+1],"[") ){ | 
| 141 | if ( numinp < i+2 ){ | 
| 142 | r2d2usage(); | 
| 143 | throw -3; | 
| 144 | }; | 
| 145 | i += 2; | 
| 146 | while ( strcmp(inps[i],"]") ){ | 
| 147 | if ( !strcmp(inps[i],"-tzone") ) { | 
| 148 | if ( numinp-1 < i+1 ){ | 
| 149 | r2d2usage(); | 
| 150 | exit(-3); | 
| 151 | }; | 
| 152 | tzone = (TString)inps[i+1]; | 
| 153 | }; | 
| 154 | if ( !strcmp(inps[i],"-dbtime") ) { | 
| 155 | if ( numinp-1 < i+1 ){ | 
| 156 | r2d2usage(); | 
| 157 | exit(-3); | 
| 158 | }; | 
| 159 | dbti = (UInt_t)atoll(inps[i+1]); | 
| 160 | }; | 
| 161 | i++; | 
| 162 | if ( i > numinp-1 ){ | 
| 163 | r2d2usage(); | 
| 164 | throw -3; | 
| 165 | }; | 
| 166 | }; | 
| 167 | }; | 
| 168 | }; | 
| 169 | // | 
| 170 | if ( !strcmp(inps[i],"-runat") ) { | 
| 171 | ruti = true; | 
| 172 | if ( numinp >= i+2 && !strcmp(inps[i+1],"[") ){ | 
| 173 | if ( numinp < i+2 ){ | 
| 174 | r2d2usage(); | 
| 175 | throw -3; | 
| 176 | }; | 
| 177 | i += 2; | 
| 178 | while ( strcmp(inps[i],"]") ){ | 
| 179 | if ( !strcmp(inps[i],"-tzone") ) { | 
| 180 | if ( numinp-1 < i+1 ){ | 
| 181 | r2d2usage(); | 
| 182 | exit(-3); | 
| 183 | }; | 
| 184 | tzone2 = (TString)inps[i+1]; | 
| 185 | }; | 
| 186 | if ( !strcmp(inps[i],"-time") ) { | 
| 187 | if ( numinp-1 < i+1 ){ | 
| 188 | r2d2usage(); | 
| 189 | exit(-3); | 
| 190 | }; | 
| 191 | runtime = (TString)inps[i+1]; | 
| 192 | }; | 
| 193 | i++; | 
| 194 | if ( i > numinp-1 ){ | 
| 195 | r2d2usage(); | 
| 196 | throw -3; | 
| 197 | }; | 
| 198 | }; | 
| 199 | }; | 
| 200 | }; | 
| 201 | // | 
| 202 | i++; | 
| 203 | }; | 
| 204 | // | 
| 205 | } else { | 
| 206 | // | 
| 207 | // no input parameters exit with error, we need at least the run id. | 
| 208 | // | 
| 209 | r2d2usage(); | 
| 210 | exit(-2); | 
| 211 | }; | 
| 212 | // | 
| 213 | // Connect to the DB | 
| 214 | // | 
| 215 | dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data()); | 
| 216 | if( !dbc ) throw -2; | 
| 217 | // | 
| 218 | bool connect = dbc->IsConnected(); | 
| 219 | // | 
| 220 | if( !connect ){ | 
| 221 | printf(" Error, not connected to DB\n"); | 
| 222 | exit(-1); | 
| 223 | }; | 
| 224 | // | 
| 225 | GL_ROOT *glroot = new GL_ROOT(); | 
| 226 | GL_RUN *glrun = new GL_RUN(); | 
| 227 | GL_TIMESYNC *dbtime = new GL_TIMESYNC(); | 
| 228 | // | 
| 229 | // At which date correspond the DB time "dbti"? | 
| 230 | // | 
| 231 | if ( convert ){ | 
| 232 | printf("\n DB time %u is %s %s \n",dbti,dbtime->ConvertTime(tzone.Data(),dbti).Data(),tzone.Data()); | 
| 233 | }; | 
| 234 | // | 
| 235 | // Which run contains the date "runtime"? | 
| 236 | // | 
| 237 | if ( ruti ){ | 
| 238 | // | 
| 239 | TDatime *time = new TDatime(runtime.Data()); | 
| 240 | UInt_t dbti = time->Convert(); | 
| 241 | // | 
| 242 | TString thetime = dbtime->UnConvertTime(tzone2.Data(),dbti); | 
| 243 | // | 
| 244 | TDatime *time2 = new TDatime(thetime.Data()); | 
| 245 | UInt_t mytime = time2->Convert(); | 
| 246 | // | 
| 247 | myquery.str(""); | 
| 248 | myquery << "select "; | 
| 249 | myquery << " ID "; | 
| 250 | myquery << " from GL_RUN where RUNHEADER_TIME<=" << mytime << " AND " | 
| 251 | << " RUNTRAILER_TIME>=" << mytime  << " ;"; | 
| 252 | //    printf("myquery is %s \n",myquery.str().c_str()); | 
| 253 | pResult = dbc->Query(myquery.str().c_str()); | 
| 254 | for( r=0; r < 1000; r++){ | 
| 255 | Row = pResult->Next(); | 
| 256 | if ( !r && !Row ){ | 
| 257 | printf("\n No run contains date %s %s (DB time %u )\n",runtime.Data(),tzone2.Data(),mytime); | 
| 258 | }; | 
| 259 | if( Row == NULL ) break; | 
| 260 | printf("\n Date %s %s (DB time %u ) is contained in run %u \n",runtime.Data(),tzone2.Data(),mytime,(UInt_t)atoll(Row->GetField(0))); | 
| 261 | }; | 
| 262 | }; | 
| 263 | // | 
| 264 | // To which file the run "run" belongs? | 
| 265 | // | 
| 266 | if ( run != 0 ){ | 
| 267 | glrun->Clear(); | 
| 268 | error = glrun->Query_GL_RUN(run,dbc); | 
| 269 | glroot->Clear(); | 
| 270 | error = glroot->Query_GL_ROOT(glrun->ID_ROOT_L0,dbc); | 
| 271 | if ( !glrun->ID_ROOT_L0 ){ | 
| 272 | printf("\n No run with ID=%u in the DB!\n",run); | 
| 273 | } else { | 
| 274 | if ( error ){ | 
| 275 | printf(" Error querying the DB! \n"); | 
| 276 | exit(-4); | 
| 277 | }; | 
| 278 | printf("\n Run %u belongs to file %s \n",run,(glroot->PATH+glroot->NAME).Data()); | 
| 279 | }; | 
| 280 | }; | 
| 281 | // | 
| 282 | // Which runs are contained in the file "filename"? | 
| 283 | // | 
| 284 | if ( strcmp(filename.Data(),"") ){ | 
| 285 | // ---------------- | 
| 286 | Int_t ID = 0; | 
| 287 | Int_t ID_RAW = 0; | 
| 288 | // | 
| 289 | const char *rawpath = ""; | 
| 290 | const char *rawname = ""; | 
| 291 | // | 
| 292 | myquery.str(""); | 
| 293 | myquery << "select "; | 
| 294 | myquery << " ID"; | 
| 295 | myquery << ",ID_RAW"; | 
| 296 | myquery << ",PATH"; | 
| 297 | myquery << ",NAME"; | 
| 298 | myquery << " from GL_ROOT where NAME=\"" << filename.Data() << "\";"; | 
| 299 | pResult = dbc->Query(myquery.str().c_str()); | 
| 300 | for( r=0; r < 1000; r++){ | 
| 301 | Row = pResult->Next(); | 
| 302 | if( Row == NULL ) break; | 
| 303 | for( t = 0; t < pResult->GetFieldCount(); t++){ | 
| 304 | if(t==0) ID     = atoi(Row->GetField(t)); | 
| 305 | if(t==1) ID_RAW = atoi(Row->GetField(t)); | 
| 306 | }; | 
| 307 | }; | 
| 308 | delete pResult; | 
| 309 | if ( !ID && !ID_RAW ){ | 
| 310 | printf("\n No file with name %s in the DB!\n",filename.Data()); | 
| 311 | } else { | 
| 312 | myquery.str(""); | 
| 313 | myquery << "select "; | 
| 314 | myquery << " ID,RUNHEADER_TIME,RUNTRAILER_TIME "; | 
| 315 | myquery << " from GL_RUN where ID_ROOT_L0=" << ID << ";"; | 
| 316 | pResult = dbc->Query(myquery.str().c_str()); | 
| 317 | for( r=0; r < 1000; r++){ | 
| 318 | Row = pResult->Next(); | 
| 319 | if ( !r && !Row ){ | 
| 320 | printf("\n No run associated to the file %s \n",filename.Data()); | 
| 321 | }; | 
| 322 | if( Row == NULL ) break; | 
| 323 | if ( !r ) printf("\n File %s contains the following runs: \n\n",filename.Data()); | 
| 324 | 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()); | 
| 325 | }; | 
| 326 | delete pResult; | 
| 327 | myquery.str(""); | 
| 328 | myquery << "select "; | 
| 329 | myquery << " PATH,NAME"; | 
| 330 | myquery << " from GL_RAW where ID=" << ID_RAW << ";"; | 
| 331 | pResult = dbc->Query(myquery.str().c_str()); | 
| 332 | for( r=0; r < 1000; r++){ | 
| 333 | Row = pResult->Next(); | 
| 334 | if( Row == NULL ) break; | 
| 335 | for( t = 0; t < pResult->GetFieldCount(); t++){ | 
| 336 | if(t==0) rawpath = Row->GetField(t); | 
| 337 | if(t==1) rawname = Row->GetField(t); | 
| 338 | }; | 
| 339 | }; | 
| 340 | delete pResult; | 
| 341 | printf("\n File %s belongs to raw data file %s/%s \n",filename.Data(),rawpath,rawname); | 
| 342 | }; | 
| 343 | }; | 
| 344 | // | 
| 345 | // Close the DB connection | 
| 346 | // | 
| 347 | if ( dbc ) dbc->Close(); | 
| 348 | // | 
| 349 | printf("\n"); | 
| 350 | // | 
| 351 | exit(0); | 
| 352 | } |