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

Diff of /YodaProfiler/src/PamelaDBOperations.cpp

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

revision 1.9 by mocchiut, Tue Sep 12 07:28:46 2006 UTC revision 1.12 by mocchiut, Tue Oct 17 15:20:04 2006 UTC
# Line 31  Line 31 
31  #include <varDump/VarDumpRecord.h>  #include <varDump/VarDumpRecord.h>
32  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
33  //  //
34    #include <cTle.h>
35    #include <cEci.h>
36    #include <cJulian.h>
37    
38  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
39  //  //
40  using namespace std;  using namespace std;
41  using namespace pamela;  using namespace pamela;
42    
43    // Some function to work with cTle stuff.
44    bool compTLE(cTle* tle1, cTle *tle2);
45    float getTleJulian(cTle *);
46    string getTleDatetime(cTle*);
47    
48  /**  /**
49   * Constructor.   * Constructor.
50   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 47  using namespace pamela; Line 56  using namespace pamela;
56   * @param obt0         file obt0.   * @param obt0         file obt0.
57   * @param tsync        file timesync.   * @param tsync        file timesync.
58   * @param debug        debug flag.   * @param debug        debug flag.
59     * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){
62    //    //
63    //    //
64    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 62  PamelaDBOperations::PamelaDBOperations(T Line 72  PamelaDBOperations::PamelaDBOperations(T
72    SetTsync(tsync);    SetTsync(tsync);
73    SetObt0(obt0);    SetObt0(obt0);
74    //    //
75      SetTLEPath(tlefilename);
76      //
77    //    //
78    INSERT_RAW =!filerawname.IsNull();    INSERT_RAW =!filerawname.IsNull();
79    if(INSERT_RAW)SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
80    //    //
81    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
82    if( INSERT_ROOT ){    if( INSERT_ROOT ){
83        this->SetRootName(filerootname);
84      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
     //    if ( !file ) INSERT_ROOT = false;  
85    };    };
   //  this->OpenFile();  
86    //    //
87    this->SetID_RAW(0);    this->SetID_RAW(0);
88    this->SetID_ROOT(0);    this->SetID_ROOT(0);
# Line 191  void PamelaDBOperations::SetNOBOOT(Bool_ Line 202  void PamelaDBOperations::SetNOBOOT(Bool_
202  };  };
203    
204  /**  /**
205     * Store path to the TLE file.
206     */
207    void PamelaDBOperations::SetTLEPath(TString str){
208      tlefilename = str;
209    };
210    
211    /**
212   * Store the olderthan variable   * Store the olderthan variable
213   * @param olderthan   * @param olderthan
214   */   */
# Line 932  Int_t PamelaDBOperations::insertPamelaGL Line 950  Int_t PamelaDBOperations::insertPamelaGL
950          //          //
951          TYPE = 55;//224;          TYPE = 55;//224;
952          //          //
953            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
954            //
955          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
956            existsts = true;            existsts = true;
957            goto eout;            goto eout;
# Line 954  Int_t PamelaDBOperations::insertPamelaGL Line 974  Int_t PamelaDBOperations::insertPamelaGL
974      //      //
975      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
976      //      //
977      if ( rhev > 0 ){      Int_t nrhev = rh->GetEntries();
978        rh->GetEntry(0);      Int_t nrtev = rt->GetEntries();
979        //      if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
980        TSYNC = runh->LAST_TIME_SYNC_INFO;      //
981        OBT = runh->OBT_TIME_SYNC * 1000;      if ( nrhev > 0 ){
982        //        for (Int_t i=0; i<nrhev; i++){
983        TYPE = 20;          //
984        //          rh->GetEntry(i);
985        if ( TSYNC && OBT ){          //
986          existsts = true;          TSYNC = runh->LAST_TIME_SYNC_INFO;
987          goto eout;          OBT = runh->OBT_TIME_SYNC * 1000;
988            //
989            TYPE = 20;
990            //
991            if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
992            //
993            if ( TSYNC && OBT ){
994              existsts = true;
995              goto eout;
996            };
997        };        };
998        //        //
999      };      };
1000      if ( rtev > 0 ){      if ( nrtev > 0 ){
1001        //        //
1002        if ( IsDebug() ) printf(" No runheader \n");        if ( IsDebug() ) printf(" No runheader \n");
1003        signal = 6;        signal = 6;
1004        //        //
1005        rt->GetEntry(0);        for (Int_t i=0; i<nrtev; i++){
1006        //          //
1007        TSYNC = runt->LAST_TYME_SYNC_INFO;          rt->GetEntry(i);
1008        OBT = runt->OBT_TYME_SYNC * 1000;          //
1009        //          TSYNC = runt->LAST_TYME_SYNC_INFO;
1010        TYPE = 21;          OBT = runt->OBT_TYME_SYNC * 1000;
1011        //          //
1012        if ( TSYNC && OBT ){          TYPE = 21;
1013          existsts = true;          //
1014          goto eout;          if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1015            //
1016            if ( TSYNC && OBT ){
1017              existsts = true;
1018              goto eout;
1019            };
1020        };        };
1021        //        //
1022      } else {      } else {
# Line 3212  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3246  Int_t PamelaDBOperations::CleanGL_RUN_FR
3246      //      //
3247      row = result->Next();      row = result->Next();
3248      //      //
3249      if ( row ){      while ( row ){
3250        //        //
3251        oss.str("");        oss.str("");
3252        oss << " ID= "<< row->GetField(0);        oss << " ID= "<< row->GetField(0);
# Line 3289  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3323  Int_t PamelaDBOperations::CleanGL_RUN_FR
3323        //        //
3324        if ( !result2 ) throw -4;        if ( !result2 ) throw -4;
3325        //        //
3326          row = result->Next();
3327      };      };
3328    };    };
3329    if ( IsDebug() ) printf(" Moved %u runs\n",moved);    if ( IsDebug() ) printf(" Moved %u runs\n",moved);
# Line 3523  Int_t PamelaDBOperations::assignVALIDATI Line 3558  Int_t PamelaDBOperations::assignVALIDATI
3558    
3559    
3560    
3561    // Insert TLEs from file tlefilename in the table GL_TLE in the db
3562    // opened by conn, sorting them by date from older to newer, if each
3563    // TLE has not been alread inserted.
3564    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3565    {
3566      fstream tlefile(tlefilename, ios::in);
3567    
3568      if ( !tlefile ) throw -7;
3569    
3570      vector<cTle*> ctles;
3571      vector<cTle*>::iterator iter;
3572      int present = 0;
3573    
3574      // Get three lines from tlefile, create a cTle object and put it
3575      // into ctles
3576      while(1) {
3577        cTle *tlef;
3578        string str1, str2, str3;
3579    
3580        getline(tlefile, str1);
3581        if(tlefile.eof()) break;
3582    
3583        getline(tlefile, str2);
3584        if(tlefile.eof()) break;
3585    
3586        getline(tlefile, str3);
3587        if(tlefile.eof()) break;
3588    
3589        // We now have three good lines for a cTle.
3590        tlef = new cTle(str1, str2, str3);
3591        ctles.push_back(tlef);
3592      }
3593    
3594      tlefile.close();
3595    
3596      // Sort by date
3597      sort(ctles.begin(), ctles.end(), compTLE);
3598    
3599      // Now we insert each TLE into the db
3600      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3601        cTle *tle = *iter;
3602    
3603        // Do nothing if it's already present in the db.  Just increase
3604        // the counter present.
3605        if (! isTlePresent(tle))
3606          {
3607            int status = insertTle(tle);
3608    
3609            // Insert query failed.  Return 1.
3610            if(status == EXIT_FAILURE) {
3611              
3612              if( IsDebug() ) {
3613                cerr << "Error: inserting TLE:" << endl
3614                     << tle->getName() << endl
3615                     << tle->getLine1() << endl
3616                     << tle->getLine2() << endl;
3617              }
3618    
3619              throw -4;
3620              return 1;
3621            }
3622    
3623          }
3624        else
3625          present++;
3626    
3627      }
3628    
3629      int inserted = ctles.size() - present;  // Number of inserted TLE.
3630      if ( IsDebug() )
3631        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3632             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3633    
3634      ctles.clear();
3635    
3636    
3637      // Return 2 if no new TLE has been inserted.  0 otherwise.
3638      if(! inserted ) return 2;
3639      return 0;
3640    }
3641    
3642    
3643    // Insert tle in the table GL_TLE using the connection conn.
3644    int PamelaDBOperations::insertTle(cTle *tle)
3645    {
3646      stringstream oss;
3647      TSQLResult *result = 0;
3648    
3649      oss.str("");
3650      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3651          << " VALUES ( '"
3652          << tle->getName() << "', '"
3653          << tle->getLine1() << "', '"
3654          << tle->getLine2() << "', '"
3655          << getTleDatetime(tle) << "')";
3656    
3657      //  cout << oss.str().c_str() << endl;
3658      result = conn->Query(oss.str().c_str());
3659      if (result == NULL)
3660        return EXIT_FAILURE;
3661    
3662      return EXIT_SUCCESS;
3663    }
3664    
3665    
3666    // Return whether tle is already in the db connected by conn.
3667    bool PamelaDBOperations::isTlePresent(cTle *tle)
3668    {
3669      stringstream oss;
3670      TSQLResult *result = 0;
3671    
3672      oss.str("");
3673      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3674          << getTleDatetime(tle) << "'";
3675    
3676      result = conn->Query(oss.str().c_str());
3677      if (result == NULL) throw -4;
3678    
3679      if (result->GetRowCount())
3680        return true;
3681      else
3682        return false;
3683    }
3684    
3685    
3686    // Return whether the first TLE is dated early than the second
3687    bool compTLE (cTle *tle1, cTle *tle2)
3688    {
3689      return getTleJulian(tle1) < getTleJulian(tle2);
3690    }
3691    
3692    
3693    // Return the date of the tle using the format (year-2000)*1e3 +
3694    // julian day.  e.g. 6365 is the 31th Dec 2006.
3695    // It does *not* return a cJulian date.
3696    float getTleJulian(cTle *tle) {
3697      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3698    }
3699    
3700    
3701    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3702    // format.
3703    string getTleDatetime(cTle *tle)
3704    {
3705      int year, mon, day, hh, mm, ss;
3706      double dom; // day of month (is double!)
3707      stringstream date; // date in datetime format
3708    
3709      // create a cJulian from the date in tle
3710      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3711    
3712      // get year, month, day of month
3713      jdate.getComponent(&year, &mon, &dom);
3714    
3715      // build a datetime YYYY-MM-DD hh:mm:ss
3716      date.str("");
3717      day = (int) floor(dom);
3718      hh = (int) floor( (dom - day) * 24);
3719      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3720      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3721      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3722    
3723      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3724    
3725      return date.str();
3726    }

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.12

  ViewVC Help
Powered by ViewVC 1.1.23