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

  ViewVC Help
Powered by ViewVC 1.1.23