/[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.20 by mocchiut, Mon Nov 20 09:11:58 2006 UTC revision 1.52 by mocchiut, Thu Oct 25 13:23:37 2007 UTC
# Line 24  Line 24 
24  #include <RunHeaderEvent.h>  #include <RunHeaderEvent.h>
25  #include <RunTrailerEvent.h>  #include <RunTrailerEvent.h>
26  #include <CalibCalPedEvent.h>  #include <CalibCalPedEvent.h>
27    #include <CalibCalPulse1Event.h>
28    #include <CalibCalPulse2Event.h>
29  #include <CalibS4Event.h>  #include <CalibS4Event.h>
30  #include <CalibTrk1Event.h>  #include <CalibTrk1Event.h>
31  #include <CalibTrk2Event.h>  #include <CalibTrk2Event.h>
# Line 56  string getTleDatetime(cTle*); Line 58  string getTleDatetime(cTle*);
58   * @param debug        debug flag.   * @param debug        debug flag.
59   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, 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){
62    //    //
63      STATIC=false;
64      if ( staticp ) STATIC=true;
65    //    //
66    SetConnection(host,user,password);    SetConnection(host,user,password);
67    //    //
# Line 66  PamelaDBOperations::PamelaDBOperations(T Line 70  PamelaDBOperations::PamelaDBOperations(T
70    glrun = new GL_RUN();    glrun = new GL_RUN();
71    //    //
72    if ( !boot ) SetNOBOOT(false);    if ( !boot ) SetNOBOOT(false);
73    SetBOOTnumber(boot);    SetTsync(tsync,gpamela);
74    SetTsync(tsync);    SetBOOTnumber(boot,gpamela);
75    SetObt0(obt0);    SetObt0(obt0);
76    //    //
77    SetTLEPath(tlefilename);    SetTLEPath(tlefilename);
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 83  PamelaDBOperations::PamelaDBOperations(T
83    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
84    if( INSERT_ROOT ){    if( INSERT_ROOT ){
85      this->SetRootName(filerootname);      this->SetRootName(filerootname);
86      this->SetOrbitNo();      this->SetOrbitNo(dwinput);
87      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
88    } else {    } else {
89      this->SetRootName("");      this->SetRootName("");
# Line 157  void PamelaDBOperations::SetDebugFlag(Bo Line 161  void PamelaDBOperations::SetDebugFlag(Bo
161  };  };
162    
163  /**  /**
164     * Set the autoboot flag
165     *
166     */
167    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
168      AUTOBOOT = dbg;
169    };
170    
171    /**
172   * Set the nofrag flag   * Set the nofrag flag
173   *   *
174   */   */
# Line 169  void PamelaDBOperations::SetNoFrag(Bool_ Line 181  void PamelaDBOperations::SetNoFrag(Bool_
181   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
182   */   */
183  void PamelaDBOperations::SetBOOTnumber(UInt_t boot){  void PamelaDBOperations::SetBOOTnumber(UInt_t boot){
184      this->SetBOOTnumber(boot,false);
185    };
186    
187    /**
188     * Store the BOOT number of the RAW file.
189     * @param boot    BOOT number of the RAW file
190     */
191    void PamelaDBOperations::SetBOOTnumber(UInt_t boot, Bool_t gpamela){
192    BOOTNO=boot;    BOOTNO=boot;
193      if ( gpamela ){
194        stringstream oss;
195        TSQLResult *result = 0;
196        TSQLRow    *row    = 0;    
197        if ( !boot ){
198          //
199          BOOTNO = 1;
200          //
201          // look in the DB for the last timesync and the last run
202          //
203          oss.str("");
204          oss << "SELECT BOOT_NUMBER FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
205          result = conn->Query(oss.str().c_str());
206          if ( result ){
207            row = result->Next();
208            if ( row ){
209              BOOTNO = (UInt_t)atoll(row->GetField(0)) + 1;
210            };
211          };
212        };
213      };
214  };  };
215    
216  /**  /**
# Line 177  void PamelaDBOperations::SetBOOTnumber(U Line 218  void PamelaDBOperations::SetBOOTnumber(U
218   * @param boot    time sync   * @param boot    time sync
219   */   */
220  void PamelaDBOperations::SetTsync(UInt_t ts){  void PamelaDBOperations::SetTsync(UInt_t ts){
221      this->SetTsync(ts,false);
222    };
223    
224    /**
225     * Store the time sync of the RAW file.
226     * @param boot    time sync
227     */
228    void PamelaDBOperations::SetTsync(UInt_t ts, Bool_t gpamela){
229      //
230      // if not gpamela or given tsync file set ts
231      //
232    tsync=ts;    tsync=ts;
233      if ( gpamela ){
234        stringstream oss;
235        TSQLResult *result = 0;
236        TSQLRow    *row    = 0;    
237        TSQLResult *result2 = 0;
238        TSQLRow    *row2    = 0;    
239        if ( !ts ){
240          //
241          tsync = 1;
242          //
243          // look in the DB for the last timesync and the last run
244          //
245          oss.str("");
246          oss << "SELECT TIMESYNC FROM GL_TIMESYNC order by TIMESYNC desc limit 1;";
247          result = conn->Query(oss.str().c_str());
248          if ( result ){
249            row = result->Next();
250            if ( row ){
251              tsync = (UInt_t)atoll(row->GetField(0)) + 1;
252              oss.str("");
253              oss << "SELECT (RUNTRAILER_TIME-RUNHEADER_TIME) FROM GL_RUN order by RUNHEADER_TIME desc limit 1;";
254              result2 = conn->Query(oss.str().c_str());
255              if ( result2 ){
256                row2 = result2->Next();
257                if ( row2 ){
258                  tsync += (UInt_t)atoll(row2->GetField(0));
259                };
260              }
261            };
262          };
263        };
264      };
265  };  };
266    
267  /**  /**
# Line 207  void PamelaDBOperations::SetRootName(TSt Line 291  void PamelaDBOperations::SetRootName(TSt
291  /**  /**
292   * Store the downlink orbit number from filename.   * Store the downlink orbit number from filename.
293   */   */
294  void PamelaDBOperations::SetOrbitNo(){  void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
295    dworbit = 0;    dworbit = 0;
296      //
297      if ( dwinput ){
298        dworbit = dwinput;
299        if ( IsDebug() ) printf(" Downlink orbit given by hand: %i  \n",dworbit);
300        return;
301      };
302      //
303    TString name = this->GetRootFile();    TString name = this->GetRootFile();
304    Int_t nlength = name.Length();    Int_t nlength = name.Length();
305    if ( nlength < 5 ) return;    if ( nlength < 5 ) return;
# Line 246  void PamelaDBOperations::SetTLEPath(TStr Line 337  void PamelaDBOperations::SetTLEPath(TStr
337    tlefilename = str;    tlefilename = str;
338  };  };
339    
340    TString PamelaDBOperations::GetRawPath(){
341      if ( STATIC ){
342        return((TString)gSystem->DirName(filerawname.Data())+'/');
343      } else {
344        return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
345      };
346    };
347    
348    TString PamelaDBOperations::GetRootPath(){
349      if ( STATIC ){
350        return((TString)gSystem->DirName(filerootname.Data())+'/');
351      } else {
352        return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
353      };
354    };
355    
356  /**  /**
357   * Store the olderthan variable   * Store the olderthan variable
358   * @param olderthan   * @param olderthan
# Line 262  Bool_t PamelaDBOperations::SetID_RAW(){ Line 369  Bool_t PamelaDBOperations::SetID_RAW(){
369    TSQLResult *result = 0;    TSQLResult *result = 0;
370    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
371    oss.str("");    oss.str("");
372    oss << "SELECT ID FROM GL_RAW WHERE "    if ( STATIC ){
373        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID FROM GL_RAW WHERE "
374        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
375            << " NAME = '" << this->GetRawFile().Data() << "' ";  
376      } else {
377        oss << "SELECT ID FROM GL_RAW WHERE "
378            << " PATH = '$PAM_RAW' AND "
379            << " NAME = '" << this->GetRawFile().Data() << "' ";  
380      }
381    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
382    if ( result == NULL ) throw -4;    if ( result == NULL ) throw -4;
383    row = result->Next();    row = result->Next();
# Line 295  Int_t PamelaDBOperations::SetUpperLimits Line 408  Int_t PamelaDBOperations::SetUpperLimits
408    UInt_t nevent = 0;    UInt_t nevent = 0;
409    UInt_t pktlast = 0;    UInt_t pktlast = 0;
410    UInt_t obtlast = 0;    UInt_t obtlast = 0;
411    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
412    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
413    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
414    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
415      Long64_t upperobt2 = 0LL;
416    UInt_t zomp = 0;    UInt_t zomp = 0;
417    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
418    EventCounter *code=0;    EventCounter *code=0;
419    //    //
420    UInt_t deltapkt = 5000;    Long64_t deltapkt = 5000LL;
421    ULong64_t deltaobt = 50000;    Long64_t deltaobt = 50000LL;
422    //    //
423    //   pcksList packetsNames;    pcksList packetsNames;
424    //   pcksList::iterator Iter;    pcksList::iterator Iter;
425    //   getPacketsNames(packetsNames);    getPacketsNames(packetsNames);
426    //    //
427    pktfirst = 0;    pktfirst = 0;
428    obtfirst = 0;    obtfirst = 0;
429      ppktfirst = 0;
430      pobtfirst = 0;
431    //    //
432    TTree *T = 0;    TTree *T = 0;
433    T = (TTree*)file->Get("Physics");    T = (TTree*)file->Get("Physics");
# Line 325  Int_t PamelaDBOperations::SetUpperLimits Line 441  Int_t PamelaDBOperations::SetUpperLimits
441    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
442    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
443    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
444      ppktfirst = pktfirst;
445      pobtfirst = obtfirst;
446    //    //
447    //   code = eh->GetCounter();    code = eh->GetCounter();
448    //   UInt_t en = 0;    UInt_t en = 0;
449    //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){    for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
450    //     en = code->Get(GetPacketType(*Iter));      en = code->Get(GetPacketType(*Iter));
451    //     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) ){
452    //};          if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
453          //
454          TTree *TC = 0;
455          TC = (TTree*)file->Get("CalibCalPed");
456          if ( !TC || TC->IsZombie() ) throw -16;
457          EventHeader *ehc = 0;
458          PscuHeader *phc = 0;
459          TC->SetBranchAddress("Header", &ehc);
460          TC->GetEntry(0);
461          phc = ehc->GetPscuHeader();
462          pktfirst = phc->GetCounter();
463          obtfirst = phc->GetOrbitalTime();  
464          //
465        };
466      };  
467    //    //
468    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
469    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
# Line 342  Int_t PamelaDBOperations::SetUpperLimits Line 474  Int_t PamelaDBOperations::SetUpperLimits
474    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
475    upperentry = nevent-1;    upperentry = nevent-1;
476    //    //
477    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);
478    //    //
479    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);
480    //    //
481    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);
482    //    //
483    if ( !nevent ) return(2);    if ( !nevent ) return(64);
484    //    //
485    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(128);
486      if ( nevent < jump ) jump = 1;
487      //  if ( nevent < jump ) jump = int(nevent/10);
488      //  if ( !jump ) jump = 1;
489    //    //
490    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 ){
491      //      //
492        if ( IsDebug() ) printf(" starting jump %i \n",jump);
493      // go back      // go back
494      zomp = nevent - 2;      zomp = nevent - 2;
495      //      //
# Line 375  Int_t PamelaDBOperations::SetUpperLimits Line 511  Int_t PamelaDBOperations::SetUpperLimits
511          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
512          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
513          //          //
514          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) ){
515              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
516              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
517              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);    
518              throw -13;    
519            };
520          //          //
521          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 ){
522            zomp = i + jump + 1;            zomp = i + jump + 1;
523            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
524            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);
525            break;            break;
526          };          };
527          //          //
# Line 415  Int_t PamelaDBOperations::SetUpperLimits Line 556  Int_t PamelaDBOperations::SetUpperLimits
556    //    //
557    rhev = rh->GetEntries();    rhev = rh->GetEntries();
558    rtev = rt->GetEntries();    rtev = rt->GetEntries();
559    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
560    UInt_t sobth = 0;    Long64_t sobth = 0LL;
561    UInt_t spktt = 0;    Long64_t spktt = 0LL;
562    UInt_t spkth = 0;    Long64_t spkth = 0LL;
563    UInt_t pktt = 0;    Long64_t pktt = 0LL;
564    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
565    UInt_t pkth = 0;    Long64_t pkth = 0LL;
566    ULong64_t obth = 0;    Long64_t obth = 0LL;
   //  
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
   if ( lasttrail < rtev ){  
     rt->GetEntry(lasttrail);  
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
   };  
   //  
   if ( lasthead < rhev ){  
     rh->GetEntry(lasthead);  
     phh = ehh->GetPscuHeader();  
     pkth = PKT(phh->GetCounter());  
     obth = OBT(phh->GetOrbitalTime());  
   };  
   //  
   if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
   if ( pkth > upperpkt && obth > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);  
     upperpkt = pkth;  
     upperobt = obth;  
     rhev = lasthead+1;  
   } else {  
     rhev = lasthead;  
   };  
   if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
567    //    //
568    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( rhev || rtev ){
569    if ( pktt > upperpkt && obtt > upperobt ){  
570      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      T->GetEntry(upperentry);
571      upperpkt = pktt;      code = eh->GetCounter();
572      upperobt = obtt;      Int_t lasttrail = code->Get(pctp->RunTrailer);
573      rtev = lasttrail+1;      Int_t lasthead = code->Get(pctp->RunHeader);
574    } else {      if ( lasttrail < rtev ){
575      rtev = lasttrail;        rt->GetEntry(lasttrail);
576    };        pht = eht->GetPscuHeader();
577    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        pktt = PKT(pht->GetCounter());
578    //  goto kikko;        obtt = OBT(pht->GetOrbitalTime());
579    //      };
580    //      //
581    // Check if runtrailer/runheader are within lower limits      if ( lasthead < rhev ){
582    //        rh->GetEntry(lasthead);
583    //        phh = ehh->GetPscuHeader();
584    pkth = 0;        pkth = PKT(phh->GetCounter());
585    obth = 0;        obth = OBT(phh->GetOrbitalTime());
586    spkth = 0;      };
587    sobth = 0;      //
588    for (Int_t k=0; k<rhev; k++){      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
589      if ( k > 0 ){      if ( pkth > upperpkt && obth > upperobt ){
590        spkth = pkth;        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
591        sobth = obth;        upperpkt = pkth;
592      };        upperobt = obth;
593      rh->GetEntry(k);        rhev = lasthead+1;
594      phh = ehh->GetPscuHeader();      } else {
595      pkth = PKT(phh->GetCounter());        rhev = lasthead;
596      obth = OBT(phh->GetOrbitalTime());      };
597      //      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
598  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);      //
599      //      if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
600      if ( pkth < spkth && obth < sobth ){      if ( pktt > upperpkt && obtt > upperobt ){
601        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
602        //        upperpkt = pktt;
603        rhev = k-1;        upperobt = obtt;
604        rh->GetEntry(rhev);        rtev = lasttrail+1;
605        pkth = spkth;      } else {
606        obth = sobth;        rtev = lasttrail;
607        //      };
608        UInt_t evbefh = 0;      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
609        code = ehh->GetCounter();      //  goto kikko;
610        evbefh = code->Get(pctp->Physics);          //
611        if ( evbefh >= 0 ){      //
612          T->GetEntry(evbefh);      // Check if runtrailer/runheader are within lower limits
613          ph = eh->GetPscuHeader();      //
614          t_pktlast = PKT(ph->GetCounter());      //
615          t_obtlast = OBT(ph->GetOrbitalTime());      pkth = 0LL;
616          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump      obth = 0LL;
617            upperpkt = pkth;      spkth = 0LL;
618            upperobt = obth;      sobth = 0LL;
619            upperentry = evbefh-1;      for (Int_t k=0; k<rhev; k++){
620          } else {        if ( k > 0 ){
621            while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){          spkth = pkth;
622              evbefh++;            sobth = obth;
623              T->GetEntry(evbefh);        };
624              ph = eh->GetPscuHeader();        rh->GetEntry(k);
625              t_pktlast = PKT(ph->GetCounter());        phh = ehh->GetPscuHeader();
626              t_obtlast = OBT(ph->GetOrbitalTime());        pkth = PKT(phh->GetCounter());
627            };        obth = OBT(phh->GetOrbitalTime());
628            T->GetEntry(evbefh-1);        //
629          //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
630          //
631          if ( pkth < spkth && obth < sobth ){
632            if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
633            //
634            rhev = k-1;
635            rh->GetEntry(rhev);
636            pkth = spkth;
637            obth = sobth;
638            //
639            UInt_t evbefh = 0;
640            code = ehh->GetCounter();
641            evbefh = code->Get(pctp->Physics);    
642            if ( evbefh >= 0 ){
643              T->GetEntry(evbefh);
644            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
645            upperpkt = PKT(ph->GetCounter());            t_pktlast = PKT(ph->GetCounter());
646            upperobt = OBT(ph->GetOrbitalTime());            t_obtlast = OBT(ph->GetOrbitalTime());
647            upperentry = evbefh-1;            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
648          };                  upperpkt = pkth;
649                upperobt = obth;
650                upperentry = evbefh-1;
651              } else {
652                while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
653                  evbefh++;
654                  T->GetEntry(evbefh);
655                  ph = eh->GetPscuHeader();
656                  t_pktlast = PKT(ph->GetCounter());
657                  t_obtlast = OBT(ph->GetOrbitalTime());
658                };
659                T->GetEntry(evbefh-1);
660                ph = eh->GetPscuHeader();
661                upperpkt = PKT(ph->GetCounter());
662                upperobt = OBT(ph->GetOrbitalTime());
663                upperentry = evbefh-1;
664              };    
665            };
666            if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
667            goto kikko0;
668        };        };
       if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
       goto kikko0;  
669      };      };
670    };    kikko0:
671   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());  
672      //      //
 //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  
673      //      //
674      if ( pktt < spktt && obtt < sobtt ){      pktt = 0LL;
675        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);      obtt = 0LL;
676        spktt = 0LL;
677        sobtt = 0LL;
678        for (Int_t k=0; k<rtev; k++){
679          if ( k > 0 ){
680            spktt = pktt;
681            sobtt = obtt;
682          };
683          rt->GetEntry(k);
684          pht = eht->GetPscuHeader();
685          pktt = PKT(pht->GetCounter());
686          obtt = OBT(pht->GetOrbitalTime());
687        //        //
688        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);  
689        //        //
690        UInt_t evbeft = 0;        if ( pktt < spktt && obtt < sobtt ){
691        code = eht->GetCounter();          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
692        evbeft = code->Get(pctp->Physics);              //
693        if ( evbeft >= 0 ){          rtev = k-1;
694          T->GetEntry(evbeft);          rt->GetEntry(rtev);
695          ph = eh->GetPscuHeader();          pktt = spktt;
696          t_pktlast = PKT(ph->GetCounter());          obtt = sobtt;
697          t_obtlast = OBT(ph->GetOrbitalTime());          if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
698          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump          //
699            upperpkt = pktt;          UInt_t evbeft = 0;
700            upperobt = obtt;          code = eht->GetCounter();
701            upperentry = evbeft-1;          evbeft = code->Get(pctp->Physics);    
702          } else {          if ( evbeft >= 0 ){
703            while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){            T->GetEntry(evbeft);
704              evbeft++;              ph = eh->GetPscuHeader();
705              T->GetEntry(evbeft);            t_pktlast = PKT(ph->GetCounter());
706              t_obtlast = OBT(ph->GetOrbitalTime());
707              if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
708                upperpkt = pktt;
709                upperobt = obtt;
710                upperentry = evbeft-1;
711              } else {
712                while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
713                  evbeft++;
714                  T->GetEntry(evbeft);
715                  ph = eh->GetPscuHeader();
716                  t_pktlast = PKT(ph->GetCounter());
717                  t_obtlast = OBT(ph->GetOrbitalTime());
718                };
719                T->GetEntry(evbeft-1);
720              ph = eh->GetPscuHeader();              ph = eh->GetPscuHeader();
721              t_pktlast = PKT(ph->GetCounter());              upperpkt = PKT(ph->GetCounter());
722              t_obtlast = OBT(ph->GetOrbitalTime());              upperobt = OBT(ph->GetOrbitalTime());
723                upperentry = evbeft-1;
724            };            };
           T->GetEntry(evbeft-1);  
           ph = eh->GetPscuHeader();  
           upperpkt = PKT(ph->GetCounter());  
           upperobt = OBT(ph->GetOrbitalTime());  
           upperentry = evbeft-1;  
725          };          };
726            if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
727            goto kikko;
728            //      break;
729            //
730        };        };
731        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        //  
       goto kikko;  
       //      break;  
       //  
732      };      };
733      //        //
734    };    kikko:
735    //      //
736   kikko:      T->GetEntry(upperentry);
737    //      code = eh->GetCounter();
738    T->GetEntry(upperentry);      lasttrail = code->Get(pctp->RunTrailer);
739    code = eh->GetCounter();      lasthead = code->Get(pctp->RunHeader);
740    lasttrail = code->Get(pctp->RunTrailer);      if ( lasttrail < rtev ){
741    lasthead = code->Get(pctp->RunHeader);        rt->GetEntry(lasttrail);
742    if ( lasttrail < rtev ){        pht = eht->GetPscuHeader();
743      rt->GetEntry(lasttrail);        pktt = PKT(pht->GetCounter());
744      pht = eht->GetPscuHeader();        obtt = OBT(pht->GetOrbitalTime());
745      pktt = PKT(pht->GetCounter());      };
746      obtt = OBT(pht->GetOrbitalTime());      //
747    };      if ( lasthead < rhev ){
748    //        rh->GetEntry(lasthead);
749    if ( lasthead < rhev ){        phh = ehh->GetPscuHeader();
750      rh->GetEntry(lasthead);        pkth = PKT(phh->GetCounter());
751      phh = ehh->GetPscuHeader();        obth = OBT(phh->GetOrbitalTime());
752      pkth = PKT(phh->GetCounter());      };
753      obth = OBT(phh->GetOrbitalTime());      //
754    };      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
755    //      if ( pkth > upperpkt && obth > upperobt ){
756    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);
757    if ( pkth > upperpkt && obth > upperobt ){        upperpkt = pkth;
758      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;
759      upperpkt = pkth;        rhev = lasthead+1;
760      upperobt = obth;      } else {
761      rhev = lasthead+1;        rhev = lasthead;
762    } else {      };
763      rhev = lasthead;      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
764    };      //
765    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);
766    //      if ( pktt > upperpkt && obtt > upperobt ){
767    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);
768    if ( pktt > upperpkt && obtt > upperobt ){        upperpkt = pktt;
769      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;
770      upperpkt = pktt;        rtev = lasttrail+1;
771      upperobt = obtt;      } else {
772      rtev = lasttrail+1;        rtev = lasttrail;
773    } else {      };
774      rtev = lasttrail;      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
775        //
776    };    };
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
777    //    //
778    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);
779    //    //
780    return(0);    return(0);
781  }  }
# Line 764  void PamelaDBOperations::CheckConnection Line 909  void PamelaDBOperations::CheckConnection
909    if( !conn ) throw -1;    if( !conn ) throw -1;
910    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
911    if( !connect ) throw -1;    if( !connect ) throw -1;
912      //
913      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());
914      //
915    if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;    if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
916    //    //
917    // set DB timezone to UTC    // set DB timezone to UTC
# Line 775  void PamelaDBOperations::CheckConnection Line 923  void PamelaDBOperations::CheckConnection
923    TSQLResult *result = 0;    TSQLResult *result = 0;
924    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
925    if ( !result ) throw -10;    if ( !result ) throw -10;
926      oss.str("");
927      oss << "SET wait_timeout=173000;";
928      conn->Query(oss.str().c_str());
929    //    //
930  };  };
931    
932  /**  /**
933   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
934   */   */
935  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
936    //    //
937    //  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));
938    //    //
939    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
940        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
941        return((Long64_t)pkt_num+16777215LL);
942      };
943    //    //
944    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
945      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
946        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
947    };    };
948    //    //
949    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
950      return((Long64_t)pkt_num);
951    //    //
952  };  };
953    
954  /**  /**
955   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
956   */   */
957  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
958    //    //
959    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));
960    //    //
961    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
962      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
963        return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));      return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
964      } else {    };
965        return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));    //
966      };    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
967        if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
968        if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
969        return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
970    };    };
971    //    //
972    return((ULong64_t)obt);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
973      return((Long64_t)obt);
974  };  };
975    
976  /**  /**
# Line 929  Int_t PamelaDBOperations::insertPamelaRa Line 1085  Int_t PamelaDBOperations::insertPamelaRa
1085    if ( idr ) return(1);    if ( idr ) return(1);
1086    //    //
1087    oss.str("");    oss.str("");
1088    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1089        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1090            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1091      } else {
1092        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1093      };
1094    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1095    //    //
1096    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 949  Int_t PamelaDBOperations::insertPamelaGL Line 1109  Int_t PamelaDBOperations::insertPamelaGL
1109    TSQLResult *result = 0;    TSQLResult *result = 0;
1110    TSQLRow *row = 0;    TSQLRow *row = 0;
1111    UInt_t t0 = 0;    UInt_t t0 = 0;
1112      Int_t signal = 0;
1113      UInt_t idresof = 0;
1114      //
1115      signal = this->SetUpperLimits();
1116    //    //
1117    stringstream   oss;    stringstream   oss;
1118    //      //  
1119    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
1120    //    //
1121    oss.str("");    oss.str("");
1122    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='"    oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1123        << this->GetRawFile().Data() << "';";        << this->GetRawFile().Data() << "';";
1124    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1125    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 964  Int_t PamelaDBOperations::insertPamelaGL Line 1128  Int_t PamelaDBOperations::insertPamelaGL
1128    //    //
1129    if ( !row ){    if ( !row ){
1130      oss.str("");      oss.str("");
1131      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< "      oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE),ID FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1132          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
1133      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1134      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 973  Int_t PamelaDBOperations::insertPamelaGL Line 1137  Int_t PamelaDBOperations::insertPamelaGL
1137      if ( !row ) throw -10;      if ( !row ) throw -10;
1138    };    };
1139    //    //
1140      idresof = (UInt_t)atoll(row->GetField(6));
1141      //
1142    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);    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);
1143    t0 = (UInt_t)tu.GetSec();    t0 = (UInt_t)tu.GetSec();
1144    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));    if ( IsDebug() ) printf(" t0 is %u ti is %s %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),row->GetField(6));
1145    //    //
1146    /*    /*
1147     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
# Line 993  Int_t PamelaDBOperations::insertPamelaGL Line 1159  Int_t PamelaDBOperations::insertPamelaGL
1159    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1160      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1161      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;
1162        //
1163        tsync = (UInt_t)atoll(row->GetField(2));
1164        obt0 = (UInt_t)atoll(row->GetField(1));
1165        //
1166      return(1);      return(1);
1167    };    };
1168    //    //
1169    TTree *T = 0;    TTree *T = 0;
   Int_t signal = 0;  
1170    //    //
1171    UInt_t nevent = 0;    UInt_t nevent = 0;
1172    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 1178  Int_t PamelaDBOperations::insertPamelaGL Line 1347  Int_t PamelaDBOperations::insertPamelaGL
1347    if ( !existsts ) throw -3;    if ( !existsts ) throw -3;
1348    //    //
1349    oss.str("");    oss.str("");
1350    oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"    oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC,ID_RESURS_OFFSET) VALUES ('"
1351        << this->GetID_RAW() << "','"//224'"        << this->GetID_RAW() << "','"//224'"
1352        << dec << (UInt_t)TYPE << "','"        << dec << (UInt_t)TYPE << "','"
1353        << dec << (UInt_t)OBT << "','"        << dec << (UInt_t)OBT << "','"
1354        << dec << (UInt_t)TSYNC << "');";        << dec << (UInt_t)TSYNC << "','"
1355          << dec << (UInt_t)idresof << "');";
1356    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1357    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());
1358      if ( conn->GetErrorCode() ){
1359        printf(" OK, you got an error because the database structure you are using is not up to date\n Using backward compability code, hence you can continue safetly \n");
1360        oss.str("");
1361        oss << "INSERT INTO GL_TIMESYNC (ID_RAW,TYPE,OBT0,TIMESYNC) VALUES ('"
1362            << this->GetID_RAW() << "','"//224'"
1363            << dec << (UInt_t)TYPE << "','"
1364            << dec << (UInt_t)OBT << "','"
1365            << dec << (UInt_t)TSYNC << "');";
1366        conn->Query(oss.str().c_str());
1367        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to fill it:\n %s \n",oss.str().c_str());
1368      };
1369    //    //
1370    if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);    if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1371      //
1372    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1373    //    //
1374      tsync = TSYNC;
1375      obt0 = OBT;
1376      //
1377    delete result;    delete result;
1378    return(signal);    return(signal);
1379  }  }
# Line 1204  Int_t PamelaDBOperations::insertPamelaRo Line 1389  Int_t PamelaDBOperations::insertPamelaRo
1389    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1390    //    //
1391    oss.str("");    oss.str("");
1392    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1393        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1394        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1395        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1396        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1397            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1398      } else {
1399        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1400            << " LEFT JOIN GL_ROOT "
1401            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1402            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1403            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1404      };
1405    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1406    //    //
1407    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1235  Int_t PamelaDBOperations::insertPamelaRo Line 1428  Int_t PamelaDBOperations::insertPamelaRo
1428    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1429    //    //
1430    oss.str("");    oss.str("");
1431    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1432        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1433            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1434      } else {
1435        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1436            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1437      };
1438    //    //
1439    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1440    //    //
# Line 1263  Int_t PamelaDBOperations::assignBOOT_NUM Line 1461  Int_t PamelaDBOperations::assignBOOT_NUM
1461    TSQLResult *result = 0;    TSQLResult *result = 0;
1462    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1463    oss.str("");    oss.str("");
1464    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1465        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1466        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1467            << " NAME = '" << this->GetRawFile().Data() << "' ";
1468      } else {
1469        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1470            << " PATH = '$PAM_RAW' AND "
1471            << " NAME = '" << this->GetRawFile().Data() << "' ";
1472      };
1473    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1474    //    //
1475    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1288  Int_t PamelaDBOperations::assignBOOT_NUM Line 1492  Int_t PamelaDBOperations::assignBOOT_NUM
1492    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1493    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1494    //    //
1495      Bool_t found = false;
1496    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1497    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1498      Bool_t found = false;      found = false;
1499      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1500        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1501        //      vde->Records->GetEntries();        //      vde->Records->GetEntries();
# Line 1307  Int_t PamelaDBOperations::assignBOOT_NUM Line 1512  Int_t PamelaDBOperations::assignBOOT_NUM
1512        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1513        //        //
1514      } else {      } else {
1515        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1516      };      };
1517    } else {    } else {
1518      if ( !this->GetBOOTnumber() ) return(2);      if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1519      };
1520      //
1521      UInt_t bn = 0;
1522      Bool_t afound = false;
1523      if ( !found && this->AutoBoot()){
1524        afound = true;
1525        //
1526        // Search for other files with similar timesync
1527        //
1528        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1529        UInt_t upperts = tsync-(obt0/1000)+5;
1530        UInt_t lowerts = tsync-(obt0/1000)-5;
1531        oss.str("");
1532        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)<"
1533            << upperts
1534            << " AND TIMESYNC-(OBT0/1000)>"
1535            << lowerts
1536            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1537        result = conn->Query(oss.str().c_str());
1538        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1539        //
1540        if ( !result ) throw -4;;
1541        found = true;
1542        if ( result->GetRowCount()<3 ){
1543          if ( IsDebug() ) printf(" AGH! no results!\n");
1544          found = false;
1545        } else {
1546          row = result->Next();      
1547          bn = (UInt_t)atoll(row->GetField(0));
1548          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1549            if ( !row ) throw -4;
1550            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1551            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1552              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1553              found = false;
1554            };
1555            row = result->Next();
1556          };
1557        };
1558      };
1559      //
1560      Int_t sgn = 0;
1561      //
1562      if ( !found && !BOOTNO ){
1563        throw -29;
1564      } else {
1565        if ( afound ){
1566          this->SetBOOTnumber(bn);
1567          sgn = 8;
1568        };
1569    };    };
1570    //    //
1571    oss.str("");    oss.str("");
# Line 1320  Int_t PamelaDBOperations::assignBOOT_NUM Line 1575  Int_t PamelaDBOperations::assignBOOT_NUM
1575    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1576    //    //
1577    delete result;    delete result;
1578    return(0);    return(sgn);
1579  };  };
1580    
1581  /**  /**
# Line 1333  Int_t PamelaDBOperations::insertPamelaRU Line 1588  Int_t PamelaDBOperations::insertPamelaRU
1588    stringstream oss;    stringstream oss;
1589    oss.str("");    oss.str("");
1590    //    //
1591    signal = this->SetUpperLimits();    //  signal = this->SetUpperLimits();
1592    //    //
1593    // loop on runheader and runtrailer events    // loop on runheader and runtrailer events
1594    //    //
# Line 1351  Int_t PamelaDBOperations::insertPamelaRU Line 1606  Int_t PamelaDBOperations::insertPamelaRU
1606    rt->SetBranchAddress("RunTrailer", &runt);    rt->SetBranchAddress("RunTrailer", &runt);
1607    rt->SetBranchAddress("Header", &eht);    rt->SetBranchAddress("Header", &eht);
1608    //    //
1609      TTree *T = (TTree*)file->Get("Physics");
1610      if ( !T || T->IsZombie() ) throw -16;
1611      EventHeader *eh = 0;
1612      T->SetBranchAddress("Header", &eh);
1613      //
1614      if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
1615      //
1616    UInt_t obtt = 0;    UInt_t obtt = 0;
1617    UInt_t obth = 0;    UInt_t obth = 0;
1618    UInt_t pktt = 0;    UInt_t pktt = 0;
# Line 1384  Int_t PamelaDBOperations::insertPamelaRU Line 1646  Int_t PamelaDBOperations::insertPamelaRU
1646        //        //
1647        if ( !ptt &&  !(ptht+1) ){        if ( !ptt &&  !(ptht+1) ){
1648          //          //
1649          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);
1650          //          //
1651          this->HandleRunFragments(true,false,0,(evbeft-1));          this->HandleRunFragments(true,false,0,(evbeft-1));
1652          //          //
1653          //          //
1654        } else if ( pth == ptht ){        } else if ( pth == ptht ){
1655          //          //
1656          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1657          //          //
1658          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1659          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
# Line 1400  Int_t PamelaDBOperations::insertPamelaRU Line 1662  Int_t PamelaDBOperations::insertPamelaRU
1662          rt->GetEntry(ptt);          rt->GetEntry(ptt);
1663          pht = eht->GetPscuHeader();          pht = eht->GetPscuHeader();
1664          //          //
1665          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);
1666          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %u %u %u \n",pkth,obth,obtt);
1667          //          //
1668          this->HandleMissingHoT(true,false,evbefh,evbeft-1);          this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1669          //          //
1670        } else {        } else {
1671          //          //
1672          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);
1673          //          //
1674          rh->GetEntry(ptht);          rh->GetEntry(ptht);
1675          phh = ehh->GetPscuHeader();          phh = ehh->GetPscuHeader();
# Line 1415  Int_t PamelaDBOperations::insertPamelaRU Line 1677  Int_t PamelaDBOperations::insertPamelaRU
1677          obth = phh->GetOrbitalTime();          obth = phh->GetOrbitalTime();
1678          cod = ehh->GetCounter();          cod = ehh->GetCounter();
1679          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
1680          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1681          //          //
1682          // handle this run          // handle this run
1683          //          //
# Line 1425  Int_t PamelaDBOperations::insertPamelaRU Line 1687  Int_t PamelaDBOperations::insertPamelaRU
1687          //          //
1688          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1689            //            //
1690            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));  
1691            //            //
1692              if ( evbefh == 0 ) {
1693                //
1694                signal = 8;
1695                if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1696                //
1697              } else {
1698                //
1699                this->HandleRunFragments(true,true,0,(evbefh-1));
1700                //
1701              };
1702          };          };
1703          //          //
1704          //          //
1705          if ( (ptht - pth) > 1 ){          if ( (ptht - pth) > 1 ){
1706            //            //
1707            if ( IsDebug() ) printf(" Missing runtrailers! \n");            if ( IsDebug() ) printf(" Missing runtrailers! \n");
1708            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);
1709            // is not the consecutive header            // is not the consecutive header
1710            while ( pth != ptht ){                      while ( pth != ptht ){          
1711              //              //
# Line 1456  Int_t PamelaDBOperations::insertPamelaRU Line 1726  Int_t PamelaDBOperations::insertPamelaRU
1726              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
1727              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
1728              //              //
1729              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);
1730              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1731              //              //
1732              this->HandleMissingHoT(false,true,evbefh,evbeft-1);              this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1733              //              //
# Line 1466  Int_t PamelaDBOperations::insertPamelaRU Line 1736  Int_t PamelaDBOperations::insertPamelaRU
1736          } else if ( !(ptht - pth) ){          } else if ( !(ptht - pth) ){
1737            //            //
1738            if ( IsDebug() ) printf(" Missing runheader! \n");            if ( IsDebug() ) printf(" Missing runheader! \n");
1739            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);
1740            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");
1741            //            //
1742          } else {          } else {
# Line 1487  Int_t PamelaDBOperations::insertPamelaRU Line 1757  Int_t PamelaDBOperations::insertPamelaRU
1757            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
1758            cod = ehh->GetCounter();            cod = ehh->GetCounter();
1759            evbefh = cod->Get(pctp->Physics);            evbefh = cod->Get(pctp->Physics);
1760            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);
1761            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1762            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1763            //            //
1764            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
1765          } else {          } else {
# Line 1537  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1807  Bool_t PamelaDBOperations::IsRunAlreadyI
1807    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&
1808    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1809    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1810      //                                  ||
1811      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1812      //                                  )
1813      //                                  ||
1814      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time > _our_runtrail_time && nevents > 100 )
1815      //                                  )
1816      //                                  ||
1817      //                                  ( runhead_time < _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1818    //                                  )    //                                  )
1819    //    //
1820    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 1552  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1830  Bool_t PamelaDBOperations::IsRunAlreadyI
1830        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1831        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1832        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1833        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") OR "
1834          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //  these two lines in a certain way disable the patch below...
1835          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR"    //
1836          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1837          << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100) OR"    //
1838          << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1839          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)"    //
1840          << " ));";
1841    //    //
1842    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());
1843    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 1574  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1859  Bool_t PamelaDBOperations::IsRunAlreadyI
1859      //      //
1860      // the run has already been inserted      // the run has already been inserted
1861      //      //
1862      //    return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here      if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1863        return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1864      //      //
1865      // PATCH!      // PATCH!
1866      // 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 1638  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1924  Bool_t PamelaDBOperations::IsRunAlreadyI
1924    delete result;    delete result;
1925    //    //
1926    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1927      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1928    return(signal);    return(signal);
1929  };  };
1930    
# Line 1751  void PamelaDBOperations::HandleRunFragme Line 2038  void PamelaDBOperations::HandleRunFragme
2038      //      //
2039    } else {    } else {
2040      if ( IsDebug() ) printf(" The run is already present in the fragment table \n");      if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
2041        return;
2042    };    };
2043    //    //
2044    //    //
# Line 1887  void PamelaDBOperations::HandleRunFragme Line 2175  void PamelaDBOperations::HandleRunFragme
2175        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2176        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2177        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2178        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);
2179        TTree *T= 0;        TTree *T= 0;
2180        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2181        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1901  void PamelaDBOperations::HandleRunFragme Line 2189  void PamelaDBOperations::HandleRunFragme
2189          bobt = OBT(ph->GetOrbitalTime());                bobt = OBT(ph->GetOrbitalTime());      
2190          firstev++;          firstev++;
2191        };        };
2192        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);
2193        //        //
2194        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2195        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
# Line 2084  void PamelaDBOperations::HandleRunFragme Line 2372  void PamelaDBOperations::HandleRunFragme
2372        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2373        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2374        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2375        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);
2376        TTree *T= 0;        TTree *T= 0;
2377        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2378        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 2098  void PamelaDBOperations::HandleRunFragme Line 2386  void PamelaDBOperations::HandleRunFragme
2386          aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());      
2387          lastev--;          lastev--;
2388        };        };
2389        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);
2390        //        //
2391        glrun->SetEV_TO(lastev);        glrun->SetEV_TO(lastev);
2392        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
# Line 2705  Int_t PamelaDBOperations::insertCALO_CAL Line 2993  Int_t PamelaDBOperations::insertCALO_CAL
2993          fromtime = this->GetAbsTime(ph->GetOrbitalTime());            fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
2994          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) ){
2995            //            //
2996            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);
2997            //            //
2998            // check if the calibration has already been inserted            // check if the calibration has already been inserted
2999            //            //
# Line 2750  Int_t PamelaDBOperations::insertCALO_CAL Line 3038  Int_t PamelaDBOperations::insertCALO_CAL
3038                //                //
3039                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);
3040                if ( fromtime < 1150871000 ){ //1150866904                if ( fromtime < 1150871000 ){ //1150866904
3041                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3042                  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
3043                };                };
3044                //                //
# Line 2812  Int_t PamelaDBOperations::insertCALO_CAL Line 3100  Int_t PamelaDBOperations::insertCALO_CAL
3100            //            //
3101          } else {          } else {
3102            //            //
3103            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);
3104            //            //
3105          };                };      
3106          //                //      
# Line 2823  Int_t PamelaDBOperations::insertCALO_CAL Line 3111  Int_t PamelaDBOperations::insertCALO_CAL
3111    return(0);    return(0);
3112  };  };
3113    
3114    
3115    /**
3116     * Scan calorimeter calibrations packets, fill the GL_CALO_CALIB table
3117     */
3118    Int_t PamelaDBOperations::insertCALOPULSE_CALIB(){
3119      //
3120      TSQLResult *result = 0;
3121      TSQLRow    *row    = 0;
3122      //
3123      stringstream oss;
3124      oss.str("");
3125      //
3126      oss << " DESCRIBE GL_CALOPULSE_CALIB;";
3127      if ( IsDebug() ) printf(" Check if the GL_CALOPULSE_CALIB table exists: query is \n %s \n",oss.str().c_str());
3128      result = conn->Query(oss.str().c_str());
3129      //
3130      if ( conn->GetErrorCode() ){
3131        if ( IsDebug() ) printf(" The GL_CALOPULSE_CALIB table does not exists! \n");
3132        throw -30;
3133      };
3134      //
3135      // CaloPulse1
3136      //
3137      CalibCalPulse1Event *cp1 = 0;
3138      TTree *tr = 0;
3139      EventHeader *eh = 0;
3140      PscuHeader *ph = 0;
3141      //
3142      UInt_t nevents = 0;
3143      UInt_t fromtime = 0;
3144      UInt_t totime = 0;
3145      UInt_t obt = 0;
3146      UInt_t pkt = 0;
3147      //
3148      tr = (TTree*)file->Get("CalibCalPulse1");
3149      if ( !tr || tr->IsZombie() ) throw -31;
3150      //
3151      tr->SetBranchAddress("CalibCalPulse1", &cp1);
3152      tr->SetBranchAddress("Header", &eh);
3153      nevents = tr->GetEntries();
3154      //
3155      if ( nevents > 0 ){
3156        //
3157        for (UInt_t i=0; i < nevents; i++){
3158          tr->GetEntry(i);
3159          for (UInt_t section = 0; section < 4; section++){
3160            //
3161            if ( cp1->pstwerr[section] && cp1->unpackError == 0 ){
3162              valid = 1;
3163              if ( cp1->pperror[section] ) valid = 0;
3164              ph = eh->GetPscuHeader();
3165              obt = ph->GetOrbitalTime();  
3166              pkt = ph->GetCounter();  
3167              fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3168              if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3169                //
3170                if ( IsDebug() ) printf(" Calo pulse1 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3171                //
3172                // check if the calibration has already been inserted
3173                //
3174                oss.str("");
3175                oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
3176                    << " SECTION = "<< section << " AND "
3177                    << " PULSE_AMPLITUDE = 0 AND "
3178                    << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3179                    << " OBT = "<< obt << " AND "
3180                    << " PKT = "<< pkt << ";";
3181                //
3182                if ( IsDebug() ) printf(" Check if the calo pulse1 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3183                result = conn->Query(oss.str().c_str());
3184                //
3185                if ( !result ) throw -4;
3186                //
3187                row = result->Next();
3188                //
3189                if ( row ){
3190                  //
3191                  if ( IsDebug() ) printf(" Calo pulse1 calibration already inserted in the DB\n");
3192                  //
3193                } else {
3194                  //
3195                  // we have to insert a new calibration, check where to place it
3196                  //
3197                  oss.str("");
3198                  oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
3199                      << " SECTION = "<< section << " AND "
3200                      << " PULSE_AMPLITUDE = 0 AND "
3201                      << " SECTION = "<< section << " AND "
3202                      << " FROM_TIME < "<< fromtime << " AND "
3203                      << " TO_TIME > "<< fromtime << ";";
3204                  //
3205                  if ( IsDebug() ) printf(" Check where to place the pulse1 calo calibration: query is \n %s \n",oss.str().c_str());
3206                  result = conn->Query(oss.str().c_str());
3207                  //
3208                  if ( !result ) throw -4;
3209                  //
3210                  row = result->Next();
3211                  //
3212                  if ( !row ){
3213                    //
3214                    // no calibrations in the db contain our calibration
3215                    //
3216                    if ( IsDebug() ) printf(" Pulse1 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
3217                    if ( fromtime < 1150871000 ){ //1150866904
3218                      if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3219                      fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3220                    };
3221                    //
3222                    oss.str("");
3223                    oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
3224                        << " PULSE_AMPLITUDE = 0 AND "
3225                        << " SECTION = "<< section << " AND "
3226                        << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3227                    //
3228                    if ( IsDebug() ) printf(" Check the upper limit for pulse1 calibration: query is \n %s \n",oss.str().c_str());
3229                    result = conn->Query(oss.str().c_str());
3230                    //
3231                    if ( !result ) throw -4;
3232                    //
3233                    row = result->Next();
3234                    if ( !row ){
3235                      totime = numeric_limits<UInt_t>::max();
3236                    } else {
3237                      totime = (UInt_t)atoll(row->GetField(0));
3238                    };
3239                    //
3240                  } else {
3241                    //
3242                    // determine upper and lower limits and make space for the new calibration
3243                    //
3244                    totime = (UInt_t)atoll(row->GetField(1));
3245                    //
3246                    oss.str("");
3247                    oss << " UPDATE GL_CALOPULSE_CALIB SET "  
3248                        << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3249                        << " ID = "<< row->GetField(0) << ";";
3250                    //
3251                    if ( IsDebug() ) printf(" Make space for the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
3252                    result = conn->Query(oss.str().c_str());
3253                    //
3254                    if ( !result ) throw -4;
3255                    //
3256                  };
3257                  //
3258                  oss.str("");
3259                  oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
3260                      << " VALUES (NULL,' "
3261                      << idroot << "','"
3262                      << i << "','"
3263                      << fromtime << "','"
3264                      << totime << "','"
3265                      << section << "',NULL,'0','"
3266                      << obt << "','"
3267                      << pkt << "','"
3268                      << this->GetBOOTnumber() << "','"
3269                      << valid << "');";
3270                  //
3271                  if ( IsDebug() ) printf(" Insert the new pulse1 calibration: query is \n %s \n",oss.str().c_str());
3272                  //
3273                  result = conn->Query(oss.str().c_str());
3274                  //
3275                  if ( !result ) throw -4;
3276                  //
3277                };
3278                //
3279              } else {
3280                //
3281                if ( IsDebug() ) printf(" Repeated pulse1 calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3282                //
3283              };      
3284              //      
3285            };
3286          };
3287        };
3288      };
3289      //
3290      // CaloPulse2
3291      //
3292      tr->Reset();
3293      CalibCalPulse2Event *cp2 = 0;
3294      tr = 0;
3295      //
3296      nevents = 0;
3297      fromtime = 0;
3298      totime = 0;
3299      obt = 0;
3300      pkt = 0;
3301      //
3302      tr = (TTree*)file->Get("CalibCalPulse2");
3303      if ( !tr || tr->IsZombie() ) throw -32;
3304      //
3305      tr->SetBranchAddress("CalibCalPulse2", &cp2);
3306      tr->SetBranchAddress("Header", &eh);
3307      nevents = tr->GetEntries();
3308      //
3309      if ( nevents > 0 ){
3310        //
3311        for (UInt_t i=0; i < nevents; i++){
3312          tr->GetEntry(i);
3313          for (UInt_t section = 0; section < 4; section++){
3314            //
3315            if ( cp2->pstwerr[section] && cp2->unpackError == 0 ){
3316              valid = 1;
3317              if ( cp2->pperror[section] ) valid = 0;
3318              ph = eh->GetPscuHeader();
3319              obt = ph->GetOrbitalTime();  
3320              pkt = ph->GetCounter();  
3321              fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3322              if ( this->PKT(pkt) >= this->PKT(pktfirst) && this->OBT(obt) >= this->OBT(obtfirst) ){
3323                //
3324                if ( IsDebug() ) printf(" Calo pulse2 calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3325                //
3326                // check if the calibration has already been inserted
3327                //
3328                oss.str("");
3329                oss << " SELECT ID FROM GL_CALOPULSE_CALIB WHERE "
3330                    << " SECTION = "<< section << " AND "
3331                    << " PULSE_AMPLITUDE != 0 AND "
3332                    << " BOOT_NUMBER = "<< this->GetBOOTnumber() << " AND "
3333                    << " OBT = "<< obt << " AND "
3334                    << " PKT = "<< pkt << ";";
3335                //
3336                if ( IsDebug() ) printf(" Check if the calo pulse2 calibration has already been inserted: query is \n %s \n",oss.str().c_str());
3337                result = conn->Query(oss.str().c_str());
3338                //
3339                if ( !result ) throw -4;
3340                //
3341                row = result->Next();
3342                //
3343                if ( row ){
3344                  //
3345                  if ( IsDebug() ) printf(" Calo pulse2 calibration already inserted in the DB\n");
3346                  //
3347                } else {
3348                  //
3349                  // we have to insert a new calibration
3350                  //
3351                  //
3352                  // Determine the amplitude of the pulse
3353                  //
3354                  UInt_t pampli = 1;
3355                  UInt_t pstrip = 0;
3356                  UInt_t se = 0;
3357                  if ( section == 1 ) se = 2;
3358                  if ( section == 2 ) se = 3;
3359                  if ( section == 3 ) se = 1;
3360                  for (Int_t ii=0;ii<16;ii++){
3361                    if ( cp2->calpuls[se][0][ii] > 10000. ){
3362                      pampli = 2;
3363                      pstrip = ii;
3364                    };
3365                  };
3366                  if ( pampli == 1 ){
3367                    Bool_t found = false;
3368                    Float_t delta=0.;
3369                    UInt_t cstr = 0;
3370                    while ( !found && cstr < 16 ){
3371                      for (Int_t ii=0;ii<16;ii++){
3372                        delta = cp2->calpuls[se][0][ii] - cp2->calpuls[se][0][cstr];
3373                        if ( IsDebug() ) printf(" cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
3374                        if ( delta > 500. ){
3375                          pampli = 1;
3376                          pstrip = ii;
3377                          found = true;
3378                          if ( IsDebug() ) printf(" FOUND cstr is %u ii is %i delta is %f \n",cstr,ii,delta);
3379                        };
3380                      };
3381                      cstr++;
3382                    };
3383                    if ( !found ) pstrip = 100;
3384                  };
3385                  if ( IsDebug() ) printf(" The amplitude of the pulser is %u (where 1 = low pulse, 2 = high pulse), pulsed strip is %u \n",pampli,pstrip);
3386                  //
3387                  // we have to insert a new calibration, check where to place it
3388                  //
3389                  oss.str("");
3390                  oss << " SELECT ID,TO_TIME FROM GL_CALOPULSE_CALIB WHERE "
3391                      << " SECTION = "<< section << " AND "
3392                      << " PULSE_AMPLITUDE = " << pampli << " AND "
3393                      << " SECTION = "<< section << " AND "
3394                      << " FROM_TIME < "<< fromtime << " AND "
3395                      << " TO_TIME > "<< fromtime << ";";
3396                  //
3397                  if ( IsDebug() ) printf(" Check where to place the pulse2 calo calibration: query is \n %s \n",oss.str().c_str());
3398                  result = conn->Query(oss.str().c_str());
3399                  //
3400                  if ( !result ) throw -4;
3401                  //
3402                  row = result->Next();
3403                  //
3404                  if ( !row ){
3405                    //
3406                    // no calibrations in the db contain our calibration
3407                    //
3408                    if ( IsDebug() ) printf(" Pulse2 calibration with fromtime lower than others to be inserted in the DB for section %i \n",section);
3409                    if ( fromtime < 1150871000 ){ //1150866904
3410                      if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3411                      fromtime = 0;// the first flight calibration was taken at about  1156429100 s, this line allow to analyze first runs in raw mode
3412                    };
3413                    //
3414                    oss.str("");
3415                    oss << " SELECT FROM_TIME FROM GL_CALOPULSE_CALIB WHERE "
3416                        << " PULSE_AMPLITUDE = " << pampli << " AND "
3417                        << " SECTION = "<< section << " AND "
3418                        << " FROM_TIME > "<< fromtime << " ORDER BY FROM_TIME ASC LIMIT 1;";
3419                    //
3420                    if ( IsDebug() ) printf(" Check the upper limit for pulse2 calibration: query is \n %s \n",oss.str().c_str());
3421                    result = conn->Query(oss.str().c_str());
3422                    //
3423                    if ( !result ) throw -4;
3424                    //
3425                    row = result->Next();
3426                    if ( !row ){
3427                      totime = numeric_limits<UInt_t>::max();
3428                    } else {
3429                      totime = (UInt_t)atoll(row->GetField(0));
3430                    };
3431                    //
3432                  } else {
3433                    //
3434                    // determine upper and lower limits and make space for the new calibration
3435                    //
3436                    totime = (UInt_t)atoll(row->GetField(1));
3437                    //
3438                    oss.str("");
3439                    oss << " UPDATE GL_CALOPULSE_CALIB SET "  
3440                        << " TO_TIME = "<< fromtime << " WHERE "  // NOTICE: to_time is equal to from_time of the calibration before, so the interval is: [from_time,to_time[
3441                        << " ID = "<< row->GetField(0) << ";";
3442                    //
3443                    if ( IsDebug() ) printf(" Make space for the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
3444                    result = conn->Query(oss.str().c_str());
3445                    //
3446                    if ( !result ) throw -4;
3447                    //
3448                  };
3449                  //
3450                  // Fill the DB
3451                  //
3452                  oss.str("");
3453                  //              oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,OBT,PKT,BOOT_NUMBER,VALIDATION) "
3454                  oss << " INSERT INTO GL_CALOPULSE_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,SECTION,PULSED_STRIP,PULSE_AMPLITUDE,OBT,PKT,BOOT_NUMBER,VALIDATION) "
3455                      << " VALUES (NULL,' "
3456                      << idroot << "','"
3457                      << i << "','"
3458                      << fromtime << "','"
3459                      << totime << "','"
3460                      << section << "','"
3461                      << pstrip << "','"
3462                      << pampli << "','"
3463                      << obt << "','"
3464                      << pkt << "','"
3465                      << this->GetBOOTnumber() << "','"
3466                      << valid << "');";
3467                  //
3468                  if ( IsDebug() ) printf(" Insert the new pulse2 calibration: query is \n %s \n",oss.str().c_str());
3469                  //
3470                  result = conn->Query(oss.str().c_str());
3471                  //
3472                  if ( !result ) throw -4;
3473                  //
3474                };
3475                //
3476              } else {
3477                //
3478                if ( IsDebug() ) printf(" Repeated pulse2 calo calibration for section %i at time %u obt %u pkt %u \n",section,fromtime,obt,pkt);
3479                //
3480              };      
3481              //      
3482            };
3483          };
3484        };
3485      };
3486      //
3487      return(0);
3488    };
3489    
3490  /**  /**
3491   * Fill the GL_TRK_CALIB table   * Fill the GL_TRK_CALIB table
3492   */   */
# Line 3023  Int_t PamelaDBOperations::insertTRK_CALI Line 3687  Int_t PamelaDBOperations::insertTRK_CALI
3687      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3688      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3689      //      //
3690      valid = 1;  //     valid = 1;
3691      //  //     //
3692      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
3693      //      //
3694      //      //
3695      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) ){
3696        //        //
3697        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);
3698          //      
3699          valid = ValidateTrkCalib( caltrk1, eh1 );
3700          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3701        //        //
3702        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3703        //        //
# Line 3047  Int_t PamelaDBOperations::insertTRK_CALI Line 3714  Int_t PamelaDBOperations::insertTRK_CALI
3714            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3715            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3716            //            //
3717            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
3718            //                  //      
3719          } else {          } else {
3720            //            //
# Line 3070  Int_t PamelaDBOperations::insertTRK_CALI Line 3737  Int_t PamelaDBOperations::insertTRK_CALI
3737          //          //
3738        };        };
3739        //        //
3740        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);
3741        //        //
3742        // The calibration is good        // The calibration is good
3743        //        //
3744        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3745          //          //
3746          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);
3747            //
3748            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3749            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3750            valid = valid & valid2;
3751          //          //
3752          // Handle good calib          // Handle good calib
3753          //          //
# Line 3086  Int_t PamelaDBOperations::insertTRK_CALI Line 3757  Int_t PamelaDBOperations::insertTRK_CALI
3757          //          //
3758          if ( t2 != pret2+1 ){          if ( t2 != pret2+1 ){
3759            //            //
3760            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);
3761            //            //
3762            while ( t2 > pret2+1 ){            while ( t2 > pret2+1 ){
3763              //              //
# Line 3115  Int_t PamelaDBOperations::insertTRK_CALI Line 3786  Int_t PamelaDBOperations::insertTRK_CALI
3786          //          //
3787          // Check for missing calibtrk2          // Check for missing calibtrk2
3788          //          //
3789          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);
3790          t2 = pret2;          t2 = pret2;
3791          //          //
3792          // handle missing calib2          // handle missing calib2
# Line 3129  Int_t PamelaDBOperations::insertTRK_CALI Line 3800  Int_t PamelaDBOperations::insertTRK_CALI
3800        //        //
3801      } else {      } else {
3802        //        //
3803        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);
3804        //        //
3805      };      };
3806      //          //    
# Line 3154  Int_t PamelaDBOperations::insertTRK_CALI Line 3825  Int_t PamelaDBOperations::insertTRK_CALI
3825      valid = 0;      valid = 0;
3826      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) ){
3827        //        //
3828        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);
3829        //        //
3830        this->HandleTRK_CALIB(false,true);        this->HandleTRK_CALIB(false,true);
3831        //        //
# Line 3208  Int_t PamelaDBOperations::insertS4_CALIB Line 3879  Int_t PamelaDBOperations::insertS4_CALIB
3879      fromtime = this->GetAbsTime(ph->GetOrbitalTime());        fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3880      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) ){
3881        //        //
3882        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);
3883        //        //
3884        // check if the calibration has already been inserted        // check if the calibration has already been inserted
3885        //        //
# Line 3251  Int_t PamelaDBOperations::insertS4_CALIB Line 3922  Int_t PamelaDBOperations::insertS4_CALIB
3922            //            //
3923            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");
3924            if ( fromtime < 1150871000 ){              if ( fromtime < 1150871000 ){  
3925              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3926              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
3927            };            };
3928            //            //
# Line 3310  Int_t PamelaDBOperations::insertS4_CALIB Line 3981  Int_t PamelaDBOperations::insertS4_CALIB
3981        //        //
3982      } else {      } else {
3983        //        //
3984        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);
3985        //        //
3986      };            };      
3987      //            //      
# Line 3521  Int_t PamelaDBOperations::ValidateRuns(T Line 4192  Int_t PamelaDBOperations::ValidateRuns(T
4192        row = result->Next();        row = result->Next();
4193        t_stop = (UInt_t)atoll(row->GetField(4));        t_stop = (UInt_t)atoll(row->GetField(4));
4194      };      };
4195      if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);      if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
4196      // --------------------------------------------------------------      // --------------------------------------------------------------
4197      // now retrieves runs to be validated      // now retrieves runs to be validated
4198      // --------------------------------------------------------------      // --------------------------------------------------------------
# Line 3692  Int_t PamelaDBOperations::ValidateRuns(T Line 4363  Int_t PamelaDBOperations::ValidateRuns(T
4363                    }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);
4364            };            };
4365                        
4366            if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
4367            nrow++;            nrow++;
4368                        
4369    };    };
# Line 3721  Bool_t PamelaDBOperations::MissingTRK_CA Line 4392  Bool_t PamelaDBOperations::MissingTRK_CA
4392          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
4393          //==============================================================          //==============================================================
4394          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
4395                  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);
4396          //==============================================================          //==============================================================
4397          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
4398          // - MM full          // - MM full
# Line 3742  Bool_t PamelaDBOperations::MissingTRK_CA Line 4413  Bool_t PamelaDBOperations::MissingTRK_CA
4413          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
4414          //==============================================================          //==============================================================
4415          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
4416          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);
4417          return(false);          return(false);
4418                    
4419  };  };
# Line 4058  void PamelaDBOperations::ValidationOFF() Line 4729  void PamelaDBOperations::ValidationOFF()
4729        };        };
4730      };      };
4731    };    };
4732    if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);    if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4733  };  };
4734    
4735  /**  /**
# Line 4089  void PamelaDBOperations::RemoveRUNS(){ Line 4760  void PamelaDBOperations::RemoveRUNS(){
4760      if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");          if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4761      while ( Row ){      while ( Row ){
4762        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4763        if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));        if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4764        drun++;        drun++;
4765        Row = pResult->Next();            Row = pResult->Next();    
4766      };      };
# Line 4110  void PamelaDBOperations::RemoveRUNS(){ Line 4781  void PamelaDBOperations::RemoveRUNS(){
4781    } else {    } else {
4782      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4783      while ( Row ){      while ( Row ){
4784        if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));        if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4785        delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");        delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4786        if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));        if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4787        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4788        if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){        if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4789          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4790          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4791        };        };
4792        drun++;        drun++;
# Line 4141  void PamelaDBOperations::RemoveRUNS(){ Line 4812  void PamelaDBOperations::RemoveRUNS(){
4812    } else {    } else {
4813      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4814      while ( Row ){      while ( Row ){
4815        if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));        if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4816        myquery.str("");        myquery.str("");
4817        myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";              myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4818        conn->Query(myquery.str().c_str());        conn->Query(myquery.str().c_str());
# Line 4150  void PamelaDBOperations::RemoveRUNS(){ Line 4821  void PamelaDBOperations::RemoveRUNS(){
4821      };      };
4822    };    };
4823    //      //  
4824    if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);        if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);    
4825    //    //
4826    //    //
4827    //    //
# Line 4168  void PamelaDBOperations::RemoveRUNS(){ Line 4839  void PamelaDBOperations::RemoveRUNS(){
4839    } else {    } else {
4840      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4841      while ( Row ){      while ( Row ){
4842        if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));        if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4843        myquery.str("");        myquery.str("");
4844        myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";        myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4845        conn->Query(myquery.str().c_str());        conn->Query(myquery.str().c_str());
# Line 4177  void PamelaDBOperations::RemoveRUNS(){ Line 4848  void PamelaDBOperations::RemoveRUNS(){
4848      };      };
4849    };    };
4850    //      //  
4851    if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);        if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4852    //    //
4853    //    //
4854    //    //
# Line 4247  void PamelaDBOperations::RemoveCALIBS(){ Line 4918  void PamelaDBOperations::RemoveCALIBS(){
4918        //        //
4919      };      };
4920    };    };
4921      Bool_t OLDDB = false;
4922      for (Int_t section = 0; section < 4; section++){
4923        myquery.str("");
4924        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4925        myquery << " SECTION=" << section << ";";
4926        //
4927        pResult = conn->Query(myquery.str().c_str());
4928        //
4929        if ( conn->GetErrorCode() ){
4930          printf(" Section %i : warning, old databse structure no GL_CALOPULSE_CALIB table!\n",section);
4931          OLDDB=true;
4932        } else {
4933          Row = pResult->Next();      
4934          if( !Row->GetField(0) || !Row->GetField(1) ){
4935            //
4936            if ( IsDebug() ) printf(" NO PULSE CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4937            //
4938          } else {
4939            //
4940            myquery.str("");
4941            myquery << " UPDATE GL_CALOPULSE_CALIB SET TO_TIME=" << Row->GetField(1);
4942            myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4943            myquery << " SECTION=" << section << ";";
4944            //
4945            pResult = conn->Query(myquery.str().c_str());
4946            //
4947            if( !pResult ){
4948              //
4949              if ( IsDebug() ) printf(" ERROR DELETING CALO PULSE CALIBRATIONS \n");
4950              //
4951              throw -4;
4952              //
4953            };
4954            //
4955          };
4956        };
4957      };
4958    myquery.str("");    myquery.str("");
4959    myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();    myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
4960    //    //
4961    pResult = conn->Query(myquery.str().c_str());    pResult = conn->Query(myquery.str().c_str());
4962    //    //
# Line 4260  void PamelaDBOperations::RemoveCALIBS(){ Line 4968  void PamelaDBOperations::RemoveCALIBS(){
4968      //      //
4969    };    };
4970    //    //
4971      myquery.str("");
4972      myquery << " DELETE FROM GL_CALOPULSE_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
4973      //
4974      pResult = conn->Query(myquery.str().c_str());
4975      if ( IsDebug() ) printf(" Delete from GL_CALOPULSE_CALIB query is %s \n",myquery.str().c_str());
4976      if ( !OLDDB ){
4977        //
4978        if( !pResult ){
4979          //
4980          if ( IsDebug() ) printf(" ERROR DELETING PULSE CALO CALIBRATIONS \n");
4981          //
4982          throw -4;
4983          //
4984        };
4985      };
4986      //
4987    // Tracker    // Tracker
4988    //    //
4989    myquery.str("");    myquery.str("");
# Line 4289  void PamelaDBOperations::RemoveCALIBS(){ Line 5013  void PamelaDBOperations::RemoveCALIBS(){
5013      };      };
5014      //      //
5015      myquery.str("");      myquery.str("");
5016      myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();      myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
5017      //      //
5018      pResult = conn->Query(myquery.str().c_str());      pResult = conn->Query(myquery.str().c_str());
5019      //      //
# Line 4332  void PamelaDBOperations::RemoveCALIBS(){ Line 5056  void PamelaDBOperations::RemoveCALIBS(){
5056      };      };
5057      //      //
5058      myquery.str("");      myquery.str("");
5059      myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();      myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT() << ";";
5060      //      //
5061      pResult = conn->Query(myquery.str().c_str());      pResult = conn->Query(myquery.str().c_str());
5062      //      //
# Line 4346  void PamelaDBOperations::RemoveCALIBS(){ Line 5070  void PamelaDBOperations::RemoveCALIBS(){
5070      //      //
5071    };    };
5072  };  };
5073    
5074    /**
5075     *
5076     * Rearrange calibration tables
5077     *
5078     **/
5079    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
5080    
5081        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
5082        UInt_t timeaftercalib=120000; //2000;
5083    //  ----------
5084    //  Check CRCs
5085    //  ----------
5086        for(Int_t ipkt=0; ipkt<6; ipkt++){
5087            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
5088            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
5089        }
5090    //  -----------------------
5091    //  Check missing packets:
5092    //  -----------------------
5093    //    Readout order:
5094    //    ------------------
5095    //    DSP   packet board
5096    //    ------------------
5097    //    12    0      1
5098    //    10    1      1
5099    //     8    2      1
5100    //     4    3      1
5101    //     6    4      1
5102    //     2    5      1
5103    //    ------------------
5104    //    11    0      2
5105    //     9    1      2
5106    //     7    2      2
5107    //     3    3      2
5108    //     5    4      2
5109    //     1    5      2
5110    //    ------------------
5111    //  -------------------------------------------------
5112    //  Check if it is first or second calibration packet
5113    //  -------------------------------------------------
5114        UInt_t build=0;
5115        TString classname = caltrk->GetName();
5116        UInt_t base=0;
5117        UInt_t mask=0;
5118        if(classname.Contains("CalibTrk1Event")){
5119            base=12;
5120            mask=0x03F000;
5121        }
5122        if(classname.Contains("CalibTrk2Event")){
5123            base=18;
5124            mask=0xFC0000;
5125        }
5126    //  -------------------------------------------------
5127    //  Count number of packets and set build variable
5128    //  -------------------------------------------------
5129        Int_t  npkts=0;
5130        for(Int_t ipkt=0; ipkt<6; ipkt++){
5131            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
5132                npkts++;
5133                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
5134            }
5135        }
5136    //    if( npkts==6 )return 1; // :-)
5137    
5138    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
5139    
5140    //  -----------------------------------------------
5141    //  If missing packets: check the acq configuration
5142    //  (some DSPs might be excluded from acquisition)
5143    //  -----------------------------------------------
5144    
5145    //  -----------------------------------------------
5146    //  retrieve the first run header after calib
5147    //  -----------------------------------------------
5148         PacketType *pctp;
5149         EventCounter *cod;
5150         cod = eh->GetCounter();
5151         Int_t irun = cod->Get(pctp->RunHeader);
5152         TTree *rh=(TTree*)file->Get("RunHeader");
5153         if ( !rh || rh->IsZombie() ) throw -17;
5154         if( rh->GetEntries() == irun ){
5155              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
5156             return 0; // :-(
5157         }
5158    
5159         RunHeaderEvent *run  = 0;
5160         EventHeader    *hrun = 0;
5161         rh->SetBranchAddress("RunHeader", &run);
5162         rh->SetBranchAddress("Header", &hrun);
5163         rh->GetEntry(irun);
5164    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
5165    
5166         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
5167              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
5168             return 0; // :-(
5169         }
5170        
5171         if( !run->RM_ACQ_AFTER_CALIB ){
5172              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
5173             return 0; // :-(
5174         }
5175    
5176         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
5177         if( dtime > timeaftercalib ){
5178             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
5179             return 0; // :-(
5180         }
5181        
5182    
5183    
5184         if( (run->ACQ_BUILD_INFO & mask) != build ){
5185             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
5186             return 0; // :-(
5187         }
5188         return 1; // :-)
5189    
5190    }
5191    
5192    /**
5193     *
5194     * Check the DB (only for overlapping runs at the moment)
5195     *
5196     **/
5197    UInt_t PamelaDBOperations::Check(){
5198      //
5199      UInt_t test = 0;
5200      //
5201      UInt_t thisrht = 0;
5202      UInt_t thisrtt = 0;
5203      UInt_t thisid = 0;
5204      UInt_t prevrht = 0;
5205      UInt_t prevrtt = 0;
5206      UInt_t previd = 0;
5207      //
5208      UInt_t prevl0id = 0;
5209      UInt_t thisl0id = 0;
5210      //
5211      stringstream oss;
5212      TSQLResult *result = 0;
5213      TSQLRow    *row    = 0;
5214      TSQLResult *result2 = 0;
5215      TSQLRow    *row2    = 0;
5216      TSQLResult *result3 = 0;
5217      TSQLRow    *row3    = 0;
5218      oss.str("");
5219      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
5220      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
5221      result = conn->Query(oss.str().c_str());
5222      //
5223      if ( !result ) throw -4;;
5224      //
5225      row = result->Next();
5226      UInt_t nid = 0;
5227      //
5228      while ( row ){
5229        nid++;
5230        if ( !(nid%1000) && nid ) printf(" %iK run scanned \n",nid/1000);
5231        thisid = (UInt_t)atoll(row->GetField(0));
5232        thisl0id = (UInt_t)atoll(row->GetField(1));
5233        thisrht = (UInt_t)atoll(row->GetField(2));
5234        thisrtt = (UInt_t)atoll(row->GetField(3));
5235        //
5236        if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
5237          //
5238          //
5239          //
5240          oss.str("");
5241          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
5242              << thisid << " AND ( RUNHEADER_TIME="
5243              << thisrht << " OR RUNTRAILER_TIME="
5244              << thisrtt << " ) AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
5245          result3 = conn->Query(oss.str().c_str());
5246          if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
5247          if ( result3 ){
5248            //
5249            oss.str("");
5250            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
5251                << thisid << " AND RUNHEADER_TIME="
5252                << thisrht << " AND RUNTRAILER_TIME!="
5253                << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
5254            result3 = conn->Query(oss.str().c_str());
5255            if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
5256            if ( result3 ){
5257              row3 = result3->Next();
5258              //
5259              while ( row3 ){
5260                //
5261                // 2 runs with same runheader
5262                //
5263                printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));  
5264                row3 = result3->Next();
5265              };
5266            };
5267            //
5268            oss.str("");
5269            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
5270                << thisid << " AND RUNHEADER_TIME!="
5271                << thisrht << " AND RUNTRAILER_TIME="
5272                << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
5273            result3 = conn->Query(oss.str().c_str());
5274            if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
5275            if ( result3 ){
5276              row3 = result3->Next();
5277              //
5278              while ( row3 ){
5279                //
5280                // 2 runs with same runtrailer
5281                //
5282                printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
5283                row3 = result3->Next();
5284              };
5285            };
5286            //
5287            oss.str("");
5288            oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
5289                << thisid << " AND RUNHEADER_TIME="
5290                << thisrht << " AND RUNTRAILER_TIME="
5291                << thisrtt << " AND ID_RUN_FRAG!="
5292                << thisid << " order by RUNHEADER_TIME asc;";
5293            result3 = conn->Query(oss.str().c_str());
5294            if ( result3 ){
5295              row3 = result3->Next();
5296              //
5297              while ( row3 ){
5298                //
5299                // duplicated run
5300                //
5301                printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
5302                row3 = result3->Next();
5303              };
5304            };
5305          };
5306          //
5307          oss.str("");
5308          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
5309              << thisid << " AND RUNHEADER_TIME>"
5310              << thisrht << " AND RUNTRAILER_TIME<"
5311              << thisrtt << " order by RUNHEADER_TIME asc;";
5312          result3 = conn->Query(oss.str().c_str());
5313          if ( result3 ){
5314            row3 = result3->Next();
5315            //
5316            while ( row3 ){
5317              //
5318              // run contained in the checked one
5319              //
5320              printf(" CHECK n.6 RUN %u CONTAINS RUN %u  \n",thisid,(UInt_t)atoll(row3->GetField(0)));
5321              row3 = result3->Next();
5322            };
5323          };
5324          //
5325        };
5326        //
5327        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
5328        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
5329        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
5330          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
5331          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
5332          TString prevf = "";
5333          TString thisf = "";
5334          oss.str("");
5335          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
5336          result2 = conn->Query(oss.str().c_str());
5337          if ( !result2 ) throw -4;;
5338          row2 = result2->Next();
5339          prevf = (TString)row2->GetField(0);
5340          oss.str("");
5341          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
5342          result2 = conn->Query(oss.str().c_str());
5343          if ( !result2 ) throw -4;;
5344          row2 = result2->Next();
5345          thisf = (TString)row2->GetField(0);
5346          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
5347          test = 1;
5348        };
5349        //
5350        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
5351          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
5352          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
5353          TString prevf = "";
5354          TString thisf = "";
5355          oss.str("");
5356          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
5357          result2 = conn->Query(oss.str().c_str());
5358          if ( !result2 ) throw -4;
5359          row2 = result2->Next();
5360          prevf = (TString)row2->GetField(0);
5361          oss.str("");
5362          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
5363          result2 = conn->Query(oss.str().c_str());
5364          if ( !result2 ) throw -4;;
5365          row2 = result2->Next();
5366          thisf = (TString)row2->GetField(0);
5367          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
5368          test = 1;
5369        };
5370        //
5371        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
5372          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
5373          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
5374          TString prevf = "";
5375          TString thisf = "";
5376          oss.str("");
5377          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
5378          result2 = conn->Query(oss.str().c_str());
5379          if ( !result2 ) throw -4;;
5380          row2 = result2->Next();
5381          prevf = (TString)row2->GetField(0);
5382          oss.str("");
5383          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
5384          result2 = conn->Query(oss.str().c_str());
5385          if ( !result2 ) throw -4;;
5386          row2 = result2->Next();
5387          thisf = (TString)row2->GetField(0);
5388          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
5389          test = 1;
5390        };
5391    
5392        //
5393        prevrht = thisrht;
5394        prevrtt = thisrtt;
5395        previd = thisid;
5396        prevl0id = thisl0id;
5397        row = result->Next();
5398      };
5399      //
5400      return(test);
5401      //
5402    };

Legend:
Removed from v.1.20  
changed lines
  Added in v.1.52

  ViewVC Help
Powered by ViewVC 1.1.23