/[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.13 by mocchiut, Fri Oct 20 11:11:12 2006 UTC revision 1.47 by mocchiut, Fri Aug 31 08:51:21 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 31  Line 31 
31  #include <varDump/VarDumpRecord.h>  #include <varDump/VarDumpRecord.h>
32  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
33  //  //
34  #include <cTle.h>  #include <sgp4.h>
 #include <cEci.h>  
 #include <cJulian.h>  
35    
36  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
37  //  //
# Line 58  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 81  PamelaDBOperations::PamelaDBOperations(T Line 81  PamelaDBOperations::PamelaDBOperations(T
81    INSERT_ROOT = !filerootname.IsNull();    INSERT_ROOT = !filerootname.IsNull();
82    if( INSERT_ROOT ){    if( INSERT_ROOT ){
83      this->SetRootName(filerootname);      this->SetRootName(filerootname);
84      this->SetOrbitNo();      this->SetOrbitNo(dwinput);
85      file = TFile::Open(this->GetRootName().Data());      file = TFile::Open(this->GetRootName().Data());
86      } else {
87        this->SetRootName("");
88    };    };
89    //    //
90    this->SetID_RAW(0);    this->SetID_RAW(0);
# Line 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
171     *
172     */
173    void PamelaDBOperations::SetNoFrag(Bool_t nf){
174      NOFRAG = nf;
175    };
176    
177    /**
178   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
179   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
180   */   */
# Line 198  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 237  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 253  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 286  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 316  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 333  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 366  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 406  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;
   //  
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
   if ( lasttrail < rtev ){  
     rt->GetEntry(lasttrail);  
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
   };  
   //  
   if ( lasthead < rhev ){  
     rh->GetEntry(lasthead);  
     phh = ehh->GetPscuHeader();  
     pkth = PKT(phh->GetCounter());  
     obth = OBT(phh->GetOrbitalTime());  
   };  
   //  
   if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
   if ( pkth > upperpkt && obth > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);  
     upperpkt = pkth;  
     upperobt = obth;  
     rhev = lasthead+1;  
   } else {  
     rhev = lasthead;  
   };  
   if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
   //  
   if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
   if ( pktt > upperpkt && obtt > upperobt ){  
     if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);  
     upperpkt = pktt;  
     upperobt = obtt;  
     rtev = lasttrail+1;  
   } else {  
     rtev = lasttrail;  
   };  
   if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
   //  goto kikko;  
   //  
   //  
   // Check if runtrailer/runheader are within lower limits  
   //  
493    //    //
494    pkth = 0;    if ( rhev || rtev ){
495    obth = 0;  
496    spkth = 0;      T->GetEntry(upperentry);
497    sobth = 0;      code = eh->GetCounter();
498    for (Int_t k=0; k<rhev; k++){      Int_t lasttrail = code->Get(pctp->RunTrailer);
499      if ( k > 0 ){      Int_t lasthead = code->Get(pctp->RunHeader);
500        spkth = pkth;      if ( lasttrail < rtev ){
501        sobth = obth;        rt->GetEntry(lasttrail);
502      };        pht = eht->GetPscuHeader();
503      rh->GetEntry(k);        pktt = PKT(pht->GetCounter());
504      phh = ehh->GetPscuHeader();        obtt = OBT(pht->GetOrbitalTime());
505      pkth = PKT(phh->GetCounter());      };
506      obth = OBT(phh->GetOrbitalTime());      //
507      //      if ( lasthead < rhev ){
508  //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);        rh->GetEntry(lasthead);
509      //        phh = ehh->GetPscuHeader();
510      if ( pkth < spkth && obth < sobth ){        pkth = PKT(phh->GetCounter());
511        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        obth = OBT(phh->GetOrbitalTime());
512        //      };
513        rhev = k-1;      //
514        rh->GetEntry(rhev);      if ( IsDebug() ) printf(" rhev before %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
515        pkth = spkth;      if ( pkth > upperpkt && obth > upperobt ){
516        obth = sobth;        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %lld upperp %lld oh %lld uppero %lld \n",pkth,upperpkt,obth,upperobt);
517        //        upperpkt = pkth;
518        UInt_t evbefh = 0;        upperobt = obth;
519        code = ehh->GetCounter();        rhev = lasthead+1;
520        evbefh = code->Get(pctp->Physics);          } else {
521        if ( evbefh >= 0 ){        rhev = lasthead;
522          T->GetEntry(evbefh);      };
523          ph = eh->GetPscuHeader();      if ( IsDebug() ) printf(" rhev after %i ph %lld upperp %lld oh %lld uppero %lld \n",rhev,pkth,upperpkt,obth,upperobt);
524          t_pktlast = PKT(ph->GetCounter());      //
525          t_obtlast = OBT(ph->GetOrbitalTime());      if ( IsDebug() ) printf(" rtev beforev %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
526          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump      if ( pktt > upperpkt && obtt > upperobt ){
527            upperpkt = pkth;        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %lld upperp %lld ot %lld uppero %lld \n",pktt,upperpkt,obtt,upperobt);
528            upperobt = obth;        upperpkt = pktt;
529            upperentry = evbefh-1;        upperobt = obtt;
530          } else {        rtev = lasttrail+1;
531            while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){      } else {
532              evbefh++;          rtev = lasttrail;
533              T->GetEntry(evbefh);      };
534              ph = eh->GetPscuHeader();      if ( IsDebug() ) printf(" rtev after %i  pt %lld upperp %lld ot %lld uppero %lld \n",rtev,pktt,upperpkt,obtt,upperobt);
535              t_pktlast = PKT(ph->GetCounter());      //  goto kikko;
536              t_obtlast = OBT(ph->GetOrbitalTime());      //
537            };      //
538            T->GetEntry(evbefh-1);      // Check if runtrailer/runheader are within lower limits
539        //
540        //
541        pkth = 0LL;
542        obth = 0LL;
543        spkth = 0LL;
544        sobth = 0LL;
545        for (Int_t k=0; k<rhev; k++){
546          if ( k > 0 ){
547            spkth = pkth;
548            sobth = obth;
549          };
550          rh->GetEntry(k);
551          phh = ehh->GetPscuHeader();
552          pkth = PKT(phh->GetCounter());
553          obth = OBT(phh->GetOrbitalTime());
554          //
555          //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
556          //
557          if ( pkth < spkth && obth < sobth ){
558            if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
559            //
560            rhev = k-1;
561            rh->GetEntry(rhev);
562            pkth = spkth;
563            obth = sobth;
564            //
565            UInt_t evbefh = 0;
566            code = ehh->GetCounter();
567            evbefh = code->Get(pctp->Physics);    
568            if ( evbefh >= 0 ){
569              T->GetEntry(evbefh);
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;  
     };  
   };  
  kikko0:  
   //  
   //  
   //  
   pktt = 0;  
   obtt = 0;  
   spktt = 0;  
   sobtt = 0;  
   for (Int_t k=0; k<rtev; k++){  
     if ( k > 0 ){  
       spktt = pktt;  
       sobtt = obtt;  
595      };      };
596      rt->GetEntry(k);    kikko0:
     pht = eht->GetPscuHeader();  
     pktt = PKT(pht->GetCounter());  
     obtt = OBT(pht->GetOrbitalTime());  
597      //      //
 //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);  
598      //      //
599      if ( pktt < spktt && obtt < sobtt ){      //
600        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);      pktt = 0LL;
601        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 755  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    if ( !dworbit ) throw -27;    //
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;
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()));
890      } else {    };
891        return((ULong64_t)(obt-numeric_limits<UInt_t>::max()));    //
892      };    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    return((ULong64_t)obt);    if ( IsDebug() ) printf(" as it is %lld \n",(Long64_t)obt);
899      return((Long64_t)obt);
900  };  };
901    
902  /**  /**
# Line 909  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 929  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 944  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 953  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 968  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 1162  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 1179  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 1210  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 1238  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 1263  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 1282  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 {
1428        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 {    } else {
1475      if ( !this->GetBOOTnumber() ) return(2);      if ( afound ){
1476          this->SetBOOTnumber(bn);
1477          sgn = 8;
1478        };
1479    };    };
1480    //    //
1481    oss.str("");    oss.str("");
# Line 1295  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 1308  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 1326  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 1359  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 1375  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 1390  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 1400  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 1431  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 1441  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 1462  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 1512  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1717  Bool_t PamelaDBOperations::IsRunAlreadyI
1717    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&    //                                  ( runhead_time <= _our_runhead_time &&  runtrail_time >= _our_runtrail_time) &&
1718    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&    //                                  ( runhead_obt <= _our_runheadobt || runhead_pkt <= _our_runheadpkt ) &&
1719    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )    //                                  ( runtrail_obt <= _our_runtrailobt || runtrail_pkt <= _our_runtrailpkt ) )
1720      //                                  ||
1721      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1722      //                                  )
1723      //                                  ||
1724      //                                  ( runhead_time = _our_runhead_time &&  runtrail_time > _our_runtrail_time && nevents > 100 )
1725      //                                  )
1726      //                                  ||
1727      //                                  ( runhead_time < _our_runhead_time &&  runtrail_time = _our_runtrail_time && nevents > 100 )
1728    //                                  )    //                                  )
1729    //    //
1730    oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "    oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
# Line 1527  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1740  Bool_t PamelaDBOperations::IsRunAlreadyI
1740        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "        << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1741        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("        << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1742        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "        << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1743        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";        << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") OR "
1744          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //  these two lines in a certain way disable the patch below...
1745          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)"    //
1746          << " (RUNHEADER_TIME=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1747          << " RUNTRAILER_TIME>" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)"    //
1748          << " (RUNHEADER_TIME<" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "                //
1749          << " RUNTRAILER_TIME=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND NEVENTS>100)"    //
1750          << " ));";
1751    //    //
1752    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1753    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
# Line 1549  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1769  Bool_t PamelaDBOperations::IsRunAlreadyI
1769      //      //
1770      // the run has already been inserted      // the run has already been inserted
1771      //      //
1772      //    return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here      if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1773        return(true);  //<<<<<<<<<<<<<<<<<<<<<<<< patch follows, uncomment here
1774      //      //
1775      // PATCH!      // PATCH!
1776      // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader      // we keep the processing run if (in order of growing importance) 1) we have the runtrailer while the old run doesn't have it 2) we have the runheader
# Line 1558  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1779  Bool_t PamelaDBOperations::IsRunAlreadyI
1779      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1780        //        //
1781        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1782        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1783        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1784        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1785        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1786    //       conn->Query(oss.str().c_str());
1787        if ( signal ) signal = false;        if ( signal ) signal = false;
1788        goto gonext;              goto gonext;      
1789        //        //
# Line 1575  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1797  Bool_t PamelaDBOperations::IsRunAlreadyI
1797        //        //
1798        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1799        //        //
1800        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1801        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1802        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1803        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1804    //       conn->Query(oss.str().c_str());
1805        //        //
1806        if ( signal ) signal = false;        if ( signal ) signal = false;
1807        goto gonext;        goto gonext;
# Line 1592  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1815  Bool_t PamelaDBOperations::IsRunAlreadyI
1815        //        //
1816        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1817        //        //
1818        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1819        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1820        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1821        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1822    //       conn->Query(oss.str().c_str());
1823        if ( signal ) signal = false;        if ( signal ) signal = false;
1824        //        //
1825      };      };
# Line 1610  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1834  Bool_t PamelaDBOperations::IsRunAlreadyI
1834    delete result;    delete result;
1835    //    //
1836    if ( signal && IsDebug() ) printf(" The run has already been inserted \n");        if ( signal && IsDebug() ) printf(" The run has already been inserted \n");    
1837      if ( !signal && IsDebug() ) printf(" The run existed and was deleted, fill the DB \n");
1838    return(signal);    return(signal);
1839  };  };
1840    
# Line 1658  void PamelaDBOperations::HandleRunFragme Line 1883  void PamelaDBOperations::HandleRunFragme
1883    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1884    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1885    Bool_t found = false;    Bool_t found = false;
1886      Bool_t foundinrun = false;
1887    //    //
1888    TSQLResult *result = 0;    TSQLResult *result = 0;
1889    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1684  void PamelaDBOperations::HandleRunFragme Line 1910  void PamelaDBOperations::HandleRunFragme
1910    //    //
1911    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1912    //    //
1913      // First of all insert the run in the fragment table...
1914      //
1915      oss.str("");
1916      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1917          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1918          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1919          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1920          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1921          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1922          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1923          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1924          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1925          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1926          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1927          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1928          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1929          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1930      //
1931      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1932      result = conn->Query(oss.str().c_str());
1933      //
1934      if ( !result ) throw -4;
1935      //
1936      row = result->Next();
1937      //
1938      if ( !row ){
1939        //
1940        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1941        //
1942        if ( IsDebug() ) printf(" The run is new \n");
1943        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1944        //
1945        glrun->SetID(this->AssignRunID());
1946        glrun->SetID_RUN_FRAG(0);
1947        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1948        //
1949      } else {
1950        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1951        return;
1952      };
1953      //
1954      //
1955    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1956    //        //    
1957    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
# Line 1692  void PamelaDBOperations::HandleRunFragme Line 1960  void PamelaDBOperations::HandleRunFragme
1960      oss.str("");      oss.str("");
1961      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1962          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1963          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1964            << " ID != " << glrun->ID
1965          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1966      //      //
1967      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1702  void PamelaDBOperations::HandleRunFragme Line 1971  void PamelaDBOperations::HandleRunFragme
1971      //      //
1972      row = result->Next();      row = result->Next();
1973      //      //
1974        if ( !row && NoFrag() ){
1975          //
1976          oss.str("");
1977          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1978              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1979              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1980              << " ID != " << glrun->ID
1981              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1982          //
1983          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1984          result = conn->Query(oss.str().c_str());
1985          //
1986          if ( !result ) throw -4;
1987          //
1988          foundinrun = true;
1989          //
1990          row = result->Next();
1991          //
1992        };
1993        //
1994      if ( !row ){      if ( !row ){
1995        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1996        found = false;        found = false;
# Line 1776  void PamelaDBOperations::HandleRunFragme Line 2065  void PamelaDBOperations::HandleRunFragme
2065        //        //
2066        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2067        //        //
2068          if ( foundinrun ){
2069            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2070            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2071          };
2072          //
2073        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2074        //        //
2075        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2076        //        //
2077        oss.str("");        oss.str("");
2078        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1791  void PamelaDBOperations::HandleRunFragme Line 2085  void PamelaDBOperations::HandleRunFragme
2085        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun1->GetRUNTRAILER_OBT());
2086        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun->GetRUNHEADER_PKT());
2087        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun->GetRUNHEADER_OBT());
2088        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);
2089        TTree *T= 0;        TTree *T= 0;
2090        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2091        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 1805  void PamelaDBOperations::HandleRunFragme Line 2099  void PamelaDBOperations::HandleRunFragme
2099          bobt = OBT(ph->GetOrbitalTime());                bobt = OBT(ph->GetOrbitalTime());      
2100          firstev++;          firstev++;
2101        };        };
2102        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);
2103        //        //
2104        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
2105        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
# Line 1815  void PamelaDBOperations::HandleRunFragme Line 2109  void PamelaDBOperations::HandleRunFragme
2109        //        //
2110        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
2111        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
2112          //
2113        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
2114        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
2115        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1834  void PamelaDBOperations::HandleRunFragme Line 2129  void PamelaDBOperations::HandleRunFragme
2129        //        //
2130        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2131          //          //
2132          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2133          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2134          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2135          //          //
2136          // get id number          // set id number
         //  
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
 //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
 //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun0 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun0 = (UInt_t)atoll(row->GetField(0));  
 //      };  
2137          //          //
         //      glrun1->SetID_RUN_FRAG(idrun0);  
2138          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2139          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2140          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
2141        };        };
       //  
       delete glrun1;  
       //  
2142        // delete old entry in fragment table        // delete old entry in fragment table
2143        //        //
2144        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2145          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2146        //        //
2147        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
2148        //        //
       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
       result =  conn->Query(oss.str().c_str());  
       if ( !result ) throw -4;  
2149        //        //
2150        return;        return;
2151        //        //
# Line 1905  void PamelaDBOperations::HandleRunFragme Line 2154  void PamelaDBOperations::HandleRunFragme
2154    };    };
2155    //    //
2156    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
2157                                             // missing it no way we can found a piece in the frag table      // missing it no way we can found a piece in the frag table
2158      //      //
2159      oss.str("");      oss.str("");
2160      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
2161          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2162          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2163            << " ID != " << glrun->ID
2164          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2165      //      //
2166      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1920  void PamelaDBOperations::HandleRunFragme Line 2170  void PamelaDBOperations::HandleRunFragme
2170      //      //
2171      row = result->Next();      row = result->Next();
2172      //      //
2173        if ( !row && NoFrag() ){
2174          //
2175          oss.str("");
2176          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
2177              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
2178              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
2179              << " ID != " << glrun->ID
2180              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
2181          //
2182          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
2183          result = conn->Query(oss.str().c_str());
2184          //
2185          if ( !result ) throw -4;
2186          //
2187          foundinrun = true;
2188          row = result->Next();
2189          //
2190        };
2191        //
2192      if ( !row ){      if ( !row ){
2193        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
2194        found = false;        found = false;
# Line 1993  void PamelaDBOperations::HandleRunFragme Line 2262  void PamelaDBOperations::HandleRunFragme
2262        //        //
2263        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2264        //        //
2265          if ( foundinrun ){
2266            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2267            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2268          };
2269          //
2270        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2271        //        //
2272        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2273        //        //
2274        oss.str("");        oss.str("");
2275        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 2008  void PamelaDBOperations::HandleRunFragme Line 2282  void PamelaDBOperations::HandleRunFragme
2282        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());        ULong64_t aobt = OBT(glrun->GetRUNTRAILER_OBT());
2283        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());        UInt_t bpkt = PKT(glrun1->GetRUNHEADER_PKT());
2284        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());        ULong64_t bobt = OBT(glrun1->GetRUNHEADER_OBT());
2285        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);
2286        TTree *T= 0;        TTree *T= 0;
2287        T = (TTree*)file->Get("Physics");        T = (TTree*)file->Get("Physics");
2288        if ( !T || T->IsZombie() ) throw -16;        if ( !T || T->IsZombie() ) throw -16;
# Line 2022  void PamelaDBOperations::HandleRunFragme Line 2296  void PamelaDBOperations::HandleRunFragme
2296          aobt = OBT(ph->GetOrbitalTime());                aobt = OBT(ph->GetOrbitalTime());      
2297          lastev--;          lastev--;
2298        };        };
2299        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);
2300        //        //
2301        glrun->SetEV_TO(lastev);        glrun->SetEV_TO(lastev);
2302        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
# Line 2051  void PamelaDBOperations::HandleRunFragme Line 2325  void PamelaDBOperations::HandleRunFragme
2325        //        //
2326        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2327          //          //
2328          glrun->SetID(this->AssignRunID());          //      glrun->SetID(this->AssignRunID());
2329          //          //
2330          glrun->SetID_RUN_FRAG(glrun1->GetID());          glrun->SetID_RUN_FRAG(glrun1->GetID());
2331          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2332          //          //
2333          // get id number          // set id number
2334          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
 //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
 //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun0 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun0 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      glrun1->SetID_RUN_FRAG(idrun0);  
 //  
2335          glrun1->SetID_RUN_FRAG(glrun->GetID());          glrun1->SetID_RUN_FRAG(glrun->GetID());
2336          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2337          //          //
 //      oss.str("");  
 //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
 //      //  
 //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
 //      row = result->Next();  
 //      //  
 //      UInt_t idrun1 = 0;  
 //      if ( !row ){  
 //        throw -10;  
 //      } else {  
 //        idrun1 = (UInt_t)atoll(row->GetField(0));  
 //      };  
 //      //  
 //      oss.str("");  
 //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
 //      //  
 //      result =  conn->Query(oss.str().c_str());  
 //      if ( !result ) throw -4;  
         //  
2338        };        };
2339        //        //
2340        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
2341        //        //
2342        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2343        //        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
       //  
       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
       result =  conn->Query(oss.str().c_str());  
       if ( !result ) throw -4;  
2344        //        //
2345          delete glrun1;
2346        //        //
2347        return;        return;
2348        //        //
# Line 2153  void PamelaDBOperations::HandleRunFragme Line 2379  void PamelaDBOperations::HandleRunFragme
2379      //      //
2380      row = result->Next();      row = result->Next();
2381      //      //
2382      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->SetID(this->AssignRunID());  
         glrun->SetID_RUN_FRAG(0);  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2383        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2384        } else {
2385          if ( NoFrag() ){
2386            glrun->SetID_RUN_FRAG(glrun->GetID());
2387            glrun->Fill_GL_RUN(conn);      
2388            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2389          };    
2390      };      };
2391    };    };
2392    //    //
# Line 2710  Int_t PamelaDBOperations::insertCALO_CAL Line 2903  Int_t PamelaDBOperations::insertCALO_CAL
2903          fromtime = this->GetAbsTime(ph->GetOrbitalTime());            fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
2904          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) ){
2905            //            //
2906            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);
2907            //            //
2908            // check if the calibration has already been inserted            // check if the calibration has already been inserted
2909            //            //
# Line 2754  Int_t PamelaDBOperations::insertCALO_CAL Line 2947  Int_t PamelaDBOperations::insertCALO_CAL
2947                // no calibrations in the db contain our calibration                // no calibrations in the db contain our calibration
2948                //                //
2949                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);
2950                if ( fromtime < 1150863400 ){                if ( fromtime < 1150871000 ){ //1150866904
2951                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);                  if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
2952                  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
2953                };                };
2954                //                //
# Line 2817  Int_t PamelaDBOperations::insertCALO_CAL Line 3010  Int_t PamelaDBOperations::insertCALO_CAL
3010            //            //
3011          } else {          } else {
3012            //            //
3013            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);
3014            //            //
3015          };                };      
3016          //                //      
# Line 2899  void PamelaDBOperations::HandleTRK_CALIB Line 3092  void PamelaDBOperations::HandleTRK_CALIB
3092        // no calibrations in the db contain our calibration        // no calibrations in the db contain our calibration
3093        //        //
3094        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");
3095        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
3096        //        //
3097        oss.str("");        oss.str("");
3098        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "        oss << " SELECT FROM_TIME FROM GL_TRK_CALIB WHERE "
# Line 3028  Int_t PamelaDBOperations::insertTRK_CALI Line 3221  Int_t PamelaDBOperations::insertTRK_CALI
3221      pkt1 = ph1->GetCounter();        pkt1 = ph1->GetCounter();  
3222      fromtime = this->GetAbsTime(ph1->GetOrbitalTime());        fromtime = this->GetAbsTime(ph1->GetOrbitalTime());  
3223      //      //
3224      valid = 1;  //     valid = 1;
3225      //  //     //
3226      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
3227      //      //
3228      //      //
3229      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) ){
3230        //        //
3231        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);
3232          //      
3233          valid = ValidateTrkCalib( caltrk1, eh1 );
3234          if ( IsDebug() ) cout << " pkt1 validation --> "<<valid<<endl;
3235        //        //
3236        // Do we have the second calibration packet?        // Do we have the second calibration packet?
3237        //        //
# Line 3052  Int_t PamelaDBOperations::insertTRK_CALI Line 3248  Int_t PamelaDBOperations::insertTRK_CALI
3248            obt2 = ph2->GetOrbitalTime();              obt2 = ph2->GetOrbitalTime();  
3249            pkt2 = ph2->GetCounter();              pkt2 = ph2->GetCounter();  
3250            //            //
3251            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
3252            //                  //      
3253          } else {          } else {
3254            //            //
# Line 3075  Int_t PamelaDBOperations::insertTRK_CALI Line 3271  Int_t PamelaDBOperations::insertTRK_CALI
3271          //          //
3272        };        };
3273        //        //
3274        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);
3275        //        //
3276        // The calibration is good        // The calibration is good
3277        //        //
3278        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){        if ( this->PKT(pkt2) == this->PKT(pkt1)+1 ){
3279          //          //
3280          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);
3281            //
3282            UInt_t valid2 = ValidateTrkCalib( caltrk2, eh2 );
3283            if ( IsDebug() ) cout << " pkt2 validation --> "<<valid2<<endl;
3284            valid = valid & valid2;
3285          //          //
3286          // Handle good calib          // Handle good calib
3287          //          //
# Line 3091  Int_t PamelaDBOperations::insertTRK_CALI Line 3291  Int_t PamelaDBOperations::insertTRK_CALI
3291          //          //
3292          if ( t2 != pret2+1 ){          if ( t2 != pret2+1 ){
3293            //            //
3294            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);
3295            //            //
3296            while ( t2 > pret2+1 ){            while ( t2 > pret2+1 ){
3297              //              //
# Line 3120  Int_t PamelaDBOperations::insertTRK_CALI Line 3320  Int_t PamelaDBOperations::insertTRK_CALI
3320          //          //
3321          // Check for missing calibtrk2          // Check for missing calibtrk2
3322          //          //
3323          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);
3324          t2 = pret2;          t2 = pret2;
3325          //          //
3326          // handle missing calib2          // handle missing calib2
# Line 3134  Int_t PamelaDBOperations::insertTRK_CALI Line 3334  Int_t PamelaDBOperations::insertTRK_CALI
3334        //        //
3335      } else {      } else {
3336        //        //
3337        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);
3338        //        //
3339      };      };
3340      //          //    
# Line 3159  Int_t PamelaDBOperations::insertTRK_CALI Line 3359  Int_t PamelaDBOperations::insertTRK_CALI
3359      valid = 0;      valid = 0;
3360      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) ){
3361        //        //
3362        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);
3363        //        //
3364        this->HandleTRK_CALIB(false,true);        this->HandleTRK_CALIB(false,true);
3365        //        //
# Line 3213  Int_t PamelaDBOperations::insertS4_CALIB Line 3413  Int_t PamelaDBOperations::insertS4_CALIB
3413      fromtime = this->GetAbsTime(ph->GetOrbitalTime());        fromtime = this->GetAbsTime(ph->GetOrbitalTime());  
3414      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) ){
3415        //        //
3416        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);
3417        //        //
3418        // check if the calibration has already been inserted        // check if the calibration has already been inserted
3419        //        //
# Line 3255  Int_t PamelaDBOperations::insertS4_CALIB Line 3455  Int_t PamelaDBOperations::insertS4_CALIB
3455            // no calibrations in the db contain our calibration            // no calibrations in the db contain our calibration
3456            //            //
3457            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");
3458            if ( fromtime < 1150863400 ){            if ( fromtime < 1150871000 ){  
3459              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %i \n",fromtime);              if ( IsDebug() ) printf(" First PAMELA flight calibration at time %u \n",fromtime);
3460              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
3461            };            };
3462            //            //
# Line 3315  Int_t PamelaDBOperations::insertS4_CALIB Line 3515  Int_t PamelaDBOperations::insertS4_CALIB
3515        //        //
3516      } else {      } else {
3517        //        //
3518        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);
3519        //        //
3520      };            };      
3521      //            //      
# Line 3328  Int_t PamelaDBOperations::insertS4_CALIB Line 3528  Int_t PamelaDBOperations::insertS4_CALIB
3528   * Scan the fragment table and move old fragments to the GL_RUN table   * Scan the fragment table and move old fragments to the GL_RUN table
3529   */   */
3530  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){  Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3531      return(this->CleanGL_RUN_FRAGMENTS(""));
3532    };
3533    
3534    /**
3535     * Scan the fragment table and move old fragments to the GL_RUN table
3536     */
3537    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3538    //    //
3539    TSQLResult *result = 0;    TSQLResult *result = 0;
3540    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 3335  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3542  Int_t PamelaDBOperations::CleanGL_RUN_FR
3542    TSQLRow    *row2   = 0;    TSQLRow    *row2   = 0;
3543    //    //
3544    UInt_t moved = 0;    UInt_t moved = 0;
 //  UInt_t timelim = 0;  
 //  TDatime *time = new TDatime();  
3545    //    //
3546    stringstream oss;    stringstream oss;
3547    oss.str("");    oss.str("");
3548    //    //
3549    //    if ( !strcmp(fcleanfile.Data(),"") ){
3550    // check if there are entries older than "olderthan" seconds from now      //
3551    //      // check if there are entries older than "olderthan" seconds from now
3552    oss.str("");      //
3553    oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"      oss.str("");
3554        << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3555    //          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3556    if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());      //
3557    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3558        result = conn->Query(oss.str().c_str());
3559        //
3560      } else {
3561        oss.str("");
3562        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3563        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3564        result = conn->Query(oss.str().c_str());
3565        //    
3566        if ( result ){
3567          //
3568          row = result->Next();
3569          //
3570          if ( row ){
3571            oss.str("");
3572            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3573                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3574            //
3575            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3576            result = conn->Query(oss.str().c_str());
3577            //    
3578          };
3579        } else {
3580          return(2);
3581        };
3582      };
3583    //    //
3584    if ( result ){    if ( result ){
3585      //      //
# Line 3426  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3656  Int_t PamelaDBOperations::CleanGL_RUN_FR
3656        if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));              if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3657        //        //
3658        //        //
3659        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3660        oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);  //      oss.str("");
3661        if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());  //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3662        result2 = conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3663        //  //       result2 = conn->Query(oss.str().c_str());
3664        if ( !result2 ) throw -4;  //      //
3665        //  //      if ( !result2 ) throw -4;
3666    //      //
3667        row = result->Next();        row = result->Next();
3668      };      };
3669    };    };
# Line 3444  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3675  Int_t PamelaDBOperations::CleanGL_RUN_FR
3675   * Check if runs are good, i.e. if the tracker calibration is correctly associated..   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3676   */   */
3677  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3678      return(this->ValidateRuns(""));
3679    };
3680    
3681    /**
3682     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3683     */
3684    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3685    //    //
3686    TSQLResult *result = 0;    TSQLResult *result = 0;
3687    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3688    //    //
3689      UInt_t calibtime = 50;
3690      //
3691    stringstream oss;    stringstream oss;
3692    oss.str("");    oss.str("");
3693    //    //
# Line 3456  Int_t PamelaDBOperations::ValidateRuns() Line 3696  Int_t PamelaDBOperations::ValidateRuns()
3696    // =======================================================    // =======================================================
3697    UInt_t t_stop  = 0;    UInt_t t_stop  = 0;
3698    UInt_t t_start = 0;    UInt_t t_start = 0;
3699    // --------------------------------------------------------------    if ( !strcmp(valfile.Data(),"") ) {
3700    // 1) get the OBT of the last run inserted after clean-time limit      // --------------------------------------------------------------
3701    // --------------------------------------------------------------      // 1) get the OBT of the last run inserted after clean-time limit
3702    oss.str("");      // --------------------------------------------------------------
3703    oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()      oss.str("");
3704            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3705    if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());          << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3706    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3707    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3708    if ( !result->GetRowCount() ) {      if ( !result ) throw -4;
3709            printf(" No runs to validate \n");      if ( !result->GetRowCount() ) {
3710            return(1);        printf(" No runs to validate \n");
3711    }else{        return(1);
3712          row = result->Next();      }else{
3713          t_start = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3714    };          t_start = (UInt_t)atoll(row->GetField(4));
3715    // --------------------------------------------------------------      };  
3716    // 2) get the OBT of the last validated run      // --------------------------------------------------------------
3717    // --------------------------------------------------------------      // 2) get the OBT of the last validated run
3718    oss.str("");      // --------------------------------------------------------------
3719    oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start      oss.str("");
3720        <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3721    if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3722    result = conn->Query(oss.str().c_str());      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3723    if ( !result ) throw -4;      result = conn->Query(oss.str().c_str());
3724    if ( result->GetRowCount() ){      if ( !result ) throw -4;
3725            row = result->Next();      if ( result->GetRowCount() ){
3726            t_stop = (UInt_t)atoll(row->GetField(4));        row = result->Next();
3727          t_stop = (UInt_t)atoll(row->GetField(4));
3728        };
3729        if ( IsDebug() ) printf("Validation interval: from time %u - to time %u \n\n",t_stop,t_start);
3730        // --------------------------------------------------------------
3731        // now retrieves runs to be validated
3732        // --------------------------------------------------------------
3733        oss.str("");
3734        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3735        oss << " AND RUNHEADER_TIME >="<< t_stop;
3736        oss << " ORDER BY RUNHEADER_TIME DESC;";
3737        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3738        result = conn->Query(oss.str().c_str());
3739      } else {
3740        //
3741        stringstream myquery;
3742        UInt_t myid = 0;
3743        myquery.str("");
3744        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3745        //
3746        result = conn->Query(myquery.str().c_str());
3747        //
3748        row = result->Next();      
3749        if( !row ){
3750          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3751            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3752            return(2);
3753          };
3754          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3755          return(0);
3756        };
3757        myid=(UInt_t)atoll(row->GetField(0));
3758        //
3759        myquery.str("");
3760        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3761        //
3762        result = conn->Query(myquery.str().c_str());
3763        //
3764        row = result->Next();      
3765        if( !row->GetField(0) || !row->GetField(1)){
3766          //
3767          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3768          //
3769          return(0);
3770          //
3771        } else {  
3772          //
3773          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3774          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3775          UInt_t caltime = 0;
3776          //
3777          myquery.str("");
3778          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3779          myquery << " order by FROM_TIME asc limit 1;";
3780          //
3781          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3782          //
3783          //
3784          result = conn->Query(myquery.str().c_str());
3785          //
3786          row = result->Next();      
3787          if( !row ){
3788            caltime = runhtime;
3789          } else {
3790            caltime = (UInt_t)atoll(row->GetField(0));
3791          };
3792          //
3793          myquery.str("");
3794          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3795          myquery << caltime << " order by RUNHEADER_TIME DESC";
3796          //
3797          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3798          //
3799          result = conn->Query(myquery.str().c_str());
3800          //
3801        };
3802    };    };
3803    if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);    //
   // --------------------------------------------------------------  
   // now retrieves runs to be validated  
   // --------------------------------------------------------------  
   oss.str("");  
   oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;  
   oss << " AND RUNHEADER_TIME >="<< t_stop;  
   oss << " ORDER BY RUNHEADER_TIME DESC;";  
 //  if ( IsDebug() )  
   if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());  
   result = conn->Query(oss.str().c_str());  
3804    if ( !result ) throw -4;    if ( !result ) throw -4;
3805    if ( !result->GetRowCount() ) printf(" No runs to validate \n");    if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3806  //  printf("------------------------------------------------------------------------------- \n");    //
     
3807    Int_t nrow = 0;    Int_t nrow = 0;
3808    GL_RUN* this_run = new GL_RUN();    GL_RUN* this_run = new GL_RUN();
3809    GL_RUN* next_run = new GL_RUN();    GL_RUN* next_run = new GL_RUN();
# Line 3513  Int_t PamelaDBOperations::ValidateRuns() Line 3817  Int_t PamelaDBOperations::ValidateRuns()
3817    UInt_t t1=0,t2=0;    UInt_t t1=0,t2=0;
3818    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
3819    // - loop over runs, back in time,    // - loop over runs, back in time,
3820    // - select sequences of runs close in time (less than 60 s apart),    // - select sequences of runs close in time (less than calibtime s apart),
3821    //   which could be preceeded by a calibration    //   which could be preceeded by a calibration
3822    // - check if there might be a missing calibration    // - check if there might be a missing calibration
3823    // ---------------------------------------------------------------------------------    // ---------------------------------------------------------------------------------
# Line 3553  Int_t PamelaDBOperations::ValidateRuns() Line 3857  Int_t PamelaDBOperations::ValidateRuns()
3857                                                        
3858                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3859                                                        
3860                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration                            if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration
3861                                                        
3862                            if( !CHECK && this_run->VALIDATION ){                            if( !CHECK && this_run->VALIDATION ){
3863                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);                                    for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
# Line 3593  Int_t PamelaDBOperations::ValidateRuns() Line 3897  Int_t PamelaDBOperations::ValidateRuns()
3897                    }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);
3898            };            };
3899                        
3900            if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);            if ( IsDebug() ) printf("%i Run %u \n",nrow,this_run->ID);
3901            nrow++;            nrow++;
3902                        
3903    };    };
# Line 3622  Bool_t PamelaDBOperations::MissingTRK_CA Line 3926  Bool_t PamelaDBOperations::MissingTRK_CA
3926          // which should be equal to the time between ascending-nodes.          // which should be equal to the time between ascending-nodes.
3927          //==============================================================          //==============================================================
3928          if ( t2 - trkcalib->FROM_TIME > 5700) {          if ( t2 - trkcalib->FROM_TIME > 5700) {
3929                  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);
3930          //==============================================================          //==============================================================
3931          // there might be a missing calibration, due to:          // there might be a missing calibration, due to:
3932          // - MM full          // - MM full
# Line 3643  Bool_t PamelaDBOperations::MissingTRK_CA Line 3947  Bool_t PamelaDBOperations::MissingTRK_CA
3947          // it is enough to say that there are no missing calibrations          // it is enough to say that there are no missing calibrations
3948          //==============================================================          //==============================================================
3949          // the long time interval bewteen runs might be due to download          // the long time interval bewteen runs might be due to download
3950          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);
3951          return(false);          return(false);
3952                    
3953  };  };
# Line 3751  Int_t PamelaDBOperations::populateTLE()/ Line 4055  Int_t PamelaDBOperations::populateTLE()/
4055    
4056    
4057  // Insert tle in the table GL_TLE using the connection conn.  // Insert tle in the table GL_TLE using the connection conn.
4058  int PamelaDBOperations::insertTle(cTle *tle)  Int_t PamelaDBOperations::insertTle(cTle *tle)
4059  {  {
4060    stringstream oss;    stringstream oss;
4061    TSQLResult *result = 0;    TSQLResult *result = 0;
# Line 3834  string getTleDatetime(cTle *tle) Line 4138  string getTleDatetime(cTle *tle)
4138    
4139    return date.str();    return date.str();
4140  }  }
4141    
4142    /**
4143     * Remove a file from the DB, delete on cascade all entries related to that file
4144     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
4145     * calibration
4146     **/
4147    Int_t PamelaDBOperations::removeFile(TString remfile){
4148      //
4149      // Determine ID_ROOT_L0 and ID_RAW
4150      //
4151      TSQLResult *pResult;
4152      TSQLRow *Row;
4153      stringstream myquery;
4154      //  
4155      myquery.str("");
4156      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
4157      //
4158      pResult = conn->Query(myquery.str().c_str());
4159      //
4160      Row = pResult->Next();      
4161      if( !Row ){
4162        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
4163          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
4164          return(1);
4165        };
4166        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
4167        return(0);
4168      };
4169      //
4170      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
4171      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
4172      //
4173      this->ValidationOFF();
4174      //
4175      this->RemoveCALIBS();
4176      //
4177      this->RemoveRUNS();
4178      //
4179      this->RemoveFILES();
4180      //
4181      this->SetID_ROOT(0);
4182      this->SetID_RAW(0);
4183      //
4184      return(0);
4185    };
4186    
4187    /**
4188     *
4189     * Set validation bit to zero for runs following the removing file till
4190     * 1) a run with TRK_CALIB_USED=140
4191     * 2) a run with VALIDATION = 0
4192     * 3) the next calibration
4193     *
4194     **/
4195    void PamelaDBOperations::ValidationOFF(){
4196      TSQLResult *pResult;
4197      TSQLRow *Row;
4198      stringstream myquery;
4199      Int_t unv = 0;
4200      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
4201      myquery.str("");
4202      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4203      //
4204      pResult = conn->Query(myquery.str().c_str());
4205      //
4206      Row = pResult->Next();      
4207      if( !Row->GetField(0) ){
4208        //
4209        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
4210        //
4211      } else {  
4212        //
4213        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
4214        UInt_t caltime = 0;
4215        //
4216        myquery.str("");
4217        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
4218        myquery << " order by FROM_TIME asc limit 1;";
4219        //
4220        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4221        //
4222        //
4223        delete pResult;
4224        pResult = conn->Query(myquery.str().c_str());
4225        //
4226        Row = pResult->Next();      
4227        if( !Row ){
4228          caltime = runhtime;
4229        } else {
4230          caltime = (UInt_t)atoll(Row->GetField(0));
4231        };
4232        //
4233        myquery.str("");
4234        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4235        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4236        //
4237        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4238        //
4239        pResult = conn->Query(myquery.str().c_str());
4240        //
4241        Row = pResult->Next();      
4242        if( !Row ){
4243          //
4244          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4245          //
4246        } else {
4247          myquery.str("");
4248          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4249          myquery << " RUNHEADER_TIME>=" <<runhtime;
4250          myquery << " order by RUNHEADER_TIME asc;";
4251          //
4252          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4253          //
4254          pResult = conn->Query(myquery.str().c_str());
4255          //
4256          Row = pResult->Next();  
4257          while ( Row ){
4258            //      
4259            unv++;
4260            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4261            Row = pResult->Next();  
4262            //
4263          };
4264        };
4265      };
4266      if ( IsDebug() ) printf(" %u runs have been unvalidated \n",unv);
4267    };
4268    
4269    /**
4270     *
4271     * Rearrange GL_RUN table and remove runs
4272     *
4273     **/
4274    void PamelaDBOperations::RemoveRUNS(){
4275      TSQLResult *pResult;
4276      TSQLRow *Row;
4277      stringstream myquery;
4278      UInt_t drun = 0;
4279      GL_RUN *delrun = new GL_RUN();
4280      //
4281      myquery.str("");
4282      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4283      //
4284      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4285      //
4286      pResult = conn->Query(myquery.str().c_str());
4287      //
4288      Row = pResult->Next();    
4289      //
4290      //
4291      if ( !Row ){
4292        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4293      } else {
4294        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4295        while ( Row ){
4296          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4297          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4298          drun++;
4299          Row = pResult->Next();    
4300        };
4301      };
4302      //
4303      //
4304      myquery.str("");
4305      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4306      //
4307      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4308      //
4309      pResult = conn->Query(myquery.str().c_str());
4310      //
4311      Row = pResult->Next();    
4312      //
4313      if ( !Row ){
4314        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4315      } else {
4316        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4317        while ( Row ){
4318          if ( IsDebug() ) printf(" restore run %u \n",(UInt_t)atoll(Row->GetField(1)));
4319          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4320          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(1)));
4321          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4322          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4323            if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4324            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4325          };
4326          drun++;
4327          Row = pResult->Next();    
4328        };
4329      };
4330      //
4331      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4332      //
4333      //
4334      //
4335      drun = 0;
4336      //
4337      myquery.str("");
4338      myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4339      //
4340      pResult = conn->Query(myquery.str().c_str());
4341      //
4342      Row = pResult->Next();    
4343      //
4344      if ( !Row ){
4345        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4346      } else {
4347        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4348        while ( Row ){
4349          if ( IsDebug() ) printf(" del run idtrash %u \n",(UInt_t)atoll(Row->GetField(0)));
4350          myquery.str("");
4351          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4352          conn->Query(myquery.str().c_str());
4353          drun++;
4354          Row = pResult->Next();    
4355        };
4356      };
4357      //  
4358      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_TRASH table \n",drun);    
4359      //
4360      //
4361      //
4362      drun = 0;
4363      //
4364      myquery.str("");
4365      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4366      //
4367      pResult = conn->Query(myquery.str().c_str());
4368      //
4369      Row = pResult->Next();    
4370      //
4371      if ( !Row ){
4372        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4373      } else {
4374        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4375        while ( Row ){
4376          if ( IsDebug() ) printf(" del run %u \n",(UInt_t)atoll(Row->GetField(0)));
4377          myquery.str("");
4378          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4379          conn->Query(myquery.str().c_str());
4380          drun++;
4381          Row = pResult->Next();    
4382        };
4383      };
4384      //  
4385      if ( IsDebug() ) printf(" Deleted %u run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4386      //
4387      //
4388      //
4389      delete delrun;
4390      //
4391    };
4392    
4393    
4394    /**
4395     *
4396     * Rearrange calibration tables
4397     *
4398     **/
4399    void PamelaDBOperations::RemoveFILES(){
4400      stringstream myquery;
4401      //
4402      myquery.str("");
4403      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4404      //
4405      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4406      //
4407      conn->Query(myquery.str().c_str());
4408      //
4409    };
4410    
4411    /**
4412     *
4413     * Rearrange calibration tables
4414     *
4415     **/
4416    void PamelaDBOperations::RemoveCALIBS(){
4417      TSQLResult *pResult;
4418      TSQLRow *Row;
4419      stringstream myquery;
4420      //  
4421      //
4422      // Calorimeter
4423      //
4424      for (Int_t section = 0; section < 4; section++){
4425        myquery.str("");
4426        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4427        myquery << " SECTION=" << section << ";";
4428        //
4429        pResult = conn->Query(myquery.str().c_str());
4430        //
4431        Row = pResult->Next();      
4432        if( !Row->GetField(0) || !Row->GetField(1) ){
4433          //
4434          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4435          //
4436        } else {
4437          //
4438          myquery.str("");
4439          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4440          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4441          myquery << " SECTION=" << section << ";";
4442          //
4443          pResult = conn->Query(myquery.str().c_str());
4444          //
4445          if( !pResult ){
4446            //
4447            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4448            //
4449            throw -4;
4450            //
4451          };
4452          //
4453        };
4454      };
4455      myquery.str("");
4456      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4457      //
4458      pResult = conn->Query(myquery.str().c_str());
4459      //
4460      if( !pResult ){
4461        //
4462        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4463        //
4464        throw -4;
4465        //
4466      };
4467      //
4468      // Tracker
4469      //
4470      myquery.str("");
4471      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4472      //
4473      pResult = conn->Query(myquery.str().c_str());
4474      //
4475      Row = pResult->Next();      
4476      if( !Row->GetField(0) || !Row->GetField(1) ){
4477        //
4478        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4479        //
4480      } else {
4481        //
4482        myquery.str("");
4483        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4484        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4485        //
4486        pResult = conn->Query(myquery.str().c_str());
4487        //
4488        if( !pResult ){
4489          //
4490          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4491          //
4492          throw -4;
4493          //
4494        };
4495        //
4496        myquery.str("");
4497        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4498        //
4499        pResult = conn->Query(myquery.str().c_str());
4500        //
4501        if( !pResult ){
4502          //
4503          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4504          //
4505          throw -4;
4506          //
4507        };
4508      };
4509      //
4510      //
4511      // S4
4512      //
4513      myquery.str("");
4514      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4515      //
4516      pResult = conn->Query(myquery.str().c_str());
4517      //
4518      Row = pResult->Next();      
4519      if( !Row->GetField(0) || !Row->GetField(1) ){
4520        //
4521        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4522        //
4523      } else {
4524        //
4525        myquery.str("");
4526        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4527        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4528        //
4529        pResult = conn->Query(myquery.str().c_str());
4530        //
4531        if( !pResult ){
4532          //
4533          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4534          //
4535          throw -4;
4536          //
4537        };
4538        //
4539        myquery.str("");
4540        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4541        //
4542        pResult = conn->Query(myquery.str().c_str());
4543        //
4544        if( !pResult ){
4545          //
4546          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4547          //
4548          throw -4;
4549          //
4550        };
4551        //
4552      };
4553    };
4554    
4555    /**
4556     *
4557     * Rearrange calibration tables
4558     *
4559     **/
4560    UInt_t PamelaDBOperations::ValidateTrkCalib( CalibTrk1Event* caltrk, EventHeader *eh ){
4561    
4562        Int_t vorder[]={5,5,3,3,4,4,2,2,1,1,0,0};
4563        UInt_t timeaftercalib=120000; //2000;
4564    //  ----------
4565    //  Check CRCs
4566    //  ----------
4567        for(Int_t ipkt=0; ipkt<6; ipkt++){
4568            if( caltrk->crc_hcal[ipkt] )return 0; // :-(
4569            for(Int_t ilad=0; ilad<3; ilad++)if( caltrk->crc_cal[ipkt][ilad] )return 0; // :-(
4570        }
4571    //  -----------------------
4572    //  Check missing packets:
4573    //  -----------------------
4574    //    Readout order:
4575    //    ------------------
4576    //    DSP   packet board
4577    //    ------------------
4578    //    12    0      1
4579    //    10    1      1
4580    //     8    2      1
4581    //     4    3      1
4582    //     6    4      1
4583    //     2    5      1
4584    //    ------------------
4585    //    11    0      2
4586    //     9    1      2
4587    //     7    2      2
4588    //     3    3      2
4589    //     5    4      2
4590    //     1    5      2
4591    //    ------------------
4592    //  -------------------------------------------------
4593    //  Check if it is first or second calibration packet
4594    //  -------------------------------------------------
4595        UInt_t build=0;
4596        TString classname = caltrk->GetName();
4597        UInt_t base=0;
4598        UInt_t mask=0;
4599        if(classname.Contains("CalibTrk1Event")){
4600            base=12;
4601            mask=0x03F000;
4602        }
4603        if(classname.Contains("CalibTrk2Event")){
4604            base=18;
4605            mask=0xFC0000;
4606        }
4607    //  -------------------------------------------------
4608    //  Count number of packets and set build variable
4609    //  -------------------------------------------------
4610        Int_t  npkts=0;
4611        for(Int_t ipkt=0; ipkt<6; ipkt++){
4612            if(caltrk->DSPnumber[ipkt]>0 && caltrk->DSPnumber[ipkt]<=12){
4613                npkts++;
4614                build = build | ( 1<<(base+vorder[caltrk->DSPnumber[ipkt]-1]) );
4615            }
4616        }
4617    //    if( npkts==6 )return 1; // :-)
4618    
4619    //    cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime()<<endl;
4620    
4621    //  -----------------------------------------------
4622    //  If missing packets: check the acq configuration
4623    //  (some DSPs might be excluded from acquisition)
4624    //  -----------------------------------------------
4625    
4626    //  -----------------------------------------------
4627    //  retrieve the first run header after calib
4628    //  -----------------------------------------------
4629         PacketType *pctp;
4630         EventCounter *cod;
4631         cod = eh->GetCounter();
4632         Int_t irun = cod->Get(pctp->RunHeader);
4633         TTree *rh=(TTree*)file->Get("RunHeader");
4634         if ( !rh || rh->IsZombie() ) throw -17;
4635         if( rh->GetEntries() == irun ){
4636              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (1)  -- cannot validate :-( "<<endl;
4637             return 0; // :-(
4638         }
4639    
4640         RunHeaderEvent *run  = 0;
4641         EventHeader    *hrun = 0;
4642         rh->SetBranchAddress("RunHeader", &run);
4643         rh->SetBranchAddress("Header", &hrun);
4644         rh->GetEntry(irun);
4645    //     cout << classname << " "<<eh->GetPscuHeader()->GetOrbitalTime() << " Run " << hrun->GetPscuHeader()->GetOrbitalTime() <<endl;
4646    
4647         if( OBT(hrun->GetPscuHeader()->GetOrbitalTime()) < OBT(eh->GetPscuHeader()->GetOrbitalTime())){
4648              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) no runs after calib (2) -- cannot validate :-( "<<endl;
4649             return 0; // :-(
4650         }
4651        
4652         if( !run->RM_ACQ_AFTER_CALIB ){
4653              if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) RM_ACQ_AFTER_CALIB=0    -- cannot validate :-( "<<endl;
4654             return 0; // :-(
4655         }
4656    
4657         UInt_t dtime = OBT(hrun->GetPscuHeader()->GetOrbitalTime()) - OBT(eh->GetPscuHeader()->GetOrbitalTime());
4658         if( dtime > timeaftercalib ){
4659             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) run after calib too far ( "<<dtime<<"ms ) -- cannot validate :-( "<<endl;
4660             return 0; // :-(
4661         }
4662        
4663    
4664    
4665         if( (run->ACQ_BUILD_INFO & mask) != build ){
4666             if ( IsDebug() ) cout << "ValidateTrkCalib: (MISSING VIEW) ACQ_BUILD_INFO= >>> "<<hex << (run->ACQ_BUILD_INFO&mask) << " != "<< build << dec<<endl;
4667             return 0; // :-(
4668         }
4669         return 1; // :-)
4670    
4671    }
4672    
4673    /**
4674     *
4675     * Check the DB (only for overlapping runs at the moment)
4676     *
4677     **/
4678    UInt_t PamelaDBOperations::Check(){
4679      //
4680      UInt_t test = 0;
4681      //
4682      UInt_t thisrht = 0;
4683      UInt_t thisrtt = 0;
4684      UInt_t thisid = 0;
4685      UInt_t prevrht = 0;
4686      UInt_t prevrtt = 0;
4687      UInt_t previd = 0;
4688      //
4689      UInt_t prevl0id = 0;
4690      UInt_t thisl0id = 0;
4691      //
4692      stringstream oss;
4693      TSQLResult *result = 0;
4694      TSQLRow    *row    = 0;
4695      TSQLResult *result2 = 0;
4696      TSQLRow    *row2    = 0;
4697      TSQLResult *result3 = 0;
4698      TSQLRow    *row3    = 0;
4699      oss.str("");
4700      oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME,NEVENTS FROM GL_RUN order by RUNHEADER_TIME asc;";
4701      //  oss << "SELECT ID,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN where ID>10170 and ID<10190 order by RUNHEADER_TIME asc;";
4702      result = conn->Query(oss.str().c_str());
4703      //
4704      if ( !result ) throw -4;;
4705      //
4706      row = result->Next();
4707      //
4708      while ( row ){
4709        thisid = (UInt_t)atoll(row->GetField(0));
4710        thisl0id = (UInt_t)atoll(row->GetField(1));
4711        thisrht = (UInt_t)atoll(row->GetField(2));
4712        thisrtt = (UInt_t)atoll(row->GetField(3));
4713        //
4714        if ( (UInt_t)atoll(row->GetField(4)) > 1 ){
4715          //
4716          //
4717          //
4718          oss.str("");
4719          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4720              << thisid << " AND RUNHEADER_TIME="
4721              << thisrht << " AND RUNTRAILER_TIME!="
4722              << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4723          result3 = conn->Query(oss.str().c_str());
4724          if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4725          if ( result3 ){
4726            row3 = result3->Next();
4727            //
4728            while ( row3 ){
4729              //
4730              // 2 runs with same runheader
4731              //
4732              printf(" CHECK n.4 RUNs %u and %u HAVE SAME RUNHEADER \n",thisid,(UInt_t)atoll(row3->GetField(0)));    
4733              row3 = result3->Next();
4734            };
4735          };
4736          //
4737          oss.str("");
4738          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4739              << thisid << " AND RUNHEADER_TIME!="
4740              << thisrht << " AND RUNTRAILER_TIME="
4741              << thisrtt << " AND NEVENTS!=0 AND NEVENTS!=1 order by RUNHEADER_TIME asc;";
4742          result3 = conn->Query(oss.str().c_str());
4743          if ( IsDebug() ) printf(" query is %s \n",oss.str().c_str());
4744          if ( result3 ){
4745            row3 = result3->Next();
4746            //
4747            while ( row3 ){
4748              //
4749              // 2 runs with same runtrailer
4750              //
4751              printf(" CHECK n.5 RUNs %u and %u HAVE SAME RUNTRAILER \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4752              row3 = result3->Next();
4753            };
4754          };
4755          //
4756          oss.str("");
4757          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4758              << thisid << " AND RUNHEADER_TIME>"
4759              << thisrht << " AND RUNTRAILER_TIME<"
4760              << thisrtt << " order by RUNHEADER_TIME asc;";
4761          result3 = conn->Query(oss.str().c_str());
4762          if ( result3 ){
4763            row3 = result3->Next();
4764            //
4765            while ( row3 ){
4766              //
4767              // run contained in the checked one
4768              //
4769              printf(" CHECK n.6 RUN %u CONTAINS RUN %u  \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4770              row3 = result3->Next();
4771            };
4772          };
4773          //
4774          oss.str("");
4775          oss << "SELECT ID,ID_ROOT_L0,RUNHEADER_TIME,RUNTRAILER_TIME FROM GL_RUN WHERE ID!="
4776              << thisid << " AND RUNHEADER_TIME="
4777              << thisrht << " AND RUNTRAILER_TIME="
4778              << thisrtt << " AND ID_RUN_FRAG!="
4779              << thisid << " order by RUNHEADER_TIME asc;";
4780          result3 = conn->Query(oss.str().c_str());
4781          if ( result3 ){
4782            row3 = result3->Next();
4783            //
4784            while ( row3 ){
4785              //
4786              // duplicated run
4787              //
4788              printf(" CHECK n.7 RUNs %u and %u HAVE SAME RUNTRAILER AND RUNHEADER (ARE THE SAME?) \n",thisid,(UInt_t)atoll(row3->GetField(0)));
4789              row3 = result3->Next();
4790            };
4791          };
4792        };
4793        //
4794        //    if ( thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt && !(!prevrht && !prevrtt &&!previd) ){
4795        //    if ( (thisrht < prevrtt || thisrtt < prevrht || thisrht > thisrtt) && (thisrht != prevrht) ){
4796        if ( (thisrht < prevrtt) && (thisrht != prevrht) ){
4797          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4798          printf(" CHECK n.1 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-prevrtt),previd,thisid);
4799          TString prevf = "";
4800          TString thisf = "";
4801          oss.str("");
4802          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4803          result2 = conn->Query(oss.str().c_str());
4804          if ( !result2 ) throw -4;;
4805          row2 = result2->Next();
4806          prevf = (TString)row2->GetField(0);
4807          oss.str("");
4808          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4809          result2 = conn->Query(oss.str().c_str());
4810          if ( !result2 ) throw -4;;
4811          row2 = result2->Next();
4812          thisf = (TString)row2->GetField(0);
4813          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4814          test = 1;
4815        };
4816        //
4817        if ( (thisrtt < prevrht) && (thisrht != prevrht) ){
4818          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4819          printf(" CHECK n.2 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrtt-prevrht),previd,thisid);
4820          TString prevf = "";
4821          TString thisf = "";
4822          oss.str("");
4823          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4824          result2 = conn->Query(oss.str().c_str());
4825          if ( !result2 ) throw -4;
4826          row2 = result2->Next();
4827          prevf = (TString)row2->GetField(0);
4828          oss.str("");
4829          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4830          result2 = conn->Query(oss.str().c_str());
4831          if ( !result2 ) throw -4;;
4832          row2 = result2->Next();
4833          thisf = (TString)row2->GetField(0);
4834          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4835          test = 1;
4836        };
4837        //
4838        if ( (thisrht > thisrtt) && (thisrht != prevrht) ){
4839          if ( IsDebug() ) printf(" IDprev %u ID %u    prevrht %u prevrtt %u  thisrht %u thisrtt %u \n",previd,thisid,prevrht,prevrtt,thisrht,thisrtt);
4840          printf(" CHECK n.3 TIME SCREW of %i s AROUND RUNs %u and %u \n",(thisrht-thisrtt),previd,thisid);
4841          TString prevf = "";
4842          TString thisf = "";
4843          oss.str("");
4844          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)prevl0id <<";";
4845          result2 = conn->Query(oss.str().c_str());
4846          if ( !result2 ) throw -4;;
4847          row2 = result2->Next();
4848          prevf = (TString)row2->GetField(0);
4849          oss.str("");
4850          oss << "SELECT NAME FROM GL_ROOT where ID=" << (UInt_t)thisl0id <<";";
4851          result2 = conn->Query(oss.str().c_str());
4852          if ( !result2 ) throw -4;;
4853          row2 = result2->Next();
4854          thisf = (TString)row2->GetField(0);
4855          if ( IsDebug() ) printf(" ==> files %s and %s \n",prevf.Data(),thisf.Data());
4856          test = 1;
4857        };
4858    
4859        //
4860        prevrht = thisrht;
4861        prevrtt = thisrtt;
4862        previd = thisid;
4863        prevl0id = thisl0id;
4864        row = result->Next();
4865      };
4866      //
4867      return(test);
4868      //
4869    };

Legend:
Removed from v.1.13  
changed lines
  Added in v.1.47

  ViewVC Help
Powered by ViewVC 1.1.23