/[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.23 by mocchiut, Wed Feb 14 10:54:59 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);
# Line 27  ClassImp(GL_TLE); Line 29  ClassImp(GL_TLE);
29  //  //
30  using namespace std;  using namespace std;
31    
32    GL_TABLES::GL_TABLES(){
33    };
34    
35    GL_TABLES::GL_TABLES(TString host, TString user, TString psw){
36      fHost = new TString(host.Data());
37      fUser = new TString(user.Data());
38      fPsw = new TString(psw.Data());
39      fSet = true;
40      fNquery = 0;
41    };
42    
43    
44    void GL_TABLES::Set(TString host, TString user, TString psw){
45      fHost = new TString(host.Data());
46      fUser = new TString(user.Data());
47      fPsw = new TString(psw.Data());
48      fSet = true;
49      fNquery = 0;
50    };
51    
52    Bool_t GL_TABLES::IsConnected(TSQLServer *dbc){
53      //
54      //
55      //
56      if ( !fSet ){
57        return true;
58      };
59      //
60      //
61      //
62      //  printf(" host is %s \n",fHost->Data());
63      //
64      stringstream myquery;  
65      myquery.str("");
66      myquery << "show databases;";
67      dbc->Query(myquery.str().c_str());
68      fNquery++;
69      //
70      if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){
71        // is connected
72        return true;
73      } else {
74        //
75        printf(" WARNING: Lost connection to DB! try to recover... \n");
76        //
77        TString host = fHost->Data();
78        TString user = fUser->Data();
79        TString psw = fPsw->Data();
80        delete dbc;
81        dbc = TSQLServer::Connect(host.Data(),user.Data(),psw.Data());
82        //
83        myquery.str("");
84        myquery << "show databases;";
85        dbc->Query(myquery.str().c_str());
86        fNquery++;
87        //
88        if ( dbc->GetErrorCode() != 2013 && dbc->GetErrorCode() != 2006 ){
89          //
90          printf(" ...connection recovered, I can continue! \n");
91          //
92          myquery.str("");
93          myquery << "SET time_zone='+0:00'";
94          dbc->Query(myquery.str().c_str());
95          fNquery++;
96          myquery.str("");
97          myquery << "SET wait_timeout=173000;";
98          dbc->Query(myquery.str().c_str());      
99          fNquery++;
100          return true;
101        };
102      };
103      //
104      printf(" GLTABLES: connection is gone away, query will fail\n");
105      //
106      return false;
107      //
108    };
109    
110    void GL_TABLES::ResetCounters(){
111      fNquery = 0;
112    };
113    
114    void GL_TABLES::AddQ(){
115      if ( fSet ) fNquery++;
116    };
117    
118    UInt_t GL_TABLES::GetNqueries(){
119       UInt_t rn = 0;
120       rn += (UInt_t&)fNquery;
121       return(rn);
122    };
123    
124  GL_RUN::GL_RUN() {  GL_RUN::GL_RUN() {
125    ID                         = 0;    ID                         = 0;
126    ID_RUN_FRAG                = 0;    ID_RUN_FRAG                = 0;
# Line 153  GL_TIMESYNC::GL_TIMESYNC(){ Line 247  GL_TIMESYNC::GL_TIMESYNC(){
247    obtfirst = 0;    obtfirst = 0;
248    pktfirst = 0;    pktfirst = 0;
249    toffset  = 0;    toffset  = 0;
250    ID       = 0;    //  ID       = 0;
251    ID_RAW   = 0;    //  ID_RAW   = 0;
252    OBT0     = 0;    OBT0     = 0;
253    TIMESYNC = 0;    TIMESYNC = 0;
254    TYPE     = 0;    TYPE     = 0;
255  }  }
256    
 GL_TLE::GL_TLE(){  
 }  
   
257  // ****************************************************  // ****************************************************
258    
259  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 518  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
518    //    //
519    //  printf("1myquery is %s \n",myquery.str().c_str());    //  printf("1myquery is %s \n",myquery.str().c_str());
520    //    //
521      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
522      this->GetGLTABLES()->AddQ();
523    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
524    //    //
525    // retrieve this ID_TRASH    // retrieve this ID_TRASH
526    //    //
527    myquery.str("");    myquery.str("");
528    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";  
529      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
530      this->GetGLTABLES()->AddQ();
531    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
532    //    //
533    UInt_t idtrash = 0;    UInt_t idtrash = 0;
# Line 454  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 549  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
549    //    //
550    //  printf("2myquery is %s \n",myquery.str().c_str());    //  printf("2myquery is %s \n",myquery.str().c_str());
551    //    //
552      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
553      this->GetGLTABLES()->AddQ();
554    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
555    //    //
556    Row = pResult->Next();          Row = pResult->Next();      
# Line 469  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 566  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
566    //    //
567    //  printf("3myquery is %s \n",myquery.str().c_str());    //  printf("3myquery is %s \n",myquery.str().c_str());
568    //    //
569      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
570      this->GetGLTABLES()->AddQ();
571    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
572    //    //
573    Row = pResult->Next();          Row = pResult->Next();      
# Line 485  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 584  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
584    //    //
585    //  printf("4myquery is %s \n",myquery.str().c_str());    //  printf("4myquery is %s \n",myquery.str().c_str());
586    //    //
587      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
588      this->GetGLTABLES()->AddQ();
589    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
590    //    //
591    myquery.str("");    myquery.str("");
# Line 494  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 595  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
595    //    //
596    //  printf("4myquery is %s \n",myquery.str().c_str());    //  printf("4myquery is %s \n",myquery.str().c_str());
597    //    //
598      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
599      this->GetGLTABLES()->AddQ();
600    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
601    //    //
602    myquery.str("");    myquery.str("");
# Line 504  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 607  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
607    //  printf("4myquery is %s \n",myquery.str().c_str());    //  printf("4myquery is %s \n",myquery.str().c_str());
608    //    //
609    //    //
610      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
611      this->GetGLTABLES()->AddQ();
612    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
613    //    //
614    myquery.str("");    myquery.str("");
# Line 514  Int_t GL_RUN::DeleteRun(TSQLServer *dbc, Line 619  Int_t GL_RUN::DeleteRun(TSQLServer *dbc,
619    //  printf("5myquery is %s \n",myquery.str().c_str());    //  printf("5myquery is %s \n",myquery.str().c_str());
620    //    //
621    //    //
622      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
623      this->GetGLTABLES()->AddQ();
624    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
625    //    //
626    return 0;    return 0;
# Line 545  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 652  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
652      //      //
653      printf(" qui? myquery is %s \n",myquery.str().c_str());      printf(" qui? myquery is %s \n",myquery.str().c_str());
654      //      //
655        if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
656        this->GetGLTABLES()->AddQ();
657      pResult = dbc->Query(myquery.str().c_str());      pResult = dbc->Query(myquery.str().c_str());
658      //      //
659      Row = pResult->Next();            Row = pResult->Next();      
# Line 628  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 737  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
737    //    //
738    //  printf("5myquery is %s \n",myquery.str().c_str());    //  printf("5myquery is %s \n",myquery.str().c_str());
739    //    //
740      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
741      this->GetGLTABLES()->AddQ();
742    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
743    //    //
744    //    //
# Line 635  Int_t GL_RUN::RestoreRun(TSQLServer *dbc Line 746  Int_t GL_RUN::RestoreRun(TSQLServer *dbc
746    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=";
747    myquery << IDRUN << ";";    myquery << IDRUN << ";";
748    //    //
749      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
750      this->GetGLTABLES()->AddQ();
751    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
752    //    //
753    return 0;    return 0;
# Line 717  Int_t GL_RUN::Fill_GL_RUN(TSQLServer *db Line 830  Int_t GL_RUN::Fill_GL_RUN(TSQLServer *db
830    //    //
831    //  printf("myquery is %s \n",myquery.str().c_str());    //  printf("myquery is %s \n",myquery.str().c_str());
832    //    //
833      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
834      this->GetGLTABLES()->AddQ();
835    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
836    //    //
837    return 0;    return 0;
# Line 793  Int_t GL_RUN::Fill_GL_RUN_FRAGMENTS(TSQL Line 908  Int_t GL_RUN::Fill_GL_RUN_FRAGMENTS(TSQL
908    //    //
909    // printf("myquery is %s \n",myquery.str().c_str());    // printf("myquery is %s \n",myquery.str().c_str());
910    //    //
911      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
912      this->GetGLTABLES()->AddQ();
913    dbc->Query(myquery.str().c_str());    dbc->Query(myquery.str().c_str());
914    //    //
915    return 0;    return 0;
# Line 850  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T Line 967  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T
967    myquery << " from GL_RUN where ID=" << run << ";";    myquery << " from GL_RUN where ID=" << run << ";";
968    //    //
969    //  printf(" myquery is %s \n",myquery.str().c_str());    //  printf(" myquery is %s \n",myquery.str().c_str());
970      if ( !this->IsConnected(dbc) ) return -57;  
971      this->AddQ();
972    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
973    //    //
974    //  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 980  Int_t GL_RUN::Query_GL_RUN(UInt_t run, T
980        if( Row == NULL ) break;        if( Row == NULL ) break;
981  //        Set_GL_RUN(Row);  //        Set_GL_RUN(Row);
982        for( t = 0; t < pResult->GetFieldCount(); t++){        for( t = 0; t < pResult->GetFieldCount(); t++){
983                    if (t== 0) ID                = (UInt_t)atoll(Row->GetField(t));          if (t== 0) ID                = (UInt_t)atoll(Row->GetField(t));
984                    if (t== 1) ID_RUN_FRAG       = (UInt_t)atoll(Row->GetField(t));          if (t== 1) ID_RUN_FRAG       = (UInt_t)atoll(Row->GetField(t));
985                    if (t== 2) ID_ROOT_L0        = (UInt_t)atoll(Row->GetField(t));          if (t== 2) ID_ROOT_L0        = (UInt_t)atoll(Row->GetField(t));
986                    if (t== 3) ID_ROOT_L2        = (UInt_t)atoll(Row->GetField(t));          if (t== 3) ID_ROOT_L2        = (UInt_t)atoll(Row->GetField(t));
987                    if (t== 4) RUNHEADER_TIME    = (UInt_t)atoll(Row->GetField(t));          if (t== 4) RUNHEADER_TIME    = (UInt_t)atoll(Row->GetField(t));
988                    if (t== 5) RUNTRAILER_TIME   = (UInt_t)atoll(Row->GetField(t));          if (t== 5) RUNTRAILER_TIME   = (UInt_t)atoll(Row->GetField(t));
989                    if (t== 6) RUNHEADER_OBT     = (UInt_t)atoll(Row->GetField(t));          if (t== 6) RUNHEADER_OBT     = (UInt_t)atoll(Row->GetField(t));
990                    if (t== 7) RUNTRAILER_OBT    = (UInt_t)atoll(Row->GetField(t));          if (t== 7) RUNTRAILER_OBT    = (UInt_t)atoll(Row->GetField(t));
991                    if (t== 8) RUNHEADER_PKT     = (UInt_t)atoll(Row->GetField(t));          if (t== 8) RUNHEADER_PKT     = (UInt_t)atoll(Row->GetField(t));
992                    if (t== 9) RUNTRAILER_PKT    = (UInt_t)atoll(Row->GetField(t));          if (t== 9) RUNTRAILER_PKT    = (UInt_t)atoll(Row->GetField(t));
993                    if (t==10) EV_FROM           = (UInt_t)atoll(Row->GetField(t));          if (t==10) EV_FROM           = (UInt_t)atoll(Row->GetField(t));
994                    if (t==11) EV_TO             = (UInt_t)atoll(Row->GetField(t));          if (t==11) EV_TO             = (UInt_t)atoll(Row->GetField(t));
995                    if (t==12) NEVENTS           = (UInt_t)atoll(Row->GetField(t));          if (t==12) NEVENTS           = (UInt_t)atoll(Row->GetField(t));
996                    if (t==13) LAST_TIMESYNC     = (UInt_t)atoll(Row->GetField(t));          if (t==13) LAST_TIMESYNC     = (UInt_t)atoll(Row->GetField(t));
997                    if (t==14) OBT_TIMESYNC      = (UInt_t)atoll(Row->GetField(t));          if (t==14) OBT_TIMESYNC      = (UInt_t)atoll(Row->GetField(t));
998                    if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));          if (t==15) COMPILATIONTIMESTAMP = (UInt_t)atoll(Row->GetField(t));
999                    if (t==16) FAV_WRK_SCHEDULE  = (UInt_t)atoll(Row->GetField(t));          if (t==16) FAV_WRK_SCHEDULE  = (UInt_t)atoll(Row->GetField(t));
1000                    if (t==17) EFF_WRK_SCHEDULE  = (UInt_t)atoll(Row->GetField(t));          if (t==17) EFF_WRK_SCHEDULE  = (UInt_t)atoll(Row->GetField(t));
1001                    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));
1002                    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));
1003                    if (t==20) ACQ_BUILD_INFO    = (UInt_t)atoll(Row->GetField(t));          if (t==20) ACQ_BUILD_INFO    = (UInt_t)atoll(Row->GetField(t));
1004                    if (t==21) ACQ_VAR_INFO      = (UInt_t)atoll(Row->GetField(t));          if (t==21) ACQ_VAR_INFO      = (UInt_t)atoll(Row->GetField(t));
1005                    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));
1006                    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));
1007                    if (t==24) PKT_COUNTER       = (UInt_t)atoll(Row->GetField(t));          if (t==24) PKT_COUNTER       = (UInt_t)atoll(Row->GetField(t));
1008                    if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));          if (t==25) PKT_READY_COUNTER = (UInt_t)atoll(Row->GetField(t));
1009                    if (t==26) TRK_CALIB_USED    = (UInt_t)atoll(Row->GetField(t));          if (t==26) TRK_CALIB_USED    = (UInt_t)atoll(Row->GetField(t));
1010                    if (t==27) CAL_DSP_MASK      = (UInt_t)atoll(Row->GetField(t));          if (t==27) CAL_DSP_MASK      = (UInt_t)atoll(Row->GetField(t));
1011                    if (t==28) BOOT_NUMBER       = (UInt_t)atoll(Row->GetField(t));          if (t==28) BOOT_NUMBER       = (UInt_t)atoll(Row->GetField(t));
1012                    if (t==29) VALIDATION        = (UInt_t)atoll(Row->GetField(t));          if (t==29) VALIDATION        = (UInt_t)atoll(Row->GetField(t));
1013        };        };
1014    };    };
1015    //  delete pResult;    //  delete pResult;
# Line 948  Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TSt Line 1067  Int_t GL_RUN::Query_GL_RUN_FRAGMENTS(TSt
1067    //    //
1068    //  printf(" query is %s \n",myquery.str().c_str());    //  printf(" query is %s \n",myquery.str().c_str());
1069    //    //
1070      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1071      this->GetGLTABLES()->AddQ();
1072    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1073    if(!pResult->GetRowCount())return(-50);    if(!pResult->GetRowCount())return(-50);
1074    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 1134  Int_t GL_ROOT::Query_GL_ROOT(UInt_t id,
1134    myquery << ",NAME";    myquery << ",NAME";
1135    myquery << " from GL_ROOT where ID=" << id << ";";        myquery << " from GL_ROOT where ID=" << id << ";";    
1136    //    //
1137      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1138      this->GetGLTABLES()->AddQ();
1139    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1140    if(!pResult->GetRowCount())return (-51);    if(!pResult->GetRowCount())return (-51);
1141    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1049  Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(U Line 1172  Int_t GL_TRK_CALIB::Query_GL_TRK_CALIB(U
1172    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;
1173    myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";          myquery << " AND VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";      
1174    //  myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";          //  myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";      
1175      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1176      this->GetGLTABLES()->AddQ();
1177    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1178    if(!pResult->GetRowCount())return (-53);    if(!pResult->GetRowCount())return (-53);
1179    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1106  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1231  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1231    myquery << " ;";    myquery << " ;";
1232    //myquery << " and VALIDATION=1;";    //myquery << " and VALIDATION=1;";
1233    //    //
1234      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1235      this->GetGLTABLES()->AddQ();
1236    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1237    //  printf(" mysquery is %s\n",myquery.str().c_str());    //  printf(" mysquery is %s\n",myquery.str().c_str());
1238    //    //
# Line 1125  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB Line 1252  Int_t GL_CALO_CALIB::Query_GL_CALO_CALIB
1252      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;
1253      myquery << " and FROM_TIME <= " << time;      myquery << " and FROM_TIME <= " << time;
1254      myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";      myquery << " and VALIDATION=1 ORDER BY FROM_TIME DESC LIMIT 1;";
1255        if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1256        this->GetGLTABLES()->AddQ();
1257      pResult = dbc->Query(myquery.str().c_str());      pResult = dbc->Query(myquery.str().c_str());
1258      //    printf(" mysquery is %s\n",myquery.str().c_str());      //    printf(" mysquery is %s\n",myquery.str().c_str());
1259      //      //
# Line 1167  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn Line 1296  Int_t GL_S4_CALIB::Query_GL_S4_CALIB(UIn
1296    myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time;    myquery << "select * from GL_S4_CALIB where FROM_TIME <= "<< time;
1297    myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";          myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";      
1298    //  myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";          //  myquery << " ORDER BY FROM_TIME DESC LIMIT 1;";      
1299      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1300      this->GetGLTABLES()->AddQ();
1301    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1302    if(!pResult->GetRowCount())return (-55);//throw -55;    if(!pResult->GetRowCount())return (-55);//throw -55;
1303    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1213  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti Line 1344  Int_t GL_PARAM::Query_GL_PARAM(UInt_t ti
1344    myquery << " and FROM_TIME <= " << time;    myquery << " and FROM_TIME <= " << time;
1345    myquery << " ORDER BY TO_TIME DESC LIMIT 1;";      myquery << " ORDER BY TO_TIME DESC LIMIT 1;";  
1346    //    //
1347      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1348      this->GetGLTABLES()->AddQ();
1349    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1350    if(!pResult->GetRowCount())return (-52);    if(!pResult->GetRowCount())return (-52);
1351    for( r=0; r < 1000; r++){    for( r=0; r < 1000; r++){
# Line 1266  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1399  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1399    TSQLRow *Row;    TSQLRow *Row;
1400    stringstream myquery;    stringstream myquery;
1401    stringstream rname;    stringstream rname;
1402      //  pcksList packetsNames;
1403      //  pcksList::iterator Iter;
1404      //  getPacketsNames(packetsNames);
1405    rname.str("");    rname.str("");
1406    // ----------------    // ----------------
1407    myquery.str("");    myquery.str("");
# Line 1276  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1412  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1412    myquery << type.Data();    myquery << type.Data();
1413    myquery << "=" << id << ";";        myquery << "=" << id << ";";    
1414    //    //
1415      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1416      this->GetGLTABLES()->AddQ();
1417    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1418    if( pResult->GetRowCount() ){    if( pResult->GetRowCount() ){
1419      Row = pResult->Next();            Row = pResult->Next();      
# Line 1299  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1437  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1437      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
1438      pktfirst = ph->GetCounter();      pktfirst = ph->GetCounter();
1439      obtfirst = ph->GetOrbitalTime();        obtfirst = ph->GetOrbitalTime();  
1440        //
1441    //     code = eh->GetCounter();
1442    //     UInt_t en = 0;
1443    //     for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
1444    //       en = code->Get(GetPacketType(*Iter));
1445    //       if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
1446    //      //
1447    //      TTree *TC = 0;
1448    //      TC = (TTree*)file->Get("CalibCalPed");
1449    //      if ( !TC || TC->IsZombie() ) return;
1450    //      EventHeader *ehc = 0;
1451    //      PscuHeader *phc = 0;
1452    //      TC->SetBranchAddress("Header", &ehc);
1453    //      TC->GetEntry(0);
1454    //      phc = ehc->GetPscuHeader();
1455    //      pktfirst = phc->GetCounter();
1456    //      obtfirst = phc->GetOrbitalTime();  
1457    //       //
1458    //       };
1459    //     };  
1460        //
1461    };    };
1462    //    //
1463    // look for Resurs offset    // look for Resurs offset
1464    //    //
1465    UInt_t t0 = 0;    T0 = 0;
1466    //    //
1467    //    //
1468    stringstream oss;    stringstream oss;
1469    oss.str("");    oss.str("");
1470    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='"
1471        << rname.str().c_str() << "';";        << rname.str().c_str() << "';";
1472    //  if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1473      this->GetGLTABLES()->AddQ();
1474    pResult = dbc->Query(oss.str().c_str());    pResult = dbc->Query(oss.str().c_str());
1475    Row = pResult->Next();    Row = pResult->Next();
1476    //    //
# Line 1329  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1489  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1489      for (Int_t i = 8; i<13; i++){      for (Int_t i = 8; i<13; i++){
1490        dwo.Append(name[i],1);        dwo.Append(name[i],1);
1491      };          };    
1492      if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();      if ( dwo.IsDigit() ){
1493          dworbit = (UInt_t)dwo.Atoi();
1494        } else {
1495          dworbit = 1;
1496        };
1497    };    };
1498    // if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());      //
1499    //    //
1500    if ( !Row ){    if ( !Row ){
1501      oss.str("");      oss.str("");
1502      oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "      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< "
1503          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
1504      //    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1505        this->GetGLTABLES()->AddQ();
1506      pResult = dbc->Query(oss.str().c_str());      pResult = dbc->Query(oss.str().c_str());
1507      Row = pResult->Next();      Row = pResult->Next();
1508      if ( !Row ){      if ( !Row ){
# Line 1346  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr Line 1511  GL_TIMESYNC::GL_TIMESYNC(UInt_t id, TStr
1511      };      };
1512    };    };
1513    //    //
1514    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);
1515    //    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();  
   //       //  
   //     };  
   //   };  
   //  
1516    //    //
1517    // look for the correct timesync entry    // look for the correct timesync entry
1518    //    //
1519    myquery.str("");    myquery.str("");
1520    myquery << " SELECT OBT0,TIMESYNC FROM GL_TIMESYNC "    myquery << " SELECT OBT0,TIMESYNC,TYPE FROM GL_TIMESYNC "
1521        << " WHERE ID_RAW = " << idraw        << " WHERE ID_RAW = " << idraw
1522        << ";";        << ";";
1523      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return;  
1524      this->GetGLTABLES()->AddQ();
1525    pResult = dbc->Query(myquery.str().c_str());    pResult = dbc->Query(myquery.str().c_str());
1526    if ( pResult ){    if ( pResult ){
1527      Row = pResult->Next();      Row = pResult->Next();
1528      if ( (Row != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){      if ( (Row != NULL) && ((UInt_t)atoll(Row->GetField(0)) > 0 ) ){
1529        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));
1530          TIMESYNC = (UInt_t)atoll(Row->GetField(1));
1531          TYPE = (UInt_t)atoll(Row->GetField(2));
1532          toffset = (UInt_t)atoll(Row->GetField(1)) - (UInt_t)(this->DBobt((UInt_t)atoll(Row->GetField(0)))/1000) + T0;
1533      };      };
1534    };    };
1535    //    //
# Line 1392  UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT Line 1548  UInt_t GL_TIMESYNC::DBabsTime(UInt_t OBT
1548    //    //
1549  };  };
1550    
1551    /**
1552     *
1553     * Returns the Resurs time given the OBT needed to process inclination and orbital infos
1554     *
1555     */
1556    UInt_t GL_TIMESYNC::ResursTime(UInt_t OBT){  
1557      //
1558      return(((UInt_t)((Int_t)(this->DBobt(OBT)-this->DBobt(OBT0))/1000)+TIMESYNC));
1559      //
1560    };
1561    
1562  ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){    
1563    /**
1564     * Return the correct packet number if we went back to zero
1565     */
1566    Long64_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){  
1567    //    //
1568    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)  ){
1569        return((Long64_t)pkt_num+16777215LL);
1570      };
1571    //    //
1572    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
1573      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()));  
     };  
1574    };    };
1575    //    //
1576    return((ULong64_t)obt);    return((Long64_t)pkt_num);
1577    //    //
1578  };  };
1579    
1580  UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){    /**
1581     * Return the correct On Board Time if we went back to zero
1582     */
1583    Long64_t GL_TIMESYNC::DBobt(UInt_t obt){  
1584    //    //
1585    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()));
1586    //    //
1587    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1588      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));  
     };  
1589    };    };
1590    //    //
1591    return(pkt_num);    return((Long64_t)obt);
   //  
1592  };  };
1593    
1594    
1595    
1596    // ULong64_t GL_TIMESYNC::DBobt(UInt_t obt){  
1597    //   //
1598    //   if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));
1599    //   //
1600    //   if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
1601    //     if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){
1602    //       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));
1603    //     } else {
1604    //       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));
1605    //     };
1606    //   };
1607    //   //
1608    //   return((ULong64_t)obt);
1609    //   //
1610    // };
1611    
1612    // UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num){  
1613    //   //
1614    //   if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));
1615    //   //
1616    //   if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
1617    //     if ( (pkt_num-16777215) < 0 ){
1618    //       return((16777215-pkt_num));
1619    //     } else {
1620    //       return((pkt_num-16777215));
1621    //     };
1622    //   };
1623    //   //
1624    //   return(pkt_num);
1625    //   //
1626    // };
1627    
1628  /*  /*
1629   *   *
1630   * 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 1635  UInt_t GL_TIMESYNC::DBpkt(UInt_t pkt_num
1635   */   */
1636  TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){  TString GL_TIMESYNC::ConvertTime(TString &tzone, UInt_t dbt){
1637    //    //
1638    TDatime *time = new TDatime();    Int_t offset = 0;
1639    TString rtime;    TString rtime;
1640    Bool_t found = false;    Bool_t found = false;
1641    //    //
   time->Set(dbt,false); // UTC (Coordinated Universal Time)    
   //  
1642    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
1643      //      //
1644      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);  
1645      found = true;      found = true;
1646      //      //
1647    };    };
1648    //    //
1649    if ( !strcmp(tzone.Data(),"CET") ){    if ( !strcmp(tzone.Data(),"CET") ){
1650      //      //
1651      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);  
1652      found = true;      found = true;
1653      //      //
1654    };    };
1655    //    //
1656    if ( !strcmp(tzone.Data(),"CEST") ){    if ( !strcmp(tzone.Data(),"CEST") ){
1657      //      //
1658      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);  
1659      found = true;      found = true;
1660      //      //
1661    };    };
1662    //    //
1663    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST")){
1664      //      //
1665      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);  
1666      found = true;      found = true;
1667      //      //
1668    };    };
# Line 1478  TString GL_TIMESYNC::ConvertTime(TString Line 1672  TString GL_TIMESYNC::ConvertTime(TString
1672      tzone = "UTC";      tzone = "UTC";
1673    };    };
1674    //    //
1675    rtime = time->AsSQLString();    dbt += offset;
1676      //
1677      TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
1678      //
1679      rtime = time->AsString("s");
1680      //
1681      delete time;
1682    //    //
1683    return(rtime);    return(rtime);
1684  }  }
1685    
1686  /*  /*
1687   *   *
1688   * Convert the time in the DB from UInt_t to a string   * Convert the time from TZONE to UTC
1689   *   *
1690   * @param dbt time in the DB   * @param dbt time in the DB
1691   * @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 1693  TString GL_TIMESYNC::ConvertTime(TString
1693   */   */
1694  TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){  TString GL_TIMESYNC::UnConvertTime(TString &tzone, UInt_t dbt){
1695    //    //
1696    TDatime *time = new TDatime();    Int_t offset = 0;
1697    TString rtime;    TString rtime;
1698    //    //
1699    Bool_t found = false;    Bool_t found = false;
1700    //    //
   time->Set(dbt,false); // tzone  
   //  
1701    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){    if ( !strcmp(tzone.Data(),"MSK") || !strcmp(tzone.Data(),"MWT") ){
1702      //      //
1703      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);  
1704      found = true;      found = true;
1705      //      //
1706    };    };
1707    //    //
1708    if ( !strcmp(tzone.Data(),"CET") ){    if ( !strcmp(tzone.Data(),"CET") ){
1709      //      //
1710      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);  
1711      found = true;      found = true;
1712      //      //
1713    };    };
1714    //    //
1715    if ( !strcmp(tzone.Data(),"CEST") ){    if ( !strcmp(tzone.Data(),"CEST") ){
1716      //      //
1717      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);  
1718      found = true;      found = true;
1719      //      //
1720    };    };
1721    //    //
1722    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){    if ( !strcmp(tzone.Data(),"MSD") || !strcmp(tzone.Data(),"MST") ){
1723      //      //
1724      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);  
1725      found = true;      found = true;
1726      //      //
1727    };    };
1728    //    //
1729    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(),"") ){
1730      //      //
1731        offset = 0;
1732      printf("\n Unknown time zone %s using UTC \n",tzone.Data());      printf("\n Unknown time zone %s using UTC \n",tzone.Data());
1733      tzone = "UTC";      tzone = "UTC";
1734    };    };
1735    //    //
1736    rtime = time->AsSQLString();    dbt += offset;
1737      TTimeStamp *time = new TTimeStamp((time_t)dbt,0);
1738      //
1739      rtime = time->AsString("s");
1740    //    //
1741    return(rtime);    return(rtime);
1742  }  }
1743    
1744    
1745  // ****************************************************  //
1746  /**  // Build a query and call DoQuery.
1747   * Function to query the GL_TLE table of the DB.  //
1748   *  // date it's an SQL datetime date and dbc is the connection to be
1749   * 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
1750   *  // and the immediatly next one.
1751   * Return the pointer to a cTle object that has the closest and  //
1752   * 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){  
1753    stringstream myquery;    stringstream myquery;
1754    myquery.str("");    myquery.str("");
1755    
1756    myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE "    myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1757            << "WHERE FROM_TIME < FROM_UNIXTIME(" << time << ") ORDER BY FROM_TIME DESC LIMIT 1;";              << "WHERE FROM_TIME <= '" << date.Data()
1758              << "' ORDER BY FROM_TIME DESC LIMIT 1) "
1759              << "UNION "
1760              << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1761              << "WHERE FROM_TIME > '" << date.Data()
1762              << "' ORDER BY FROM_TIME ASC LIMIT 1)";
1763    
1764    return Query_GL_TLE_go(myquery.str(), dbc);    return DoQuery(myquery.str().c_str(), dbc);
1765  }  }
1766    
1767    
1768  // ****************************************************  //
1769  /**  // Build a query and call DoQuery.
1770   * Function to query the GL_TLE table of the DB.  //
1771   *  // time is the UTC date in unix time (UTC) and dbc is the connection
1772   * 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
1773   * is requested.  // previous date and the immediatly next one.
1774   *  //
1775   * Return the pointer to a cTle object that has the closest and  // Returns the value returned by DoQuery().
1776   * previous date compared with time.  //
1777   *  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){  
1778    stringstream myquery;    stringstream myquery;
1779    myquery.str("");    myquery.str("");
1780    
1781    myquery << "SELECT TLE1, TLE2, TLE3 FROM GL_TLE "    myquery << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1782            << "WHERE FROM_TIME < '" << date.Data() << "' ORDER BY FROM_TIME DESC LIMIT 1;";              << "WHERE FROM_TIME <= FROM_UNIXTIME('" << time
1783              << "') ORDER BY FROM_TIME DESC LIMIT 1) "
1784              << "UNION "
1785              << "(SELECT ID, TLE1, TLE2, TLE3, UNIX_TIMESTAMP(FROM_TIME) FROM GL_TLE "
1786              << "WHERE FROM_TIME > FROM_UNIXTIME('" << time
1787              << "') ORDER BY FROM_TIME ASC LIMIT 1)";
1788    
1789    return Query_GL_TLE_go(myquery.str(), dbc);    return DoQuery(myquery.str().c_str(), dbc);
1790  }  }
1791    
1792    
1793  // ****************************************************  //
1794  /**  // Do the query myquery on the connectio dbc.  Initialize tle,
1795   * Private function used by Query_GL_TLE methods.  // tleFromTime and tleToTime.
1796   *  //
1797   * myquery is the query string.  // We should have two rows (unless the table is old).  From the last
1798   *  // one we only take tleToTime.
1799   * Return the pointer to a cTle object that has the closest and  //
1800   * previous date compared with time.  // Returns 0 for success, 1 for failure.
1801   *  //
1802   * 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  
1803    TSQLResult *result;    TSQLResult *result;
1804    TSQLRow *row;    TSQLRow *row;
1805    
1806      // Set the right time_zone (otherwise horrible things will occur! :)
1807      dbc->Query("SET time_zone = '+0:00'");
1808    
1809      // Do the query
1810      if ( !this->GetGLTABLES()->IsConnected(dbc) ) return -57;  
1811      this->GetGLTABLES()->AddQ();
1812    result = dbc->Query(myquery.Data());    result = dbc->Query(myquery.Data());
1813    if(! result->GetRowCount() ) return NULL;    if(! result->GetRowCount() ) {
1814        cerr << "GL_TLE: query failed: " << myquery.Data() << endl;
1815        return 1;
1816      }
1817    
1818      // Get results
1819      row = result->Next(); // first tle row
1820      tle = GiveTle(row);
1821    
1822      tleFromTime = strtol(row->GetField(4), NULL, 10);
1823    
1824      row = result->Next(); // second tle row
1825      if(row)
1826        tleToTime = strtol(row->GetField(4), NULL, 10);
1827      else {
1828        cerr << "GL_TLE: Warning: using last avaible TLE. Please update GL_TLE table!\n";
1829        tleToTime = UINT_MAX;
1830      }
1831    
1832    row = result->Next();    delete row;
1833    tle1 = row->GetField(0);    delete result;
   tle2 = row->GetField(1);  
   tle3 = row->GetField(2);  
1834    
1835    tle = new cTle(tle1, tle2, tle3);    return 0;
1836    }
1837    
   delete result;  
   delete row;  
1838    
1839    return tle;  //
1840    // Build a cTle object from the GL_TLE row.
1841    //
1842    cTle* GL_TLE::GiveTle(TSQLRow *row) {
1843      cTle *thistle = NULL;
1844      string tle1, tle2, tle3;
1845    
1846      // Build cTle object
1847      tle1 = row->GetField(1);
1848      tle2 = row->GetField(2);
1849      tle3 = row->GetField(3);
1850    
1851      thistle = new cTle(tle1, tle2, tle3);
1852    
1853      return thistle;
1854  }  }

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

  ViewVC Help
Powered by ViewVC 1.1.23