/[PAMELA software]/YodaProfiler/src/R2-D2.cpp
ViewVC logotype

Contents of /YodaProfiler/src/R2-D2.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (show annotations) (download)
Tue Oct 31 15:36:05 2006 UTC (18 years, 1 month ago) by mocchiut
Branch: MAIN
CVS Tags: v2r01, v2r00
Force mode introduced together with other features, R2-D2 moved here, manual added

1 //
2 // C/C++ headers
3 //
4 #include <iostream>
5 #include <iomanip>
6 #include <fstream>
7 #include <sstream>
8 //
9 // ROOT headers
10 //
11 #include <TString.h>
12 #include <TSQLServer.h>
13 #include <TFile.h>
14 #include <TSystem.h>
15 //
16 // Detector's package headers
17 //
18 #include <GLTables.h>
19 //
20 using namespace std;
21 //
22 //
23 //
24 #include <YodaProfilerVerl2.h>
25 //
26 // Usage subroutine
27 //
28 void r2d2usage(){
29 printf("\nUsage:\n");
30 printf("\n R2-D2 [-h | --help] [--version] [-idRun ID_RUN] [-filename filename]\n");
31 printf("\n [-host host] [-user username] [-psw password] \n");
32 printf("\n --version print informations about compilation and exit\n");
33 printf("\n -h | --help print this help and exit \n");
34 printf("\n -v | --verbose be verbose [default: print nothing on STDOUT]\n");
35 printf("\n -idRun run ID_RUN: ID number of the run \n");
36 printf("\n -filename file output yoda filename \n");
37 printf("\n -host name for the host [default = $PAM_DBHOST or mysql://localhost/pamelaprod]\n");
38 printf("\n -user username for the DB [default = $PAM_DBUSER or \"anonymous\"] \n");
39 printf("\n -psw password for the DB [default = $PAM_DBPSW or \"\"]\n");
40 printf("\n -tzone timezone the time zone: UTC,GMT,MSK,MSD,CET,CEST are accepted \n");
41 printf("\n -convert dbtime convert the dbtime given in seconds (from the DB) to a string\n");
42 printf("\n -runat date returns run number which contains the given date,\n");
43 printf("\n for date use the SQL format \"yyyy-mm-dd hh:mm:ss\" \n");
44 printf("\n -runatDB time returns run number which contains the given DB time\n");
45 printf("\n -tsfile file yoda filename for the time sync (to be used with -obt)\n");
46 printf("\n -obt OBT OBT in ms returns a date (to be used with -tsfile)\n");
47 printf("\n -dumpTLEfor date save into file tle.txt the TLE for the given date,\n");
48 printf("\n for date use the SQL format \"yyyy-mm-dd hh:mm:ss\" \n");
49 printf("\nExamples: \n");
50 printf("\n R2-D2 -idRun 1085 \n");
51 printf("\n R2-D2 -filename DW_050208_00900.root \n");
52 printf("\n R2-D2 -idRun 1085 -filename DW_050208_00900.root \n");
53 };
54 //
55 // Here the main
56 //
57 int main(int numinp, char *inps[]){
58 //
59 // Variables booking
60 //
61 TString message;
62 Int_t error = 0;
63 //
64 UInt_t run = 0ULL;
65 //
66 TString filename = "";
67 //
68 TSQLServer *dbc = 0;
69 TString host = "mysql://localhost/pamelaprod";
70 TString user = "anonymous";
71 TString psw = "";
72 //
73 const char *pamdbhost=gSystem->Getenv("PAM_DBHOST");
74 const char *pamdbuser=gSystem->Getenv("PAM_DBUSER");
75 const char *pamdbpsw=gSystem->Getenv("PAM_DBPSW");
76 if ( !pamdbhost ) pamdbhost = "";
77 if ( !pamdbuser ) pamdbuser = "";
78 if ( !pamdbpsw ) pamdbpsw = "";
79 if ( strcmp(pamdbhost,"") ) host = pamdbhost;
80 if ( strcmp(pamdbuser,"") ) user = pamdbuser;
81 if ( strcmp(pamdbpsw,"") ) psw = pamdbpsw;
82 //
83 // printf(" host %s user %s psw %s \n",host.Data(),user.Data(),psw.Data());
84 //
85 TString tzone = "UTC";
86 TString runtime = "1970-01-01 00:00:00";
87 TString tletime = "1970-01-01 00:00:00";
88 UInt_t dbti = 0;
89 Bool_t convert = false;
90 Bool_t ruti = false;
91 Bool_t dtle = false;
92 Bool_t ruti2 = false;
93 Bool_t convobt = false;
94 Bool_t convobtts = false;
95 //
96 UInt_t runtime2 = 0;
97 UInt_t obt = 0;
98 TString tsfile = "";
99 //
100 TSQLResult *pResult;
101 TSQLRow *Row;
102 int t;
103 int r;
104 stringstream myquery;
105 //
106 // Checking input parameters
107 //
108 Int_t i = 0;
109 if ( numinp > 1 ){
110 while ( i < numinp ){
111 if ( !strcmp(inps[i],"--version") ){
112 YodaProfilerInfo(true);
113 exit(0);
114 };
115 if ( !strcmp(inps[i],"-h") || !strcmp(inps[i],"--help") ){
116 r2d2usage();
117 exit(0);
118 };
119 if ( !strcmp(inps[i],"-idRun") ) {
120 if ( numinp-1 < i+1 ) {
121 r2d2usage();
122 exit(-3);
123 };
124 run = (UInt_t)atoll(inps[i+1]);
125 };
126 if ( !strcmp(inps[i],"-filename") ) {
127 if ( numinp-1 < i+1 ){
128 r2d2usage();
129 exit(-3);
130 };
131 filename = (TString)inps[i+1];
132 };
133 if ( !strcmp(inps[i],"-host") ) {
134 if ( numinp-1 < i+1 ){
135 r2d2usage();
136 exit(-3);
137 };
138 host = (TString)inps[i+1];
139 };
140 if ( !strcmp(inps[i],"-user") ) {
141 if ( numinp-1 < i+1 ){
142 r2d2usage();
143 exit(-3);
144 };
145 user = (TString)inps[i+1];
146 };
147 if ( !strcmp(inps[i],"-psw") ) {
148 if ( numinp-1 < i+1 ){
149 r2d2usage();
150 exit(-3);
151 };
152 psw = (TString)inps[i+1];
153 };
154 //
155 if ( !strcmp(inps[i],"-tsfile") ) {
156 convobtts = true;
157 if ( numinp-1 < i+1 ){
158 r2d2usage();
159 exit(-3);
160 };
161 tsfile = (TString)inps[i+1];
162 };
163 //
164 if ( !strcmp(inps[i],"-obt") ) {
165 convobt = true;
166 if ( numinp-1 < i+1 ){
167 r2d2usage();
168 exit(-3);
169 };
170 obt = (UInt_t)atoll(inps[i+1]);
171 };
172 //
173 //
174 if ( !strcmp(inps[i],"-tzone") ) {
175 if ( numinp-1 < i+1 ){
176 r2d2usage();
177 exit(-3);
178 };
179 tzone = (TString)inps[i+1];
180 };
181 //
182 if ( !strcmp(inps[i],"-convert") ) {
183 convert = true;
184 if ( numinp-1 < i+1 ){
185 r2d2usage();
186 exit(-3);
187 };
188 dbti = (UInt_t)atoll(inps[i+1]);
189 };
190 //
191 if ( !strcmp(inps[i],"-runat") ) {
192 ruti = true;
193 if ( numinp-1 < i+1 ){
194 r2d2usage();
195 exit(-3);
196 };
197 runtime = (TString)inps[i+1];
198 };
199 //
200 if ( !strcmp(inps[i],"-dumpTLEfor") ) {
201 dtle = true;
202 if ( numinp-1 < i+1 ){
203 r2d2usage();
204 exit(-3);
205 };
206 tletime = (TString)inps[i+1];
207 };
208 //
209 //
210 if ( !strcmp(inps[i],"-runatDB") ) {
211 ruti2 = true;
212 if ( numinp-1 < i+1 ){
213 r2d2usage();
214 exit(-3);
215 };
216 runtime2 = (UInt_t)atoll(inps[i+1]);
217 };
218 //
219 i++;
220 };
221 //
222 } else {
223 //
224 // no input parameters exit with error, we need at least the run id.
225 //
226 r2d2usage();
227 exit(-2);
228 };
229 //
230 // Connect to the DB
231 //
232 dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
233 if( !dbc ) throw -2;
234 //
235 bool connect = dbc->IsConnected();
236 //
237 if( !connect ){
238 printf(" Error, not connected to DB\n");
239 exit(-1);
240 };
241 //
242 GL_ROOT *glroot = new GL_ROOT();
243 GL_RUN *glrun = new GL_RUN();
244 GL_TIMESYNC *dbtime = new GL_TIMESYNC();
245 //
246 // At which date correspond the DB time "dbti"?
247 //
248 if ( convert ){
249 printf("\n DB time %u is %s %s \n",dbti,dbtime->ConvertTime(tzone,dbti).Data(),tzone.Data());
250 };
251 //
252 //
253 //
254 if ( convobt && convobtts ){
255 UInt_t id = 0;
256 myquery.str("");
257 myquery << "select ";
258 myquery << " ID";
259 myquery << " from GL_ROOT where NAME=\"" << tsfile.Data() << "\";";
260 pResult = dbc->Query(myquery.str().c_str());
261 if ( pResult ){
262 Row = pResult->Next();
263 if ( Row ){
264 id = (UInt_t)atoll(Row->GetField(0));
265 delete pResult;
266 GL_TIMESYNC *ctime = new GL_TIMESYNC(id,"ID",dbc);
267 UInt_t abtime = ctime->DBabsTime(obt);
268 TString UTC="UTC";
269 TString thetime = dbtime->ConvertTime(tzone,abtime);
270 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());
271 delete ctime;
272 };
273 };
274 };
275 if ( (convobt && !convobtts) || (convobtts && !convobt) ){
276 printf("\n To convert a OBT to a date you must provide both OBT and file to be used for time sync \n");
277 };
278 //
279 // Which run contains the date "runtime"?
280 //
281 if ( ruti ){
282 //
283 TDatime *time = new TDatime(runtime.Data());
284 UInt_t dbti = time->Convert();
285 //
286 TString thetime = dbtime->UnConvertTime(tzone,dbti);
287 //
288 TDatime *time2 = new TDatime(thetime.Data());
289 UInt_t mytime = time2->Convert();
290 Bool_t found = false;
291 //
292 myquery.str("");
293 myquery << "select ";
294 myquery << " ID ";
295 myquery << " from GL_RUN where RUNHEADER_TIME<=" << mytime << " AND "
296 << " RUNTRAILER_TIME>=" << mytime << " ;";
297 // printf("myquery is %s \n",myquery.str().c_str());
298 pResult = dbc->Query(myquery.str().c_str());
299 for( r=0; r < 1000; r++){
300 Row = pResult->Next();
301 if( Row == NULL ) break;
302 found = true;
303 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)));
304 };
305 myquery.str("");
306 myquery << "select ";
307 myquery << " ID ";
308 myquery << " from GL_RUN_TRASH where BELONGED_TO='GL_RUN' AND RUNHEADER_TIME<=" << mytime << " AND "
309 << " RUNTRAILER_TIME>=" << mytime << " ;";
310 // printf("myquery is %s \n",myquery.str().c_str());
311 pResult = dbc->Query(myquery.str().c_str());
312 for( r=0; r < 1000; r++){
313 Row = pResult->Next();
314 if( Row == NULL ) break;
315 printf("\n Date %s %s (DB time %u ) is contained in run %u in the GL_RUN_TRASH table \n",runtime.Data(),tzone.Data(),mytime,(UInt_t)atoll(Row->GetField(0)));
316 found = true;
317 };
318 //
319 if ( !found ){
320 printf("\n No run contains date %s %s (DB time %u )\n",runtime.Data(),tzone.Data(),mytime);
321 };
322 //
323 };
324 //
325 // Which tle must be used for the date "tletime"?
326 //
327 if ( dtle ){
328 //
329 TDatime *time = new TDatime(tletime.Data());
330 UInt_t dbti = time->Convert();
331 //
332 TString thetime = dbtime->UnConvertTime(tzone,dbti);
333 //
334 TDatime *time2 = new TDatime(thetime.Data());
335 UInt_t mytime = time2->Convert();
336 //
337 myquery.str("");
338 myquery << " select ID,TLE1,TLE2,TLE3 from GL_TLE where FROM_TIME<='" << time2->AsSQLString() << "' ORDER BY FROM_TIME DESC LIMIT 1;";
339 // myquery << " from GL_TLE where FROM_TIME>=" << mytime << " ORDER BY FROM_TIME ASC LIMIT 1;";
340 // printf("myquery is %s \n",myquery.str().c_str());
341 pResult = dbc->Query(myquery.str().c_str());
342 Row = pResult->Next();
343 if ( !Row ){
344 printf("\n No TLE in the DB for date %s %s (DB time %u )\n",tletime.Data(),tzone.Data(),mytime);
345 };
346 printf("\n Date %s %s (DB time %u ) is contained in TLE %u \n",tletime.Data(),tzone.Data(),mytime,(UInt_t)atoll(Row->GetField(0)));
347 printf("\n%s\n",Row->GetField(1));
348 printf("%s\n",Row->GetField(2));
349 printf("%s\n",Row->GetField(3));
350 //
351 FILE *tlefile;
352 tlefile = fopen("tle.txt","w");
353 fprintf(tlefile,"%s\n",Row->GetField(1));
354 fprintf(tlefile,"%s\n",Row->GetField(2));
355 fprintf(tlefile,"%s\n",Row->GetField(3));
356 fclose (tlefile);
357 //
358 printf("\n TLE has been dumped in file tle.txt \n");
359 };
360 //
361 // Which run contains the dbtime "runtime2"?
362 //
363 if ( ruti2 ){
364 //
365 UInt_t mytime = runtime2;
366 //
367 Bool_t found = false;
368 myquery.str("");
369 myquery << "select ";
370 myquery << " ID ";
371 myquery << " from GL_RUN where RUNHEADER_TIME<=" << mytime << " AND "
372 << " RUNTRAILER_TIME>=" << mytime << " ;";
373 // printf("myquery is %s \n",myquery.str().c_str());
374 pResult = dbc->Query(myquery.str().c_str());
375 for( r=0; r < 1000; r++){
376 Row = pResult->Next();
377 if( Row == NULL ) break;
378 printf("\n DB time %u is contained in run %u \n",mytime,(UInt_t)atoll(Row->GetField(0)));
379 found = true;
380 };
381 //
382 myquery.str("");
383 myquery << "select ";
384 myquery << " ID ";
385 myquery << " from GL_RUN_TRASH where BELONGED_TO='GL_RUN' AND RUNHEADER_TIME<=" << mytime << " AND "
386 << " RUNTRAILER_TIME>=" << mytime << " ;";
387 // printf("myquery is %s \n",myquery.str().c_str());
388 pResult = dbc->Query(myquery.str().c_str());
389 for( r=0; r < 1000; r++){
390 Row = pResult->Next();
391 if( Row == NULL ) break;
392 printf("\n DB time %u is contained in run %u in the GL_RUN_TRASH table \n",mytime,(UInt_t)atoll(Row->GetField(0)));
393 found = true;
394 };
395 //
396 if ( !found ){
397 printf("\n No run contains DB time %u \n",mytime);
398 };
399 //
400 };
401 //
402 // To which file the run "run" belongs?
403 //
404 if ( run != 0 ){
405 Bool_t found = false;
406 glrun->Clear();
407 error = glrun->Query_GL_RUN(run,dbc);
408 glroot->Clear();
409 error = glroot->Query_GL_ROOT(glrun->ID_ROOT_L0,dbc);
410 if ( glrun->ID_ROOT_L0 ){
411 if ( error ){
412 printf(" Error querying the DB! \n");
413 exit(-4);
414 };
415 printf("\n Run %u belongs to file %s \n",run,(glroot->PATH+glroot->NAME).Data());
416 found = true;
417 };
418 //
419 myquery.str("");
420 myquery << "select ";
421 myquery << " ID,FILENAMEL0,FILENAMEL2 ";
422 myquery << " from GL_RUN_TRASH where BELONGED_TO='GL_RUN' AND ID=" << run << "; ";
423 pResult = dbc->Query(myquery.str().c_str());
424 for( r=0; r < 1000; r++){
425 Row = pResult->Next();
426 if( Row == NULL ) break;
427 printf("\n RUN %u has been deleted and now is contained in the GL_RUN_TRASH table \n",run);
428 printf("\n RUN %u belonged to L0 file %s and L2 file %s \n",run,Row->GetField(1),Row->GetField(2));
429 found = true;
430 };
431 //
432 if ( !found ) printf("\n No run with ID=%u in the DB!\n",run);
433 };
434 //
435 // Which runs are contained in the file "filename"?
436 //
437 if ( strcmp(filename.Data(),"") ){
438 // ----------------
439 Bool_t found = false;
440 Int_t ID = 0;
441 Int_t ID_RAW = 0;
442 //
443 const char *rawpath = "";
444 const char *rawname = "";
445 //
446 myquery.str("");
447 myquery << "select ";
448 myquery << " ID";
449 myquery << ",ID_RAW";
450 myquery << ",PATH";
451 myquery << ",NAME";
452 myquery << " from GL_ROOT where NAME=\"" << filename.Data() << "\";";
453 pResult = dbc->Query(myquery.str().c_str());
454 for( r=0; r < 1000; r++){
455 Row = pResult->Next();
456 if( Row == NULL ) break;
457 for( t = 0; t < pResult->GetFieldCount(); t++){
458 if(t==0) ID = atoi(Row->GetField(t));
459 if(t==1) ID_RAW = atoi(Row->GetField(t));
460 };
461 };
462 delete pResult;
463 if ( !ID && !ID_RAW ){
464 printf("\n No file with name %s in the DB!\n",filename.Data());
465 } else {
466 myquery.str("");
467 myquery << "select ";
468 myquery << " ID,RUNHEADER_TIME,RUNTRAILER_TIME ";
469 myquery << " from GL_RUN where ID_ROOT_L0=" << ID << ";";
470 pResult = dbc->Query(myquery.str().c_str());
471 for( r=0; r < 1000; r++){
472 Row = pResult->Next();
473 if( Row == NULL ) break;
474 found = true;
475 if ( !r ) printf("\n File %s contains the following runs: \n\n",filename.Data());
476 TString UTC="UTC";
477 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());
478 };
479 delete pResult;
480 myquery.str("");
481 myquery << "select ";
482 myquery << " ID,RUNHEADER_TIME,RUNTRAILER_TIME ";
483 myquery << " from GL_RUN_TRASH where BELONGED_TO='GL_RUN' AND FILENAMEL0='" << filename.Data() << "' ;";
484 pResult = dbc->Query(myquery.str().c_str());
485 for( r=0; r < 1000; r++){
486 Row = pResult->Next();
487 if( Row == NULL ) break;
488 if ( !r ) printf("\n File %s contains the following DELETED runs: \n\n",filename.Data());
489 TString UTC="UTC";
490 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());
491 };
492 //
493 if ( !found ){
494 printf("\n No run associated to the file %s \n",filename.Data());
495 };
496 myquery.str("");
497 myquery << "select ";
498 myquery << " PATH,NAME";
499 myquery << " from GL_RAW where ID=" << ID_RAW << ";";
500 pResult = dbc->Query(myquery.str().c_str());
501 for( r=0; r < 1000; r++){
502 Row = pResult->Next();
503 if( Row == NULL ) break;
504 for( t = 0; t < pResult->GetFieldCount(); t++){
505 if(t==0) rawpath = Row->GetField(t);
506 if(t==1) rawname = Row->GetField(t);
507 };
508 };
509 delete pResult;
510 printf("\n File %s belongs to raw data file %s/%s \n",filename.Data(),rawpath,rawname);
511 };
512 };
513 //
514 // Close the DB connection
515 //
516 if ( dbc ) dbc->Close();
517 //
518 printf("\n");
519 //
520 exit(0);
521 }

  ViewVC Help
Powered by ViewVC 1.1.23