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

  ViewVC Help
Powered by ViewVC 1.1.23