/[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.13 by mocchiut, Fri Oct 20 11:11:12 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 <cTle.h>
35    #include <cEci.h>
36    #include <cJulian.h>
37    
38  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
39  //  //
40  using namespace std;  using namespace std;
41  using namespace pamela;  using namespace pamela;
42  //using namespace yngn::util;  
43    // Some function to work with cTle stuff.
44    bool compTLE(cTle* tle1, cTle *tle2);
45    float getTleJulian(cTle *);
46    string getTleDatetime(cTle*);
47    
48  /**  /**
49   * Constructor.   * Constructor.
# Line 48  using namespace pamela; Line 56  using namespace pamela;
56   * @param obt0         file obt0.   * @param obt0         file obt0.
57   * @param tsync        file timesync.   * @param tsync        file timesync.
58   * @param debug        debug flag.   * @param debug        debug flag.
59     * @param tlefilename  ascii file with TLE 3 line elements.
60   */   */
61  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){  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){
62    //    //
63    //    //
64    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 63  PamelaDBOperations::PamelaDBOperations(T Line 72  PamelaDBOperations::PamelaDBOperations(T
72    SetTsync(tsync);    SetTsync(tsync);
73    SetObt0(obt0);    SetObt0(obt0);
74    //    //
75      SetTLEPath(tlefilename);
76    //    //
   SetRootName(filerootname);  
   SetRawName(filerawname);  
77    //    //
78    this->OpenFile();    INSERT_RAW =!filerawname.IsNull();
79      if(INSERT_RAW)SetRawName(filerawname);
80      //
81      INSERT_ROOT = !filerootname.IsNull();
82      if( INSERT_ROOT ){
83        this->SetRootName(filerootname);
84        this->SetOrbitNo();
85        file = TFile::Open(this->GetRootName().Data());
86      };
87    //    //
88    this->SetID_RAW(0);    this->SetID_RAW(0);
89    this->SetID_ROOT(0);    this->SetID_ROOT(0);
90    
91      VALIDATE = false;
92      
93    //    //
94  };  };
95    
# Line 79  PamelaDBOperations::PamelaDBOperations(T Line 98  PamelaDBOperations::PamelaDBOperations(T
98   */   */
99  void PamelaDBOperations::Close(){  void PamelaDBOperations::Close(){
100    if( conn && conn->IsConnected() ) conn->Close();    if( conn && conn->IsConnected() ) conn->Close();
101      delete clean_time;
102    delete glrun;    delete glrun;
103    delete this;    delete this;
104  };  };
# Line 87  void PamelaDBOperations::Close(){ Line 107  void PamelaDBOperations::Close(){
107  // SETTERS  // SETTERS
108  //  //
109    
110    //
111    // must be out of the constructor in order to FORCE the validation of the latest runs in case you run the validation together with the latest file
112    //
113    void PamelaDBOperations::CheckValidate(Long64_t olderthan){
114      clean_time = new TDatime();
115      if(olderthan >= 0){
116        VALIDATE = true;
117        UInt_t timelim = 0;
118        timelim =  (UInt_t)clean_time->Convert() - olderthan;
119        clean_time->Set(timelim,false);
120      };
121    };
122    
123  /**  /**
124   * Open the DB connection   * Open the DB connection
125   * @param host         hostname for the SQL connection.   * @param host         hostname for the SQL connection.
# Line 94  void PamelaDBOperations::Close(){ Line 127  void PamelaDBOperations::Close(){
127   * @param password     password for the SQL connection.   * @param password     password for the SQL connection.
128   */   */
129  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){  void PamelaDBOperations::SetConnection(TString host, TString user, TString password){
130      if ( IsDebug() ) printf(" Connecting using host = %s user = %s password = %s \n",host.Data(),user.Data(),password.Data());
131    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());    conn = TSQLServer::Connect(host.Data(),user.Data(),password.Data());
132  };  };
133    
# Line 162  void PamelaDBOperations::SetRootName(TSt Line 196  void PamelaDBOperations::SetRootName(TSt
196  };  };
197    
198  /**  /**
199     * Store the downlink orbit number from filename.
200     */
201    void PamelaDBOperations::SetOrbitNo(){
202      dworbit = 0;
203      TString name = this->GetRootFile();
204      Int_t nlength = name.Length();
205      if ( nlength < 5 ) return;
206      TString dwo = 0;
207      for (Int_t i = 0; i<5; i++){
208        dwo.Append(name[i],1);
209      };
210      if ( dwo.IsDigit() ){
211        dworbit = (UInt_t)dwo.Atoi();
212      } else {
213        dwo="";
214        for (Int_t i = 8; i<13; i++){
215          dwo.Append(name[i],1);
216        };    
217        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
218      };
219      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
220      return;
221    };
222    
223    
224    
225    /**
226   * Store the NOBOOT flag.   * Store the NOBOOT flag.
227   * @param noboot    true/false.   * @param noboot    true/false.
228   */   */
# Line 170  void PamelaDBOperations::SetNOBOOT(Bool_ Line 231  void PamelaDBOperations::SetNOBOOT(Bool_
231  };  };
232    
233  /**  /**
234     * Store path to the TLE file.
235     */
236    void PamelaDBOperations::SetTLEPath(TString str){
237      tlefilename = str;
238    };
239    
240    /**
241     * Store the olderthan variable
242     * @param olderthan
243     */
244    // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
245    //   olderthan = oldthan;
246    // };
247    
248    /**
249   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
250   */   */
251  Bool_t PamelaDBOperations::SetID_RAW(){  Bool_t PamelaDBOperations::SetID_RAW(){
# Line 181  Bool_t PamelaDBOperations::SetID_RAW(){ Line 257  Bool_t PamelaDBOperations::SetID_RAW(){
257        << " PATH = '" << this->GetRawPath().Data() << "' AND "        << " PATH = '" << this->GetRawPath().Data() << "' AND "
258        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
259    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
260    if (result == NULL) throw -4;    if ( result == NULL ) throw -4;
261    row = result->Next();    row = result->Next();
262    if (row == NULL) return false;    if ( !row ) return(false);
263    delete result;    delete result;
264    id = (UInt_t)atoll(row->GetField(0));    id = (UInt_t)atoll(row->GetField(0));
265    return(true);    return(true);
# Line 216  Int_t PamelaDBOperations::SetUpperLimits Line 292  Int_t PamelaDBOperations::SetUpperLimits
292    ULong64_t upperobt2 = 0;    ULong64_t upperobt2 = 0;
293    UInt_t zomp = 0;    UInt_t zomp = 0;
294    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
295      EventCounter *code=0;
296      //
297      UInt_t deltapkt = 5000;
298      ULong64_t deltaobt = 50000;
299      //
300      //   pcksList packetsNames;
301      //   pcksList::iterator Iter;
302      //   getPacketsNames(packetsNames);
303    //    //
304    pktfirst = 0;    pktfirst = 0;
305    obtfirst = 0;    obtfirst = 0;
# Line 233  Int_t PamelaDBOperations::SetUpperLimits Line 317  Int_t PamelaDBOperations::SetUpperLimits
317    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
318    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
319    //    //
320      //   code = eh->GetCounter();
321      //   UInt_t en = 0;
322      //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
323      //     en = code->Get(GetPacketType(*Iter));
324      //     if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);
325      //};  
326      //
327    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
328    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
329    pktlast = ph->GetCounter();    pktlast = ph->GetCounter();
# Line 252  Int_t PamelaDBOperations::SetUpperLimits Line 343  Int_t PamelaDBOperations::SetUpperLimits
343    //    //
344    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
345    //    //
346    if ( PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst) ){    if ( (PKT(pktlast) < PKT(pktfirst) && OBT(obtlast) < OBT(obtfirst)) || (labs(PKT(pktlast)-PKT(pktfirst))<deltapkt && labs(OBT(obtlast)-OBT(obtfirst))<deltaobt) ){
347        //
348      // go back      // go back
349      zomp = nevent - 2;      zomp = nevent - 2;
350      //      //
# Line 301  Int_t PamelaDBOperations::SetUpperLimits Line 393  Int_t PamelaDBOperations::SetUpperLimits
393    // 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)
394    //    //
395    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);  
396    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
397    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
398    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 319  Int_t PamelaDBOperations::SetUpperLimits Line 406  Int_t PamelaDBOperations::SetUpperLimits
406    //    //
407    rhev = rh->GetEntries();    rhev = rh->GetEntries();
408    rtev = rt->GetEntries();    rtev = rt->GetEntries();
409      UInt_t sobtt = 0;
410      UInt_t sobth = 0;
411      UInt_t spktt = 0;
412      UInt_t spkth = 0;
413    UInt_t pktt = 0;    UInt_t pktt = 0;
414    ULong64_t obtt = 0;    ULong64_t obtt = 0;
415    UInt_t pkth = 0;    UInt_t pkth = 0;
416    ULong64_t obth = 0;    ULong64_t obth = 0;
417    //    //
418      T->GetEntry(upperentry);
419      code = eh->GetCounter();
420      Int_t lasttrail = code->Get(pctp->RunTrailer);
421      Int_t lasthead = code->Get(pctp->RunHeader);
422    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
423      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
424      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 331  Int_t PamelaDBOperations::SetUpperLimits Line 426  Int_t PamelaDBOperations::SetUpperLimits
426      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
427    };    };
428    //    //
429      if ( lasthead < rhev ){
430        rh->GetEntry(lasthead);
431        phh = ehh->GetPscuHeader();
432        pkth = PKT(phh->GetCounter());
433        obth = OBT(phh->GetOrbitalTime());
434      };
435      //
436      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
437      if ( pkth > upperpkt && obth > upperobt ){
438        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
439        upperpkt = pkth;
440        upperobt = obth;
441        rhev = lasthead+1;
442      } else {
443        rhev = lasthead;
444      };
445      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
446      //
447    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);
448    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
449      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 454  Int_t PamelaDBOperations::SetUpperLimits
454      rtev = lasttrail;      rtev = lasttrail;
455    };    };
456    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);
457      //  goto kikko;
458      //
459      //
460      // Check if runtrailer/runheader are within lower limits
461      //
462      //
463      pkth = 0;
464      obth = 0;
465      spkth = 0;
466      sobth = 0;
467      for (Int_t k=0; k<rhev; k++){
468        if ( k > 0 ){
469          spkth = pkth;
470          sobth = obth;
471        };
472        rh->GetEntry(k);
473        phh = ehh->GetPscuHeader();
474        pkth = PKT(phh->GetCounter());
475        obth = OBT(phh->GetOrbitalTime());
476        //
477    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
478        //
479        if ( pkth < spkth && obth < sobth ){
480          if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
481          //
482          rhev = k-1;
483          rh->GetEntry(rhev);
484          pkth = spkth;
485          obth = sobth;
486          //
487          UInt_t evbefh = 0;
488          code = ehh->GetCounter();
489          evbefh = code->Get(pctp->Physics);    
490          if ( evbefh >= 0 ){
491            T->GetEntry(evbefh);
492            ph = eh->GetPscuHeader();
493            t_pktlast = PKT(ph->GetCounter());
494            t_obtlast = OBT(ph->GetOrbitalTime());
495            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
496              upperpkt = pkth;
497              upperobt = obth;
498              upperentry = evbefh-1;
499            } else {
500              while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
501                evbefh++;  
502                T->GetEntry(evbefh);
503                ph = eh->GetPscuHeader();
504                t_pktlast = PKT(ph->GetCounter());
505                t_obtlast = OBT(ph->GetOrbitalTime());
506              };
507              T->GetEntry(evbefh-1);
508              ph = eh->GetPscuHeader();
509              upperpkt = PKT(ph->GetCounter());
510              upperobt = OBT(ph->GetOrbitalTime());
511              upperentry = evbefh-1;
512            };    
513          };
514          if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
515          goto kikko0;
516        };
517      };
518     kikko0:
519      //
520      //
521      //
522      pktt = 0;
523      obtt = 0;
524      spktt = 0;
525      sobtt = 0;
526      for (Int_t k=0; k<rtev; k++){
527        if ( k > 0 ){
528          spktt = pktt;
529          sobtt = obtt;
530        };
531        rt->GetEntry(k);
532        pht = eht->GetPscuHeader();
533        pktt = PKT(pht->GetCounter());
534        obtt = OBT(pht->GetOrbitalTime());
535        //
536    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
537        //
538        if ( pktt < spktt && obtt < sobtt ){
539          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
540          //
541          rtev = k-1;
542          rt->GetEntry(rtev);
543          pktt = spktt;
544          obtt = sobtt;
545          if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
546          //
547          UInt_t evbeft = 0;
548          code = eht->GetCounter();
549          evbeft = code->Get(pctp->Physics);    
550          if ( evbeft >= 0 ){
551            T->GetEntry(evbeft);
552            ph = eh->GetPscuHeader();
553            t_pktlast = PKT(ph->GetCounter());
554            t_obtlast = OBT(ph->GetOrbitalTime());
555            if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
556              upperpkt = pktt;
557              upperobt = obtt;
558              upperentry = evbeft-1;
559            } else {
560              while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
561                evbeft++;  
562                T->GetEntry(evbeft);
563                ph = eh->GetPscuHeader();
564                t_pktlast = PKT(ph->GetCounter());
565                t_obtlast = OBT(ph->GetOrbitalTime());
566              };
567              T->GetEntry(evbeft-1);
568              ph = eh->GetPscuHeader();
569              upperpkt = PKT(ph->GetCounter());
570              upperobt = OBT(ph->GetOrbitalTime());
571              upperentry = evbeft-1;
572            };
573          };
574          if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
575          goto kikko;
576          //      break;
577          //
578        };
579        //  
580      };
581      //
582     kikko:
583      //
584      T->GetEntry(upperentry);
585      code = eh->GetCounter();
586      lasttrail = code->Get(pctp->RunTrailer);
587      lasthead = code->Get(pctp->RunHeader);
588      if ( lasttrail < rtev ){
589        rt->GetEntry(lasttrail);
590        pht = eht->GetPscuHeader();
591        pktt = PKT(pht->GetCounter());
592        obtt = OBT(pht->GetOrbitalTime());
593      };
594    //    //
595    if ( lasthead < rhev ){    if ( lasthead < rhev ){
596      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
597      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
598      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
599      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
600    };    };
601    //    //
602    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 610  Int_t PamelaDBOperations::SetUpperLimits
610    };    };
611    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);
612    //    //
613      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
614      if ( pktt > upperpkt && obtt > upperobt ){
615        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
616        upperpkt = pktt;
617        upperobt = obtt;
618        rtev = lasttrail+1;
619      } else {
620        rtev = lasttrail;
621      };
622      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
623      //
624    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);
625    //    //
626    return(0);    return(0);
627  }  }
628    
629    /**
630     *
631     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
632     * Entries in the _RUNID_GEN table are never deleted.
633     *
634     **/
635    UInt_t PamelaDBOperations::AssignRunID(){
636      //
637      TSQLResult *result = 0;
638      TSQLRow *row = 0;
639      UInt_t runid = 0;
640      //
641      stringstream   oss;
642      //  
643      oss.str("");
644      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
645      result = conn->Query(oss.str().c_str());
646      if ( !result ) throw -10;
647      oss.str("");
648      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
649      result = conn->Query(oss.str().c_str());
650      if ( !result ) throw -10;
651      //
652      row = result->Next();
653      //
654      if ( !row ) throw -28;
655      //
656      runid = (UInt_t)atoll(row->GetField(0));
657      //
658      return(runid);
659    };
660    
661  //  //
662  // GETTERS  // GETTERS
663  //  //
# Line 440  const PacketType* PamelaDBOperations::Ge Line 733  const PacketType* PamelaDBOperations::Ge
733  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
734  //  //
735    
736  /**  // /**
737   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
738   */  // */
739  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
740    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
741    //   //
742    
743    void PamelaDBOperations::CheckFile(){  
744      if ( !file ) throw -12;
745  };  };
746    
747    
748  /**  /**
749   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
750   */   */
751  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
752    //    //
753    // check connection    // check connection
754    //    //
755    if( !conn ) throw -1;        if( !conn ) throw -1;
756    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
757    if( !connect ) throw -1;        if( !connect ) throw -1;
758      if ( !dworbit ) throw -27;
759  };  };
760    
761  /**  /**
# Line 550  void PamelaDBOperations::FillClass(Bool_ Line 846  void PamelaDBOperations::FillClass(Bool_
846      lastPkt = glrun->GetRUNTRAILER_PKT();      lastPkt = glrun->GetRUNTRAILER_PKT();
847    };    };
848    //    //
849    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
850    //    //
851    if ( mishead ) {    if ( mishead ) {
852      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
# Line 639  Int_t PamelaDBOperations::insertPamelaGL Line 935  Int_t PamelaDBOperations::insertPamelaGL
935    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
936    //    //
937    oss.str("");    oss.str("");
938    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
939        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
940    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
941    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
942    if ( !result ) throw -10;    if ( !result ) throw -10;
943    row = result->Next();    row = result->Next();
944    if ( !row ) throw -10;    //
945      if ( !row ){
946        oss.str("");
947        oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
948            << dworbit << " order by FROM_ORBIT desc limit 1;";
949        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
950        result = conn->Query(oss.str().c_str());
951        if ( !result ) throw -10;
952        row = result->Next();
953        if ( !row ) throw -10;
954      };
955    //    //
956    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();
957    /*    /*
# Line 668  Int_t PamelaDBOperations::insertPamelaGL Line 973  Int_t PamelaDBOperations::insertPamelaGL
973    };    };
974    //    //
975    TTree *T = 0;    TTree *T = 0;
976      Int_t signal = 0;
977    //    //
978    UInt_t nevent = 0;    UInt_t nevent = 0;
979    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 719  Int_t PamelaDBOperations::insertPamelaGL Line 1025  Int_t PamelaDBOperations::insertPamelaGL
1025          //          //
1026          TYPE = 55;//224;          TYPE = 55;//224;
1027          //          //
1028            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1029            //
1030          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1031            existsts = true;            existsts = true;
1032            goto out;            goto eout;
1033          };          };
1034          //          //
1035        };        };
# Line 729  Int_t PamelaDBOperations::insertPamelaGL Line 1037  Int_t PamelaDBOperations::insertPamelaGL
1037    };    };
1038    if ( !existsts ) { // try with runheader and runtrailer    if ( !existsts ) { // try with runheader and runtrailer
1039      //      //
1040        if ( IsDebug() ) printf(" No ts mcmd \n");
1041        signal = 2;
1042        //
1043      TTree *rh=(TTree*)file->Get("RunHeader");      TTree *rh=(TTree*)file->Get("RunHeader");
1044      if ( !rh || rh->IsZombie() ) throw -17;      if ( !rh || rh->IsZombie() ) throw -17;
1045      TTree *rt=(TTree*)file->Get("RunTrailer");      TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 738  Int_t PamelaDBOperations::insertPamelaGL Line 1049  Int_t PamelaDBOperations::insertPamelaGL
1049      //      //
1050      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1051      //      //
1052      //    Int_t rhev = rh->GetEntries();      Int_t nrhev = rh->GetEntries();
1053      //    Int_t rtev = rt->GetEntries();      Int_t nrtev = rt->GetEntries();
1054        if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1055      //      //
1056      if ( rhev > 0 ){      if ( nrhev > 0 ){
1057        rh->GetEntry(0);        for (Int_t i=0; i<nrhev; i++){
1058        //          //
1059        TSYNC = runh->LAST_TIME_SYNC_INFO;          rh->GetEntry(i);
1060        OBT = runh->OBT_TIME_SYNC * 1000;          //
1061        //          TSYNC = runh->LAST_TIME_SYNC_INFO;
1062        TYPE = 20;          OBT = runh->OBT_TIME_SYNC * 1000;
1063        //          //
1064        if ( TSYNC && OBT ){          TYPE = 20;
1065          existsts = true;          //
1066          goto out;          if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1067            //
1068            if ( TSYNC && OBT ){
1069              existsts = true;
1070              goto eout;
1071            };
1072        };        };
1073        //        //
1074      };      };
1075      if ( rtev > 0 ){      if ( nrtev > 0 ){
       rt->GetEntry(0);  
1076        //        //
1077        TSYNC = runt->LAST_TYME_SYNC_INFO;        if ( IsDebug() ) printf(" No runheader \n");
1078        OBT = runt->OBT_TYME_SYNC * 1000;        signal = 6;
1079        //        //
1080        TYPE = 21;        for (Int_t i=0; i<nrtev; i++){
1081        //          //
1082        if ( TSYNC && OBT ){          rt->GetEntry(i);
1083          existsts = true;          //
1084          goto out;          TSYNC = runt->LAST_TYME_SYNC_INFO;
1085            OBT = runt->OBT_TYME_SYNC * 1000;
1086            //
1087            TYPE = 21;
1088            //
1089            if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1090            //
1091            if ( TSYNC && OBT ){
1092              existsts = true;
1093              goto eout;
1094            };
1095        };        };
1096        //        //
1097        } else {
1098          if ( IsDebug() ) printf(" No runheader \n");
1099      };      };
1100    };    };
1101    //    //
1102    if ( !existsts ){ // try with inclination mcmd    if ( !existsts ){ // try with inclination mcmd
1103        //
1104        if ( IsDebug() ) printf(" No runtrailer \n");
1105        signal = 14;
1106        //
1107      Double_t timesync = 0.;      Double_t timesync = 0.;
1108      for (UInt_t i=0; i<nevent;i++){      for (UInt_t i=0; i<nevent;i++){
1109        //        //
# Line 800  Int_t PamelaDBOperations::insertPamelaGL Line 1132  Int_t PamelaDBOperations::insertPamelaGL
1132        TYPE = 666;        TYPE = 666;
1133        if ( TSYNC && OBT ){        if ( TSYNC && OBT ){
1134          existsts = true;          existsts = true;
1135          goto out;          goto eout;
1136        };        };
1137      };      };
1138    };    };
1139    //    //
1140    if ( !existsts && obt0 ){ // insert timesync by hand    if ( !existsts && obt0 ){ // insert timesync by hand
1141        //
1142        if ( IsDebug() ) printf(" No incl mcmd \n");
1143        signal = 30;
1144        //
1145      OBT = obt0;      OBT = obt0;
1146      TSYNC = tsync;      TSYNC = tsync;
1147      TYPE = 999;      TYPE = 999;
1148      existsts = true;      existsts = true;
1149      goto out;      goto eout;
1150    };    };
1151    //    //
1152   out:   eout:
1153    //    //
1154    if ( !existsts ) throw -3;    if ( !existsts ) throw -3;
1155    //    //
# Line 829  Int_t PamelaDBOperations::insertPamelaGL Line 1165  Int_t PamelaDBOperations::insertPamelaGL
1165    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1166    //    //
1167    delete result;    delete result;
1168    return(0);    return(signal);
1169  }  }
1170    
1171  /**  /**
# Line 907  Int_t PamelaDBOperations::assignBOOT_NUM Line 1243  Int_t PamelaDBOperations::assignBOOT_NUM
1243        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
1244    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1245    //    //
1246    if ( !result ) return(8);    if ( !result ) throw -4;;
1247    row = result->Next();    row = result->Next();
1248    if ( !row ) return(16);    if ( !row ) return(16);
1249    if ( row->GetField(1) ){    if ( row->GetField(1) ){
1250      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1251      return(1);      return(1);
1252    };    };
1253    if ( !row->GetField(0) ) return(8);    if ( !row->GetField(0) ) throw -26;
1254    //    //
1255    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1256    //    //
# Line 924  Int_t PamelaDBOperations::assignBOOT_NUM Line 1260  Int_t PamelaDBOperations::assignBOOT_NUM
1260    trDumpEv = (TTree*)file->Get("VarDump");    trDumpEv = (TTree*)file->Get("VarDump");
1261    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1262    //    //
   if (trDumpEv == NULL) return(2);  
   //      
1263    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1264    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1265    //    //
1266    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1267    if (trDumpEv->GetEntries() > 0){    if ( trDumpEv->GetEntries() > 0 ){
1268      trDumpEv->GetEntry(0);      Bool_t found = false;
1269      vde->Records->GetEntries();      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1270      if ( !vde->Records->GetEntries() ){        trDumpEv->GetEntry(i);
1271        if ( !this->GetBOOTnumber() ) return(4);        vde->Records->GetEntries();
1272      } else {        if ( vde->Records->GetEntries()>0 ){
1273            found = true;
1274            goto fill;
1275          };
1276        };
1277      fill:
1278        if ( found ){
1279          //
1280        vdr = (VarDumpRecord*)vde->Records->At(6);        vdr = (VarDumpRecord*)vde->Records->At(6);
1281          //
1282        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1283          //
1284        } else {
1285          if ( !this->GetBOOTnumber() ) return(4);
1286      };      };
1287    } else {    } else {
1288      if ( !this->GetBOOTnumber() ) return(2);      if ( !this->GetBOOTnumber() ) return(2);
# Line 1023  Int_t PamelaDBOperations::insertPamelaRU Line 1368  Int_t PamelaDBOperations::insertPamelaRU
1368          //          //
1369          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1370          //          //
1371          if ( (ptt-1) < 0 ) throw -15;          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1372          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
1373          cod = eht->GetCounter();          cod = eht->GetCounter();
1374          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
# Line 1129  Int_t PamelaDBOperations::insertPamelaRU Line 1474  Int_t PamelaDBOperations::insertPamelaRU
1474            if ( (UInt_t)evbeft < upperentry-1 ){            if ( (UInt_t)evbeft < upperentry-1 ){
1475              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");
1476              //              //
1477              if ( (ptt-1) < 0 ) throw -15;              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1478              rt->GetEntry(ptt-1);              rt->GetEntry(ptt-1);
1479              cod = eht->GetCounter();              cod = eht->GetCounter();
1480              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
# Line 1286  void PamelaDBOperations::HandleRun(){   Line 1631  void PamelaDBOperations::HandleRun(){  
1631      //      //
1632      this->FillClass();      this->FillClass();
1633      //      //
1634      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1635          glrun->SetID(this->AssignRunID());
1636          glrun->SetID_RUN_FRAG(0);
1637          glrun->Fill_GL_RUN(conn);
1638        };
1639    } else {    } else {
1640      //      //
1641      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);      if ( IsDebug() ) printf(" oh no! the distance between runheader and runtrailer seems wrong: check %llu pktt %llu \n",chkpkt,pktt);
# Line 1458  void PamelaDBOperations::HandleRunFragme Line 1807  void PamelaDBOperations::HandleRunFragme
1807        };        };
1808        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: %i %i %llu %llu firstev is %i\n",apkt,bpkt,aobt,bobt,firstev);
1809        //        //
       glrun1->SetID(0);  
1810        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1811        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1812        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1486  void PamelaDBOperations::HandleRunFragme Line 1834  void PamelaDBOperations::HandleRunFragme
1834        //        //
1835        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1836          //          //
1837            glrun->SetID(this->AssignRunID());
1838            glrun->SetID_RUN_FRAG(glrun1->GetID());
1839          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1840          //          //
1841          // get id number          // get id number
1842          //          //
1843          oss.str("");  //      oss.str("");
1844          oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
1845              << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
1846              << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
1847          //  //      //
1848          if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
1849          //  //      //
1850          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1851          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1852          row = result->Next();  //      row = result->Next();
1853          //  //      //
1854          UInt_t idrun0 = 0;  //      UInt_t idrun0 = 0;
1855          if ( !row ){  //      if ( !row ){
1856            throw -10;  //        throw -10;
1857          } else {  //      } else {
1858            idrun0 = (UInt_t)atoll(row->GetField(0));  //        idrun0 = (UInt_t)atoll(row->GetField(0));
1859          };  //      };
         //  
         glrun1->SetID_RUN_FRAG(idrun0);  
1860          //          //
1861            //      glrun1->SetID_RUN_FRAG(idrun0);
1862            glrun1->SetID_RUN_FRAG(glrun->GetID());
1863          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1864          //          //
1865          oss.str("");  //      oss.str("");
1866          oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
1867          //  //      //
1868          if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
1869          //  //      //
1870          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1871          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1872          row = result->Next();  //      row = result->Next();
1873          //  //      //
1874          UInt_t idrun1 = 0;  //      UInt_t idrun1 = 0;
1875          if ( !row ){  //      if ( !row ){
1876            throw -10;  //        throw -10;
1877          } else {  //      } else {
1878            idrun1 = (UInt_t)atoll(row->GetField(0));  //        idrun1 = (UInt_t)atoll(row->GetField(0));
1879          };  //      };
1880          //  //      //
1881          oss.str("");  //      oss.str("");
1882          oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
1883          //  //      //
1884          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
1885          if ( !result ) throw -4;  //      if ( !result ) throw -4;
1886          //          //
1887        };        };
1888        //        //
# Line 1682  void PamelaDBOperations::HandleRunFragme Line 2032  void PamelaDBOperations::HandleRunFragme
2032        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2033        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2034        //        //
       glrun1->SetID(0);  
2035        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2036        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2037        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1702  void PamelaDBOperations::HandleRunFragme Line 2051  void PamelaDBOperations::HandleRunFragme
2051        //        //
2052        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2053          //          //
2054            glrun->SetID(this->AssignRunID());
2055            //
2056            glrun->SetID_RUN_FRAG(glrun1->GetID());
2057          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
2058          //          //
2059          // get id number          // get id number
2060          //          //
2061          oss.str("");  //      oss.str("");
2062          oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  //      oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "
2063              << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  //          << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "
2064              << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  //          << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";
2065          //  //      //
2066          if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());
2067          //  //      //
2068          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2069          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2070          row = result->Next();  //      row = result->Next();
2071          //  //      //
2072          UInt_t idrun0 = 0;  //      UInt_t idrun0 = 0;
2073          if ( !row ){  //      if ( !row ){
2074            throw -10;  //        throw -10;
2075          } else {  //      } else {
2076            idrun0 = (UInt_t)atoll(row->GetField(0));  //        idrun0 = (UInt_t)atoll(row->GetField(0));
2077          };  //      };
2078          //  //      //
2079          glrun1->SetID_RUN_FRAG(idrun0);  //      glrun1->SetID_RUN_FRAG(idrun0);
2080          //  //
2081            glrun1->SetID_RUN_FRAG(glrun->GetID());
2082          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2083          //          //
2084          oss.str("");  //      oss.str("");
2085          oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  //      oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";
2086          //  //      //
2087          if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  //      if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());
2088          //  //      //
2089          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2090          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2091          row = result->Next();  //      row = result->Next();
2092          //  //      //
2093          UInt_t idrun1 = 0;  //      UInt_t idrun1 = 0;
2094          if ( !row ){  //      if ( !row ){
2095            throw -10;  //        throw -10;
2096          } else {  //      } else {
2097            idrun1 = (UInt_t)atoll(row->GetField(0));  //        idrun1 = (UInt_t)atoll(row->GetField(0));
2098          };  //      };
2099          //  //      //
2100          oss.str("");  //      oss.str("");
2101          oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";
2102          //  //      //
2103          result =  conn->Query(oss.str().c_str());  //      result =  conn->Query(oss.str().c_str());
2104          if ( !result ) throw -4;  //      if ( !result ) throw -4;
2105          //          //
2106        };        };
2107        //        //
# Line 1832  void PamelaDBOperations::HandleRunFragme Line 2185  void PamelaDBOperations::HandleRunFragme
2185          if ( IsDebug() ) printf(" The run is new \n");          if ( IsDebug() ) printf(" The run is new \n");
2186          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");          if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
2187          //          //
2188            glrun->SetID(this->AssignRunID());
2189            glrun->SetID_RUN_FRAG(0);
2190          glrun->Fill_GL_RUN_FRAGMENTS(conn);          glrun->Fill_GL_RUN_FRAGMENTS(conn);
2191          //          //
2192        } else {        } else {
# Line 1864  void PamelaDBOperations::HandleMissingHo Line 2219  void PamelaDBOperations::HandleMissingHo
2219      //      //
2220      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2221      //      //
2222      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2223          glrun->SetID(this->AssignRunID());
2224          glrun->SetID_RUN_FRAG(0);
2225          glrun->Fill_GL_RUN(conn);    
2226        };
2227      //      //
2228    };    };
2229    //    //
# Line 2031  Bool_t PamelaDBOperations::IsRunConsiste Line 2390  Bool_t PamelaDBOperations::IsRunConsiste
2390            //                  //      
2391            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2392            //            //
2393            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2394                glrun->SetID(this->AssignRunID());
2395                glrun->SetID_RUN_FRAG(0);
2396                glrun->Fill_GL_RUN(conn);      
2397              };
2398            //            //
2399            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2400            //            //
# Line 2106  void PamelaDBOperations::HandleSuspiciou Line 2469  void PamelaDBOperations::HandleSuspiciou
2469    if ( firstev == lastev+1 ) { // no events inside the run!    if ( firstev == lastev+1 ) { // no events inside the run!
2470      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2471      //      //
     //    if ( IsDebug() ) printf(" -> fill the DB \n");  
     //  
2472      this->FillClass();      this->FillClass();
2473      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2474          glrun->SetID(this->AssignRunID());
2475          glrun->SetID_RUN_FRAG(0);
2476          glrun->Fill_GL_RUN(conn);    
2477        };
2478      //      //
2479    } else {    } else {
2480      //      //
# Line 2137  void PamelaDBOperations::HandleSuspiciou Line 2502  void PamelaDBOperations::HandleSuspiciou
2502        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");        if ( IsDebug() ) printf(" No packets but physics inside the run, I will consider it as good\n");
2503        //        //
2504        this->FillClass();        this->FillClass();
2505        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2506            glrun->SetID(this->AssignRunID());
2507            glrun->SetID_RUN_FRAG(0);
2508            glrun->Fill_GL_RUN(conn);        
2509          };
2510        //        //
2511      } else {      } else {
2512        //        //
# Line 2235  void PamelaDBOperations::HandleSuspiciou Line 2604  void PamelaDBOperations::HandleSuspiciou
2604            //                  //      
2605            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2606            //            //
2607            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2608                glrun->SetID(this->AssignRunID());
2609                glrun->SetID_RUN_FRAG(0);
2610                glrun->Fill_GL_RUN(conn);      
2611              };
2612            //            //
2613            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...            if ( i == lastev && checkfirst < check ){ // if the last event gives a wrong check...
2614              //              //
# Line 2270  void PamelaDBOperations::HandleSuspiciou Line 2643  void PamelaDBOperations::HandleSuspiciou
2643              //                  //    
2644              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2645              //              //
2646              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2647                  glrun->SetID(this->AssignRunID());
2648                  glrun->SetID_RUN_FRAG(0);
2649                  glrun->Fill_GL_RUN(conn);      
2650                };
2651            };            };
2652            //            //
2653            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2318  Int_t PamelaDBOperations::insertCALO_CAL Line 2695  Int_t PamelaDBOperations::insertCALO_CAL
2695    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
2696    nevents = tr->GetEntries();    nevents = tr->GetEntries();
2697    //    //
2698    if ( !nevents ) return(0);    if ( !nevents ) return(1);
2699    //    //
2700    for (UInt_t i=0; i < nevents; i++){    for (UInt_t i=0; i < nevents; i++){
2701      tr->GetEntry(i);      tr->GetEntry(i);
# Line 2635  Int_t PamelaDBOperations::insertTRK_CALI Line 3012  Int_t PamelaDBOperations::insertTRK_CALI
3012    tr2->SetBranchAddress("Header", &eh2);    tr2->SetBranchAddress("Header", &eh2);
3013    nevents2 = tr2->GetEntries();    nevents2 = tr2->GetEntries();
3014    //    //
3015    if ( !nevents1 && !nevents2 ) return(0);    if ( !nevents1 && !nevents2 ) return(1);
3016    //    //
3017    t2 = -1;    t2 = -1;
3018    Int_t pret2 = 0;    Int_t pret2 = 0;
# Line 2807  Int_t PamelaDBOperations::insertS4_CALIB Line 3184  Int_t PamelaDBOperations::insertS4_CALIB
3184    stringstream oss;    stringstream oss;
3185    oss.str("");    oss.str("");
3186    //    //
   CalibS4Event *calibS4    = new  CalibS4Event();  
3187    TTree *tr = 0;    TTree *tr = 0;
3188    EventHeader *eh = 0;    EventHeader *eh = 0;
3189    PscuHeader *ph = 0;    PscuHeader *ph = 0;
# Line 2821  Int_t PamelaDBOperations::insertS4_CALIB Line 3197  Int_t PamelaDBOperations::insertS4_CALIB
3197    tr = (TTree*)file->Get("CalibS4");    tr = (TTree*)file->Get("CalibS4");
3198    if ( !tr || tr->IsZombie() ) throw -24;    if ( !tr || tr->IsZombie() ) throw -24;
3199    //    //
   tr->SetBranchAddress("CalibS4", &calibS4);  
3200    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
3201    //    //
3202    nevents = tr->GetEntries();    nevents = tr->GetEntries();
3203    //    //
3204    if ( !nevents ) return(0);    if ( !nevents ) return(1);
3205    //    //
3206    for (UInt_t i = 0; i < nevents; i++){    for (UInt_t i = 0; i < nevents; i++){
3207      //      //
3208      tr->GetEntry(i);      tr->GetEntry(i);
     TArrayD params = S4_paramfit(calibS4);  
3209      //      //
3210      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
3211      obt = ph->GetOrbitalTime();        obt = ph->GetOrbitalTime();  
# Line 2921  Int_t PamelaDBOperations::insertS4_CALIB Line 3295  Int_t PamelaDBOperations::insertS4_CALIB
3295          };          };
3296          //          //
3297          oss.str("");          oss.str("");
3298          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) "
3299              << " VALUES (NULL,' "              << " VALUES (NULL,' "
3300              << idroot << "','"              << idroot << "','"
3301              << i << "','"              << i << "','"
3302              << fromtime << "','"              << fromtime << "','"
3303              << totime << "','"              << totime << "','"
             << dec << params.At(0) << "','"  
             << dec << params.At(1) << "','"        
3304              << obt << "','"              << obt << "','"
3305              << pkt << "','"              << pkt << "','"
3306              << this->GetBOOTnumber() << "','"              << this->GetBOOTnumber() << "');";
             << valid << "');";  
3307          //          //
3308          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());
3309          //          //
# Line 2953  Int_t PamelaDBOperations::insertS4_CALIB Line 3324  Int_t PamelaDBOperations::insertS4_CALIB
3324    return(0);    return(0);
3325  };  };
3326    
3327    /**
3328     * Scan the fragment table and move old fragments to the GL_RUN table
3329     */
3330    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3331      //
3332      TSQLResult *result = 0;
3333      TSQLRow    *row    = 0;
3334      TSQLResult *result2 = 0;
3335      TSQLRow    *row2   = 0;
3336      //
3337      UInt_t moved = 0;
3338    //  UInt_t timelim = 0;
3339    //  TDatime *time = new TDatime();
3340      //
3341      stringstream oss;
3342      oss.str("");
3343      //
3344      //
3345      // check if there are entries older than "olderthan" seconds from now
3346      //
3347      oss.str("");
3348      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3349          << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3350      //
3351      if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3352      result = conn->Query(oss.str().c_str());
3353      //
3354      if ( result ){
3355        //
3356        row = result->Next();
3357        //
3358        while ( row ){
3359          //
3360          oss.str("");
3361          oss << " ID= "<< row->GetField(0);
3362          //
3363          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3364          //
3365          oss.str("");
3366          oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3367              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3368              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3369              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3370              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3371              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3372              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3373              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3374              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3375              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3376              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3377              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3378              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3379              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3380          //
3381          if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3382          result2 = conn->Query(oss.str().c_str());
3383          //
3384          if ( !result2 ) throw -4;
3385          //
3386          row2 = result2->Next();
3387          //
3388          if ( !row2 ){
3389            //
3390            if ( IsDebug() ) printf(" The run is new \n");
3391            if ( IsDebug() ) printf(" -> fill the DB \n");      
3392            //
3393            //      glrun->SetID(this->AssignRunID()); we use the old run number!
3394            glrun->SetID_RUN_FRAG(glrun->GetID());
3395            glrun->Fill_GL_RUN(conn);  
3396            //
3397    //      oss.str("");
3398    //      oss << " SELECT ID FROM GL_RUN WHERE "
3399    //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3400    //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3401    //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3402    //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3403    //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3404    //      //
3405    //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3406    //      result2 = conn->Query(oss.str().c_str());
3407    //      //
3408    //      if ( !result2 ) throw -4;
3409    //      //
3410    //      row2 = result2->Next();
3411    //      //
3412    //      if ( !row2 ) throw -25;
3413    //      //
3414    //      oss.str("");
3415    //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3416    //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3417    //      result2 = conn->Query(oss.str().c_str());
3418    //      //
3419    //      if ( !result2 ) throw -4;
3420            //
3421            moved++;
3422            //
3423          } else {
3424            if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3425          };
3426          if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3427          //
3428          //
3429          oss.str("");
3430          oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3431          if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3432          result2 = conn->Query(oss.str().c_str());
3433          //
3434          if ( !result2 ) throw -4;
3435          //
3436          row = result->Next();
3437        };
3438      };
3439      if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3440      return(0);
3441    };
3442    
3443  /*  /**
3444   * Fit function Received from Valeria Malvezzi 06/02/2006   * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3445   */   */
3446  Double_t fitf(Double_t *x, Double_t *par){    Int_t PamelaDBOperations::ValidateRuns(){
3447    Double_t fitval =(par[0]*x[0])+par[1];    //
3448    return fitval;    TSQLResult *result = 0;
3449      TSQLRow    *row    = 0;
3450      //
3451      stringstream oss;
3452      oss.str("");
3453      //
3454      // =======================================================
3455      // validate runs by checking missing calibrations
3456      // =======================================================
3457      UInt_t t_stop  = 0;
3458      UInt_t t_start = 0;
3459      // --------------------------------------------------------------
3460      // 1) get the OBT of the last run inserted after clean-time limit
3461      // --------------------------------------------------------------
3462      oss.str("");
3463      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3464              << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3465      if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3466      result = conn->Query(oss.str().c_str());
3467      if ( !result ) throw -4;
3468      if ( !result->GetRowCount() ) {
3469              printf(" No runs to validate \n");
3470              return(1);
3471      }else{
3472            row = result->Next();
3473            t_start = (UInt_t)atoll(row->GetField(4));
3474      };  
3475      // --------------------------------------------------------------
3476      // 2) get the OBT of the last validated run
3477      // --------------------------------------------------------------
3478      oss.str("");
3479      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3480          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3481      if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3482      result = conn->Query(oss.str().c_str());
3483      if ( !result ) throw -4;
3484      if ( result->GetRowCount() ){
3485              row = result->Next();
3486              t_stop = (UInt_t)atoll(row->GetField(4));
3487      };
3488      if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3489      // --------------------------------------------------------------
3490      // now retrieves runs to be validated
3491      // --------------------------------------------------------------
3492      oss.str("");
3493      oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3494      oss << " AND RUNHEADER_TIME >="<< t_stop;
3495      oss << " ORDER BY RUNHEADER_TIME DESC;";
3496    //  if ( IsDebug() )
3497      if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3498      result = conn->Query(oss.str().c_str());
3499      if ( !result ) throw -4;
3500      if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3501    //  printf("------------------------------------------------------------------------------- \n");
3502      
3503      Int_t nrow = 0;
3504      GL_RUN* this_run = new GL_RUN();
3505      GL_RUN* next_run = new GL_RUN();
3506      Int_t   nseq_max = 1000;
3507    //  UInt_t* sequence = new UInt_t[100];
3508      vector<UInt_t> sequence(nseq_max);
3509      Int_t   nseq = 0;
3510      Bool_t CHECK = false;
3511      Bool_t this_ONLINE = false;
3512      Bool_t next_ONLINE = false;
3513      UInt_t t1=0,t2=0;
3514      // ---------------------------------------------------------------------------------
3515      // - loop over runs, back in time,
3516      // - select sequences of runs close in time (less than 60 s apart),
3517      //   which could be preceeded by a calibration
3518      // - check if there might be a missing calibration
3519      // ---------------------------------------------------------------------------------
3520      while(1){
3521              
3522              row = result->Next();
3523              if( row == NULL ) break;
3524              
3525              //------------
3526              //get run info
3527              //------------
3528              this_run->Set_GL_RUN(row);
3529                      
3530              Bool_t this_BAD = false;
3531              if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3532              else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3533              else{
3534    //                printf("Missing or corrupted header!! \n");
3535                      this_ONLINE = false;
3536                      this_BAD = true;
3537              };
3538    
3539              //-----------------------------------
3540              //compare with previous(next in time)
3541              //-----------------------------------
3542              CHECK = false;
3543              UInt_t interval=0;
3544              
3545              if( nrow != 0){
3546            
3547                      
3548                      t1 = this_run->GetRUNTRAILER_TIME();
3549                      t2 = next_run->GetRUNHEADER_TIME();
3550                      interval = (t2-t1);
3551                      
3552                      if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE
3553                              
3554                              if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3555                              
3556                              if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration
3557                              
3558                              if( !CHECK && this_run->VALIDATION ){
3559                                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3560                                      nseq=0;
3561                              }
3562                      
3563                      }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3564                              
3565                              CHECK = true;
3566    
3567                      }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3568                              
3569                              assignVALIDATION(next_run->ID,true);
3570                              nseq=0;
3571                      }
3572              }
3573    
3574              //----------------------------
3575              //check run sequence for calib
3576              //----------------------------
3577              if( CHECK ){
3578                      // check if calibration exists
3579                      if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3580                      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3581                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3582                      nseq=0;
3583              };
3584              //--------------
3585              //store run info
3586              //--------------
3587              *next_run   = *this_run;
3588              next_ONLINE = this_ONLINE;
3589              if( !this_BAD ){
3590                      if(nseq < nseq_max){
3591                              sequence[nseq] = this_run->ID;
3592                              nseq++;
3593                      }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3594              };
3595              
3596              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3597              nrow++;
3598              
3599      };
3600      delete this_run;
3601      delete next_run;
3602      //
3603      return(0);
3604    };
3605    /**
3606     * Check if there might be a missing tracker calibration in a given time interval
3607     * @param t1 From absolute time
3608     * @param t2 To absolute time
3609     * @return true if there might be a missing calibration
3610     */
3611    Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3612            
3613            GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3614            
3615            // get the closest VALIDATED calibration before the run start (t2)
3616            if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing
3617            
3618            if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing
3619            
3620            //==============================================================
3621            // Check is done first on the basis of time between calibration,
3622            // which should be equal to the time between ascending-nodes.
3623            //==============================================================
3624            if ( t2 - trkcalib->FROM_TIME > 5700) {
3625                    if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3626            //==============================================================
3627            // there might be a missing calibration, due to:
3628            // - MM full
3629            // - corrupted packets
3630            // - loss of data
3631            // There is an exception in case a download was done during ascending node
3632            //==============================================================
3633                    Bool_t DOWNLOAD = false;
3634                    // check if the calib was skipped becouse of download .... DA FARE!!
3635                    if(DOWNLOAD)return(false);
3636                    
3637                    return(true);                                   //>>> missing
3638                    
3639            };
3640            
3641            //==============================================================
3642            // If the last calibration is close to the run less than this time,
3643            // it is enough to say that there are no missing calibrations
3644            //==============================================================
3645            // the long time interval bewteen runs might be due to download
3646            if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3647            return(false);
3648            
3649    };
3650    /**
3651     * Assign VALIDATION value to a GL_RUN entry
3652     * @param idrun Run ID
3653     * @param validation true/false
3654     */
3655    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3656            TSQLResult *result = 0;
3657            stringstream oss;
3658            oss.str("");
3659            oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3660            //
3661    //      if ( IsDebug() )
3662    //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3663            if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3664            result = conn->Query(oss.str().c_str());
3665            if ( !result ) throw -4;
3666            return(0);
3667  }  }
3668    
 /*  
  * Fit the S4 calibration with a straight line - Received from Valeria Malvezzi 06/02/2006  
  */  
 TArrayD PamelaDBOperations::S4_paramfit(pamela::CalibS4Event *S4CalibEvent){        
3669    
   //----------- variable initialization -------------------------------------------------  
3670    
3671    Double_t mip[3]={1, 30, 300};  // Insert TLEs from file tlefilename in the table GL_TLE in the db
3672    Double_t adc[3] = {0.,0.,0.};  // opened by conn, sorting them by date from older to newer, if each
3673      // TLE has not been alread inserted.
3674    TArrayD parametri(2);  Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3675    {
3676    valid = 1;    fstream tlefile(tlefilename, ios::in);
3677    
3678      if ( !tlefile ) throw -7;
3679    
3680      vector<cTle*> ctles;
3681      vector<cTle*>::iterator iter;
3682      int present = 0;
3683    
3684      // Get three lines from tlefile, create a cTle object and put it
3685      // into ctles
3686      while(1) {
3687        cTle *tlef;
3688        string str1, str2, str3;
3689    
3690        getline(tlefile, str1);
3691        if(tlefile.eof()) break;
3692    
3693        getline(tlefile, str2);
3694        if(tlefile.eof()) break;
3695    
3696        getline(tlefile, str3);
3697        if(tlefile.eof()) break;
3698    
3699        // We now have three good lines for a cTle.
3700        tlef = new cTle(str1, str2, str3);
3701        ctles.push_back(tlef);
3702      }
3703    
3704      tlefile.close();
3705    
3706      // Sort by date
3707      sort(ctles.begin(), ctles.end(), compTLE);
3708    
3709      // Now we insert each TLE into the db
3710      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3711        cTle *tle = *iter;
3712    
3713        // Do nothing if it's already present in the db.  Just increase
3714        // the counter present.
3715        if (! isTlePresent(tle))
3716          {
3717            int status = insertTle(tle);
3718    
3719            // Insert query failed.  Return 1.
3720            if(status == EXIT_FAILURE) {
3721              
3722              if( IsDebug() ) {
3723                cerr << "Error: inserting TLE:" << endl
3724                     << tle->getName() << endl
3725                     << tle->getLine1() << endl
3726                     << tle->getLine2() << endl;
3727              }
3728    
3729              throw -4;
3730              return 1;
3731            }
3732    
3733          }
3734        else
3735          present++;
3736    
3737      }
3738    
3739      int inserted = ctles.size() - present;  // Number of inserted TLE.
3740      if ( IsDebug() )
3741        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3742             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3743    
3744      ctles.clear();
3745    
3746    
3747      // Return 2 if no new TLE has been inserted.  0 otherwise.
3748      if(! inserted ) return 2;
3749      return 0;
3750    }
3751    
   //------------ Fit calibrations and find parameters to calibrate data ------------------  
   pamela::S4::S4Event  *s4Record;  
3752    
3753    for (Int_t j = 0; j < 4; j++){  // Insert tle in the table GL_TLE using the connection conn.
3754      for (Int_t i = 0; i < 128; i++){  int PamelaDBOperations::insertTle(cTle *tle)
3755        s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i));  {
3756        switch (j) {    stringstream oss;
3757        case 0 :{    TSQLResult *result = 0;
         adc[0]=adc[0]+((s4Record->S4_DATA)-32);  
         break;  
       };  
       case 1 :{  
         adc[1]=adc[1]+((s4Record->S4_DATA)-32);  
         break;  
       };  
       case 3 :{  
         adc[2]=adc[2]+((s4Record->S4_DATA)-32);  
         break;  
       };  
       };  
     };  
   };  
     
   adc[0]=adc[0]/128;  
   adc[1]=adc[1]/128;  
   adc[2]=adc[2]/128;  
     
   TGraph *fitpar = new TGraph (3, adc, mip);  
   TF1 *func = new TF1("fitf", fitf, -0., 1000., 2); // definizione della funzione, 2 = num. parametri  
     
   func->SetParameters(0.3,1.);        //inizializzazione dei parametri a 1  
   func->SetParNames("m","q");      //definisce il nome dei parametri  
   fitpar->Fit(func,"qr");          //fitta fitpar con la funzione func nel range definito nella funzione  
   //fitpar->Fit(func,"r");          //fitta fitpar con la funzione func nel range definito nella funzione  
       
   parametri[0] = func -> GetParameter(0);  
   parametri[1] = func -> GetParameter(1);  
3758    
3759    if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.8 || parametri[1] > 1. ) valid = 0;    oss.str("");
3760      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3761          << " VALUES ( '"
3762          << tle->getName() << "', '"
3763          << tle->getLine1() << "', '"
3764          << tle->getLine2() << "', '"
3765          << getTleDatetime(tle) << "')";
3766    
3767    if ( IsDebug() ) printf(" par1 = %g par2 = %g\n",parametri[0],parametri[1]);    //  cout << oss.str().c_str() << endl;
3768      result = conn->Query(oss.str().c_str());
3769    return parametri;    if (result == NULL)
3770  };      return EXIT_FAILURE;
3771    
3772      return EXIT_SUCCESS;
3773    }
3774    
3775    
3776    // Return whether tle is already in the db connected by conn.
3777    bool PamelaDBOperations::isTlePresent(cTle *tle)
3778    {
3779      stringstream oss;
3780      TSQLResult *result = 0;
3781    
3782      oss.str("");
3783      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3784          << getTleDatetime(tle) << "'";
3785    
3786      result = conn->Query(oss.str().c_str());
3787      if (result == NULL) throw -4;
3788    
3789      if (result->GetRowCount())
3790        return true;
3791      else
3792        return false;
3793    }
3794    
3795    
3796    // Return whether the first TLE is dated early than the second
3797    bool compTLE (cTle *tle1, cTle *tle2)
3798    {
3799      return getTleJulian(tle1) < getTleJulian(tle2);
3800    }
3801    
3802    
3803    // Return the date of the tle using the format (year-2000)*1e3 +
3804    // julian day.  e.g. 6365 is the 31th Dec 2006.
3805    // It does *not* return a cJulian date.
3806    float getTleJulian(cTle *tle) {
3807      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3808    }
3809    
3810    
3811    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3812    // format.
3813    string getTleDatetime(cTle *tle)
3814    {
3815      int year, mon, day, hh, mm, ss;
3816      double dom; // day of month (is double!)
3817      stringstream date; // date in datetime format
3818    
3819      // create a cJulian from the date in tle
3820      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3821    
3822      // get year, month, day of month
3823      jdate.getComponent(&year, &mon, &dom);
3824    
3825      // build a datetime YYYY-MM-DD hh:mm:ss
3826      date.str("");
3827      day = (int) floor(dom);
3828      hh = (int) floor( (dom - day) * 24);
3829      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3830      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3831      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3832    
3833      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3834    
3835      return date.str();
3836    }

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

  ViewVC Help
Powered by ViewVC 1.1.23