/[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.1 by mocchiut, Wed Aug 30 11:18:13 2006 UTC revision 1.8 by pam-fi, Mon Sep 11 16:38:18 2006 UTC
# Line 8  Line 8 
8  #include <list>  #include <list>
9  #include <errno.h>  #include <errno.h>
10  //  //
11    #include <TFile.h>
12    #include <TSystem.h>
13  #include <TSQLResult.h>  #include <TSQLResult.h>
 #include <TRFIOFile.h>  
14  #include <TSQLRow.h>  #include <TSQLRow.h>
15  #include <TTree.h>  #include <TTree.h>
16  #include <TGraph.h>  #include <TGraph.h>
# Line 29  Line 30 
30  #include <varDump/VarDumpEvent.h>  #include <varDump/VarDumpEvent.h>
31  #include <varDump/VarDumpRecord.h>  #include <varDump/VarDumpRecord.h>
32  #include <physics/S4/S4Event.h>  #include <physics/S4/S4Event.h>
   
33  //  //
34  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
35  //  //
36  using namespace std;  using namespace std;
37  using namespace pamela;  using namespace pamela;
 //using namespace yngn::util;  
38    
39  /**  /**
40   * Constructor.   * Constructor.
# Line 49  using namespace pamela; Line 48  using namespace pamela;
48   * @param tsync        file timesync.   * @param tsync        file timesync.
49   * @param debug        debug flag.   * @param debug        debug flag.
50   */   */
51  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug, Long64_t olderthan){
52    //    //
53    //    //
54    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 64  PamelaDBOperations::PamelaDBOperations(T Line 63  PamelaDBOperations::PamelaDBOperations(T
63    SetObt0(obt0);    SetObt0(obt0);
64    //    //
65    //    //
66    SetRootName(filerootname);    INSERT_RAW =!filerawname.IsNull();
67    SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
68    //    //
69    this->OpenFile();    INSERT_ROOT = !filerootname.IsNull();
70      if(INSERT_ROOT){
71              file = TFile::Open(this->GetRootName().Data());
72              if ( !file ) INSERT_ROOT = false;
73      };
74    //  this->OpenFile();
75    //    //
76    this->SetID_RAW(0);    this->SetID_RAW(0);
77    this->SetID_ROOT(0);    this->SetID_ROOT(0);
78    //  
79      VALIDATE = false;
80      clean_time = new TDatime();
81      if(olderthan >= 0){
82            VALIDATE = true;
83            UInt_t timelim = 0;
84            // timelim = now - olderthan
85            timelim =  (UInt_t)clean_time->Convert() - olderthan;
86            clean_time->Set(timelim,false);
87      };
88      
89        //
90  };  };
91    
92  /**  /**
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 94  PamelaDBOperations::PamelaDBOperations(T
94   */   */
95  void PamelaDBOperations::Close(){  void PamelaDBOperations::Close(){
96    if( conn && conn->IsConnected() ) conn->Close();    if( conn && conn->IsConnected() ) conn->Close();
97      delete clean_time;
98    delete glrun;    delete glrun;
99    delete this;    delete this;
100  };  };
# Line 170  void PamelaDBOperations::SetNOBOOT(Bool_ Line 186  void PamelaDBOperations::SetNOBOOT(Bool_
186  };  };
187    
188  /**  /**
189     * Store the olderthan variable
190     * @param olderthan
191     */
192    // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
193    //   olderthan = oldthan;
194    // };
195    
196    /**
197   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
198   */   */
199  Bool_t PamelaDBOperations::SetID_RAW(){  Bool_t PamelaDBOperations::SetID_RAW(){
# Line 181  Bool_t PamelaDBOperations::SetID_RAW(){ Line 205  Bool_t PamelaDBOperations::SetID_RAW(){
205        << " PATH = '" << this->GetRawPath().Data() << "' AND "        << " PATH = '" << this->GetRawPath().Data() << "' AND "
206        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
207    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
208    if (result == NULL) throw -4;    if ( result == NULL ) throw -4;
209    row = result->Next();    row = result->Next();
210    if (row == NULL) return false;    if ( !row ) return(false);
211    delete result;    delete result;
212    id = (UInt_t)atoll(row->GetField(0));    id = (UInt_t)atoll(row->GetField(0));
213    return(true);    return(true);
# Line 216  Int_t PamelaDBOperations::SetUpperLimits Line 240  Int_t PamelaDBOperations::SetUpperLimits
240    ULong64_t upperobt2 = 0;    ULong64_t upperobt2 = 0;
241    UInt_t zomp = 0;    UInt_t zomp = 0;
242    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
243      EventCounter *code=0;
244      //
245      //   pcksList packetsNames;
246      //   pcksList::iterator Iter;
247      //   getPacketsNames(packetsNames);
248    //    //
249    pktfirst = 0;    pktfirst = 0;
250    obtfirst = 0;    obtfirst = 0;
# Line 233  Int_t PamelaDBOperations::SetUpperLimits Line 262  Int_t PamelaDBOperations::SetUpperLimits
262    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
263    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
264    //    //
265      //   code = eh->GetCounter();
266      //   UInt_t en = 0;
267      //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
268      //     en = code->Get(GetPacketType(*Iter));
269      //     if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);
270      //};  
271      //
272    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
273    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
274    pktlast = ph->GetCounter();    pktlast = ph->GetCounter();
# Line 301  Int_t PamelaDBOperations::SetUpperLimits Line 337  Int_t PamelaDBOperations::SetUpperLimits
337    // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)    // check if last runtrailer is within limits, if not extend limits (one should check for all packets but we need only runtrailer)
338    //    //
339    PacketType *pctp=0;    PacketType *pctp=0;
   EventCounter *code=0;  
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
340    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
341    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
342    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 319  Int_t PamelaDBOperations::SetUpperLimits Line 350  Int_t PamelaDBOperations::SetUpperLimits
350    //    //
351    rhev = rh->GetEntries();    rhev = rh->GetEntries();
352    rtev = rt->GetEntries();    rtev = rt->GetEntries();
353      UInt_t sobtt = 0;
354      UInt_t sobth = 0;
355      UInt_t spktt = 0;
356      UInt_t spkth = 0;
357    UInt_t pktt = 0;    UInt_t pktt = 0;
358    ULong64_t obtt = 0;    ULong64_t obtt = 0;
359    UInt_t pkth = 0;    UInt_t pkth = 0;
360    ULong64_t obth = 0;    ULong64_t obth = 0;
361    //    //
362      T->GetEntry(upperentry);
363      code = eh->GetCounter();
364      Int_t lasttrail = code->Get(pctp->RunTrailer);
365      Int_t lasthead = code->Get(pctp->RunHeader);
366    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
367      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
368      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 331  Int_t PamelaDBOperations::SetUpperLimits Line 370  Int_t PamelaDBOperations::SetUpperLimits
370      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
371    };    };
372    //    //
373      if ( lasthead < rhev ){
374        rh->GetEntry(lasthead);
375        phh = ehh->GetPscuHeader();
376        pkth = PKT(phh->GetCounter());
377        obth = OBT(phh->GetOrbitalTime());
378      };
379      //
380      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
381      if ( pkth > upperpkt && obth > upperobt ){
382        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
383        upperpkt = pkth;
384        upperobt = obth;
385        rhev = lasthead+1;
386      } else {
387        rhev = lasthead;
388      };
389      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
390      //
391    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
392    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
393      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);      if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
# Line 341  Int_t PamelaDBOperations::SetUpperLimits Line 398  Int_t PamelaDBOperations::SetUpperLimits
398      rtev = lasttrail;      rtev = lasttrail;
399    };    };
400    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);    if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
401      //  goto kikko;
402      //
403      //
404      // Check if runtrailer/runheader are within lower limits
405      //
406      //
407      pkth = 0;
408      obth = 0;
409      spkth = 0;
410      sobth = 0;
411      for (Int_t k=0; k<rhev; k++){
412        if ( k > 0 ){
413          spkth = pkth;
414          sobth = obth;
415        };
416        rh->GetEntry(k);
417        phh = ehh->GetPscuHeader();
418        pkth = PKT(phh->GetCounter());
419        obth = OBT(phh->GetOrbitalTime());
420        //
421    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
422        //
423        if ( pkth < spkth && obth < sobth ){
424          if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
425          //
426          rhev = k-1;
427          rh->GetEntry(rhev);
428          pkth = spkth;
429          obth = sobth;
430          //
431          UInt_t evbefh = 0;
432          code = ehh->GetCounter();
433          evbefh = code->Get(pctp->Physics);    
434          if ( evbefh >= 0 ){
435            T->GetEntry(evbefh);
436            ph = eh->GetPscuHeader();
437            t_pktlast = PKT(ph->GetCounter());
438            t_obtlast = OBT(ph->GetOrbitalTime());
439            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
440              upperpkt = pkth;
441              upperobt = obth;
442              upperentry = evbefh-1;
443            } else {
444              while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
445                evbefh++;  
446                T->GetEntry(evbefh);
447                ph = eh->GetPscuHeader();
448                t_pktlast = PKT(ph->GetCounter());
449                t_obtlast = OBT(ph->GetOrbitalTime());
450              };
451              T->GetEntry(evbefh-1);
452              ph = eh->GetPscuHeader();
453              upperpkt = PKT(ph->GetCounter());
454              upperobt = OBT(ph->GetOrbitalTime());
455              upperentry = evbefh-1;
456            };    
457          };
458          if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
459          goto kikko0;
460        };
461      };
462     kikko0:
463      //
464      //
465      //
466      pktt = 0;
467      obtt = 0;
468      spktt = 0;
469      sobtt = 0;
470      for (Int_t k=0; k<rtev; k++){
471        if ( k > 0 ){
472          spktt = pktt;
473          sobtt = obtt;
474        };
475        rt->GetEntry(k);
476        pht = eht->GetPscuHeader();
477        pktt = PKT(pht->GetCounter());
478        obtt = OBT(pht->GetOrbitalTime());
479        //
480    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
481        //
482        if ( pktt < spktt && obtt < sobtt ){
483          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
484          //
485          rtev = k-1;
486          rt->GetEntry(rtev);
487          pktt = spktt;
488          obtt = sobtt;
489          if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
490          //
491          UInt_t evbeft = 0;
492          code = eht->GetCounter();
493          evbeft = code->Get(pctp->Physics);    
494          if ( evbeft >= 0 ){
495            T->GetEntry(evbeft);
496            ph = eh->GetPscuHeader();
497            t_pktlast = PKT(ph->GetCounter());
498            t_obtlast = OBT(ph->GetOrbitalTime());
499            if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
500              upperpkt = pktt;
501              upperobt = obtt;
502              upperentry = evbeft-1;
503            } else {
504              while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
505                evbeft++;  
506                T->GetEntry(evbeft);
507                ph = eh->GetPscuHeader();
508                t_pktlast = PKT(ph->GetCounter());
509                t_obtlast = OBT(ph->GetOrbitalTime());
510              };
511              T->GetEntry(evbeft-1);
512              ph = eh->GetPscuHeader();
513              upperpkt = PKT(ph->GetCounter());
514              upperobt = OBT(ph->GetOrbitalTime());
515              upperentry = evbeft-1;
516            };
517          };
518          if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
519          goto kikko;
520          //      break;
521          //
522        };
523        //  
524      };
525      //
526     kikko:
527      //
528      T->GetEntry(upperentry);
529      code = eh->GetCounter();
530      lasttrail = code->Get(pctp->RunTrailer);
531      lasthead = code->Get(pctp->RunHeader);
532      if ( lasttrail < rtev ){
533        rt->GetEntry(lasttrail);
534        pht = eht->GetPscuHeader();
535        pktt = PKT(pht->GetCounter());
536        obtt = OBT(pht->GetOrbitalTime());
537      };
538    //    //
539    if ( lasthead < rhev ){    if ( lasthead < rhev ){
540      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
541      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
542      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
543      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
544    };    };
545    //    //
546    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
# Line 360  Int_t PamelaDBOperations::SetUpperLimits Line 554  Int_t PamelaDBOperations::SetUpperLimits
554    };    };
555    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);    if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
556    //    //
557      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
558      if ( pktt > upperpkt && obtt > upperobt ){
559        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
560        upperpkt = pktt;
561        upperobt = obtt;
562        rtev = lasttrail+1;
563      } else {
564        rtev = lasttrail;
565      };
566      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
567      //
568    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 %llu pkt_num %i upper entry %i \n",upperobt,upperpkt,upperentry);
569    //    //
570    return(0);    return(0);
# Line 440  const PacketType* PamelaDBOperations::Ge Line 645  const PacketType* PamelaDBOperations::Ge
645  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
646  //  //
647    
648  /**  // /**
649   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
650   */  // */
651  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
652    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
653  };  //   //
654    //   if ( !file ) throw -12;
655    // };
656    
657    
658  /**  /**
659   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
660   */   */
661  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
662    //    //
663    // check connection    // check connection
664    //    //
665    if( !conn ) throw -1;        if( !conn ) throw -1;
666    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
667    if( !connect ) throw -1;        if( !connect ) throw -1;
668  };  };
669    
670  /**  /**
# Line 550  void PamelaDBOperations::FillClass(Bool_ Line 755  void PamelaDBOperations::FillClass(Bool_
755      lastPkt = glrun->GetRUNTRAILER_PKT();      lastPkt = glrun->GetRUNTRAILER_PKT();
756    };    };
757    //    //
758    if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported    if ( mishead && mistrail && lastev+1 == firstev ) throw -14; // run with no events, no runtrailer, no runheader... unsupported should never arrive here
759    //    //
760    if ( mishead ) {    if ( mishead ) {
761      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
# Line 668  Int_t PamelaDBOperations::insertPamelaGL Line 873  Int_t PamelaDBOperations::insertPamelaGL
873    };    };
874    //    //
875    TTree *T = 0;    TTree *T = 0;
876      Int_t signal = 0;
877    //    //
878    UInt_t nevent = 0;    UInt_t nevent = 0;
879    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 721  Int_t PamelaDBOperations::insertPamelaGL Line 927  Int_t PamelaDBOperations::insertPamelaGL
927          //          //
928          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
929            existsts = true;            existsts = true;
930            goto out;            goto eout;
931          };          };
932          //          //
933        };        };
# Line 729  Int_t PamelaDBOperations::insertPamelaGL Line 935  Int_t PamelaDBOperations::insertPamelaGL
935    };    };
936    if ( !existsts ) { // try with runheader and runtrailer    if ( !existsts ) { // try with runheader and runtrailer
937      //      //
938        if ( IsDebug() ) printf(" No ts mcmd \n");
939        signal = 2;
940        //
941      TTree *rh=(TTree*)file->Get("RunHeader");      TTree *rh=(TTree*)file->Get("RunHeader");
942      if ( !rh || rh->IsZombie() ) throw -17;      if ( !rh || rh->IsZombie() ) throw -17;
943      TTree *rt=(TTree*)file->Get("RunTrailer");      TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 738  Int_t PamelaDBOperations::insertPamelaGL Line 947  Int_t PamelaDBOperations::insertPamelaGL
947      //      //
948      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
949      //      //
     //    Int_t rhev = rh->GetEntries();  
     //    Int_t rtev = rt->GetEntries();  
     //  
950      if ( rhev > 0 ){      if ( rhev > 0 ){
951        rh->GetEntry(0);        rh->GetEntry(0);
952        //        //
# Line 751  Int_t PamelaDBOperations::insertPamelaGL Line 957  Int_t PamelaDBOperations::insertPamelaGL
957        //        //
958        if ( TSYNC && OBT ){        if ( TSYNC && OBT ){
959          existsts = true;          existsts = true;
960          goto out;          goto eout;
961        };        };
962        //        //
963      };      };
964      if ( rtev > 0 ){      if ( rtev > 0 ){
965          //
966          if ( IsDebug() ) printf(" No runheader \n");
967          signal = 6;
968          //
969        rt->GetEntry(0);        rt->GetEntry(0);
970        //        //
971        TSYNC = runt->LAST_TYME_SYNC_INFO;        TSYNC = runt->LAST_TYME_SYNC_INFO;
# Line 765  Int_t PamelaDBOperations::insertPamelaGL Line 975  Int_t PamelaDBOperations::insertPamelaGL
975        //        //
976        if ( TSYNC && OBT ){        if ( TSYNC && OBT ){
977          existsts = true;          existsts = true;
978          goto out;          goto eout;
979        };        };
980        //        //
981        } else {
982          if ( IsDebug() ) printf(" No runheader \n");
983      };      };
984    };    };
985    //    //
986    if ( !existsts ){ // try with inclination mcmd    if ( !existsts ){ // try with inclination mcmd
987        //
988        if ( IsDebug() ) printf(" No runtrailer \n");
989        signal = 14;
990        //
991      Double_t timesync = 0.;      Double_t timesync = 0.;
992      for (UInt_t i=0; i<nevent;i++){      for (UInt_t i=0; i<nevent;i++){
993        //        //
# Line 800  Int_t PamelaDBOperations::insertPamelaGL Line 1016  Int_t PamelaDBOperations::insertPamelaGL
1016        TYPE = 666;        TYPE = 666;
1017        if ( TSYNC && OBT ){        if ( TSYNC && OBT ){
1018          existsts = true;          existsts = true;
1019          goto out;          goto eout;
1020        };        };
1021      };      };
1022    };    };
1023    //    //
1024    if ( !existsts && obt0 ){ // insert timesync by hand    if ( !existsts && obt0 ){ // insert timesync by hand
1025        //
1026        if ( IsDebug() ) printf(" No incl mcmd \n");
1027        signal = 30;
1028        //
1029      OBT = obt0;      OBT = obt0;
1030      TSYNC = tsync;      TSYNC = tsync;
1031      TYPE = 999;      TYPE = 999;
1032      existsts = true;      existsts = true;
1033      goto out;      goto eout;
1034    };    };
1035    //    //
1036   out:   eout:
1037    //    //
1038    if ( !existsts ) throw -3;    if ( !existsts ) throw -3;
1039    //    //
# Line 829  Int_t PamelaDBOperations::insertPamelaGL Line 1049  Int_t PamelaDBOperations::insertPamelaGL
1049    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1050    //    //
1051    delete result;    delete result;
1052    return(0);    return(signal);
1053  }  }
1054    
1055  /**  /**
# Line 907  Int_t PamelaDBOperations::assignBOOT_NUM Line 1127  Int_t PamelaDBOperations::assignBOOT_NUM
1127        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
1128    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1129    //    //
1130    if ( !result ) return(8);    if ( !result ) throw -4;;
1131    row = result->Next();    row = result->Next();
1132    if ( !row ) return(16);    if ( !row ) return(16);
1133    if ( row->GetField(1) ){    if ( row->GetField(1) ){
1134      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1135      return(1);      return(1);
1136    };    };
1137    if ( !row->GetField(0) ) return(8);    if ( !row->GetField(0) ) throw -26;
1138    //    //
1139    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1140    //    //
# Line 924  Int_t PamelaDBOperations::assignBOOT_NUM Line 1144  Int_t PamelaDBOperations::assignBOOT_NUM
1144    trDumpEv = (TTree*)file->Get("VarDump");    trDumpEv = (TTree*)file->Get("VarDump");
1145    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1146    //    //
   if (trDumpEv == NULL) return(2);  
   //      
1147    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1148    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1149    //    //
1150    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1151    if (trDumpEv->GetEntries() > 0){    if ( trDumpEv->GetEntries() > 0 ){
1152      trDumpEv->GetEntry(0);      Bool_t found = false;
1153      vde->Records->GetEntries();      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1154      if ( !vde->Records->GetEntries() ){        trDumpEv->GetEntry(i);
1155        if ( !this->GetBOOTnumber() ) return(4);        vde->Records->GetEntries();
1156      } else {        if ( vde->Records->GetEntries()>0 ){
1157            found = true;
1158            goto fill;
1159          };
1160        };
1161      fill:
1162        if ( found ){
1163          //
1164        vdr = (VarDumpRecord*)vde->Records->At(6);        vdr = (VarDumpRecord*)vde->Records->At(6);
1165          //
1166        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1167          //
1168        } else {
1169          if ( !this->GetBOOTnumber() ) return(4);
1170      };      };
1171    } else {    } else {
1172      if ( !this->GetBOOTnumber() ) return(2);      if ( !this->GetBOOTnumber() ) return(2);
# Line 1023  Int_t PamelaDBOperations::insertPamelaRU Line 1252  Int_t PamelaDBOperations::insertPamelaRU
1252          //          //
1253          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1254          //          //
1255          if ( (ptt-1) < 0 ) throw -15;          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1256          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
1257          cod = eht->GetCounter();          cod = eht->GetCounter();
1258          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
# Line 1129  Int_t PamelaDBOperations::insertPamelaRU Line 1358  Int_t PamelaDBOperations::insertPamelaRU
1358            if ( (UInt_t)evbeft < upperentry-1 ){            if ( (UInt_t)evbeft < upperentry-1 ){
1359              if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");              if ( IsDebug() ) printf(" Piece of run at the end of the file with NO RUNHEADER!\n");
1360              //              //
1361              if ( (ptt-1) < 0 ) throw -15;              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1362              rt->GetEntry(ptt-1);              rt->GetEntry(ptt-1);
1363              cod = eht->GetCounter();              cod = eht->GetCounter();
1364              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
# Line 2106  void PamelaDBOperations::HandleSuspiciou Line 2335  void PamelaDBOperations::HandleSuspiciou
2335    if ( firstev == lastev+1 ) { // no events inside the run!    if ( firstev == lastev+1 ) { // no events inside the run!
2336      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2337      //      //
     //    if ( IsDebug() ) printf(" -> fill the DB \n");  
     //  
2338      this->FillClass();      this->FillClass();
2339      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);    
2340      //      //
# Line 2318  Int_t PamelaDBOperations::insertCALO_CAL Line 2545  Int_t PamelaDBOperations::insertCALO_CAL
2545    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
2546    nevents = tr->GetEntries();    nevents = tr->GetEntries();
2547    //    //
2548    if ( !nevents ) return(0);    if ( !nevents ) return(1);
2549    //    //
2550    for (UInt_t i=0; i < nevents; i++){    for (UInt_t i=0; i < nevents; i++){
2551      tr->GetEntry(i);      tr->GetEntry(i);
# Line 2635  Int_t PamelaDBOperations::insertTRK_CALI Line 2862  Int_t PamelaDBOperations::insertTRK_CALI
2862    tr2->SetBranchAddress("Header", &eh2);    tr2->SetBranchAddress("Header", &eh2);
2863    nevents2 = tr2->GetEntries();    nevents2 = tr2->GetEntries();
2864    //    //
2865    if ( !nevents1 && !nevents2 ) return(0);    if ( !nevents1 && !nevents2 ) return(1);
2866    //    //
2867    t2 = -1;    t2 = -1;
2868    Int_t pret2 = 0;    Int_t pret2 = 0;
# Line 2807  Int_t PamelaDBOperations::insertS4_CALIB Line 3034  Int_t PamelaDBOperations::insertS4_CALIB
3034    stringstream oss;    stringstream oss;
3035    oss.str("");    oss.str("");
3036    //    //
   CalibS4Event *calibS4    = new  CalibS4Event();  
3037    TTree *tr = 0;    TTree *tr = 0;
3038    EventHeader *eh = 0;    EventHeader *eh = 0;
3039    PscuHeader *ph = 0;    PscuHeader *ph = 0;
# Line 2821  Int_t PamelaDBOperations::insertS4_CALIB Line 3047  Int_t PamelaDBOperations::insertS4_CALIB
3047    tr = (TTree*)file->Get("CalibS4");    tr = (TTree*)file->Get("CalibS4");
3048    if ( !tr || tr->IsZombie() ) throw -24;    if ( !tr || tr->IsZombie() ) throw -24;
3049    //    //
   tr->SetBranchAddress("CalibS4", &calibS4);  
3050    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
3051    //    //
3052    nevents = tr->GetEntries();    nevents = tr->GetEntries();
3053    //    //
3054    if ( !nevents ) return(0);    if ( !nevents ) return(1);
3055    //    //
3056    for (UInt_t i = 0; i < nevents; i++){    for (UInt_t i = 0; i < nevents; i++){
3057      //      //
3058      tr->GetEntry(i);      tr->GetEntry(i);
     TArrayD params = S4_paramfit(calibS4);  
3059      //      //
3060      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
3061      obt = ph->GetOrbitalTime();        obt = ph->GetOrbitalTime();  
# Line 2921  Int_t PamelaDBOperations::insertS4_CALIB Line 3145  Int_t PamelaDBOperations::insertS4_CALIB
3145          };          };
3146          //          //
3147          oss.str("");          oss.str("");
3148          oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,PARAM_FIT0,PARAM_FIT1,OBT,PKT,BOOT_NUMBER,VALIDATION) "          oss << " INSERT INTO GL_S4_CALIB (ID,ID_ROOT_L0,EV_ROOT,FROM_TIME,TO_TIME,OBT,PKT,BOOT_NUMBER) "
3149              << " VALUES (NULL,' "              << " VALUES (NULL,' "
3150              << idroot << "','"              << idroot << "','"
3151              << i << "','"              << i << "','"
3152              << fromtime << "','"              << fromtime << "','"
3153              << totime << "','"              << totime << "','"
             << dec << params.At(0) << "','"  
             << dec << params.At(1) << "','"        
3154              << obt << "','"              << obt << "','"
3155              << pkt << "','"              << pkt << "','"
3156              << this->GetBOOTnumber() << "','"              << this->GetBOOTnumber() << "');";
             << valid << "');";  
3157          //          //
3158          if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());          if ( IsDebug() ) printf(" Insert the new calibration: query is \n %s \n",oss.str().c_str());
3159          //          //
# Line 2953  Int_t PamelaDBOperations::insertS4_CALIB Line 3174  Int_t PamelaDBOperations::insertS4_CALIB
3174    return(0);    return(0);
3175  };  };
3176    
3177    /**
3178  /*   * Scan the fragment table and move old fragments to the GL_RUN table
  * Fit function Received from Valeria Malvezzi 06/02/2006  
3179   */   */
3180  Double_t fitf(Double_t *x, Double_t *par){    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3181    Double_t fitval =(par[0]*x[0])+par[1];    //
3182    return fitval;    TSQLResult *result = 0;
3183  }    TSQLRow    *row    = 0;
3184      TSQLResult *result2 = 0;
3185      TSQLRow    *row2   = 0;
3186      //
3187      UInt_t moved = 0;
3188    //  UInt_t timelim = 0;
3189    //  TDatime *time = new TDatime();
3190      //
3191      stringstream oss;
3192      oss.str("");
3193      //
3194      //
3195      // check if there are entries older than "olderthan" seconds from now
3196      //
3197      oss.str("");
3198      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3199          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3200      //
3201      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3202      result = conn->Query(oss.str().c_str());
3203      //
3204      if ( result ){
3205        //
3206        row = result->Next();
3207        //
3208        if ( row ){
3209          //
3210          oss.str("");
3211          oss << " ID= "<< row->GetField(0);
3212          //
3213          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3214          //
3215          oss.str("");
3216          oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3217              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3218              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3219              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3220              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3221              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3222              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3223              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3224              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3225              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3226              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3227              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3228              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3229              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3230          //
3231          if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3232          result2 = conn->Query(oss.str().c_str());
3233          //
3234          if ( !result2 ) throw -4;
3235          //
3236          row2 = result2->Next();
3237          //
3238          if ( !row2 ){
3239            //
3240            if ( IsDebug() ) printf(" The run is new \n");
3241            if ( IsDebug() ) printf(" -> fill the DB \n");      
3242            //
3243            glrun->SetID(0);
3244            glrun->Fill_GL_RUN(conn);  
3245            //
3246            oss.str("");
3247            oss << " SELECT ID FROM GL_RUN WHERE "
3248                << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3249                << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3250                << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3251                << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3252                << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3253            //
3254            if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3255            result2 = conn->Query(oss.str().c_str());
3256            //
3257            if ( !result2 ) throw -4;
3258            //
3259            row2 = result2->Next();
3260            //
3261            if ( !row2 ) throw -25;
3262            //
3263            oss.str("");
3264            oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3265            if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3266            result2 = conn->Query(oss.str().c_str());
3267            //
3268            if ( !result2 ) throw -4;
3269            //
3270            moved++;
3271            //
3272          } else {
3273            if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3274          };
3275          if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3276          //
3277          //
3278          oss.str("");
3279          oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3280          if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3281          result2 = conn->Query(oss.str().c_str());
3282          //
3283          if ( !result2 ) throw -4;
3284          //
3285        };
3286      };
3287      if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3288      return(0);
3289    };
3290    
3291  /*  /**
3292   * Fit the S4 calibration with a straight line - Received from Valeria Malvezzi 06/02/2006   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3293   */   */
3294  TArrayD PamelaDBOperations::S4_paramfit(pamela::CalibS4Event *S4CalibEvent){        Int_t PamelaDBOperations::ValidateRuns(){
3295      //
3296    //----------- variable initialization -------------------------------------------------    TSQLResult *result = 0;
3297      TSQLRow    *row    = 0;
3298    Double_t mip[3]={1, 30, 300};    //
3299    Double_t adc[3] = {0.,0.,0.};    stringstream oss;
3300      oss.str("");
3301      //
3302      // =======================================================
3303      // validate runs by checking missing calibrations
3304      // =======================================================
3305      UInt_t t_stop  = 0;
3306      UInt_t t_start = 0;
3307      // --------------------------------------------------------------
3308      // 1) get the OBT of the last run inserted after clean-time limit
3309      // --------------------------------------------------------------
3310      oss.str("");
3311      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3312              << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3313      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3314      result = conn->Query(oss.str().c_str());
3315      if ( !result ) throw -4;
3316      if ( !result->GetRowCount() ) {
3317              printf(" No runs to validate \n");
3318              return(1);
3319      }else{
3320            row = result->Next();
3321            t_start = (UInt_t)atoll(row->GetField(4));
3322      };  
3323      // --------------------------------------------------------------
3324      // 2) get the OBT of the last validated run
3325      // --------------------------------------------------------------
3326      oss.str("");
3327      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3328          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3329      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3330      result = conn->Query(oss.str().c_str());
3331      if ( !result ) throw -4;
3332      if ( result->GetRowCount() ){
3333              row = result->Next();
3334              t_stop = (UInt_t)atoll(row->GetField(4));
3335      };
3336      if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3337      // --------------------------------------------------------------
3338      // now retrieves runs to be validated
3339      // --------------------------------------------------------------
3340      oss.str("");
3341      oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3342      oss << " AND RUNHEADER_TIME >="<< t_stop;
3343      oss << " ORDER BY RUNHEADER_TIME DESC;";
3344    //  if ( IsDebug() )
3345      if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3346      result = conn->Query(oss.str().c_str());
3347      if ( !result ) throw -4;
3348      if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3349    //  printf("------------------------------------------------------------------------------- \n");
3350        
3351    TArrayD parametri(2);    Int_t nrow = 0;
3352      GL_RUN* this_run = new GL_RUN();
3353    valid = 1;    GL_RUN* next_run = new GL_RUN();
3354      Int_t   nseq_max = 1000;
3355    //------------ Fit calibrations and find parameters to calibrate data ------------------  //  UInt_t* sequence = new UInt_t[100];
3356    pamela::S4::S4Event  *s4Record;    vector<UInt_t> sequence(nseq_max);
3357      Int_t   nseq = 0;
3358      Bool_t CHECK = false;
3359      Bool_t this_ONLINE = false;
3360      Bool_t next_ONLINE = false;
3361      UInt_t t1=0,t2=0;
3362      // ---------------------------------------------------------------------------------
3363      // - loop over runs, back in time,
3364      // - select sequences of runs close in time (less than 60 s apart),
3365      //   which could be preceeded by a calibration
3366      // - check if there might be a missing calibration
3367      // ---------------------------------------------------------------------------------
3368      while(1){
3369              
3370              row = result->Next();
3371              if( row == NULL ) break;
3372              
3373              //------------
3374              //get run info
3375              //------------
3376              this_run->Set_GL_RUN(row);
3377                      
3378              Bool_t this_BAD = false;
3379              if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3380              else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3381              else{
3382    //                printf("Missing or corrupted header!! \n");
3383                      this_ONLINE = false;
3384                      this_BAD = true;
3385              };
3386    
3387    for (Int_t j = 0; j < 4; j++){            //-----------------------------------
3388      for (Int_t i = 0; i < 128; i++){            //compare with previous(next in time)
3389        s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i));            //-----------------------------------
3390        switch (j) {            CHECK = false;
3391        case 0 :{            UInt_t interval=0;
3392          adc[0]=adc[0]+((s4Record->S4_DATA)-32);            
3393          break;            if( nrow != 0){
3394        };          
3395        case 1 :{                    
3396          adc[1]=adc[1]+((s4Record->S4_DATA)-32);                    t1 = this_run->GetRUNTRAILER_TIME();
3397          break;                    t2 = next_run->GetRUNHEADER_TIME();
3398        };                    interval = (t2-t1);
3399        case 3 :{                    
3400          adc[2]=adc[2]+((s4Record->S4_DATA)-32);                    if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE
3401          break;                            
3402        };                            if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3403        };                            
3404      };                            if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration
3405                              
3406                              if( !CHECK && this_run->VALIDATION ){
3407                                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3408                                      nseq=0;
3409                              }
3410                      
3411                      }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3412                              
3413                              CHECK = true;
3414    
3415                      }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3416                              
3417                              assignVALIDATION(next_run->ID,true);
3418                              nseq=0;
3419                      }
3420              }
3421    
3422              //----------------------------
3423              //check run sequence for calib
3424              //----------------------------
3425              if( CHECK ){
3426                      // check if calibration exists
3427                      if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3428                      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3429                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3430                      nseq=0;
3431              };
3432              //--------------
3433              //store run info
3434              //--------------
3435              *next_run   = *this_run;
3436              next_ONLINE = this_ONLINE;
3437              if( !this_BAD ){
3438                      if(nseq < nseq_max){
3439                              sequence[nseq] = this_run->ID;
3440                              nseq++;
3441                      }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3442              };
3443              
3444              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3445              nrow++;
3446              
3447    };    };
3448        delete this_run;
3449    adc[0]=adc[0]/128;    delete next_run;
3450    adc[1]=adc[1]/128;    //
3451    adc[2]=adc[2]/128;    return(0);
3452      };
3453    TGraph *fitpar = new TGraph (3, adc, mip);  /**
3454    TF1 *func = new TF1("fitf", fitf, -0., 1000., 2); // definizione della funzione, 2 = num. parametri   * Check if there might be a missing tracker calibration in a given time interval
3455       * @param t1 From absolute time
3456    func->SetParameters(0.3,1.);        //inizializzazione dei parametri a 1   * @param t2 To absolute time
3457    func->SetParNames("m","q");      //definisce il nome dei parametri   * @return true if there might be a missing calibration
3458    fitpar->Fit(func,"qr");          //fitta fitpar con la funzione func nel range definito nella funzione   */
3459    //fitpar->Fit(func,"r");          //fitta fitpar con la funzione func nel range definito nella funzione  Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3460                
3461    parametri[0] = func -> GetParameter(0);          GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3462    parametri[1] = func -> GetParameter(1);          
3463            // get the closest VALIDATED calibration before the run start (t2)
3464            if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing
3465            
3466            if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing
3467            
3468            //==============================================================
3469            // Check is done first on the basis of time between calibration,
3470            // which should be equal to the time between ascending-nodes.
3471            //==============================================================
3472            if ( t2 - trkcalib->FROM_TIME > 5700) {
3473                    if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3474            //==============================================================
3475            // there might be a missing calibration, due to:
3476            // - MM full
3477            // - corrupted packets
3478            // - loss of data
3479            // There is an exception in case a download was done during ascending node
3480            //==============================================================
3481                    Bool_t DOWNLOAD = false;
3482                    // check if the calib was skipped becouse of download .... DA FARE!!
3483                    if(DOWNLOAD)return(false);
3484                    
3485                    return(true);                                   //>>> missing
3486                    
3487            };
3488            
3489            //==============================================================
3490            // If the last calibration is close to the run less than this time,
3491            // it is enough to say that there are no missing calibrations
3492            //==============================================================
3493            // the long time interval bewteen runs might be due to download
3494            if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3495            return(false);
3496            
3497    };
3498    /**
3499     * Assign VALIDATION value to a GL_RUN entry
3500     * @param idrun Run ID
3501     * @param validation true/false
3502     */
3503    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3504            TSQLResult *result = 0;
3505            stringstream oss;
3506            oss.str("");
3507            oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3508            //
3509    //      if ( IsDebug() )
3510    //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3511            if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3512            result = conn->Query(oss.str().c_str());
3513            if ( !result ) throw -4;
3514            return(0);
3515    }
3516    
3517    
   if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.8 || parametri[1] > 1. ) valid = 0;  
3518    
   if ( IsDebug() ) printf(" par1 = %g par2 = %g\n",parametri[0],parametri[1]);  
   
   return parametri;  
 };  

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.8

  ViewVC Help
Powered by ViewVC 1.1.23