/[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.1 - (hide annotations) (download)
Tue Sep 23 07:20:33 2008 UTC (16 years, 2 months ago) by mocchiut
Branch: MAIN
Branch point for: v0r00
Initial revision

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

  ViewVC Help
Powered by ViewVC 1.1.23