/[PAMELA software]/chewbacca/YodaProfiler/src/GLTables.cpp
ViewVC logotype

Contents of /chewbacca/YodaProfiler/src/GLTables.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.15 - (show annotations) (download)
Tue Nov 29 13:42:18 2011 UTC (13 years, 1 month ago) by pam-fi
Branch: MAIN
Changes since 1.14: +13 -0 lines
More fixes for memory leaks.

1 /**
2 * \file GLTables.cpp
3 * \author Elena Vannuccini
4 *
5 * The file contains implementation of the methods to query the DB.
6 */
7 //
8 #include <sstream>
9 #include <iostream>
10 #include <limits.h>
11 //
12 #include <TFile.h>
13 #include <TTree.h>
14 #include <TTimeStamp.h>
15 #include <EventHeader.h>
16 #include <PscuHeader.h>
17 //
18 #include <GLTables.h>
19 #include <sgp4.h>
20 //
21 ClassImp(Q2TH);
22 ClassImp(GL_TABLES);
23 ClassImp(GL_TRK_CALIB);
24 ClassImp(GL_RUN);
25 ClassImp(GL_ROOT);
26 ClassImp(GL_PARAM);
27 ClassImp(GL_S4_CALIB);
28 ClassImp(GL_CALO_CALIB);
29 ClassImp(GL_CALOPULSE_CALIB);
30 ClassImp(GL_TIMESYNC);
31 ClassImp(GL_TLE);
32 //
33 using namespace std;
34
35 Q2TH::Q2TH(TString host, TString user, TString psw){
36 this->Open(host,user,psw);
37 };
38
39 void Q2TH::Open(TString host, TString user, TString psw){
40 fh = gSystem->ExpandPathName(host.Data());
41 fu = gSystem->ExpandPathName(user.Data());
42 fp = gSystem->ExpandPathName(psw.Data());
43 printf(" Connecting to DB %s \n",fh.Data());
44 dbc = TSQLServer::Connect(fh.Data(),fu.Data(),fp.Data());
45 if ( dbc && dbc->IsConnected() ){
46 printf(" connected! \n");
47 } else {
48 printf(" ERROR! not connected... :( \n");
49 };
50 };
51
52 TObject *Q2TH::Draw(TString query, Bool_t verbose, TString hname){
53 //
54 if ( !strcmp(query.Data(),"help") ){
55 printf(" USAGE: \n");
56 printf(" 1) start root and create Q2TH object with \n");
57 printf(" Q2TH *qt = new Q2TH() \n");
58 printf(" or \n");
59 printf(" Q2TH *qt = new Q2TH(\"mysql://srvg-g2-01.ts.infn.it/pamelaProcessing9_TS\",\"pamelaprod_ro\",\"mypassword\") \n");
60 printf(" 2) query the DB with \n");
61 printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\"); \n");
62 printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",true); this will print numbers on screen \n");
63 printf(" qt->Draw(\"select REAL_TIME_INIT from ROOT_TABLE_MERGING;\",true,\"myhisto\"); this will print numbers on screen and create histo \"myhisto\"\n");
64 printf(" 3) to use your own THxD create it and then query the DB giving as argument the name of histo: \n");
65 printf(" TH2D *myhisto=new TH2D(\"myhisto\",\"myhisto\",5000,1140000000.,1240000000.,10000,0.,1.) \n");
66 printf(" qt->Draw(\"select REAL_TIME_INIT,BAD_PKT_PERCENTAGE from ROOT_TABLE_MERGING;\",false,\"myhisto\")\n\n\n");
67
68 return NULL;
69 };
70 //
71 if (Row)
72 delete Row;
73 pResult = dbc->Query(query.Data());
74 //
75 Row = pResult->Next();
76 //
77 Int_t dim = pResult->GetFieldCount();
78 if ( dim < 1 || dim > 2 ){
79 printf(" Dim == %i not supported yet \n",dim);
80 return NULL;
81 };
82 //
83 TH1D *h1 = NULL;
84 TH2D *h2 = NULL;
85 Double_t f1 = 0.;
86 Double_t minf1 = numeric_limits<Double_t>::max();
87 Double_t maxf1 = numeric_limits<Double_t>::min();
88 Double_t f2 = 0.;
89 Double_t minf2 = numeric_limits<Double_t>::max();
90 Double_t maxf2 = numeric_limits<Double_t>::min();
91 //
92 while ( Row ){
93 f1 = (Double_t)atof(Row->GetField(0));
94 if ( f1 > maxf1 ) maxf1 = f1;
95 if ( f1 < minf1 ) minf1 = f1;
96 if ( dim == 2 ){
97 f2 = (Double_t)atof(Row->GetField(1));
98 if ( f2 > maxf2 ) maxf2 = f2;
99 if ( f2 < minf2 ) minf2 = f2;
100
101 };
102 if (Row)
103 delete Row;
104 Row = pResult->Next();
105 };
106 pResult->Delete();
107 //
108
109 //
110 Int_t f1bin = 70;
111 Int_t f2bin = 70;
112 if ( dim == 1 ){
113 f1bin = int((maxf1-minf1)/1000.);
114 if ( f1bin < 70 ) f1bin = 70;
115 if ( f1bin > 1000 ) f1bin = 1000;
116 if ( !strcmp(hname.Data(),"q2th") ) hname += "1";
117 // h1 = dynamic_cast<TH1D*>(gDirectory->FindObject(hname.Data()));
118 h1 = (TH1D*)(gDirectory->FindObject(hname.Data()));
119 if ( !strcmp(hname.Data(),"q2th1") ){
120 if ( h1 ) h1->Delete();
121 };
122 if ( !h1 ) h1 = new TH1D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02);
123 // h1->SetBit(TH1::kCanRebin);
124 if ( verbose ) printf("\n\n Row %s \n",pResult->GetFieldName(0));
125 };
126 if ( dim == 2 ){
127 f2bin = int((maxf2-minf2)/1000.);
128 if ( f2bin < 70 ) f2bin = 70;
129 if ( f2bin > 1000 ) f2bin = 1000;
130 if ( !strcmp(hname.Data(),"q2th") ) hname += "2";
131 // h2 = dynamic_cast<TH2D*>(gDirectory->FindObject(hname.Data()));
132 h2 = (TH2D*)(gDirectory->FindObject(hname.Data()));
133 if ( !strcmp(hname.Data(),"q2th2") ){
134 if ( h2 ) h2->Delete();
135 };
136 if ( !h2 ) h2 = new TH2D(hname.Data(),hname.Data(),f1bin,minf1*0.98,maxf1*1.02,f2bin,minf2*0.98,maxf2*1.02);
137 // h2->SetBit(TH2::kCanRebin);
138 if ( verbose ) printf("\n\n Row %s %s \n",pResult->GetFieldName(0),pResult->GetFieldName(1));
139 };
140 //
141 pResult = dbc->Query(query.Data());
142 //
143 if (Row)
144 delete Row;
145 Row = pResult->Next();
146 //
147 Int_t r = 0;
148 //
149 while ( Row ){
150 f1 = (Double_t)atof(Row->GetField(0));
151 if ( dim == 1 ){
152 if ( verbose ) printf(" %i %f \n",r,f1);
153 h1->Fill(f1);
154 } else {
155 f2 = (Double_t)atof(Row->GetField(1));
156 if ( verbose ) printf(" %i %f %f \n",r,f1,f2);
157 h2->Fill(f1,f2);
158 };
159 r++;
160 if (Row)
161 delete Row;
162 Row = pResult->Next();
163 };
164 //
165 TCanvas *c = NULL;
166 TString cname = Form("%sc",hname.Data());
167 // c = dynamic_cast<TCanvas*>(gDirectory->FindObject(hname.Data()));
168 c = (TCanvas*)(gDirectory->FindObject(cname.Data()));
169 if ( !c ) c = new TCanvas(Form("%sc",cname.Data()));
170 c->Clear();
171 c->cd();
172 if ( dim == 1 ) h1->Draw();
173 if ( dim == 2 ) h2->Draw();
174 //
175 if (Row)
176 delete Row;
177 pResult->Delete();
178 if ( dim == 1 ) return h1;
179 if ( dim == 2 ) return h2;
180 //
181 return NULL;
182 };
183
184 GL_TABLES::GL_TABLES(){
185 };
186
187 GL_TABLES::GL_TABLES(TString host, TString user, TString psw){
188 fHost = new TString(host.Data());
189 fUser = new TString(user.Data());
190 fPsw = new TString(psw.Data());
191 fSet = true;
192 fNquery = 0;
193 mh = host.Data();
194 mu = user.Data();
195 mp = psw.Data();
196 };
197
198
199 void GL_TABLES::Set(TString host, TString user, TString psw){
200 fHost = new TString(host.Data());
201 fUser = new TString(user.Data());
202 fPsw = new TString(psw.Data());
203 fSet = true;
204 fNquery = 0;
205 mh = host.Data();
206 mu = user.Data();
207 mp = psw.Data();
208 };
209
210 //Bool_t GL_TABLES::IsConnected(TSQLServer *&dbc){
211 Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){
212 //
213 //
214 //
215 if ( !fSet ){
216 return true;
217 };
218 //
219 // printf(" host is %s \n",fHost->Data());
220 //
221 stringstream myquery;
222 myquery.str("");
223 myquery << "show databases;";
224 if ( dbc ){
225 if ( dbc->IsConnected() ){
226 dbc->Query(myquery.str().c_str());
227 fNquery++;
228 if ( !(dbc->GetErrorCode()) ){
229 // printf("ok\n");
230 //
231 // if ( (dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006) ){
232 // if ( !(dbc->GetErrorCode()) ){
233 // is connected
234 return true;
235 // };
236 };
237 };
238 };
239 //
240 if ( true ) {
241 //
242 printf(" WARNING: Lost connection to DB! try to recover... \n");
243 //
244 TString host = fHost->Data();
245 TString user = fUser->Data();
246 TString psw = fPsw->Data();
247 if ( dbc ){
248 dbc->Close();
249 delete dbc;
250 dbc = 0;
251 };
252 dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
253 //
254 myquery.str("");
255 myquery << "show databases;";
256 dbc->Query(myquery.str().c_str());
257 fNquery++;
258 // if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){
259 if ( !(dbc->GetErrorCode()) ){
260 //
261 printf(" ...connection recovered, I can continue! \n");
262 //
263 myquery.str("");
264 myquery << "SET time_zone='+0:00'";
265 dbc->Query(myquery.str().c_str());
266 fNquery++;
267 myquery.str("");
268 myquery << "SET wait_timeout=173000;";
269 dbc->Query(myquery.str().c_str());
270 fNquery++;
271 return true;
272 };
273 };
274 //
275 printf(" GLTABLES: connection is gone away, query will fail\n");
276 //
277 return false;
278 //
279 };
280
281 void GL_TABLES::ResetCounters(){
282 fNquery = 0;
283 };
284
285 void GL_TABLES::AddQ(){
286 if ( fSet ) fNquery++;
287 };
288
289 UInt_t GL_TABLES::GetNqueries(){
290 UInt_t rn = 0;
291 rn += (UInt_t&)fNquery;
292 return(rn);
293 };
294
295 GL_RUN::GL_RUN() {
296 ID = 0;
297 ID_RUN_FRAG = 0;
298 ID_ROOT_L0 = 0;
299 ID_ROOT_L2 = 0;
300 RUNHEADER_TIME = 0;
301 RUNTRAILER_TIME = 0;
302 EV_FROM = 0;
303 EV_TO = 0;
304 TRK_CALIB_USED = 0;
305 EFF_WRK_SCHEDULE = 0;
306 PRH_VAR_TRG_MODE_A = 0;
307 PRH_VAR_TRG_MODE_B = 0;
308 ACQ_BUILD_INFO = 0;
309 ACQ_VAR_INFO = 0;
310 RUNHEADER_OBT = 0;
311 RUNTRAILER_OBT = 0;
312 RUNHEADER_PKT = 0;
313 RUNTRAILER_PKT = 0;
314 NEVENTS = 0;
315 LAST_TIMESYNC = 0;
316 OBT_TIMESYNC = 0;
317 COMPILATIONTIMESTAMP = 0;
318 FAV_WRK_SCHEDULE = 0;
319 RM_ACQ_AFTER_CALIB = 0;
320 RM_ACQ_SETTING_MODE = 0;
321 PKT_COUNTER = 0;
322 PKT_READY_COUNTER = 0;
323 TRK_CALIB_USED = 0;
324 CAL_DSP_MASK = 0;
325 BOOT_NUMBER = 0;
326 PHYSENDRUN_MASK_S3S2S12 = 0;
327 PHYSENDRUN_MASK_S11CRC = 0;
328 VALIDATION = 0;
329 }
330
331
332 void GL_RUN::Clear(Option_t *t) {
333 ID = 0;
334 ID_RUN_FRAG = 0;
335 ID_ROOT_L0 = 0;
336 ID_ROOT_L2 = 0;
337 RUNHEADER_TIME = 0;
338 RUNTRAILER_TIME = 0;
339 EV_FROM = 0;
340 EV_TO = 0;
341 TRK_CALIB_USED = 0;
342 EFF_WRK_SCHEDULE = 0;
343 PRH_VAR_TRG_MODE_A = 0;
344 PRH_VAR_TRG_MODE_B = 0;
345 ACQ_BUILD_INFO = 0;
346 ACQ_VAR_INFO = 0;
347 RUNHEADER_OBT = 0;
348 RUNTRAILER_OBT = 0;
349 RUNHEADER_PKT = 0;
350 RUNTRAILER_PKT = 0;
351 NEVENTS = 0;
352 LAST_TIMESYNC = 0;
353 OBT_TIMESYNC = 0;
354 COMPILATIONTIMESTAMP = 0;
355 FAV_WRK_SCHEDULE = 0;
356 RM_ACQ_AFTER_CALIB = 0;
357 RM_ACQ_SETTING_MODE = 0;
358 PKT_COUNTER = 0;
359 PKT_READY_COUNTER = 0;
360 TRK_CALIB_USED = 0;
361 CAL_DSP_MASK = 0;
362 BOOT_NUMBER = 0;
363 PHYSENDRUN_MASK_S3S2S12 = 0;
364 PHYSENDRUN_MASK_S11CRC = 0;
365 VALIDATION = 0;
366 }
367
368 GL_ROOT::GL_ROOT(){
369 ID = 0;
370 ID_RAW = 0;
371 ID_TIMESYNC = 0;
372 PATH = "";
373 NAME = "";
374 }
375
376 GL_RAW::GL_RAW(){
377 ID = 0;
378 PATH = "";
379 NAME = "";
380 BOOT_NUMBER = 0;
381 }
382
383 GL_PARAM::GL_PARAM(){
384 ID = 0;
385 PATH = "";
386 NAME = "";
387 DESCR = "";
388 FROM_TIME = 0;
389 TO_TIME = 0;
390 TYPE = 0;
391 }
392
393
394 GL_TRK_CALIB::GL_TRK_CALIB(){
395 ID = 0;
396 ID_ROOT_L0 = 0;
397 EV_ROOT_CALIBTRK1 = 0;
398 EV_ROOT_CALIBTRK2 = 0;
399 FROM_TIME = 0;
400 TO_TIME = 0;
401 OBT1 = 0;
402 OBT2 = 0;
403 PKT1 = 0;
404 PKT2 = 0;
405 BOOT_NUMBER = 0;
406 VALIDATION = 0;
407 }
408
409 GL_CALO_CALIB::GL_CALO_CALIB(){
410 ID = 0;
411 ID_ROOT_L0 = 0;
412 EV_ROOT = 0;
413 FROM_TIME = 0;
414 TO_TIME = 0;
415 SECTION = 0;
416 OBT = 0;
417 PKT = 0;
418 BOOT_NUMBER = 0;
419 VALIDATION = 0;
420 }
421
422 GL_CALOPULSE_CALIB::GL_CALOPULSE_CALIB(){
423 ID = 0;
424 ID_ROOT_L0 = 0;
425 EV_ROOT = 0;
426 FROM_TIME = 0;
427 TO_TIME = 0;
428 SECTION = 0;
429 PULSED_STRIP = 0;
430 PULSE_AMPLITUDE = 0;
431 OBT = 0;
432 PKT = 0;
433 BOOT_NUMBER = 0;
434 VALIDATION = 0;
435 }
436
437 GL_S4_CALIB::GL_S4_CALIB(){
438 ID = 0;
439 ID_ROOT_L0 = 0;
440 EV_ROOT = 0;
441 FROM_TIME = 0;
442 TO_TIME = 0;
443 OBT = 0;
444 PKT = 0;
445 BOOT_NUMBER = 0;
446 }
447
448 GL_TIMESYNC::GL_TIMESYNC(){
449 obtfirst = 0;
450 pktfirst = 0;
451 ID_RESURS_OFFSET = 0;
452 ID = 0;
453 ID_RAW = 0;
454 OBT0 = 0;
455 TIMESYNC = 0;
456 TYPE = 0;
457 }
458
459 // ****************************************************
460
461 void GL_RUN::SetEV_FROM(UInt_t evfrom){
462 EV_FROM = evfrom;
463 };
464
465 void GL_RUN::SetEV_TO(UInt_t evto){
466 EV_TO = evto;
467 };
468
469 void GL_RUN::SetNEVENTS(UInt_t nev){
470 NEVENTS = nev;
471 };
472
473 void GL_RUN::SetBOOTNUMBER(UInt_t boot){
474 BOOT_NUMBER = boot;
475 };
476
477 void GL_RUN::SetRUNHEADER_TIME(UInt_t absth){
478 RUNHEADER_TIME = absth;
479 };
480
481 void GL_RUN::SetRUNTRAILER_TIME(UInt_t abstt){
482 RUNTRAILER_TIME = abstt;
483 };
484
485 void GL_RUN::SetRUNHEADER_PKT(UInt_t absth){
486 RUNHEADER_PKT = absth;
487 };
488
489 void GL_RUN::SetRUNTRAILER_PKT(UInt_t abstt){
490 RUNTRAILER_PKT = abstt;
491 };
492
493 void GL_RUN::SetRUNHEADER_OBT(UInt_t absth){
494 RUNHEADER_OBT = absth;
495 };
496
497 void GL_RUN::SetRUNTRAILER_OBT(UInt_t abstt){
498 RUNTRAILER_OBT = abstt;
499 };
500
501 void GL_RUN::SetID_ROOT_L2(UInt_t idl2){
502 ID_ROOT_L2 = idl2;
503 };
504
505 void GL_RUN::SetID_ROOT_L0(UInt_t idroot){
506 ID_ROOT_L0 = idroot;
507 };
508
509 void GL_RUN::SetID_RUN_FRAG(UInt_t idfrag){
510 ID_RUN_FRAG = idfrag;
511 };
512
513 void GL_RUN::SetVALIDATION(UInt_t valid){
514 VALIDATION = valid;
515 };
516
517 void GL_RUN::SetLAST_TIMESYNC(UInt_t ts){
518 LAST_TIMESYNC = ts;
519 };
520
521 void GL_RUN::SetOBT_TIMESYNC(UInt_t ts){
522 OBT_TIMESYNC = ts;
523 };
524
525 void GL_RUN:: SetPKT_COUNTER(UInt_t value){
526 PKT_COUNTER = value;
527 };
528
529 void GL_RUN:: SetPKT_READY_COUNTER(UInt_t value){
530 PKT_READY_COUNTER = value;
531 };
532
533 void GL_RUN:: SetCOMPILATIONTIMESTAMP(UInt_t value){
534 COMPILATIONTIMESTAMP = value;
535 };
536
537 void GL_RUN:: SetPHYSENDRUN_MASK_S3S2S12(UInt_t value){
538 PHYSENDRUN_MASK_S3S2S12 = value;
539 };
540
541 void GL_RUN:: SetPHYSENDRUN_MASK_S11CRC(UInt_t value){
542 PHYSENDRUN_MASK_S11CRC = value;
543 };
544
545
546 void GL_RUN:: SetFAV_WRK_SCHEDULE(UInt_t value){
547 FAV_WRK_SCHEDULE = value;
548 };
549
550 void GL_RUN:: SetEFF_WRK_SCHEDULE(UInt_t value){
551 EFF_WRK_SCHEDULE = value;
552 };
553
554 void GL_RUN:: SetPRH_VAR_TRG_MODE_A(UInt_t value){
555 PRH_VAR_TRG_MODE_A = value;
556 };
557
558 void GL_RUN:: SetPRH_VAR_TRG_MODE_B(UInt_t value){
559 PRH_VAR_TRG_MODE_B = value;
560 };
561
562 void GL_RUN:: SetACQ_BUILD_INFO(UInt_t value){
563 ACQ_BUILD_INFO = value;
564 };
565
566 void GL_RUN:: SetACQ_VAR_INFO(UInt_t value){
567 ACQ_VAR_INFO = value;
568 };
569
570 void GL_RUN:: SetRM_ACQ_AFTER_CALIB(UInt_t value){
571 RM_ACQ_AFTER_CALIB = value;
572 };
573
574 void GL_RUN:: SetRM_ACQ_SETTING_MODE(UInt_t value){
575 RM_ACQ_SETTING_MODE = value;
576 };
577
578 void GL_RUN:: SetTRK_CALIB_USED(UInt_t value){
579 TRK_CALIB_USED = value;
580 };
581
582 void GL_RUN:: SetCAL_DSP_MASK(UInt_t value){
583 CAL_DSP_MASK = value;
584 };
585
586 void GL_RUN:: SetID(UInt_t value){
587 ID = value;
588 };
589
590 void GL_RUN::Set_GL_RUNT(RunTrailerEvent *runt, PscuHeader *pht){
591 PKT_COUNTER = runt->PKT_COUNTER;
592 PKT_READY_COUNTER = runt->PKT_ReadyCounter;
593 RUNTRAILER_OBT = pht->GetOrbitalTime();
594 RUNTRAILER_PKT = pht->GetCounter();
595 };
596
597 void GL_RUN::Set_GL_RUNH(RunHeaderEvent *runh, PscuHeader *phh){
598 TRK_CALIB_USED = runh->TRK_CALIB_USED;
599 PRH_VAR_TRG_MODE_A = runh->PRH_VAR_TRIGGER_MODE_A;
600 PRH_VAR_TRG_MODE_B = runh->PRH_VAR_TRIGGER_MODE_B;
601 ACQ_BUILD_INFO = runh->ACQ_BUILD_INFO;
602 ACQ_VAR_INFO = runh->ACQ_VAR_INFO;
603 RUNHEADER_OBT = phh->GetOrbitalTime();
604 RUNHEADER_PKT = phh->GetCounter();
605 LAST_TIMESYNC = runh->LAST_TIME_SYNC_INFO;
606 OBT_TIMESYNC = runh->OBT_TIME_SYNC;
607 COMPILATIONTIMESTAMP = runh->COMPILATIONTIMESTAMP;
608 FAV_WRK_SCHEDULE = runh->FAVOURITE_WORKING_SCHEDULE;
609 EFF_WRK_SCHEDULE = runh->EFFECTIVE_WORKING_SCHEDULE;
610 RM_ACQ_AFTER_CALIB = runh->RM_ACQ_AFTER_CALIB;
611 RM_ACQ_SETTING_MODE = runh->RM_ACQ_SETTING_MODE;
612 TRK_CALIB_USED = runh->TRK_CALIB_USED;
613 CAL_DSP_MASK = runh->CAL_DSP_MASK;
614 };
615
616 void GL_RUN::Set_GL_RUNT0(){
617 PKT_COUNTER = 0;
618 PKT_READY_COUNTER = 0;
619 RUNTRAILER_OBT = 0;
620 RUNTRAILER_PKT = 0;
621 };
622
623 void GL_RUN::Set_GL_RUNH0(){
624 TRK_CALIB_USED = 0;
625 PRH_VAR_TRG_MODE_A = 0;
626 PRH_VAR_TRG_MODE_B = 0;
627 ACQ_BUILD_INFO = 0;
628 ACQ_VAR_INFO = 0;
629 RUNHEADER_OBT = 0;
630 RUNHEADER_PKT = 0;
631 LAST_TIMESYNC = 0;
632 OBT_TIMESYNC = 0;
633 COMPILATIONTIMESTAMP = 0;
634 FAV_WRK_SCHEDULE = 0;
635 EFF_WRK_SCHEDULE = 0;
636 RM_ACQ_AFTER_CALIB = 0;
637 RM_ACQ_SETTING_MODE = 0;
638 TRK_CALIB_USED = 0;
639 CAL_DSP_MASK = 0;
640 };
641
642 void GL_RUN::Set_GL_RUN(TSQLRow *Row){
643 for( Int_t t = 0; t < 32; t++){
644 if (t== 0) ID = (UInt_t)atoll(Row->GetField(t));
645 if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t));
646 if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
647 if (t== 3) ID_ROOT_L2 = (UInt_t)atoll(Row->GetField(t));
648 if (t== 4) RUNHEADER_TIME = (UInt_t)atoll(Row->GetField(t));
649 if (t== 5) RUNTRAILER_TIME = (UInt_t)atoll(Row->GetField(t));
650 if (t== 6) RUNHEADER_OBT = (UInt_t)atoll(Row->GetField(t));
651 if (t== 7) RUNTRAILER_OBT = (UInt_t)atoll(Row->GetField(t));
652 if (t== 8) RUNHEADER_PKT = (UInt_t)atoll(Row->GetField(t));
653 if (t== 9) RUNTRAILER_PKT = (UInt_t)atoll(Row->GetField(t));
654 if (t==10) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t));
655 if (t==11) EV_FROM = (UInt_t)atoll(Row->GetField(t));
656 if (t==12) EV_TO = (UInt_t)atoll(Row->GetField(t));
657 if (t==13) NEVENTS = (UInt_t)atoll(Row->GetField(t));
658 if (t==14) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t));
659 if (t==15) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));
660 if (t==16) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));
661 if (t==17) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
662 if (t==18) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
663 if (t==19) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t));
664 if (t==20) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t));
665 if (t==21) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t));
666 if (t==22) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t));
667 if (t==23) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t));
668 if (t==24) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t));
669 if (t==25) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t));
670 if (t==26) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t));
671 if (t==27) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
672 if (t==28) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
673 if (t==29) PHYSENDRUN_MASK_S3S2S12 = (UInt_t)atoll(Row->GetField(t));
674 if (t==30) PHYSENDRUN_MASK_S11CRC = (UInt_t)atoll(Row->GetField(t));
675 if (t==31) VALIDATION = (UInt_t)atoll(Row->GetField(t));
676 };
677
678 }
679
680 /**
681 * This method delete the run from the "FromTable" table and store it in the GL_RUN_TRASH table
682 * If IDRUN is 0 "this->ID" run is used.
683 *
684 **/
685 Int_t GL_RUN::DeleteRun(TSQLServer *dbc,UInt_t IDRUN,TString FromTable){
686 // MySQL variables
687 TSQLResult *pResult;
688 TSQLRow *Row = NULL;
689 stringstream myquery;
690 //
691 if ( !IDRUN ) IDRUN = ID;
692 if ( !IDRUN ) return 1;
693 // ----------------
694 myquery.str("");
695 myquery << " INSERT INTO GL_RUN_TRASH (";
696 myquery << "ID";
697 myquery << ",ID_RUN_FRAG";
698 myquery << ",ID_ROOT_L0";
699 myquery << ",ID_ROOT_L2";
700 myquery << ",RUNHEADER_TIME";
701 myquery << ",RUNTRAILER_TIME";
702 myquery << ",RUNHEADER_OBT";
703 myquery << ",RUNTRAILER_OBT";
704 myquery << ",RUNHEADER_PKT";
705 myquery << ",RUNTRAILER_PKT";
706 myquery << ",BOOT_NUMBER";
707 myquery << ",EV_FROM";
708 myquery << ",EV_TO";
709 myquery << ",NEVENTS";
710 myquery << ",PKT_COUNTER";
711 myquery << ",PKT_READY_COUNTER";
712 myquery << ",COMPILATIONTIMESTAMP";
713 myquery << ",FAV_WRK_SCHEDULE";
714 myquery << ",EFF_WRK_SCHEDULE";
715 myquery << ",PRH_VAR_TRG_MODE_A";
716 myquery << ",PRH_VAR_TRG_MODE_B";
717 myquery << ",ACQ_BUILD_INFO";
718 myquery << ",ACQ_VAR_INFO";
719 myquery << ",RM_ACQ_AFTER_CALIB";
720 myquery << ",RM_ACQ_SETTING_MODE";
721 myquery << ",TRK_CALIB_USED";
722 myquery << ",CAL_DSP_MASK";
723 myquery << ",LAST_TIMESYNC";
724 myquery << ",OBT_TIMESYNC";
725 myquery << ",PHYSENDRUN_MASK_S3S2S12";
726 myquery << ",PHYSENDRUN_MASK_S11CRC";
727 myquery << ",VALIDATION";
728 myquery << ",INSERT_TIME";
729 myquery << ") SELECT * FROM ";
730 myquery << FromTable.Data();
731 myquery << " WHERE ID=";
732 myquery << (UInt_t)IDRUN << ";";
733 //
734 // printf("1myquery is %s \n",myquery.str().c_str());
735 //
736 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
737 this->GetGLTABLES()->AddQ();
738 dbc->Query(myquery.str().c_str());
739 //
740 // retrieve this ID_TRASH
741 //
742 myquery.str("");
743 myquery << " SELECT ID_TRASH,ID_ROOT_L0,ID_ROOT_L2 FROM GL_RUN_TRASH ORDER BY ID_TRASH DESC LIMIT 1";
744 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
745 this->GetGLTABLES()->AddQ();
746 pResult = dbc->Query(myquery.str().c_str());
747 //
748 UInt_t idtrash = 0;
749 UInt_t idl0 = 0;
750 UInt_t idl2 = 0;
751 //
752 if (Row)
753 delete Row;
754 Row = pResult->Next();
755 if( Row != NULL ){
756 idtrash = (UInt_t)atoll(Row->GetField(0));
757 idl0 = (UInt_t)atoll(Row->GetField(1));
758 idl2 = (UInt_t)atoll(Row->GetField(2));
759 };
760 //
761 TString fileL0 = "";
762 TString fileL2 = "";
763 myquery.str("");
764 myquery << " SELECT NAME FROM GL_ROOT WHERE ID=";
765 myquery << idl0 << ";";
766 //
767 // printf("2myquery is %s \n",myquery.str().c_str());
768 //
769 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
770 this->GetGLTABLES()->AddQ();
771 pResult = dbc->Query(myquery.str().c_str());
772 //
773 if (Row)
774 delete Row;
775 Row = pResult->Next();
776 if( Row != NULL ){
777 fileL0 = (TString)Row->GetField(0);
778 };
779 //
780 //
781 //
782 myquery.str("");
783 myquery << " SELECT NAME FROM GL_ROOT WHERE ID=";
784 myquery << idl2 << ";";
785 //
786 // printf("3myquery is %s \n",myquery.str().c_str());
787 //
788 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
789 this->GetGLTABLES()->AddQ();
790 pResult = dbc->Query(myquery.str().c_str());
791 //
792 if (Row)
793 delete Row;
794 Row = pResult->Next();
795 if( Row != NULL ){
796 fileL2 = (TString)Row->GetField(0);
797 }
798 if (Row){
799 delete Row;
800 Row = NULL; // This variable is not used below
801 }
802 //
803 //
804 //
805 myquery.str("");
806 myquery << " UPDATE GL_RUN_TRASH SET FILENAMEL0='";
807 myquery << fileL0.Data() << "' where ID_TRASH=";
808 myquery << idtrash << ";";
809 //
810 // printf("4myquery is %s \n",myquery.str().c_str());
811 //
812 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
813 this->GetGLTABLES()->AddQ();
814 dbc->Query(myquery.str().c_str());
815 //
816 myquery.str("");
817 myquery << " UPDATE GL_RUN_TRASH SET FILENAMEL2='";
818 myquery << fileL2.Data() << "' where ID_TRASH=";
819 myquery << idtrash << ";";
820 //
821 // printf("4myquery is %s \n",myquery.str().c_str());
822 //
823 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
824 this->GetGLTABLES()->AddQ();
825 dbc->Query(myquery.str().c_str());
826 //
827 myquery.str("");
828 myquery << " UPDATE GL_RUN_TRASH SET BELONGED_TO='";
829 myquery << FromTable.Data() << "' where ID_TRASH=";
830 myquery << idtrash << ";";
831 //
832 // printf("4myquery is %s \n",myquery.str().c_str());
833 //
834 //
835 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
836 this->GetGLTABLES()->AddQ();
837 dbc->Query(myquery.str().c_str());
838 //
839 myquery.str("");
840 myquery << " DELETE FROM ";
841 myquery << FromTable.Data() << " where ID=";
842 myquery << IDRUN << ";";
843 //
844 // printf("5myquery is %s \n",myquery.str().c_str());
845 //
846 //
847 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
848 this->GetGLTABLES()->AddQ();
849 dbc->Query(myquery.str().c_str());
850 //
851 return 0;
852 };
853
854
855
856 /**
857 * This method restore a run from the GL_RUN_TRASH table.
858 * If ID is 0 "this->ID" is used; if "ToTable" is empty BELONG_TO field of GL_RUN_TRASH is used.
859 *
860 **/
861 Int_t GL_RUN::RestoreRun(TSQLServer *dbc,UInt_t IDRUN,TString ToTable){
862 // insert into GL_RUN_FRAGMENTS select * FROM GL_RUN where ID=11;
863 //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;
864 // MySQL variables
865 TSQLResult *pResult;
866 TSQLRow *Row = NULL;
867 stringstream myquery;
868 //
869 if ( !IDRUN ) IDRUN = ID;
870 if ( !IDRUN ) return 1;
871 //
872 if ( !strcmp(ToTable.Data(),"") ){
873 //
874 myquery.str("");
875 myquery << " SELECT BELONGED_TO FROM GL_RUN_TRASH WHERE ID=";
876 myquery << (UInt_t)IDRUN << ";";
877 //
878 printf(" qui? myquery is %s \n",myquery.str().c_str());
879 //
880 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
881 this->GetGLTABLES()->AddQ();
882 pResult = dbc->Query(myquery.str().c_str());
883 //
884 Row = pResult->Next();
885 if( Row != NULL ){
886 ToTable = (TString)Row->GetField(0);
887 } else {
888 return 1;
889 };
890 };
891 // ----------------
892 myquery.str("");
893 myquery << " INSERT INTO ";
894 myquery << ToTable.Data();
895 myquery << " (";
896 myquery << "ID";
897 myquery << ",ID_RUN_FRAG";
898 myquery << ",ID_ROOT_L0";
899 myquery << ",ID_ROOT_L2";
900 myquery << ",RUNHEADER_TIME";
901 myquery << ",RUNTRAILER_TIME";
902 myquery << ",RUNHEADER_OBT";
903 myquery << ",RUNTRAILER_OBT";
904 myquery << ",RUNHEADER_PKT";
905 myquery << ",RUNTRAILER_PKT";
906 myquery << ",BOOT_NUMBER";
907 myquery << ",EV_FROM";
908 myquery << ",EV_TO";
909 myquery << ",NEVENTS";
910 myquery << ",PKT_COUNTER";
911 myquery << ",PKT_READY_COUNTER";
912 myquery << ",COMPILATIONTIMESTAMP";
913 myquery << ",FAV_WRK_SCHEDULE";
914 myquery << ",EFF_WRK_SCHEDULE";
915 myquery << ",PRH_VAR_TRG_MODE_A";
916 myquery << ",PRH_VAR_TRG_MODE_B";
917 myquery << ",ACQ_BUILD_INFO";
918 myquery << ",ACQ_VAR_INFO";
919 myquery << ",RM_ACQ_AFTER_CALIB";
920 myquery << ",RM_ACQ_SETTING_MODE";
921 myquery << ",TRK_CALIB_USED";
922 myquery << ",CAL_DSP_MASK";
923 myquery << ",LAST_TIMESYNC";
924 myquery << ",OBT_TIMESYNC";
925 myquery << ",PHYSENDRUN_MASK_S3S2S12";
926 myquery << ",PHYSENDRUN_MASK_S11CRC";
927 myquery << ",VALIDATION";
928 myquery << ",INSERT_TIME";
929 myquery << ") SELECT ";
930 myquery << "ID";
931 myquery << ",ID_RUN_FRAG";
932 myquery << ",ID_ROOT_L0";
933 myquery << ",ID_ROOT_L2";
934 myquery << ",RUNHEADER_TIME";
935 myquery << ",RUNTRAILER_TIME";
936 myquery << ",RUNHEADER_OBT";
937 myquery << ",RUNTRAILER_OBT";
938 myquery << ",RUNHEADER_PKT";
939 myquery << ",RUNTRAILER_PKT";
940 myquery << ",BOOT_NUMBER";
941 myquery << ",EV_FROM";
942 myquery << ",EV_TO";
943 myquery << ",NEVENTS";
944 myquery << ",PKT_COUNTER";
945 myquery << ",PKT_READY_COUNTER";
946 myquery << ",COMPILATIONTIMESTAMP";
947 myquery << ",FAV_WRK_SCHEDULE";
948 myquery << ",EFF_WRK_SCHEDULE";
949 myquery << ",PRH_VAR_TRG_MODE_A";
950 myquery << ",PRH_VAR_TRG_MODE_B";
951 myquery << ",ACQ_BUILD_INFO";
952 myquery << ",ACQ_VAR_INFO";
953 myquery << ",RM_ACQ_AFTER_CALIB";
954 myquery << ",RM_ACQ_SETTING_MODE";
955 myquery << ",TRK_CALIB_USED";
956 myquery << ",CAL_DSP_MASK";
957 myquery << ",LAST_TIMESYNC";
958 myquery << ",OBT_TIMESYNC";
959 myquery << ",PHYSENDRUN_MASK_S3S2S12";
960 myquery << ",PHYSENDRUN_MASK_S11CRC";
961 myquery << ",VALIDATION";
962 myquery << ",INSERT_TIME";
963 myquery << " FROM GL_RUN_TRASH ";
964 myquery << " WHERE BELONGED_TO='GL_RUN_FRAGMENTS' AND ID=";
965 myquery << (UInt_t)IDRUN << ";";
966 //
967 // printf("5myquery is %s \n",myquery.str().c_str());
968 //
969 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
970 this->GetGLTABLES()->AddQ();
971 dbc->Query(myquery.str().c_str());
972 //
973 //
974 myquery.str("");
975 myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID=";
976 myquery << IDRUN << ";";
977 //
978 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
979 this->GetGLTABLES()->AddQ();
980 dbc->Query(myquery.str().c_str());
981 //
982 return 0;
983 };
984
985 /**
986 * Function to fill the GL_RUN table of the DB.
987 *
988 * \param
989 *
990 */
991 Int_t GL_RUN::Fill_GL_RUN(TSQLServer *dbc){
992 // MySQL variables
993 stringstream myquery;
994 // ----------------
995 myquery.str("");
996 myquery << " INSERT INTO GL_RUN (";
997 myquery << "ID";
998 myquery << ",ID_RUN_FRAG";
999 myquery << ",ID_ROOT_L0";
1000 myquery << ",ID_ROOT_L2";
1001 myquery << ",RUNHEADER_TIME";
1002 myquery << ",RUNTRAILER_TIME";
1003 myquery << ",RUNHEADER_OBT";
1004 myquery << ",RUNTRAILER_OBT";
1005 myquery << ",RUNHEADER_PKT";
1006 myquery << ",RUNTRAILER_PKT";
1007 myquery << ",EV_FROM";
1008 myquery << ",EV_TO";
1009 myquery << ",NEVENTS";
1010 myquery << ",LAST_TIMESYNC";
1011 myquery << ",OBT_TIMESYNC";
1012 myquery << ",COMPILATIONTIMESTAMP";
1013 myquery << ",FAV_WRK_SCHEDULE";
1014 myquery << ",EFF_WRK_SCHEDULE";
1015 myquery << ",PRH_VAR_TRG_MODE_A";
1016 myquery << ",PRH_VAR_TRG_MODE_B";
1017 myquery << ",ACQ_BUILD_INFO";
1018 myquery << ",ACQ_VAR_INFO";
1019 myquery << ",RM_ACQ_AFTER_CALIB";
1020 myquery << ",RM_ACQ_SETTING_MODE";
1021 myquery << ",PKT_COUNTER";
1022 myquery << ",PKT_READY_COUNTER";
1023 myquery << ",TRK_CALIB_USED";
1024 myquery << ",CAL_DSP_MASK";
1025 myquery << ",BOOT_NUMBER";
1026 myquery << ",PHYSENDRUN_MASK_S3S2S12";
1027 myquery << ",PHYSENDRUN_MASK_S11CRC";
1028 myquery << ",VALIDATION";
1029 myquery << ") VALUES ('";
1030
1031 myquery << (UInt_t)ID << "','";
1032 myquery << (UInt_t)ID_RUN_FRAG << "','";
1033 myquery << (UInt_t)ID_ROOT_L0 << "','";
1034 myquery << (UInt_t)ID_ROOT_L2 << "','";
1035 myquery << (UInt_t)RUNHEADER_TIME << "','";
1036 myquery << (UInt_t)RUNTRAILER_TIME << "','";
1037 myquery << (UInt_t)RUNHEADER_OBT << "','";
1038 myquery << (UInt_t)RUNTRAILER_OBT << "','";
1039 myquery << (UInt_t)RUNHEADER_PKT << "','";
1040 myquery << (UInt_t)RUNTRAILER_PKT << "','";
1041 myquery << (UInt_t)EV_FROM << "','";
1042 myquery << (UInt_t)EV_TO << "','";
1043 myquery << (UInt_t)NEVENTS << "','";
1044 myquery << (UInt_t)LAST_TIMESYNC << "','";
1045 myquery << (UInt_t)OBT_TIMESYNC << "','";
1046 myquery << (UInt_t)COMPILATIONTIMESTAMP << "','";
1047 myquery << (UInt_t)FAV_WRK_SCHEDULE << "','";
1048 myquery << (UInt_t)EFF_WRK_SCHEDULE << "','";
1049 myquery << (UInt_t)PRH_VAR_TRG_MODE_A << "','";
1050 myquery << (UInt_t)PRH_VAR_TRG_MODE_B << "','";
1051 myquery << (UInt_t)ACQ_BUILD_INFO << "','";
1052 myquery << (UInt_t)ACQ_VAR_INFO << "','";
1053 myquery << (UInt_t)RM_ACQ_AFTER_CALIB << "','";
1054 myquery << (UInt_t)RM_ACQ_SETTING_MODE << "','";
1055 myquery << (UInt_t)PKT_COUNTER << "','";
1056 myquery << (UInt_t)PKT_READY_COUNTER << "','";
1057 myquery << (UInt_t)TRK_CALIB_USED << "','";
1058 myquery << (UInt_t)CAL_DSP_MASK << "','";
1059 myquery << (UInt_t)BOOT_NUMBER << "','";
1060 myquery << (UInt_t)PHYSENDRUN_MASK_S3S2S12 << "','";
1061 myquery << (UInt_t)PHYSENDRUN_MASK_S11CRC << "','";
1062 myquery << (UInt_t)VALIDATION << "');";
1063 //
1064 // printf("myquery is %s \n",myquery.str().c_str());
1065 //
1066 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1067 this->GetGLTABLES()->AddQ();
1068 dbc->Query(myquery.str().c_str());
1069 //
1070 return 0;
1071
1072 };// ****************************************************
1073
1074 /**
1075 * Function to fill the GL_RUN table of the DB.
1076 *
1077 * \param
1078 *
1079 */
1080 Int_t GL_RUN::Fill_GL_RUN_FRAGMENTS(TSQLServer *dbc){
1081 // MySQL variables
1082 stringstream myquery;
1083 // ----------------
1084 myquery.str("");
1085 myquery << " INSERT INTO GL_RUN_FRAGMENTS (";
1086 myquery << "ID";
1087 myquery << ",ID_ROOT_L0";
1088 myquery << ",RUNHEADER_TIME";
1089 myquery << ",RUNTRAILER_TIME";
1090 myquery << ",RUNHEADER_OBT";
1091 myquery << ",RUNTRAILER_OBT";
1092 myquery << ",RUNHEADER_PKT";
1093 myquery << ",RUNTRAILER_PKT";
1094 myquery << ",EV_FROM";
1095 myquery << ",EV_TO";
1096 myquery << ",NEVENTS";
1097 myquery << ",LAST_TIMESYNC";
1098 myquery << ",OBT_TIMESYNC";
1099 myquery << ",COMPILATIONTIMESTAMP";
1100 myquery << ",FAV_WRK_SCHEDULE";
1101 myquery << ",EFF_WRK_SCHEDULE";
1102 myquery << ",PRH_VAR_TRG_MODE_A";
1103 myquery << ",PRH_VAR_TRG_MODE_B";
1104 myquery << ",ACQ_BUILD_INFO";
1105 myquery << ",ACQ_VAR_INFO";
1106 myquery << ",RM_ACQ_AFTER_CALIB";
1107 myquery << ",RM_ACQ_SETTING_MODE";
1108 myquery << ",PKT_COUNTER";
1109 myquery << ",PKT_READY_COUNTER";
1110 myquery << ",TRK_CALIB_USED";
1111 myquery << ",CAL_DSP_MASK";
1112 myquery << ",BOOT_NUMBER";
1113 myquery << ",PHYSENDRUN_MASK_S3S2S12";
1114 myquery << ",PHYSENDRUN_MASK_S11CRC";
1115 myquery << ") VALUES ('";
1116 myquery << (UInt_t)ID << "','";
1117 myquery << (UInt_t)ID_ROOT_L0 << "','";
1118 myquery << (UInt_t)RUNHEADER_TIME << "','";
1119 myquery << (UInt_t)RUNTRAILER_TIME << "','";
1120 myquery << (UInt_t)RUNHEADER_OBT << "','";
1121 myquery << (UInt_t)RUNTRAILER_OBT << "','";
1122 myquery << (UInt_t)RUNHEADER_PKT << "','";
1123 myquery << (UInt_t)RUNTRAILER_PKT << "','";
1124 myquery << (UInt_t)EV_FROM << "','";
1125 myquery << (UInt_t)EV_TO << "','";
1126 myquery << (UInt_t)NEVENTS << "','";
1127 myquery << (UInt_t)LAST_TIMESYNC << "','";
1128 myquery << (UInt_t)OBT_TIMESYNC << "','";
1129 myquery << (UInt_t)COMPILATIONTIMESTAMP << "','";
1130 myquery << (UInt_t)FAV_WRK_SCHEDULE << "','";
1131 myquery << (UInt_t)EFF_WRK_SCHEDULE << "','";
1132 myquery << (UInt_t)PRH_VAR_TRG_MODE_A << "','";
1133 myquery << (UInt_t)PRH_VAR_TRG_MODE_B << "','";
1134 myquery << (UInt_t)ACQ_BUILD_INFO << "','";
1135 myquery << (UInt_t)ACQ_VAR_INFO << "','";
1136 myquery << (UInt_t)RM_ACQ_AFTER_CALIB << "','";
1137 myquery << (UInt_t)RM_ACQ_SETTING_MODE << "','";
1138 myquery << (UInt_t)PKT_COUNTER << "','";
1139 myquery << (UInt_t)PKT_READY_COUNTER << "','";
1140 myquery << (UInt_t)TRK_CALIB_USED << "','";
1141 myquery << (UInt_t)CAL_DSP_MASK << "','";
1142 myquery << (UInt_t)BOOT_NUMBER << "','";
1143 myquery << (UInt_t)PHYSENDRUN_MASK_S3S2S12 << "','";
1144 myquery << (UInt_t)PHYSENDRUN_MASK_S11CRC << "');";
1145 //
1146 // printf("myquery is %s \n",myquery.str().c_str());
1147 //
1148 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1149 this->GetGLTABLES()->AddQ();
1150 dbc->Query(myquery.str().c_str());
1151 //
1152 return 0;
1153
1154 };// ****************************************************
1155
1156
1157 /**
1158 * Function to query the GL_RUN table of the DB.
1159 *
1160 * \param RUN id
1161 * \return struct of type GL_RUN _data, which stores the query result
1162 *
1163 */
1164 Int_t GL_RUN::Query_GL_RUN(UInt_t run, TSQLServer *dbc){
1165 // MySQL variables
1166 TSQLResult *pResult;
1167 TSQLRow *Row = NULL;
1168 int r;
1169 stringstream myquery;
1170 // ----------------
1171 myquery.str("");
1172 myquery << " select ";
1173 myquery << "ID";
1174 myquery << ",ID_RUN_FRAG";
1175 myquery << ",ID_ROOT_L0";
1176 myquery << ",ID_ROOT_L2";
1177 myquery << ",RUNHEADER_TIME";
1178 myquery << ",RUNTRAILER_TIME";
1179 myquery << ",RUNHEADER_OBT";
1180 myquery << ",RUNTRAILER_OBT";
1181 myquery << ",RUNHEADER_PKT";
1182 myquery << ",RUNTRAILER_PKT";
1183 myquery << ",EV_FROM";
1184 myquery << ",EV_TO";
1185 myquery << ",NEVENTS";
1186 myquery << ",LAST_TIMESYNC";
1187 myquery << ",OBT_TIMESYNC";
1188 myquery << ",COMPILATIONTIMESTAMP";
1189 myquery << ",FAV_WRK_SCHEDULE";
1190 myquery << ",EFF_WRK_SCHEDULE";
1191 myquery << ",PRH_VAR_TRG_MODE_A";
1192 myquery << ",PRH_VAR_TRG_MODE_B";
1193 myquery << ",ACQ_BUILD_INFO";
1194 myquery << ",ACQ_VAR_INFO";
1195 myquery << ",RM_ACQ_AFTER_CALIB";
1196 myquery << ",RM_ACQ_SETTING_MODE";
1197 myquery << ",PKT_COUNTER";
1198 myquery << ",PKT_READY_COUNTER";
1199 myquery << ",TRK_CALIB_USED";
1200 myquery << ",CAL_DSP_MASK";
1201 myquery << ",BOOT_NUMBER";
1202 myquery << ",PHYSENDRUN_MASK_S3S2S12";
1203 myquery << ",PHYSENDRUN_MASK_S11CRC";
1204 myquery << ",VALIDATION";
1205 myquery << " from GL_RUN where ID=" << run << ";";
1206 //
1207 // printf(" myquery is %s \n",myquery.str().c_str());
1208 if ( !this->IsConnected(dbc) ) return -57;
1209 this->AddQ();
1210 pResult = dbc->Query(myquery.str().c_str());
1211 //
1212 // printf(" getrowcount %i \n",pResult->GetRowCount());
1213 //
1214 if( !pResult->GetRowCount() ) return(-50);
1215 //
1216 for( r=0; r < 1000; r++){
1217 if (Row)
1218 delete Row;
1219 Row = pResult->Next();
1220 if( Row == NULL ) break;
1221 // Set_GL_RUN(Row);
1222 for( int t = 0; t < pResult->GetFieldCount(); t++){
1223 if (t== 0) ID = (UInt_t)atoll(Row->GetField(t));
1224 if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t));
1225 if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1226 if (t== 3) ID_ROOT_L2 = (UInt_t)atoll(Row->GetField(t));
1227 if (t== 4) RUNHEADER_TIME = (UInt_t)atoll(Row->GetField(t));
1228 if (t== 5) RUNTRAILER_TIME = (UInt_t)atoll(Row->GetField(t));
1229 if (t== 6) RUNHEADER_OBT = (UInt_t)atoll(Row->GetField(t));
1230 if (t== 7) RUNTRAILER_OBT = (UInt_t)atoll(Row->GetField(t));
1231 if (t== 8) RUNHEADER_PKT = (UInt_t)atoll(Row->GetField(t));
1232 if (t== 9) RUNTRAILER_PKT = (UInt_t)atoll(Row->GetField(t));
1233 if (t==10) EV_FROM = (UInt_t)atoll(Row->GetField(t));
1234 if (t==11) EV_TO = (UInt_t)atoll(Row->GetField(t));
1235 if (t==12) NEVENTS = (UInt_t)atoll(Row->GetField(t));
1236 if (t==13) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1237 if (t==14) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1238 if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));
1239 if (t==16) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
1240 if (t==17) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
1241 if (t==18) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t));
1242 if (t==19) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t));
1243 if (t==20) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t));
1244 if (t==21) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t));
1245 if (t==22) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t));
1246 if (t==23) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t));
1247 if (t==24) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t));
1248 if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));
1249 if (t==26) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t));
1250 if (t==27) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t));
1251 if (t==28) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t));
1252 if (t==29) PHYSENDRUN_MASK_S3S2S12 = (UInt_t)atoll(Row->GetField(t));
1253 if (t==30) PHYSENDRUN_MASK_S11CRC = (UInt_t)atoll(Row->GetField(t));
1254 if (t==31) VALIDATION = (UInt_t)atoll(Row->GetField(t));
1255 };
1256 }
1257
1258 if (Row)
1259 delete Row;
1260 delete pResult;
1261 return(0);
1262 };
1263
1264 /**
1265 * Function to query the GL_RUN table of the DB.
1266 *
1267 * \param where = condition string
1268 * \return struct of type GL_RUN _data, which stores the query result
1269 *
1270 */
1271 Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TString where, TSQLServer *dbc){
1272 // MySQL variables
1273 TSQLResult *pResult;
1274 TSQLRow *Row = NULL;
1275 int t;
1276 int r;
1277 stringstream myquery;
1278 // ----------------
1279 myquery.str("");
1280 myquery << " select ";
1281 myquery << "ID";
1282 myquery << ",ID_RUN_FRAG";
1283 myquery << ",ID_ROOT_L0";
1284 myquery << ",ID_ROOT_L2";
1285 myquery << ",RUNHEADER_TIME";
1286 myquery << ",RUNTRAILER_TIME";
1287 myquery << ",RUNHEADER_OBT";
1288 myquery << ",RUNTRAILER_OBT";
1289 myquery << ",RUNHEADER_PKT";
1290 myquery << ",RUNTRAILER_PKT";
1291 myquery << ",EV_FROM";
1292 myquery << ",EV_TO";
1293 myquery << ",NEVENTS";
1294 myquery << ",LAST_TIMESYNC";
1295 myquery << ",OBT_TIMESYNC";
1296 myquery << ",COMPILATIONTIMESTAMP";
1297 myquery << ",FAV_WRK_SCHEDULE";
1298 myquery << ",EFF_WRK_SCHEDULE";
1299 myquery << ",PRH_VAR_TRG_MODE_A";
1300 myquery << ",PRH_VAR_TRG_MODE_B";
1301 myquery << ",ACQ_BUILD_INFO";
1302 myquery << ",ACQ_VAR_INFO";
1303 myquery << ",RM_ACQ_AFTER_CALIB";
1304 myquery << ",RM_ACQ_SETTING_MODE";
1305 myquery << ",PKT_COUNTER";
1306 myquery << ",PKT_READY_COUNTER";
1307 myquery << ",TRK_CALIB_USED";
1308 myquery << ",CAL_DSP_MASK";
1309 myquery << ",BOOT_NUMBER";
1310 myquery << ",PHYSENDRUN_MASK_S3S2S12";
1311 myquery << ",PHYSENDRUN_MASK_S11CRC";
1312 myquery << ",VALIDATION";
1313 myquery << " from GL_RUN_FRAGMENTS where " << where.Data() << ";";
1314 //
1315 // printf(" query is %s \n",myquery.str().c_str());
1316 //
1317 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1318 this->GetGLTABLES()->AddQ();
1319 pResult = dbc->Query(myquery.str().c_str());
1320 if(!pResult->GetRowCount())return(-50);
1321 for( r=0; r < 1000; r++){
1322 if (Row)
1323 delete Row;
1324 Row = pResult->Next();
1325 if( Row == NULL ) break;
1326 for( t = 0; t < pResult->GetFieldCount(); t++){
1327 if (t== 0) ID = (UInt_t)atoll(Row->GetField(t));
1328 if (t== 1) ID_RUN_FRAG = (UInt_t)atoll(Row->GetField(t));
1329 if (t== 2) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1330 if (t== 3) ID_ROOT_L2 = (UInt_t)atoll(Row->GetField(t));
1331 if (t== 4) RUNHEADER_TIME = (UInt_t)atoll(Row->GetField(t));
1332 if (t== 5) RUNTRAILER_TIME = (UInt_t)atoll(Row->GetField(t));
1333 if (t== 6) RUNHEADER_OBT = (UInt_t)atoll(Row->GetField(t));
1334 if (t== 7) RUNTRAILER_OBT = (UInt_t)atoll(Row->GetField(t));
1335 if (t== 8) RUNHEADER_PKT = (UInt_t)atoll(Row->GetField(t));
1336 if (t== 9) RUNTRAILER_PKT = (UInt_t)atoll(Row->GetField(t));
1337 if (t==10) EV_FROM = (UInt_t)atoll(Row->GetField(t));
1338 if (t==11) EV_TO = (UInt_t)atoll(Row->GetField(t));
1339 if (t==12) NEVENTS = (UInt_t)atoll(Row->GetField(t));
1340 if (t==13) LAST_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1341 if (t==14) OBT_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1342 if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));
1343 if (t==16) FAV_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
1344 if (t==17) EFF_WRK_SCHEDULE = (UInt_t)atoll(Row->GetField(t));
1345 if (t==18) PRH_VAR_TRG_MODE_A= (UInt_t)atoll(Row->GetField(t));
1346 if (t==19) PRH_VAR_TRG_MODE_B= (UInt_t)atoll(Row->GetField(t));
1347 if (t==20) ACQ_BUILD_INFO = (UInt_t)atoll(Row->GetField(t));
1348 if (t==21) ACQ_VAR_INFO = (UInt_t)atoll(Row->GetField(t));
1349 if (t==22) RM_ACQ_AFTER_CALIB= (UInt_t)atoll(Row->GetField(t));
1350 if (t==23) RM_ACQ_SETTING_MODE = (UInt_t)atoll(Row->GetField(t));
1351 if (t==24) PKT_COUNTER = (UInt_t)atoll(Row->GetField(t));
1352 if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));
1353 if (t==26) TRK_CALIB_USED = (UInt_t)atoll(Row->GetField(t));
1354 if (t==27) CAL_DSP_MASK = (UInt_t)atoll(Row->GetField(t));
1355 if (t==28) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t));
1356 if (t==29) PHYSENDRUN_MASK_S3S2S12 = (UInt_t)atoll(Row->GetField(t));
1357 if (t==30) PHYSENDRUN_MASK_S11CRC = (UInt_t)atoll(Row->GetField(t));
1358 if (t==31) VALIDATION = (UInt_t)atoll(Row->GetField(t));
1359 };
1360 }
1361
1362 if (Row)
1363 delete Row;
1364 delete pResult;
1365 return(0);
1366 };// ****************************************************
1367
1368 /**
1369 * Function to query the GL_ROOT table of the DB.
1370 *
1371 * \param entry ID
1372 * \return struct of type GL_ROOT_data, which stores the query result
1373 */
1374 Int_t GL_ROOT::Query_GL_ROOT(UInt_t id, TSQLServer *dbc){
1375 // MySQL variables
1376 TSQLResult *pResult;
1377 TSQLRow *Row = NULL;
1378 int t;
1379 int r;
1380 stringstream myquery;
1381 // ----------------
1382 myquery.str("");
1383 myquery << "select ";
1384 myquery << " ID";
1385 myquery << ",ID_RAW";
1386 myquery << ",ID_TIMESYNC";
1387 myquery << ",PATH";
1388 myquery << ",NAME";
1389 myquery << " from GL_ROOT where ID=" << id << ";";
1390 //
1391 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1392 this->GetGLTABLES()->AddQ();
1393 pResult = dbc->Query(myquery.str().c_str());
1394 if(!pResult->GetRowCount())return (-51);
1395 for( r=0; r < 1000; r++){
1396 if (Row)
1397 delete Row;
1398 Row = pResult->Next();
1399 if( Row == NULL ) break;
1400 for( t = 0; t < pResult->GetFieldCount(); t++){
1401 if(t==0) ID = (UInt_t)atoll(Row->GetField(t));
1402 if(t==1) ID_RAW = (UInt_t)atoll(Row->GetField(t));
1403 if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1404 if(t==3) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/';
1405 if(t==4) NAME = Row->GetField(t);
1406 }
1407 }
1408 if (Row)
1409 delete Row;
1410 delete pResult;
1411 return 0;
1412 };
1413 // ****************************************************
1414 /**
1415 * Function to query the GL_TRK_CALIB table of the DB.
1416 *
1417 * \param run starting time
1418 * \param dbc DB server
1419 * \return struct of type GL_TRK_CALIB_data, which stores the query result
1420 */
1421 Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(UInt_t time, TSQLServer *dbc){
1422 // MySQL variables
1423 TSQLResult *pResult;
1424 TSQLRow *Row = NULL;
1425 int t;
1426 int r;
1427 stringstream myquery;
1428 // ----------------
1429 myquery.str("");
1430 myquery << "select ID,ID_ROOT_L0,EV_ROOT_CALIBTRK1,EV_ROOT_CALIBTRK2,FROM_TIME,TO_TIME,OBT1,OBT2,PKT1,PKT2,BOOT_NUMBER,VALIDATION from GL_TRK_CALIB where FROM_TIME <= "<< time;
1431 // myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;"; // RIVEDERE LA VALIDAZIONE!!!
1432 myquery << " and EV_ROOT_CALIBTRK1>=0 and EV_ROOT_CALIBTRK2>=0 "; // EM! SE MANCA UN PACCHETTO DEI DUE IL PROCESSAMENTO CRASHA... DA RIVEDERE LA VALIDAZIONE
1433 myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";
1434 // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";
1435 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1436 this->GetGLTABLES()->AddQ();
1437 pResult = dbc->Query(myquery.str().c_str());
1438 if(!pResult->GetRowCount())return (-53);
1439 for( r=0; r < 1000; r++){
1440 if (Row)
1441 delete Row;
1442 Row = pResult->Next();
1443 if( Row == NULL ) break;
1444 for( t = 0; t < pResult->GetFieldCount(); t++){
1445 stringstream row;
1446 row.str("");
1447 row << "0" << Row->GetField(t); // add leading 0 since we have two fields that could be "null" and we want to avoid crashes due to atoll
1448 if (t==0) ID = (UInt_t)atoll(Row->GetField(t));
1449 if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1450 if (t==2) EV_ROOT_CALIBTRK1 = (UInt_t)atoll(row.str().c_str());
1451 if (t==3) EV_ROOT_CALIBTRK2 = (UInt_t)atoll(row.str().c_str());
1452 if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1453 if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t));
1454 //
1455 if (t==6) OBT1 = (UInt_t)atoll(Row->GetField(t));
1456 if (t==7) OBT2 = (UInt_t)atoll(Row->GetField(t));
1457 if (t==8) PKT1 = (UInt_t)atoll(Row->GetField(t));
1458 if (t==9) PKT2 = (UInt_t)atoll(Row->GetField(t));
1459 if (t==10) BOOT_NUMBER = (UInt_t)atoll(Row->GetField(t));
1460 if (t==11) VALIDATION = (UInt_t)atoll(Row->GetField(t));
1461 };
1462 }
1463 if (Row)
1464 delete Row;
1465 delete pResult;
1466 //
1467 // if ( TO_TIME < time ) return(51);
1468 //
1469 if ( (!OBT1 && !PKT1 ) || (!OBT2 && !PKT2) ) return(52); // ONE CALIBRATION PACKET IS MISSING!
1470 //
1471 return 0;
1472 };
1473
1474 // ****************************************************
1475 /**
1476 * Function to query the GL_CALO_CALIB table of the DB.
1477 *
1478 * \param run starting time
1479 * \return struct of type GL_CALO_CALIB_data, which stores the query result
1480 */
1481 Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB(UInt_t time, UInt_t &uptime, UInt_t section, TSQLServer *dbc){
1482 // MySQL variables
1483 TSQLResult *pResult;
1484 TSQLRow *Row = NULL;
1485 int t;
1486 stringstream myquery;
1487 uptime = 0;
1488 //
1489 // select the correct calibration
1490 //
1491 myquery.str("");
1492 myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT,VALIDATION from GL_CALO_CALIB where SECTION=" << section;
1493 myquery << " and FROM_TIME <= " << time;
1494 myquery << " and TO_TIME > " << time;
1495 myquery << " ;";
1496 //myquery << " and VALIDATION=1;";
1497 //
1498 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1499 this->GetGLTABLES()->AddQ();
1500 pResult = dbc->Query(myquery.str().c_str());
1501 // printf(" mysquery is %s\n",myquery.str().c_str());
1502 //
1503 if( !pResult->GetRowCount() ) return(-54);
1504 if (Row)
1505 delete Row;
1506 Row = pResult->Next();
1507 if( Row == NULL ) return (-54);
1508 //
1509 uptime = (UInt_t)atoll(Row->GetField(2));
1510 //
1511 UInt_t myfromtime = (UInt_t)atoll(Row->GetField(1));
1512 UInt_t mytotime = (UInt_t)atoll(Row->GetField(2));
1513 //
1514 // if it is corrupted validation is 0 and we have no results from the query...
1515 //
1516 if( atoi(Row->GetField(4)) == 0 ){ // if validation = 0
1517 //
1518 // in this case take relax the conditions and take the valid calibration that preceed the correct one
1519 //
1520 myquery.str("");
1521 myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT,VALIDATION from GL_CALO_CALIB where SECTION=" << section;
1522 myquery << " and FROM_TIME <= " << time;
1523 myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";
1524 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1525 this->GetGLTABLES()->AddQ();
1526 pResult = dbc->Query(myquery.str().c_str());
1527 // printf(" mysquery is %s\n",myquery.str().c_str());
1528 //
1529 // if no results yet quit with error
1530 //
1531 if( !pResult->GetRowCount() ) return (-54);
1532 //
1533 if (Row)
1534 delete Row;
1535 Row = pResult->Next();
1536 //
1537 myfromtime = (UInt_t)atoll(Row->GetField(1));
1538 //
1539 };
1540 //
1541 // if the selected calibration is too old (more than 5 orbits old) try to take the closest not corrupted one
1542 //
1543 if ( (time-myfromtime)>28500 && myfromtime > 0 ){
1544 //
1545 myquery.str("");
1546 myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT from GL_CALO_CALIB where SECTION=" << section;
1547 myquery << " and VALIDATION=1 ORDER BY ABS(" << time << "-FROM_TIME) asc limit 1;";
1548 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1549 this->GetGLTABLES()->AddQ();
1550 pResult = dbc->Query(myquery.str().c_str());
1551 // printf(" mysquery is %s\n",myquery.str().c_str());
1552 //
1553 // if no results yet quit with error
1554 //
1555 if( !pResult->GetRowCount() ) return (-54);
1556 //
1557 if (Row)
1558 delete Row;
1559 Row = pResult->Next();
1560 //
1561 };
1562 //
1563 // store infos and exit
1564 //
1565 if( Row == NULL ) return (-54);
1566 for( t = 0; t < pResult->GetFieldCount(); t++){
1567 if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1568 if (t==1) FROM_TIME = myfromtime;
1569 if (t==2) TO_TIME = mytotime;
1570 if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t));
1571 }
1572 if (Row)
1573 delete Row;
1574 pResult->Delete();
1575 return 0;
1576 };
1577
1578
1579 // ****************************************************
1580 /**
1581 * Function to query the GL_CALOPULSE_CALIB table of the DB.
1582 *
1583 * \param run starting time
1584 * \return struct of type GL_CALOPULSE_CALIB_data, which stores the query result
1585 */
1586 Int_t GL_CALOPULSE_CALIB::Query_GL_CALOPULSE_CALIB(UInt_t time, UInt_t section, UInt_t pampli, TSQLServer *dbc){
1587 // MySQL variables
1588 TSQLResult *pResult;
1589 TSQLRow *Row = NULL;
1590 int t;
1591 stringstream myquery;
1592 //
1593 // select the correct calibration i.e. the closest to our time
1594 //
1595 myquery.str("");
1596 myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT from GL_CALOPULSE_CALIB where SECTION=" << section;
1597 myquery << " and PULSE_AMPLITUDE=" << pampli;
1598 myquery << " and VALIDATION=1 and (PULSED_STRIP IS NULL OR PULSED_STRIP<100) ORDER BY ABS(" << time << "-FROM_TIME) asc limit 1;";
1599 // printf(" myq is %s \n",myquery.str().c_str());
1600 //
1601 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1602 this->GetGLTABLES()->AddQ();
1603 pResult = dbc->Query(myquery.str().c_str());
1604 //
1605 if( !pResult ) return(-54);
1606 //
1607 if (Row)
1608 delete Row;
1609 Row = pResult->Next();
1610 //
1611 if( !Row ) return (-54);
1612 //
1613 // store infos and exit
1614 //
1615 for( t = 0; t < pResult->GetFieldCount(); t++){
1616 // printf(" field %i is %s \n",t,Row->GetField(t));
1617 if (t==0) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1618 if (t==1) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1619 if (t==2) TO_TIME = (UInt_t)atoll(Row->GetField(t));
1620 if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t));
1621 }
1622 if (Row)
1623 delete Row;
1624 pResult->Delete();
1625 return 0;
1626 };
1627
1628
1629 // ****************************************************
1630 /**
1631 * Function to query the GL_S4_CALIB table of the DB.
1632 *
1633 * \param run starting time
1634 * \return struct of type GL_S4_CALIB_data, which stores the query result
1635 */
1636 Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UInt_t time, TSQLServer *dbc){
1637 // MySQL variables
1638 TSQLResult *pResult;
1639 TSQLRow *Row = NULL;
1640 int t;
1641 int r;
1642 stringstream myquery;
1643 // ----------------
1644 myquery.str("");
1645 myquery << "select ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME from GL_S4_CALIB where FROM_TIME <= "<< time;
1646 myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";
1647 // myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";
1648 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1649 this->GetGLTABLES()->AddQ();
1650 pResult = dbc->Query(myquery.str().c_str());
1651 if(!pResult->GetRowCount())return (-55);//throw -55;
1652 for( r=0; r < 1000; r++){
1653 if (Row)
1654 delete Row;
1655 Row = pResult->Next();
1656 if( Row == NULL ) break;
1657 for( t = 0; t < pResult->GetFieldCount(); t++){
1658 if (t==0) ID = (UInt_t)atoll(Row->GetField(t));
1659 if (t==1) ID_ROOT_L0 = (UInt_t)atoll(Row->GetField(t));
1660 if (t==2) EV_ROOT = (UInt_t)atoll(Row->GetField(t));
1661 if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1662 if (t==4) TO_TIME = (UInt_t)atoll(Row->GetField(t));
1663 };
1664 }
1665 if (Row)
1666 delete Row;
1667 delete pResult;
1668 //
1669 if(TO_TIME < time)return(51);
1670 //
1671 return 0;
1672 };
1673 // ****************************************************
1674 /**
1675 * Function to query the GL_PARAM table of the DB.
1676 *
1677 * \param run starting time
1678 * \param parameter description (DESCR)
1679 * \return struct of type GL_ROOT_data, which stores the query result
1680 */
1681 Int_t GL_PARAM::Query_GL_PARAM(UInt_t time, UInt_t type, TSQLServer *dbc){
1682 // Bool_t debug = 1;
1683 // MySQL variables
1684 TSQLResult *pResult;
1685 TSQLRow *Row = NULL;
1686 int t;
1687 int r;
1688 stringstream myquery;
1689 // ----------------
1690 myquery.str("");
1691 myquery << " select ";
1692 myquery << " ID, PATH, NAME, DESCR, FROM_TIME,TO_TIME, TYPE ";
1693 myquery << " from GL_PARAM ";
1694 myquery << " where TYPE = '"<<type<<"' ";
1695 myquery << " and FROM_TIME <= " << time;
1696 myquery << " ORDER BY TO_TIME DESC LIMIT 1;";
1697 //
1698 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
1699 this->GetGLTABLES()->AddQ();
1700 pResult = dbc->Query(myquery.str().c_str());
1701 if(!pResult->GetRowCount())return (-52);
1702 for( r=0; r < 1000; r++){
1703 if (Row)
1704 delete Row;
1705 Row = pResult->Next();
1706 if( Row == NULL ) break;
1707 for( t = 0; t < pResult->GetFieldCount(); t++){
1708 if (t==0) ID = (UInt_t)atoll(Row->GetField(t));
1709 if (t==1) PATH = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/';// put in fpath the path to that file
1710 if (t==2) NAME = Row->GetField(t);
1711 if (t==3) DESCR = Row->GetField(t);
1712 if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1713 if (t==5) TO_TIME = (UInt_t)atoll(Row->GetField(t));
1714 if (t==6) TYPE = (UInt_t)atoll(Row->GetField(t));
1715 };
1716 }
1717 if (Row)
1718 delete Row;
1719 delete pResult;
1720 //
1721 if(TO_TIME==0) TO_TIME = numeric_limits<UInt_t>::max();
1722 //
1723 if(TO_TIME < time) return(51);
1724 //
1725 return 0;
1726 };
1727
1728
1729 /**
1730 * Fills a struct cGLRun with values from a GLRun object (to put data into a F77 common).
1731 */
1732
1733 void GL_RUN::GetLevel2Struct(cGLRun *l2) const{
1734 l2->id = ID;
1735 l2->id_reg_run = ID_ROOT_L0;
1736 l2->id_reg_run_l2 = ID_ROOT_L2;
1737 l2->runheader_time = RUNHEADER_TIME;
1738 l2->runtrailer_time = RUNTRAILER_TIME;
1739 l2->ev_from = EV_FROM;
1740 l2->trk_calib_used = TRK_CALIB_USED;
1741 l2->eff_wrk_schedule = EFF_WRK_SCHEDULE;
1742 l2->prh_var_trg_mode_a = PRH_VAR_TRG_MODE_A;
1743 l2->prh_var_trg_mode_b = PRH_VAR_TRG_MODE_B;
1744 l2->acq_build_info = ACQ_BUILD_INFO;
1745 l2->acq_var_info = ACQ_VAR_INFO;
1746 };
1747
1748 GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc){
1749 // MySQL variables
1750 TFile *file = 0;
1751 UInt_t idtsy = 0;
1752 //
1753 TSQLResult *pResult;
1754 TSQLRow *Row = NULL;
1755 stringstream myquery;
1756 stringstream rname;
1757 // pcksList packetsNames;
1758 // pcksList::iterator Iter;
1759 // getPacketsNames(packetsNames);
1760 rname.str("");
1761 // ----------------
1762 myquery.str("");
1763 myquery << "select ";
1764 myquery << "PATH";
1765 myquery << ",NAME,ID_TIMESYNC";
1766 myquery << " from GL_ROOT where ";
1767 myquery << type.Data();
1768 myquery << "=" << id << ";";
1769 //
1770 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1771 this->GetGLTABLES()->AddQ();
1772 pResult = dbc->Query(myquery.str().c_str());
1773 if( pResult->GetRowCount() ){
1774 if (Row)
1775 delete Row;
1776 Row = pResult->Next();
1777 if( Row ){
1778 stringstream fname;
1779 fname.str("");
1780 fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1);
1781 rname << Row->GetField(1);
1782 file = new TFile(fname.str().c_str(),"READ");
1783 idtsy = (UInt_t)atoll(Row->GetField(2));
1784 };
1785 };
1786 //
1787 if ( file && file->IsOpen() ){
1788 TTree *T=(TTree*)file->Get("Physics");
1789 pamela::EventHeader *eh = 0;
1790 pamela::PscuHeader *ph = 0;
1791 T->SetBranchAddress("Header", &eh);
1792 //
1793 T->GetEntry(0);
1794 ph = eh->GetPscuHeader();
1795 pktfirst = ph->GetCounter();
1796 // obtfirst = ph->GetOrbitalTime();
1797 //
1798 };
1799 //
1800 // look for Resurs offset
1801 //
1802 T0 = 0;
1803 //
1804 stringstream oss;
1805 //
1806 TString name=rname.str().c_str();
1807 UInt_t dworbit = 0;
1808 // Int_t nlength = name.Length();
1809 delete pResult;
1810 //
1811 // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset
1812 //
1813 oss.str("");
1814 oss << "SELECT OBT0,TIMESYNC,TYPE,ID_RESURS_OFFSET from GL_TIMESYNC where ID=" << idtsy <<";";
1815 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1816 this->GetGLTABLES()->AddQ();
1817 pResult = dbc->Query(oss.str().c_str());
1818 Bool_t fndit = false;
1819 if ( pResult ){
1820 if (Row)
1821 delete Row;
1822 Row = pResult->Next();
1823 if ( Row ){
1824 //
1825 OBT0 = (UInt_t)atoll(Row->GetField(0));
1826 obtfirst = OBT0;
1827 TIMESYNC = (UInt_t)atoll(Row->GetField(1));
1828 TYPE = (UInt_t)atoll(Row->GetField(2));
1829 //
1830 oss.str("");
1831 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE ID="
1832 << Row->GetField(3) << ";";
1833 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1834 this->GetGLTABLES()->AddQ();
1835 delete pResult;
1836 pResult = dbc->Query(oss.str().c_str());
1837 if (pResult){
1838 if (Row)
1839 delete Row;
1840 Row = pResult->Next();
1841 if ( Row ){
1842 // printf(" GREAT! the DB structure is the new one! \n");
1843 fndit = true;
1844 dworbit = 1;
1845 };
1846 };
1847 };
1848 };
1849 if ( !fndit ){
1850 //
1851 printf(" ERROR OLD DB! \n");
1852 printf(" ERROR FROM GLTables! cannot determine Resurs offset \n");
1853 //
1854 };
1855 //
1856 TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0);
1857 T0 = (UInt_t)tu.GetSec();
1858 //
1859 toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0;
1860 //
1861 // printf(" T0 %u toffset is %u \n",T0,toffset);
1862 //
1863 if ( file ) file->Close();
1864 if (Row)
1865 delete Row;
1866 delete pResult;
1867 };
1868
1869 GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TString type, TSQLServer *dbc, Bool_t usel0file){
1870 // MySQL variables
1871 TFile *file = 0;
1872 UInt_t idtsy = 0;
1873 //
1874 TSQLResult *pResult;
1875 TSQLRow *Row = NULL;
1876 stringstream myquery;
1877 stringstream rname;
1878 // pcksList packetsNames;
1879 // pcksList::iterator Iter;
1880 // getPacketsNames(packetsNames);
1881 rname.str("");
1882 // ----------------
1883 myquery.str("");
1884 myquery << "select ";
1885 myquery << "PATH";
1886 myquery << ",NAME,ID_TIMESYNC";
1887 myquery << " from GL_ROOT where ";
1888 myquery << type.Data();
1889 myquery << "=" << id << ";";
1890 //
1891 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1892 this->GetGLTABLES()->AddQ();
1893 pResult = dbc->Query(myquery.str().c_str());
1894 if( pResult->GetRowCount() ){
1895 if (Row)
1896 delete Row;
1897 Row = pResult->Next();
1898 if( Row ){
1899 stringstream fname;
1900 fname.str("");
1901 fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1);
1902 rname << Row->GetField(1);
1903 if ( usel0file ) file = new TFile(fname.str().c_str(),"READ");
1904 idtsy = (UInt_t)atoll(Row->GetField(2));
1905 };
1906 };
1907 //
1908 if ( usel0file && file && file->IsOpen() ){
1909 TTree *T=(TTree*)file->Get("Physics");
1910 pamela::EventHeader *eh = 0;
1911 pamela::PscuHeader *ph = 0;
1912 T->SetBranchAddress("Header", &eh);
1913 //
1914 T->GetEntry(0);
1915 ph = eh->GetPscuHeader();
1916 pktfirst = ph->GetCounter();
1917 // obtfirst = ph->GetOrbitalTime();
1918 //
1919 };
1920 if ( !usel0file ) pktfirst = 0;
1921 //
1922 // look for Resurs offset
1923 //
1924 T0 = 0;
1925 //
1926 stringstream oss;
1927 //
1928 TString name=rname.str().c_str();
1929 UInt_t dworbit = 0;
1930 // Int_t nlength = name.Length();
1931 delete pResult;
1932 //
1933 // New code, we have one more column on GL_TIMESYNC so we can trust that one for the Resurs offset
1934 //
1935 oss.str("");
1936 oss << "SELECT OBT0,TIMESYNC,TYPE,ID_RESURS_OFFSET from GL_TIMESYNC where ID=" << idtsy <<";";
1937 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1938 this->GetGLTABLES()->AddQ();
1939 pResult = dbc->Query(oss.str().c_str());
1940 Bool_t fndit = false;
1941 if ( pResult ){
1942 if (Row)
1943 delete Row;
1944 Row = pResult->Next();
1945 if ( Row ){
1946 //
1947 OBT0 = (UInt_t)atoll(Row->GetField(0));
1948 obtfirst = OBT0;
1949 TIMESYNC = (UInt_t)atoll(Row->GetField(1));
1950 TYPE = (UInt_t)atoll(Row->GetField(2));
1951 //
1952 oss.str("");
1953 oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE ID="
1954 << Row->GetField(3) << ";";
1955 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;
1956 this->GetGLTABLES()->AddQ();
1957 delete pResult;
1958 pResult = dbc->Query(oss.str().c_str());
1959 if (pResult){
1960 if (Row)
1961 delete Row;
1962 Row = pResult->Next();
1963 if (Row){
1964 // printf(" GREAT! the DB structure is the new one! \n");
1965 fndit = true;
1966 dworbit = 1;
1967 };
1968 };
1969 };
1970 };
1971 if ( !fndit ){
1972 //
1973 printf(" ERROR OLD DB! \n");
1974 printf(" ERROR FROM GLTables! cannot determine Resurs offset \n");
1975 //
1976 };
1977 //
1978 TTimeStamp tu = TTimeStamp((UInt_t)atoi(Row->GetField(0)),(UInt_t)atoi(Row->GetField(1)),(UInt_t)atoi(Row->GetField(2)),(UInt_t)atoi(Row->GetField(3)),(UInt_t)atoi(Row->GetField(4)),(UInt_t)atoi(Row->GetField(5)),0,true,0);
1979 T0 = (UInt_t)tu.GetSec();
1980 //
1981 toffset = (UInt_t)TIMESYNC - (UInt_t)(this->DBobt(OBT0)/1000) + T0;
1982 //
1983 // printf(" T0 %u toffset is %u \n",T0,toffset);
1984 //
1985 if ( file ) file->Close();
1986 if (Row)
1987 delete Row;
1988 delete pResult;
1989 };
1990
1991 /**
1992 *
1993 * Returns the DB absolute time needed to associate calibrations to data
1994 *
1995 */
1996 UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT){
1997 //
1998 // printf(" OBT %u DBobt %llu toffset %u dbabstime %u\n",OBT,this->DBobt(OBT),toffset,((UInt_t)(this->DBobt(OBT)/1000)+toffset));
1999 return(((UInt_t)(this->DBobt(OBT)/1000)+toffset));
2000 //
2001 };
2002
2003 /**
2004 *
2005 * Returns the Resurs time given the OBT needed to process inclination and orbital infos
2006 *
2007 */
2008 UInt_t GL_TIMESYNC::ResursTime(UInt_t OBT){
2009 //
2010 return(((UInt_t)((Int_t)(this->DBobt(OBT)-this->DBobt(OBT0)/1000))+TIMESYNC));
2011 //
2012 };
2013
2014
2015 /**
2016 * Return the correct packet number if we went back to zero
2017 */
2018 Long64_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){
2019 //
2020 if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ){
2021 return((Long64_t)pkt_num+16777215LL);
2022 };
2023 //
2024 if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
2025 return((Long64_t)pkt_num-16777215LL);
2026 };
2027 //
2028 return((Long64_t)pkt_num);
2029 //
2030 };
2031
2032 /**
2033 * Return the correct On Board Time if we went back to zero
2034 */
2035 Long64_t GL_TIMESYNC::DBobt(UInt_t obt){
2036 //
2037 if ( obt < ((Long64_t)obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ){
2038 return((Long64_t)obt+(Long64_t)numeric_limits<UInt_t>::max());
2039 };
2040 //
2041 if ( obt > ((Long64_t)obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
2042 return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
2043 };
2044 //
2045 return((Long64_t)obt);
2046 };
2047
2048
2049
2050 // ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){
2051 // //
2052 // if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));
2053 // //
2054 // if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
2055 // if ( (obt-numeric_limits<UInt_t>::max()) < 0 ){
2056 // return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));
2057 // } else {
2058 // return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));
2059 // };
2060 // };
2061 // //
2062 // return((ULong64_t)obt);
2063 // //
2064 // };
2065
2066 // UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){
2067 // //
2068 // if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2) ) return((pkt_num+16777215));
2069 // //
2070 // if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
2071 // if ( (pkt_num-16777215) < 0 ){
2072 // return((16777215-pkt_num));
2073 // } else {
2074 // return((pkt_num-16777215));
2075 // };
2076 // };
2077 // //
2078 // return(pkt_num);
2079 // //
2080 // };
2081
2082 /*
2083 *
2084 * Convert the time in the DB from UInt_t to a string
2085 *
2086 * @param dbt time in the DB
2087 * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK
2088 *
2089 */
2090 TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){
2091 //
2092 Int_t offset = 0;
2093 TString rtime;
2094 Bool_t found = false;
2095 //
2096 if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
2097 //
2098 offset = 60*60*3; // UTC (Coordinated Universal Time) + 3hs = Moscow Winter Time
2099 found = true;
2100 //
2101 };
2102 //
2103 if ( !strcmp(tzone.Data(),"CET") ){
2104 //
2105 offset = 60*60*1; // CET (Central European Time) = UTC + 1 hs
2106 found = true;
2107 //
2108 };
2109 //
2110 if ( !strcmp(tzone.Data(),"CEST") ){
2111 //
2112 offset = 60*60*2; // CEST (Central European Summer Time) = UTC + 2 h
2113 found = true;
2114 //
2115 };
2116 //
2117 if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){
2118 //
2119 offset = 60*60*4; // MSD (Moscow Summer Time) = UTC + 4 h
2120 found = true;
2121 //
2122 };
2123 //
2124 if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){
2125 printf("\n Unknown time zone %s using UTC \n",tzone.Data());
2126 tzone = "UTC";
2127 };
2128 //
2129 dbt += offset;
2130 //
2131 TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
2132 //
2133 rtime = time->AsString("s");
2134 //
2135 delete time;
2136 //
2137 return(rtime);
2138 }
2139
2140 /*
2141 *
2142 * Convert the time from TZONE to UTC
2143 *
2144 * @param dbt time in the DB
2145 * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK
2146 *
2147 */
2148 TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){
2149 //
2150 Int_t offset = 0;
2151 TString rtime;
2152 //
2153 Bool_t found = false;
2154 //
2155 if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
2156 //
2157 offset = -60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3hs
2158 found = true;
2159 //
2160 };
2161 //
2162 if ( !strcmp(tzone.Data(),"CET") ){
2163 //
2164 offset = -60*60*1; // CET (Central European Time) - 1 hs = UTC
2165 found = true;
2166 //
2167 };
2168 //
2169 if ( !strcmp(tzone.Data(),"CEST") ){
2170 //
2171 offset = -60*60*2; // CEST (Central European Summer Time) - 2 h = UTC
2172 found = true;
2173 //
2174 };
2175 //
2176 if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){
2177 //
2178 offset = -60*60*4; // MSD (Moscow Summer Time) - 4 h = UTC
2179 found = true;
2180 //
2181 };
2182 //
2183 if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){
2184 //
2185 offset = 0;
2186 printf("\n Unknown time zone %s using UTC \n",tzone.Data());
2187 tzone = "UTC";
2188 };
2189 //
2190 dbt += offset;
2191 TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
2192 //
2193 rtime = time->AsString("s");
2194 //
2195 return(rtime);
2196 }
2197
2198
2199 //
2200 // Build a query and call DoQuery.
2201 //
2202 // date it's an SQL datetime date and dbc is the connection to be
2203 // used. It will query for the tle with the nearest but previous date
2204 // and the immediatly next one.
2205 //
2206 Int_t GL_TLE::Query(TString date, TSQLServer *dbc){
2207 stringstream myquery;
2208 myquery.str("");
2209
2210 myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
2211 << "WHERE FROM_TIME <= '" << date.Data()
2212 << "' ORDER BY FROM_TIME DESC LIMIT 1) "
2213 << "UNION "
2214 << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
2215 << "WHERE FROM_TIME > '" << date.Data()
2216 << "' ORDER BY FROM_TIME ASC LIMIT 1)";
2217
2218 return DoQuery(myquery.str().c_str(), dbc);
2219 }
2220
2221
2222 //
2223 // Build a query and call DoQuery.
2224 //
2225 // time is the UTC date in unix time (UTC) and dbc is the connection
2226 // to be used. It will query for the tle with the nearest but
2227 // previous date and the immediatly next one.
2228 //
2229 // Returns the value returned by DoQuery().
2230 //
2231 Int_t GL_TLE::Query(UInt_t time, TSQLServer *dbc){
2232 stringstream myquery;
2233 myquery.str("");
2234
2235 myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
2236 << "WHERE FROM_TIME <= FROM_UNIXTIME('" << time
2237 << "') ORDER BY FROM_TIME DESC LIMIT 1) "
2238 << "UNION "
2239 << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
2240 << "WHERE FROM_TIME > FROM_UNIXTIME('" << time
2241 << "') ORDER BY FROM_TIME ASC LIMIT 1)";
2242
2243 return DoQuery(myquery.str().c_str(), dbc);
2244 }
2245
2246
2247 //
2248 // Do the query myquery on the connectio dbc. Initialize tle,
2249 // tleFromTime and tleToTime.
2250 //
2251 // We should have two rows (unless the table is old). From the last
2252 // one we only take tleToTime.
2253 //
2254 // Returns 0 for success, 1 for failure.
2255 //
2256 Int_t GL_TLE::DoQuery(TString myquery, TSQLServer *dbc){
2257 TSQLResult *result;
2258 TSQLRow *row = NULL;
2259
2260 // Set the right time_zone (otherwise horrible things will occur! :)
2261 if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;
2262 dbc->Query("SET time_zone = '+0:00'");
2263
2264 // Do the query
2265 this->GetGLTABLES()->AddQ();
2266 result = dbc->Query(myquery.Data());
2267 if(! result->GetRowCount() ) {
2268 cerr << "GL_TLE: query failed: " << myquery.Data() << endl;
2269 return 1;
2270 }
2271
2272 // Get results
2273 row = result->Next(); // first tle row
2274 tle = GiveTle(row);
2275
2276 tleFromTime = strtol(row->GetField(4), NULL, 10);
2277 if (row)
2278 delete row;
2279 row = result->Next(); // second tle row
2280 if(row)
2281 tleToTime = strtol(row->GetField(4), NULL, 10);
2282 else {
2283 cerr << "GL_TLE: Warning: using last avaible TLE. Please update GL_TLE table!\n";
2284 tleToTime = UINT_MAX;
2285 }
2286
2287 if (row)
2288 delete row;
2289 delete result;
2290
2291 return 0;
2292 }
2293
2294
2295 //
2296 // Build a cTle object from the GL_TLE row.
2297 //
2298 cTle* GL_TLE::GiveTle(TSQLRow *row) {
2299 cTle *thistle = NULL;
2300 string tle1, tle2, tle3;
2301
2302 // Build cTle object
2303 tle1 = row->GetField(1);
2304 tle2 = row->GetField(2);
2305 tle3 = row->GetField(3);
2306
2307 thistle = new cTle(tle1, tle2, tle3);
2308
2309 return thistle;
2310 }

  ViewVC Help
Powered by ViewVC 1.1.23