/[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.20 - (show annotations) (download)
Mon Jan 19 12:34:16 2015 UTC (9 years, 10 months ago) by mocchiut
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +8 -0 lines
Memory leak fixed in pamela::mapval, pamela::mappa, pamela::EventCounter, new IGRF12 parameters added in DB

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

  ViewVC Help
Powered by ViewVC 1.1.23