/[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.12 by mocchiut, Tue Oct 17 15:20:04 2006 UTC revision 1.24 by mocchiut, Mon Dec 4 11:32:07 2006 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 31  Line 31 
31  #include <varDump/VarDumpRecord.h>  #include <varDump/VarDumpRecord.h>
32  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
33  //  //
34  #include <cTle.h>  #include <sgp4.h>
 #include <cEci.h>  
 #include <cJulian.h>  
35    
36  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
37  //  //
# Line 81  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();
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 111  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 126  void PamelaDBOperations::CheckValidate(L Line 128  void PamelaDBOperations::CheckValidate(L
128   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
129   */   */
130  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
131      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
132    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
133  };  };
134    
# 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 194  void PamelaDBOperations::SetRootName(TSt Line 213  void PamelaDBOperations::SetRootName(TSt
213  };  };
214    
215  /**  /**
216     * Store the downlink orbit number from filename.
217     */
218    void PamelaDBOperations::SetOrbitNo(){
219      dworbit = 0;
220      TString name = this->GetRootFile();
221      Int_t nlength = name.Length();
222      if ( nlength < 5 ) return;
223      TString dwo = 0;
224      for (Int_t i = 0; i<5; i++){
225        dwo.Append(name[i],1);
226      };
227      if ( dwo.IsDigit() ){
228        dworbit = (UInt_t)dwo.Atoi();
229      } else {
230        dwo="";
231        for (Int_t i = 8; i<13; i++){
232          dwo.Append(name[i],1);
233        };    
234        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
235      };
236      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
237      return;
238    };
239    
240    
241    
242    /**
243   * Store the NOBOOT flag.   * Store the NOBOOT flag.
244   * @param noboot    true/false.   * @param noboot    true/false.
245   */   */
# Line 257  Int_t PamelaDBOperations::SetUpperLimits Line 303  Int_t PamelaDBOperations::SetUpperLimits
303    UInt_t nevent = 0;    UInt_t nevent = 0;
304    UInt_t pktlast = 0;    UInt_t pktlast = 0;
305    UInt_t obtlast = 0;    UInt_t obtlast = 0;
306    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
307    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
308    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
309    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
310      Long64_t upperobt2 = 0LL;
311    UInt_t zomp = 0;    UInt_t zomp = 0;
312    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
313    EventCounter *code=0;    EventCounter *code=0;
314    //    //
315      Long64_t deltapkt = 5000LL;
316      Long64_t deltaobt = 50000LL;
317      //
318    //   pcksList packetsNames;    //   pcksList packetsNames;
319    //   pcksList::iterator Iter;    //   pcksList::iterator Iter;
320    //   getPacketsNames(packetsNames);    //   getPacketsNames(packetsNames);
# Line 301  Int_t PamelaDBOperations::SetUpperLimits Line 351  Int_t PamelaDBOperations::SetUpperLimits
351    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
352    upperentry = nevent-1;    upperentry = nevent-1;
353    //    //
354    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);
355    //    //
356    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);
357    //    //
358    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);
359    //    //
# Line 311  Int_t PamelaDBOperations::SetUpperLimits Line 361  Int_t PamelaDBOperations::SetUpperLimits
361    //    //
362    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
363    //    //
364    if ( PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) ){
365        //
366      // go back      // go back
367      zomp = nevent - 2;      zomp = nevent - 2;
368      //      //
# Line 333  Int_t PamelaDBOperations::SetUpperLimits Line 384  Int_t PamelaDBOperations::SetUpperLimits
384          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
385          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
386          //          //
387          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) ){
388              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
389              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
390              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);    
391              throw -13;    
392            };
393          //          //
394          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 ){
395            zomp = i + jump + 1;            zomp = i + jump + 1;
396            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
397            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);
398            break;            break;
399          };          };
400          //          //
# Line 373  Int_t PamelaDBOperations::SetUpperLimits Line 429  Int_t PamelaDBOperations::SetUpperLimits
429    //    //
430    rhev = rh->GetEntries();    rhev = rh->GetEntries();
431    rtev = rt->GetEntries();    rtev = rt->GetEntries();
432    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
433    UInt_t sobth = 0;    Long64_t sobth = 0LL;
434    UInt_t spktt = 0;    Long64_t spktt = 0LL;
435    UInt_t spkth = 0;    Long64_t spkth = 0LL;
436    UInt_t pktt = 0;    Long64_t pktt = 0LL;
437    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
438    UInt_t pkth = 0;    Long64_t pkth = 0LL;
439    ULong64_t obth = 0;    Long64_t obth = 0LL;
440    //    //
441    T->GetEntry(upperentry);    T->GetEntry(upperentry);
442    code = eh->GetCounter();    code = eh->GetCounter();
# Line 400  Int_t PamelaDBOperations::SetUpperLimits Line 456  Int_t PamelaDBOperations::SetUpperLimits
456      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
457    };    };
458    //    //
459    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);
460    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
461      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);
462      upperpkt = pkth;      upperpkt = pkth;
463      upperobt = obth;      upperobt = obth;
464      rhev = lasthead+1;      rhev = lasthead+1;
465    } else {    } else {
466      rhev = lasthead;      rhev = lasthead;
467    };    };
468    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);
469    //    //
470    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);
471    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
472      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);
473      upperpkt = pktt;      upperpkt = pktt;
474      upperobt = obtt;      upperobt = obtt;
475      rtev = lasttrail+1;      rtev = lasttrail+1;
476    } else {    } else {
477      rtev = lasttrail;      rtev = lasttrail;
478    };    };
479    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);
480    //  goto kikko;    //  goto kikko;
481    //    //
482    //    //
483    // Check if runtrailer/runheader are within lower limits    // Check if runtrailer/runheader are within lower limits
484    //    //
485    //    //
486    pkth = 0;    pkth = 0LL;
487    obth = 0;    obth = 0LL;
488    spkth = 0;    spkth = 0LL;
489    sobth = 0;    sobth = 0LL;
490    for (Int_t k=0; k<rhev; k++){    for (Int_t k=0; k<rhev; k++){
491      if ( k > 0 ){      if ( k > 0 ){
492        spkth = pkth;        spkth = pkth;
# Line 478  Int_t PamelaDBOperations::SetUpperLimits Line 534  Int_t PamelaDBOperations::SetUpperLimits
534            upperentry = evbefh-1;            upperentry = evbefh-1;
535          };              };    
536        };        };
537        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);
538        goto kikko0;        goto kikko0;
539      };      };
540    };    };
# Line 486  Int_t PamelaDBOperations::SetUpperLimits Line 542  Int_t PamelaDBOperations::SetUpperLimits
542    //    //
543    //    //
544    //    //
545    pktt = 0;    pktt = 0LL;
546    obtt = 0;    obtt = 0LL;
547    spktt = 0;    spktt = 0LL;
548    sobtt = 0;    sobtt = 0LL;
549    for (Int_t k=0; k<rtev; k++){    for (Int_t k=0; k<rtev; k++){
550      if ( k > 0 ){      if ( k > 0 ){
551        spktt = pktt;        spktt = pktt;
# Line 509  Int_t PamelaDBOperations::SetUpperLimits Line 565  Int_t PamelaDBOperations::SetUpperLimits
565        rt->GetEntry(rtev);        rt->GetEntry(rtev);
566        pktt = spktt;        pktt = spktt;
567        obtt = sobtt;        obtt = sobtt;
568        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);
569        //        //
570        UInt_t evbeft = 0;        UInt_t evbeft = 0;
571        code = eht->GetCounter();        code = eht->GetCounter();
# Line 538  Int_t PamelaDBOperations::SetUpperLimits Line 594  Int_t PamelaDBOperations::SetUpperLimits
594            upperentry = evbeft-1;            upperentry = evbeft-1;
595          };          };
596        };        };
597        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);
598        goto kikko;        goto kikko;
599        //      break;        //      break;
600        //        //
# Line 566  Int_t PamelaDBOperations::SetUpperLimits Line 622  Int_t PamelaDBOperations::SetUpperLimits
622      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
623    };    };
624    //    //
625    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);
626    if ( pkth > upperpkt && obth > upperobt ){    if ( pkth > upperpkt && obth > upperobt ){
627      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);
628      upperpkt = pkth;      upperpkt = pkth;
629      upperobt = obth;      upperobt = obth;
630      rhev = lasthead+1;      rhev = lasthead+1;
631    } else {    } else {
632      rhev = lasthead;      rhev = lasthead;
633    };    };
634    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);
635    //    //
636    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);
637    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
638      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);
639      upperpkt = pktt;      upperpkt = pktt;
640      upperobt = obtt;      upperobt = obtt;
641      rtev = lasttrail+1;      rtev = lasttrail+1;
642    } else {    } else {
643      rtev = lasttrail;      rtev = lasttrail;
644    };    };
645    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);
646    //    //
647    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);
648    //    //
649    return(0);    return(0);
650  }  }
651    
652    /**
653     *
654     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
655     * Entries in the _RUNID_GEN table are never deleted.
656     *
657     **/
658    UInt_t PamelaDBOperations::AssignRunID(){
659      //
660      TSQLResult *result = 0;
661      TSQLRow *row = 0;
662      UInt_t runid = 0;
663      //
664      stringstream   oss;
665      //  
666      oss.str("");
667      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
668      result = conn->Query(oss.str().c_str());
669      if ( !result ) throw -10;
670      oss.str("");
671      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
672      result = conn->Query(oss.str().c_str());
673      if ( !result ) throw -10;
674      //
675      row = result->Next();
676      //
677      if ( !row ) throw -28;
678      //
679      runid = (UInt_t)atoll(row->GetField(0));
680      //
681      return(runid);
682    };
683    
684  //  //
685  // GETTERS  // GETTERS
686  //  //
# Line 690  void PamelaDBOperations::CheckConnection Line 778  void PamelaDBOperations::CheckConnection
778    if( !conn ) throw -1;    if( !conn ) throw -1;
779    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
780    if( !connect ) throw -1;    if( !connect ) throw -1;
781      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
782      //
783      // set DB timezone to UTC
784      //
785      stringstream oss;
786      //  
787      oss.str("");
788      oss << "SET time_zone='+0:00';";
789      TSQLResult *result = 0;
790      result = conn->Query(oss.str().c_str());
791      if ( !result ) throw -10;
792      //
793  };  };
794    
795  /**  /**
796   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
797   */   */
798  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
799    //    //
800    //  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));
801    //    //
802    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ){
803        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
804        return((Long64_t)pkt_num+16777215LL);
805      };
806    //    //
807    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){
808      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
809        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
810    };    };
811    //    //
812    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
813      return((Long64_t)pkt_num);
814    //    //
815  };  };
816    
817  /**  /**
818   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
819   */   */
820  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
821    //    //
822    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()));
823    //    //
824    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
825      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()));  
     };  
826    };    };
827    //    //
828    return((ULong64_t)obt);    return((Long64_t)obt);
829  };  };
830    
831  /**  /**
# Line 869  Int_t PamelaDBOperations::insertPamelaGL Line 966  Int_t PamelaDBOperations::insertPamelaGL
966    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
967    //    //
968    oss.str("");    oss.str("");
969    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    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='"
970        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
971    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
972    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
973    if ( !result ) throw -10;    if ( !result ) throw -10;
974    row = result->Next();    row = result->Next();
   if ( !row ) throw -10;  
975    //    //
976    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    if ( !row ){
977        oss.str("");
978        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< "
979            << dworbit << " order by FROM_ORBIT desc limit 1;";
980        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
981        result = conn->Query(oss.str().c_str());
982        if ( !result ) throw -10;
983        row = result->Next();
984        if ( !row ) throw -10;
985      };
986      //
987      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);
988      t0 = (UInt_t)tu.GetSec();
989      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));
990      //
991    /*    /*
992     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
993     */     */
# Line 893  Int_t PamelaDBOperations::insertPamelaGL Line 1002  Int_t PamelaDBOperations::insertPamelaGL
1002    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1003    row = result->Next();    row = result->Next();
1004    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1005        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1006      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;
1007        //
1008        tsync = (UInt_t)atoll(row->GetField(2));
1009        obt0 = (UInt_t)atoll(row->GetField(1));
1010        //
1011      return(1);      return(1);
1012    };    };
1013    //    //
# Line 1087  Int_t PamelaDBOperations::insertPamelaGL Line 1201  Int_t PamelaDBOperations::insertPamelaGL
1201    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1202    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());
1203    //    //
1204      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1205    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1206    //    //
1207      tsync = TSYNC;
1208      obt0 = OBT;
1209      //
1210    delete result;    delete result;
1211    return(signal);    return(signal);
1212  }  }
# Line 1188  Int_t PamelaDBOperations::assignBOOT_NUM Line 1306  Int_t PamelaDBOperations::assignBOOT_NUM
1306    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1307    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1308    //    //
1309      Bool_t found = false;
1310    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1311    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1312      Bool_t found = false;      found = false;
1313      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1314        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1315        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1316        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1317          found = true;          found = true;
1318          goto fill;          goto fill;
1319        };        };
# Line 1207  Int_t PamelaDBOperations::assignBOOT_NUM Line 1326  Int_t PamelaDBOperations::assignBOOT_NUM
1326        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1327        //        //
1328      } else {      } else {
1329        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1330        };
1331      } else {
1332        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1333      };
1334      //
1335      UInt_t bn = 0;
1336      Bool_t afound = false;
1337      if ( !found && this->AutoBoot()){
1338        afound = true;
1339        //
1340        // Search for other files with similar timesync
1341        //
1342        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1343        UInt_t upperts = tsync-(obt0/1000)+5;
1344        UInt_t lowerts = tsync-(obt0/1000)-5;
1345        oss.str("");
1346        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)<"
1347            << upperts
1348            << " AND TIMESYNC-(OBT0/1000)>"
1349            << lowerts
1350            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1351        result = conn->Query(oss.str().c_str());
1352        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1353        //
1354        if ( !result ) throw -4;;
1355        found = true;
1356        if ( result->GetRowCount()<3 ){
1357          if ( IsDebug() ) printf(" AGH! no results!\n");
1358          found = false;
1359        } else {
1360          row = result->Next();      
1361          bn = (UInt_t)atoll(row->GetField(0));
1362          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1363            if ( !row ) throw -4;
1364            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1365            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1366              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1367              found = false;
1368            };
1369            row = result->Next();
1370          };
1371      };      };
1372      };
1373      //
1374      Int_t sgn = 0;
1375      //
1376      if ( !found ){
1377        throw -29;
1378    } else {    } else {
1379      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1380          this->SetBOOTnumber(bn);
1381          sgn = 8;
1382        };
1383    };    };
1384    //    //
1385    oss.str("");    oss.str("");
# Line 1220  Int_t PamelaDBOperations::assignBOOT_NUM Line 1389  Int_t PamelaDBOperations::assignBOOT_NUM
1389    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1390    //    //
1391    delete result;    delete result;
1392    return(0);    return(sgn);
1393  };  };
1394    
1395  /**  /**
# Line 1483  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1652  Bool_t PamelaDBOperations::IsRunAlreadyI
1652      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1653        //        //
1654        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");
1655        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1656        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1657        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) <<";";
1658        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1659    //       conn->Query(oss.str().c_str());
1660        if ( signal ) signal = false;        if ( signal ) signal = false;
1661        goto gonext;              goto gonext;      
1662        //        //
# Line 1500  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1670  Bool_t PamelaDBOperations::IsRunAlreadyI
1670        //        //
1671        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");
1672        //        //
1673        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1674        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1675        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) <<";";
1676        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1677    //       conn->Query(oss.str().c_str());
1678        //        //
1679        if ( signal ) signal = false;        if ( signal ) signal = false;
1680        goto gonext;        goto gonext;
# Line 1517  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1688  Bool_t PamelaDBOperations::IsRunAlreadyI
1688        //        //
1689        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");
1690        //        //
1691        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1692        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1693        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) <<";";
1694        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1695    //       conn->Query(oss.str().c_str());
1696        if ( signal ) signal = false;        if ( signal ) signal = false;
1697        //        //
1698      };      };
# Line 1535  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1707  Bool_t PamelaDBOperations::IsRunAlreadyI
1707    delete result;    delete result;
1708    //    //
1709    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1710      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1711    return(signal);    return(signal);
1712  };  };
1713    
# Line 1556  void PamelaDBOperations::HandleRun(){   Line 1729  void PamelaDBOperations::HandleRun(){  
1729      //      //
1730      this->FillClass();      this->FillClass();
1731      //      //
1732      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1733          glrun->SetID(this->AssignRunID());
1734          glrun->SetID_RUN_FRAG(0);
1735          glrun->Fill_GL_RUN(conn);
1736        };
1737    } else {    } else {
1738      //      //
1739      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
# Line 1579  void PamelaDBOperations::HandleRunFragme Line 1756  void PamelaDBOperations::HandleRunFragme
1756    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1757    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1758    Bool_t found = false;    Bool_t found = false;
1759      Bool_t foundinrun = false;
1760    //    //
1761    TSQLResult *result = 0;    TSQLResult *result = 0;
1762    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1605  void PamelaDBOperations::HandleRunFragme Line 1783  void PamelaDBOperations::HandleRunFragme
1783    //    //
1784    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");
1785    //    //
1786      // First of all insert the run in the fragment table...
1787      //
1788      oss.str("");
1789      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1790          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1791          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1792          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1793          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1794          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1795          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1796          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1797          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1798          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1799          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1800          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1801          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1802          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1803      //
1804      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1805      result = conn->Query(oss.str().c_str());
1806      //
1807      if ( !result ) throw -4;
1808      //
1809      row = result->Next();
1810      //
1811      if ( !row ){
1812        //
1813        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1814        //
1815        if ( IsDebug() ) printf(" The run is new \n");
1816        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1817        //
1818        glrun->SetID(this->AssignRunID());
1819        glrun->SetID_RUN_FRAG(0);
1820        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1821        //
1822      } else {
1823        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1824        return;
1825      };
1826      //
1827      //
1828    // 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?
1829    //        //    
1830    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 1613  void PamelaDBOperations::HandleRunFragme Line 1833  void PamelaDBOperations::HandleRunFragme
1833      oss.str("");      oss.str("");
1834      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 "
1835          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1836          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1837            << " ID != " << glrun->ID
1838          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1839      //      //
1840      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 1623  void PamelaDBOperations::HandleRunFragme Line 1844  void PamelaDBOperations::HandleRunFragme
1844      //      //
1845      row = result->Next();      row = result->Next();
1846      //      //
1847        if ( !row && NoFrag() ){
1848          //
1849          oss.str("");
1850          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1851              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1852              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1853              << " ID != " << glrun->ID
1854              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1855          //
1856          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1857          result = conn->Query(oss.str().c_str());
1858          //
1859          if ( !result ) throw -4;
1860          //
1861          foundinrun = true;
1862          //
1863          row = result->Next();
1864          //
1865        };
1866        //
1867      if ( !row ){      if ( !row ){
1868        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1869        found = false;        found = false;
# Line 1697  void PamelaDBOperations::HandleRunFragme Line 1938  void PamelaDBOperations::HandleRunFragme
1938        //        //
1939        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 ");
1940        //        //
1941          if ( foundinrun ){
1942            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1943            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1944          };
1945          //
1946        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1947        //        //
1948        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1949        //        //
1950        oss.str("");        oss.str("");
1951        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1728  void PamelaDBOperations::HandleRunFragme Line 1974  void PamelaDBOperations::HandleRunFragme
1974        };        };
1975        if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);        if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1976        //        //
       glrun1->SetID(0);  
1977        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1978        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1979        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1737  void PamelaDBOperations::HandleRunFragme Line 1982  void PamelaDBOperations::HandleRunFragme
1982        //        //
1983        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1984        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1985          //
1986        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1987        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1988        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1756  void PamelaDBOperations::HandleRunFragme Line 2002  void PamelaDBOperations::HandleRunFragme
2002        //        //
2003        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2004          //          //
2005            //      glrun->SetID(this->AssignRunID());
2006            glrun->SetID_RUN_FRAG(glrun1->GetID());
2007          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2008          //          //
2009          // 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));  
         };  
         //  
         glrun1->SetID_RUN_FRAG(idrun0);  
2010          //          //
2011            glrun1->SetID_RUN_FRAG(glrun->GetID());
2012          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2013          //          //
         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;  
         //  
2014        };        };
       //  
       delete glrun1;  
       //  
2015        // delete old entry in fragment table        // delete old entry in fragment table
2016        //        //
2017        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2018          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2019        //        //
2020        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2021        //        //
       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;  
2022        //        //
2023        return;        return;
2024        //        //
# Line 1825  void PamelaDBOperations::HandleRunFragme Line 2027  void PamelaDBOperations::HandleRunFragme
2027    };    };
2028    //    //
2029    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
2030                                             // 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
2031      //      //
2032      oss.str("");      oss.str("");
2033      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 "
2034          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2035          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2036            << " ID != " << glrun->ID
2037          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2038      //      //
2039      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 1840  void PamelaDBOperations::HandleRunFragme Line 2043  void PamelaDBOperations::HandleRunFragme
2043      //      //
2044      row = result->Next();      row = result->Next();
2045      //      //
2046        if ( !row && NoFrag() ){
2047          //
2048          oss.str("");
2049          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2050              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2051              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2052              << " ID != " << glrun->ID
2053              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2054          //
2055          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2056          result = conn->Query(oss.str().c_str());
2057          //
2058          if ( !result ) throw -4;
2059          //
2060          foundinrun = true;
2061          row = result->Next();
2062          //
2063        };
2064        //
2065      if ( !row ){      if ( !row ){
2066        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2067        found = false;        found = false;
# Line 1913  void PamelaDBOperations::HandleRunFragme Line 2135  void PamelaDBOperations::HandleRunFragme
2135        //        //
2136        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 ");
2137        //        //
2138          if ( foundinrun ){
2139            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2140            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2141          };
2142          //
2143        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2144        //        //
2145        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2146        //        //
2147        oss.str("");        oss.str("");
2148        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1952  void PamelaDBOperations::HandleRunFragme Line 2179  void PamelaDBOperations::HandleRunFragme
2179        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2180        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2181        //        //
       glrun1->SetID(0);  
2182        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2183        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2184        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1972  void PamelaDBOperations::HandleRunFragme Line 2198  void PamelaDBOperations::HandleRunFragme
2198        //        //
2199        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2200          //          //
2201          glrun->Fill_GL_RUN(conn);          //      glrun->SetID(this->AssignRunID());
         //  
         // get 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();  
2202          //          //
2203          UInt_t idrun0 = 0;          glrun->SetID_RUN_FRAG(glrun1->GetID());
2204          if ( !row ){          glrun->Fill_GL_RUN(conn);
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2205          //          //
2206          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2207          //          //
2208            glrun1->SetID_RUN_FRAG(glrun->GetID());
2209          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2210          //          //
         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;  
         //  
2211        };        };
2212        //        //
2213        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 << ";";  
2214        //        //
2215        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");
2216        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2217        //        //
2218          delete glrun1;
2219        //        //
2220        return;        return;
2221        //        //
# Line 2070  void PamelaDBOperations::HandleRunFragme Line 2252  void PamelaDBOperations::HandleRunFragme
2252      //      //
2253      row = result->Next();      row = result->Next();
2254      //      //
2255      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->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2256        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");
2257        } else {
2258          if ( NoFrag() ){
2259            glrun->SetID_RUN_FRAG(glrun->GetID());
2260            glrun->Fill_GL_RUN(conn);      
2261            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2262          };    
2263      };      };
2264    };    };
2265    //    //
# Line 2134  void PamelaDBOperations::HandleMissingHo Line 2285  void PamelaDBOperations::HandleMissingHo
2285      //      //
2286      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2287      //      //
2288      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2289          glrun->SetID(this->AssignRunID());
2290          glrun->SetID_RUN_FRAG(0);
2291          glrun->Fill_GL_RUN(conn);    
2292        };
2293      //      //
2294    };    };
2295    //    //
# Line 2301  Bool_t PamelaDBOperations::IsRunConsiste Line 2456  Bool_t PamelaDBOperations::IsRunConsiste
2456            //                  //      
2457            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2458            //            //
2459            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2460                glrun->SetID(this->AssignRunID());
2461                glrun->SetID_RUN_FRAG(0);
2462                glrun->Fill_GL_RUN(conn);      
2463              };
2464            //            //
2465            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2466            //            //
# Line 2377  void PamelaDBOperations::HandleSuspiciou Line 2536  void PamelaDBOperations::HandleSuspiciou
2536      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2537      //      //
2538      this->FillClass();      this->FillClass();
2539      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2540          glrun->SetID(this->AssignRunID());
2541          glrun->SetID_RUN_FRAG(0);
2542          glrun->Fill_GL_RUN(conn);    
2543        };
2544      //      //
2545    } else {    } else {
2546      //      //
# Line 2405  void PamelaDBOperations::HandleSuspiciou Line 2568  void PamelaDBOperations::HandleSuspiciou
2568        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2569        //        //
2570        this->FillClass();        this->FillClass();
2571        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2572            glrun->SetID(this->AssignRunID());
2573            glrun->SetID_RUN_FRAG(0);
2574            glrun->Fill_GL_RUN(conn);        
2575          };
2576        //        //
2577      } else {      } else {
2578        //        //
# Line 2503  void PamelaDBOperations::HandleSuspiciou Line 2670  void PamelaDBOperations::HandleSuspiciou
2670            //                  //      
2671            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2672            //            //
2673            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2674                glrun->SetID(this->AssignRunID());
2675                glrun->SetID_RUN_FRAG(0);
2676                glrun->Fill_GL_RUN(conn);      
2677              };
2678            //            //
2679            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2680              //              //
# Line 2538  void PamelaDBOperations::HandleSuspiciou Line 2709  void PamelaDBOperations::HandleSuspiciou
2709              //                  //    
2710              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2711              //              //
2712              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2713                  glrun->SetID(this->AssignRunID());
2714                  glrun->SetID_RUN_FRAG(0);
2715                  glrun->Fill_GL_RUN(conn);      
2716                };
2717            };            };
2718            //            //
2719            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2645  Int_t PamelaDBOperations::insertCALO_CAL Line 2820  Int_t PamelaDBOperations::insertCALO_CAL
2820                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2821                //                //
2822                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);
2823                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2824                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
2825                  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
2826                };                };
# Line 2790  void PamelaDBOperations::HandleTRK_CALIB Line 2965  void PamelaDBOperations::HandleTRK_CALIB
2965        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
2966        //        //
2967        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");
2968        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
2969        //        //
2970        oss.str("");        oss.str("");
2971        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 2919  Int_t PamelaDBOperations::insertTRK_CALI Line 3094  Int_t PamelaDBOperations::insertTRK_CALI
3094      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3095      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3096      //      //
3097      valid = 1;  //     valid = 1;
3098      //  //     //
3099      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
3100      //      //
3101      //      //
3102      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) ){
3103        //        //
3104        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);
3105          //      
3106          valid = ValidateTrkCalib( caltrk1, eh1 );
3107          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3108        //        //
3109        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3110        //        //
# Line 2943  Int_t PamelaDBOperations::insertTRK_CALI Line 3121  Int_t PamelaDBOperations::insertTRK_CALI
3121            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3122            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3123            //            //
3124            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
3125            //                  //      
3126          } else {          } else {
3127            //            //
# Line 2974  Int_t PamelaDBOperations::insertTRK_CALI Line 3152  Int_t PamelaDBOperations::insertTRK_CALI
3152          //          //
3153          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);
3154          //          //
3155            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3156            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3157            valid = valid & valid2;
3158            //
3159          // Handle good calib          // Handle good calib
3160          //          //
3161          this->HandleTRK_CALIB(true,true);          this->HandleTRK_CALIB(true,true);
# Line 3146  Int_t PamelaDBOperations::insertS4_CALIB Line 3328  Int_t PamelaDBOperations::insertS4_CALIB
3328            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3329            //            //
3330            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");
3331            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3332              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);
3333              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
3334            };            };
# Line 3219  Int_t PamelaDBOperations::insertS4_CALIB Line 3401  Int_t PamelaDBOperations::insertS4_CALIB
3401   * 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
3402   */   */
3403  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3404      return(this->CleanGL_RUN_FRAGMENTS(""));
3405    };
3406    
3407    /**
3408     * Scan the fragment table and move old fragments to the GL_RUN table
3409     */
3410    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3411    //    //
3412    TSQLResult *result = 0;    TSQLResult *result = 0;
3413    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3226  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3415  Int_t PamelaDBOperations::CleanGL_RUN_FR
3415    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3416    //    //
3417    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3418    //    //
3419    stringstream oss;    stringstream oss;
3420    oss.str("");    oss.str("");
3421    //    //
3422    //    if ( !strcmp(fcleanfile.Data(),"") ){
3423    // check if there are entries older than "olderthan" seconds from now      //
3424    //      // check if there are entries older than "olderthan" seconds from now
3425    oss.str("");      //
3426    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3427        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3428    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3429    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3430    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());
3431        result = conn->Query(oss.str().c_str());
3432        //
3433      } else {
3434        oss.str("");
3435        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3436        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3437        result = conn->Query(oss.str().c_str());
3438        //    
3439        if ( result ){
3440          //
3441          row = result->Next();
3442          //
3443          if ( row ){
3444            oss.str("");
3445            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3446                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3447            //
3448            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3449            result = conn->Query(oss.str().c_str());
3450            //    
3451          };
3452        } else {
3453          return(2);
3454        };
3455      };
3456    //    //
3457    if ( result ){    if ( result ){
3458      //      //
# Line 3281  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3493  Int_t PamelaDBOperations::CleanGL_RUN_FR
3493          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
3494          if ( IsDebug() ) printf(" -> fill the DB \n");                if ( IsDebug() ) printf(" -> fill the DB \n");      
3495          //          //
3496          glrun->SetID(0);          //      glrun->SetID(this->AssignRunID()); we use the old run number!
3497            glrun->SetID_RUN_FRAG(glrun->GetID());
3498          glrun->Fill_GL_RUN(conn);            glrun->Fill_GL_RUN(conn);  
3499          //          //
3500          oss.str("");  //      oss.str("");
3501          oss << " SELECT ID FROM GL_RUN WHERE "  //      oss << " SELECT ID FROM GL_RUN WHERE "
3502              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "  //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3503              << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "  //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3504              << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "  //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3505              << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "  //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3506              << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";  //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3507          //  //      //
3508          if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());  //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3509          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3510          //  //      //
3511          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3512          //  //      //
3513          row2 = result2->Next();  //      row2 = result2->Next();
3514          //  //      //
3515          if ( !row2 ) throw -25;  //      if ( !row2 ) throw -25;
3516          //  //      //
3517          oss.str("");  //      oss.str("");
3518          oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3519          if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());  //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3520          result2 = conn->Query(oss.str().c_str());  //      result2 = conn->Query(oss.str().c_str());
3521          //  //      //
3522          if ( !result2 ) throw -4;  //      if ( !result2 ) throw -4;
3523          //          //
3524          moved++;          moved++;
3525          //          //
# Line 3316  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3529  Int_t PamelaDBOperations::CleanGL_RUN_FR
3529        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));      
3530        //        //
3531        //        //
3532        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3533        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3534        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);
3535        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());
3536        //  //       result2 = conn->Query(oss.str().c_str());
3537        if ( !result2 ) throw -4;  //      //
3538        //  //      if ( !result2 ) throw -4;
3539    //      //
3540        row = result->Next();        row = result->Next();
3541      };      };
3542    };    };
# Line 3334  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3548  Int_t PamelaDBOperations::CleanGL_RUN_FR
3548   * 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..
3549   */   */
3550  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3551      return(this->ValidateRuns(""));
3552    };
3553    
3554    /**
3555     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3556     */
3557    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3558    //    //
3559    TSQLResult *result = 0;    TSQLResult *result = 0;
3560    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3561    //    //
3562      UInt_t calibtime = 50;
3563      //
3564    stringstream oss;    stringstream oss;
3565    oss.str("");    oss.str("");
3566    //    //
# Line 3346  Int_t PamelaDBOperations::ValidateRuns() Line 3569  Int_t PamelaDBOperations::ValidateRuns()
3569    // =======================================================    // =======================================================
3570    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3571    UInt_t t_start = 0;    UInt_t t_start = 0;
3572    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3573    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3574    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3575    oss.str("");      // --------------------------------------------------------------
3576    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3577            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3578    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3579    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3580    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3581    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3582            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3583            return(1);        printf(" No runs to validate \n");
3584    }else{        return(1);
3585          row = result->Next();      }else{
3586          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3587    };          t_start = (UInt_t)atoll(row->GetField(4));
3588    // --------------------------------------------------------------      };  
3589    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3590    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3591    oss.str("");      // --------------------------------------------------------------
3592    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3593        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3594    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3595    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3596    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3597    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3598            row = result->Next();      if ( result->GetRowCount() ){
3599            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3600          t_stop = (UInt_t)atoll(row->GetField(4));
3601        };
3602        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3603        // --------------------------------------------------------------
3604        // now retrieves runs to be validated
3605        // --------------------------------------------------------------
3606        oss.str("");
3607        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3608        oss << " AND RUNHEADER_TIME >="<< t_stop;
3609        oss << " ORDER BY RUNHEADER_TIME DESC;";
3610        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3611        result = conn->Query(oss.str().c_str());
3612      } else {
3613        //
3614        stringstream myquery;
3615        UInt_t myid = 0;
3616        myquery.str("");
3617        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3618        //
3619        result = conn->Query(myquery.str().c_str());
3620        //
3621        row = result->Next();      
3622        if( !row ){
3623          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3624            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3625            return(2);
3626          };
3627          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3628          return(0);
3629        };
3630        myid=(UInt_t)atoll(row->GetField(0));
3631        //
3632        myquery.str("");
3633        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3634        //
3635        result = conn->Query(myquery.str().c_str());
3636        //
3637        row = result->Next();      
3638        if( !row->GetField(0) || !row->GetField(1)){
3639          //
3640          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3641          //
3642          return(0);
3643          //
3644        } else {  
3645          //
3646          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3647          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3648          UInt_t caltime = 0;
3649          //
3650          myquery.str("");
3651          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3652          myquery << " order by FROM_TIME asc limit 1;";
3653          //
3654          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3655          //
3656          //
3657          result = conn->Query(myquery.str().c_str());
3658          //
3659          row = result->Next();      
3660          if( !row ){
3661            caltime = runhtime;
3662          } else {
3663            caltime = (UInt_t)atoll(row->GetField(0));
3664          };
3665          //
3666          myquery.str("");
3667          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3668          myquery << caltime << " order by RUNHEADER_TIME DESC";
3669          //
3670          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3671          //
3672          result = conn->Query(myquery.str().c_str());
3673          //
3674        };
3675    };    };
3676    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());  
3677    if ( !result ) throw -4;    if ( !result ) throw -4;
3678    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3679  //  printf("------------------------------------------------------------------------------- \n");    //
     
3680    Int_t nrow = 0;    Int_t nrow = 0;
3681    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3682    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3403  Int_t PamelaDBOperations::ValidateRuns() Line 3690  Int_t PamelaDBOperations::ValidateRuns()
3690    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3691    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3692    // - loop over runs, back in time,    // - loop over runs, back in time,
3693    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3694    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3695    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3696    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3443  Int_t PamelaDBOperations::ValidateRuns() Line 3730  Int_t PamelaDBOperations::ValidateRuns()
3730                                                        
3731                            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
3732                                                        
3733                            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
3734                                                        
3735                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3736                                    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 3641  Int_t PamelaDBOperations::populateTLE()/ Line 3928  Int_t PamelaDBOperations::populateTLE()/
3928    
3929    
3930  // Insert tle in the table GL_TLE using the connection conn.  // Insert tle in the table GL_TLE using the connection conn.
3931  int PamelaDBOperations::insertTle(cTle *tle)  Int_t PamelaDBOperations::insertTle(cTle *tle)
3932  {  {
3933    stringstream oss;    stringstream oss;
3934    TSQLResult *result = 0;    TSQLResult *result = 0;
# Line 3724  string getTleDatetime(cTle *tle) Line 4011  string getTleDatetime(cTle *tle)
4011    
4012    return date.str();    return date.str();
4013  }  }
4014    
4015    /**
4016     * Remove a file from the DB, delete on cascade all entries related to that file
4017     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4018     * calibration
4019     **/
4020    Int_t PamelaDBOperations::removeFile(TString remfile){
4021      //
4022      // Determine ID_ROOT_L0 and ID_RAW
4023      //
4024      TSQLResult *pResult;
4025      TSQLRow *Row;
4026      stringstream myquery;
4027      //  
4028      myquery.str("");
4029      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4030      //
4031      pResult = conn->Query(myquery.str().c_str());
4032      //
4033      Row = pResult->Next();      
4034      if( !Row ){
4035        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4036          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4037          return(1);
4038        };
4039        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4040        return(0);
4041      };
4042      //
4043      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4044      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4045      //
4046      this->ValidationOFF();
4047      //
4048      this->RemoveCALIBS();
4049      //
4050      this->RemoveRUNS();
4051      //
4052      this->RemoveFILES();
4053      //
4054      this->SetID_ROOT(0);
4055      this->SetID_RAW(0);
4056      //
4057      return(0);
4058    };
4059    
4060    /**
4061     *
4062     * Set validation bit to zero for runs following the removing file till
4063     * 1) a run with TRK_CALIB_USED=140
4064     * 2) a run with VALIDATION = 0
4065     * 3) the next calibration
4066     *
4067     **/
4068    void PamelaDBOperations::ValidationOFF(){
4069      TSQLResult *pResult;
4070      TSQLRow *Row;
4071      stringstream myquery;
4072      Int_t unv = 0;
4073      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4074      myquery.str("");
4075      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4076      //
4077      pResult = conn->Query(myquery.str().c_str());
4078      //
4079      Row = pResult->Next();      
4080      if( !Row->GetField(0) ){
4081        //
4082        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4083        //
4084      } else {  
4085        //
4086        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4087        UInt_t caltime = 0;
4088        //
4089        myquery.str("");
4090        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4091        myquery << " order by FROM_TIME asc limit 1;";
4092        //
4093        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4094        //
4095        //
4096        delete pResult;
4097        pResult = conn->Query(myquery.str().c_str());
4098        //
4099        Row = pResult->Next();      
4100        if( !Row ){
4101          caltime = runhtime;
4102        } else {
4103          caltime = (UInt_t)atoll(Row->GetField(0));
4104        };
4105        //
4106        myquery.str("");
4107        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4108        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4109        //
4110        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4111        //
4112        pResult = conn->Query(myquery.str().c_str());
4113        //
4114        Row = pResult->Next();      
4115        if( !Row ){
4116          //
4117          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4118          //
4119        } else {
4120          myquery.str("");
4121          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4122          myquery << " RUNHEADER_TIME>=" <<runhtime;
4123          myquery << " order by RUNHEADER_TIME asc;";
4124          //
4125          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4126          //
4127          pResult = conn->Query(myquery.str().c_str());
4128          //
4129          Row = pResult->Next();  
4130          while ( Row ){
4131            //      
4132            unv++;
4133            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4134            Row = pResult->Next();  
4135            //
4136          };
4137        };
4138      };
4139      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4140    };
4141    
4142    /**
4143     *
4144     * Rearrange GL_RUN table and remove runs
4145     *
4146     **/
4147    void PamelaDBOperations::RemoveRUNS(){
4148      TSQLResult *pResult;
4149      TSQLRow *Row;
4150      stringstream myquery;
4151      UInt_t drun = 0;
4152      GL_RUN *delrun = new GL_RUN();
4153      //
4154      myquery.str("");
4155      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4156      //
4157      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4158      //
4159      pResult = conn->Query(myquery.str().c_str());
4160      //
4161      Row = pResult->Next();    
4162      //
4163      //
4164      if ( !Row ){
4165        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4166      } else {
4167        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4168        while ( Row ){
4169          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4170          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4171          drun++;
4172          Row = pResult->Next();    
4173        };
4174      };
4175      //
4176      //
4177      myquery.str("");
4178      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4179      //
4180      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4181      //
4182      pResult = conn->Query(myquery.str().c_str());
4183      //
4184      Row = pResult->Next();    
4185      //
4186      if ( !Row ){
4187        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4188      } else {
4189        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4190        while ( Row ){
4191          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4192          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4193          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4194          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4195          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4196            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4197            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4198          };
4199          drun++;
4200          Row = pResult->Next();    
4201        };
4202      };
4203      //
4204      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4205      //
4206      //
4207      //
4208      drun = 0;
4209      //
4210      myquery.str("");
4211      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4212      //
4213      pResult = conn->Query(myquery.str().c_str());
4214      //
4215      Row = pResult->Next();    
4216      //
4217      if ( !Row ){
4218        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4219      } else {
4220        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4221        while ( Row ){
4222          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4223          myquery.str("");
4224          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4225          conn->Query(myquery.str().c_str());
4226          drun++;
4227          Row = pResult->Next();    
4228        };
4229      };
4230      //  
4231      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4232      //
4233      //
4234      //
4235      drun = 0;
4236      //
4237      myquery.str("");
4238      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4239      //
4240      pResult = conn->Query(myquery.str().c_str());
4241      //
4242      Row = pResult->Next();    
4243      //
4244      if ( !Row ){
4245        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4246      } else {
4247        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4248        while ( Row ){
4249          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4250          myquery.str("");
4251          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4252          conn->Query(myquery.str().c_str());
4253          drun++;
4254          Row = pResult->Next();    
4255        };
4256      };
4257      //  
4258      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4259      //
4260      //
4261      //
4262      delete delrun;
4263      //
4264    };
4265    
4266    
4267    /**
4268     *
4269     * Rearrange calibration tables
4270     *
4271     **/
4272    void PamelaDBOperations::RemoveFILES(){
4273      stringstream myquery;
4274      //
4275      myquery.str("");
4276      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4277      //
4278      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4279      //
4280      conn->Query(myquery.str().c_str());
4281      //
4282    };
4283    
4284    /**
4285     *
4286     * Rearrange calibration tables
4287     *
4288     **/
4289    void PamelaDBOperations::RemoveCALIBS(){
4290      TSQLResult *pResult;
4291      TSQLRow *Row;
4292      stringstream myquery;
4293      //  
4294      //
4295      // Calorimeter
4296      //
4297      for (Int_t section = 0; section < 4; section++){
4298        myquery.str("");
4299        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4300        myquery << " SECTION=" << section << ";";
4301        //
4302        pResult = conn->Query(myquery.str().c_str());
4303        //
4304        Row = pResult->Next();      
4305        if( !Row->GetField(0) || !Row->GetField(1) ){
4306          //
4307          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4308          //
4309        } else {
4310          //
4311          myquery.str("");
4312          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4313          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4314          myquery << " SECTION=" << section << ";";
4315          //
4316          pResult = conn->Query(myquery.str().c_str());
4317          //
4318          if( !pResult ){
4319            //
4320            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4321            //
4322            throw -4;
4323            //
4324          };
4325          //
4326        };
4327      };
4328      myquery.str("");
4329      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4330      //
4331      pResult = conn->Query(myquery.str().c_str());
4332      //
4333      if( !pResult ){
4334        //
4335        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4336        //
4337        throw -4;
4338        //
4339      };
4340      //
4341      // Tracker
4342      //
4343      myquery.str("");
4344      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4345      //
4346      pResult = conn->Query(myquery.str().c_str());
4347      //
4348      Row = pResult->Next();      
4349      if( !Row->GetField(0) || !Row->GetField(1) ){
4350        //
4351        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4352        //
4353      } else {
4354        //
4355        myquery.str("");
4356        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4357        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4358        //
4359        pResult = conn->Query(myquery.str().c_str());
4360        //
4361        if( !pResult ){
4362          //
4363          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4364          //
4365          throw -4;
4366          //
4367        };
4368        //
4369        myquery.str("");
4370        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4371        //
4372        pResult = conn->Query(myquery.str().c_str());
4373        //
4374        if( !pResult ){
4375          //
4376          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4377          //
4378          throw -4;
4379          //
4380        };
4381      };
4382      //
4383      //
4384      // S4
4385      //
4386      myquery.str("");
4387      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4388      //
4389      pResult = conn->Query(myquery.str().c_str());
4390      //
4391      Row = pResult->Next();      
4392      if( !Row->GetField(0) || !Row->GetField(1) ){
4393        //
4394        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4395        //
4396      } else {
4397        //
4398        myquery.str("");
4399        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4400        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4401        //
4402        pResult = conn->Query(myquery.str().c_str());
4403        //
4404        if( !pResult ){
4405          //
4406          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4407          //
4408          throw -4;
4409          //
4410        };
4411        //
4412        myquery.str("");
4413        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4414        //
4415        pResult = conn->Query(myquery.str().c_str());
4416        //
4417        if( !pResult ){
4418          //
4419          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4420          //
4421          throw -4;
4422          //
4423        };
4424        //
4425      };
4426    };
4427    
4428    /**
4429     *
4430     * Rearrange calibration tables
4431     *
4432     **/
4433    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4434    
4435        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4436        UInt_t timeaftercalib=120000; //2000;
4437    //  ----------
4438    //  Check CRCs
4439    //  ----------
4440        for(Int_t ipkt=0; ipkt<6; ipkt++){
4441            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4442            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4443        }
4444    //  -----------------------
4445    //  Check missing packets:
4446    //  -----------------------
4447    //    Readout order:
4448    //    ------------------
4449    //    DSP   packet board
4450    //    ------------------
4451    //    12    0      1
4452    //    10    1      1
4453    //     8    2      1
4454    //     4    3      1
4455    //     6    4      1
4456    //     2    5      1
4457    //    ------------------
4458    //    11    0      2
4459    //     9    1      2
4460    //     7    2      2
4461    //     3    3      2
4462    //     5    4      2
4463    //     1    5      2
4464    //    ------------------
4465    //  -------------------------------------------------
4466    //  Check if it is first or second calibration packet
4467    //  -------------------------------------------------
4468        UInt_t build=0;
4469        TString classname = caltrk->GetName();
4470        UInt_t base=0;
4471        UInt_t mask=0;
4472        if(classname.Contains("CalibTrk1Event")){
4473            base=12;
4474            mask=0x03F000;
4475        }
4476        if(classname.Contains("CalibTrk2Event")){
4477            base=18;
4478            mask=0xFC0000;
4479        }
4480    //  -------------------------------------------------
4481    //  Count number of packets and set build variable
4482    //  -------------------------------------------------
4483        Int_t  npkts=0;
4484        for(Int_t ipkt=0; ipkt<6; ipkt++){
4485            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4486                npkts++;
4487                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4488            }
4489        }
4490    //    if( npkts==6 )return 1; // :-)
4491    
4492    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4493    
4494    //  -----------------------------------------------
4495    //  If missing packets: check the acq configuration
4496    //  (some DSPs might be excluded from acquisition)
4497    //  -----------------------------------------------
4498    
4499    //  -----------------------------------------------
4500    //  retrieve the first run header after calib
4501    //  -----------------------------------------------
4502         PacketType *pctp;
4503         EventCounter *cod;
4504         cod = eh->GetCounter();
4505         Int_t irun = cod->Get(pctp->RunHeader);
4506         TTree *rh=(TTree*)file->Get("RunHeader");
4507         if ( !rh || rh->IsZombie() ) throw -17;
4508         if( rh->GetEntries() == irun ){
4509              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4510             return 0; // :-(
4511         }
4512    
4513         RunHeaderEvent *run  = 0;
4514         EventHeader    *hrun = 0;
4515         rh->SetBranchAddress("RunHeader", &run);
4516         rh->SetBranchAddress("Header", &hrun);
4517         rh->GetEntry(irun);
4518    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4519    
4520         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4521              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4522             return 0; // :-(
4523         }
4524        
4525         if( !run->RM_ACQ_AFTER_CALIB ){
4526              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4527             return 0; // :-(
4528         }
4529    
4530         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4531         if( dtime > timeaftercalib ){
4532             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4533             return 0; // :-(
4534         }
4535        
4536    
4537    
4538         if( (run->ACQ_BUILD_INFO & mask) != build ){
4539             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4540             return 0; // :-(
4541         }
4542         return 1; // :-)
4543    
4544    }
4545    
4546    /**
4547     *
4548     * Check the DB (only for overlapping runs at the moment)
4549     *
4550     **/
4551    UInt_t PamelaDBOperations::Check(){
4552      //
4553      UInt_t test = 0;
4554      //
4555      UInt_t thisrht = 0;
4556      UInt_t thisrtt = 0;
4557      UInt_t thisid = 0;
4558      UInt_t prevrht = 0;
4559      UInt_t prevrtt = 0;
4560      UInt_t previd = 0;
4561      //
4562      UInt_t prevl0id = 0;
4563      UInt_t thisl0id = 0;
4564      //
4565      stringstream oss;
4566      TSQLResult *result = 0;
4567      TSQLRow    *row    = 0;
4568      TSQLResult *result2 = 0;
4569      TSQLRow    *row2    = 0;
4570      oss.str("");
4571      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4572      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4573      result = conn->Query(oss.str().c_str());
4574      //
4575      if ( !result ) throw -4;;
4576      //
4577      row = result->Next();
4578      //
4579      while ( row ){
4580        thisid = (UInt_t)atoll(row->GetField(0));
4581        thisl0id = (UInt_t)atoll(row->GetField(1));
4582        thisrht = (UInt_t)atoll(row->GetField(2));
4583        thisrtt = (UInt_t)atoll(row->GetField(3));
4584        //
4585        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4586        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4587        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4588          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4589          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4590          TString prevf = "";
4591          TString thisf = "";
4592          oss.str("");
4593          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4594          result2 = conn->Query(oss.str().c_str());
4595          if ( !result2 ) throw -4;;
4596          row2 = result2->Next();
4597          prevf = (TString)row2->GetField(0);
4598          oss.str("");
4599          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4600          result2 = conn->Query(oss.str().c_str());
4601          if ( !result2 ) throw -4;;
4602          row2 = result2->Next();
4603          thisf = (TString)row2->GetField(0);
4604          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4605          test = 1;
4606        };
4607        //
4608        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4609          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4610          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4611          TString prevf = "";
4612          TString thisf = "";
4613          oss.str("");
4614          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4615          result2 = conn->Query(oss.str().c_str());
4616          if ( !result2 ) throw -4;;
4617          row2 = result2->Next();
4618          prevf = (TString)row2->GetField(0);
4619          oss.str("");
4620          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4621          result2 = conn->Query(oss.str().c_str());
4622          if ( !result2 ) throw -4;;
4623          row2 = result2->Next();
4624          thisf = (TString)row2->GetField(0);
4625          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4626          test = 1;
4627        };
4628        //
4629        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4630          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4631          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4632          TString prevf = "";
4633          TString thisf = "";
4634          oss.str("");
4635          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4636          result2 = conn->Query(oss.str().c_str());
4637          if ( !result2 ) throw -4;;
4638          row2 = result2->Next();
4639          prevf = (TString)row2->GetField(0);
4640          oss.str("");
4641          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4642          result2 = conn->Query(oss.str().c_str());
4643          if ( !result2 ) throw -4;;
4644          row2 = result2->Next();
4645          thisf = (TString)row2->GetField(0);
4646          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4647          test = 1;
4648        };
4649    
4650        //
4651        prevrht = thisrht;
4652        prevrtt = thisrtt;
4653        previd = thisid;
4654        prevl0id = thisl0id;
4655        row = result->Next();
4656      };
4657      //
4658      return(test);
4659      //
4660    };

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

  ViewVC Help
Powered by ViewVC 1.1.23