/[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.2 - (show annotations) (download)
Tue Nov 7 12:17:45 2006 UTC (18 years ago) by mocchiut
Branch: MAIN
CVS Tags: v2r03, v2r02
Changes since 1.1: +28 -11 lines
Time screw bug fixed, now everything in UTC

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

  ViewVC Help
Powered by ViewVC 1.1.23