/[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.10 - (hide annotations) (download)
Wed Aug 5 18:48:44 2009 UTC (15 years, 4 months ago) by pam-fi
Branch: MAIN
Changes since 1.9: +1 -0 lines
Various minor modifications for compatibility with gcc 4.4, removal of warnings due to mismatch between char* and const char*, bug fix.

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

  ViewVC Help
Powered by ViewVC 1.1.23