/[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.16 by mocchiut, Tue Oct 31 15:36:04 2006 UTC revision 1.44 by mocchiut, Sun Jun 3 16:44:36 2007 UTC
# Line 14  Line 14 
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
17  #include <TDatime.h>  #include <TTimeStamp.h>
18  #include <TF1.h>  #include <TF1.h>
19  //  //
20  #include <EventHeader.h>  #include <EventHeader.h>
# Line 56  string getTleDatetime(cTle*); Line 56  string getTleDatetime(cTle*);
56   * @param debug        debug flag.   * @param debug        debug flag.
57   * @param tlefilename  ascii file with TLE 3 line elements.   * @param tlefilename  ascii file with TLE 3 line elements.
58   */   */
59  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, TString tlefilename, UInt_t dwinput, Bool_t staticp){
60    //    //
61      STATIC=false;
62      if ( staticp ) STATIC=true;
63    //    //
64    SetConnection(host,user,password);    SetConnection(host,user,password);
65    //    //
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 81  PamelaDBOperations::PamelaDBOperations(T
81    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
82    if( INSERT_ROOT ){    if( INSERT_ROOT ){
83      this->SetRootName(filerootname);      this->SetRootName(filerootname);
84      this->SetOrbitNo();      this->SetOrbitNo(dwinput);
85      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
86    } else {    } else {
87      this->SetRootName("");      this->SetRootName("");
# Line 112  void PamelaDBOperations::Close(){ Line 114  void PamelaDBOperations::Close(){
114  //  //
115  void PamelaDBOperations::CheckValidate(Long64_t olderthan){  void PamelaDBOperations::CheckValidate(Long64_t olderthan){
116    clean_time = new TDatime();    clean_time = new TDatime();
117      //
118    if(olderthan >= 0){    if(olderthan >= 0){
119      VALIDATE = true;      VALIDATE = true;
120      UInt_t timelim = 0;      UInt_t timelim = 0;
121      timelim =  (UInt_t)clean_time->Convert() - olderthan;      timelim =  (UInt_t)clean_time->Convert(true) - olderthan;
122      clean_time->Set(timelim,false);      clean_time->Set(timelim,false);
123    };    };
124  };  };
# Line 156  void PamelaDBOperations::SetDebugFlag(Bo Line 159  void PamelaDBOperations::SetDebugFlag(Bo
159  };  };
160    
161  /**  /**
162     * Set the autoboot flag
163     *
164     */
165    void PamelaDBOperations::SetAutoBoot(Bool_t dbg){
166      AUTOBOOT = dbg;
167    };
168    
169    /**
170   * Set the nofrag flag   * Set the nofrag flag
171   *   *
172   */   */
# Line 206  void PamelaDBOperations::SetRootName(TSt Line 217  void PamelaDBOperations::SetRootName(TSt
217  /**  /**
218   * Store the downlink orbit number from filename.   * Store the downlink orbit number from filename.
219   */   */
220  void PamelaDBOperations::SetOrbitNo(){  void PamelaDBOperations::SetOrbitNo(UInt_t dwinput){
221    dworbit = 0;    dworbit = 0;
222      //
223      if ( dwinput ){
224        dworbit = dwinput;
225        if ( IsDebug() ) printf(" Downlink orbit given by hand: %i  \n",dworbit);
226        return;
227      };
228      //
229    TString name = this->GetRootFile();    TString name = this->GetRootFile();
230    Int_t nlength = name.Length();    Int_t nlength = name.Length();
231    if ( nlength < 5 ) return;    if ( nlength < 5 ) return;
# Line 245  void PamelaDBOperations::SetTLEPath(TStr Line 263  void PamelaDBOperations::SetTLEPath(TStr
263    tlefilename = str;    tlefilename = str;
264  };  };
265    
266    TString PamelaDBOperations::GetRawPath(){
267      if ( STATIC ){
268        return((TString)gSystem->DirName(filerawname.Data())+'/');
269      } else {
270        return((TString)gSystem->ExpandPathName("$PAM_RAW")+'/');
271      };
272    };
273    
274    TString PamelaDBOperations::GetRootPath(){
275      if ( STATIC ){
276        return((TString)gSystem->DirName(filerootname.Data())+'/');
277      } else {
278        return((TString)gSystem->ExpandPathName("$PAM_L0")+'/');
279      };
280    };
281    
282  /**  /**
283   * Store the olderthan variable   * Store the olderthan variable
284   * @param olderthan   * @param olderthan
# Line 261  Bool_t PamelaDBOperations::SetID_RAW(){ Line 295  Bool_t PamelaDBOperations::SetID_RAW(){
295    TSQLResult *result = 0;    TSQLResult *result = 0;
296    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
297    oss.str("");    oss.str("");
298    oss << "SELECT ID FROM GL_RAW WHERE "    if ( STATIC ){
299        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID FROM GL_RAW WHERE "
300        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
301            << " NAME = '" << this->GetRawFile().Data() << "' ";  
302      } else {
303        oss << "SELECT ID FROM GL_RAW WHERE "
304            << " PATH = '$PAM_RAW' AND "
305            << " NAME = '" << this->GetRawFile().Data() << "' ";  
306      }
307    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
308    if ( result == NULL ) throw -4;    if ( result == NULL ) throw -4;
309    row = result->Next();    row = result->Next();
# Line 294  Int_t PamelaDBOperations::SetUpperLimits Line 334  Int_t PamelaDBOperations::SetUpperLimits
334    UInt_t nevent = 0;    UInt_t nevent = 0;
335    UInt_t pktlast = 0;    UInt_t pktlast = 0;
336    UInt_t obtlast = 0;    UInt_t obtlast = 0;
337    UInt_t t_pktlast = 0;    Long64_t t_pktlast = 0LL;
338    UInt_t t_obtlast = 0;    //  UInt_t t_obtlast = 0;
339    UInt_t upperpkt2 = 0;    Long64_t t_obtlast = 0LL;
340    ULong64_t upperobt2 = 0;    Long64_t upperpkt2 = 0LL;
341      Long64_t upperobt2 = 0LL;
342    UInt_t zomp = 0;    UInt_t zomp = 0;
343    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
344    EventCounter *code=0;    EventCounter *code=0;
345    //    //
346    UInt_t deltapkt = 5000;    Long64_t deltapkt = 5000LL;
347    ULong64_t deltaobt = 50000;    Long64_t deltaobt = 50000LL;
348    //    //
349    //   pcksList packetsNames;    pcksList packetsNames;
350    //   pcksList::iterator Iter;    pcksList::iterator Iter;
351    //   getPacketsNames(packetsNames);    getPacketsNames(packetsNames);
352    //    //
353    pktfirst = 0;    pktfirst = 0;
354    obtfirst = 0;    obtfirst = 0;
355      ppktfirst = 0;
356      pobtfirst = 0;
357    //    //
358    TTree *T = 0;    TTree *T = 0;
359    T = (TTree*)file->Get("Physics");    T = (TTree*)file->Get("Physics");
# Line 324  Int_t PamelaDBOperations::SetUpperLimits Line 367  Int_t PamelaDBOperations::SetUpperLimits
367    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
368    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
369    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
370      ppktfirst = pktfirst;
371      pobtfirst = obtfirst;
372    //    //
373    //   code = eh->GetCounter();    code = eh->GetCounter();
374    //   UInt_t en = 0;    UInt_t en = 0;
375    //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){    for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
376    //     en = code->Get(GetPacketType(*Iter));      en = code->Get(GetPacketType(*Iter));
377    //     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) ){
378    //};          if ( IsDebug() ) printf(" We have a calibration before the first physic packet: packet type is %s, entries: %i \n",*Iter,en);
379          //
380          TTree *TC = 0;
381          TC = (TTree*)file->Get("CalibCalPed");
382          if ( !TC || TC->IsZombie() ) throw -16;
383          EventHeader *ehc = 0;
384          PscuHeader *phc = 0;
385          TC->SetBranchAddress("Header", &ehc);
386          TC->GetEntry(0);
387          phc = ehc->GetPscuHeader();
388          pktfirst = phc->GetCounter();
389          obtfirst = phc->GetOrbitalTime();  
390          //
391        };
392      };  
393    //    //
394    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
395    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
# Line 341  Int_t PamelaDBOperations::SetUpperLimits Line 400  Int_t PamelaDBOperations::SetUpperLimits
400    upperobt = OBT(obtlast);    upperobt = OBT(obtlast);
401    upperentry = nevent-1;    upperentry = nevent-1;
402    //    //
403    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);
404    //    //
405    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);
406    //    //
407    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);
408    //    //
409    if ( !nevent ) return(2);    if ( !nevent ) return(64);
410    //    //
411    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(128);
412      if ( nevent < jump ) jump = 1;
413      //  if ( nevent < jump ) jump = int(nevent/10);
414      //  if ( !jump ) jump = 1;
415    //    //
416    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 ){
417      //      //
418        if ( IsDebug() ) printf(" starting jump %i \n",jump);
419      // go back      // go back
420      zomp = nevent - 2;      zomp = nevent - 2;
421      //      //
# Line 374  Int_t PamelaDBOperations::SetUpperLimits Line 437  Int_t PamelaDBOperations::SetUpperLimits
437          upperpkt2 = PKT(ph->GetCounter());          upperpkt2 = PKT(ph->GetCounter());
438          upperobt2 = OBT(ph->GetOrbitalTime());          upperobt2 = OBT(ph->GetOrbitalTime());
439          //          //
440          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) ){
441              if ( IsDebug() ) printf(" .-. upperpkt2 %lld upperobt2 %lld \n",upperpkt2,upperobt2);  
442              if ( IsDebug() ) printf(" .-. upperpkt %lld t_pktlast %lld upperobt %lld t_obtlast %lld \n",upperpkt,t_pktlast,upperobt,t_obtlast);    
443              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);    
444              throw -13;    
445            };
446          //          //
447          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 ){
448            zomp = i + jump + 1;            zomp = i + jump + 1;
449            if ( zomp > nevent-2 ) zomp = nevent - 2;            if ( zomp > nevent-2 ) zomp = nevent - 2;
450            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);
451            break;            break;
452          };          };
453          //          //
# Line 414  Int_t PamelaDBOperations::SetUpperLimits Line 482  Int_t PamelaDBOperations::SetUpperLimits
482    //    //
483    rhev = rh->GetEntries();    rhev = rh->GetEntries();
484    rtev = rt->GetEntries();    rtev = rt->GetEntries();
485    UInt_t sobtt = 0;    Long64_t sobtt = 0LL;
486    UInt_t sobth = 0;    Long64_t sobth = 0LL;
487    UInt_t spktt = 0;    Long64_t spktt = 0LL;
488    UInt_t spkth = 0;    Long64_t spkth = 0LL;
489    UInt_t pktt = 0;    Long64_t pktt = 0LL;
490    ULong64_t obtt = 0;    Long64_t obtt = 0LL;
491    UInt_t pkth = 0;    Long64_t pkth = 0LL;
492    ULong64_t obth = 0;    Long64_t obth = 0LL;
493    //    //
494    T->GetEntry(upperentry);    if ( rhev || rtev ){
495    code = eh->GetCounter();  
496    Int_t lasttrail = code->Get(pctp->RunTrailer);      T->GetEntry(upperentry);
497    Int_t lasthead = code->Get(pctp->RunHeader);      code = eh->GetCounter();
498    if ( lasttrail < rtev ){      Int_t lasttrail = code->Get(pctp->RunTrailer);
499      rt->GetEntry(lasttrail);      Int_t lasthead = code->Get(pctp->RunHeader);
500      pht = eht->GetPscuHeader();      if ( lasttrail < rtev ){
501      pktt = PKT(pht->GetCounter());        rt->GetEntry(lasttrail);
502      obtt = OBT(pht->GetOrbitalTime());        pht = eht->GetPscuHeader();
503    };        pktt = PKT(pht->GetCounter());
504    //        obtt = OBT(pht->GetOrbitalTime());
505    if ( lasthead < rhev ){      };
506      rh->GetEntry(lasthead);      //
507      phh = ehh->GetPscuHeader();      if ( lasthead < rhev ){
508      pkth = PKT(phh->GetCounter());        rh->GetEntry(lasthead);
509      obth = OBT(phh->GetOrbitalTime());        phh = ehh->GetPscuHeader();
510    };        pkth = PKT(phh->GetCounter());
511    //        obth = OBT(phh->GetOrbitalTime());
512    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);      };
513    if ( pkth > upperpkt && obth > upperobt ){      //
514      if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515      upperpkt = pkth;      if ( pkth > upperpkt && obth > upperobt ){
516      upperobt = obth;        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517      rhev = lasthead+1;        upperpkt = pkth;
518    } else {        upperobt = obth;
519      rhev = lasthead;        rhev = lasthead+1;
520    };      } else {
521    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);        rhev = lasthead;
522    //      };
523    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524    if ( pktt > upperpkt && obtt > upperobt ){      //
525      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526      upperpkt = pktt;      if ( pktt > upperpkt && obtt > upperobt ){
527      upperobt = obtt;        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528      rtev = lasttrail+1;        upperpkt = pktt;
529    } else {        upperobt = obtt;
530      rtev = lasttrail;        rtev = lasttrail+1;
531    };      } else {
532    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        rtev = lasttrail;
533    //  goto kikko;      };
534    //      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535    //      //  goto kikko;
536    // Check if runtrailer/runheader are within lower limits      //
537    //      //
538    //      // Check if runtrailer/runheader are within lower limits
539    pkth = 0;      //
540    obth = 0;      //
541    spkth = 0;      pkth = 0LL;
542    sobth = 0;      obth = 0LL;
543    for (Int_t k=0; k<rhev; k++){      spkth = 0LL;
544      if ( k > 0 ){      sobth = 0LL;
545        spkth = pkth;      for (Int_t k=0; k<rhev; k++){
546        sobth = obth;        if ( k > 0 ){
547      };          spkth = pkth;
548      rh->GetEntry(k);          sobth = obth;
549      phh = ehh->GetPscuHeader();        };
550      pkth = PKT(phh->GetCounter());        rh->GetEntry(k);
551      obth = OBT(phh->GetOrbitalTime());        phh = ehh->GetPscuHeader();
552      //        pkth = PKT(phh->GetCounter());
553  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);        obth = OBT(phh->GetOrbitalTime());
554      //        //
555      if ( pkth < spkth && obth < sobth ){        //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        //
557        //        if ( pkth < spkth && obth < sobth ){
558        rhev = k-1;          if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
559        rh->GetEntry(rhev);          //
560        pkth = spkth;          rhev = k-1;
561        obth = sobth;          rh->GetEntry(rhev);
562        //          pkth = spkth;
563        UInt_t evbefh = 0;          obth = sobth;
564        code = ehh->GetCounter();          //
565        evbefh = code->Get(pctp->Physics);              UInt_t evbefh = 0;
566        if ( evbefh >= 0 ){          code = ehh->GetCounter();
567          T->GetEntry(evbefh);          evbefh = code->Get(pctp->Physics);    
568          ph = eh->GetPscuHeader();          if ( evbefh >= 0 ){
569          t_pktlast = PKT(ph->GetCounter());            T->GetEntry(evbefh);
         t_obtlast = OBT(ph->GetOrbitalTime());  
         if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump  
           upperpkt = pkth;  
           upperobt = obth;  
           upperentry = evbefh-1;  
         } else {  
           while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){  
             evbefh++;    
             T->GetEntry(evbefh);  
             ph = eh->GetPscuHeader();  
             t_pktlast = PKT(ph->GetCounter());  
             t_obtlast = OBT(ph->GetOrbitalTime());  
           };  
           T->GetEntry(evbefh-1);  
570            ph = eh->GetPscuHeader();            ph = eh->GetPscuHeader();
571            upperpkt = PKT(ph->GetCounter());            t_pktlast = PKT(ph->GetCounter());
572            upperobt = OBT(ph->GetOrbitalTime());            t_obtlast = OBT(ph->GetOrbitalTime());
573            upperentry = evbefh-1;            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
574          };                  upperpkt = pkth;
575                upperobt = obth;
576                upperentry = evbefh-1;
577              } else {
578                while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
579                  evbefh++;
580                  T->GetEntry(evbefh);
581                  ph = eh->GetPscuHeader();
582                  t_pktlast = PKT(ph->GetCounter());
583                  t_obtlast = OBT(ph->GetOrbitalTime());
584                };
585                T->GetEntry(evbefh-1);
586                ph = eh->GetPscuHeader();
587                upperpkt = PKT(ph->GetCounter());
588                upperobt = OBT(ph->GetOrbitalTime());
589                upperentry = evbefh-1;
590              };    
591            };
592            if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
593            goto kikko0;
594        };        };
       if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
       goto kikko0;  
595      };      };
596    };    kikko0:
597   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());  
598      //      //
 //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  
599      //      //
600      if ( pktt < spktt && obtt < sobtt ){      pktt = 0LL;
601        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);      obtt = 0LL;
602        spktt = 0LL;
603        sobtt = 0LL;
604        for (Int_t k=0; k<rtev; k++){
605          if ( k > 0 ){
606            spktt = pktt;
607            sobtt = obtt;
608          };
609          rt->GetEntry(k);
610          pht = eht->GetPscuHeader();
611          pktt = PKT(pht->GetCounter());
612          obtt = OBT(pht->GetOrbitalTime());
613        //        //
614        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);  
615        //        //
616        UInt_t evbeft = 0;        if ( pktt < spktt && obtt < sobtt ){
617        code = eht->GetCounter();          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
618        evbeft = code->Get(pctp->Physics);              //
619        if ( evbeft >= 0 ){          rtev = k-1;
620          T->GetEntry(evbeft);          rt->GetEntry(rtev);
621          ph = eh->GetPscuHeader();          pktt = spktt;
622          t_pktlast = PKT(ph->GetCounter());          obtt = sobtt;
623          t_obtlast = OBT(ph->GetOrbitalTime());          if ( IsDebug() ) printf(" lasttrail %i pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
624          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump          //
625            upperpkt = pktt;          UInt_t evbeft = 0;
626            upperobt = obtt;          code = eht->GetCounter();
627            upperentry = evbeft-1;          evbeft = code->Get(pctp->Physics);    
628          } else {          if ( evbeft >= 0 ){
629            while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){            T->GetEntry(evbeft);
630              evbeft++;              ph = eh->GetPscuHeader();
631              T->GetEntry(evbeft);            t_pktlast = PKT(ph->GetCounter());
632              t_obtlast = OBT(ph->GetOrbitalTime());
633              if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
634                upperpkt = pktt;
635                upperobt = obtt;
636                upperentry = evbeft-1;
637              } else {
638                while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
639                  evbeft++;
640                  T->GetEntry(evbeft);
641                  ph = eh->GetPscuHeader();
642                  t_pktlast = PKT(ph->GetCounter());
643                  t_obtlast = OBT(ph->GetOrbitalTime());
644                };
645                T->GetEntry(evbeft-1);
646              ph = eh->GetPscuHeader();              ph = eh->GetPscuHeader();
647              t_pktlast = PKT(ph->GetCounter());              upperpkt = PKT(ph->GetCounter());
648              t_obtlast = OBT(ph->GetOrbitalTime());              upperobt = OBT(ph->GetOrbitalTime());
649                upperentry = evbeft-1;
650            };            };
           T->GetEntry(evbeft-1);  
           ph = eh->GetPscuHeader();  
           upperpkt = PKT(ph->GetCounter());  
           upperobt = OBT(ph->GetOrbitalTime());  
           upperentry = evbeft-1;  
651          };          };
652            if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
653            goto kikko;
654            //      break;
655            //
656        };        };
657        if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);        //  
       goto kikko;  
       //      break;  
       //  
658      };      };
659      //        //
660    };    kikko:
661    //      //
662   kikko:      T->GetEntry(upperentry);
663    //      code = eh->GetCounter();
664    T->GetEntry(upperentry);      lasttrail = code->Get(pctp->RunTrailer);
665    code = eh->GetCounter();      lasthead = code->Get(pctp->RunHeader);
666    lasttrail = code->Get(pctp->RunTrailer);      if ( lasttrail < rtev ){
667    lasthead = code->Get(pctp->RunHeader);        rt->GetEntry(lasttrail);
668    if ( lasttrail < rtev ){        pht = eht->GetPscuHeader();
669      rt->GetEntry(lasttrail);        pktt = PKT(pht->GetCounter());
670      pht = eht->GetPscuHeader();        obtt = OBT(pht->GetOrbitalTime());
671      pktt = PKT(pht->GetCounter());      };
672      obtt = OBT(pht->GetOrbitalTime());      //
673    };      if ( lasthead < rhev ){
674    //        rh->GetEntry(lasthead);
675    if ( lasthead < rhev ){        phh = ehh->GetPscuHeader();
676      rh->GetEntry(lasthead);        pkth = PKT(phh->GetCounter());
677      phh = ehh->GetPscuHeader();        obth = OBT(phh->GetOrbitalTime());
678      pkth = PKT(phh->GetCounter());      };
679      obth = OBT(phh->GetOrbitalTime());      //
680    };      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
681    //      if ( pkth > upperpkt && obth > upperobt ){
682    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);
683    if ( pkth > upperpkt && obth > upperobt ){        upperpkt = pkth;
684      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;
685      upperpkt = pkth;        rhev = lasthead+1;
686      upperobt = obth;      } else {
687      rhev = lasthead+1;        rhev = lasthead;
688    } else {      };
689      rhev = lasthead;      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
690    };      //
691    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);
692    //      if ( pktt > upperpkt && obtt > upperobt ){
693    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);
694    if ( pktt > upperpkt && obtt > upperobt ){        upperpkt = pktt;
695      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;
696      upperpkt = pktt;        rtev = lasttrail+1;
697      upperobt = obtt;      } else {
698      rtev = lasttrail+1;        rtev = lasttrail;
699    } else {      };
700      rtev = lasttrail;      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
701        //
702    };    };
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
703    //    //
704    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);
705    //    //
706    return(0);    return(0);
707  }  }
# Line 763  void PamelaDBOperations::CheckConnection Line 835  void PamelaDBOperations::CheckConnection
835    if( !conn ) throw -1;    if( !conn ) throw -1;
836    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
837    if( !connect ) throw -1;    if( !connect ) throw -1;
838      //
839      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());
840      //
841    if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;    if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
842      //
843      // set DB timezone to UTC
844      //
845      stringstream oss;
846      //  
847      oss.str("");
848      oss << "SET time_zone='+0:00';";
849      TSQLResult *result = 0;
850      result = conn->Query(oss.str().c_str());
851      if ( !result ) throw -10;
852      oss.str("");
853      oss << "SET wait_timeout=173000;";
854      conn->Query(oss.str().c_str());
855      //
856  };  };
857    
858  /**  /**
859   * Return the correct packet number if we went back to zero   * Return the correct packet number if we went back to zero
860   */   */
861  UInt_t PamelaDBOperations::PKT(UInt_t pkt_num){    Long64_t PamelaDBOperations::PKT(UInt_t pkt_num){  
862    //    //
863    //  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));
864    //    //
865    if ( pkt_num < (pktfirst/2) && pktfirst > (16777214/2)  ) return((pkt_num+16777215));    if ( pkt_num < (ppktfirst/2) && ppktfirst > (16777214/2)  ){
866        if ( IsDebug() ) printf(" rise up pktnum %lld \n",(Long64_t)pkt_num+16777215LL);
867        return((Long64_t)pkt_num+16777215LL);
868      };
869    //    //
870    if ( pkt_num > pktfirst*2 && pkt_num > (16777214/2) ){    if ( pkt_num > ((Long64_t)ppktfirst*2) && pkt_num > (16777214/2) ){
871      if ( (pkt_num-16777215) < 0 ){      if ( IsDebug() ) printf(" rise down pktnum %lld \n",(Long64_t)pkt_num-16777215LL);
872        return((16777215-pkt_num));      return((Long64_t)pkt_num-16777215LL);
     } else {  
       return((pkt_num-16777215));  
     };  
873    };    };
874    //    //
875    return(pkt_num);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)pkt_num);
876      return((Long64_t)pkt_num);
877    //    //
878  };  };
879    
880  /**  /**
881   * Return the correct On Board Time if we went back to zero   * Return the correct On Board Time if we went back to zero
882   */   */
883  ULong64_t PamelaDBOperations::OBT(UInt_t obt){    Long64_t PamelaDBOperations::OBT(UInt_t obt){  
884    //    //
885    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));
886    //    //
887    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ){
888      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      if ( IsDebug() ) printf(" rise up obt %lld \n",(Long64_t)(obt+numeric_limits<UInt_t>::max()));
889        return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));      return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
     } else {  
       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));  
     };  
890    };    };
891    //    //
892    return((ULong64_t)obt);    if ( obt > ((Long64_t)pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
893        if ( IsDebug() ) printf(" pobtfirst*2 %lld \n",((Long64_t)pobtfirst*2));
894        if ( IsDebug() ) printf(" rise down pktnum %lld \n", (Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
895        return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
896      };
897      //
898      if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
899      return((Long64_t)obt);
900  };  };
901    
902  /**  /**
# Line 917  Int_t PamelaDBOperations::insertPamelaRa Line 1011  Int_t PamelaDBOperations::insertPamelaRa
1011    if ( idr ) return(1);    if ( idr ) return(1);
1012    //    //
1013    oss.str("");    oss.str("");
1014    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1015        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1016            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1017      } else {
1018        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1019      };
1020    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1021    //    //
1022    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 937  Int_t PamelaDBOperations::insertPamelaGL Line 1035  Int_t PamelaDBOperations::insertPamelaGL
1035    TSQLResult *result = 0;    TSQLResult *result = 0;
1036    TSQLRow *row = 0;    TSQLRow *row = 0;
1037    UInt_t t0 = 0;    UInt_t t0 = 0;
1038      Int_t signal = 0;
1039      //
1040      signal = this->SetUpperLimits();
1041    //    //
1042    stringstream   oss;    stringstream   oss;
1043    //      //  
1044    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
1045    //    //
1046    oss.str("");    oss.str("");
1047    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"    oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
1048        << this->GetRawFile().Data() << "';";        << this->GetRawFile().Data() << "';";
1049    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1050    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 952  Int_t PamelaDBOperations::insertPamelaGL Line 1053  Int_t PamelaDBOperations::insertPamelaGL
1053    //    //
1054    if ( !row ){    if ( !row ){
1055      oss.str("");      oss.str("");
1056      oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "      oss << "SELECT YEAR(OFFSET_DATE),MONTH(OFFSET_DATE),DAY(OFFSET_DATE),HOUR(OFFSET_DATE),MINUTE(OFFSET_DATE),SECOND(OFFSET_DATE) FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
1057          << dworbit << " order by FROM_ORBIT desc limit 1;";          << dworbit << " order by FROM_ORBIT desc limit 1;";
1058      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
1059      result = conn->Query(oss.str().c_str());      result = conn->Query(oss.str().c_str());
# Line 961  Int_t PamelaDBOperations::insertPamelaGL Line 1062  Int_t PamelaDBOperations::insertPamelaGL
1062      if ( !row ) throw -10;      if ( !row ) throw -10;
1063    };    };
1064    //    //
1065    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    TTimeStamp tu = TTimeStamp((UInt_t)atoi(row->GetField(0)),(UInt_t)atoi(row->GetField(1)),(UInt_t)atoi(row->GetField(2)),(UInt_t)atoi(row->GetField(3)),(UInt_t)atoi(row->GetField(4)),(UInt_t)atoi(row->GetField(5)),0,true,0);
1066      t0 = (UInt_t)tu.GetSec();
1067      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));
1068      //
1069    /*    /*
1070     * Verify that the TIMESYNC have been not already processed     * Verify that the TIMESYNC have been not already processed
1071     */     */
# Line 976  Int_t PamelaDBOperations::insertPamelaGL Line 1080  Int_t PamelaDBOperations::insertPamelaGL
1080    if (result == NULL) throw -10;    if (result == NULL) throw -10;
1081    row = result->Next();    row = result->Next();
1082    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1083        if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1084      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;
1085        //
1086        tsync = (UInt_t)atoll(row->GetField(2));
1087        obt0 = (UInt_t)atoll(row->GetField(1));
1088        //
1089      return(1);      return(1);
1090    };    };
1091    //    //
1092    TTree *T = 0;    TTree *T = 0;
   Int_t signal = 0;  
1093    //    //
1094    UInt_t nevent = 0;    UInt_t nevent = 0;
1095    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 1067  Int_t PamelaDBOperations::insertPamelaGL Line 1175  Int_t PamelaDBOperations::insertPamelaGL
1175          rh->GetEntry(i);          rh->GetEntry(i);
1176          //          //
1177          TSYNC = runh->LAST_TIME_SYNC_INFO;          TSYNC = runh->LAST_TIME_SYNC_INFO;
1178          OBT = runh->OBT_TIME_SYNC * 1000;          OBT = runh->OBT_TIME_SYNC;
1179          //          //
1180          TYPE = 20;          TYPE = 20;
1181          //          //
# Line 1090  Int_t PamelaDBOperations::insertPamelaGL Line 1198  Int_t PamelaDBOperations::insertPamelaGL
1198          rt->GetEntry(i);          rt->GetEntry(i);
1199          //          //
1200          TSYNC = runt->LAST_TYME_SYNC_INFO;          TSYNC = runt->LAST_TYME_SYNC_INFO;
1201          OBT = runt->OBT_TYME_SYNC * 1000;          OBT = runt->OBT_TYME_SYNC;
1202          //          //
1203          TYPE = 21;          TYPE = 21;
1204          //          //
# Line 1170  Int_t PamelaDBOperations::insertPamelaGL Line 1278  Int_t PamelaDBOperations::insertPamelaGL
1278    conn->Query(oss.str().c_str());    conn->Query(oss.str().c_str());
1279    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());
1280    //    //
1281      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1282    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1283    //    //
1284      tsync = TSYNC;
1285      obt0 = OBT;
1286      //
1287    delete result;    delete result;
1288    return(signal);    return(signal);
1289  }  }
# Line 1187  Int_t PamelaDBOperations::insertPamelaRo Line 1299  Int_t PamelaDBOperations::insertPamelaRo
1299    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1300    //    //
1301    oss.str("");    oss.str("");
1302    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1303        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1304        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1305        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1306        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1307            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1308      } else {
1309        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1310            << " LEFT JOIN GL_ROOT "
1311            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1312            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1313            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1314      };
1315    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1316    //    //
1317    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1218  Int_t PamelaDBOperations::insertPamelaRo Line 1338  Int_t PamelaDBOperations::insertPamelaRo
1338    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1339    //    //
1340    oss.str("");    oss.str("");
1341    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1342        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1343            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1344      } else {
1345        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1346            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1347      };
1348    //    //
1349    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1350    //    //
# Line 1246  Int_t PamelaDBOperations::assignBOOT_NUM Line 1371  Int_t PamelaDBOperations::assignBOOT_NUM
1371    TSQLResult *result = 0;    TSQLResult *result = 0;
1372    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1373    oss.str("");    oss.str("");
1374    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1375        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1376        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1377            << " NAME = '" << this->GetRawFile().Data() << "' ";
1378      } else {
1379        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1380            << " PATH = '$PAM_RAW' AND "
1381            << " NAME = '" << this->GetRawFile().Data() << "' ";
1382      };
1383    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1384    //    //
1385    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1271  Int_t PamelaDBOperations::assignBOOT_NUM Line 1402  Int_t PamelaDBOperations::assignBOOT_NUM
1402    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1403    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1404    //    //
1405      Bool_t found = false;
1406    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1407    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1408      Bool_t found = false;      found = false;
1409      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1410        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1411        vde->Records->GetEntries();        //      vde->Records->GetEntries();
1412        if ( vde->Records->GetEntries()>0 ){        if ( vde->Records->GetEntries()>5 ){
1413          found = true;          found = true;
1414          goto fill;          goto fill;
1415        };        };
# Line 1290  Int_t PamelaDBOperations::assignBOOT_NUM Line 1422  Int_t PamelaDBOperations::assignBOOT_NUM
1422        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1423        //        //
1424      } else {      } else {
1425        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1426      };      };
1427    } else {    } else {
1428      if ( !this->GetBOOTnumber() ) return(2);      if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1429      };
1430      //
1431      UInt_t bn = 0;
1432      Bool_t afound = false;
1433      if ( !found && this->AutoBoot()){
1434        afound = true;
1435        //
1436        // Search for other files with similar timesync
1437        //
1438        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1439        UInt_t upperts = tsync-(obt0/1000)+5;
1440        UInt_t lowerts = tsync-(obt0/1000)-5;
1441        oss.str("");
1442        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)<"
1443            << upperts
1444            << " AND TIMESYNC-(OBT0/1000)>"
1445            << lowerts
1446            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1447        result = conn->Query(oss.str().c_str());
1448        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1449        //
1450        if ( !result ) throw -4;;
1451        found = true;
1452        if ( result->GetRowCount()<3 ){
1453          if ( IsDebug() ) printf(" AGH! no results!\n");
1454          found = false;
1455        } else {
1456          row = result->Next();      
1457          bn = (UInt_t)atoll(row->GetField(0));
1458          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1459            if ( !row ) throw -4;
1460            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1461            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1462              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1463              found = false;
1464            };
1465            row = result->Next();
1466          };
1467        };
1468      };
1469      //
1470      Int_t sgn = 0;
1471      //
1472      if ( !found && !BOOTNO ){
1473        throw -29;
1474      } else {
1475        if ( afound ){
1476          this->SetBOOTnumber(bn);
1477          sgn = 8;
1478        };
1479    };    };
1480    //    //
1481    oss.str("");    oss.str("");
# Line 1303  Int_t PamelaDBOperations::assignBOOT_NUM Line 1485  Int_t PamelaDBOperations::assignBOOT_NUM
1485    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1486    //    //
1487    delete result;    delete result;
1488    return(0);    return(sgn);
1489  };  };
1490    
1491  /**  /**
# Line 1316  Int_t PamelaDBOperations::insertPamelaRU Line 1498  Int_t PamelaDBOperations::insertPamelaRU
1498    stringstream oss;    stringstream oss;
1499    oss.str("");    oss.str("");
1500    //    //
1501    signal = this->SetUpperLimits();    //  signal = this->SetUpperLimits();
1502    //    //
1503    // loop on runheader and runtrailer events    // loop on runheader and runtrailer events
1504    //    //
# Line 1334  Int_t PamelaDBOperations::insertPamelaRU Line 1516  Int_t PamelaDBOperations::insertPamelaRU
1516    rt->SetBranchAddress("RunTrailer", &runt);    rt->SetBranchAddress("RunTrailer", &runt);
1517    rt->SetBranchAddress("Header", &eht);    rt->SetBranchAddress("Header", &eht);
1518    //    //
1519      TTree *T = (TTree*)file->Get("Physics");
1520      if ( !T || T->IsZombie() ) throw -16;
1521      EventHeader *eh = 0;
1522      T->SetBranchAddress("Header", &eh);
1523      //
1524      if ( !(rh->GetEntries()) && !(rt->GetEntries()) && !(T->GetEntries()) ) return(16);
1525      //
1526    UInt_t obtt = 0;    UInt_t obtt = 0;
1527    UInt_t obth = 0;    UInt_t obth = 0;
1528    UInt_t pktt = 0;    UInt_t pktt = 0;
# Line 1367  Int_t PamelaDBOperations::insertPamelaRU Line 1556  Int_t PamelaDBOperations::insertPamelaRU
1556        //        //
1557        if ( !ptt &&  !(ptht+1) ){        if ( !ptt &&  !(ptht+1) ){
1558          //          //
1559          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);
1560          //          //
1561          this->HandleRunFragments(true,false,0,(evbeft-1));          this->HandleRunFragments(true,false,0,(evbeft-1));
1562          //          //
1563          //          //
1564        } else if ( pth == ptht ){        } else if ( pth == ptht ){
1565          //          //
1566          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %u %u %u\n",ptht,pth,ptt);
1567          //          //
1568          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1569          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
# Line 1383  Int_t PamelaDBOperations::insertPamelaRU Line 1572  Int_t PamelaDBOperations::insertPamelaRU
1572          rt->GetEntry(ptt);          rt->GetEntry(ptt);
1573          pht = eht->GetPscuHeader();          pht = eht->GetPscuHeader();
1574          //          //
1575          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);
1576          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''  %u %u %u \n",pkth,obth,obtt);
1577          //          //
1578          this->HandleMissingHoT(true,false,evbefh,evbeft-1);          this->HandleMissingHoT(true,false,evbefh,evbeft-1);
1579          //          //
1580        } else {        } else {
1581          //          //
1582          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);
1583          //          //
1584          rh->GetEntry(ptht);          rh->GetEntry(ptht);
1585          phh = ehh->GetPscuHeader();          phh = ehh->GetPscuHeader();
# Line 1398  Int_t PamelaDBOperations::insertPamelaRU Line 1587  Int_t PamelaDBOperations::insertPamelaRU
1587          obth = phh->GetOrbitalTime();          obth = phh->GetOrbitalTime();
1588          cod = ehh->GetCounter();          cod = ehh->GetCounter();
1589          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
1590          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);          if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1591          //          //
1592          // handle this run          // handle this run
1593          //          //
# Line 1408  Int_t PamelaDBOperations::insertPamelaRU Line 1597  Int_t PamelaDBOperations::insertPamelaRU
1597          //          //
1598          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){          if ( PKT(pkth)>PKT(pktfirst) && OBT(obth)>OBT(obtfirst) && !ptt ){
1599            //            //
1600            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));  
1601            //            //
1602              if ( evbefh == 0 ) {
1603                //
1604                signal = 8;
1605                if ( IsDebug() ) printf(" Not supported yet: run with no events, no runtrailer, no runheader \n");
1606                //
1607              } else {
1608                //
1609                this->HandleRunFragments(true,true,0,(evbefh-1));
1610                //
1611              };
1612          };          };
1613          //          //
1614          //          //
1615          if ( (ptht - pth) > 1 ){          if ( (ptht - pth) > 1 ){
1616            //            //
1617            if ( IsDebug() ) printf(" Missing runtrailers! \n");            if ( IsDebug() ) printf(" Missing runtrailers! \n");
1618            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);
1619            // is not the consecutive header            // is not the consecutive header
1620            while ( pth != ptht ){                      while ( pth != ptht ){          
1621              //              //
# Line 1439  Int_t PamelaDBOperations::insertPamelaRU Line 1636  Int_t PamelaDBOperations::insertPamelaRU
1636              obth = phh->GetOrbitalTime();              obth = phh->GetOrbitalTime();
1637              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
1638              //              //
1639              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);
1640              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %i %i %i \n",pkth,obth,obtt);              if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' %u %u %u \n",pkth,obth,obtt);
1641              //              //
1642              this->HandleMissingHoT(false,true,evbefh,evbeft-1);              this->HandleMissingHoT(false,true,evbefh,evbeft-1);
1643              //              //
# Line 1449  Int_t PamelaDBOperations::insertPamelaRU Line 1646  Int_t PamelaDBOperations::insertPamelaRU
1646          } else if ( !(ptht - pth) ){          } else if ( !(ptht - pth) ){
1647            //            //
1648            if ( IsDebug() ) printf(" Missing runheader! \n");            if ( IsDebug() ) printf(" Missing runheader! \n");
1649            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);
1650            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");
1651            //            //
1652          } else {          } else {
# Line 1470  Int_t PamelaDBOperations::insertPamelaRU Line 1667  Int_t PamelaDBOperations::insertPamelaRU
1667            obth = phh->GetOrbitalTime();            obth = phh->GetOrbitalTime();
1668            cod = ehh->GetCounter();            cod = ehh->GetCounter();
1669            evbefh = cod->Get(pctp->Physics);            evbefh = cod->Get(pctp->Physics);
1670            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);
1671            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i %i %i \n",ptht,pth,ptt);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u %u %u \n",ptht,pth,ptt);
1672            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %i \n",rhev);            if ( IsDebug() ) printf(" ''''''''''''''''''''''''''''''' %u \n",rhev);
1673            //            //
1674            this->HandleRunFragments(false,true,evbefh,upperentry);            this->HandleRunFragments(false,true,evbefh,upperentry);
1675          } else {          } else {
# Line 1621  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1818  Bool_t PamelaDBOperations::IsRunAlreadyI
1818    delete result;    delete result;
1819    //    //
1820    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1821      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1822    return(signal);    return(signal);
1823  };  };
1824    
# Line 1734  void PamelaDBOperations::HandleRunFragme Line 1932  void PamelaDBOperations::HandleRunFragme
1932      //      //
1933    } else {    } else {
1934      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");
1935        return;
1936    };    };
1937    //    //
1938    //    //
# Line 1870  void PamelaDBOperations::HandleRunFragme Line 2069  void PamelaDBOperations::HandleRunFragme
2069        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2070        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2071        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2072        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);
2073        TTree *T= 0;        TTree *T= 0;
2074        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2075        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1884  void PamelaDBOperations::HandleRunFragme Line 2083  void PamelaDBOperations::HandleRunFragme
2083          bobt = OBT(ph->GetOrbitalTime());                bobt = OBT(ph->GetOrbitalTime());      
2084          firstev++;          firstev++;
2085        };        };
2086        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);
2087        //        //
2088        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2089        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
# Line 2067  void PamelaDBOperations::HandleRunFragme Line 2266  void PamelaDBOperations::HandleRunFragme
2266        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2267        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2268        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2269        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);
2270        TTree *T= 0;        TTree *T= 0;
2271        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2272        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 2081  void PamelaDBOperations::HandleRunFragme Line 2280  void PamelaDBOperations::HandleRunFragme
2280          aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());      
2281          lastev--;          lastev--;
2282        };        };
2283        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);
2284        //        //
2285        glrun->SetEV_TO(lastev);        glrun->SetEV_TO(lastev);
2286        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
# Line 2688  Int_t PamelaDBOperations::insertCALO_CAL Line 2887  Int_t PamelaDBOperations::insertCALO_CAL
2887          fromtime = this->GetAbsTime(ph->GetOrbitalTime());            fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
2888          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) ){
2889            //            //
2890            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);
2891            //            //
2892            // check if the calibration has already been inserted            // check if the calibration has already been inserted
2893            //            //
# Line 2732  Int_t PamelaDBOperations::insertCALO_CAL Line 2931  Int_t PamelaDBOperations::insertCALO_CAL
2931                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2932                //                //
2933                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);
2934                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2935                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2936                  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
2937                };                };
2938                //                //
# Line 2795  Int_t PamelaDBOperations::insertCALO_CAL Line 2994  Int_t PamelaDBOperations::insertCALO_CAL
2994            //            //
2995          } else {          } else {
2996            //            //
2997            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);
2998            //            //
2999          };                };      
3000          //                //      
# Line 2877  void PamelaDBOperations::HandleTRK_CALIB Line 3076  void PamelaDBOperations::HandleTRK_CALIB
3076        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
3077        //        //
3078        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");
3079        if ( fromtime < 1150863400 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode        if ( fromtime < 1150871000 ) fromtime = 0; // the first flight calibration was taken at about 1150863300 s, this line allows to analyze first runs in raw mode
3080        //        //
3081        oss.str("");        oss.str("");
3082        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3006  Int_t PamelaDBOperations::insertTRK_CALI Line 3205  Int_t PamelaDBOperations::insertTRK_CALI
3205      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3206      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3207      //      //
3208      valid = 1;  //     valid = 1;
3209      //  //     //
3210      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
3211      //      //
3212      //      //
3213      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) ){
3214        //        //
3215        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);
3216          //      
3217          valid = ValidateTrkCalib( caltrk1, eh1 );
3218          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3219        //        //
3220        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3221        //        //
# Line 3030  Int_t PamelaDBOperations::insertTRK_CALI Line 3232  Int_t PamelaDBOperations::insertTRK_CALI
3232            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3233            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3234            //            //
3235            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
3236            //                  //      
3237          } else {          } else {
3238            //            //
# Line 3053  Int_t PamelaDBOperations::insertTRK_CALI Line 3255  Int_t PamelaDBOperations::insertTRK_CALI
3255          //          //
3256        };        };
3257        //        //
3258        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);
3259        //        //
3260        // The calibration is good        // The calibration is good
3261        //        //
3262        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3263          //          //
3264          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);
3265            //
3266            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3267            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3268            valid = valid & valid2;
3269          //          //
3270          // Handle good calib          // Handle good calib
3271          //          //
# Line 3069  Int_t PamelaDBOperations::insertTRK_CALI Line 3275  Int_t PamelaDBOperations::insertTRK_CALI
3275          //          //
3276          if ( t2 != pret2+1 ){          if ( t2 != pret2+1 ){
3277            //            //
3278            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);
3279            //            //
3280            while ( t2 > pret2+1 ){            while ( t2 > pret2+1 ){
3281              //              //
# Line 3098  Int_t PamelaDBOperations::insertTRK_CALI Line 3304  Int_t PamelaDBOperations::insertTRK_CALI
3304          //          //
3305          // Check for missing calibtrk2          // Check for missing calibtrk2
3306          //          //
3307          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);
3308          t2 = pret2;          t2 = pret2;
3309          //          //
3310          // handle missing calib2          // handle missing calib2
# Line 3112  Int_t PamelaDBOperations::insertTRK_CALI Line 3318  Int_t PamelaDBOperations::insertTRK_CALI
3318        //        //
3319      } else {      } else {
3320        //        //
3321        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);
3322        //        //
3323      };      };
3324      //          //    
# Line 3137  Int_t PamelaDBOperations::insertTRK_CALI Line 3343  Int_t PamelaDBOperations::insertTRK_CALI
3343      valid = 0;      valid = 0;
3344      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) ){
3345        //        //
3346        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);
3347        //        //
3348        this->HandleTRK_CALIB(false,true);        this->HandleTRK_CALIB(false,true);
3349        //        //
# Line 3191  Int_t PamelaDBOperations::insertS4_CALIB Line 3397  Int_t PamelaDBOperations::insertS4_CALIB
3397      fromtime = this->GetAbsTime(ph->GetOrbitalTime());        fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3398      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) ){
3399        //        //
3400        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);
3401        //        //
3402        // check if the calibration has already been inserted        // check if the calibration has already been inserted
3403        //        //
# Line 3233  Int_t PamelaDBOperations::insertS4_CALIB Line 3439  Int_t PamelaDBOperations::insertS4_CALIB
3439            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3440            //            //
3441            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");
3442            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3443              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3444              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
3445            };            };
3446            //            //
# Line 3293  Int_t PamelaDBOperations::insertS4_CALIB Line 3499  Int_t PamelaDBOperations::insertS4_CALIB
3499        //        //
3500      } else {      } else {
3501        //        //
3502        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);
3503        //        //
3504      };            };      
3505      //            //      
# Line 3504  Int_t PamelaDBOperations::ValidateRuns(T Line 3710  Int_t PamelaDBOperations::ValidateRuns(T
3710        row = result->Next();        row = result->Next();
3711        t_stop = (UInt_t)atoll(row->GetField(4));        t_stop = (UInt_t)atoll(row->GetField(4));
3712      };      };
3713      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);
3714      // --------------------------------------------------------------      // --------------------------------------------------------------
3715      // now retrieves runs to be validated      // now retrieves runs to be validated
3716      // --------------------------------------------------------------      // --------------------------------------------------------------
# Line 3675  Int_t PamelaDBOperations::ValidateRuns(T Line 3881  Int_t PamelaDBOperations::ValidateRuns(T
3881                    }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);
3882            };            };
3883                        
3884            if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3885            nrow++;            nrow++;
3886                        
3887    };    };
# Line 3704  Bool_t PamelaDBOperations::MissingTRK_CA Line 3910  Bool_t PamelaDBOperations::MissingTRK_CA
3910          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3911          //==============================================================          //==============================================================
3912          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3913                  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);
3914          //==============================================================          //==============================================================
3915          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3916          // - MM full          // - MM full
# Line 3725  Bool_t PamelaDBOperations::MissingTRK_CA Line 3931  Bool_t PamelaDBOperations::MissingTRK_CA
3931          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3932          //==============================================================          //==============================================================
3933          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3934          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);
3935          return(false);          return(false);
3936                    
3937  };  };
# Line 4041  void PamelaDBOperations::ValidationOFF() Line 4247  void PamelaDBOperations::ValidationOFF()
4247        };        };
4248      };      };
4249    };    };
4250    if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);    if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4251  };  };
4252    
4253  /**  /**
# Line 4072  void PamelaDBOperations::RemoveRUNS(){ Line 4278  void PamelaDBOperations::RemoveRUNS(){
4278      if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");          if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4279      while ( Row ){      while ( Row ){
4280        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4281        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)));
4282        drun++;        drun++;
4283        Row = pResult->Next();            Row = pResult->Next();    
4284      };      };
# Line 4093  void PamelaDBOperations::RemoveRUNS(){ Line 4299  void PamelaDBOperations::RemoveRUNS(){
4299    } else {    } else {
4300      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4301      while ( Row ){      while ( Row ){
4302        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)));
4303        delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");        delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4304        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)));
4305        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");        delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4306        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)) ){
4307          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)));
4308          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4309        };        };
4310        drun++;        drun++;
# Line 4124  void PamelaDBOperations::RemoveRUNS(){ Line 4330  void PamelaDBOperations::RemoveRUNS(){
4330    } else {    } else {
4331      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4332      while ( Row ){      while ( Row ){
4333        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)));
4334        myquery.str("");        myquery.str("");
4335        myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";              myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4336        conn->Query(myquery.str().c_str());        conn->Query(myquery.str().c_str());
# Line 4133  void PamelaDBOperations::RemoveRUNS(){ Line 4339  void PamelaDBOperations::RemoveRUNS(){
4339      };      };
4340    };    };
4341    //      //  
4342    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);    
4343    //    //
4344    //    //
4345    //    //
# Line 4151  void PamelaDBOperations::RemoveRUNS(){ Line 4357  void PamelaDBOperations::RemoveRUNS(){
4357    } else {    } else {
4358      if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");          if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4359      while ( Row ){      while ( Row ){
4360        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)));
4361        myquery.str("");        myquery.str("");
4362        myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";        myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4363        conn->Query(myquery.str().c_str());        conn->Query(myquery.str().c_str());
# Line 4160  void PamelaDBOperations::RemoveRUNS(){ Line 4366  void PamelaDBOperations::RemoveRUNS(){
4366      };      };
4367    };    };
4368    //      //  
4369    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);    
4370    //    //
4371    //    //
4372    //    //
# Line 4329  void PamelaDBOperations::RemoveCALIBS(){ Line 4535  void PamelaDBOperations::RemoveCALIBS(){
4535      //      //
4536    };    };
4537  };  };
4538    
4539    /**
4540     *
4541     * Rearrange calibration tables
4542     *
4543     **/
4544    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4545    
4546        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4547        UInt_t timeaftercalib=120000; //2000;
4548    //  ----------
4549    //  Check CRCs
4550    //  ----------
4551        for(Int_t ipkt=0; ipkt<6; ipkt++){
4552            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4553            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4554        }
4555    //  -----------------------
4556    //  Check missing packets:
4557    //  -----------------------
4558    //    Readout order:
4559    //    ------------------
4560    //    DSP   packet board
4561    //    ------------------
4562    //    12    0      1
4563    //    10    1      1
4564    //     8    2      1
4565    //     4    3      1
4566    //     6    4      1
4567    //     2    5      1
4568    //    ------------------
4569    //    11    0      2
4570    //     9    1      2
4571    //     7    2      2
4572    //     3    3      2
4573    //     5    4      2
4574    //     1    5      2
4575    //    ------------------
4576    //  -------------------------------------------------
4577    //  Check if it is first or second calibration packet
4578    //  -------------------------------------------------
4579        UInt_t build=0;
4580        TString classname = caltrk->GetName();
4581        UInt_t base=0;
4582        UInt_t mask=0;
4583        if(classname.Contains("CalibTrk1Event")){
4584            base=12;
4585            mask=0x03F000;
4586        }
4587        if(classname.Contains("CalibTrk2Event")){
4588            base=18;
4589            mask=0xFC0000;
4590        }
4591    //  -------------------------------------------------
4592    //  Count number of packets and set build variable
4593    //  -------------------------------------------------
4594        Int_t  npkts=0;
4595        for(Int_t ipkt=0; ipkt<6; ipkt++){
4596            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4597                npkts++;
4598                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4599            }
4600        }
4601    //    if( npkts==6 )return 1; // :-)
4602    
4603    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4604    
4605    //  -----------------------------------------------
4606    //  If missing packets: check the acq configuration
4607    //  (some DSPs might be excluded from acquisition)
4608    //  -----------------------------------------------
4609    
4610    //  -----------------------------------------------
4611    //  retrieve the first run header after calib
4612    //  -----------------------------------------------
4613         PacketType *pctp;
4614         EventCounter *cod;
4615         cod = eh->GetCounter();
4616         Int_t irun = cod->Get(pctp->RunHeader);
4617         TTree *rh=(TTree*)file->Get("RunHeader");
4618         if ( !rh || rh->IsZombie() ) throw -17;
4619         if( rh->GetEntries() == irun ){
4620              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4621             return 0; // :-(
4622         }
4623    
4624         RunHeaderEvent *run  = 0;
4625         EventHeader    *hrun = 0;
4626         rh->SetBranchAddress("RunHeader", &run);
4627         rh->SetBranchAddress("Header", &hrun);
4628         rh->GetEntry(irun);
4629    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4630    
4631         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4632              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4633             return 0; // :-(
4634         }
4635        
4636         if( !run->RM_ACQ_AFTER_CALIB ){
4637              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4638             return 0; // :-(
4639         }
4640    
4641         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4642         if( dtime > timeaftercalib ){
4643             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4644             return 0; // :-(
4645         }
4646        
4647    
4648    
4649         if( (run->ACQ_BUILD_INFO & mask) != build ){
4650             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4651             return 0; // :-(
4652         }
4653         return 1; // :-)
4654    
4655    }
4656    
4657    /**
4658     *
4659     * Check the DB (only for overlapping runs at the moment)
4660     *
4661     **/
4662    UInt_t PamelaDBOperations::Check(){
4663      //
4664      UInt_t test = 0;
4665      //
4666      UInt_t thisrht = 0;
4667      UInt_t thisrtt = 0;
4668      UInt_t thisid = 0;
4669      UInt_t prevrht = 0;
4670      UInt_t prevrtt = 0;
4671      UInt_t previd = 0;
4672      //
4673      UInt_t prevl0id = 0;
4674      UInt_t thisl0id = 0;
4675      //
4676      stringstream oss;
4677      TSQLResult *result = 0;
4678      TSQLRow    *row    = 0;
4679      TSQLResult *result2 = 0;
4680      TSQLRow    *row2    = 0;
4681      oss.str("");
4682      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4683      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4684      result = conn->Query(oss.str().c_str());
4685      //
4686      if ( !result ) throw -4;;
4687      //
4688      row = result->Next();
4689      //
4690      while ( row ){
4691        thisid = (UInt_t)atoll(row->GetField(0));
4692        thisl0id = (UInt_t)atoll(row->GetField(1));
4693        thisrht = (UInt_t)atoll(row->GetField(2));
4694        thisrtt = (UInt_t)atoll(row->GetField(3));
4695        //
4696        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4697        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4698        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4699          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4700          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4701          TString prevf = "";
4702          TString thisf = "";
4703          oss.str("");
4704          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4705          result2 = conn->Query(oss.str().c_str());
4706          if ( !result2 ) throw -4;;
4707          row2 = result2->Next();
4708          prevf = (TString)row2->GetField(0);
4709          oss.str("");
4710          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4711          result2 = conn->Query(oss.str().c_str());
4712          if ( !result2 ) throw -4;;
4713          row2 = result2->Next();
4714          thisf = (TString)row2->GetField(0);
4715          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4716          test = 1;
4717        };
4718        //
4719        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4720          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4721          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4722          TString prevf = "";
4723          TString thisf = "";
4724          oss.str("");
4725          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4726          result2 = conn->Query(oss.str().c_str());
4727          if ( !result2 ) throw -4;;
4728          row2 = result2->Next();
4729          prevf = (TString)row2->GetField(0);
4730          oss.str("");
4731          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4732          result2 = conn->Query(oss.str().c_str());
4733          if ( !result2 ) throw -4;;
4734          row2 = result2->Next();
4735          thisf = (TString)row2->GetField(0);
4736          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4737          test = 1;
4738        };
4739        //
4740        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4741          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4742          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4743          TString prevf = "";
4744          TString thisf = "";
4745          oss.str("");
4746          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4747          result2 = conn->Query(oss.str().c_str());
4748          if ( !result2 ) throw -4;;
4749          row2 = result2->Next();
4750          prevf = (TString)row2->GetField(0);
4751          oss.str("");
4752          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4753          result2 = conn->Query(oss.str().c_str());
4754          if ( !result2 ) throw -4;;
4755          row2 = result2->Next();
4756          thisf = (TString)row2->GetField(0);
4757          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4758          test = 1;
4759        };
4760    
4761        //
4762        prevrht = thisrht;
4763        prevrtt = thisrtt;
4764        previd = thisid;
4765        prevl0id = thisl0id;
4766        row = result->Next();
4767      };
4768      //
4769      return(test);
4770      //
4771    };

Legend:
Removed from v.1.16  
changed lines
  Added in v.1.44

  ViewVC Help
Powered by ViewVC 1.1.23