/[PAMELA software]/YodaProfiler/src/GLTables.cpp
ViewVC logotype

Diff of /YodaProfiler/src/GLTables.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.15 by mocchiut, Tue Oct 31 15:36:04 2006 UTC revision 1.37 by mocchiut, Mon Nov 26 08:38:52 2007 UTC
# Line 10  Line 10 
10  //  //
11  #include <TFile.h>  #include <TFile.h>
12  #include <TTree.h>  #include <TTree.h>
13    #include <TTimeStamp.h>
14  #include <EventHeader.h>  #include <EventHeader.h>
15  #include <PscuHeader.h>  #include <PscuHeader.h>
16  //  //
17  #include <GLTables.h>  #include <GLTables.h>
18  #include <sgp4.h>  #include <sgp4.h>
19  //  //
20    ClassImp(GL_TABLES);
21  ClassImp(GL_TRK_CALIB);  ClassImp(GL_TRK_CALIB);
22  ClassImp(GL_RUN);  ClassImp(GL_RUN);
23  ClassImp(GL_ROOT);  ClassImp(GL_ROOT);
24  ClassImp(GL_PARAM);  ClassImp(GL_PARAM);
25  ClassImp(GL_S4_CALIB);  ClassImp(GL_S4_CALIB);
26  ClassImp(GL_CALO_CALIB);  ClassImp(GL_CALO_CALIB);
27    ClassImp(GL_CALOPULSE_CALIB);
28  ClassImp(GL_TIMESYNC);  ClassImp(GL_TIMESYNC);
29  ClassImp(GL_TLE);  ClassImp(GL_TLE);
30  //  //
31  using namespace std;  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() {  GL_RUN::GL_RUN() {
141    ID                         = 0;    ID                         = 0;
142    ID_RUN_FRAG                = 0;    ID_RUN_FRAG                = 0;
# Line 62  GL_RUN::GL_RUN() { Line 172  GL_RUN::GL_RUN() {
172  }  }
173    
174    
175  void GL_RUN::Clear() {  void GL_RUN::Clear(Option_t *t) {
176    ID                         = 0;    ID                         = 0;
177    ID_RUN_FRAG                = 0;    ID_RUN_FRAG                = 0;
178    ID_ROOT_L0                 = 0;    ID_ROOT_L0                 = 0;
# Line 137  GL_CALO_CALIB::GL_CALO_CALIB(){ Line 247  GL_CALO_CALIB::GL_CALO_CALIB(){
247    FROM_TIME = 0;    FROM_TIME = 0;
248    TO_TIME   = 0;    TO_TIME   = 0;
249    SECTION   = 0;    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(){  GL_S4_CALIB::GL_S4_CALIB(){
# Line 145  GL_S4_CALIB::GL_S4_CALIB(){ Line 274  GL_S4_CALIB::GL_S4_CALIB(){
274    EV_ROOT = 0;    EV_ROOT = 0;
275    FROM_TIME = 0;    FROM_TIME = 0;
276    TO_TIME   = 0;    TO_TIME   = 0;
277    PARAM_FIT0 = 0.;    OBT = 0;
278    PARAM_FIT1 = 0.;    PKT = 0;
279      BOOT_NUMBER                = 0;
280  }  }
281    
282  GL_TIMESYNC::GL_TIMESYNC(){  GL_TIMESYNC::GL_TIMESYNC(){
283    obtfirst = 0;    obtfirst = 0;
284    pktfirst = 0;    pktfirst = 0;
285    toffset  = 0;    ID_RESURS_OFFSET  = 0;
286    ID       = 0;    ID       = 0;
287    ID_RAW   = 0;    ID_RAW   = 0;
288    OBT0     = 0;    OBT0     = 0;
# Line 160  GL_TIMESYNC::GL_TIMESYNC(){ Line 290  GL_TIMESYNC::GL_TIMESYNC(){
290    TYPE     = 0;    TYPE     = 0;
291  }  }
292    
 GL_TLE::GL_TLE(){  
 }  
   
293  // ****************************************************  // ****************************************************
294    
295  void GL_RUN::SetEV_FROM(UInt_t evfrom){  void GL_RUN::SetEV_FROM(UInt_t evfrom){
# Line 427  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 554  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
554    //    //
555    //  printf("1myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
560    //    //
561    // retrieve this ID_TRASH    // retrieve this ID_TRASH
562    //    //
563    myquery.str("");    myquery.str("");
564    myquery << " SELECT ID_TRASH,ID_ROOT_L0,ID_ROOT_L2 FROM GL_RUN_TRASH ORDER BY ID_TRASH DESC LIMIT 1";      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());    pResult = dbc->Query(myquery.str().c_str());
568    //    //
569    UInt_t idtrash = 0;    UInt_t idtrash = 0;
# Line 454  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 585  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
585    //    //
586    //  printf("2myquery is %s \n",myquery.str().c_str());    //  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());    pResult = dbc->Query(myquery.str().c_str());
591    //    //
592    Row = pResult->Next();          Row = pResult->Next();      
# Line 469  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 602  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
602    //    //
603    //  printf("3myquery is %s \n",myquery.str().c_str());    //  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());    pResult = dbc->Query(myquery.str().c_str());
608    //    //
609    Row = pResult->Next();          Row = pResult->Next();      
# Line 485  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 620  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
620    //    //
621    //  printf("4myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
626    //    //
627    myquery.str("");    myquery.str("");
# Line 494  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 631  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
631    //    //
632    //  printf("4myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
637    //    //
638    myquery.str("");    myquery.str("");
# Line 504  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 643  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
643    //  printf("4myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
649    //    //
650    myquery.str("");    myquery.str("");
# Line 514  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 655  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
655    //  printf("5myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
661    //    //
662    return 0;    return 0;
# Line 545  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 688  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
688      //      //
689      printf(" qui? myquery is %s \n",myquery.str().c_str());      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());      pResult = dbc->Query(myquery.str().c_str());
694      //      //
695      Row = pResult->Next();            Row = pResult->Next();      
# Line 628  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 773  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
773    //    //
774    //  printf("5myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
779    //    //
780    //    //
# Line 635  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 782  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
782    myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID=";    myquery << " DELETE FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID=";
783    myquery << IDRUN << ";";    myquery << IDRUN << ";";
784    //    //
785      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
786      this->GetGLTABLES()->AddQ();
787    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
788    //    //
789    return 0;    return 0;
# Line 717  Int_t GL_RUN::Fill_GL_RUN(TSQLServer *db Line 866  Int_t GL_RUN::Fill_GL_RUN(TSQLServer *db
866    //    //
867    //  printf("myquery is %s \n",myquery.str().c_str());    //  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());    dbc->Query(myquery.str().c_str());
872    //    //
873    return 0;    return 0;
# Line 793  Int_t GL_RUN::Fill_GL_RUN_FRAGMENTS(TSQL Line 944  Int_t GL_RUN::Fill_GL_RUN_FRAGMENTS(TSQL
944    //    //
945    // printf("myquery is %s \n",myquery.str().c_str());    // 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());    dbc->Query(myquery.str().c_str());
950    //    //
951    return 0;    return 0;
# Line 850  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T Line 1003  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T
1003    myquery << " from GL_RUN where ID=" << run << ";";    myquery << " from GL_RUN where ID=" << run << ";";
1004    //    //
1005    //  printf(" myquery is %s \n",myquery.str().c_str());    //  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());    pResult = dbc->Query(myquery.str().c_str());
1009    //    //
1010    //  printf(" getrowcount %i \n",pResult->GetRowCount());    //  printf(" getrowcount %i \n",pResult->GetRowCount());
# Line 861  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T Line 1016  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T
1016        if( Row == NULL ) break;        if( Row == NULL ) break;
1017  //        Set_GL_RUN(Row);  //        Set_GL_RUN(Row);
1018        for( t = 0; t < pResult->GetFieldCount(); t++){        for( t = 0; t < pResult->GetFieldCount(); t++){
1019                    if (t== 0) ID                = (UInt_t)atoll(Row->GetField(t));          if (t== 0) ID                = (UInt_t)atoll(Row->GetField(t));
1020                    if (t== 1) ID_RUN_FRAG       = (UInt_t)atoll(Row->GetField(t));          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));          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));          if (t== 3) ID_ROOT_L2        = (UInt_t)atoll(Row->GetField(t));
1023                    if (t== 4) RUNHEADER_TIME    = (UInt_t)atoll(Row->GetField(t));          if (t== 4) RUNHEADER_TIME    = (UInt_t)atoll(Row->GetField(t));
1024                    if (t== 5) RUNTRAILER_TIME   = (UInt_t)atoll(Row->GetField(t));          if (t== 5) RUNTRAILER_TIME   = (UInt_t)atoll(Row->GetField(t));
1025                    if (t== 6) RUNHEADER_OBT     = (UInt_t)atoll(Row->GetField(t));          if (t== 6) RUNHEADER_OBT     = (UInt_t)atoll(Row->GetField(t));
1026                    if (t== 7) RUNTRAILER_OBT    = (UInt_t)atoll(Row->GetField(t));          if (t== 7) RUNTRAILER_OBT    = (UInt_t)atoll(Row->GetField(t));
1027                    if (t== 8) RUNHEADER_PKT     = (UInt_t)atoll(Row->GetField(t));          if (t== 8) RUNHEADER_PKT     = (UInt_t)atoll(Row->GetField(t));
1028                    if (t== 9) RUNTRAILER_PKT    = (UInt_t)atoll(Row->GetField(t));          if (t== 9) RUNTRAILER_PKT    = (UInt_t)atoll(Row->GetField(t));
1029                    if (t==10) EV_FROM           = (UInt_t)atoll(Row->GetField(t));          if (t==10) EV_FROM           = (UInt_t)atoll(Row->GetField(t));
1030                    if (t==11) EV_TO             = (UInt_t)atoll(Row->GetField(t));          if (t==11) EV_TO             = (UInt_t)atoll(Row->GetField(t));
1031                    if (t==12) NEVENTS           = (UInt_t)atoll(Row->GetField(t));          if (t==12) NEVENTS           = (UInt_t)atoll(Row->GetField(t));
1032                    if (t==13) LAST_TIMESYNC     = (UInt_t)atoll(Row->GetField(t));          if (t==13) LAST_TIMESYNC     = (UInt_t)atoll(Row->GetField(t));
1033                    if (t==14) OBT_TIMESYNC      = (UInt_t)atoll(Row->GetField(t));          if (t==14) OBT_TIMESYNC      = (UInt_t)atoll(Row->GetField(t));
1034                    if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));          if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));
1035                    if (t==16) FAV_WRK_SCHEDULE  = (UInt_t)atoll(Row->GetField(t));          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));          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));          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));          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));          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));          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));          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));          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));          if (t==24) PKT_COUNTER       = (UInt_t)atoll(Row->GetField(t));
1044                    if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));          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));          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));          if (t==27) CAL_DSP_MASK      = (UInt_t)atoll(Row->GetField(t));
1047                    if (t==28) BOOT_NUMBER       = (UInt_t)atoll(Row->GetField(t));          if (t==28) BOOT_NUMBER       = (UInt_t)atoll(Row->GetField(t));
1048                    if (t==29) VALIDATION        = (UInt_t)atoll(Row->GetField(t));          if (t==29) VALIDATION        = (UInt_t)atoll(Row->GetField(t));
1049        };        };
1050    };    };
1051    //  delete pResult;    //  delete pResult;
# Line 948  Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TSt Line 1103  Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TSt
1103    //    //
1104    //  printf(" query is %s \n",myquery.str().c_str());    //  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());    pResult = dbc->Query(myquery.str().c_str());
1109    if(!pResult->GetRowCount())return(-50);    if(!pResult->GetRowCount())return(-50);
1110    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1013  Int_t GL_ROOT::Query_GL_ROOT(UInt_t id, Line 1170  Int_t GL_ROOT::Query_GL_ROOT(UInt_t id,
1170    myquery << ",NAME";    myquery << ",NAME";
1171    myquery << " from GL_ROOT where ID=" << id << ";";        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());    pResult = dbc->Query(myquery.str().c_str());
1176    if(!pResult->GetRowCount())return (-51);    if(!pResult->GetRowCount())return (-51);
1177    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1022  Int_t GL_ROOT::Query_GL_ROOT(UInt_t id, Line 1181  Int_t GL_ROOT::Query_GL_ROOT(UInt_t id,
1181            if(t==0) ID     = (UInt_t)atoll(Row->GetField(t));            if(t==0) ID     = (UInt_t)atoll(Row->GetField(t));
1182            if(t==1) ID_RAW = (UInt_t)atoll(Row->GetField(t));            if(t==1) ID_RAW = (UInt_t)atoll(Row->GetField(t));
1183            if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t));            if(t==2) ID_TIMESYNC = (UInt_t)atoll(Row->GetField(t));
1184            if(t==3) PATH   = Row->GetField(t);            if(t==3) PATH   = (TString)gSystem->ExpandPathName(Row->GetField(t))+'/';
1185            if(t==4) NAME   = Row->GetField(t);            if(t==4) NAME   = Row->GetField(t);
1186        };        };
1187    };    };
# Line 1047  Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(U Line 1206  Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(U
1206    // ----------------    // ----------------
1207    myquery.str("");    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;    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;";        //  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;";          //  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());    pResult = dbc->Query(myquery.str().c_str());
1216    if(!pResult->GetRowCount())return (-53);    if(!pResult->GetRowCount())return (-53);
1217    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1106  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1269  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1269    myquery << " ;";    myquery << " ;";
1270    //myquery << " and VALIDATION=1;";    //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());    pResult = dbc->Query(myquery.str().c_str());
1275    //  printf(" mysquery is %s\n",myquery.str().c_str());    //  printf(" mysquery is %s\n",myquery.str().c_str());
1276    //    //
# Line 1115  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1280  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1280    //    //
1281    uptime = (UInt_t)atoll(Row->GetField(2));    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...    // 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    if( atoi(Row->GetField(4)) == 0 ){ // if validation = 0
# Line 1125  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1293  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1293      myquery << "select ID_ROOT_L0, FROM_TIME, TO_TIME, EV_ROOT,VALIDATION from GL_CALO_CALIB where SECTION=" << section;      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;      myquery << " and FROM_TIME <= " << time;
1295      myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";      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());      pResult = dbc->Query(myquery.str().c_str());
1321      //    printf(" mysquery is %s\n",myquery.str().c_str());      //    printf(" mysquery is %s\n",myquery.str().c_str());
1322      //      //
# Line 1141  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1333  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1333    if( Row == NULL ) return (-54);    if( Row == NULL ) return (-54);
1334    for( t = 0; t < pResult->GetFieldCount(); t++){    for( t = 0; t < pResult->GetFieldCount(); t++){
1335      if (t==0) ID_ROOT_L0  = (UInt_t)atoll(Row->GetField(t));      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));      if (t==1) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1383      if (t==2) TO_TIME   = (UInt_t)atoll(Row->GetField(t));                  if (t==2) TO_TIME   = (UInt_t)atoll(Row->GetField(t));            
1384      if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t));      if (t==3) EV_ROOT = (UInt_t)atoll(Row->GetField(t));
# Line 1148  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1386  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1386    pResult->Delete();    pResult->Delete();
1387    return 0;    return 0;
1388  };  };
1389    
1390    
1391  // ****************************************************  // ****************************************************
1392  /**  /**
1393   * Function to query the GL_S4_CALIB table of the DB.   * Function to query the GL_S4_CALIB table of the DB.
# Line 1164  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn Line 1404  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn
1404    stringstream myquery;    stringstream myquery;
1405    // ----------------    // ----------------
1406    myquery.str("");    myquery.str("");
1407    myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time;    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;";          myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";      
1409    //  myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";          //  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());    pResult = dbc->Query(myquery.str().c_str());
1413    if(!pResult->GetRowCount())return (-55);//throw -55;    if(!pResult->GetRowCount())return (-55);//throw -55;
1414    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1174  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn Line 1416  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn
1416        if( Row == NULL ) break;        if( Row == NULL ) break;
1417        for( t = 0; t < pResult->GetFieldCount(); t++){        for( t = 0; t < pResult->GetFieldCount(); t++){
1418          if (t==0) ID               = (UInt_t)atoll(Row->GetField(t));          if (t==0) ID               = (UInt_t)atoll(Row->GetField(t));
1419            if (t==1) ID_ROOT_L0  = (UInt_t)atoll(Row->GetField(t));          if (t==1) ID_ROOT_L0  = (UInt_t)atoll(Row->GetField(t));
1420            if (t==2) EV_ROOT    = (UInt_t)atoll(Row->GetField(t));          if (t==2) EV_ROOT    = (UInt_t)atoll(Row->GetField(t));
1421            if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t));          if (t==3) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
1422            if (t==4) TO_TIME   = (UInt_t)atoll(Row->GetField(t));          if (t==4) TO_TIME   = (UInt_t)atoll(Row->GetField(t));
           if (t==5) PARAM_FIT0   = atof(Row->GetField(t));  
           if (t==6) PARAM_FIT1   = atof(Row->GetField(t));  
1423        };        };
1424    };    };
1425    delete pResult;        delete pResult;    
# Line 1207  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti Line 1447  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti
1447    // ----------------        // ----------------    
1448    myquery.str("");    myquery.str("");
1449    myquery << " select ";    myquery << " select ";
1450    myquery << " ID, PATH, NAME, DESCR, TYPE, FROM_TIME,TO_TIME ";    myquery << " ID, PATH, NAME, DESCR, FROM_TIME,TO_TIME, TYPE ";
1451    myquery << " from GL_PARAM ";    myquery << " from GL_PARAM ";
1452    myquery << " where TYPE = '"<<type<<"' ";    myquery << " where TYPE = '"<<type<<"' ";
1453    myquery << " and FROM_TIME <= " << time;    myquery << " and FROM_TIME <= " << time;
1454    myquery << " ORDER BY TO_TIME DESC LIMIT 1;";      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());    pResult = dbc->Query(myquery.str().c_str());
1459    if(!pResult->GetRowCount())return (-52);    if(!pResult->GetRowCount())return (-52);
1460    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1220  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti Line 1462  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti
1462        if( Row == NULL ) break;        if( Row == NULL ) break;
1463        for( t = 0; t < pResult->GetFieldCount(); t++){        for( t = 0; t < pResult->GetFieldCount(); t++){
1464          if (t==0) ID        = (UInt_t)atoll(Row->GetField(t));          if (t==0) ID        = (UInt_t)atoll(Row->GetField(t));
1465            if (t==1) PATH      = Row->GetField(t);// put in fpath the path to that file          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);            if (t==2) NAME      = Row->GetField(t);
1467            if (t==3) DESCR     = Row->GetField(t);            if (t==3) DESCR     = Row->GetField(t);
1468            if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t));            if (t==4) FROM_TIME = (UInt_t)atoll(Row->GetField(t));
# Line 1266  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1508  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1508    TSQLRow *Row;    TSQLRow *Row;
1509    stringstream myquery;    stringstream myquery;
1510    stringstream rname;    stringstream rname;
1511      //  pcksList packetsNames;
1512      //  pcksList::iterator Iter;
1513      //  getPacketsNames(packetsNames);
1514    rname.str("");    rname.str("");
1515    // ----------------    // ----------------
1516    myquery.str("");    myquery.str("");
# Line 1276  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1521  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1521    myquery << type.Data();    myquery << type.Data();
1522    myquery << "=" << id << ";";        myquery << "=" << id << ";";    
1523    //    //
1524      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1525      this->GetGLTABLES()->AddQ();
1526    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1527    if( pResult->GetRowCount() ){    if( pResult->GetRowCount() ){
1528      Row = pResult->Next();            Row = pResult->Next();      
1529      if( Row ){      if( Row ){
1530        stringstream fname;        stringstream fname;
1531        fname.str("");        fname.str("");
1532        fname << Row->GetField(0) << "/" << Row->GetField(1);        fname << gSystem->ExpandPathName(Row->GetField(0)) << "/" << Row->GetField(1);
1533        rname << Row->GetField(1);        rname << Row->GetField(1);
1534        file = new TFile(fname.str().c_str(),"READ");        file = new TFile(fname.str().c_str(),"READ");
1535        idraw = (UInt_t)atoll(Row->GetField(2));        idraw = (UInt_t)atoll(Row->GetField(2));
# Line 1299  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1546  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1546      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
1547      pktfirst = ph->GetCounter();      pktfirst = ph->GetCounter();
1548      obtfirst = ph->GetOrbitalTime();        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    // look for Resurs offset
1573    //    //
1574    UInt_t t0 = 0;    T0 = 0;
1575    //    //
1576    //    //
1577    stringstream oss;    stringstream oss;
1578      TString frn = rname.str().c_str();
1579      frn = frn.ReplaceAll(".root",5,".pam",4);
1580    oss.str("");    oss.str("");
1581    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"    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        << rname.str().c_str() << "';";        << frn.Data() << "';";
1583    //  if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1584      this->GetGLTABLES()->AddQ();
1585    pResult = dbc->Query(oss.str().c_str());    pResult = dbc->Query(oss.str().c_str());
1586    Row = pResult->Next();    Row = pResult->Next();
1587    //    //
1588    TString name=rname.str().c_str();    TString name=rname.str().c_str();
1589    UInt_t dworbit = 0;    UInt_t dworbit = 0;
1590    Int_t nlength = name.Length();    Int_t nlength = name.Length();
1591    if ( nlength < 5 ) return;    //
1592    TString dwo = 0;    // Is not a special file
   for (Int_t i = 0; i<5; i++){  
     dwo.Append(name[i],1);  
   };  
   if ( dwo.IsDigit() ){  
     dworbit = (UInt_t)dwo.Atoi();  
   } else {  
     dwo="";  
     for (Int_t i = 8; i<13; i++){  
       dwo.Append(name[i],1);  
     };      
     if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();  
   };  
   // if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());    
1593    //    //
1594    if ( !Row ){    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("");      oss.str("");
1600      oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "      oss << "SELECT ID_RESURS_OFFSET from GL_TIMESYNC where ID_RAW=" << idraw <<";";
1601          << dworbit << " order by FROM_ORBIT desc limit 1;";      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1602      //    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      this->GetGLTABLES()->AddQ();
1603      pResult = dbc->Query(oss.str().c_str());      pResult = dbc->Query(oss.str().c_str());
1604      Row = pResult->Next();      Bool_t fndit = false;
1605      if ( !Row ){      if ( pResult ){
1606        printf(" ERROR FROM GLTables! cannot determine Resurs offset \n");        Row = pResult->Next();
1607        return;        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    t0 = (UInt_t)TDatime(Row->GetField(0)).Convert();    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();
   //  myquery.str("");  
   //   myquery << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "  
   //       << idraw << " AND TO_ID_RAW >= "  
   //       << idraw << ";";  
   //   pResult = dbc->Query(myquery.str().c_str());  
   //   if ( pResult ){  
   //     Row = pResult->Next();  
   //     if ( Row ){  
   //       //  
   //       t0 = (UInt_t)TDatime(Row->GetField(0)).Convert();  
   //       //  
   //     };  
   //   };  
   //  
1666    //    //
1667    // look for the correct timesync entry    // look for the correct timesync entry
1668    //    //
1669    myquery.str("");    myquery.str("");
1670    myquery << " SELECT OBT0,TIMESYNC FROM GL_TIMESYNC "    myquery << " SELECT OBT0,TIMESYNC,TYPE FROM GL_TIMESYNC "
1671        << " WHERE ID_RAW = " << idraw        << " WHERE ID_RAW = " << idraw
1672        << ";";        << ";";
1673      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1674      this->GetGLTABLES()->AddQ();
1675    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1676    if ( pResult ){    if ( pResult ){
1677      Row = pResult->Next();      Row = pResult->Next();
1678      if ( (Row != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){      if ( (Row != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){
1679        toffset = (UInt_t)atoll(Row->GetField(1)) - (UInt_t)(this->DBobt((UInt_t)atoll(Row->GetField(0)))/1000) + t0;        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    //    //
# Line 1392  UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT Line 1698  UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT
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  ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){    
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 ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ){
1719        return((Long64_t)pkt_num+16777215LL);
1720      };
1721    //    //
1722    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
1723      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      return((Long64_t)pkt_num-16777215LL);
       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));  
     } else {  
       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));  
     };  
1724    };    };
1725    //    //
1726    return((ULong64_t)obt);    return((Long64_t)pkt_num);
1727    //    //
1728  };  };
1729    
1730  UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){    /**
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 ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
1736    //    //
1737    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1738      if ( (pkt_num-16777215) < 0 ){      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
       return((16777215-pkt_num));  
     } else {  
       return((pkt_num-16777215));  
     };  
1739    };    };
1740    //    //
1741    return(pkt_num);    return((Long64_t)obt);
   //  
1742  };  };
1743    
1744    
1745    
1746    // ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){  
1747    //   //
1748    //   if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));
1749    //   //
1750    //   if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1751    //     if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){
1752    //       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));
1753    //     } else {
1754    //       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));
1755    //     };
1756    //   };
1757    //   //
1758    //   return((ULong64_t)obt);
1759    //   //
1760    // };
1761    
1762    // UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){  
1763    //   //
1764    //   if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));
1765    //   //
1766    //   if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
1767    //     if ( (pkt_num-16777215) < 0 ){
1768    //       return((16777215-pkt_num));
1769    //     } else {
1770    //       return((pkt_num-16777215));
1771    //     };
1772    //   };
1773    //   //
1774    //   return(pkt_num);
1775    //   //
1776    // };
1777    
1778  /*  /*
1779   *   *
1780   * Convert the time in the DB from UInt_t to a string   * Convert the time in the DB from UInt_t to a string
# Line 1435  UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num Line 1785  UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num
1785   */   */
1786  TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){  TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){
1787    //    //
1788    TDatime *time = new TDatime();    Int_t offset = 0;
1789    TString rtime;    TString rtime;
1790    Bool_t found = false;    Bool_t found = false;
1791    //    //
   time->Set(dbt,false); // UTC (Coordinated Universal Time)    
   //  
1792    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
1793      //      //
1794      UInt_t timeMSK = time->Convert() + 60*60*3; // UTC (Coordinated Universal Time) + 3hs = Moscow Winter Time      offset = 60*60*3; // UTC (Coordinated Universal Time) + 3hs = Moscow Winter Time
     time->Set(timeMSK,false);  
1795      found = true;      found = true;
1796      //      //
1797    };    };
1798    //    //
1799    if ( !strcmp(tzone.Data(),"CET") ){    if ( !strcmp(tzone.Data(),"CET") ){
1800      //      //
1801      UInt_t timeCET = time->Convert() + 60*60*1; // CET (Central European Time) = UTC + 1 hs      offset = 60*60*1; // CET (Central European Time) = UTC + 1 hs
     time->Set(timeCET,false);  
1802      found = true;      found = true;
1803      //      //
1804    };    };
1805    //    //
1806    if ( !strcmp(tzone.Data(),"CEST") ){    if ( !strcmp(tzone.Data(),"CEST") ){
1807      //      //
1808      UInt_t timeCEST = time->Convert() + 60*60*2; // CEST (Central European Summer Time) = UTC + 2 h      offset = 60*60*2; // CEST (Central European Summer Time) = UTC + 2 h
     time->Set(timeCEST,false);  
1809      found = true;      found = true;
1810      //      //
1811    };    };
1812    //    //
1813    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){
1814      //      //
1815      UInt_t timeMSD = time->Convert() + 60*60*4; // MSD (Moscow Summer Time) = UTC + 4 h      offset = 60*60*4; // MSD (Moscow Summer Time) = UTC + 4 h
     time->Set(timeMSD,false);  
1816      found = true;      found = true;
1817      //      //
1818    };    };
# Line 1478  TString GL_TIMESYNC::ConvertTime(TString Line 1822  TString GL_TIMESYNC::ConvertTime(TString
1822      tzone = "UTC";      tzone = "UTC";
1823    };    };
1824    //    //
1825    rtime = time->AsSQLString();    dbt += offset;
1826      //
1827      TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
1828      //
1829      rtime = time->AsString("s");
1830      //
1831      delete time;
1832    //    //
1833    return(rtime);    return(rtime);
1834  }  }
1835    
1836  /*  /*
1837   *   *
1838   * Convert the time in the DB from UInt_t to a string   * Convert the time from TZONE to UTC
1839   *   *
1840   * @param dbt time in the DB   * @param dbt time in the DB
1841   * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK   * @param tzone Time Zone, can be UTC,GMT,CET,CEST,MSD default is MSK
# Line 1493  TString GL_TIMESYNC::ConvertTime(TString Line 1843  TString GL_TIMESYNC::ConvertTime(TString
1843   */   */
1844  TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){  TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){
1845    //    //
1846    TDatime *time = new TDatime();    Int_t offset = 0;
1847    TString rtime;    TString rtime;
1848    //    //
1849    Bool_t found = false;    Bool_t found = false;
1850    //    //
   time->Set(dbt,false); // tzone  
   //  
1851    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
1852      //      //
1853      UInt_t timeUTC = time->Convert() - 60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3hs      offset = -60*60*3; // UTC (Coordinated Universal Time) = Moscow Winter Time - 3hs
     time->Set(timeUTC,false);  
1854      found = true;      found = true;
1855      //      //
1856    };    };
1857    //    //
1858    if ( !strcmp(tzone.Data(),"CET") ){    if ( !strcmp(tzone.Data(),"CET") ){
1859      //      //
1860      UInt_t timeCET = time->Convert() - 60*60*1; // CET (Central European Time) - 1 hs = UTC        offset = -60*60*1; // CET (Central European Time) - 1 hs = UTC  
     time->Set(timeCET,false);  
1861      found = true;      found = true;
1862      //      //
1863    };    };
1864    //    //
1865    if ( !strcmp(tzone.Data(),"CEST") ){    if ( !strcmp(tzone.Data(),"CEST") ){
1866      //      //
1867      UInt_t timeCEST = time->Convert() - 60*60*2; // CEST (Central European Summer Time) - 2 h = UTC      offset = -60*60*2; // CEST (Central European Summer Time) - 2 h = UTC
     time->Set(timeCEST,false);  
1868      found = true;      found = true;
1869      //      //
1870    };    };
1871    //    //
1872    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){
1873      //      //
1874      UInt_t timeMSD = time->Convert() - 60*60*4; // MSD (Moscow Summer Time) - 4 h = UTC      offset = -60*60*4; // MSD (Moscow Summer Time) - 4 h = UTC
     time->Set(timeMSD,false);  
1875      found = true;      found = true;
1876      //      //
1877    };    };
1878    //    //
1879    if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){    if ( !found && strcmp(tzone.Data(),"UTC") && strcmp(tzone.Data(),"GMT") && strcmp(tzone.Data(),"") ){
1880      //      //
1881        offset = 0;
1882      printf("\n Unknown time zone %s using UTC \n",tzone.Data());      printf("\n Unknown time zone %s using UTC \n",tzone.Data());
1883      tzone = "UTC";      tzone = "UTC";
1884    };    };
1885    //    //
1886    rtime = time->AsSQLString();    dbt += offset;
1887      TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
1888      //
1889      rtime = time->AsString("s");
1890    //    //
1891    return(rtime);    return(rtime);
1892  }  }
1893    
1894    
1895  // ****************************************************  //
1896  /**  // Build a query and call DoQuery.
1897   * Function to query the GL_TLE table of the DB.  //
1898   *  // date it's an SQL datetime date and dbc is the connection to be
1899   * time is the unix time for which a good tle is requested.  // used.  It will query for the tle with the nearest but previous date
1900   *  // and the immediatly next one.
1901   * Return the pointer to a cTle object that has the closest and  //
1902   * previous date compared with time.  Int_t GL_TLE::Query(TString date, TSQLServer *dbc){
  *  
  * If errors occurs it returns NULL.  
  */  
 cTle* GL_TLE::Query_GL_TLE(UInt_t time, TSQLServer *dbc){  
1903    stringstream myquery;    stringstream myquery;
1904    myquery.str("");    myquery.str("");
1905    
1906    myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE "    myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1907            << "WHERE FROM_TIME < FROM_UNIXTIME(" << time << ") ORDER BY FROM_TIME DESC LIMIT 1;";              << "WHERE FROM_TIME <= '" << date.Data()
1908              << "' ORDER BY FROM_TIME DESC LIMIT 1) "
1909              << "UNION "
1910              << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1911              << "WHERE FROM_TIME > '" << date.Data()
1912              << "' ORDER BY FROM_TIME ASC LIMIT 1)";
1913    
1914    return Query_GL_TLE_go(myquery.str(), dbc);    return DoQuery(myquery.str().c_str(), dbc);
1915  }  }
1916    
1917    
1918  // ****************************************************  //
1919  /**  // Build a query and call DoQuery.
1920   * Function to query the GL_TLE table of the DB.  //
1921   *  // time is the UTC date in unix time (UTC) and dbc is the connection
1922   * date is a datetime format YYYY-MM-DD hh:mm:ss for which a good tle  // to be used.  It will query for the tle with the nearest but
1923   * is requested.  // previous date and the immediatly next one.
1924   *  //
1925   * Return the pointer to a cTle object that has the closest and  // Returns the value returned by DoQuery().
1926   * previous date compared with time.  //
1927   *  Int_t GL_TLE::Query(UInt_t time, TSQLServer *dbc){
  * If errors occurs it returns NULL.  
  */  
 cTle* GL_TLE::Query_GL_TLE(TString date, TSQLServer *dbc){  
1928    stringstream myquery;    stringstream myquery;
1929    myquery.str("");    myquery.str("");
1930    
1931    myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE "    myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1932            << "WHERE FROM_TIME < '" << date.Data() << "' ORDER BY FROM_TIME DESC LIMIT 1;";              << "WHERE FROM_TIME <= FROM_UNIXTIME('" << time
1933              << "') ORDER BY FROM_TIME DESC LIMIT 1) "
1934              << "UNION "
1935              << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1936              << "WHERE FROM_TIME > FROM_UNIXTIME('" << time
1937              << "') ORDER BY FROM_TIME ASC LIMIT 1)";
1938    
1939      return DoQuery(myquery.str().c_str(), dbc);
   return Query_GL_TLE_go(myquery.str(), dbc);  
1940  }  }
1941    
1942    
1943  // ****************************************************  //
1944  /**  // Do the query myquery on the connectio dbc.  Initialize tle,
1945   * Private function used by Query_GL_TLE methods.  // tleFromTime and tleToTime.
1946   *  //
1947   * myquery is the query string.  // We should have two rows (unless the table is old).  From the last
1948   *  // one we only take tleToTime.
1949   * Return the pointer to a cTle object that has the closest and  //
1950   * previous date compared with time.  // Returns 0 for success, 1 for failure.
1951   *  //
1952   * If errors occurs it returns NULL.  Int_t GL_TLE::DoQuery(TString myquery, TSQLServer *dbc){
  */  
 cTle* GL_TLE::Query_GL_TLE_go(TString myquery, TSQLServer *dbc){  
   cTle *tle;  
   string tle1, tle2, tle3;  
   
   // MySQL variables  
1953    TSQLResult *result;    TSQLResult *result;
1954    TSQLRow *row;    TSQLRow *row;
1955    
1956      // Set the right time_zone (otherwise horrible things will occur! :)
1957      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1958      dbc->Query("SET time_zone = '+0:00'");
1959    
1960      // Do the query
1961      this->GetGLTABLES()->AddQ();
1962    result = dbc->Query(myquery.Data());    result = dbc->Query(myquery.Data());
1963    if(! result->GetRowCount() ) return NULL;    if(! result->GetRowCount() ) {
1964        cerr << "GL_TLE: query failed: " << myquery.Data() << endl;
1965        return 1;
1966      }
1967    
1968      // Get results
1969      row = result->Next(); // first tle row
1970      tle = GiveTle(row);
1971    
1972      tleFromTime = strtol(row->GetField(4), NULL, 10);
1973    
1974      row = result->Next(); // second tle row
1975      if(row)
1976        tleToTime = strtol(row->GetField(4), NULL, 10);
1977      else {
1978        cerr << "GL_TLE: Warning: using last avaible TLE. Please update GL_TLE table!\n";
1979        tleToTime = UINT_MAX;
1980      }
1981    
1982    row = result->Next();    delete row;
1983    tle1 = row->GetField(0);    delete result;
   tle2 = row->GetField(1);  
   tle3 = row->GetField(2);  
1984    
1985    tle = new cTle(tle1, tle2, tle3);    return 0;
1986    }
1987    
   delete result;  
   delete row;  
1988    
1989    return tle;  //
1990    // Build a cTle object from the GL_TLE row.
1991    //
1992    cTle* GL_TLE::GiveTle(TSQLRow *row) {
1993      cTle *thistle = NULL;
1994      string tle1, tle2, tle3;
1995    
1996      // Build cTle object
1997      tle1 = row->GetField(1);
1998      tle2 = row->GetField(2);
1999      tle3 = row->GetField(3);
2000    
2001      thistle = new cTle(tle1, tle2, tle3);
2002    
2003      return thistle;
2004  }  }

Legend:
Removed from v.1.15  
changed lines
  Added in v.1.37

  ViewVC Help
Powered by ViewVC 1.1.23