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

Diff of /YodaProfiler/src/PamelaDBOperations.cpp

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

revision 1.14 by mocchiut, Fri Oct 20 11:39:37 2006 UTC revision 1.50 by mocchiut, Wed Sep 5 15:34:49 2007 UTC
# Line 14  Line 14 
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
17  #include <TDatime.h>  #include <TTimeStamp.h>
18  #include <TF1.h>  #include <TF1.h>
19  //  //
20  #include <EventHeader.h>  #include <EventHeader.h>
# Line 56  string getTleDatetime(cTle*); Line 56  string getTleDatetime(cTle*);
56   * @param debug        debug flag.   * @param debug        debug flag.
57   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
58   */   */
59  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp, Bool_t gpamela){
60    //    //
61      STATIC=false;
62      if ( staticp ) STATIC=true;
63    //    //
64    SetConnection(host,user,password);    SetConnection(host,user,password);
65    //    //
# Line 66  PamelaDBOperations::PamelaDBOperations(T Line 68  PamelaDBOperations::PamelaDBOperations(T
68    glrun = new GL_RUN();    glrun = new GL_RUN();
69    //    //
70    if ( !boot ) SetNOBOOT(false);    if ( !boot ) SetNOBOOT(false);
71    SetBOOTnumber(boot);    SetTsync(tsync,gpamela);
72    SetTsync(tsync);    SetBOOTnumber(boot,gpamela);
73    SetObt0(obt0);    SetObt0(obt0);
74    //    //
75    SetTLEPath(tlefilename);    SetTLEPath(tlefilename);
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 81  PamelaDBOperations::PamelaDBOperations(T
81    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
82    if( INSERT_ROOT ){    if( INSERT_ROOT ){
83      this->SetRootName(filerootname);      this->SetRootName(filerootname);
84      this->SetOrbitNo();      this->SetOrbitNo(dwinput);
85      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
86      } else {
87        this->SetRootName("");
88    };    };
89    //    //
90    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 110  void PamelaDBOperations::Close(){ Line 114  void PamelaDBOperations::Close(){
114  //  //
115  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116    clean_time = new TDatime();    clean_time = new TDatime();
117      //
118    if(olderthan >= 0){    if(olderthan >= 0){
119      VALIDATE = true;      VALIDATE = true;
120      UInt_t timelim = 0;      UInt_t timelim = 0;
121      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
122      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
123    };    };
124  };  };
# Line 154  void PamelaDBOperations::SetDebugFlag(Bo Line 159  void PamelaDBOperations::SetDebugFlag(Bo
159  };  };
160    
161  /**  /**
162     * Set the autoboot flag
163     *
164     */
165    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166      AUTOBOOT = dbg;
167    };
168    
169    /**
170     * Set the nofrag flag
171     *
172     */
173    void PamelaDBOperations::SetNoFrag(Bool_t nf){
174      NOFRAG = nf;
175    };
176    
177    /**
178   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
179   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
180   */   */
181  void PamelaDBOperations::SetBOOTnumber(UInt_t boot){  void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
182      this->SetBOOTnumber(boot,false);
183    };
184    
185    /**
186     * Store the BOOT number of the RAW file.
187     * @param boot    BOOT number of the RAW file
188     */
189    void PamelaDBOperations::SetBOOTnumber(UInt_t boot, Bool_t gpamela){
190    BOOTNO=boot;    BOOTNO=boot;
191      if ( gpamela ){
192        stringstream oss;
193        TSQLResult *result = 0;
194        TSQLRow    *row    = 0;    
195        if ( !boot ){
196          //
197          BOOTNO = 1;
198          //
199          // look in the DB for the last timesync and the last run
200          //
201          oss.str("");
202          oss << "SELECT BOOT_NUMBER FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
203          result = conn->Query(oss.str().c_str());
204          if ( result ){
205            row = result->Next();
206            if ( row ){
207              BOOTNO = (UInt_t)atoll(row->GetField(0)) + 1;
208            };
209          };
210        };
211      };
212  };  };
213    
214  /**  /**
# Line 166  void PamelaDBOperations::SetBOOTnumber(U Line 216  void PamelaDBOperations::SetBOOTnumber(U
216   * @param boot    time sync   * @param boot    time sync
217   */   */
218  void PamelaDBOperations::SetTsync(UInt_t ts){  void PamelaDBOperations::SetTsync(UInt_t ts){
219      this->SetTsync(ts,false);
220    };
221    
222    /**
223     * Store the time sync of the RAW file.
224     * @param boot    time sync
225     */
226    void PamelaDBOperations::SetTsync(UInt_t ts, Bool_t gpamela){
227      //
228      // if not gpamela or given tsync file set ts
229      //
230    tsync=ts;    tsync=ts;
231      if ( gpamela ){
232        stringstream oss;
233        TSQLResult *result = 0;
234        TSQLRow    *row    = 0;    
235        TSQLResult *result2 = 0;
236        TSQLRow    *row2    = 0;    
237        if ( !ts ){
238          //
239          tsync = 1;
240          //
241          // look in the DB for the last timesync and the last run
242          //
243          oss.str("");
244          oss << "SELECT TIMESYNC FROM GL_TIMESYNC order by TIMESYNC desc limit 1;";
245          result = conn->Query(oss.str().c_str());
246          if ( result ){
247            row = result->Next();
248            if ( row ){
249              tsync = (UInt_t)atoll(row->GetField(0)) + 1;
250              oss.str("");
251              oss << "SELECT (RUNTRAILER_TIME-RUNHEADER_TIME) FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
252              result2 = conn->Query(oss.str().c_str());
253              if ( result2 ){
254                row2 = result2->Next();
255                if ( row2 ){
256                  tsync += (UInt_t)atoll(row2->GetField(0));
257                };
258              }
259            };
260          };
261        };
262      };
263  };  };
264    
265  /**  /**
# Line 196  void PamelaDBOperations::SetRootName(TSt Line 289  void PamelaDBOperations::SetRootName(TSt
289  /**  /**
290   * Store the downlink orbit number from filename.   * Store the downlink orbit number from filename.
291   */   */
292  void PamelaDBOperations::SetOrbitNo(){  void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
293    dworbit = 0;    dworbit = 0;
294      //
295      if ( dwinput ){
296        dworbit = dwinput;
297        if ( IsDebug() ) printf(" Downlink orbit given by hand: %i  \n",dworbit);
298        return;
299      };
300      //
301    TString name = this->GetRootFile();    TString name = this->GetRootFile();
302    Int_t nlength = name.Length();    Int_t nlength = name.Length();
303    if ( nlength < 5 ) return;    if ( nlength < 5 ) return;
# Line 235  void PamelaDBOperations::SetTLEPath(TStr Line 335  void PamelaDBOperations::SetTLEPath(TStr
335    tlefilename = str;    tlefilename = str;
336  };  };
337    
338    TString PamelaDBOperations::GetRawPath(){
339      if ( STATIC ){
340        return((TString)gSystem->DirName(filerawname.Data())+'/');
341      } else {
342        return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
343      };
344    };
345    
346    TString PamelaDBOperations::GetRootPath(){
347      if ( STATIC ){
348        return((TString)gSystem->DirName(filerootname.Data())+'/');
349      } else {
350        return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
351      };
352    };
353    
354  /**  /**
355   * Store the olderthan variable   * Store the olderthan variable
356   * @param olderthan   * @param olderthan
# Line 251  Bool_t PamelaDBOperations::SetID_RAW(){ Line 367  Bool_t PamelaDBOperations::SetID_RAW(){
367    TSQLResult *result = 0;    TSQLResult *result = 0;
368    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
369    oss.str("");    oss.str("");
370    oss << "SELECT ID FROM GL_RAW WHERE "    if ( STATIC ){
371        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID FROM GL_RAW WHERE "
372        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
373            << " NAME = '" << this->GetRawFile().Data() << "' ";  
374      } else {
375        oss << "SELECT ID FROM GL_RAW WHERE "
376            << " PATH = '$PAM_RAW' AND "
377            << " NAME = '" << this->GetRawFile().Data() << "' ";  
378      }
379    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
380    if ( result == NULL ) throw -4;    if ( result == NULL ) throw -4;
381    row = result->Next();    row = result->Next();
# Line 284  Int_t PamelaDBOperations::SetUpperLimits Line 406  Int_t PamelaDBOperations::SetUpperLimits
406    UInt_t nevent = 0;    UInt_t nevent = 0;
407    UInt_t pktlast = 0;    UInt_t pktlast = 0;
408    UInt_t obtlast = 0;    UInt_t obtlast = 0;
409    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
410    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
411    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
412    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
413      Long64_t upperobt2 = 0LL;
414    UInt_t zomp = 0;    UInt_t zomp = 0;
415    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
416    EventCounter *code=0;    EventCounter *code=0;
417    //    //
418    UInt_t deltapkt = 5000;    Long64_t deltapkt = 5000LL;
419    ULong64_t deltaobt = 50000;    Long64_t deltaobt = 50000LL;
420    //    //
421    //   pcksList packetsNames;    pcksList packetsNames;
422    //   pcksList::iterator Iter;    pcksList::iterator Iter;
423    //   getPacketsNames(packetsNames);    getPacketsNames(packetsNames);
424    //    //
425    pktfirst = 0;    pktfirst = 0;
426    obtfirst = 0;    obtfirst = 0;
427      ppktfirst = 0;
428      pobtfirst = 0;
429    //    //
430    TTree *T = 0;    TTree *T = 0;
431    T = (TTree*)file->Get("Physics");    T = (TTree*)file->Get("Physics");
# Line 314  Int_t PamelaDBOperations::SetUpperLimits Line 439  Int_t PamelaDBOperations::SetUpperLimits
439    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
440    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
441    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
442      ppktfirst = pktfirst;
443      pobtfirst = obtfirst;
444    //    //
445    //   code = eh->GetCounter();    code = eh->GetCounter();
446    //   UInt_t en = 0;    UInt_t en = 0;
447    //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){    for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
448    //     en = code->Get(GetPacketType(*Iter));      en = code->Get(GetPacketType(*Iter));
449    //     if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);      if ( !strcmp("CalibCalPed",*Iter) || !strcmp("CalibTrk1",*Iter) || !strcmp("CalibTrk2",*Iter) || !strcmp("CalibS4",*Iter) ){
450    //};          if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
451          //
452          TTree *TC = 0;
453          TC = (TTree*)file->Get("CalibCalPed");
454          if ( !TC || TC->IsZombie() ) throw -16;
455          EventHeader *ehc = 0;
456          PscuHeader *phc = 0;
457          TC->SetBranchAddress("Header", &ehc);
458          TC->GetEntry(0);
459          phc = ehc->GetPscuHeader();
460          pktfirst = phc->GetCounter();
461          obtfirst = phc->GetOrbitalTime();  
462          //
463        };
464      };  
465    //    //
466    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
467    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
# Line 331  Int_t PamelaDBOperations::SetUpperLimits Line 472  Int_t PamelaDBOperations::SetUpperLimits
472    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
473    upperentry = nevent-1;    upperentry = nevent-1;
474    //    //
475    if ( IsDebug() ) printf(" First entries are: OBT %llu pkt_num %i \n",obtfirst,pktfirst);    if ( IsDebug() ) printf(" First entries are: OBT %u pkt_num %u \n",obtfirst,pktfirst);
476    //    //
477    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);
478    //    //
479    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(32);
480    //    //
481    if ( !nevent ) return(2);    if ( !nevent ) return(64);
482    //    //
483    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(128);
484      if ( nevent < jump ) jump = 1;
485      //  if ( nevent < jump ) jump = int(nevent/10);
486      //  if ( !jump ) jump = 1;
487    //    //
488    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) && nevent > deltapkt ){
489      //      //
490        if ( IsDebug() ) printf(" starting jump %i \n",jump);
491      // go back      // go back
492      zomp = nevent - 2;      zomp = nevent - 2;
493      //      //
# Line 364  Int_t PamelaDBOperations::SetUpperLimits Line 509  Int_t PamelaDBOperations::SetUpperLimits
509          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
510          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
511          //          //
512          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) ){
513              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
514              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
515              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);    
516              throw -13;    
517            };
518          //          //
519          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 ){
520            zomp = i + jump + 1;            zomp = i + jump + 1;
521            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
522            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 %u upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
523            break;            break;
524          };          };
525          //          //
# Line 404  Int_t PamelaDBOperations::SetUpperLimits Line 554  Int_t PamelaDBOperations::SetUpperLimits
554    //    //
555    rhev = rh->GetEntries();    rhev = rh->GetEntries();
556    rtev = rt->GetEntries();    rtev = rt->GetEntries();
557    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
558    UInt_t sobth = 0;    Long64_t sobth = 0LL;
559    UInt_t spktt = 0;    Long64_t spktt = 0LL;
560    UInt_t spkth = 0;    Long64_t spkth = 0LL;
561    UInt_t pktt = 0;    Long64_t pktt = 0LL;
562    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
563    UInt_t pkth = 0;    Long64_t pkth = 0LL;
564    ULong64_t obth = 0;    Long64_t obth = 0LL;
565    //    //
566    T->GetEntry(upperentry);    if ( rhev || rtev ){
567    code = eh->GetCounter();  
568    Int_t lasttrail = code->Get(pctp->RunTrailer);      T->GetEntry(upperentry);
569    Int_t lasthead = code->Get(pctp->RunHeader);      code = eh->GetCounter();
570    if ( lasttrail < rtev ){      Int_t lasttrail = code->Get(pctp->RunTrailer);
571      rt->GetEntry(lasttrail);      Int_t lasthead = code->Get(pctp->RunHeader);
572      pht = eht->GetPscuHeader();      if ( lasttrail < rtev ){
573      pktt = PKT(pht->GetCounter());        rt->GetEntry(lasttrail);
574      obtt = OBT(pht->GetOrbitalTime());        pht = eht->GetPscuHeader();
575    };        pktt = PKT(pht->GetCounter());
576    //        obtt = OBT(pht->GetOrbitalTime());
577    if ( lasthead < rhev ){      };
578      rh->GetEntry(lasthead);      //
579      phh = ehh->GetPscuHeader();      if ( lasthead < rhev ){
580      pkth = PKT(phh->GetCounter());        rh->GetEntry(lasthead);
581      obth = OBT(phh->GetOrbitalTime());        phh = ehh->GetPscuHeader();
582    };        pkth = PKT(phh->GetCounter());
583    //        obth = OBT(phh->GetOrbitalTime());
584    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);      };
585    if ( pkth > upperpkt && obth > upperobt ){      //
586      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(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
587      upperpkt = pkth;      if ( pkth > upperpkt && obth > upperobt ){
588      upperobt = obth;        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
589      rhev = lasthead+1;        upperpkt = pkth;
590    } else {        upperobt = obth;
591      rhev = lasthead;        rhev = lasthead+1;
592    };      } else {
593    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);        rhev = lasthead;
594    //      };
595    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
596    if ( pktt > upperpkt && obtt > upperobt ){      //
597      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(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
598      upperpkt = pktt;      if ( pktt > upperpkt && obtt > upperobt ){
599      upperobt = obtt;        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
600      rtev = lasttrail+1;        upperpkt = pktt;
601    } else {        upperobt = obtt;
602      rtev = lasttrail;        rtev = lasttrail+1;
603    };      } else {
604    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        rtev = lasttrail;
605    //  goto kikko;      };
606    //      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
607    //      //  goto kikko;
608    // Check if runtrailer/runheader are within lower limits      //
609    //      //
610    //      // Check if runtrailer/runheader are within lower limits
611    pkth = 0;      //
612    obth = 0;      //
613    spkth = 0;      pkth = 0LL;
614    sobth = 0;      obth = 0LL;
615    for (Int_t k=0; k<rhev; k++){      spkth = 0LL;
616      if ( k > 0 ){      sobth = 0LL;
617        spkth = pkth;      for (Int_t k=0; k<rhev; k++){
618        sobth = obth;        if ( k > 0 ){
619      };          spkth = pkth;
620      rh->GetEntry(k);          sobth = obth;
621      phh = ehh->GetPscuHeader();        };
622      pkth = PKT(phh->GetCounter());        rh->GetEntry(k);
623      obth = OBT(phh->GetOrbitalTime());        phh = ehh->GetPscuHeader();
624      //        pkth = PKT(phh->GetCounter());
625  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);        obth = OBT(phh->GetOrbitalTime());
626      //        //
627      if ( pkth < spkth && obth < sobth ){        //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
628        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        //
629        //        if ( pkth < spkth && obth < sobth ){
630        rhev = k-1;          if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
631        rh->GetEntry(rhev);          //
632        pkth = spkth;          rhev = k-1;
633        obth = sobth;          rh->GetEntry(rhev);
634        //          pkth = spkth;
635        UInt_t evbefh = 0;          obth = sobth;
636        code = ehh->GetCounter();          //
637        evbefh = code->Get(pctp->Physics);              UInt_t evbefh = 0;
638        if ( evbefh >= 0 ){          code = ehh->GetCounter();
639          T->GetEntry(evbefh);          evbefh = code->Get(pctp->Physics);    
640          ph = eh->GetPscuHeader();          if ( evbefh >= 0 ){
641          t_pktlast = PKT(ph->GetCounter());            T->GetEntry(evbefh);
         t_obtlast = OBT(ph->GetOrbitalTime());  
         if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump  
           upperpkt = pkth;  
           upperobt = obth;  
           upperentry = evbefh-1;  
         } else {  
           while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){  
             evbefh++;    
             T->GetEntry(evbefh);  
             ph = eh->GetPscuHeader();  
             t_pktlast = PKT(ph->GetCounter());  
             t_obtlast = OBT(ph->GetOrbitalTime());  
           };  
           T->GetEntry(evbefh-1);  
642            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
643            upperpkt = PKT(ph->GetCounter());            t_pktlast = PKT(ph->GetCounter());
644            upperobt = OBT(ph->GetOrbitalTime());            t_obtlast = OBT(ph->GetOrbitalTime());
645            upperentry = evbefh-1;            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
646          };                  upperpkt = pkth;
647                upperobt = obth;
648                upperentry = evbefh-1;
649              } else {
650                while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
651                  evbefh++;
652                  T->GetEntry(evbefh);
653                  ph = eh->GetPscuHeader();
654                  t_pktlast = PKT(ph->GetCounter());
655                  t_obtlast = OBT(ph->GetOrbitalTime());
656                };
657                T->GetEntry(evbefh-1);
658                ph = eh->GetPscuHeader();
659                upperpkt = PKT(ph->GetCounter());
660                upperobt = OBT(ph->GetOrbitalTime());
661                upperentry = evbefh-1;
662              };    
663            };
664            if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
665            goto kikko0;
666        };        };
       if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
       goto kikko0;  
667      };      };
668    };    kikko0:
669   kikko0:      //
   //  
   //  
   //  
   pktt = 0;  
   obtt = 0;  
   spktt = 0;  
   sobtt = 0;  
   for (Int_t k=0; k<rtev; k++){  
     if ( k > 0 ){  
       spktt = pktt;  
       sobtt = obtt;  
     };  
     rt->GetEntry(k);  
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
670      //      //
 //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  
671      //      //
672      if ( pktt < spktt && obtt < sobtt ){      pktt = 0LL;
673        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);      obtt = 0LL;
674        spktt = 0LL;
675        sobtt = 0LL;
676        for (Int_t k=0; k<rtev; k++){
677          if ( k > 0 ){
678            spktt = pktt;
679            sobtt = obtt;
680          };
681          rt->GetEntry(k);
682          pht = eht->GetPscuHeader();
683          pktt = PKT(pht->GetCounter());
684          obtt = OBT(pht->GetOrbitalTime());
685        //        //
686        rtev = k-1;        //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
       rt->GetEntry(rtev);  
       pktt = spktt;  
       obtt = sobtt;  
       if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
687        //        //
688        UInt_t evbeft = 0;        if ( pktt < spktt && obtt < sobtt ){
689        code = eht->GetCounter();          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
690        evbeft = code->Get(pctp->Physics);              //
691        if ( evbeft >= 0 ){          rtev = k-1;
692          T->GetEntry(evbeft);          rt->GetEntry(rtev);
693          ph = eh->GetPscuHeader();          pktt = spktt;
694          t_pktlast = PKT(ph->GetCounter());          obtt = sobtt;
695          t_obtlast = OBT(ph->GetOrbitalTime());          if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
696          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump          //
697            upperpkt = pktt;          UInt_t evbeft = 0;
698            upperobt = obtt;          code = eht->GetCounter();
699            upperentry = evbeft-1;          evbeft = code->Get(pctp->Physics);    
700          } else {          if ( evbeft >= 0 ){
701            while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){            T->GetEntry(evbeft);
702              evbeft++;              ph = eh->GetPscuHeader();
703              T->GetEntry(evbeft);            t_pktlast = PKT(ph->GetCounter());
704              t_obtlast = OBT(ph->GetOrbitalTime());
705              if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
706                upperpkt = pktt;
707                upperobt = obtt;
708                upperentry = evbeft-1;
709              } else {
710                while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
711                  evbeft++;
712                  T->GetEntry(evbeft);
713                  ph = eh->GetPscuHeader();
714                  t_pktlast = PKT(ph->GetCounter());
715                  t_obtlast = OBT(ph->GetOrbitalTime());
716                };
717                T->GetEntry(evbeft-1);
718              ph = eh->GetPscuHeader();              ph = eh->GetPscuHeader();
719              t_pktlast = PKT(ph->GetCounter());              upperpkt = PKT(ph->GetCounter());
720              t_obtlast = OBT(ph->GetOrbitalTime());              upperobt = OBT(ph->GetOrbitalTime());
721                upperentry = evbeft-1;
722            };            };
           T->GetEntry(evbeft-1);  
           ph = eh->GetPscuHeader();  
           upperpkt = PKT(ph->GetCounter());  
           upperobt = OBT(ph->GetOrbitalTime());  
           upperentry = evbeft-1;  
723          };          };
724            if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
725            goto kikko;
726            //      break;
727            //
728        };        };
729        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        //  
       goto kikko;  
       //      break;  
       //  
730      };      };
731      //        //
732    };    kikko:
733    //      //
734   kikko:      T->GetEntry(upperentry);
735    //      code = eh->GetCounter();
736    T->GetEntry(upperentry);      lasttrail = code->Get(pctp->RunTrailer);
737    code = eh->GetCounter();      lasthead = code->Get(pctp->RunHeader);
738    lasttrail = code->Get(pctp->RunTrailer);      if ( lasttrail < rtev ){
739    lasthead = code->Get(pctp->RunHeader);        rt->GetEntry(lasttrail);
740    if ( lasttrail < rtev ){        pht = eht->GetPscuHeader();
741      rt->GetEntry(lasttrail);        pktt = PKT(pht->GetCounter());
742      pht = eht->GetPscuHeader();        obtt = OBT(pht->GetOrbitalTime());
743      pktt = PKT(pht->GetCounter());      };
744      obtt = OBT(pht->GetOrbitalTime());      //
745    };      if ( lasthead < rhev ){
746    //        rh->GetEntry(lasthead);
747    if ( lasthead < rhev ){        phh = ehh->GetPscuHeader();
748      rh->GetEntry(lasthead);        pkth = PKT(phh->GetCounter());
749      phh = ehh->GetPscuHeader();        obth = OBT(phh->GetOrbitalTime());
750      pkth = PKT(phh->GetCounter());      };
751      obth = OBT(phh->GetOrbitalTime());      //
752    };      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
753    //      if ( pkth > upperpkt && obth > upperobt ){
754    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,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);
755    if ( pkth > upperpkt && obth > upperobt ){        upperpkt = pkth;
756      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);        upperobt = obth;
757      upperpkt = pkth;        rhev = lasthead+1;
758      upperobt = obth;      } else {
759      rhev = lasthead+1;        rhev = lasthead;
760    } else {      };
761      rhev = lasthead;      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
762    };      //
763    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
764    //      if ( pktt > upperpkt && obtt > upperobt ){
765    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,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);
766    if ( pktt > upperpkt && obtt > upperobt ){        upperpkt = pktt;
767      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);        upperobt = obtt;
768      upperpkt = pktt;        rtev = lasttrail+1;
769      upperobt = obtt;      } else {
770      rtev = lasttrail+1;        rtev = lasttrail;
771    } else {      };
772      rtev = lasttrail;      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
773        //
774    };    };
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
775    //    //
776    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);
777    //    //
778    return(0);    return(0);
779  }  }
# Line 753  void PamelaDBOperations::CheckConnection Line 907  void PamelaDBOperations::CheckConnection
907    if( !conn ) throw -1;    if( !conn ) throw -1;
908    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
909    if( !connect ) throw -1;    if( !connect ) throw -1;
910    if ( !dworbit ) throw -27;    //
911      if ( IsDebug() ) printf("\n DB INFORMATIONS:\n SQL: %s Version: %s Host %s Port %i \n\n",conn->GetDBMS(),conn->ServerInfo(),conn->GetHost(),conn->GetPort());
912      //
913      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
914      //
915      // set DB timezone to UTC
916      //
917      stringstream oss;
918      //  
919      oss.str("");
920      oss << "SET time_zone='+0:00';";
921      TSQLResult *result = 0;
922      result = conn->Query(oss.str().c_str());
923      if ( !result ) throw -10;
924      oss.str("");
925      oss << "SET wait_timeout=173000;";
926      conn->Query(oss.str().c_str());
927      //
928  };  };
929    
930  /**  /**
931   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
932   */   */
933  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
934    //    //
935    //  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,ppktfirst,(UInt_t)(16777214/2));
936    //    //
937    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
938        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
939        return((Long64_t)pkt_num+16777215LL);
940      };
941    //    //
942    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
943      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
944        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
945    };    };
946    //    //
947    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
948      return((Long64_t)pkt_num);
949    //    //
950  };  };
951    
952  /**  /**
953   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
954   */   */
955  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
956    //    //
957    if ( obt < (obtfirst/2) && obtfirst > (numeric_limits<UInt_t>::max()/2) ) return((ULong64_t)(obt+numeric_limits<UInt_t>::max()));    if ( IsDebug() ) printf(" obt conversion: obt is %u obtfirst is %u  (numeric_limits<UInt_t>::max()/2) is %u  \n",obt,pobtfirst,(UInt_t)(numeric_limits<UInt_t>::max()/2));
958    //    //
959    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
960      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
961        return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));      return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
962      } else {    };
963        return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));    //
964      };    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
965        if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
966        if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
967        return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
968    };    };
969    //    //
970    return((ULong64_t)obt);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
971      return((Long64_t)obt);
972  };  };
973    
974  /**  /**
# Line 907  Int_t PamelaDBOperations::insertPamelaRa Line 1083  Int_t PamelaDBOperations::insertPamelaRa
1083    if ( idr ) return(1);    if ( idr ) return(1);
1084    //    //
1085    oss.str("");    oss.str("");
1086    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1087        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1088            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1089      } else {
1090        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1091      };
1092    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1093    //    //
1094    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 927  Int_t PamelaDBOperations::insertPamelaGL Line 1107  Int_t PamelaDBOperations::insertPamelaGL
1107    TSQLResult *result = 0;    TSQLResult *result = 0;
1108    TSQLRow *row = 0;    TSQLRow *row = 0;
1109    UInt_t t0 = 0;    UInt_t t0 = 0;
1110      Int_t signal = 0;
1111      //
1112      signal = this->SetUpperLimits();
1113    //    //
1114    stringstream   oss;    stringstream   oss;
1115    //      //  
1116    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
1117    //    //
1118    oss.str("");    oss.str("");
1119    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"    oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1120        << this->GetRawFile().Data() << "';";        << this->GetRawFile().Data() << "';";
1121    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1122    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 942  Int_t PamelaDBOperations::insertPamelaGL Line 1125  Int_t PamelaDBOperations::insertPamelaGL
1125    //    //
1126    if ( !row ){    if ( !row ){
1127      oss.str("");      oss.str("");
1128      oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "      oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1129          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
1130      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1131      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 951  Int_t PamelaDBOperations::insertPamelaGL Line 1134  Int_t PamelaDBOperations::insertPamelaGL
1134      if ( !row ) throw -10;      if ( !row ) throw -10;
1135    };    };
1136    //    //
1137    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1138      t0 = (UInt_t)tu.GetSec();
1139      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));
1140      //
1141    /*    /*
1142     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
1143     */     */
# Line 966  Int_t PamelaDBOperations::insertPamelaGL Line 1152  Int_t PamelaDBOperations::insertPamelaGL
1152    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1153    row = result->Next();    row = result->Next();
1154    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1155        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1156      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;
1157        //
1158        tsync = (UInt_t)atoll(row->GetField(2));
1159        obt0 = (UInt_t)atoll(row->GetField(1));
1160        //
1161      return(1);      return(1);
1162    };    };
1163    //    //
1164    TTree *T = 0;    TTree *T = 0;
   Int_t signal = 0;  
1165    //    //
1166    UInt_t nevent = 0;    UInt_t nevent = 0;
1167    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 1160  Int_t PamelaDBOperations::insertPamelaGL Line 1350  Int_t PamelaDBOperations::insertPamelaGL
1350    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1351    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());
1352    //    //
1353      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1354    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1355    //    //
1356      tsync = TSYNC;
1357      obt0 = OBT;
1358      //
1359    delete result;    delete result;
1360    return(signal);    return(signal);
1361  }  }
# Line 1177  Int_t PamelaDBOperations::insertPamelaRo Line 1371  Int_t PamelaDBOperations::insertPamelaRo
1371    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1372    //    //
1373    oss.str("");    oss.str("");
1374    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1375        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1376        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1377        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1378        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1379            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1380      } else {
1381        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1382            << " LEFT JOIN GL_ROOT "
1383            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1384            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1385            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1386      };
1387    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1388    //    //
1389    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1208  Int_t PamelaDBOperations::insertPamelaRo Line 1410  Int_t PamelaDBOperations::insertPamelaRo
1410    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1411    //    //
1412    oss.str("");    oss.str("");
1413    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1414        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1415            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1416      } else {
1417        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1418            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1419      };
1420    //    //
1421    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1422    //    //
# Line 1236  Int_t PamelaDBOperations::assignBOOT_NUM Line 1443  Int_t PamelaDBOperations::assignBOOT_NUM
1443    TSQLResult *result = 0;    TSQLResult *result = 0;
1444    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1445    oss.str("");    oss.str("");
1446    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1447        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1448        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1449            << " NAME = '" << this->GetRawFile().Data() << "' ";
1450      } else {
1451        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1452            << " PATH = '$PAM_RAW' AND "
1453            << " NAME = '" << this->GetRawFile().Data() << "' ";
1454      };
1455    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1456    //    //
1457    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1261  Int_t PamelaDBOperations::assignBOOT_NUM Line 1474  Int_t PamelaDBOperations::assignBOOT_NUM
1474    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1475    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1476    //    //
1477      Bool_t found = false;
1478    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1479    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1480      Bool_t found = false;      found = false;
1481      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1482        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1483        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1484        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1485          found = true;          found = true;
1486          goto fill;          goto fill;
1487        };        };
# Line 1280  Int_t PamelaDBOperations::assignBOOT_NUM Line 1494  Int_t PamelaDBOperations::assignBOOT_NUM
1494        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1495        //        //
1496      } else {      } else {
1497        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1498        };
1499      } else {
1500        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1501      };
1502      //
1503      UInt_t bn = 0;
1504      Bool_t afound = false;
1505      if ( !found && this->AutoBoot()){
1506        afound = true;
1507        //
1508        // Search for other files with similar timesync
1509        //
1510        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1511        UInt_t upperts = tsync-(obt0/1000)+5;
1512        UInt_t lowerts = tsync-(obt0/1000)-5;
1513        oss.str("");
1514        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)<"
1515            << upperts
1516            << " AND TIMESYNC-(OBT0/1000)>"
1517            << lowerts
1518            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1519        result = conn->Query(oss.str().c_str());
1520        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1521        //
1522        if ( !result ) throw -4;;
1523        found = true;
1524        if ( result->GetRowCount()<3 ){
1525          if ( IsDebug() ) printf(" AGH! no results!\n");
1526          found = false;
1527        } else {
1528          row = result->Next();      
1529          bn = (UInt_t)atoll(row->GetField(0));
1530          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1531            if ( !row ) throw -4;
1532            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1533            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1534              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1535              found = false;
1536            };
1537            row = result->Next();
1538          };
1539      };      };
1540      };
1541      //
1542      Int_t sgn = 0;
1543      //
1544      if ( !found && !BOOTNO ){
1545        throw -29;
1546    } else {    } else {
1547      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1548          this->SetBOOTnumber(bn);
1549          sgn = 8;
1550        };
1551    };    };
1552    //    //
1553    oss.str("");    oss.str("");
# Line 1293  Int_t PamelaDBOperations::assignBOOT_NUM Line 1557  Int_t PamelaDBOperations::assignBOOT_NUM
1557    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1558    //    //
1559    delete result;    delete result;
1560    return(0);    return(sgn);
1561  };  };
1562    
1563  /**  /**
# Line 1306  Int_t PamelaDBOperations::insertPamelaRU Line 1570  Int_t PamelaDBOperations::insertPamelaRU
1570    stringstream oss;    stringstream oss;
1571    oss.str("");    oss.str("");
1572    //    //
1573    signal = this->SetUpperLimits();    //  signal = this->SetUpperLimits();
1574    //    //
1575    // loop on runheader and runtrailer events    // loop on runheader and runtrailer events
1576    //    //
# Line 1324  Int_t PamelaDBOperations::insertPamelaRU Line 1588  Int_t PamelaDBOperations::insertPamelaRU
1588    rt->SetBranchAddress("RunTrailer", &runt);    rt->SetBranchAddress("RunTrailer", &runt);
1589    rt->SetBranchAddress("Header", &eht);    rt->SetBranchAddress("Header", &eht);
1590    //    //
1591      TTree *T = (TTree*)file->Get("Physics");
1592      if ( !T || T->IsZombie() ) throw -16;
1593      EventHeader *eh = 0;
1594      T->SetBranchAddress("Header", &eh);
1595      //
1596      if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
1597      //
1598    UInt_t obtt = 0;    UInt_t obtt = 0;
1599    UInt_t obth = 0;    UInt_t obth = 0;
1600    UInt_t pktt = 0;    UInt_t pktt = 0;
# Line 1357  Int_t PamelaDBOperations::insertPamelaRU Line 1628  Int_t PamelaDBOperations::insertPamelaRU
1628        //        //
1629        if ( !ptt &&  !(ptht+1) ){        if ( !ptt &&  !(ptht+1) ){
1630          //          //
1631          if ( IsDebug() ) printf(" Piece of run at the beginning of the file %i %i %i \n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Piece of run at the beginning of the file %u %u %u \n",ptht,pth,ptt);
1632          //          //
1633          this->HandleRunFragments(true,false,0,(evbeft-1));          this->HandleRunFragments(true,false,0,(evbeft-1));
1634          //          //
1635          //          //
1636        } else if ( pth == ptht ){        } else if ( pth == ptht ){
1637          //          //
1638          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1639          //          //
1640          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1641          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
# Line 1373  Int_t PamelaDBOperations::insertPamelaRU Line 1644  Int_t PamelaDBOperations::insertPamelaRU
1644          rt->GetEntry(ptt);          rt->GetEntry(ptt);
1645          pht = eht->GetPscuHeader();          pht = eht->GetPscuHeader();
1646          //          //
1647          if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer   %i %i %i \n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Try to find the beginning of a run which has only the runtrailer   %u %u %u \n",ptht,pth,ptt);
1648          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %u %u %u \n",pkth,obth,obtt);
1649          //          //
1650          this->HandleMissingHoT(true,false,evbefh,evbeft-1);          this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1651          //          //
1652        } else {        } else {
1653          //          //
1654          if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Could be a good run, we have a runheader followed by a runtrailer %u %u %u\n",ptht,pth,ptt);
1655          //          //
1656          rh->GetEntry(ptht);          rh->GetEntry(ptht);
1657          phh = ehh->GetPscuHeader();          phh = ehh->GetPscuHeader();
# Line 1388  Int_t PamelaDBOperations::insertPamelaRU Line 1659  Int_t PamelaDBOperations::insertPamelaRU
1659          obth = phh->GetOrbitalTime();          obth = phh->GetOrbitalTime();
1660          cod = ehh->GetCounter();          cod = ehh->GetCounter();
1661          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
1662          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1663          //          //
1664          // handle this run          // handle this run
1665          //          //
# Line 1398  Int_t PamelaDBOperations::insertPamelaRU Line 1669  Int_t PamelaDBOperations::insertPamelaRU
1669          //          //
1670          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1671            //            //
1672            if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER \n");            if ( IsDebug() ) printf(" Piece of run at the beginning of the file WITH NO RUNTRAILER evbefh = %u \n",evbefh);        
           //  
           this->HandleRunFragments(true,true,0,(evbefh-1));  
1673            //            //
1674              if ( evbefh == 0 ) {
1675                //
1676                signal = 8;
1677                if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1678                //
1679              } else {
1680                //
1681                this->HandleRunFragments(true,true,0,(evbefh-1));
1682                //
1683              };
1684          };          };
1685          //          //
1686          //          //
1687          if ( (ptht - pth) > 1 ){          if ( (ptht - pth) > 1 ){
1688            //            //
1689            if ( IsDebug() ) printf(" Missing runtrailers! \n");            if ( IsDebug() ) printf(" Missing runtrailers! \n");
1690            if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" Attention there is a jump in the runheader counter %u %u %u \n",ptht,pth,ptt);
1691            // is not the consecutive header            // is not the consecutive header
1692            while ( pth != ptht ){                      while ( pth != ptht ){          
1693              //              //
# Line 1429  Int_t PamelaDBOperations::insertPamelaRU Line 1708  Int_t PamelaDBOperations::insertPamelaRU
1708              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
1709              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
1710              //              //
1711              if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader   %i %i %i \n",ptht,pth,ptt);              if ( IsDebug() ) printf(" Try to find the end of a run which has only the runheader   %u %u %u \n",ptht,pth,ptt);
1712              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1713              //              //
1714              this->HandleMissingHoT(false,true,evbefh,evbeft-1);              this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1715              //              //
# Line 1439  Int_t PamelaDBOperations::insertPamelaRU Line 1718  Int_t PamelaDBOperations::insertPamelaRU
1718          } else if ( !(ptht - pth) ){          } else if ( !(ptht - pth) ){
1719            //            //
1720            if ( IsDebug() ) printf(" Missing runheader! \n");            if ( IsDebug() ) printf(" Missing runheader! \n");
1721            if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" Attention! the runheader counter did not changed %u %u %u \n",ptht,pth,ptt);
1722            if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");            if ( IsDebug() ) printf(" The run should have already been handled by HandleRun() \n");
1723            //            //
1724          } else {          } else {
# Line 1460  Int_t PamelaDBOperations::insertPamelaRU Line 1739  Int_t PamelaDBOperations::insertPamelaRU
1739            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
1740            cod = ehh->GetCounter();            cod = ehh->GetCounter();
1741            evbefh = cod->Get(pctp->Physics);            evbefh = cod->Get(pctp->Physics);
1742            if ( IsDebug() ) printf(" Piece of run at the end of file %i %i %i \n",pkth,obth,obtt);            if ( IsDebug() ) printf(" Piece of run at the end of file %u %u %u \n",pkth,obth,obtt);
1743            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1744            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1745            //            //
1746            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
1747          } else {          } else {
# Line 1510  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1789  Bool_t PamelaDBOperations::IsRunAlreadyI
1789    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&
1790    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1791    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1792      //                                  ||
1793      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1794      //                                  )
1795      //                                  ||
1796      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time > _our_runtrail_time && nevents > 100 )
1797      //                                  )
1798      //                                  ||
1799      //                                  ( runhead_time < _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1800    //                                  )    //                                  )
1801    //    //
1802    oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "    oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
# Line 1525  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1812  Bool_t PamelaDBOperations::IsRunAlreadyI
1812        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1813        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1814        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1815        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") OR "
1816          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //  these two lines in a certain way disable the patch below...
1817          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR"    //
1818          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1819          << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR"    //
1820          << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1821          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)"    //
1822          << " ));";
1823    //    //
1824    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1825    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 1547  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1841  Bool_t PamelaDBOperations::IsRunAlreadyI
1841      //      //
1842      // the run has already been inserted      // the run has already been inserted
1843      //      //
1844      //    return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here      if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1845        return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1846      //      //
1847      // PATCH!      // PATCH!
1848      // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader      // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader
# Line 1556  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1851  Bool_t PamelaDBOperations::IsRunAlreadyI
1851      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1852        //        //
1853        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");
1854        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1855        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1856        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) <<";";
1857        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1858    //       conn->Query(oss.str().c_str());
1859        if ( signal ) signal = false;        if ( signal ) signal = false;
1860        goto gonext;              goto gonext;      
1861        //        //
# Line 1573  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1869  Bool_t PamelaDBOperations::IsRunAlreadyI
1869        //        //
1870        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");
1871        //        //
1872        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1873        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1874        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) <<";";
1875        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1876    //       conn->Query(oss.str().c_str());
1877        //        //
1878        if ( signal ) signal = false;        if ( signal ) signal = false;
1879        goto gonext;        goto gonext;
# Line 1590  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1887  Bool_t PamelaDBOperations::IsRunAlreadyI
1887        //        //
1888        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");
1889        //        //
1890        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1891        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1892        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) <<";";
1893        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1894    //       conn->Query(oss.str().c_str());
1895        if ( signal ) signal = false;        if ( signal ) signal = false;
1896        //        //
1897      };      };
# Line 1608  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1906  Bool_t PamelaDBOperations::IsRunAlreadyI
1906    delete result;    delete result;
1907    //    //
1908    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1909      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1910    return(signal);    return(signal);
1911  };  };
1912    
# Line 1656  void PamelaDBOperations::HandleRunFragme Line 1955  void PamelaDBOperations::HandleRunFragme
1955    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1956    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1957    Bool_t found = false;    Bool_t found = false;
1958      Bool_t foundinrun = false;
1959    //    //
1960    TSQLResult *result = 0;    TSQLResult *result = 0;
1961    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1682  void PamelaDBOperations::HandleRunFragme Line 1982  void PamelaDBOperations::HandleRunFragme
1982    //    //
1983    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");
1984    //    //
1985    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // First of all insert the run in the fragment table...
1986    //        //
1987    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    oss.str("");
1988                                             // missing it no way we can found a piece in the frag table    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1989          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1990          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1991          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1992          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1993          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1994          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1995          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1996          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1997          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1998          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1999          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
2000          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
2001          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
2002      //
2003      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
2004      result = conn->Query(oss.str().c_str());
2005      //
2006      if ( !result ) throw -4;
2007      //
2008      row = result->Next();
2009      //
2010      if ( !row ){
2011      //      //
2012      oss.str("");      // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
2013      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      //
2014          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "      if ( IsDebug() ) printf(" The run is new \n");
2015          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()      if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2016          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!      //
2017        glrun->SetID(this->AssignRunID());
2018        glrun->SetID_RUN_FRAG(0);
2019        glrun->Fill_GL_RUN_FRAGMENTS(conn);
2020        //
2021      } else {
2022        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2023        return;
2024      };
2025      //
2026      //
2027      // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
2028      //    
2029      if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
2030                                               // missing it no way we can found a piece in the frag table
2031        //
2032        oss.str("");
2033        oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2034            << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2035            << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2036            << " ID != " << glrun->ID
2037            << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
2038      //      //
2039      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());
2040      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 1700  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,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
2050              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2051              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
2052              << " ID != " << glrun->ID
2053              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
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          //
2062          row = result->Next();
2063          //
2064        };
2065        //
2066      if ( !row ){      if ( !row ){
2067        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2068        found = false;        found = false;
# Line 1774  void PamelaDBOperations::HandleRunFragme Line 2137  void PamelaDBOperations::HandleRunFragme
2137        //        //
2138        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 ");
2139        //        //
2140          if ( foundinrun ){
2141            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2142            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2143          };
2144          //
2145        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2146        //        //
2147        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2148        //        //
2149        oss.str("");        oss.str("");
2150        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1789  void PamelaDBOperations::HandleRunFragme Line 2157  void PamelaDBOperations::HandleRunFragme
2157        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2158        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2159        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2160        if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2161        TTree *T= 0;        TTree *T= 0;
2162        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2163        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1803  void PamelaDBOperations::HandleRunFragme Line 2171  void PamelaDBOperations::HandleRunFragme
2171          bobt = OBT(ph->GetOrbitalTime());                bobt = OBT(ph->GetOrbitalTime());      
2172          firstev++;          firstev++;
2173        };        };
2174        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: %u %u %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
2175        //        //
2176        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2177        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
# Line 1813  void PamelaDBOperations::HandleRunFragme Line 2181  void PamelaDBOperations::HandleRunFragme
2181        //        //
2182        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
2183        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
2184          //
2185        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2186        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2187        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1832  void PamelaDBOperations::HandleRunFragme Line 2201  void PamelaDBOperations::HandleRunFragme
2201        //        //
2202        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2203          //          //
2204          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2205          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2206          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2207          //          //
2208          // 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));  
 //      };  
2209          //          //
         //      glrun1->SetID_RUN_FRAG(idrun0);  
2210          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2211          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2212          //          //
 //      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;  
         //  
2213        };        };
       //  
       delete glrun1;  
       //  
2214        // delete old entry in fragment table        // delete old entry in fragment table
2215        //        //
2216        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2217          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2218        //        //
2219        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2220        //        //
       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;  
2221        //        //
2222        return;        return;
2223        //        //
# Line 1903  void PamelaDBOperations::HandleRunFragme Line 2226  void PamelaDBOperations::HandleRunFragme
2226    };    };
2227    //    //
2228    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
2229                                             // 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
2230      //      //
2231      oss.str("");      oss.str("");
2232      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 "
2233          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2234          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2235            << " ID != " << glrun->ID
2236          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2237      //      //
2238      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1918  void PamelaDBOperations::HandleRunFragme Line 2242  void PamelaDBOperations::HandleRunFragme
2242      //      //
2243      row = result->Next();      row = result->Next();
2244      //      //
2245        if ( !row && NoFrag() ){
2246          //
2247          oss.str("");
2248          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2249              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2250              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2251              << " ID != " << glrun->ID
2252              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2253          //
2254          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2255          result = conn->Query(oss.str().c_str());
2256          //
2257          if ( !result ) throw -4;
2258          //
2259          foundinrun = true;
2260          row = result->Next();
2261          //
2262        };
2263        //
2264      if ( !row ){      if ( !row ){
2265        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2266        found = false;        found = false;
# Line 1991  void PamelaDBOperations::HandleRunFragme Line 2334  void PamelaDBOperations::HandleRunFragme
2334        //        //
2335        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 ");
2336        //        //
2337          if ( foundinrun ){
2338            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2339            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2340          };
2341          //
2342        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2343        //        //
2344        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2345        //        //
2346        oss.str("");        oss.str("");
2347        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 2006  void PamelaDBOperations::HandleRunFragme Line 2354  void PamelaDBOperations::HandleRunFragme
2354        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2355        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2356        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2357        if ( IsDebug() ) printf(" Check overlapping events: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);        if ( IsDebug() ) printf(" Check overlapping events: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2358        TTree *T= 0;        TTree *T= 0;
2359        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2360        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 2020  void PamelaDBOperations::HandleRunFragme Line 2368  void PamelaDBOperations::HandleRunFragme
2368          aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());      
2369          lastev--;          lastev--;
2370        };        };
2371        if ( IsDebug() ) printf(" Check overlapping events done: %i %i %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);        if ( IsDebug() ) printf(" Check overlapping events done: %u %u %llu %llu lastev is %i\n",apkt,bpkt,aobt,bobt,lastev);
2372        //        //
2373        glrun->SetEV_TO(lastev);        glrun->SetEV_TO(lastev);
2374        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
# Line 2049  void PamelaDBOperations::HandleRunFragme Line 2397  void PamelaDBOperations::HandleRunFragme
2397        //        //
2398        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2399          //          //
2400          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2401          //          //
2402          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2403          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2404          //          //
2405          // get id number          // set id number
2406          //          //
 //      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);  
 //  
2407          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2408          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2409          //          //
 //      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;  
         //  
2410        };        };
2411        //        //
2412        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
       //  
       oss.str("");  
2413        //        //
2414        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2415        //        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       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;  
2416        //        //
2417          delete glrun1;
2418        //        //
2419        return;        return;
2420        //        //
# Line 2151  void PamelaDBOperations::HandleRunFragme Line 2451  void PamelaDBOperations::HandleRunFragme
2451      //      //
2452      row = result->Next();      row = result->Next();
2453      //      //
2454      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->SetID(this->AssignRunID());  
         glrun->SetID_RUN_FRAG(0);  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2455        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");
2456        } else {
2457          if ( NoFrag() ){
2458            glrun->SetID_RUN_FRAG(glrun->GetID());
2459            glrun->Fill_GL_RUN(conn);      
2460            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2461          };    
2462      };      };
2463    };    };
2464    //    //
# Line 2708  Int_t PamelaDBOperations::insertCALO_CAL Line 2975  Int_t PamelaDBOperations::insertCALO_CAL
2975          fromtime = this->GetAbsTime(ph->GetOrbitalTime());            fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
2976          if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){          if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
2977            //            //
2978            if ( IsDebug() ) printf(" Calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);            if ( IsDebug() ) printf(" Calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
2979            //            //
2980            // check if the calibration has already been inserted            // check if the calibration has already been inserted
2981            //            //
# Line 2752  Int_t PamelaDBOperations::insertCALO_CAL Line 3019  Int_t PamelaDBOperations::insertCALO_CAL
3019                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
3020                //                //
3021                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);
3022                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
3023                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3024                  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
3025                };                };
3026                //                //
# Line 2815  Int_t PamelaDBOperations::insertCALO_CAL Line 3082  Int_t PamelaDBOperations::insertCALO_CAL
3082            //            //
3083          } else {          } else {
3084            //            //
3085            if ( IsDebug() ) printf(" Repetead calo calibration for section %i at time %i obt %i pkt %i \n",section,fromtime,obt,pkt);            if ( IsDebug() ) printf(" Repeated calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3086            //            //
3087          };                };      
3088          //                //      
# Line 2897  void PamelaDBOperations::HandleTRK_CALIB Line 3164  void PamelaDBOperations::HandleTRK_CALIB
3164        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
3165        //        //
3166        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");
3167        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
3168        //        //
3169        oss.str("");        oss.str("");
3170        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3026  Int_t PamelaDBOperations::insertTRK_CALI Line 3293  Int_t PamelaDBOperations::insertTRK_CALI
3293      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3294      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3295      //      //
3296      valid = 1;  //     valid = 1;
3297      //  //     //
3298      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
3299      //      //
3300      //      //
3301      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) ){
3302        //        //
3303        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3304          //      
3305          valid = ValidateTrkCalib( caltrk1, eh1 );
3306          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3307        //        //
3308        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3309        //        //
# Line 3050  Int_t PamelaDBOperations::insertTRK_CALI Line 3320  Int_t PamelaDBOperations::insertTRK_CALI
3320            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3321            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3322            //            //
3323            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
3324            //                  //      
3325          } else {          } else {
3326            //            //
# Line 3073  Int_t PamelaDBOperations::insertTRK_CALI Line 3343  Int_t PamelaDBOperations::insertTRK_CALI
3343          //          //
3344        };        };
3345        //        //
3346        if ( IsDebug() ) printf(" Found trk calibration2 at obt %i pkt %i t2 is %i \n",obt2,pkt2,t2);        if ( IsDebug() ) printf(" Found trk calibration2 at obt %u pkt %u t2 is %u \n",obt2,pkt2,t2);
3347        //        //
3348        // The calibration is good        // The calibration is good
3349        //        //
3350        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3351          //          //
3352          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 %u pkt %u t2 is %u is good \n",obt2,pkt2,t2);
3353            //
3354            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3355            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3356            valid = valid & valid2;
3357          //          //
3358          // Handle good calib          // Handle good calib
3359          //          //
# Line 3089  Int_t PamelaDBOperations::insertTRK_CALI Line 3363  Int_t PamelaDBOperations::insertTRK_CALI
3363          //          //
3364          if ( t2 != pret2+1 ){          if ( t2 != pret2+1 ){
3365            //            //
3366            if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i pret2 is %i \n",obt2,pkt2,t2,pret2);            if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u pret2 is %u \n",obt2,pkt2,t2,pret2);
3367            //            //
3368            while ( t2 > pret2+1 ){            while ( t2 > pret2+1 ){
3369              //              //
# Line 3118  Int_t PamelaDBOperations::insertTRK_CALI Line 3392  Int_t PamelaDBOperations::insertTRK_CALI
3392          //          //
3393          // Check for missing calibtrk2          // Check for missing calibtrk2
3394          //          //
3395          if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" Missing the trk calibration2! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3396          t2 = pret2;          t2 = pret2;
3397          //          //
3398          // handle missing calib2          // handle missing calib2
# Line 3132  Int_t PamelaDBOperations::insertTRK_CALI Line 3406  Int_t PamelaDBOperations::insertTRK_CALI
3406        //        //
3407      } else {      } else {
3408        //        //
3409        if ( IsDebug() ) printf(" Repetead trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Repetead trk calibration1 at time %u obt %u pkt %u \n",fromtime,obt1,pkt1);
3410        //        //
3411      };      };
3412      //          //    
# Line 3157  Int_t PamelaDBOperations::insertTRK_CALI Line 3431  Int_t PamelaDBOperations::insertTRK_CALI
3431      valid = 0;      valid = 0;
3432      if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){      if ( this->PKT(pkt2) > this->PKT(pktfirst) || this->OBT(obt2) > this->OBT(obtfirst) ){
3433        //        //
3434        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %i pkt %i t2 is %i\n",obt2,pkt2,t2);        if ( IsDebug() ) printf(" Missing the trk calibration1! Next one at obt %u pkt %u t2 is %u\n",obt2,pkt2,t2);
3435        //        //
3436        this->HandleTRK_CALIB(false,true);        this->HandleTRK_CALIB(false,true);
3437        //        //
# Line 3211  Int_t PamelaDBOperations::insertS4_CALIB Line 3485  Int_t PamelaDBOperations::insertS4_CALIB
3485      fromtime = this->GetAbsTime(ph->GetOrbitalTime());        fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3486      if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){      if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3487        //        //
3488        if ( IsDebug() ) printf(" S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);        if ( IsDebug() ) printf(" S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3489        //        //
3490        // check if the calibration has already been inserted        // check if the calibration has already been inserted
3491        //        //
# Line 3253  Int_t PamelaDBOperations::insertS4_CALIB Line 3527  Int_t PamelaDBOperations::insertS4_CALIB
3527            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3528            //            //
3529            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");
3530            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3531              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3532              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
3533            };            };
3534            //            //
# Line 3313  Int_t PamelaDBOperations::insertS4_CALIB Line 3587  Int_t PamelaDBOperations::insertS4_CALIB
3587        //        //
3588      } else {      } else {
3589        //        //
3590        if ( IsDebug() ) printf(" Repetead S4 calibration at time %i obt %i pkt %i \n",fromtime,obt,pkt);        if ( IsDebug() ) printf(" Repeated S4 calibration at time %u obt %u pkt %u \n",fromtime,obt,pkt);
3591        //        //
3592      };            };      
3593      //            //      
# Line 3326  Int_t PamelaDBOperations::insertS4_CALIB Line 3600  Int_t PamelaDBOperations::insertS4_CALIB
3600   * 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
3601   */   */
3602  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3603      return(this->CleanGL_RUN_FRAGMENTS(""));
3604    };
3605    
3606    /**
3607     * Scan the fragment table and move old fragments to the GL_RUN table
3608     */
3609    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3610    //    //
3611    TSQLResult *result = 0;    TSQLResult *result = 0;
3612    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3333  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3614  Int_t PamelaDBOperations::CleanGL_RUN_FR
3614    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3615    //    //
3616    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3617    //    //
3618    stringstream oss;    stringstream oss;
3619    oss.str("");    oss.str("");
3620    //    //
3621    //    if ( !strcmp(fcleanfile.Data(),"") ){
3622    // check if there are entries older than "olderthan" seconds from now      //
3623    //      // check if there are entries older than "olderthan" seconds from now
3624    oss.str("");      //
3625    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3626        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3627    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3628    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3629    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());
3630        result = conn->Query(oss.str().c_str());
3631        //
3632      } else {
3633        oss.str("");
3634        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3635        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3636        result = conn->Query(oss.str().c_str());
3637        //    
3638        if ( result ){
3639          //
3640          row = result->Next();
3641          //
3642          if ( row ){
3643            oss.str("");
3644            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3645                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3646            //
3647            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3648            result = conn->Query(oss.str().c_str());
3649            //    
3650          };
3651        } else {
3652          return(2);
3653        };
3654      };
3655    //    //
3656    if ( result ){    if ( result ){
3657      //      //
# Line 3424  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3728  Int_t PamelaDBOperations::CleanGL_RUN_FR
3728        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));      
3729        //        //
3730        //        //
3731        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3732        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3733        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);
3734        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());
3735        //  //       result2 = conn->Query(oss.str().c_str());
3736        if ( !result2 ) throw -4;  //      //
3737        //  //      if ( !result2 ) throw -4;
3738    //      //
3739        row = result->Next();        row = result->Next();
3740      };      };
3741    };    };
# Line 3442  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3747  Int_t PamelaDBOperations::CleanGL_RUN_FR
3747   * 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..
3748   */   */
3749  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3750      return(this->ValidateRuns(""));
3751    };
3752    
3753    /**
3754     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3755     */
3756    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3757    //    //
3758    TSQLResult *result = 0;    TSQLResult *result = 0;
3759    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3760    //    //
3761      UInt_t calibtime = 50;
3762      //
3763    stringstream oss;    stringstream oss;
3764    oss.str("");    oss.str("");
3765    //    //
# Line 3454  Int_t PamelaDBOperations::ValidateRuns() Line 3768  Int_t PamelaDBOperations::ValidateRuns()
3768    // =======================================================    // =======================================================
3769    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3770    UInt_t t_start = 0;    UInt_t t_start = 0;
3771    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3772    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3773    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3774    oss.str("");      // --------------------------------------------------------------
3775    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3776            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3777    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3778    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3779    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3780    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3781            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3782            return(1);        printf(" No runs to validate \n");
3783    }else{        return(1);
3784          row = result->Next();      }else{
3785          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3786    };          t_start = (UInt_t)atoll(row->GetField(4));
3787    // --------------------------------------------------------------      };  
3788    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3789    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3790    oss.str("");      // --------------------------------------------------------------
3791    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3792        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3793    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3794    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3795    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3796    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3797            row = result->Next();      if ( result->GetRowCount() ){
3798            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3799          t_stop = (UInt_t)atoll(row->GetField(4));
3800        };
3801        if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3802        // --------------------------------------------------------------
3803        // now retrieves runs to be validated
3804        // --------------------------------------------------------------
3805        oss.str("");
3806        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3807        oss << " AND RUNHEADER_TIME >="<< t_stop;
3808        oss << " ORDER BY RUNHEADER_TIME DESC;";
3809        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3810        result = conn->Query(oss.str().c_str());
3811      } else {
3812        //
3813        stringstream myquery;
3814        UInt_t myid = 0;
3815        myquery.str("");
3816        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3817        //
3818        result = conn->Query(myquery.str().c_str());
3819        //
3820        row = result->Next();      
3821        if( !row ){
3822          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3823            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3824            return(2);
3825          };
3826          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3827          return(0);
3828        };
3829        myid=(UInt_t)atoll(row->GetField(0));
3830        //
3831        myquery.str("");
3832        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3833        //
3834        result = conn->Query(myquery.str().c_str());
3835        //
3836        row = result->Next();      
3837        if( !row->GetField(0) || !row->GetField(1)){
3838          //
3839          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3840          //
3841          return(0);
3842          //
3843        } else {  
3844          //
3845          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3846          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3847          UInt_t caltime = 0;
3848          //
3849          myquery.str("");
3850          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3851          myquery << " order by FROM_TIME asc limit 1;";
3852          //
3853          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3854          //
3855          //
3856          result = conn->Query(myquery.str().c_str());
3857          //
3858          row = result->Next();      
3859          if( !row ){
3860            caltime = runhtime;
3861          } else {
3862            caltime = (UInt_t)atoll(row->GetField(0));
3863          };
3864          //
3865          myquery.str("");
3866          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3867          myquery << caltime << " order by RUNHEADER_TIME DESC";
3868          //
3869          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3870          //
3871          result = conn->Query(myquery.str().c_str());
3872          //
3873        };
3874    };    };
3875    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());  
3876    if ( !result ) throw -4;    if ( !result ) throw -4;
3877    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3878  //  printf("------------------------------------------------------------------------------- \n");    //
     
3879    Int_t nrow = 0;    Int_t nrow = 0;
3880    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3881    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3511  Int_t PamelaDBOperations::ValidateRuns() Line 3889  Int_t PamelaDBOperations::ValidateRuns()
3889    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3890    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3891    // - loop over runs, back in time,    // - loop over runs, back in time,
3892    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3893    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3894    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3895    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3551  Int_t PamelaDBOperations::ValidateRuns() Line 3929  Int_t PamelaDBOperations::ValidateRuns()
3929                                                        
3930                            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
3931                                                        
3932                            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
3933                                                        
3934                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3935                                    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 3591  Int_t PamelaDBOperations::ValidateRuns() Line 3969  Int_t PamelaDBOperations::ValidateRuns()
3969                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);                    }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3970            };            };
3971                        
3972            if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3973            nrow++;            nrow++;
3974                        
3975    };    };
# Line 3620  Bool_t PamelaDBOperations::MissingTRK_CA Line 3998  Bool_t PamelaDBOperations::MissingTRK_CA
3998          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3999          //==============================================================          //==============================================================
4000          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
4001                  if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);                  if ( IsDebug() )printf("Long time between calib and run start %u :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
4002          //==============================================================          //==============================================================
4003          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
4004          // - MM full          // - MM full
# Line 3641  Bool_t PamelaDBOperations::MissingTRK_CA Line 4019  Bool_t PamelaDBOperations::MissingTRK_CA
4019          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
4020          //==============================================================          //==============================================================
4021          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
4022          if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);          if ( IsDebug() )printf("Short time between calib and run start %u :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
4023          return(false);          return(false);
4024                    
4025  };  };
# Line 3749  Int_t PamelaDBOperations::populateTLE()/ Line 4127  Int_t PamelaDBOperations::populateTLE()/
4127    
4128    
4129  // Insert tle in the table GL_TLE using the connection conn.  // Insert tle in the table GL_TLE using the connection conn.
4130  int PamelaDBOperations::insertTle(cTle *tle)  Int_t PamelaDBOperations::insertTle(cTle *tle)
4131  {  {
4132    stringstream oss;    stringstream oss;
4133    TSQLResult *result = 0;    TSQLResult *result = 0;
# Line 3832  string getTleDatetime(cTle *tle) Line 4210  string getTleDatetime(cTle *tle)
4210    
4211    return date.str();    return date.str();
4212  }  }
4213    
4214    /**
4215     * Remove a file from the DB, delete on cascade all entries related to that file
4216     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4217     * calibration
4218     **/
4219    Int_t PamelaDBOperations::removeFile(TString remfile){
4220      //
4221      // Determine ID_ROOT_L0 and ID_RAW
4222      //
4223      TSQLResult *pResult;
4224      TSQLRow *Row;
4225      stringstream myquery;
4226      //  
4227      myquery.str("");
4228      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4229      //
4230      pResult = conn->Query(myquery.str().c_str());
4231      //
4232      Row = pResult->Next();      
4233      if( !Row ){
4234        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4235          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4236          return(1);
4237        };
4238        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4239        return(0);
4240      };
4241      //
4242      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4243      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4244      //
4245      this->ValidationOFF();
4246      //
4247      this->RemoveCALIBS();
4248      //
4249      this->RemoveRUNS();
4250      //
4251      this->RemoveFILES();
4252      //
4253      this->SetID_ROOT(0);
4254      this->SetID_RAW(0);
4255      //
4256      return(0);
4257    };
4258    
4259    /**
4260     *
4261     * Set validation bit to zero for runs following the removing file till
4262     * 1) a run with TRK_CALIB_USED=140
4263     * 2) a run with VALIDATION = 0
4264     * 3) the next calibration
4265     *
4266     **/
4267    void PamelaDBOperations::ValidationOFF(){
4268      TSQLResult *pResult;
4269      TSQLRow *Row;
4270      stringstream myquery;
4271      Int_t unv = 0;
4272      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4273      myquery.str("");
4274      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4275      //
4276      pResult = conn->Query(myquery.str().c_str());
4277      //
4278      Row = pResult->Next();      
4279      if( !Row->GetField(0) ){
4280        //
4281        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4282        //
4283      } else {  
4284        //
4285        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4286        UInt_t caltime = 0;
4287        //
4288        myquery.str("");
4289        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4290        myquery << " order by FROM_TIME asc limit 1;";
4291        //
4292        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4293        //
4294        //
4295        delete pResult;
4296        pResult = conn->Query(myquery.str().c_str());
4297        //
4298        Row = pResult->Next();      
4299        if( !Row ){
4300          caltime = runhtime;
4301        } else {
4302          caltime = (UInt_t)atoll(Row->GetField(0));
4303        };
4304        //
4305        myquery.str("");
4306        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4307        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4308        //
4309        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4310        //
4311        pResult = conn->Query(myquery.str().c_str());
4312        //
4313        Row = pResult->Next();      
4314        if( !Row ){
4315          //
4316          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4317          //
4318        } else {
4319          myquery.str("");
4320          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4321          myquery << " RUNHEADER_TIME>=" <<runhtime;
4322          myquery << " order by RUNHEADER_TIME asc;";
4323          //
4324          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4325          //
4326          pResult = conn->Query(myquery.str().c_str());
4327          //
4328          Row = pResult->Next();  
4329          while ( Row ){
4330            //      
4331            unv++;
4332            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4333            Row = pResult->Next();  
4334            //
4335          };
4336        };
4337      };
4338      if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4339    };
4340    
4341    /**
4342     *
4343     * Rearrange GL_RUN table and remove runs
4344     *
4345     **/
4346    void PamelaDBOperations::RemoveRUNS(){
4347      TSQLResult *pResult;
4348      TSQLRow *Row;
4349      stringstream myquery;
4350      UInt_t drun = 0;
4351      GL_RUN *delrun = new GL_RUN();
4352      //
4353      myquery.str("");
4354      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4355      //
4356      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4357      //
4358      pResult = conn->Query(myquery.str().c_str());
4359      //
4360      Row = pResult->Next();    
4361      //
4362      //
4363      if ( !Row ){
4364        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4365      } else {
4366        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4367        while ( Row ){
4368          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4369          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4370          drun++;
4371          Row = pResult->Next();    
4372        };
4373      };
4374      //
4375      //
4376      myquery.str("");
4377      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4378      //
4379      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4380      //
4381      pResult = conn->Query(myquery.str().c_str());
4382      //
4383      Row = pResult->Next();    
4384      //
4385      if ( !Row ){
4386        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4387      } else {
4388        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4389        while ( Row ){
4390          if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4391          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4392          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4393          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4394          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4395            if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4396            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4397          };
4398          drun++;
4399          Row = pResult->Next();    
4400        };
4401      };
4402      //
4403      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4404      //
4405      //
4406      //
4407      drun = 0;
4408      //
4409      myquery.str("");
4410      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4411      //
4412      pResult = conn->Query(myquery.str().c_str());
4413      //
4414      Row = pResult->Next();    
4415      //
4416      if ( !Row ){
4417        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4418      } else {
4419        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4420        while ( Row ){
4421          if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4422          myquery.str("");
4423          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4424          conn->Query(myquery.str().c_str());
4425          drun++;
4426          Row = pResult->Next();    
4427        };
4428      };
4429      //  
4430      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);    
4431      //
4432      //
4433      //
4434      drun = 0;
4435      //
4436      myquery.str("");
4437      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4438      //
4439      pResult = conn->Query(myquery.str().c_str());
4440      //
4441      Row = pResult->Next();    
4442      //
4443      if ( !Row ){
4444        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4445      } else {
4446        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4447        while ( Row ){
4448          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4449          myquery.str("");
4450          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4451          conn->Query(myquery.str().c_str());
4452          drun++;
4453          Row = pResult->Next();    
4454        };
4455      };
4456      //  
4457      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4458      //
4459      //
4460      //
4461      delete delrun;
4462      //
4463    };
4464    
4465    
4466    /**
4467     *
4468     * Rearrange calibration tables
4469     *
4470     **/
4471    void PamelaDBOperations::RemoveFILES(){
4472      stringstream myquery;
4473      //
4474      myquery.str("");
4475      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4476      //
4477      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4478      //
4479      conn->Query(myquery.str().c_str());
4480      //
4481    };
4482    
4483    /**
4484     *
4485     * Rearrange calibration tables
4486     *
4487     **/
4488    void PamelaDBOperations::RemoveCALIBS(){
4489      TSQLResult *pResult;
4490      TSQLRow *Row;
4491      stringstream myquery;
4492      //  
4493      //
4494      // Calorimeter
4495      //
4496      for (Int_t section = 0; section < 4; section++){
4497        myquery.str("");
4498        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4499        myquery << " SECTION=" << section << ";";
4500        //
4501        pResult = conn->Query(myquery.str().c_str());
4502        //
4503        Row = pResult->Next();      
4504        if( !Row->GetField(0) || !Row->GetField(1) ){
4505          //
4506          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4507          //
4508        } else {
4509          //
4510          myquery.str("");
4511          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4512          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4513          myquery << " SECTION=" << section << ";";
4514          //
4515          pResult = conn->Query(myquery.str().c_str());
4516          //
4517          if( !pResult ){
4518            //
4519            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4520            //
4521            throw -4;
4522            //
4523          };
4524          //
4525        };
4526      };
4527      myquery.str("");
4528      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4529      //
4530      pResult = conn->Query(myquery.str().c_str());
4531      //
4532      if( !pResult ){
4533        //
4534        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4535        //
4536        throw -4;
4537        //
4538      };
4539      //
4540      // Tracker
4541      //
4542      myquery.str("");
4543      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4544      //
4545      pResult = conn->Query(myquery.str().c_str());
4546      //
4547      Row = pResult->Next();      
4548      if( !Row->GetField(0) || !Row->GetField(1) ){
4549        //
4550        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4551        //
4552      } else {
4553        //
4554        myquery.str("");
4555        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4556        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4557        //
4558        pResult = conn->Query(myquery.str().c_str());
4559        //
4560        if( !pResult ){
4561          //
4562          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4563          //
4564          throw -4;
4565          //
4566        };
4567        //
4568        myquery.str("");
4569        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4570        //
4571        pResult = conn->Query(myquery.str().c_str());
4572        //
4573        if( !pResult ){
4574          //
4575          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4576          //
4577          throw -4;
4578          //
4579        };
4580      };
4581      //
4582      //
4583      // S4
4584      //
4585      myquery.str("");
4586      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4587      //
4588      pResult = conn->Query(myquery.str().c_str());
4589      //
4590      Row = pResult->Next();      
4591      if( !Row->GetField(0) || !Row->GetField(1) ){
4592        //
4593        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4594        //
4595      } else {
4596        //
4597        myquery.str("");
4598        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4599        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4600        //
4601        pResult = conn->Query(myquery.str().c_str());
4602        //
4603        if( !pResult ){
4604          //
4605          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4606          //
4607          throw -4;
4608          //
4609        };
4610        //
4611        myquery.str("");
4612        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4613        //
4614        pResult = conn->Query(myquery.str().c_str());
4615        //
4616        if( !pResult ){
4617          //
4618          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4619          //
4620          throw -4;
4621          //
4622        };
4623        //
4624      };
4625    };
4626    
4627    /**
4628     *
4629     * Rearrange calibration tables
4630     *
4631     **/
4632    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4633    
4634        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4635        UInt_t timeaftercalib=120000; //2000;
4636    //  ----------
4637    //  Check CRCs
4638    //  ----------
4639        for(Int_t ipkt=0; ipkt<6; ipkt++){
4640            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4641            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4642        }
4643    //  -----------------------
4644    //  Check missing packets:
4645    //  -----------------------
4646    //    Readout order:
4647    //    ------------------
4648    //    DSP   packet board
4649    //    ------------------
4650    //    12    0      1
4651    //    10    1      1
4652    //     8    2      1
4653    //     4    3      1
4654    //     6    4      1
4655    //     2    5      1
4656    //    ------------------
4657    //    11    0      2
4658    //     9    1      2
4659    //     7    2      2
4660    //     3    3      2
4661    //     5    4      2
4662    //     1    5      2
4663    //    ------------------
4664    //  -------------------------------------------------
4665    //  Check if it is first or second calibration packet
4666    //  -------------------------------------------------
4667        UInt_t build=0;
4668        TString classname = caltrk->GetName();
4669        UInt_t base=0;
4670        UInt_t mask=0;
4671        if(classname.Contains("CalibTrk1Event")){
4672            base=12;
4673            mask=0x03F000;
4674        }
4675        if(classname.Contains("CalibTrk2Event")){
4676            base=18;
4677            mask=0xFC0000;
4678        }
4679    //  -------------------------------------------------
4680    //  Count number of packets and set build variable
4681    //  -------------------------------------------------
4682        Int_t  npkts=0;
4683        for(Int_t ipkt=0; ipkt<6; ipkt++){
4684            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4685                npkts++;
4686                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4687            }
4688        }
4689    //    if( npkts==6 )return 1; // :-)
4690    
4691    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4692    
4693    //  -----------------------------------------------
4694    //  If missing packets: check the acq configuration
4695    //  (some DSPs might be excluded from acquisition)
4696    //  -----------------------------------------------
4697    
4698    //  -----------------------------------------------
4699    //  retrieve the first run header after calib
4700    //  -----------------------------------------------
4701         PacketType *pctp;
4702         EventCounter *cod;
4703         cod = eh->GetCounter();
4704         Int_t irun = cod->Get(pctp->RunHeader);
4705         TTree *rh=(TTree*)file->Get("RunHeader");
4706         if ( !rh || rh->IsZombie() ) throw -17;
4707         if( rh->GetEntries() == irun ){
4708              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4709             return 0; // :-(
4710         }
4711    
4712         RunHeaderEvent *run  = 0;
4713         EventHeader    *hrun = 0;
4714         rh->SetBranchAddress("RunHeader", &run);
4715         rh->SetBranchAddress("Header", &hrun);
4716         rh->GetEntry(irun);
4717    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4718    
4719         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4720              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4721             return 0; // :-(
4722         }
4723        
4724         if( !run->RM_ACQ_AFTER_CALIB ){
4725              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4726             return 0; // :-(
4727         }
4728    
4729         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4730         if( dtime > timeaftercalib ){
4731             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4732             return 0; // :-(
4733         }
4734        
4735    
4736    
4737         if( (run->ACQ_BUILD_INFO & mask) != build ){
4738             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4739             return 0; // :-(
4740         }
4741         return 1; // :-)
4742    
4743    }
4744    
4745    /**
4746     *
4747     * Check the DB (only for overlapping runs at the moment)
4748     *
4749     **/
4750    UInt_t PamelaDBOperations::Check(){
4751      //
4752      UInt_t test = 0;
4753      //
4754      UInt_t thisrht = 0;
4755      UInt_t thisrtt = 0;
4756      UInt_t thisid = 0;
4757      UInt_t prevrht = 0;
4758      UInt_t prevrtt = 0;
4759      UInt_t previd = 0;
4760      //
4761      UInt_t prevl0id = 0;
4762      UInt_t thisl0id = 0;
4763      //
4764      stringstream oss;
4765      TSQLResult *result = 0;
4766      TSQLRow    *row    = 0;
4767      TSQLResult *result2 = 0;
4768      TSQLRow    *row2    = 0;
4769      TSQLResult *result3 = 0;
4770      TSQLRow    *row3    = 0;
4771      oss.str("");
4772      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
4773      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4774      result = conn->Query(oss.str().c_str());
4775      //
4776      if ( !result ) throw -4;;
4777      //
4778      row = result->Next();
4779      UInt_t nid = 0;
4780      //
4781      while ( row ){
4782        nid++;
4783        if ( !(nid%1000) && nid ) printf(" %iK run scanned \n",nid/1000);
4784        thisid = (UInt_t)atoll(row->GetField(0));
4785        thisl0id = (UInt_t)atoll(row->GetField(1));
4786        thisrht = (UInt_t)atoll(row->GetField(2));
4787        thisrtt = (UInt_t)atoll(row->GetField(3));
4788        //
4789        if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
4790          //
4791          //
4792          //
4793          oss.str("");
4794          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4795              << thisid << " AND ( RUNHEADER_TIME="
4796              << thisrht << " OR RUNTRAILER_TIME="
4797              << thisrtt << " ) AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4798          result3 = conn->Query(oss.str().c_str());
4799          if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4800          if ( result3 ){
4801            //
4802            oss.str("");
4803            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4804                << thisid << " AND RUNHEADER_TIME="
4805                << thisrht << " AND RUNTRAILER_TIME!="
4806                << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4807            result3 = conn->Query(oss.str().c_str());
4808            if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4809            if ( result3 ){
4810              row3 = result3->Next();
4811              //
4812              while ( row3 ){
4813                //
4814                // 2 runs with same runheader
4815                //
4816                printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));  
4817                row3 = result3->Next();
4818              };
4819            };
4820            //
4821            oss.str("");
4822            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4823                << thisid << " AND RUNHEADER_TIME!="
4824                << thisrht << " AND RUNTRAILER_TIME="
4825                << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4826            result3 = conn->Query(oss.str().c_str());
4827            if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4828            if ( result3 ){
4829              row3 = result3->Next();
4830              //
4831              while ( row3 ){
4832                //
4833                // 2 runs with same runtrailer
4834                //
4835                printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4836                row3 = result3->Next();
4837              };
4838            };
4839            //
4840            oss.str("");
4841            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4842                << thisid << " AND RUNHEADER_TIME="
4843                << thisrht << " AND RUNTRAILER_TIME="
4844                << thisrtt << " AND ID_RUN_FRAG!="
4845                << thisid << " order by RUNHEADER_TIME asc;";
4846            result3 = conn->Query(oss.str().c_str());
4847            if ( result3 ){
4848              row3 = result3->Next();
4849              //
4850              while ( row3 ){
4851                //
4852                // duplicated run
4853                //
4854                printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4855                row3 = result3->Next();
4856              };
4857            };
4858          };
4859          //
4860          oss.str("");
4861          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4862              << thisid << " AND RUNHEADER_TIME>"
4863              << thisrht << " AND RUNTRAILER_TIME<"
4864              << thisrtt << " order by RUNHEADER_TIME asc;";
4865          result3 = conn->Query(oss.str().c_str());
4866          if ( result3 ){
4867            row3 = result3->Next();
4868            //
4869            while ( row3 ){
4870              //
4871              // run contained in the checked one
4872              //
4873              printf(" CHECK n.6 RUN %u CONTAINS RUN %u  \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4874              row3 = result3->Next();
4875            };
4876          };
4877          //
4878        };
4879        //
4880        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4881        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4882        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4883          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4884          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4885          TString prevf = "";
4886          TString thisf = "";
4887          oss.str("");
4888          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4889          result2 = conn->Query(oss.str().c_str());
4890          if ( !result2 ) throw -4;;
4891          row2 = result2->Next();
4892          prevf = (TString)row2->GetField(0);
4893          oss.str("");
4894          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4895          result2 = conn->Query(oss.str().c_str());
4896          if ( !result2 ) throw -4;;
4897          row2 = result2->Next();
4898          thisf = (TString)row2->GetField(0);
4899          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4900          test = 1;
4901        };
4902        //
4903        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4904          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4905          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4906          TString prevf = "";
4907          TString thisf = "";
4908          oss.str("");
4909          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4910          result2 = conn->Query(oss.str().c_str());
4911          if ( !result2 ) throw -4;
4912          row2 = result2->Next();
4913          prevf = (TString)row2->GetField(0);
4914          oss.str("");
4915          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4916          result2 = conn->Query(oss.str().c_str());
4917          if ( !result2 ) throw -4;;
4918          row2 = result2->Next();
4919          thisf = (TString)row2->GetField(0);
4920          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4921          test = 1;
4922        };
4923        //
4924        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4925          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4926          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4927          TString prevf = "";
4928          TString thisf = "";
4929          oss.str("");
4930          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4931          result2 = conn->Query(oss.str().c_str());
4932          if ( !result2 ) throw -4;;
4933          row2 = result2->Next();
4934          prevf = (TString)row2->GetField(0);
4935          oss.str("");
4936          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4937          result2 = conn->Query(oss.str().c_str());
4938          if ( !result2 ) throw -4;;
4939          row2 = result2->Next();
4940          thisf = (TString)row2->GetField(0);
4941          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4942          test = 1;
4943        };
4944    
4945        //
4946        prevrht = thisrht;
4947        prevrtt = thisrtt;
4948        previd = thisid;
4949        prevl0id = thisl0id;
4950        row = result->Next();
4951      };
4952      //
4953      return(test);
4954      //
4955    };

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

  ViewVC Help
Powered by ViewVC 1.1.23