/[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.14 by mocchiut, Fri Oct 20 11:39:37 2006 UTC revision 1.27 by mocchiut, Mon Feb 12 13:32:18 2007 UTC
# Line 14  Line 14 
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
17  #include <TDatime.h>  #include <TTimeStamp.h>
18  #include <TF1.h>  #include <TF1.h>
19  //  //
20  #include <EventHeader.h>  #include <EventHeader.h>
# Line 56  string getTleDatetime(cTle*); Line 56  string getTleDatetime(cTle*);
56   * @param debug        debug flag.   * @param debug        debug flag.
57   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
58   */   */
59  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){  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, UInt_t dwinput){
60    //    //
61    //    //
62    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 79  PamelaDBOperations::PamelaDBOperations(T
79    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
80    if( INSERT_ROOT ){    if( INSERT_ROOT ){
81      this->SetRootName(filerootname);      this->SetRootName(filerootname);
82      this->SetOrbitNo();      this->SetOrbitNo(dwinput);
83      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
84      } else {
85        this->SetRootName("");
86    };    };
87    //    //
88    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 110  void PamelaDBOperations::Close(){ Line 112  void PamelaDBOperations::Close(){
112  //  //
113  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114    clean_time = new TDatime();    clean_time = new TDatime();
115      //
116    if(olderthan >= 0){    if(olderthan >= 0){
117      VALIDATE = true;      VALIDATE = true;
118      UInt_t timelim = 0;      UInt_t timelim = 0;
119      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
120      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
121    };    };
122  };  };
# Line 154  void PamelaDBOperations::SetDebugFlag(Bo Line 157  void PamelaDBOperations::SetDebugFlag(Bo
157  };  };
158    
159  /**  /**
160     * Set the autoboot flag
161     *
162     */
163    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
164      AUTOBOOT = dbg;
165    };
166    
167    /**
168     * Set the nofrag flag
169     *
170     */
171    void PamelaDBOperations::SetNoFrag(Bool_t nf){
172      NOFRAG = nf;
173    };
174    
175    /**
176   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
177   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
178   */   */
# Line 196  void PamelaDBOperations::SetRootName(TSt Line 215  void PamelaDBOperations::SetRootName(TSt
215  /**  /**
216   * Store the downlink orbit number from filename.   * Store the downlink orbit number from filename.
217   */   */
218  void PamelaDBOperations::SetOrbitNo(){  void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
219    dworbit = 0;    dworbit = 0;
220      //
221      if ( dwinput ){
222        dworbit = dwinput;
223        if ( IsDebug() ) printf(" Downlink orbit given by hand: %i  \n",dworbit);
224        return;
225      };
226      //
227    TString name = this->GetRootFile();    TString name = this->GetRootFile();
228    Int_t nlength = name.Length();    Int_t nlength = name.Length();
229    if ( nlength < 5 ) return;    if ( nlength < 5 ) return;
# Line 254  Bool_t PamelaDBOperations::SetID_RAW(){ Line 280  Bool_t PamelaDBOperations::SetID_RAW(){
280    oss << "SELECT ID FROM GL_RAW WHERE "    oss << "SELECT ID FROM GL_RAW WHERE "
281        << " PATH = '" << this->GetRawPath().Data() << "' AND "        << " PATH = '" << this->GetRawPath().Data() << "' AND "
282        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
283    
284    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
285    if ( result == NULL ) throw -4;    if ( result == NULL ) throw -4;
286    row = result->Next();    row = result->Next();
# Line 284  Int_t PamelaDBOperations::SetUpperLimits Line 311  Int_t PamelaDBOperations::SetUpperLimits
311    UInt_t nevent = 0;    UInt_t nevent = 0;
312    UInt_t pktlast = 0;    UInt_t pktlast = 0;
313    UInt_t obtlast = 0;    UInt_t obtlast = 0;
314    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
315    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
316    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
317    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
318      Long64_t upperobt2 = 0LL;
319    UInt_t zomp = 0;    UInt_t zomp = 0;
320    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
321    EventCounter *code=0;    EventCounter *code=0;
322    //    //
323    UInt_t deltapkt = 5000;    Long64_t deltapkt = 5000LL;
324    ULong64_t deltaobt = 50000;    Long64_t deltaobt = 50000LL;
325    //    //
326    //   pcksList packetsNames;    //   pcksList packetsNames;
327    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
# Line 331  Int_t PamelaDBOperations::SetUpperLimits Line 359  Int_t PamelaDBOperations::SetUpperLimits
359    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
360    upperentry = nevent-1;    upperentry = nevent-1;
361    //    //
362    if ( IsDebug() ) printf(" First entries are: OBT %llu pkt_num %i \n",obtfirst,pktfirst);    if ( IsDebug() ) printf(" First entries are: OBT %i pkt_num %i \n",obtfirst,pktfirst);
363    //    //
364    if ( IsDebug() ) printf(" Last entries are: OBT %llu pkt_num %i entry %i\n",upperobt,upperpkt,upperentry);    if ( IsDebug() ) printf(" Last entries are: OBT %lld pkt_num %lld entry %i\n",upperobt,upperpkt,upperentry);
365    //    //
366    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) > OBT(obtfirst)) || (PKT(pktlast) > PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) ) return(1);
367    //    //
368    if ( !nevent ) return(2);    if ( !nevent ) return(2);
369    //    //
370    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
371      if ( nevent < jump ) jump = 1;
372      //  if ( nevent < jump ) jump = int(nevent/10);
373      //  if ( !jump ) jump = 1;
374    //    //
375    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
376      //      //
377        if ( IsDebug() ) printf(" starting jump %i \n",jump);
378      // go back      // go back
379      zomp = nevent - 2;      zomp = nevent - 2;
380      //      //
# Line 364  Int_t PamelaDBOperations::SetUpperLimits Line 396  Int_t PamelaDBOperations::SetUpperLimits
396          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
397          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
398          //          //
399          if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ) throw -13;              if ( (t_pktlast < upperpkt && t_obtlast > upperobt) || (t_pktlast < upperpkt2 && t_obtlast > upperobt2) ){
400              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
401              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
402              if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);    
403              throw -13;    
404            };
405          //          //
406          if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){          if ( t_pktlast < upperpkt && t_obtlast < upperobt && t_pktlast < upperpkt2 && t_obtlast < upperobt2 ){
407            zomp = i + jump + 1;            zomp = i + jump + 1;
408            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
409            if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %i pktlast %i upperobt %llu obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);            if ( IsDebug() ) printf(" .-. jump %i zomp %i upperpkt %lld pktlast %i upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
410            break;            break;
411          };          };
412          //          //
# Line 404  Int_t PamelaDBOperations::SetUpperLimits Line 441  Int_t PamelaDBOperations::SetUpperLimits
441    //    //
442    rhev = rh->GetEntries();    rhev = rh->GetEntries();
443    rtev = rt->GetEntries();    rtev = rt->GetEntries();
444    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
445    UInt_t sobth = 0;    Long64_t sobth = 0LL;
446    UInt_t spktt = 0;    Long64_t spktt = 0LL;
447    UInt_t spkth = 0;    Long64_t spkth = 0LL;
448    UInt_t pktt = 0;    Long64_t pktt = 0LL;
449    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
450    UInt_t pkth = 0;    Long64_t pkth = 0LL;
451    ULong64_t obth = 0;    Long64_t obth = 0LL;
452    //    //
453      if ( rhev || rtev ){
454    
455    
456    T->GetEntry(upperentry);    T->GetEntry(upperentry);
457    code = eh->GetCounter();    code = eh->GetCounter();
458    Int_t lasttrail = code->Get(pctp->RunTrailer);    Int_t lasttrail = code->Get(pctp->RunTrailer);
# Line 431  Int_t PamelaDBOperations::SetUpperLimits Line 471  Int_t PamelaDBOperations::SetUpperLimits
471      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
472    };    };
473    //    //
474    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
475    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
476      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
477      upperpkt = pkth;      upperpkt = pkth;
478      upperobt = obth;      upperobt = obth;
479      rhev = lasthead+1;      rhev = lasthead+1;
480    } else {    } else {
481      rhev = lasthead;      rhev = lasthead;
482    };    };
483    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
484    //    //
485    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
486    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
487      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
488      upperpkt = pktt;      upperpkt = pktt;
489      upperobt = obtt;      upperobt = obtt;
490      rtev = lasttrail+1;      rtev = lasttrail+1;
491    } else {    } else {
492      rtev = lasttrail;      rtev = lasttrail;
493    };    };
494    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
495    //  goto kikko;    //  goto kikko;
496    //    //
497    //    //
498    // Check if runtrailer/runheader are within lower limits    // Check if runtrailer/runheader are within lower limits
499    //    //
500    //    //
501    pkth = 0;    pkth = 0LL;
502    obth = 0;    obth = 0LL;
503    spkth = 0;    spkth = 0LL;
504    sobth = 0;    sobth = 0LL;
505    for (Int_t k=0; k<rhev; k++){    for (Int_t k=0; k<rhev; k++){
506      if ( k > 0 ){      if ( k > 0 ){
507        spkth = pkth;        spkth = pkth;
# Line 509  Int_t PamelaDBOperations::SetUpperLimits Line 549  Int_t PamelaDBOperations::SetUpperLimits
549            upperentry = evbefh-1;            upperentry = evbefh-1;
550          };              };    
551        };        };
552        if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);        if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
553        goto kikko0;        goto kikko0;
554      };      };
555    };    };
# Line 517  Int_t PamelaDBOperations::SetUpperLimits Line 557  Int_t PamelaDBOperations::SetUpperLimits
557    //    //
558    //    //
559    //    //
560    pktt = 0;    pktt = 0LL;
561    obtt = 0;    obtt = 0LL;
562    spktt = 0;    spktt = 0LL;
563    sobtt = 0;    sobtt = 0LL;
564    for (Int_t k=0; k<rtev; k++){    for (Int_t k=0; k<rtev; k++){
565      if ( k > 0 ){      if ( k > 0 ){
566        spktt = pktt;        spktt = pktt;
# Line 540  Int_t PamelaDBOperations::SetUpperLimits Line 580  Int_t PamelaDBOperations::SetUpperLimits
580        rt->GetEntry(rtev);        rt->GetEntry(rtev);
581        pktt = spktt;        pktt = spktt;
582        obtt = sobtt;        obtt = sobtt;
583        if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
584        //        //
585        UInt_t evbeft = 0;        UInt_t evbeft = 0;
586        code = eht->GetCounter();        code = eht->GetCounter();
# Line 569  Int_t PamelaDBOperations::SetUpperLimits Line 609  Int_t PamelaDBOperations::SetUpperLimits
609            upperentry = evbeft-1;            upperentry = evbeft-1;
610          };          };
611        };        };
612        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
613        goto kikko;        goto kikko;
614        //      break;        //      break;
615        //        //
# Line 597  Int_t PamelaDBOperations::SetUpperLimits Line 637  Int_t PamelaDBOperations::SetUpperLimits
637      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
638    };    };
639    //    //
640    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
641    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
642      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
643      upperpkt = pkth;      upperpkt = pkth;
644      upperobt = obth;      upperobt = obth;
645      rhev = lasthead+1;      rhev = lasthead+1;
646    } else {    } else {
647      rhev = lasthead;      rhev = lasthead;
648    };    };
649    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
650    //    //
651    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
652    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
653      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
654      upperpkt = pktt;      upperpkt = pktt;
655      upperobt = obtt;      upperobt = obtt;
656      rtev = lasttrail+1;      rtev = lasttrail+1;
657    } else {    } else {
658      rtev = lasttrail;      rtev = lasttrail;
659    };    };
660    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
661      //
662    
663    
664      };
665    //    //
666    if ( IsDebug() ) printf(" Upper limits are: OBT %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);    if ( IsDebug() ) printf(" Upper limits are: OBT %lld pkt_num %lld upper entry %i \n",upperobt,upperpkt,upperentry);
667    //    //
668    return(0);    return(0);
669  }  }
# Line 753  void PamelaDBOperations::CheckConnection Line 797  void PamelaDBOperations::CheckConnection
797    if( !conn ) throw -1;    if( !conn ) throw -1;
798    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
799    if( !connect ) throw -1;    if( !connect ) throw -1;
800    if ( !dworbit ) throw -27;    //
801      if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
802      //
803      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
804      //
805      // set DB timezone to UTC
806      //
807      stringstream oss;
808      //  
809      oss.str("");
810      oss << "SET time_zone='+0:00';";
811      TSQLResult *result = 0;
812      result = conn->Query(oss.str().c_str());
813      if ( !result ) throw -10;
814      oss.str("");
815      oss << "SET wait_timeout=173000;";
816      conn->Query(oss.str().c_str());
817      //
818  };  };
819    
820  /**  /**
821   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
822   */   */
823  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
824    //    //
825    //  if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u (pktfirst - (UInt_t)(16777212/2)) is %u  \n",pkt_num,pktfirst,(pktfirst - (UInt_t)(16777212/2)));    if ( IsDebug() ) printf(" pkt conversion: pkt_num is %u pktfirst is %u  (UInt_t)(16777214/2)) is %u  \n",pkt_num,pktfirst,(UInt_t)(16777214/2));
826    //    //
827    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ){
828        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
829        return((Long64_t)pkt_num+16777215LL);
830      };
831    //    //
832    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
833      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
834        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
835    };    };
836    //    //
837    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
838      return((Long64_t)pkt_num);
839    //    //
840  };  };
841    
842  /**  /**
843   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
844   */   */
845  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
846    //    //
847    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
848    //    //
849    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
850      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));  
     } else {  
       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));  
     };  
851    };    };
852    //    //
853    return((ULong64_t)obt);    return((Long64_t)obt);
854  };  };
855    
856  /**  /**
# Line 933  Int_t PamelaDBOperations::insertPamelaGL Line 991  Int_t PamelaDBOperations::insertPamelaGL
991    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
992    //    //
993    oss.str("");    oss.str("");
994    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='"
995        << this->GetRawFile().Data() << "';";        << this->GetRawFile().Data() << "';";
996    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
997    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 942  Int_t PamelaDBOperations::insertPamelaGL Line 1000  Int_t PamelaDBOperations::insertPamelaGL
1000    //    //
1001    if ( !row ){    if ( !row ){
1002      oss.str("");      oss.str("");
1003      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< "
1004          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
1005      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1006      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 951  Int_t PamelaDBOperations::insertPamelaGL Line 1009  Int_t PamelaDBOperations::insertPamelaGL
1009      if ( !row ) throw -10;      if ( !row ) throw -10;
1010    };    };
1011    //    //
1012    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);
1013      t0 = (UInt_t)tu.GetSec();
1014      if ( IsDebug() ) printf(" t0 is %u ti is %s %s %s %s %s %s\n",t0,row->GetField(0),row->GetField(1),row->GetField(2),row->GetField(3),row->GetField(4),row->GetField(5));
1015      //
1016    /*    /*
1017     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
1018     */     */
# Line 966  Int_t PamelaDBOperations::insertPamelaGL Line 1027  Int_t PamelaDBOperations::insertPamelaGL
1027    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1028    row = result->Next();    row = result->Next();
1029    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1030        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1031      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;      toffset = (UInt_t)atoll(row->GetField(2)) - (UInt_t)(this->OBT((UInt_t)atoll(row->GetField(1)))/1000) + t0;
1032        //
1033        tsync = (UInt_t)atoll(row->GetField(2));
1034        obt0 = (UInt_t)atoll(row->GetField(1));
1035        //
1036      return(1);      return(1);
1037    };    };
1038    //    //
# Line 1160  Int_t PamelaDBOperations::insertPamelaGL Line 1226  Int_t PamelaDBOperations::insertPamelaGL
1226    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1227    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1228    //    //
1229      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1230    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1231    //    //
1232      tsync = TSYNC;
1233      obt0 = OBT;
1234      //
1235    delete result;    delete result;
1236    return(signal);    return(signal);
1237  }  }
# Line 1261  Int_t PamelaDBOperations::assignBOOT_NUM Line 1331  Int_t PamelaDBOperations::assignBOOT_NUM
1331    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1332    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1333    //    //
1334      Bool_t found = false;
1335    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1336    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1337      Bool_t found = false;      found = false;
1338      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1339        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1340        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1341        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1342          found = true;          found = true;
1343          goto fill;          goto fill;
1344        };        };
# Line 1280  Int_t PamelaDBOperations::assignBOOT_NUM Line 1351  Int_t PamelaDBOperations::assignBOOT_NUM
1351        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1352        //        //
1353      } else {      } else {
1354        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1355        };
1356      } else {
1357        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1358      };
1359      //
1360      UInt_t bn = 0;
1361      Bool_t afound = false;
1362      if ( !found && this->AutoBoot()){
1363        afound = true;
1364        //
1365        // Search for other files with similar timesync
1366        //
1367        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1368        UInt_t upperts = tsync-(obt0/1000)+5;
1369        UInt_t lowerts = tsync-(obt0/1000)-5;
1370        oss.str("");
1371        oss << "select GL_RAW.BOOT_NUMBER from GL_TIMESYNC LEFT JOIN GL_RAW ON GL_RAW.ID = GL_TIMESYNC.ID_RAW where TIMESYNC-(OBT0/1000)<"
1372            << upperts
1373            << " AND TIMESYNC-(OBT0/1000)>"
1374            << lowerts
1375            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1376        result = conn->Query(oss.str().c_str());
1377        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1378        //
1379        if ( !result ) throw -4;;
1380        found = true;
1381        if ( result->GetRowCount()<3 ){
1382          if ( IsDebug() ) printf(" AGH! no results!\n");
1383          found = false;
1384        } else {
1385          row = result->Next();      
1386          bn = (UInt_t)atoll(row->GetField(0));
1387          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1388            if ( !row ) throw -4;
1389            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1390            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1391              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1392              found = false;
1393            };
1394            row = result->Next();
1395          };
1396      };      };
1397      };
1398      //
1399      Int_t sgn = 0;
1400      //
1401      if ( !found && !BOOTNO ){
1402        throw -29;
1403    } else {    } else {
1404      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1405          this->SetBOOTnumber(bn);
1406          sgn = 8;
1407        };
1408    };    };
1409    //    //
1410    oss.str("");    oss.str("");
# Line 1293  Int_t PamelaDBOperations::assignBOOT_NUM Line 1414  Int_t PamelaDBOperations::assignBOOT_NUM
1414    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1415    //    //
1416    delete result;    delete result;
1417    return(0);    return(sgn);
1418  };  };
1419    
1420  /**  /**
# Line 1556  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1677  Bool_t PamelaDBOperations::IsRunAlreadyI
1677      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1678        //        //
1679        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1680        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1681        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1682        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1683        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1684    //       conn->Query(oss.str().c_str());
1685        if ( signal ) signal = false;        if ( signal ) signal = false;
1686        goto gonext;              goto gonext;      
1687        //        //
# Line 1573  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1695  Bool_t PamelaDBOperations::IsRunAlreadyI
1695        //        //
1696        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1697        //        //
1698        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1699        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1700        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1701        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1702    //       conn->Query(oss.str().c_str());
1703        //        //
1704        if ( signal ) signal = false;        if ( signal ) signal = false;
1705        goto gonext;        goto gonext;
# Line 1590  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1713  Bool_t PamelaDBOperations::IsRunAlreadyI
1713        //        //
1714        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1715        //        //
1716        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1717        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1718        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1719        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1720    //       conn->Query(oss.str().c_str());
1721        if ( signal ) signal = false;        if ( signal ) signal = false;
1722        //        //
1723      };      };
# Line 1608  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1732  Bool_t PamelaDBOperations::IsRunAlreadyI
1732    delete result;    delete result;
1733    //    //
1734    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1735      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1736    return(signal);    return(signal);
1737  };  };
1738    
# Line 1656  void PamelaDBOperations::HandleRunFragme Line 1781  void PamelaDBOperations::HandleRunFragme
1781    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1782    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1783    Bool_t found = false;    Bool_t found = false;
1784      Bool_t foundinrun = false;
1785    //    //
1786    TSQLResult *result = 0;    TSQLResult *result = 0;
1787    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1682  void PamelaDBOperations::HandleRunFragme Line 1808  void PamelaDBOperations::HandleRunFragme
1808    //    //
1809    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1810    //    //
1811      // First of all insert the run in the fragment table...
1812      //
1813      oss.str("");
1814      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1815          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1816          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1817          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1818          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1819          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1820          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1821          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1822          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1823          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1824          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1825          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1826          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1827          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1828      //
1829      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1830      result = conn->Query(oss.str().c_str());
1831      //
1832      if ( !result ) throw -4;
1833      //
1834      row = result->Next();
1835      //
1836      if ( !row ){
1837        //
1838        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1839        //
1840        if ( IsDebug() ) printf(" The run is new \n");
1841        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1842        //
1843        glrun->SetID(this->AssignRunID());
1844        glrun->SetID_RUN_FRAG(0);
1845        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1846        //
1847      } else {
1848        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1849        return;
1850      };
1851      //
1852      //
1853    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1854    //        //    
1855    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
# Line 1690  void PamelaDBOperations::HandleRunFragme Line 1858  void PamelaDBOperations::HandleRunFragme
1858      oss.str("");      oss.str("");
1859      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1860          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1861          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1862            << " ID != " << glrun->ID
1863          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1864      //      //
1865      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1700  void PamelaDBOperations::HandleRunFragme Line 1869  void PamelaDBOperations::HandleRunFragme
1869      //      //
1870      row = result->Next();      row = result->Next();
1871      //      //
1872        if ( !row && NoFrag() ){
1873          //
1874          oss.str("");
1875          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1876              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1877              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1878              << " ID != " << glrun->ID
1879              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1880          //
1881          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1882          result = conn->Query(oss.str().c_str());
1883          //
1884          if ( !result ) throw -4;
1885          //
1886          foundinrun = true;
1887          //
1888          row = result->Next();
1889          //
1890        };
1891        //
1892      if ( !row ){      if ( !row ){
1893        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1894        found = false;        found = false;
# Line 1774  void PamelaDBOperations::HandleRunFragme Line 1963  void PamelaDBOperations::HandleRunFragme
1963        //        //
1964        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1965        //        //
1966          if ( foundinrun ){
1967            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1968            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1969          };
1970          //
1971        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1972        //        //
1973        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1974        //        //
1975        oss.str("");        oss.str("");
1976        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1813  void PamelaDBOperations::HandleRunFragme Line 2007  void PamelaDBOperations::HandleRunFragme
2007        //        //
2008        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
2009        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
2010          //
2011        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2012        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2013        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1832  void PamelaDBOperations::HandleRunFragme Line 2027  void PamelaDBOperations::HandleRunFragme
2027        //        //
2028        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2029          //          //
2030          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2031          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2032          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2033          //          //
2034          // get id number          // set id number
         //  
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
 //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
 //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun0 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun0 = (UInt_t)atoll(row->GetField(0));  
 //      };  
2035          //          //
         //      glrun1->SetID_RUN_FRAG(idrun0);  
2036          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2037          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2038          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
2039        };        };
       //  
       delete glrun1;  
       //  
2040        // delete old entry in fragment table        // delete old entry in fragment table
2041        //        //
2042        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2043          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2044        //        //
2045        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2046        //        //
       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
       result =  conn->Query(oss.str().c_str());  
       if ( !result ) throw -4;  
2047        //        //
2048        return;        return;
2049        //        //
# Line 1903  void PamelaDBOperations::HandleRunFragme Line 2052  void PamelaDBOperations::HandleRunFragme
2052    };    };
2053    //    //
2054    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2055                                             // missing it no way we can found a piece in the frag table      // missing it no way we can found a piece in the frag table
2056      //      //
2057      oss.str("");      oss.str("");
2058      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2059          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2060          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2061            << " ID != " << glrun->ID
2062          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2063      //      //
2064      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1918  void PamelaDBOperations::HandleRunFragme Line 2068  void PamelaDBOperations::HandleRunFragme
2068      //      //
2069      row = result->Next();      row = result->Next();
2070      //      //
2071        if ( !row && NoFrag() ){
2072          //
2073          oss.str("");
2074          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2075              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2076              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2077              << " ID != " << glrun->ID
2078              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2079          //
2080          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2081          result = conn->Query(oss.str().c_str());
2082          //
2083          if ( !result ) throw -4;
2084          //
2085          foundinrun = true;
2086          row = result->Next();
2087          //
2088        };
2089        //
2090      if ( !row ){      if ( !row ){
2091        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2092        found = false;        found = false;
# Line 1991  void PamelaDBOperations::HandleRunFragme Line 2160  void PamelaDBOperations::HandleRunFragme
2160        //        //
2161        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2162        //        //
2163          if ( foundinrun ){
2164            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2165            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2166          };
2167          //
2168        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2169        //        //
2170        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2171        //        //
2172        oss.str("");        oss.str("");
2173        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 2049  void PamelaDBOperations::HandleRunFragme Line 2223  void PamelaDBOperations::HandleRunFragme
2223        //        //
2224        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2225          //          //
2226          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2227          //          //
2228          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2229          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2230          //          //
2231          // get id number          // set id number
2232          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
 //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
 //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun0 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun0 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      glrun1->SetID_RUN_FRAG(idrun0);  
 //  
2233          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2234          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2235          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
2236        };        };
2237        //        //
2238        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
       //  
       oss.str("");  
       //  
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
2239        //        //
2240        if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2241        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2242        //        //
2243          delete glrun1;
2244        //        //
2245        return;        return;
2246        //        //
# Line 2151  void PamelaDBOperations::HandleRunFragme Line 2277  void PamelaDBOperations::HandleRunFragme
2277      //      //
2278      row = result->Next();      row = result->Next();
2279      //      //
2280      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->SetID(this->AssignRunID());  
         glrun->SetID_RUN_FRAG(0);  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2281        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2282        } else {
2283          if ( NoFrag() ){
2284            glrun->SetID_RUN_FRAG(glrun->GetID());
2285            glrun->Fill_GL_RUN(conn);      
2286            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2287          };    
2288      };      };
2289    };    };
2290    //    //
# Line 2752  Int_t PamelaDBOperations::insertCALO_CAL Line 2845  Int_t PamelaDBOperations::insertCALO_CAL
2845                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2846                //                //
2847                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);                if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
2848                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2849                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2850                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode                  fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
2851                };                };
# Line 2897  void PamelaDBOperations::HandleTRK_CALIB Line 2990  void PamelaDBOperations::HandleTRK_CALIB
2990        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
2991        //        //
2992        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");        if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB\n");
2993        if ( fromtime < 1150863400 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode        if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
2994        //        //
2995        oss.str("");        oss.str("");
2996        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3026  Int_t PamelaDBOperations::insertTRK_CALI Line 3119  Int_t PamelaDBOperations::insertTRK_CALI
3119      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3120      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3121      //      //
3122      valid = 1;  //     valid = 1;
3123      //  //     //
3124      if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1  //     if ( caltrk1->unpackError != 0 && caltrk1->good0 == 0 ) valid = 0;// CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT1
3125      //      //
3126      //      //
3127      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){      if ( this->PKT(pkt1) >= this->PKT(pktfirst) && this->OBT(obt1) >= this->OBT(obtfirst) ){
3128        //        //
3129        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3130          //      
3131          valid = ValidateTrkCalib( caltrk1, eh1 );
3132          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3133        //        //
3134        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3135        //        //
# Line 3050  Int_t PamelaDBOperations::insertTRK_CALI Line 3146  Int_t PamelaDBOperations::insertTRK_CALI
3146            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3147            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3148            //            //
3149            if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2  //        if ( caltrk2->unpackError != 0 || caltrk2->good0 == 0 ) valid = 0; // CONDITIONS ON THE GOODNESS OF THE CALIBRATION PKT2
3150            //                  //      
3151          } else {          } else {
3152            //            //
# Line 3081  Int_t PamelaDBOperations::insertTRK_CALI Line 3177  Int_t PamelaDBOperations::insertTRK_CALI
3177          //          //
3178          if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
3179          //          //
3180            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3181            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3182            valid = valid & valid2;
3183            //
3184          // Handle good calib          // Handle good calib
3185          //          //
3186          this->HandleTRK_CALIB(true,true);          this->HandleTRK_CALIB(true,true);
# Line 3253  Int_t PamelaDBOperations::insertS4_CALIB Line 3353  Int_t PamelaDBOperations::insertS4_CALIB
3353            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3354            //            //
3355            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");            if ( IsDebug() ) printf(" Calibration with fromtime lower than others to be inserted in the DB \n");
3356            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3357              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3358              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode              fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3359            };            };
# Line 3326  Int_t PamelaDBOperations::insertS4_CALIB Line 3426  Int_t PamelaDBOperations::insertS4_CALIB
3426   * Scan the fragment table and move old fragments to the GL_RUN table   * Scan the fragment table and move old fragments to the GL_RUN table
3427   */   */
3428  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3429      return(this->CleanGL_RUN_FRAGMENTS(""));
3430    };
3431    
3432    /**
3433     * Scan the fragment table and move old fragments to the GL_RUN table
3434     */
3435    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3436    //    //
3437    TSQLResult *result = 0;    TSQLResult *result = 0;
3438    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3333  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3440  Int_t PamelaDBOperations::CleanGL_RUN_FR
3440    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3441    //    //
3442    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3443    //    //
3444    stringstream oss;    stringstream oss;
3445    oss.str("");    oss.str("");
3446    //    //
3447    //    if ( !strcmp(fcleanfile.Data(),"") ){
3448    // check if there are entries older than "olderthan" seconds from now      //
3449    //      // check if there are entries older than "olderthan" seconds from now
3450    oss.str("");      //
3451    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3452        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3453    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3454    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3455    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3456        result = conn->Query(oss.str().c_str());
3457        //
3458      } else {
3459        oss.str("");
3460        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3461        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3462        result = conn->Query(oss.str().c_str());
3463        //    
3464        if ( result ){
3465          //
3466          row = result->Next();
3467          //
3468          if ( row ){
3469            oss.str("");
3470            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3471                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3472            //
3473            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3474            result = conn->Query(oss.str().c_str());
3475            //    
3476          };
3477        } else {
3478          return(2);
3479        };
3480      };
3481    //    //
3482    if ( result ){    if ( result ){
3483      //      //
# Line 3424  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3554  Int_t PamelaDBOperations::CleanGL_RUN_FR
3554        if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));              if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3555        //        //
3556        //        //
3557        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3558        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3559        if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3560        result2 = conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3561        //  //       result2 = conn->Query(oss.str().c_str());
3562        if ( !result2 ) throw -4;  //      //
3563        //  //      if ( !result2 ) throw -4;
3564    //      //
3565        row = result->Next();        row = result->Next();
3566      };      };
3567    };    };
# Line 3442  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3573  Int_t PamelaDBOperations::CleanGL_RUN_FR
3573   * Check if runs are good, i.e. if the tracker calibration is correctly associated..   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3574   */   */
3575  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3576      return(this->ValidateRuns(""));
3577    };
3578    
3579    /**
3580     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3581     */
3582    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3583    //    //
3584    TSQLResult *result = 0;    TSQLResult *result = 0;
3585    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3586    //    //
3587      UInt_t calibtime = 50;
3588      //
3589    stringstream oss;    stringstream oss;
3590    oss.str("");    oss.str("");
3591    //    //
# Line 3454  Int_t PamelaDBOperations::ValidateRuns() Line 3594  Int_t PamelaDBOperations::ValidateRuns()
3594    // =======================================================    // =======================================================
3595    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3596    UInt_t t_start = 0;    UInt_t t_start = 0;
3597    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3598    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3599    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3600    oss.str("");      // --------------------------------------------------------------
3601    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3602            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3603    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3604    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3605    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3606    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3607            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3608            return(1);        printf(" No runs to validate \n");
3609    }else{        return(1);
3610          row = result->Next();      }else{
3611          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3612    };          t_start = (UInt_t)atoll(row->GetField(4));
3613    // --------------------------------------------------------------      };  
3614    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3615    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3616    oss.str("");      // --------------------------------------------------------------
3617    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3618        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3619    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3620    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3621    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3622    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3623            row = result->Next();      if ( result->GetRowCount() ){
3624            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3625          t_stop = (UInt_t)atoll(row->GetField(4));
3626        };
3627        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3628        // --------------------------------------------------------------
3629        // now retrieves runs to be validated
3630        // --------------------------------------------------------------
3631        oss.str("");
3632        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3633        oss << " AND RUNHEADER_TIME >="<< t_stop;
3634        oss << " ORDER BY RUNHEADER_TIME DESC;";
3635        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3636        result = conn->Query(oss.str().c_str());
3637      } else {
3638        //
3639        stringstream myquery;
3640        UInt_t myid = 0;
3641        myquery.str("");
3642        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3643        //
3644        result = conn->Query(myquery.str().c_str());
3645        //
3646        row = result->Next();      
3647        if( !row ){
3648          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3649            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3650            return(2);
3651          };
3652          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3653          return(0);
3654        };
3655        myid=(UInt_t)atoll(row->GetField(0));
3656        //
3657        myquery.str("");
3658        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3659        //
3660        result = conn->Query(myquery.str().c_str());
3661        //
3662        row = result->Next();      
3663        if( !row->GetField(0) || !row->GetField(1)){
3664          //
3665          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3666          //
3667          return(0);
3668          //
3669        } else {  
3670          //
3671          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3672          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3673          UInt_t caltime = 0;
3674          //
3675          myquery.str("");
3676          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3677          myquery << " order by FROM_TIME asc limit 1;";
3678          //
3679          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3680          //
3681          //
3682          result = conn->Query(myquery.str().c_str());
3683          //
3684          row = result->Next();      
3685          if( !row ){
3686            caltime = runhtime;
3687          } else {
3688            caltime = (UInt_t)atoll(row->GetField(0));
3689          };
3690          //
3691          myquery.str("");
3692          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3693          myquery << caltime << " order by RUNHEADER_TIME DESC";
3694          //
3695          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3696          //
3697          result = conn->Query(myquery.str().c_str());
3698          //
3699        };
3700    };    };
3701    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);    //
   // --------------------------------------------------------------  
   // now retrieves runs to be validated  
   // --------------------------------------------------------------  
   oss.str("");  
   oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;  
   oss << " AND RUNHEADER_TIME >="<< t_stop;  
   oss << " ORDER BY RUNHEADER_TIME DESC;";  
 //  if ( IsDebug() )  
   if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
3702    if ( !result ) throw -4;    if ( !result ) throw -4;
3703    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3704  //  printf("------------------------------------------------------------------------------- \n");    //
     
3705    Int_t nrow = 0;    Int_t nrow = 0;
3706    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3707    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3511  Int_t PamelaDBOperations::ValidateRuns() Line 3715  Int_t PamelaDBOperations::ValidateRuns()
3715    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3716    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3717    // - loop over runs, back in time,    // - loop over runs, back in time,
3718    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3719    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3720    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3721    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3551  Int_t PamelaDBOperations::ValidateRuns() Line 3755  Int_t PamelaDBOperations::ValidateRuns()
3755                                                        
3756                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3757                                                        
3758                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration                            if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration
3759                                                        
3760                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3761                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
# Line 3749  Int_t PamelaDBOperations::populateTLE()/ Line 3953  Int_t PamelaDBOperations::populateTLE()/
3953    
3954    
3955  // Insert tle in the table GL_TLE using the connection conn.  // Insert tle in the table GL_TLE using the connection conn.
3956  int PamelaDBOperations::insertTle(cTle *tle)  Int_t PamelaDBOperations::insertTle(cTle *tle)
3957  {  {
3958    stringstream oss;    stringstream oss;
3959    TSQLResult *result = 0;    TSQLResult *result = 0;
# Line 3832  string getTleDatetime(cTle *tle) Line 4036  string getTleDatetime(cTle *tle)
4036    
4037    return date.str();    return date.str();
4038  }  }
4039    
4040    /**
4041     * Remove a file from the DB, delete on cascade all entries related to that file
4042     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4043     * calibration
4044     **/
4045    Int_t PamelaDBOperations::removeFile(TString remfile){
4046      //
4047      // Determine ID_ROOT_L0 and ID_RAW
4048      //
4049      TSQLResult *pResult;
4050      TSQLRow *Row;
4051      stringstream myquery;
4052      //  
4053      myquery.str("");
4054      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4055      //
4056      pResult = conn->Query(myquery.str().c_str());
4057      //
4058      Row = pResult->Next();      
4059      if( !Row ){
4060        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4061          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4062          return(1);
4063        };
4064        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4065        return(0);
4066      };
4067      //
4068      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4069      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4070      //
4071      this->ValidationOFF();
4072      //
4073      this->RemoveCALIBS();
4074      //
4075      this->RemoveRUNS();
4076      //
4077      this->RemoveFILES();
4078      //
4079      this->SetID_ROOT(0);
4080      this->SetID_RAW(0);
4081      //
4082      return(0);
4083    };
4084    
4085    /**
4086     *
4087     * Set validation bit to zero for runs following the removing file till
4088     * 1) a run with TRK_CALIB_USED=140
4089     * 2) a run with VALIDATION = 0
4090     * 3) the next calibration
4091     *
4092     **/
4093    void PamelaDBOperations::ValidationOFF(){
4094      TSQLResult *pResult;
4095      TSQLRow *Row;
4096      stringstream myquery;
4097      Int_t unv = 0;
4098      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4099      myquery.str("");
4100      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4101      //
4102      pResult = conn->Query(myquery.str().c_str());
4103      //
4104      Row = pResult->Next();      
4105      if( !Row->GetField(0) ){
4106        //
4107        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4108        //
4109      } else {  
4110        //
4111        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4112        UInt_t caltime = 0;
4113        //
4114        myquery.str("");
4115        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4116        myquery << " order by FROM_TIME asc limit 1;";
4117        //
4118        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4119        //
4120        //
4121        delete pResult;
4122        pResult = conn->Query(myquery.str().c_str());
4123        //
4124        Row = pResult->Next();      
4125        if( !Row ){
4126          caltime = runhtime;
4127        } else {
4128          caltime = (UInt_t)atoll(Row->GetField(0));
4129        };
4130        //
4131        myquery.str("");
4132        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4133        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4134        //
4135        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4136        //
4137        pResult = conn->Query(myquery.str().c_str());
4138        //
4139        Row = pResult->Next();      
4140        if( !Row ){
4141          //
4142          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4143          //
4144        } else {
4145          myquery.str("");
4146          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4147          myquery << " RUNHEADER_TIME>=" <<runhtime;
4148          myquery << " order by RUNHEADER_TIME asc;";
4149          //
4150          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4151          //
4152          pResult = conn->Query(myquery.str().c_str());
4153          //
4154          Row = pResult->Next();  
4155          while ( Row ){
4156            //      
4157            unv++;
4158            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4159            Row = pResult->Next();  
4160            //
4161          };
4162        };
4163      };
4164      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4165    };
4166    
4167    /**
4168     *
4169     * Rearrange GL_RUN table and remove runs
4170     *
4171     **/
4172    void PamelaDBOperations::RemoveRUNS(){
4173      TSQLResult *pResult;
4174      TSQLRow *Row;
4175      stringstream myquery;
4176      UInt_t drun = 0;
4177      GL_RUN *delrun = new GL_RUN();
4178      //
4179      myquery.str("");
4180      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4181      //
4182      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4183      //
4184      pResult = conn->Query(myquery.str().c_str());
4185      //
4186      Row = pResult->Next();    
4187      //
4188      //
4189      if ( !Row ){
4190        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4191      } else {
4192        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4193        while ( Row ){
4194          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4195          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4196          drun++;
4197          Row = pResult->Next();    
4198        };
4199      };
4200      //
4201      //
4202      myquery.str("");
4203      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4204      //
4205      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4206      //
4207      pResult = conn->Query(myquery.str().c_str());
4208      //
4209      Row = pResult->Next();    
4210      //
4211      if ( !Row ){
4212        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4213      } else {
4214        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4215        while ( Row ){
4216          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4217          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4218          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4219          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4220          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4221            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4222            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4223          };
4224          drun++;
4225          Row = pResult->Next();    
4226        };
4227      };
4228      //
4229      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4230      //
4231      //
4232      //
4233      drun = 0;
4234      //
4235      myquery.str("");
4236      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4237      //
4238      pResult = conn->Query(myquery.str().c_str());
4239      //
4240      Row = pResult->Next();    
4241      //
4242      if ( !Row ){
4243        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4244      } else {
4245        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4246        while ( Row ){
4247          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4248          myquery.str("");
4249          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4250          conn->Query(myquery.str().c_str());
4251          drun++;
4252          Row = pResult->Next();    
4253        };
4254      };
4255      //  
4256      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4257      //
4258      //
4259      //
4260      drun = 0;
4261      //
4262      myquery.str("");
4263      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4264      //
4265      pResult = conn->Query(myquery.str().c_str());
4266      //
4267      Row = pResult->Next();    
4268      //
4269      if ( !Row ){
4270        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4271      } else {
4272        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4273        while ( Row ){
4274          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4275          myquery.str("");
4276          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4277          conn->Query(myquery.str().c_str());
4278          drun++;
4279          Row = pResult->Next();    
4280        };
4281      };
4282      //  
4283      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4284      //
4285      //
4286      //
4287      delete delrun;
4288      //
4289    };
4290    
4291    
4292    /**
4293     *
4294     * Rearrange calibration tables
4295     *
4296     **/
4297    void PamelaDBOperations::RemoveFILES(){
4298      stringstream myquery;
4299      //
4300      myquery.str("");
4301      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4302      //
4303      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4304      //
4305      conn->Query(myquery.str().c_str());
4306      //
4307    };
4308    
4309    /**
4310     *
4311     * Rearrange calibration tables
4312     *
4313     **/
4314    void PamelaDBOperations::RemoveCALIBS(){
4315      TSQLResult *pResult;
4316      TSQLRow *Row;
4317      stringstream myquery;
4318      //  
4319      //
4320      // Calorimeter
4321      //
4322      for (Int_t section = 0; section < 4; section++){
4323        myquery.str("");
4324        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4325        myquery << " SECTION=" << section << ";";
4326        //
4327        pResult = conn->Query(myquery.str().c_str());
4328        //
4329        Row = pResult->Next();      
4330        if( !Row->GetField(0) || !Row->GetField(1) ){
4331          //
4332          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4333          //
4334        } else {
4335          //
4336          myquery.str("");
4337          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4338          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4339          myquery << " SECTION=" << section << ";";
4340          //
4341          pResult = conn->Query(myquery.str().c_str());
4342          //
4343          if( !pResult ){
4344            //
4345            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4346            //
4347            throw -4;
4348            //
4349          };
4350          //
4351        };
4352      };
4353      myquery.str("");
4354      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4355      //
4356      pResult = conn->Query(myquery.str().c_str());
4357      //
4358      if( !pResult ){
4359        //
4360        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4361        //
4362        throw -4;
4363        //
4364      };
4365      //
4366      // Tracker
4367      //
4368      myquery.str("");
4369      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4370      //
4371      pResult = conn->Query(myquery.str().c_str());
4372      //
4373      Row = pResult->Next();      
4374      if( !Row->GetField(0) || !Row->GetField(1) ){
4375        //
4376        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4377        //
4378      } else {
4379        //
4380        myquery.str("");
4381        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4382        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4383        //
4384        pResult = conn->Query(myquery.str().c_str());
4385        //
4386        if( !pResult ){
4387          //
4388          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4389          //
4390          throw -4;
4391          //
4392        };
4393        //
4394        myquery.str("");
4395        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4396        //
4397        pResult = conn->Query(myquery.str().c_str());
4398        //
4399        if( !pResult ){
4400          //
4401          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4402          //
4403          throw -4;
4404          //
4405        };
4406      };
4407      //
4408      //
4409      // S4
4410      //
4411      myquery.str("");
4412      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4413      //
4414      pResult = conn->Query(myquery.str().c_str());
4415      //
4416      Row = pResult->Next();      
4417      if( !Row->GetField(0) || !Row->GetField(1) ){
4418        //
4419        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4420        //
4421      } else {
4422        //
4423        myquery.str("");
4424        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4425        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4426        //
4427        pResult = conn->Query(myquery.str().c_str());
4428        //
4429        if( !pResult ){
4430          //
4431          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4432          //
4433          throw -4;
4434          //
4435        };
4436        //
4437        myquery.str("");
4438        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4439        //
4440        pResult = conn->Query(myquery.str().c_str());
4441        //
4442        if( !pResult ){
4443          //
4444          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4445          //
4446          throw -4;
4447          //
4448        };
4449        //
4450      };
4451    };
4452    
4453    /**
4454     *
4455     * Rearrange calibration tables
4456     *
4457     **/
4458    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4459    
4460        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4461        UInt_t timeaftercalib=120000; //2000;
4462    //  ----------
4463    //  Check CRCs
4464    //  ----------
4465        for(Int_t ipkt=0; ipkt<6; ipkt++){
4466            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4467            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4468        }
4469    //  -----------------------
4470    //  Check missing packets:
4471    //  -----------------------
4472    //    Readout order:
4473    //    ------------------
4474    //    DSP   packet board
4475    //    ------------------
4476    //    12    0      1
4477    //    10    1      1
4478    //     8    2      1
4479    //     4    3      1
4480    //     6    4      1
4481    //     2    5      1
4482    //    ------------------
4483    //    11    0      2
4484    //     9    1      2
4485    //     7    2      2
4486    //     3    3      2
4487    //     5    4      2
4488    //     1    5      2
4489    //    ------------------
4490    //  -------------------------------------------------
4491    //  Check if it is first or second calibration packet
4492    //  -------------------------------------------------
4493        UInt_t build=0;
4494        TString classname = caltrk->GetName();
4495        UInt_t base=0;
4496        UInt_t mask=0;
4497        if(classname.Contains("CalibTrk1Event")){
4498            base=12;
4499            mask=0x03F000;
4500        }
4501        if(classname.Contains("CalibTrk2Event")){
4502            base=18;
4503            mask=0xFC0000;
4504        }
4505    //  -------------------------------------------------
4506    //  Count number of packets and set build variable
4507    //  -------------------------------------------------
4508        Int_t  npkts=0;
4509        for(Int_t ipkt=0; ipkt<6; ipkt++){
4510            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4511                npkts++;
4512                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4513            }
4514        }
4515    //    if( npkts==6 )return 1; // :-)
4516    
4517    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4518    
4519    //  -----------------------------------------------
4520    //  If missing packets: check the acq configuration
4521    //  (some DSPs might be excluded from acquisition)
4522    //  -----------------------------------------------
4523    
4524    //  -----------------------------------------------
4525    //  retrieve the first run header after calib
4526    //  -----------------------------------------------
4527         PacketType *pctp;
4528         EventCounter *cod;
4529         cod = eh->GetCounter();
4530         Int_t irun = cod->Get(pctp->RunHeader);
4531         TTree *rh=(TTree*)file->Get("RunHeader");
4532         if ( !rh || rh->IsZombie() ) throw -17;
4533         if( rh->GetEntries() == irun ){
4534              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4535             return 0; // :-(
4536         }
4537    
4538         RunHeaderEvent *run  = 0;
4539         EventHeader    *hrun = 0;
4540         rh->SetBranchAddress("RunHeader", &run);
4541         rh->SetBranchAddress("Header", &hrun);
4542         rh->GetEntry(irun);
4543    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4544    
4545         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4546              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4547             return 0; // :-(
4548         }
4549        
4550         if( !run->RM_ACQ_AFTER_CALIB ){
4551              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4552             return 0; // :-(
4553         }
4554    
4555         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4556         if( dtime > timeaftercalib ){
4557             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4558             return 0; // :-(
4559         }
4560        
4561    
4562    
4563         if( (run->ACQ_BUILD_INFO & mask) != build ){
4564             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4565             return 0; // :-(
4566         }
4567         return 1; // :-)
4568    
4569    }
4570    
4571    /**
4572     *
4573     * Check the DB (only for overlapping runs at the moment)
4574     *
4575     **/
4576    UInt_t PamelaDBOperations::Check(){
4577      //
4578      UInt_t test = 0;
4579      //
4580      UInt_t thisrht = 0;
4581      UInt_t thisrtt = 0;
4582      UInt_t thisid = 0;
4583      UInt_t prevrht = 0;
4584      UInt_t prevrtt = 0;
4585      UInt_t previd = 0;
4586      //
4587      UInt_t prevl0id = 0;
4588      UInt_t thisl0id = 0;
4589      //
4590      stringstream oss;
4591      TSQLResult *result = 0;
4592      TSQLRow    *row    = 0;
4593      TSQLResult *result2 = 0;
4594      TSQLRow    *row2    = 0;
4595      oss.str("");
4596      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4597      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4598      result = conn->Query(oss.str().c_str());
4599      //
4600      if ( !result ) throw -4;;
4601      //
4602      row = result->Next();
4603      //
4604      while ( row ){
4605        thisid = (UInt_t)atoll(row->GetField(0));
4606        thisl0id = (UInt_t)atoll(row->GetField(1));
4607        thisrht = (UInt_t)atoll(row->GetField(2));
4608        thisrtt = (UInt_t)atoll(row->GetField(3));
4609        //
4610        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4611        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4612        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4613          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4614          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4615          TString prevf = "";
4616          TString thisf = "";
4617          oss.str("");
4618          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4619          result2 = conn->Query(oss.str().c_str());
4620          if ( !result2 ) throw -4;;
4621          row2 = result2->Next();
4622          prevf = (TString)row2->GetField(0);
4623          oss.str("");
4624          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4625          result2 = conn->Query(oss.str().c_str());
4626          if ( !result2 ) throw -4;;
4627          row2 = result2->Next();
4628          thisf = (TString)row2->GetField(0);
4629          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4630          test = 1;
4631        };
4632        //
4633        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4634          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4635          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4636          TString prevf = "";
4637          TString thisf = "";
4638          oss.str("");
4639          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4640          result2 = conn->Query(oss.str().c_str());
4641          if ( !result2 ) throw -4;;
4642          row2 = result2->Next();
4643          prevf = (TString)row2->GetField(0);
4644          oss.str("");
4645          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4646          result2 = conn->Query(oss.str().c_str());
4647          if ( !result2 ) throw -4;;
4648          row2 = result2->Next();
4649          thisf = (TString)row2->GetField(0);
4650          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4651          test = 1;
4652        };
4653        //
4654        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4655          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4656          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4657          TString prevf = "";
4658          TString thisf = "";
4659          oss.str("");
4660          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4661          result2 = conn->Query(oss.str().c_str());
4662          if ( !result2 ) throw -4;;
4663          row2 = result2->Next();
4664          prevf = (TString)row2->GetField(0);
4665          oss.str("");
4666          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4667          result2 = conn->Query(oss.str().c_str());
4668          if ( !result2 ) throw -4;;
4669          row2 = result2->Next();
4670          thisf = (TString)row2->GetField(0);
4671          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4672          test = 1;
4673        };
4674    
4675        //
4676        prevrht = thisrht;
4677        prevrtt = thisrtt;
4678        previd = thisid;
4679        prevl0id = thisl0id;
4680        row = result->Next();
4681      };
4682      //
4683      return(test);
4684      //
4685    };

Legend:
Removed from v.1.14  
changed lines
  Added in v.1.27

  ViewVC Help
Powered by ViewVC 1.1.23