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

Diff of /YodaProfiler/src/PamelaDBOperations.cpp

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

revision 1.20 by mocchiut, Mon Nov 20 09:11:58 2006 UTC revision 1.29 by mocchiut, Tue Apr 17 12:07:01 2007 UTC
# 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 157  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 207  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 246  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 262  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 295  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 325  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 342  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 %i pkt_num %i \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(1);
408    //    //
409    if ( !nevent ) return(2);    if ( !nevent ) return(2);
410    //    //
411    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
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 375  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 %i upperobt %lld obtlast %u last entry is %i \n",jump,zomp,upperpkt,pktlast,upperobt,obtlast,i);
451            break;            break;
452          };          };
453          //          //
# Line 415  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 764  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    // set DB timezone to UTC
# Line 775  void PamelaDBOperations::CheckConnection Line 849  void PamelaDBOperations::CheckConnection
849    TSQLResult *result = 0;    TSQLResult *result = 0;
850    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
851    if ( !result ) throw -10;    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 > 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 ( obt < (pobtfirst/2) && pobtfirst > (numeric_limits<UInt_t>::max()/2) ) return((Long64_t)(obt+numeric_limits<UInt_t>::max()));
886    //    //
887    if ( obt > (obtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){    if ( obt > (pobtfirst*2) && obt > (numeric_limits<UInt_t>::max()/2) ){
888      if ( (obt-numeric_limits<UInt_t>::max()) < 0  ){      return((Long64_t)obt-(Long64_t)numeric_limits<UInt_t>::max());
       return((ULong64_t)(numeric_limits<UInt_t>::max()-obt));  
     } else {  
       return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));  
     };  
889    };    };
890    //    //
891    return((ULong64_t)obt);    return((Long64_t)obt);
892  };  };
893    
894  /**  /**
# Line 929  Int_t PamelaDBOperations::insertPamelaRa Line 1003  Int_t PamelaDBOperations::insertPamelaRa
1003    if ( idr ) return(1);    if ( idr ) return(1);
1004    //    //
1005    oss.str("");    oss.str("");
1006    oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"    if ( STATIC ){
1007        << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";      oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('"
1008            << this->GetRawPath().Data() << "', '" << this->GetRawFile().Data() << "')";
1009      } else {
1010        oss << "INSERT INTO GL_RAW (PATH, NAME) VALUES ('$PAM_RAW', '" << this->GetRawFile().Data() << "')";
1011      };
1012    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;    if ( conn->Query(oss.str().c_str()) == 0 ) throw -4;
1013    //    //
1014    idr =  this->SetID_RAW();    idr =  this->SetID_RAW();
# Line 993  Int_t PamelaDBOperations::insertPamelaGL Line 1071  Int_t PamelaDBOperations::insertPamelaGL
1071    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){    if ((row != NULL) && ((UInt_t)atoll(row->GetField(0)) > 0)){
1072      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);      if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1073      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;
1074        //
1075        tsync = (UInt_t)atoll(row->GetField(2));
1076        obt0 = (UInt_t)atoll(row->GetField(1));
1077        //
1078      return(1);      return(1);
1079    };    };
1080    //    //
# Line 1189  Int_t PamelaDBOperations::insertPamelaGL Line 1271  Int_t PamelaDBOperations::insertPamelaGL
1271    if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);    if ( IsDebug() ) printf(" found a timesync t0 is %u \n",t0);
1272    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1273    //    //
1274      tsync = TSYNC;
1275      obt0 = OBT;
1276      //
1277    delete result;    delete result;
1278    return(signal);    return(signal);
1279  }  }
# Line 1204  Int_t PamelaDBOperations::insertPamelaRo Line 1289  Int_t PamelaDBOperations::insertPamelaRo
1289    UInt_t idtimesync = 0;    UInt_t idtimesync = 0;
1290    //    //
1291    oss.str("");    oss.str("");
1292    oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "    if ( STATIC ){
1293        << " LEFT JOIN GL_ROOT "      oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1294        << " ON GL_RAW.ID = GL_ROOT.ID_RAW "          << " LEFT JOIN GL_ROOT "
1295        << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "          << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1296        << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";          << " WHERE GL_RAW.PATH = '" << this->GetRawPath().Data() << "' AND "
1297            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1298      } else {
1299        oss << " SELECT COUNT(GL_ROOT.ID_RAW),GL_RAW.ID,GL_ROOT.ID FROM GL_RAW "
1300            << " LEFT JOIN GL_ROOT "
1301            << " ON GL_RAW.ID = GL_ROOT.ID_RAW "
1302            << " WHERE GL_RAW.PATH = '$PAM_RAW' AND "
1303            << " GL_RAW.NAME = '" << this->GetRawFile().Data() << "' GROUP BY GL_RAW.ID ";
1304      };
1305    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1306    //    //
1307    if ( !result ) throw -12;    if ( !result ) throw -12;
# Line 1235  Int_t PamelaDBOperations::insertPamelaRo Line 1328  Int_t PamelaDBOperations::insertPamelaRo
1328    idtimesync = (UInt_t)atoll(row->GetField(0));    idtimesync = (UInt_t)atoll(row->GetField(0));
1329    //    //
1330    oss.str("");    oss.str("");
1331    oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"    if ( STATIC ){
1332        << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";      oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1333            << this->GetID_RAW() << "', '" << idtimesync << "', '" << this->GetRootPath().Data() << "', '" << this->GetRootFile().Data() << "')";
1334      } else {
1335        oss << "INSERT INTO GL_ROOT (ID_RAW, ID_TIMESYNC,PATH, NAME) VALUES ('"
1336            << this->GetID_RAW() << "', '" << idtimesync << "', '$PAM_L0', '" << this->GetRootFile().Data() << "')";
1337      };
1338    //    //
1339    if (conn->Query(oss.str().c_str()) == 0) throw -4;    if (conn->Query(oss.str().c_str()) == 0) throw -4;
1340    //    //
# Line 1263  Int_t PamelaDBOperations::assignBOOT_NUM Line 1361  Int_t PamelaDBOperations::assignBOOT_NUM
1361    TSQLResult *result = 0;    TSQLResult *result = 0;
1362    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
1363    oss.str("");    oss.str("");
1364    oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "    if ( STATIC ){
1365        << " PATH = '" << this->GetRawPath().Data() << "' AND "      oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1366        << " NAME = '" << this->GetRawFile().Data() << "' ";          << " PATH = '" << this->GetRawPath().Data() << "' AND "
1367            << " NAME = '" << this->GetRawFile().Data() << "' ";
1368      } else {
1369        oss << "SELECT ID, BOOT_NUMBER FROM GL_RAW WHERE "
1370            << " PATH = '$PAM_RAW' AND "
1371            << " NAME = '" << this->GetRawFile().Data() << "' ";
1372      };
1373    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1374    //    //
1375    if ( !result ) throw -4;;    if ( !result ) throw -4;;
# Line 1288  Int_t PamelaDBOperations::assignBOOT_NUM Line 1392  Int_t PamelaDBOperations::assignBOOT_NUM
1392    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1393    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1394    //    //
1395      Bool_t found = false;
1396    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1397    if ( trDumpEv->GetEntries() > 0 ){    if ( trDumpEv->GetEntries() > 0 ){
1398      Bool_t found = false;      found = false;
1399      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1400        trDumpEv->GetEntry(i);        trDumpEv->GetEntry(i);
1401        //      vde->Records->GetEntries();        //      vde->Records->GetEntries();
# Line 1307  Int_t PamelaDBOperations::assignBOOT_NUM Line 1412  Int_t PamelaDBOperations::assignBOOT_NUM
1412        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1413        //        //
1414      } else {      } else {
1415        if ( !this->GetBOOTnumber() ) return(4);        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(4);
1416        };
1417      } else {
1418        if ( !this->GetBOOTnumber() && !this->AutoBoot()) return(2);
1419      };
1420      //
1421      UInt_t bn = 0;
1422      Bool_t afound = false;
1423      if ( !found && this->AutoBoot()){
1424        afound = true;
1425        //
1426        // Search for other files with similar timesync
1427        //
1428        if ( IsDebug() ) printf(" tsync %u obt0 %u \n",tsync,obt0);
1429        UInt_t upperts = tsync-(obt0/1000)+5;
1430        UInt_t lowerts = tsync-(obt0/1000)-5;
1431        oss.str("");
1432        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)<"
1433            << upperts
1434            << " AND TIMESYNC-(OBT0/1000)>"
1435            << lowerts
1436            << " AND GL_RAW.BOOT_NUMBER>0 GROUP BY GL_TIMESYNC.OBT0;";
1437        result = conn->Query(oss.str().c_str());
1438        if ( IsDebug() ) printf(" Query the GL_TIMESYNC table to find boot number:\n %s \n",oss.str().c_str());
1439        //
1440        if ( !result ) throw -4;;
1441        found = true;
1442        if ( result->GetRowCount()<3 ){
1443          if ( IsDebug() ) printf(" AGH! no results!\n");
1444          found = false;
1445        } else {
1446          row = result->Next();      
1447          bn = (UInt_t)atoll(row->GetField(0));
1448          for ( Int_t r=1; r<result->GetRowCount() ;r++){
1449            if ( !row ) throw -4;
1450            if ( IsDebug() ) printf(" BOOT number is %s \n",row->GetField(0));
1451            if ( bn != (UInt_t)atoll(row->GetField(0)) ){
1452              if ( IsDebug() ) printf(" AGH! bn = %u here instead %u \n",bn,(UInt_t)atoll(row->GetField(0)));
1453              found = false;
1454            };
1455            row = result->Next();
1456          };
1457      };      };
1458      };
1459      //
1460      Int_t sgn = 0;
1461      //
1462      if ( !found && !BOOTNO ){
1463        throw -29;
1464    } else {    } else {
1465      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1466          this->SetBOOTnumber(bn);
1467          sgn = 8;
1468        };
1469    };    };
1470    //    //
1471    oss.str("");    oss.str("");
# Line 1320  Int_t PamelaDBOperations::assignBOOT_NUM Line 1475  Int_t PamelaDBOperations::assignBOOT_NUM
1475    conn->Query(oss.str().c_str());          conn->Query(oss.str().c_str());      
1476    //    //
1477    delete result;    delete result;
1478    return(0);    return(sgn);
1479  };  };
1480    
1481  /**  /**
# Line 1638  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1793  Bool_t PamelaDBOperations::IsRunAlreadyI
1793    delete result;    delete result;
1794    //    //
1795    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1796      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1797    return(signal);    return(signal);
1798  };  };
1799    
# Line 1751  void PamelaDBOperations::HandleRunFragme Line 1907  void PamelaDBOperations::HandleRunFragme
1907      //      //
1908    } else {    } else {
1909      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");
1910        return;
1911    };    };
1912    //    //
1913    //    //
# Line 2812  Int_t PamelaDBOperations::insertCALO_CAL Line 2969  Int_t PamelaDBOperations::insertCALO_CAL
2969            //            //
2970          } else {          } else {
2971            //            //
2972            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 %i obt %i pkt %i \n",section,fromtime,obt,pkt);
2973            //            //
2974          };                };      
2975          //                //      
# Line 3023  Int_t PamelaDBOperations::insertTRK_CALI Line 3180  Int_t PamelaDBOperations::insertTRK_CALI
3180      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3181      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3182      //      //
3183      valid = 1;  //     valid = 1;
3184      //  //     //
3185      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
3186      //      //
3187      //      //
3188      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) ){
3189        //        //
3190        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);        if ( IsDebug() ) printf(" Trk calibration1 at time %i obt %i pkt %i \n",fromtime,obt1,pkt1);
3191          //      
3192          valid = ValidateTrkCalib( caltrk1, eh1 );
3193          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3194        //        //
3195        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3196        //        //
# Line 3047  Int_t PamelaDBOperations::insertTRK_CALI Line 3207  Int_t PamelaDBOperations::insertTRK_CALI
3207            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3208            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3209            //            //
3210            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
3211            //                  //      
3212          } else {          } else {
3213            //            //
# Line 3078  Int_t PamelaDBOperations::insertTRK_CALI Line 3238  Int_t PamelaDBOperations::insertTRK_CALI
3238          //          //
3239          if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);          if ( IsDebug() ) printf(" The trk calibration2 at obt %i pkt %i t2 is %i is good \n",obt2,pkt2,t2);
3240          //          //
3241            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3242            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3243            valid = valid & valid2;
3244            //
3245          // Handle good calib          // Handle good calib
3246          //          //
3247          this->HandleTRK_CALIB(true,true);          this->HandleTRK_CALIB(true,true);
# Line 4346  void PamelaDBOperations::RemoveCALIBS(){ Line 4510  void PamelaDBOperations::RemoveCALIBS(){
4510      //      //
4511    };    };
4512  };  };
4513    
4514    /**
4515     *
4516     * Rearrange calibration tables
4517     *
4518     **/
4519    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4520    
4521        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4522        UInt_t timeaftercalib=120000; //2000;
4523    //  ----------
4524    //  Check CRCs
4525    //  ----------
4526        for(Int_t ipkt=0; ipkt<6; ipkt++){
4527            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4528            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4529        }
4530    //  -----------------------
4531    //  Check missing packets:
4532    //  -----------------------
4533    //    Readout order:
4534    //    ------------------
4535    //    DSP   packet board
4536    //    ------------------
4537    //    12    0      1
4538    //    10    1      1
4539    //     8    2      1
4540    //     4    3      1
4541    //     6    4      1
4542    //     2    5      1
4543    //    ------------------
4544    //    11    0      2
4545    //     9    1      2
4546    //     7    2      2
4547    //     3    3      2
4548    //     5    4      2
4549    //     1    5      2
4550    //    ------------------
4551    //  -------------------------------------------------
4552    //  Check if it is first or second calibration packet
4553    //  -------------------------------------------------
4554        UInt_t build=0;
4555        TString classname = caltrk->GetName();
4556        UInt_t base=0;
4557        UInt_t mask=0;
4558        if(classname.Contains("CalibTrk1Event")){
4559            base=12;
4560            mask=0x03F000;
4561        }
4562        if(classname.Contains("CalibTrk2Event")){
4563            base=18;
4564            mask=0xFC0000;
4565        }
4566    //  -------------------------------------------------
4567    //  Count number of packets and set build variable
4568    //  -------------------------------------------------
4569        Int_t  npkts=0;
4570        for(Int_t ipkt=0; ipkt<6; ipkt++){
4571            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4572                npkts++;
4573                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4574            }
4575        }
4576    //    if( npkts==6 )return 1; // :-)
4577    
4578    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4579    
4580    //  -----------------------------------------------
4581    //  If missing packets: check the acq configuration
4582    //  (some DSPs might be excluded from acquisition)
4583    //  -----------------------------------------------
4584    
4585    //  -----------------------------------------------
4586    //  retrieve the first run header after calib
4587    //  -----------------------------------------------
4588         PacketType *pctp;
4589         EventCounter *cod;
4590         cod = eh->GetCounter();
4591         Int_t irun = cod->Get(pctp->RunHeader);
4592         TTree *rh=(TTree*)file->Get("RunHeader");
4593         if ( !rh || rh->IsZombie() ) throw -17;
4594         if( rh->GetEntries() == irun ){
4595              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4596             return 0; // :-(
4597         }
4598    
4599         RunHeaderEvent *run  = 0;
4600         EventHeader    *hrun = 0;
4601         rh->SetBranchAddress("RunHeader", &run);
4602         rh->SetBranchAddress("Header", &hrun);
4603         rh->GetEntry(irun);
4604    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4605    
4606         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4607              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4608             return 0; // :-(
4609         }
4610        
4611         if( !run->RM_ACQ_AFTER_CALIB ){
4612              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4613             return 0; // :-(
4614         }
4615    
4616         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4617         if( dtime > timeaftercalib ){
4618             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4619             return 0; // :-(
4620         }
4621        
4622    
4623    
4624         if( (run->ACQ_BUILD_INFO & mask) != build ){
4625             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4626             return 0; // :-(
4627         }
4628         return 1; // :-)
4629    
4630    }
4631    
4632    /**
4633     *
4634     * Check the DB (only for overlapping runs at the moment)
4635     *
4636     **/
4637    UInt_t PamelaDBOperations::Check(){
4638      //
4639      UInt_t test = 0;
4640      //
4641      UInt_t thisrht = 0;
4642      UInt_t thisrtt = 0;
4643      UInt_t thisid = 0;
4644      UInt_t prevrht = 0;
4645      UInt_t prevrtt = 0;
4646      UInt_t previd = 0;
4647      //
4648      UInt_t prevl0id = 0;
4649      UInt_t thisl0id = 0;
4650      //
4651      stringstream oss;
4652      TSQLResult *result = 0;
4653      TSQLRow    *row    = 0;
4654      TSQLResult *result2 = 0;
4655      TSQLRow    *row2    = 0;
4656      oss.str("");
4657      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN order by RUNHEADER_TIME asc;";
4658      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4659      result = conn->Query(oss.str().c_str());
4660      //
4661      if ( !result ) throw -4;;
4662      //
4663      row = result->Next();
4664      //
4665      while ( row ){
4666        thisid = (UInt_t)atoll(row->GetField(0));
4667        thisl0id = (UInt_t)atoll(row->GetField(1));
4668        thisrht = (UInt_t)atoll(row->GetField(2));
4669        thisrtt = (UInt_t)atoll(row->GetField(3));
4670        //
4671        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4672        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4673        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4674          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4675          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4676          TString prevf = "";
4677          TString thisf = "";
4678          oss.str("");
4679          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4680          result2 = conn->Query(oss.str().c_str());
4681          if ( !result2 ) throw -4;;
4682          row2 = result2->Next();
4683          prevf = (TString)row2->GetField(0);
4684          oss.str("");
4685          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4686          result2 = conn->Query(oss.str().c_str());
4687          if ( !result2 ) throw -4;;
4688          row2 = result2->Next();
4689          thisf = (TString)row2->GetField(0);
4690          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4691          test = 1;
4692        };
4693        //
4694        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4695          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4696          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4697          TString prevf = "";
4698          TString thisf = "";
4699          oss.str("");
4700          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4701          result2 = conn->Query(oss.str().c_str());
4702          if ( !result2 ) throw -4;;
4703          row2 = result2->Next();
4704          prevf = (TString)row2->GetField(0);
4705          oss.str("");
4706          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4707          result2 = conn->Query(oss.str().c_str());
4708          if ( !result2 ) throw -4;;
4709          row2 = result2->Next();
4710          thisf = (TString)row2->GetField(0);
4711          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4712          test = 1;
4713        };
4714        //
4715        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4716          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4717          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4718          TString prevf = "";
4719          TString thisf = "";
4720          oss.str("");
4721          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4722          result2 = conn->Query(oss.str().c_str());
4723          if ( !result2 ) throw -4;;
4724          row2 = result2->Next();
4725          prevf = (TString)row2->GetField(0);
4726          oss.str("");
4727          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4728          result2 = conn->Query(oss.str().c_str());
4729          if ( !result2 ) throw -4;;
4730          row2 = result2->Next();
4731          thisf = (TString)row2->GetField(0);
4732          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4733          test = 1;
4734        };
4735    
4736        //
4737        prevrht = thisrht;
4738        prevrtt = thisrtt;
4739        previd = thisid;
4740        prevl0id = thisl0id;
4741        row = result->Next();
4742      };
4743      //
4744      return(test);
4745      //
4746    };

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

  ViewVC Help
Powered by ViewVC 1.1.23