/[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.3 - (hide annotations) (download)
Mon Sep 29 12:41:58 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Changes since 1.2: +1 -1 lines
Calorimeter first calibration bug fixed

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

  ViewVC Help
Powered by ViewVC 1.1.23