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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.12 - (hide annotations) (download)
Tue Dec 15 09:58:25 2009 UTC (14 years, 11 months ago) by mocchiut
Branch: MAIN
Changes since 1.11: +41 -5 lines
Small Q2TH class upgrade

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

  ViewVC Help
Powered by ViewVC 1.1.23