/[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.16 by mocchiut, Tue Oct 31 15:36:04 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 <sgp4.h>
35    
36  #include <PamelaDBOperations.h>  #include <PamelaDBOperations.h>
37  //  //
38  using namespace std;  using namespace std;
39  using namespace pamela;  using namespace pamela;
40  //using namespace yngn::util;  
41    // Some function to work with cTle stuff.
42    bool compTLE(cTle* tle1, cTle *tle2);
43    float getTleJulian(cTle *);
44    string getTleDatetime(cTle*);
45    
46  /**  /**
47   * Constructor.   * Constructor.
# Line 48  using namespace pamela; Line 54  using namespace pamela;
54   * @param obt0         file obt0.   * @param obt0         file obt0.
55   * @param tsync        file timesync.   * @param tsync        file timesync.
56   * @param debug        debug flag.   * @param debug        debug flag.
57     * @param tlefilename  ascii file with TLE 3 line elements.
58   */   */
59  PamelaDBOperations::PamelaDBOperations(TString host, TString user, TString password, TString filerawname, TString filerootname, UInt_t boot, UInt_t tsync, UInt_t obt0, Bool_t debug){  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){
60    //    //
61    //    //
62    SetConnection(host,user,password);    SetConnection(host,user,password);
# Line 63  PamelaDBOperations::PamelaDBOperations(T Line 70  PamelaDBOperations::PamelaDBOperations(T
70    SetTsync(tsync);    SetTsync(tsync);
71    SetObt0(obt0);    SetObt0(obt0);
72    //    //
73      SetTLEPath(tlefilename);
74      //
75    //    //
76    SetRootName(filerootname);    INSERT_RAW =!filerawname.IsNull();
77    SetRawName(filerawname);    if(INSERT_RAW)SetRawName(filerawname);
78    //    //
79    this->OpenFile();    INSERT_ROOT = !filerootname.IsNull();
80      if( INSERT_ROOT ){
81        this->SetRootName(filerootname);
82        this->SetOrbitNo();
83        file = TFile::Open(this->GetRootName().Data());
84      } else {
85        this->SetRootName("");
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 122  void PamelaDBOperations::SetDebugFlag(Bo Line 156  void PamelaDBOperations::SetDebugFlag(Bo
156  };  };
157    
158  /**  /**
159     * Set the nofrag flag
160     *
161     */
162    void PamelaDBOperations::SetNoFrag(Bool_t nf){
163      NOFRAG = nf;
164    };
165    
166    /**
167   * Store the BOOT number of the RAW file.   * Store the BOOT number of the RAW file.
168   * @param boot    BOOT number of the RAW file   * @param boot    BOOT number of the RAW file
169   */   */
# Line 162  void PamelaDBOperations::SetRootName(TSt Line 204  void PamelaDBOperations::SetRootName(TSt
204  };  };
205    
206  /**  /**
207     * Store the downlink orbit number from filename.
208     */
209    void PamelaDBOperations::SetOrbitNo(){
210      dworbit = 0;
211      TString name = this->GetRootFile();
212      Int_t nlength = name.Length();
213      if ( nlength < 5 ) return;
214      TString dwo = 0;
215      for (Int_t i = 0; i<5; i++){
216        dwo.Append(name[i],1);
217      };
218      if ( dwo.IsDigit() ){
219        dworbit = (UInt_t)dwo.Atoi();
220      } else {
221        dwo="";
222        for (Int_t i = 8; i<13; i++){
223          dwo.Append(name[i],1);
224        };    
225        if ( dwo.IsDigit() ) dworbit = (UInt_t)dwo.Atoi();
226      };
227      if ( IsDebug() ) printf(" Downlink orbit is %i (dwo = %s) \n",dworbit,dwo.Data());
228      return;
229    };
230    
231    
232    
233    /**
234   * Store the NOBOOT flag.   * Store the NOBOOT flag.
235   * @param noboot    true/false.   * @param noboot    true/false.
236   */   */
# Line 170  void PamelaDBOperations::SetNOBOOT(Bool_ Line 239  void PamelaDBOperations::SetNOBOOT(Bool_
239  };  };
240    
241  /**  /**
242     * Store path to the TLE file.
243     */
244    void PamelaDBOperations::SetTLEPath(TString str){
245      tlefilename = str;
246    };
247    
248    /**
249     * Store the olderthan variable
250     * @param olderthan
251     */
252    // void PamelaDBOperations::SetOlderThan(Long64_t oldthan){
253    //   olderthan = oldthan;
254    // };
255    
256    /**
257   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.   * Retrieve the ID_RAW, if exists, returns NULL if does not exist.
258   */   */
259  Bool_t PamelaDBOperations::SetID_RAW(){  Bool_t PamelaDBOperations::SetID_RAW(){
# Line 181  Bool_t PamelaDBOperations::SetID_RAW(){ Line 265  Bool_t PamelaDBOperations::SetID_RAW(){
265        << " PATH = '" << this->GetRawPath().Data() << "' AND "        << " PATH = '" << this->GetRawPath().Data() << "' AND "
266        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
267    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
268    if (result == NULL) throw -4;    if ( result == NULL ) throw -4;
269    row = result->Next();    row = result->Next();
270    if (row == NULL) return false;    if ( !row ) return(false);
271    delete result;    delete result;
272    id = (UInt_t)atoll(row->GetField(0));    id = (UInt_t)atoll(row->GetField(0));
273    return(true);    return(true);
# Line 216  Int_t PamelaDBOperations::SetUpperLimits Line 300  Int_t PamelaDBOperations::SetUpperLimits
300    ULong64_t upperobt2 = 0;    ULong64_t upperobt2 = 0;
301    UInt_t zomp = 0;    UInt_t zomp = 0;
302    UInt_t jump = 50000; // was 5000    UInt_t jump = 50000; // was 5000
303      EventCounter *code=0;
304      //
305      UInt_t deltapkt = 5000;
306      ULong64_t deltaobt = 50000;
307      //
308      //   pcksList packetsNames;
309      //   pcksList::iterator Iter;
310      //   getPacketsNames(packetsNames);
311    //    //
312    pktfirst = 0;    pktfirst = 0;
313    obtfirst = 0;    obtfirst = 0;
# Line 233  Int_t PamelaDBOperations::SetUpperLimits Line 325  Int_t PamelaDBOperations::SetUpperLimits
325    pktfirst = ph->GetCounter();    pktfirst = ph->GetCounter();
326    obtfirst = ph->GetOrbitalTime();      obtfirst = ph->GetOrbitalTime();  
327    //    //
328      //   code = eh->GetCounter();
329      //   UInt_t en = 0;
330      //   for(Iter = packetsNames.begin(); Iter != packetsNames.end(); Iter++){
331      //     en = code->Get(GetPacketType(*Iter));
332      //     if ( en ) printf(" Packet type is %s, entries: %i \n",*Iter,en);
333      //};  
334      //
335    T->GetEntry(nevent-1);    T->GetEntry(nevent-1);
336    ph = eh->GetPscuHeader();    ph = eh->GetPscuHeader();
337    pktlast = ph->GetCounter();    pktlast = ph->GetCounter();
# Line 252  Int_t PamelaDBOperations::SetUpperLimits Line 351  Int_t PamelaDBOperations::SetUpperLimits
351    //    //
352    if ( nevent < 2 ) return(4);    if ( nevent < 2 ) return(4);
353    //    //
354    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) ){
355        //
356      // go back      // go back
357      zomp = nevent - 2;      zomp = nevent - 2;
358      //      //
# Line 301  Int_t PamelaDBOperations::SetUpperLimits Line 401  Int_t PamelaDBOperations::SetUpperLimits
401    // 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)
402    //    //
403    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);  
404    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
405    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
406    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 319  Int_t PamelaDBOperations::SetUpperLimits Line 414  Int_t PamelaDBOperations::SetUpperLimits
414    //    //
415    rhev = rh->GetEntries();    rhev = rh->GetEntries();
416    rtev = rt->GetEntries();    rtev = rt->GetEntries();
417      UInt_t sobtt = 0;
418      UInt_t sobth = 0;
419      UInt_t spktt = 0;
420      UInt_t spkth = 0;
421    UInt_t pktt = 0;    UInt_t pktt = 0;
422    ULong64_t obtt = 0;    ULong64_t obtt = 0;
423    UInt_t pkth = 0;    UInt_t pkth = 0;
424    ULong64_t obth = 0;    ULong64_t obth = 0;
425    //    //
426      T->GetEntry(upperentry);
427      code = eh->GetCounter();
428      Int_t lasttrail = code->Get(pctp->RunTrailer);
429      Int_t lasthead = code->Get(pctp->RunHeader);
430    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
431      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
432      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 331  Int_t PamelaDBOperations::SetUpperLimits Line 434  Int_t PamelaDBOperations::SetUpperLimits
434      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
435    };    };
436    //    //
437      if ( lasthead < rhev ){
438        rh->GetEntry(lasthead);
439        phh = ehh->GetPscuHeader();
440        pkth = PKT(phh->GetCounter());
441        obth = OBT(phh->GetOrbitalTime());
442      };
443      //
444      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
445      if ( pkth > upperpkt && obth > upperobt ){
446        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
447        upperpkt = pkth;
448        upperobt = obth;
449        rhev = lasthead+1;
450      } else {
451        rhev = lasthead;
452      };
453      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
454      //
455    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);
456    if ( pktt > upperpkt && obtt > upperobt ){    if ( pktt > upperpkt && obtt > upperobt ){
457      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 462  Int_t PamelaDBOperations::SetUpperLimits
462      rtev = lasttrail;      rtev = lasttrail;
463    };    };
464    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);
465      //  goto kikko;
466      //
467      //
468      // Check if runtrailer/runheader are within lower limits
469      //
470      //
471      pkth = 0;
472      obth = 0;
473      spkth = 0;
474      sobth = 0;
475      for (Int_t k=0; k<rhev; k++){
476        if ( k > 0 ){
477          spkth = pkth;
478          sobth = obth;
479        };
480        rh->GetEntry(k);
481        phh = ehh->GetPscuHeader();
482        pkth = PKT(phh->GetCounter());
483        obth = OBT(phh->GetOrbitalTime());
484        //
485    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
486        //
487        if ( pkth < spkth && obth < sobth ){
488          if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
489          //
490          rhev = k-1;
491          rh->GetEntry(rhev);
492          pkth = spkth;
493          obth = sobth;
494          //
495          UInt_t evbefh = 0;
496          code = ehh->GetCounter();
497          evbefh = code->Get(pctp->Physics);    
498          if ( evbefh >= 0 ){
499            T->GetEntry(evbefh);
500            ph = eh->GetPscuHeader();
501            t_pktlast = PKT(ph->GetCounter());
502            t_obtlast = OBT(ph->GetOrbitalTime());
503            if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
504              upperpkt = pkth;
505              upperobt = obth;
506              upperentry = evbefh-1;
507            } else {
508              while ( t_pktlast > spkth && t_obtlast > sobth && evbefh < nevent ){
509                evbefh++;  
510                T->GetEntry(evbefh);
511                ph = eh->GetPscuHeader();
512                t_pktlast = PKT(ph->GetCounter());
513                t_obtlast = OBT(ph->GetOrbitalTime());
514              };
515              T->GetEntry(evbefh-1);
516              ph = eh->GetPscuHeader();
517              upperpkt = PKT(ph->GetCounter());
518              upperobt = OBT(ph->GetOrbitalTime());
519              upperentry = evbefh-1;
520            };    
521          };
522          if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
523          goto kikko0;
524        };
525      };
526     kikko0:
527      //
528      //
529      //
530      pktt = 0;
531      obtt = 0;
532      spktt = 0;
533      sobtt = 0;
534      for (Int_t k=0; k<rtev; k++){
535        if ( k > 0 ){
536          spktt = pktt;
537          sobtt = obtt;
538        };
539        rt->GetEntry(k);
540        pht = eht->GetPscuHeader();
541        pktt = PKT(pht->GetCounter());
542        obtt = OBT(pht->GetOrbitalTime());
543        //
544    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
545        //
546        if ( pktt < spktt && obtt < sobtt ){
547          if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
548          //
549          rtev = k-1;
550          rt->GetEntry(rtev);
551          pktt = spktt;
552          obtt = sobtt;
553          if ( IsDebug() ) printf(" lasttrail %i pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
554          //
555          UInt_t evbeft = 0;
556          code = eht->GetCounter();
557          evbeft = code->Get(pctp->Physics);    
558          if ( evbeft >= 0 ){
559            T->GetEntry(evbeft);
560            ph = eh->GetPscuHeader();
561            t_pktlast = PKT(ph->GetCounter());
562            t_obtlast = OBT(ph->GetOrbitalTime());
563            if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
564              upperpkt = pktt;
565              upperobt = obtt;
566              upperentry = evbeft-1;
567            } else {
568              while ( t_pktlast > spktt && t_obtlast > sobtt && evbeft < nevent ){
569                evbeft++;  
570                T->GetEntry(evbeft);
571                ph = eh->GetPscuHeader();
572                t_pktlast = PKT(ph->GetCounter());
573                t_obtlast = OBT(ph->GetOrbitalTime());
574              };
575              T->GetEntry(evbeft-1);
576              ph = eh->GetPscuHeader();
577              upperpkt = PKT(ph->GetCounter());
578              upperobt = OBT(ph->GetOrbitalTime());
579              upperentry = evbeft-1;
580            };
581          };
582          if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
583          goto kikko;
584          //      break;
585          //
586        };
587        //  
588      };
589      //
590     kikko:
591      //
592      T->GetEntry(upperentry);
593      code = eh->GetCounter();
594      lasttrail = code->Get(pctp->RunTrailer);
595      lasthead = code->Get(pctp->RunHeader);
596      if ( lasttrail < rtev ){
597        rt->GetEntry(lasttrail);
598        pht = eht->GetPscuHeader();
599        pktt = PKT(pht->GetCounter());
600        obtt = OBT(pht->GetOrbitalTime());
601      };
602    //    //
603    if ( lasthead < rhev ){    if ( lasthead < rhev ){
604      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
605      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
606      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
607      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
608    };    };
609    //    //
610    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 618  Int_t PamelaDBOperations::SetUpperLimits
618    };    };
619    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);
620    //    //
621      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
622      if ( pktt > upperpkt && obtt > upperobt ){
623        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
624        upperpkt = pktt;
625        upperobt = obtt;
626        rtev = lasttrail+1;
627      } else {
628        rtev = lasttrail;
629      };
630      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
631      //
632    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);
633    //    //
634    return(0);    return(0);
635  }  }
636    
637    /**
638     *
639     * Trick to have unique RUN ID numbers even when runs are deleted and mysql deamon restarted.
640     * Entries in the _RUNID_GEN table are never deleted.
641     *
642     **/
643    UInt_t PamelaDBOperations::AssignRunID(){
644      //
645      TSQLResult *result = 0;
646      TSQLRow *row = 0;
647      UInt_t runid = 0;
648      //
649      stringstream   oss;
650      //  
651      oss.str("");
652      oss << "INSERT INTO _RUNID_GEN VALUES (NULL);";
653      result = conn->Query(oss.str().c_str());
654      if ( !result ) throw -10;
655      oss.str("");
656      oss << "SELECT ID FROM _RUNID_GEN ORDER BY ID DESC LIMIT 1;";
657      result = conn->Query(oss.str().c_str());
658      if ( !result ) throw -10;
659      //
660      row = result->Next();
661      //
662      if ( !row ) throw -28;
663      //
664      runid = (UInt_t)atoll(row->GetField(0));
665      //
666      return(runid);
667    };
668    
669  //  //
670  // GETTERS  // GETTERS
671  //  //
# Line 440  const PacketType* PamelaDBOperations::Ge Line 741  const PacketType* PamelaDBOperations::Ge
741  // PRIVATE FUNCTIONS  // PRIVATE FUNCTIONS
742  //  //
743    
744  /**  // /**
745   * Open the ROOT filename for reading  // * Open the ROOT filename for reading
746   */  // */
747  void PamelaDBOperations::OpenFile(){  // void PamelaDBOperations::OpenFile(){
748    file = TFile::Open(this->GetRootName().Data());  //   file = TFile::Open(this->GetRootName().Data());
749    //   //
750    
751    void PamelaDBOperations::CheckFile(){  
752      if ( !file ) throw -12;
753  };  };
754    
755    
756  /**  /**
757   * Check if LEVEL0 file and DB connection have really be opened   * Check if LEVEL0 file and DB connection have really be opened
758   */   */
759  void PamelaDBOperations::CheckFile(){    void PamelaDBOperations::CheckConnection(){  
   //  
   if ( !file ) throw -12;  
760    //    //
761    // check connection    // check connection
762    //    //
763    if( !conn ) throw -1;        if( !conn ) throw -1;
764    bool connect = conn->IsConnected();    bool connect = conn->IsConnected();
765    if( !connect ) throw -1;        if( !connect ) throw -1;
766      if ( !dworbit && strcmp(this->GetRootName().Data(),"") ) throw -27;
767  };  };
768    
769  /**  /**
# Line 550  void PamelaDBOperations::FillClass(Bool_ Line 854  void PamelaDBOperations::FillClass(Bool_
854      lastPkt = glrun->GetRUNTRAILER_PKT();      lastPkt = glrun->GetRUNTRAILER_PKT();
855    };    };
856    //    //
857    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
858    //    //
859    if ( mishead ) {    if ( mishead ) {
860      glrun->Set_GL_RUNH0();      glrun->Set_GL_RUNH0();
# Line 639  Int_t PamelaDBOperations::insertPamelaGL Line 943  Int_t PamelaDBOperations::insertPamelaGL
943    if ( this->GetID_RAW() == 0 )  throw -11;    if ( this->GetID_RAW() == 0 )  throw -11;
944    //    //
945    oss.str("");    oss.str("");
946    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ID_RAW<= "    oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE SPECIAL_FILE='"
947        << id << " AND TO_ID_RAW >= "        << this->GetRawFile().Data() << "';";
       << id << ";";  
948    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());    if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
949    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
950    if ( !result ) throw -10;    if ( !result ) throw -10;
951    row = result->Next();    row = result->Next();
952    if ( !row ) throw -10;    //
953      if ( !row ){
954        oss.str("");
955        oss << "SELECT OFFSET_DATE FROM GL_RESURS_OFFSET WHERE FROM_ORBIT< "
956            << dworbit << " order by FROM_ORBIT desc limit 1;";
957        if ( IsDebug() ) printf(" %s \n",oss.str().c_str());
958        result = conn->Query(oss.str().c_str());
959        if ( !result ) throw -10;
960        row = result->Next();
961        if ( !row ) throw -10;
962      };
963    //    //
964    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();    t0 = (UInt_t)TDatime(row->GetField(0)).Convert();
965    /*    /*
# Line 668  Int_t PamelaDBOperations::insertPamelaGL Line 981  Int_t PamelaDBOperations::insertPamelaGL
981    };    };
982    //    //
983    TTree *T = 0;    TTree *T = 0;
984      Int_t signal = 0;
985    //    //
986    UInt_t nevent = 0;    UInt_t nevent = 0;
987    UInt_t recEntries = 0;    UInt_t recEntries = 0;
# Line 719  Int_t PamelaDBOperations::insertPamelaGL Line 1033  Int_t PamelaDBOperations::insertPamelaGL
1033          //          //
1034          TYPE = 55;//224;          TYPE = 55;//224;
1035          //          //
1036            if ( IsDebug() ) printf("mcmd tsync %i tsync %u obt %u \n",i,TSYNC,OBT);
1037            //
1038          if ( TSYNC && OBT ){          if ( TSYNC && OBT ){
1039            existsts = true;            existsts = true;
1040            goto out;            goto eout;
1041          };          };
1042          //          //
1043        };        };
# Line 729  Int_t PamelaDBOperations::insertPamelaGL Line 1045  Int_t PamelaDBOperations::insertPamelaGL
1045    };    };
1046    if ( !existsts ) { // try with runheader and runtrailer    if ( !existsts ) { // try with runheader and runtrailer
1047      //      //
1048        if ( IsDebug() ) printf(" No ts mcmd \n");
1049        signal = 2;
1050        //
1051      TTree *rh=(TTree*)file->Get("RunHeader");      TTree *rh=(TTree*)file->Get("RunHeader");
1052      if ( !rh || rh->IsZombie() ) throw -17;      if ( !rh || rh->IsZombie() ) throw -17;
1053      TTree *rt=(TTree*)file->Get("RunTrailer");      TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 738  Int_t PamelaDBOperations::insertPamelaGL Line 1057  Int_t PamelaDBOperations::insertPamelaGL
1057      //      //
1058      rt->SetBranchAddress("RunTrailer", &runt);      rt->SetBranchAddress("RunTrailer", &runt);
1059      //      //
1060      //    Int_t rhev = rh->GetEntries();      Int_t nrhev = rh->GetEntries();
1061      //    Int_t rtev = rt->GetEntries();      Int_t nrtev = rt->GetEntries();
1062        if ( IsDebug() ) printf(" ou nevent %i rhev %i rtev %i \n",nevent,nrhev,nrtev);
1063      //      //
1064      if ( rhev > 0 ){      if ( nrhev > 0 ){
1065        rh->GetEntry(0);        for (Int_t i=0; i<nrhev; i++){
1066        //          //
1067        TSYNC = runh->LAST_TIME_SYNC_INFO;          rh->GetEntry(i);
1068        OBT = runh->OBT_TIME_SYNC * 1000;          //
1069        //          TSYNC = runh->LAST_TIME_SYNC_INFO;
1070        TYPE = 20;          OBT = runh->OBT_TIME_SYNC * 1000;
1071        //          //
1072        if ( TSYNC && OBT ){          TYPE = 20;
1073          existsts = true;          //
1074          goto out;          if ( IsDebug() ) printf("runheader %i tsync %u obt %u \n",i,TSYNC,OBT);
1075            //
1076            if ( TSYNC && OBT ){
1077              existsts = true;
1078              goto eout;
1079            };
1080        };        };
1081        //        //
1082      };      };
1083      if ( rtev > 0 ){      if ( nrtev > 0 ){
       rt->GetEntry(0);  
1084        //        //
1085        TSYNC = runt->LAST_TYME_SYNC_INFO;        if ( IsDebug() ) printf(" No runheader \n");
1086        OBT = runt->OBT_TYME_SYNC * 1000;        signal = 6;
1087        //        //
1088        TYPE = 21;        for (Int_t i=0; i<nrtev; i++){
1089        //          //
1090        if ( TSYNC && OBT ){          rt->GetEntry(i);
1091          existsts = true;          //
1092          goto out;          TSYNC = runt->LAST_TYME_SYNC_INFO;
1093            OBT = runt->OBT_TYME_SYNC * 1000;
1094            //
1095            TYPE = 21;
1096            //
1097            if ( IsDebug() ) printf("runtrailer %i tsync %u obt %u \n",i,TSYNC,OBT);
1098            //
1099            if ( TSYNC && OBT ){
1100              existsts = true;
1101              goto eout;
1102            };
1103        };        };
1104        //        //
1105        } else {
1106          if ( IsDebug() ) printf(" No runheader \n");
1107      };      };
1108    };    };
1109    //    //
1110    if ( !existsts ){ // try with inclination mcmd    if ( !existsts ){ // try with inclination mcmd
1111        //
1112        if ( IsDebug() ) printf(" No runtrailer \n");
1113        signal = 14;
1114        //
1115      Double_t timesync = 0.;      Double_t timesync = 0.;
1116      for (UInt_t i=0; i<nevent;i++){      for (UInt_t i=0; i<nevent;i++){
1117        //        //
# Line 800  Int_t PamelaDBOperations::insertPamelaGL Line 1140  Int_t PamelaDBOperations::insertPamelaGL
1140        TYPE = 666;        TYPE = 666;
1141        if ( TSYNC && OBT ){        if ( TSYNC && OBT ){
1142          existsts = true;          existsts = true;
1143          goto out;          goto eout;
1144        };        };
1145      };      };
1146    };    };
1147    //    //
1148    if ( !existsts && obt0 ){ // insert timesync by hand    if ( !existsts && obt0 ){ // insert timesync by hand
1149        //
1150        if ( IsDebug() ) printf(" No incl mcmd \n");
1151        signal = 30;
1152        //
1153      OBT = obt0;      OBT = obt0;
1154      TSYNC = tsync;      TSYNC = tsync;
1155      TYPE = 999;      TYPE = 999;
1156      existsts = true;      existsts = true;
1157      goto out;      goto eout;
1158    };    };
1159    //    //
1160   out:   eout:
1161    //    //
1162    if ( !existsts ) throw -3;    if ( !existsts ) throw -3;
1163    //    //
# Line 829  Int_t PamelaDBOperations::insertPamelaGL Line 1173  Int_t PamelaDBOperations::insertPamelaGL
1173    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;    toffset = (UInt_t)TSYNC - (UInt_t)(this->OBT(OBT)/1000) + t0;
1174    //    //
1175    delete result;    delete result;
1176    return(0);    return(signal);
1177  }  }
1178    
1179  /**  /**
# Line 907  Int_t PamelaDBOperations::assignBOOT_NUM Line 1251  Int_t PamelaDBOperations::assignBOOT_NUM
1251        << " NAME = '" << this->GetRawFile().Data() << "' ";        << " NAME = '" << this->GetRawFile().Data() << "' ";
1252    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
1253    //    //
1254    if ( !result ) return(8);    if ( !result ) throw -4;;
1255    row = result->Next();    row = result->Next();
1256    if ( !row ) return(16);    if ( !row ) return(16);
1257    if ( row->GetField(1) ){    if ( row->GetField(1) ){
1258      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));      this->SetBOOTnumber((UInt_t)atoll(row->GetField(1)));
1259      return(1);      return(1);
1260    };    };
1261    if ( !row->GetField(0) ) return(8);    if ( !row->GetField(0) ) throw -26;
1262    //    //
1263    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));    UInt_t idRaw = (UInt_t)atoll(row->GetField(0));
1264    //    //
# Line 924  Int_t PamelaDBOperations::assignBOOT_NUM Line 1268  Int_t PamelaDBOperations::assignBOOT_NUM
1268    trDumpEv = (TTree*)file->Get("VarDump");    trDumpEv = (TTree*)file->Get("VarDump");
1269    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;    if ( !trDumpEv || trDumpEv->IsZombie() ) throw -20;
1270    //    //
   if (trDumpEv == NULL) return(2);  
   //      
1271    VarDumpEvent  *vde = 0;    VarDumpEvent  *vde = 0;
1272    VarDumpRecord *vdr = 0;    VarDumpRecord *vdr = 0;
1273    //    //
1274    trDumpEv->SetBranchAddress("VarDump", &vde);    trDumpEv->SetBranchAddress("VarDump", &vde);
1275    if (trDumpEv->GetEntries() > 0){    if ( trDumpEv->GetEntries() > 0 ){
1276      trDumpEv->GetEntry(0);      Bool_t found = false;
1277      vde->Records->GetEntries();      for ( Int_t i = 0; i < trDumpEv->GetEntries(); i++){
1278      if ( !vde->Records->GetEntries() ){        trDumpEv->GetEntry(i);
1279        if ( !this->GetBOOTnumber() ) return(4);        vde->Records->GetEntries();
1280      } else {        if ( vde->Records->GetEntries()>0 ){
1281            found = true;
1282            goto fill;
1283          };
1284        };
1285      fill:
1286        if ( found ){
1287          //
1288        vdr = (VarDumpRecord*)vde->Records->At(6);        vdr = (VarDumpRecord*)vde->Records->At(6);
1289          //
1290        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);        this->SetBOOTnumber((Int_t)vdr->VAR_VALUE);
1291          //
1292        } else {
1293          if ( !this->GetBOOTnumber() ) return(4);
1294      };      };
1295    } else {    } else {
1296      if ( !this->GetBOOTnumber() ) return(2);      if ( !this->GetBOOTnumber() ) return(2);
# Line 1023  Int_t PamelaDBOperations::insertPamelaRU Line 1376  Int_t PamelaDBOperations::insertPamelaRU
1376          //          //
1377          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);          if ( IsDebug() ) printf(" Missing header %i %i %i\n",ptht,pth,ptt);
1378          //          //
1379          if ( (ptt-1) < 0 ) throw -15;          if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1380          rt->GetEntry(ptt-1);          rt->GetEntry(ptt-1);
1381          cod = eht->GetCounter();          cod = eht->GetCounter();
1382          evbefh = cod->Get(pctp->Physics);          evbefh = cod->Get(pctp->Physics);
# Line 1129  Int_t PamelaDBOperations::insertPamelaRU Line 1482  Int_t PamelaDBOperations::insertPamelaRU
1482            if ( (UInt_t)evbeft < upperentry-1 ){            if ( (UInt_t)evbeft < upperentry-1 ){
1483              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");
1484              //              //
1485              if ( (ptt-1) < 0 ) throw -15;              if ( (ptt-1) < 0 ) throw -15; // should never arrive here!
1486              rt->GetEntry(ptt-1);              rt->GetEntry(ptt-1);
1487              cod = eht->GetCounter();              cod = eht->GetCounter();
1488              evbefh = cod->Get(pctp->Physics);              evbefh = cod->Get(pctp->Physics);
# Line 1213  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1566  Bool_t PamelaDBOperations::IsRunAlreadyI
1566      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){      if ( glrun->GetNEVENTS() > (UInt_t)atoll(row->GetField(1)) ){
1567        //        //
1568        if ( IsDebug() ) printf(" The new run has more events than the old one \n");        if ( IsDebug() ) printf(" The new run has more events than the old one \n");
1569        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1570        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1571        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1572        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1573    //       conn->Query(oss.str().c_str());
1574        if ( signal ) signal = false;        if ( signal ) signal = false;
1575        goto gonext;              goto gonext;      
1576        //        //
# Line 1230  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1584  Bool_t PamelaDBOperations::IsRunAlreadyI
1584        //        //
1585        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");        if ( IsDebug() ) printf(" The new run has the same number of events and the runheader the old one miss the runheader \n");
1586        //        //
1587        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1588        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1589        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1590        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1591    //       conn->Query(oss.str().c_str());
1592        //        //
1593        if ( signal ) signal = false;        if ( signal ) signal = false;
1594        goto gonext;        goto gonext;
# Line 1247  Bool_t PamelaDBOperations::IsRunAlreadyI Line 1602  Bool_t PamelaDBOperations::IsRunAlreadyI
1602        //        //
1603        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");        if ( IsDebug() ) printf(" The new run has the same number of events, the runheader and the runtrailer the old one miss the runtrailer \n");
1604        //        //
1605        oss.str("");        glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1606        oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";  //       oss.str("");
1607        if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());        //       oss << "DELETE FROM GL_RUN WHERE ID=" << row->GetField(0) <<";";
1608        conn->Query(oss.str().c_str());  //       if ( IsDebug() ) printf(" delete the run entry: query is \n %s \n",oss.str().c_str());      
1609    //       conn->Query(oss.str().c_str());
1610        if ( signal ) signal = false;        if ( signal ) signal = false;
1611        //        //
1612      };      };
# Line 1286  void PamelaDBOperations::HandleRun(){   Line 1642  void PamelaDBOperations::HandleRun(){  
1642      //      //
1643      this->FillClass();      this->FillClass();
1644      //      //
1645      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);      if ( !IsRunAlreadyInserted() ){
1646          glrun->SetID(this->AssignRunID());
1647          glrun->SetID_RUN_FRAG(0);
1648          glrun->Fill_GL_RUN(conn);
1649        };
1650    } else {    } else {
1651      //      //
1652      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 1309  void PamelaDBOperations::HandleRunFragme Line 1669  void PamelaDBOperations::HandleRunFragme
1669    UInt_t rhfirstev = firstev;    UInt_t rhfirstev = firstev;
1670    UInt_t rtlastev = lastev;    UInt_t rtlastev = lastev;
1671    Bool_t found = false;    Bool_t found = false;
1672      Bool_t foundinrun = false;
1673    //    //
1674    TSQLResult *result = 0;    TSQLResult *result = 0;
1675    TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
# Line 1335  void PamelaDBOperations::HandleRunFragme Line 1696  void PamelaDBOperations::HandleRunFragme
1696    //    //
1697    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");    if ( IsDebug() ) printf("The run is good, is it the other piece in the GL_RUN_FRAGMENTS table?\n");
1698    //    //
1699      // First of all insert the run in the fragment table...
1700      //
1701      oss.str("");
1702      oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "
1703          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("
1704          << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
1705          << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
1706          << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
1707          << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1708          << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
1709          << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
1710          << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1711          << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
1712          << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
1713          << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
1714          << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
1715          << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
1716      //
1717      if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
1718      result = conn->Query(oss.str().c_str());
1719      //
1720      if ( !result ) throw -4;
1721      //
1722      row = result->Next();
1723      //
1724      if ( !row ){
1725        //
1726        // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)
1727        //
1728        if ( IsDebug() ) printf(" The run is new \n");
1729        if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");
1730        //
1731        glrun->SetID(this->AssignRunID());
1732        glrun->SetID_RUN_FRAG(0);
1733        glrun->Fill_GL_RUN_FRAGMENTS(conn);
1734        //
1735      } else {
1736        if ( IsDebug() ) printf(" The run is already present in the fragment table \n");
1737      };
1738      //
1739      //
1740    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?    // can we find the other piece of the run in the GL_RUN_FRAGMENTS table?
1741    //        //    
1742    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is    if ( mishead && rhfirstev == firstev ) { // look for runheader (only when at the beginning of the file, if at the end and the runh is
# Line 1343  void PamelaDBOperations::HandleRunFragme Line 1745  void PamelaDBOperations::HandleRunFragme
1745      oss.str("");      oss.str("");
1746      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1747          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1748          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME()          << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1749            << " ID != " << glrun->ID
1750          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!          << " ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1751      //      //
1752      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runheader in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1353  void PamelaDBOperations::HandleRunFragme Line 1756  void PamelaDBOperations::HandleRunFragme
1756      //      //
1757      row = result->Next();      row = result->Next();
1758      //      //
1759        if ( !row && NoFrag() ){
1760          //
1761          oss.str("");
1762          oss << " SELECT ID,TRK_CALIB_USED,RUNTRAILER_TIME,RUNTRAILER_OBT,RUNHEADER_PKT,RUNTRAILER_PKT FROM GL_RUN WHERE "
1763              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1764              << " RUNHEADER_TIME <= " << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
1765              << " ID != " << glrun->ID
1766              << " AND ID=ID_RUN_FRAG ORDER BY RUNHEADER_TIME DESC LIMIT 1;"; // DESC NOT ASC!!
1767          //
1768          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1769          result = conn->Query(oss.str().c_str());
1770          //
1771          if ( !result ) throw -4;
1772          //
1773          foundinrun = true;
1774          //
1775          row = result->Next();
1776          //
1777        };
1778        //
1779      if ( !row ){      if ( !row ){
1780        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1781        found = false;        found = false;
# Line 1427  void PamelaDBOperations::HandleRunFragme Line 1850  void PamelaDBOperations::HandleRunFragme
1850        //        //
1851        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
1852        //        //
1853          if ( foundinrun ){
1854            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
1855            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
1856          };
1857          //
1858        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
1859        //        //
1860        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
1861        //        //
1862        oss.str("");        oss.str("");
1863        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1458  void PamelaDBOperations::HandleRunFragme Line 1886  void PamelaDBOperations::HandleRunFragme
1886        };        };
1887        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);
1888        //        //
       glrun1->SetID(0);  
1889        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());        glrun1->SetPKT_COUNTER(glrun->GetPKT_COUNTER());
1890        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());        glrun1->SetPKT_READY_COUNTER(glrun->GetPKT_READY_COUNTER());
1891        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());        glrun1->SetRUNTRAILER_TIME(glrun->GetRUNTRAILER_TIME());
# Line 1467  void PamelaDBOperations::HandleRunFragme Line 1894  void PamelaDBOperations::HandleRunFragme
1894        //        //
1895        glrun->SetEV_FROM(firstev);        glrun->SetEV_FROM(firstev);
1896        glrun->SetNEVENTS(lastev-firstev+1);        glrun->SetNEVENTS(lastev-firstev+1);
1897          //
1898        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());        glrun->SetRUNHEADER_TIME(glrun1->GetRUNHEADER_TIME());
1899        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());        glrun->SetRUNHEADER_OBT(glrun1->GetRUNHEADER_OBT());
1900        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());        glrun->SetRUNHEADER_PKT(glrun1->GetRUNHEADER_PKT());
# Line 1486  void PamelaDBOperations::HandleRunFragme Line 1914  void PamelaDBOperations::HandleRunFragme
1914        //        //
1915        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
1916          //          //
1917            //      glrun->SetID(this->AssignRunID());
1918            glrun->SetID_RUN_FRAG(glrun1->GetID());
1919          glrun->Fill_GL_RUN(conn);          glrun->Fill_GL_RUN(conn);
1920          //          //
1921          // get id number          // set id number
         //  
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "  
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun0 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         glrun1->SetID_RUN_FRAG(idrun0);  
1922          //          //
1923            glrun1->SetID_RUN_FRAG(glrun->GetID());
1924          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
1925          //          //
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
         //  
         if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun1 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun1 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         oss.str("");  
         oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         //  
1926        };        };
       //  
       delete glrun1;  
       //  
1927        // delete old entry in fragment table        // delete old entry in fragment table
1928        //        //
1929        oss.str("");        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1930          glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
1931        //        //
1932        oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";        delete glrun1;
1933        //        //
       if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());  
       result =  conn->Query(oss.str().c_str());  
       if ( !result ) throw -4;  
1934        //        //
1935        return;        return;
1936        //        //
# Line 1555  void PamelaDBOperations::HandleRunFragme Line 1939  void PamelaDBOperations::HandleRunFragme
1939    };    };
1940    //    //
1941    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is    if ( mistrail && rtlastev == lastev ) { // look for runtrailer (only when at the end of the file, if at the beginning and the runh is
1942                                             // missing it no way we can found a piece in the frag table      // missing it no way we can found a piece in the frag table
1943      //      //
1944      oss.str("");      oss.str("");
1945      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "      oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN_FRAGMENTS WHERE "
1946          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "          << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1947          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME()          << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1948            << " ID != " << glrun->ID
1949          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";          << " ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1950      //      //
1951      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());      if ( IsDebug() ) printf(" look for runtrailer in the fragments table: query is \n %s \n",oss.str().c_str());
# Line 1570  void PamelaDBOperations::HandleRunFragme Line 1955  void PamelaDBOperations::HandleRunFragme
1955      //      //
1956      row = result->Next();      row = result->Next();
1957      //      //
1958        if ( !row && NoFrag() ){
1959          //
1960          oss.str("");
1961          oss << " SELECT ID,PKT_COUNTER,RUNHEADER_TIME,RUNHEADER_OBT,RUNTRAILER_PKT,RUNHEADER_PKT FROM GL_RUN WHERE "
1962              << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND "
1963              << " RUNTRAILER_TIME >= " << (UInt_t)glrun->GetRUNTRAILER_TIME() << " AND "
1964              << " ID != " << glrun->ID
1965              << " AND ID=ID_RUN_FRAG ORDER BY RUNTRAILER_TIME ASC LIMIT 1;";
1966          //
1967          if ( IsDebug() ) printf(" look for runheader in the GL_RUN table: query is \n %s \n",oss.str().c_str());
1968          result = conn->Query(oss.str().c_str());
1969          //
1970          if ( !result ) throw -4;
1971          //
1972          foundinrun = true;
1973          row = result->Next();
1974          //
1975        };
1976        //
1977      if ( !row ){      if ( !row ){
1978        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");        if ( IsDebug() ) printf(" the corresponding piece has NOT been found \n");
1979        found = false;        found = false;
# Line 1643  void PamelaDBOperations::HandleRunFragme Line 2047  void PamelaDBOperations::HandleRunFragme
2047        //        //
2048        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");        if ( IsDebug() ) printf(" now you can handle the piece of the run \n ");
2049        //        //
2050          if ( foundinrun ){
2051            glrun->RestoreRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
2052            glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN");
2053          };
2054          //
2055        GL_RUN *glrun1 = new GL_RUN();        GL_RUN *glrun1 = new GL_RUN();
2056        //        //
2057        UInt_t idfrag = (UInt_t)atoll(row->GetField(0));        //      UInt_t idfrag = (UInt_t)atoll(row->GetField(0));
2058        //        //
2059        oss.str("");        oss.str("");
2060        oss << " ID="<<row->GetField(0)<<";";        oss << " ID="<<row->GetField(0)<<";";
# Line 1682  void PamelaDBOperations::HandleRunFragme Line 2091  void PamelaDBOperations::HandleRunFragme
2091        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());        glrun->SetRUNTRAILER_OBT(glrun1->GetRUNTRAILER_OBT());
2092        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());        glrun->SetRUNTRAILER_PKT(glrun1->GetRUNTRAILER_PKT());
2093        //        //
       glrun1->SetID(0);  
2094        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());        glrun1->SetRUNHEADER_TIME(glrun->GetRUNHEADER_TIME());
2095        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());        glrun1->SetRUNHEADER_OBT(glrun->GetRUNHEADER_OBT());
2096        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());        glrun1->SetRUNHEADER_PKT(glrun->GetRUNHEADER_PKT());
# Line 1702  void PamelaDBOperations::HandleRunFragme Line 2110  void PamelaDBOperations::HandleRunFragme
2110        //        //
2111        if ( !IsRunAlreadyInserted() ){        if ( !IsRunAlreadyInserted() ){
2112          //          //
2113          glrun->Fill_GL_RUN(conn);          //      glrun->SetID(this->AssignRunID());
         //  
         // get id number  
2114          //          //
2115          oss.str("");          glrun->SetID_RUN_FRAG(glrun1->GetID());
2116          oss << " SELECT ID FROM GL_RUN WHERE BOOT_NUMBER="<<this->GetBOOTnumber()<<" AND "          glrun->Fill_GL_RUN(conn);
             << " RUNHEADER_OBT="<<glrun->GetRUNHEADER_OBT()<<" AND "  
             << " RUNTRAILER_OBT="<<glrun->GetRUNTRAILER_OBT()<<";";  
         //  
         if ( IsDebug() ) printf(" search for idrun0 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun0 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun0 = (UInt_t)atoll(row->GetField(0));  
         };  
2117          //          //
2118          glrun1->SetID_RUN_FRAG(idrun0);          // set id number
2119          //          //
2120            glrun1->SetID_RUN_FRAG(glrun->GetID());
2121          glrun1->Fill_GL_RUN(conn);          glrun1->Fill_GL_RUN(conn);
2122          //          //
         oss.str("");  
         oss << " SELECT ID FROM GL_RUN WHERE ID_RUN_FRAG="<<idrun0<<" ;";  
         //  
         if ( IsDebug() ) printf(" search for idrun1 , query is : \n%s\n",oss.str().c_str());  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         row = result->Next();  
         //  
         UInt_t idrun1 = 0;  
         if ( !row ){  
           throw -10;  
         } else {  
           idrun1 = (UInt_t)atoll(row->GetField(0));  
         };  
         //  
         oss.str("");  
         oss << " UPDATE GL_RUN SET ID_RUN_FRAG="<<idrun1<<" WHERE ID="<<idrun0<<" ;";  
         //  
         result =  conn->Query(oss.str().c_str());  
         if ( !result ) throw -4;  
         //  
2123        };        };
2124        //        //
2125        delete glrun1;        // delete old entries in fragment table
       //  
       // delete old entry in fragment table  
       //  
       oss.str("");  
       //  
       oss << " DELETE FROM GL_RUN_FRAGMENTS WHERE ID = " << idfrag << ";";  
2126        //        //
2127        if ( IsDebug() ) printf(" Delete from frag table the old run :\n query is \n %s \n",oss.str().c_str());        glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2128        result =  conn->Query(oss.str().c_str());        glrun1->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
       if ( !result ) throw -4;  
2129        //        //
2130          delete glrun1;
2131        //        //
2132        return;        return;
2133        //        //
# Line 1800  void PamelaDBOperations::HandleRunFragme Line 2164  void PamelaDBOperations::HandleRunFragme
2164      //      //
2165      row = result->Next();      row = result->Next();
2166      //      //
2167      if ( !row ){      if ( row ){
       //  
       oss.str("");  
       oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE "  
           << " BOOT_NUMBER=" << this->GetBOOTnumber() << " AND ("  
           << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "  
           << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("  
           << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "  
           << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "  
           << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("  
           << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "  
           << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("  
           << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "  
           << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";  
       //  
       if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());  
       result = conn->Query(oss.str().c_str());  
       //  
       if ( !result ) throw -4;  
       //  
       row = result->Next();  
       //  
       if ( !row ){  
         //  
         // no, insert this run in the GL_RUN_FRAGMENTS table (check if exist before!)  
         //  
         if ( IsDebug() ) printf(" The run is new \n");  
         if ( IsDebug() ) printf(" -> fill the GL_RUNFRAGMENTS table \n");  
         //  
         glrun->Fill_GL_RUN_FRAGMENTS(conn);  
         //  
       } else {  
         if ( IsDebug() ) printf(" The run is already present in the fragment table \n");  
       };  
     } else {  
2168        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");        if ( IsDebug() ) printf(" The run is already present in the GL_RUN table \n");
2169        } else {
2170          if ( NoFrag() ){
2171            glrun->SetID_RUN_FRAG(glrun->GetID());
2172            glrun->Fill_GL_RUN(conn);      
2173            glrun->DeleteRun(conn,0,"GL_RUN_FRAGMENTS");
2174          };    
2175      };      };
2176    };    };
2177    //    //
# Line 1864  void PamelaDBOperations::HandleMissingHo Line 2197  void PamelaDBOperations::HandleMissingHo
2197      //      //
2198      this->FillClass(mishead,mistrail,firstev,lastev);      this->FillClass(mishead,mistrail,firstev,lastev);
2199      //      //
2200      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2201          glrun->SetID(this->AssignRunID());
2202          glrun->SetID_RUN_FRAG(0);
2203          glrun->Fill_GL_RUN(conn);    
2204        };
2205      //      //
2206    };    };
2207    //    //
# Line 2031  Bool_t PamelaDBOperations::IsRunConsiste Line 2368  Bool_t PamelaDBOperations::IsRunConsiste
2368            //                  //      
2369            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2370            //            //
2371            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2372                glrun->SetID(this->AssignRunID());
2373                glrun->SetID_RUN_FRAG(0);
2374                glrun->Fill_GL_RUN(conn);      
2375              };
2376            //            //
2377            firstevno = lastentry + 1;            firstevno = lastentry + 1;
2378            //            //
# Line 2106  void PamelaDBOperations::HandleSuspiciou Line 2447  void PamelaDBOperations::HandleSuspiciou
2447    if ( firstev == lastev+1 ) { // no events inside the run!    if ( firstev == lastev+1 ) { // no events inside the run!
2448      if ( IsDebug() ) printf(" Checking but no events in the run! \n");      if ( IsDebug() ) printf(" Checking but no events in the run! \n");
2449      //      //
     //    if ( IsDebug() ) printf(" -> fill the DB \n");  
     //  
2450      this->FillClass();      this->FillClass();
2451      if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);          if ( !IsRunAlreadyInserted() ){
2452          glrun->SetID(this->AssignRunID());
2453          glrun->SetID_RUN_FRAG(0);
2454          glrun->Fill_GL_RUN(conn);    
2455        };
2456      //      //
2457    } else {    } else {
2458      //      //
# Line 2137  void PamelaDBOperations::HandleSuspiciou Line 2480  void PamelaDBOperations::HandleSuspiciou
2480        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");
2481        //        //
2482        this->FillClass();        this->FillClass();
2483        if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                if ( !IsRunAlreadyInserted() ){
2484            glrun->SetID(this->AssignRunID());
2485            glrun->SetID_RUN_FRAG(0);
2486            glrun->Fill_GL_RUN(conn);        
2487          };
2488        //        //
2489      } else {      } else {
2490        //        //
# Line 2235  void PamelaDBOperations::HandleSuspiciou Line 2582  void PamelaDBOperations::HandleSuspiciou
2582            //                  //      
2583            this->SetCommonGLRUN(firstTime,lastTime);            this->SetCommonGLRUN(firstTime,lastTime);
2584            //            //
2585            if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                  if ( !IsRunAlreadyInserted() ){
2586                glrun->SetID(this->AssignRunID());
2587                glrun->SetID_RUN_FRAG(0);
2588                glrun->Fill_GL_RUN(conn);      
2589              };
2590            //            //
2591            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...
2592              //              //
# Line 2270  void PamelaDBOperations::HandleSuspiciou Line 2621  void PamelaDBOperations::HandleSuspiciou
2621              //                  //    
2622              this->SetCommonGLRUN(firstTime,lastTime);              this->SetCommonGLRUN(firstTime,lastTime);
2623              //              //
2624              if ( !IsRunAlreadyInserted() ) glrun->Fill_GL_RUN(conn);                    if ( !IsRunAlreadyInserted() ){
2625                  glrun->SetID(this->AssignRunID());
2626                  glrun->SetID_RUN_FRAG(0);
2627                  glrun->Fill_GL_RUN(conn);      
2628                };
2629            };            };
2630            //            //
2631            firstevno = lastentry + 1;            firstevno = lastentry + 1;
# Line 2318  Int_t PamelaDBOperations::insertCALO_CAL Line 2673  Int_t PamelaDBOperations::insertCALO_CAL
2673    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
2674    nevents = tr->GetEntries();    nevents = tr->GetEntries();
2675    //    //
2676    if ( !nevents ) return(0);    if ( !nevents ) return(1);
2677    //    //
2678    for (UInt_t i=0; i < nevents; i++){    for (UInt_t i=0; i < nevents; i++){
2679      tr->GetEntry(i);      tr->GetEntry(i);
# Line 2635  Int_t PamelaDBOperations::insertTRK_CALI Line 2990  Int_t PamelaDBOperations::insertTRK_CALI
2990    tr2->SetBranchAddress("Header", &eh2);    tr2->SetBranchAddress("Header", &eh2);
2991    nevents2 = tr2->GetEntries();    nevents2 = tr2->GetEntries();
2992    //    //
2993    if ( !nevents1 && !nevents2 ) return(0);    if ( !nevents1 && !nevents2 ) return(1);
2994    //    //
2995    t2 = -1;    t2 = -1;
2996    Int_t pret2 = 0;    Int_t pret2 = 0;
# Line 2807  Int_t PamelaDBOperations::insertS4_CALIB Line 3162  Int_t PamelaDBOperations::insertS4_CALIB
3162    stringstream oss;    stringstream oss;
3163    oss.str("");    oss.str("");
3164    //    //
   CalibS4Event *calibS4    = new  CalibS4Event();  
3165    TTree *tr = 0;    TTree *tr = 0;
3166    EventHeader *eh = 0;    EventHeader *eh = 0;
3167    PscuHeader *ph = 0;    PscuHeader *ph = 0;
# Line 2821  Int_t PamelaDBOperations::insertS4_CALIB Line 3175  Int_t PamelaDBOperations::insertS4_CALIB
3175    tr = (TTree*)file->Get("CalibS4");    tr = (TTree*)file->Get("CalibS4");
3176    if ( !tr || tr->IsZombie() ) throw -24;    if ( !tr || tr->IsZombie() ) throw -24;
3177    //    //
   tr->SetBranchAddress("CalibS4", &calibS4);  
3178    tr->SetBranchAddress("Header", &eh);    tr->SetBranchAddress("Header", &eh);
3179    //    //
3180    nevents = tr->GetEntries();    nevents = tr->GetEntries();
3181    //    //
3182    if ( !nevents ) return(0);    if ( !nevents ) return(1);
3183    //    //
3184    for (UInt_t i = 0; i < nevents; i++){    for (UInt_t i = 0; i < nevents; i++){
3185      //      //
3186      tr->GetEntry(i);      tr->GetEntry(i);
     TArrayD params = S4_paramfit(calibS4);  
3187      //      //
3188      ph = eh->GetPscuHeader();      ph = eh->GetPscuHeader();
3189      obt = ph->GetOrbitalTime();        obt = ph->GetOrbitalTime();  
# Line 2921  Int_t PamelaDBOperations::insertS4_CALIB Line 3273  Int_t PamelaDBOperations::insertS4_CALIB
3273          };          };
3274          //          //
3275          oss.str("");          oss.str("");
3276          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) "
3277              << " VALUES (NULL,' "              << " VALUES (NULL,' "
3278              << idroot << "','"              << idroot << "','"
3279              << i << "','"              << i << "','"
3280              << fromtime << "','"              << fromtime << "','"
3281              << totime << "','"              << totime << "','"
             << dec << params.At(0) << "','"  
             << dec << params.At(1) << "','"        
3282              << obt << "','"              << obt << "','"
3283              << pkt << "','"              << pkt << "','"
3284              << this->GetBOOTnumber() << "','"              << this->GetBOOTnumber() << "');";
             << valid << "');";  
3285          //          //
3286          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());
3287          //          //
# Line 2953  Int_t PamelaDBOperations::insertS4_CALIB Line 3302  Int_t PamelaDBOperations::insertS4_CALIB
3302    return(0);    return(0);
3303  };  };
3304    
3305    /**
3306     * Scan the fragment table and move old fragments to the GL_RUN table
3307     */
3308    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(){
3309      return(this->CleanGL_RUN_FRAGMENTS(""));
3310    };
3311    
3312  /*  /**
3313   * Fit function Received from Valeria Malvezzi 06/02/2006   * Scan the fragment table and move old fragments to the GL_RUN table
3314   */   */
3315  Double_t fitf(Double_t *x, Double_t *par){    Int_t PamelaDBOperations::CleanGL_RUN_FRAGMENTS(TString fcleanfile){
3316    Double_t fitval =(par[0]*x[0])+par[1];    //
3317    return fitval;    TSQLResult *result = 0;
3318  }    TSQLRow    *row    = 0;
3319      TSQLResult *result2 = 0;
3320      TSQLRow    *row2   = 0;
3321      //
3322      UInt_t moved = 0;
3323      //
3324      stringstream oss;
3325      oss.str("");
3326      //
3327      if ( !strcmp(fcleanfile.Data(),"") ){
3328        //
3329        // check if there are entries older than "olderthan" seconds from now
3330        //
3331        oss.str("");
3332        oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3333            << " INSERT_TIME <= '" << clean_time->AsSQLString() << "';";
3334        //
3335        if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS runs older than %s : query is \n %s \n",clean_time->AsSQLString(),oss.str().c_str());
3336        result = conn->Query(oss.str().c_str());
3337        //
3338      } else {
3339        oss.str("");
3340        oss << " SELECT ID FROM GL_ROOT WHERE NAME='" << fcleanfile.Data() << "';";
3341        if ( IsDebug() ) printf(" Getting ID_ROOT_L0 query %s \n",oss.str().c_str());
3342        result = conn->Query(oss.str().c_str());
3343        //    
3344        if ( result ){
3345          //
3346          row = result->Next();
3347          //
3348          if ( row ){
3349            oss.str("");
3350            oss << " SELECT ID FROM GL_RUN_FRAGMENTS WHERE"
3351                << " ID_ROOT_L0=" << row->GetField(0) << ";";
3352            //
3353            if ( IsDebug() ) printf(" Select from GL_RUN_FRAGMENTS for ROOT file query is \n %s \n",oss.str().c_str());
3354            result = conn->Query(oss.str().c_str());
3355            //    
3356          };
3357        } else {
3358          return(2);
3359        };
3360      };
3361      //
3362      if ( result ){
3363        //
3364        row = result->Next();
3365        //
3366        while ( row ){
3367          //
3368          oss.str("");
3369          oss << " ID= "<< row->GetField(0);
3370          //
3371          glrun->Query_GL_RUN_FRAGMENTS(oss.str().c_str(),conn);  
3372          //
3373          oss.str("");
3374          oss << " SELECT ID,NEVENTS,TRK_CALIB_USED,PKT_COUNTER FROM GL_RUN WHERE "
3375              << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND ("
3376              << " (RUNHEADER_TIME>=" << (UInt_t)(glrun->GetRUNHEADER_TIME()-10) << " AND "
3377              << " RUNTRAILER_TIME<=" << (UInt_t)(glrun->GetRUNTRAILER_TIME()+10) << " AND ("
3378              << " RUNHEADER_OBT>=" << glrun->GetRUNHEADER_OBT() << " OR "
3379              << " RUNHEADER_PKT>=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3380              << " RUNTRAILER_OBT<=" << glrun->GetRUNTRAILER_OBT() << " OR "
3381              << " RUNTRAILER_PKT<=" << glrun->GetRUNTRAILER_PKT() << ") ) OR "
3382              << " (RUNHEADER_TIME<=" << (UInt_t)glrun->GetRUNHEADER_TIME() << " AND "
3383              << " RUNTRAILER_TIME>=" << (UInt_t)glrun->GetRUNTRAILER_TIME() <<" AND ("
3384              << " RUNHEADER_OBT<=" << glrun->GetRUNHEADER_OBT() << " OR "
3385              << " RUNHEADER_PKT<=" << glrun->GetRUNHEADER_PKT() << ") AND ("
3386              << " RUNTRAILER_OBT>=" << glrun->GetRUNTRAILER_OBT() << " OR "
3387              << " RUNTRAILER_PKT>=" << glrun->GetRUNTRAILER_PKT() << ") ));";
3388          //
3389          if ( IsDebug() ) printf(" check if run has been inserted: query is \n %s \n",oss.str().c_str());
3390          result2 = conn->Query(oss.str().c_str());
3391          //
3392          if ( !result2 ) throw -4;
3393          //
3394          row2 = result2->Next();
3395          //
3396          if ( !row2 ){
3397            //
3398            if ( IsDebug() ) printf(" The run is new \n");
3399            if ( IsDebug() ) printf(" -> fill the DB \n");      
3400            //
3401            //      glrun->SetID(this->AssignRunID()); we use the old run number!
3402            glrun->SetID_RUN_FRAG(glrun->GetID());
3403            glrun->Fill_GL_RUN(conn);  
3404            //
3405    //      oss.str("");
3406    //      oss << " SELECT ID FROM GL_RUN WHERE "
3407    //          << " BOOT_NUMBER=" << glrun->GetBOOT_NUMBER() << " AND "
3408    //          << " RUNHEADER_PKT=" << (UInt_t)glrun->GetRUNHEADER_PKT() << " AND "
3409    //          << " RUNTRAILER_PKT=" << (UInt_t)glrun->GetRUNTRAILER_PKT() << " AND "
3410    //          << " RUNHEADER_OBT=" << (UInt_t)glrun->GetRUNHEADER_OBT() << " AND "
3411    //          << " RUNTRAILER_OBT=" << (UInt_t)glrun->GetRUNTRAILER_OBT() << "; ";
3412    //      //
3413    //      if ( IsDebug() ) printf(" Look for the ID of the inserted run: query is \n %s \n",oss.str().c_str());
3414    //      result2 = conn->Query(oss.str().c_str());
3415    //      //
3416    //      if ( !result2 ) throw -4;
3417    //      //
3418    //      row2 = result2->Next();
3419    //      //
3420    //      if ( !row2 ) throw -25;
3421    //      //
3422    //      oss.str("");
3423    //      oss << " UPDATE GL_RUN SET ID_RUN_FRAG = " << row2->GetField(0) << " WHERE ID = " << row2->GetField(0);
3424    //      if ( IsDebug() ) printf(" Update the ID_RUN_FRAG of the inserted run: query is \n %s \n",oss.str().c_str());
3425    //      result2 = conn->Query(oss.str().c_str());
3426    //      //
3427    //      if ( !result2 ) throw -4;
3428            //
3429            moved++;
3430            //
3431          } else {
3432            if ( IsDebug() ) printf(" The already exist in the GL_RUN table! \n");
3433          };
3434          if ( IsDebug() ) printf(" Delete run %s from the GL_RUN_FRAGMENTS table \n",row->GetField(0));      
3435          //
3436          //
3437          glrun->DeleteRun(conn,(UInt_t)atoll(row->GetField(0)),"GL_RUN_FRAGMENTS");
3438    //      oss.str("");
3439    //       oss << " DELETE from GL_RUN_FRAGMENTS where ID = " << row->GetField(0);
3440    //       if ( IsDebug() ) printf(" Clean the GL_RUN_FRAGMENTS table: query is \n %s \n",oss.str().c_str());
3441    //       result2 = conn->Query(oss.str().c_str());
3442    //      //
3443    //      if ( !result2 ) throw -4;
3444    //      //
3445          row = result->Next();
3446        };
3447      };
3448      if ( IsDebug() ) printf(" Moved %u runs\n",moved);
3449      return(0);
3450    };
3451    
3452    /**
3453     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3454     */
3455    Int_t PamelaDBOperations::ValidateRuns(){
3456      return(this->ValidateRuns(""));
3457    };
3458    
3459    /**
3460     * Check if runs are good, i.e. if the tracker calibration is correctly associated..
3461     */
3462    Int_t PamelaDBOperations::ValidateRuns(TString valfile){
3463      //
3464      TSQLResult *result = 0;
3465      TSQLRow    *row    = 0;
3466      //
3467      UInt_t calibtime = 50;
3468      //
3469      stringstream oss;
3470      oss.str("");
3471      //
3472      // =======================================================
3473      // validate runs by checking missing calibrations
3474      // =======================================================
3475      UInt_t t_stop  = 0;
3476      UInt_t t_start = 0;
3477      if ( !strcmp(valfile.Data(),"") ) {
3478        // --------------------------------------------------------------
3479        // 1) get the OBT of the last run inserted after clean-time limit
3480        // --------------------------------------------------------------
3481        oss.str("");
3482        oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << clean_time->AsSQLString()
3483            << "' ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3484        if ( IsDebug() ) printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3485        result = conn->Query(oss.str().c_str());
3486        if ( !result ) throw -4;
3487        if ( !result->GetRowCount() ) {
3488          printf(" No runs to validate \n");
3489          return(1);
3490        }else{
3491          row = result->Next();
3492          t_start = (UInt_t)atoll(row->GetField(4));
3493        };  
3494        // --------------------------------------------------------------
3495        // 2) get the OBT of the last validated run
3496        // --------------------------------------------------------------
3497        oss.str("");
3498        oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3499            <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3500        if ( IsDebug() ) printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3501        result = conn->Query(oss.str().c_str());
3502        if ( !result ) throw -4;
3503        if ( result->GetRowCount() ){
3504          row = result->Next();
3505          t_stop = (UInt_t)atoll(row->GetField(4));
3506        };
3507        if ( IsDebug() ) printf("Validation interval: from time %i - to time %i \n\n",t_stop,t_start);
3508        // --------------------------------------------------------------
3509        // now retrieves runs to be validated
3510        // --------------------------------------------------------------
3511        oss.str("");
3512        oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3513        oss << " AND RUNHEADER_TIME >="<< t_stop;
3514        oss << " ORDER BY RUNHEADER_TIME DESC;";
3515        if ( IsDebug() )printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3516        result = conn->Query(oss.str().c_str());
3517      } else {
3518        //
3519        stringstream myquery;
3520        UInt_t myid = 0;
3521        myquery.str("");
3522        myquery << " SELECT ID FROM GL_ROOT where NAME='"<<valfile.Data() <<"';";
3523        //
3524        result = conn->Query(myquery.str().c_str());
3525        //
3526        row = result->Next();      
3527        if( !row ){
3528          if ( strcmp(valfile.Data(),GetRootName().Data()) ){
3529            if ( IsDebug() ) printf(" No file to be validated even if option \"-validate file\" was used!!\n");
3530            return(2);
3531          };
3532          if ( IsDebug() ) printf(" No file to be validated (force mode)! \n");
3533          return(0);
3534        };
3535        myid=(UInt_t)atoll(row->GetField(0));
3536        //
3537        myquery.str("");
3538        myquery << " SELECT MAX(RUNTRAILER_TIME),MIN(RUNHEADER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< myid <<";";
3539        //
3540        result = conn->Query(myquery.str().c_str());
3541        //
3542        row = result->Next();      
3543        if( !row->GetField(0) || !row->GetField(1)){
3544          //
3545          if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3546          //
3547          return(0);
3548          //
3549        } else {  
3550          //
3551          UInt_t runhtime = (UInt_t)atoll(row->GetField(0));
3552          UInt_t runttime = (UInt_t)atoll(row->GetField(1));
3553          UInt_t caltime = 0;
3554          //
3555          myquery.str("");
3556          myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3557          myquery << " order by FROM_TIME asc limit 1;";
3558          //
3559          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3560          //
3561          //
3562          result = conn->Query(myquery.str().c_str());
3563          //
3564          row = result->Next();      
3565          if( !row ){
3566            caltime = runhtime;
3567          } else {
3568            caltime = (UInt_t)atoll(row->GetField(0));
3569          };
3570          //
3571          myquery.str("");
3572          myquery << " SELECT * from GL_RUN where RUNHEADER_TIME>="<< runttime <<" AND RUNHEADER_TIME<=" ;
3573          myquery << caltime << " order by RUNHEADER_TIME DESC";
3574          //
3575          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3576          //
3577          result = conn->Query(myquery.str().c_str());
3578          //
3579        };
3580      };
3581      //
3582      if ( !result ) throw -4;
3583      if ( !result->GetRowCount() && IsDebug() ) printf(" No runs to validate \n");
3584      //
3585      Int_t nrow = 0;
3586      GL_RUN* this_run = new GL_RUN();
3587      GL_RUN* next_run = new GL_RUN();
3588      Int_t   nseq_max = 1000;
3589    //  UInt_t* sequence = new UInt_t[100];
3590      vector<UInt_t> sequence(nseq_max);
3591      Int_t   nseq = 0;
3592      Bool_t CHECK = false;
3593      Bool_t this_ONLINE = false;
3594      Bool_t next_ONLINE = false;
3595      UInt_t t1=0,t2=0;
3596      // ---------------------------------------------------------------------------------
3597      // - loop over runs, back in time,
3598      // - select sequences of runs close in time (less than calibtime s apart),
3599      //   which could be preceeded by a calibration
3600      // - check if there might be a missing calibration
3601      // ---------------------------------------------------------------------------------
3602      while(1){
3603              
3604              row = result->Next();
3605              if( row == NULL ) break;
3606              
3607              //------------
3608              //get run info
3609              //------------
3610              this_run->Set_GL_RUN(row);
3611                      
3612              Bool_t this_BAD = false;
3613              if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3614              else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3615              else{
3616    //                printf("Missing or corrupted header!! \n");
3617                      this_ONLINE = false;
3618                      this_BAD = true;
3619              };
3620    
3621  /*            //-----------------------------------
3622   * Fit the S4 calibration with a straight line - Received from Valeria Malvezzi 06/02/2006            //compare with previous(next in time)
3623              //-----------------------------------
3624              CHECK = false;
3625              UInt_t interval=0;
3626              
3627              if( nrow != 0){
3628            
3629                      
3630                      t1 = this_run->GetRUNTRAILER_TIME();
3631                      t2 = next_run->GetRUNHEADER_TIME();
3632                      interval = (t2-t1);
3633                      
3634                      if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE
3635                              
3636                              if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3637                              
3638                              if( interval >= calibtime )CHECK = true;                      //more than calibtime s => there might be a calibration
3639                              
3640                              if( !CHECK && this_run->VALIDATION ){
3641                                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3642                                      nseq=0;
3643                              }
3644                      
3645                      }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3646                              
3647                              CHECK = true;
3648    
3649                      }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3650                              
3651                              assignVALIDATION(next_run->ID,true);
3652                              nseq=0;
3653                      }
3654              }
3655    
3656              //----------------------------
3657              //check run sequence for calib
3658              //----------------------------
3659              if( CHECK ){
3660                      // check if calibration exists
3661                      if ( IsDebug() )printf("DT %i ===> CHECK Missing calibration\n",interval);
3662                      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3663                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3664                      nseq=0;
3665              };
3666              //--------------
3667              //store run info
3668              //--------------
3669              *next_run   = *this_run;
3670              next_ONLINE = this_ONLINE;
3671              if( !this_BAD ){
3672                      if(nseq < nseq_max){
3673                              sequence[nseq] = this_run->ID;
3674                              nseq++;
3675                      }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3676              };
3677              
3678              if ( IsDebug() ) printf("%i Run %i \n",nrow,this_run->ID);
3679              nrow++;
3680              
3681      };
3682      delete this_run;
3683      delete next_run;
3684      //
3685      return(0);
3686    };
3687    /**
3688     * Check if there might be a missing tracker calibration in a given time interval
3689     * @param t1 From absolute time
3690     * @param t2 To absolute time
3691     * @return true if there might be a missing calibration
3692   */   */
3693  TArrayD PamelaDBOperations::S4_paramfit(pamela::CalibS4Event *S4CalibEvent){        Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3694            
3695            GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3696            
3697            // get the closest VALIDATED calibration before the run start (t2)
3698            if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing
3699            
3700            if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing
3701            
3702            //==============================================================
3703            // Check is done first on the basis of time between calibration,
3704            // which should be equal to the time between ascending-nodes.
3705            //==============================================================
3706            if ( t2 - trkcalib->FROM_TIME > 5700) {
3707                    if ( IsDebug() )printf("Long time between calib and run start %i :-( ==> there might be a missing calib \n",t2 - trkcalib->FROM_TIME);
3708            //==============================================================
3709            // there might be a missing calibration, due to:
3710            // - MM full
3711            // - corrupted packets
3712            // - loss of data
3713            // There is an exception in case a download was done during ascending node
3714            //==============================================================
3715                    Bool_t DOWNLOAD = false;
3716                    // check if the calib was skipped becouse of download .... DA FARE!!
3717                    if(DOWNLOAD)return(false);
3718                    
3719                    return(true);                                   //>>> missing
3720                    
3721            };
3722            
3723            //==============================================================
3724            // If the last calibration is close to the run less than this time,
3725            // it is enough to say that there are no missing calibrations
3726            //==============================================================
3727            // the long time interval bewteen runs might be due to download
3728            if ( IsDebug() )printf("Short time between calib and run start %i :-) ==> OK! \n",t2 - trkcalib->FROM_TIME);
3729            return(false);
3730            
3731    };
3732    /**
3733     * Assign VALIDATION value to a GL_RUN entry
3734     * @param idrun Run ID
3735     * @param validation true/false
3736     */
3737    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3738            TSQLResult *result = 0;
3739            stringstream oss;
3740            oss.str("");
3741            oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << ";";
3742            //
3743    //      if ( IsDebug() )
3744    //      printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3745            if ( IsDebug() )printf(" Query: %s \n",oss.str().c_str());
3746            result = conn->Query(oss.str().c_str());
3747            if ( !result ) throw -4;
3748            return(0);
3749    }
3750    
   //----------- variable initialization -------------------------------------------------  
3751    
   Double_t mip[3]={1, 30, 300};  
   Double_t adc[3] = {0.,0.,0.};  
     
   TArrayD parametri(2);  
   
   valid = 1;  
3752    
3753    //------------ Fit calibrations and find parameters to calibrate data ------------------  // Insert TLEs from file tlefilename in the table GL_TLE in the db
3754    pamela::S4::S4Event  *s4Record;  // opened by conn, sorting them by date from older to newer, if each
3755    // TLE has not been alread inserted.
3756    Int_t PamelaDBOperations::populateTLE()//(TSQLServer *conn, char *tleFile)
3757    {
3758      fstream tlefile(tlefilename, ios::in);
3759    
3760      if ( !tlefile ) throw -7;
3761    
3762      vector<cTle*> ctles;
3763      vector<cTle*>::iterator iter;
3764      int present = 0;
3765    
3766      // Get three lines from tlefile, create a cTle object and put it
3767      // into ctles
3768      while(1) {
3769        cTle *tlef;
3770        string str1, str2, str3;
3771    
3772        getline(tlefile, str1);
3773        if(tlefile.eof()) break;
3774    
3775        getline(tlefile, str2);
3776        if(tlefile.eof()) break;
3777    
3778        getline(tlefile, str3);
3779        if(tlefile.eof()) break;
3780    
3781        // We now have three good lines for a cTle.
3782        tlef = new cTle(str1, str2, str3);
3783        ctles.push_back(tlef);
3784      }
3785    
3786      tlefile.close();
3787    
3788      // Sort by date
3789      sort(ctles.begin(), ctles.end(), compTLE);
3790    
3791      // Now we insert each TLE into the db
3792      for(iter = ctles.begin(); iter != ctles.end(); iter++) {
3793        cTle *tle = *iter;
3794    
3795        // Do nothing if it's already present in the db.  Just increase
3796        // the counter present.
3797        if (! isTlePresent(tle))
3798          {
3799            int status = insertTle(tle);
3800    
3801            // Insert query failed.  Return 1.
3802            if(status == EXIT_FAILURE) {
3803              
3804              if( IsDebug() ) {
3805                cerr << "Error: inserting TLE:" << endl
3806                     << tle->getName() << endl
3807                     << tle->getLine1() << endl
3808                     << tle->getLine2() << endl;
3809              }
3810    
3811              throw -4;
3812              return 1;
3813            }
3814    
3815          }
3816        else
3817          present++;
3818    
3819      }
3820    
3821      int inserted = ctles.size() - present;  // Number of inserted TLE.
3822      if ( IsDebug() )
3823        cout << "\nProcessed TLEs ranging from " << getTleDatetime(ctles[0]) << " to " << getTleDatetime(ctles[ctles.size()-1]) << "." << endl
3824             << inserted << " newly inserted TLEs out of " << ctles.size() << " processed." << endl;
3825    
3826      ctles.clear();
3827    
3828    
3829      // Return 2 if no new TLE has been inserted.  0 otherwise.
3830      if(! inserted ) return 2;
3831      return 0;
3832    }
3833    
3834    
3835    // Insert tle in the table GL_TLE using the connection conn.
3836    Int_t PamelaDBOperations::insertTle(cTle *tle)
3837    {
3838      stringstream oss;
3839      TSQLResult *result = 0;
3840    
3841      oss.str("");
3842      oss << " INSERT INTO GL_TLE (TLE1, TLE2, TLE3, FROM_TIME)"
3843          << " VALUES ( '"
3844          << tle->getName() << "', '"
3845          << tle->getLine1() << "', '"
3846          << tle->getLine2() << "', '"
3847          << getTleDatetime(tle) << "')";
3848    
3849    for (Int_t j = 0; j < 4; j++){    //  cout << oss.str().c_str() << endl;
3850      for (Int_t i = 0; i < 128; i++){    result = conn->Query(oss.str().c_str());
3851        s4Record = (pamela::S4::S4Event*)S4CalibEvent->Records->At((j*128 + i));    if (result == NULL)
3852        switch (j) {      return EXIT_FAILURE;
3853        case 0 :{  
3854          adc[0]=adc[0]+((s4Record->S4_DATA)-32);    return EXIT_SUCCESS;
3855          break;  }
3856        };  
3857        case 1 :{  
3858          adc[1]=adc[1]+((s4Record->S4_DATA)-32);  // Return whether tle is already in the db connected by conn.
3859          break;  bool PamelaDBOperations::isTlePresent(cTle *tle)
3860        };  {
3861        case 3 :{    stringstream oss;
3862          adc[2]=adc[2]+((s4Record->S4_DATA)-32);    TSQLResult *result = 0;
3863          break;  
3864      oss.str("");
3865      oss << "SELECT * FROM GL_TLE WHERE FROM_TIME = '"
3866          << getTleDatetime(tle) << "'";
3867    
3868      result = conn->Query(oss.str().c_str());
3869      if (result == NULL) throw -4;
3870    
3871      if (result->GetRowCount())
3872        return true;
3873      else
3874        return false;
3875    }
3876    
3877    
3878    // Return whether the first TLE is dated early than the second
3879    bool compTLE (cTle *tle1, cTle *tle2)
3880    {
3881      return getTleJulian(tle1) < getTleJulian(tle2);
3882    }
3883    
3884    
3885    // Return the date of the tle using the format (year-2000)*1e3 +
3886    // julian day.  e.g. 6365 is the 31th Dec 2006.
3887    // It does *not* return a cJulian date.
3888    float getTleJulian(cTle *tle) {
3889      return tle->getField(cTle::FLD_EPOCHYEAR)*1e3 + tle->getField(cTle::FLD_EPOCHDAY);
3890    }
3891    
3892    
3893    // Return a string like YYYY-MM-DD hh:mm:ss, usable for mysql datetime
3894    // format.
3895    string getTleDatetime(cTle *tle)
3896    {
3897      int year, mon, day, hh, mm, ss;
3898      double dom; // day of month (is double!)
3899      stringstream date; // date in datetime format
3900    
3901      // create a cJulian from the date in tle
3902      cJulian jdate = cJulian( 2000 + (int) tle->getField(cTle::FLD_EPOCHYEAR), tle->getField(cTle::FLD_EPOCHDAY));
3903    
3904      // get year, month, day of month
3905      jdate.getComponent(&year, &mon, &dom);
3906    
3907      // build a datetime YYYY-MM-DD hh:mm:ss
3908      date.str("");
3909      day = (int) floor(dom);
3910      hh = (int) floor( (dom - day) * 24);
3911      mm = (int) floor( ((dom - day) * 24 - hh) * 60);
3912      ss = (int) floor( ((((dom - day) * 24 - hh) * 60 - mm) * 60));
3913      //  ms = (int) floor( (((((dom - day) * 24 - hh) * 60 - mm) * 60) - ss) * 1000);
3914    
3915      date << year << "-" << mon << "-" << day << " " << hh << ":" << mm << ":" << ss;
3916    
3917      return date.str();
3918    }
3919    
3920    /**
3921     * Remove a file from the DB, delete on cascade all entries related to that file
3922     * rearrange GL_RUN and GL_XXX_CALIB tables, turn off validation till the following
3923     * calibration
3924     **/
3925    Int_t PamelaDBOperations::removeFile(TString remfile){
3926      //
3927      // Determine ID_ROOT_L0 and ID_RAW
3928      //
3929      TSQLResult *pResult;
3930      TSQLRow *Row;
3931      stringstream myquery;
3932      //  
3933      myquery.str("");
3934      myquery << " SELECT ID, ID_RAW FROM GL_ROOT where NAME='"<<remfile.Data() <<"';";
3935      //
3936      pResult = conn->Query(myquery.str().c_str());
3937      //
3938      Row = pResult->Next();      
3939      if( !Row ){
3940        if ( strcmp(remfile.Data(),GetRootName().Data()) ){
3941          if ( IsDebug() ) printf(" No file to be removed even if option \"-remove file\" was used!!\n");
3942          return(1);
3943        };
3944        if ( IsDebug() ) printf(" No file to be removed (force mode)! \n");
3945        return(0);
3946      };
3947      //
3948      this->SetID_ROOT((UInt_t)atoll(Row->GetField(0)));
3949      this->SetID_RAW((UInt_t)atoll(Row->GetField(1)));
3950      //
3951      this->ValidationOFF();
3952      //
3953      this->RemoveCALIBS();
3954      //
3955      this->RemoveRUNS();
3956      //
3957      this->RemoveFILES();
3958      //
3959      this->SetID_ROOT(0);
3960      this->SetID_RAW(0);
3961      //
3962      return(0);
3963    };
3964    
3965    /**
3966     *
3967     * Set validation bit to zero for runs following the removing file till
3968     * 1) a run with TRK_CALIB_USED=140
3969     * 2) a run with VALIDATION = 0
3970     * 3) the next calibration
3971     *
3972     **/
3973    void PamelaDBOperations::ValidationOFF(){
3974      TSQLResult *pResult;
3975      TSQLRow *Row;
3976      stringstream myquery;
3977      Int_t unv = 0;
3978      //select ID from GL_RUN where RUNHEADER_TIME>=1152671382 AND (VALIDATION=0 OR TRK_CALIB_USED=104) order by RUNHEADER_TIME asc limit 1;
3979      myquery.str("");
3980      myquery << " SELECT MAX(RUNTRAILER_TIME) FROM GL_RUN WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
3981      //
3982      pResult = conn->Query(myquery.str().c_str());
3983      //
3984      Row = pResult->Next();      
3985      if( !Row->GetField(0) ){
3986        //
3987        if ( IsDebug() ) printf(" NO RUN ASSOCIATED TO THIS FILE! \n");
3988        //
3989      } else {  
3990        //
3991        UInt_t runhtime = (UInt_t)atoll(Row->GetField(0));
3992        UInt_t caltime = 0;
3993        //
3994        myquery.str("");
3995        myquery << " SELECT FROM_TIME FROM GL_TRK_CALIB where FROM_TIME>" <<runhtime;
3996        myquery << " order by FROM_TIME asc limit 1;";
3997        //
3998        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
3999        //
4000        //
4001        delete pResult;
4002        pResult = conn->Query(myquery.str().c_str());
4003        //
4004        Row = pResult->Next();      
4005        if( !Row ){
4006          caltime = runhtime;
4007        } else {
4008          caltime = (UInt_t)atoll(Row->GetField(0));
4009        };
4010        //
4011        myquery.str("");
4012        myquery << " SELECT ID,RUNHEADER_TIME from GL_RUN where RUNHEADER_TIME>="<< runhtime <<" AND (VALIDATION=0 OR TRK_CALIB_USED=104 OR RUNHEADER_TIME>" ;
4013        myquery << caltime << ") order by RUNHEADER_TIME asc LIMIT 1";
4014        //
4015        if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4016        //
4017        pResult = conn->Query(myquery.str().c_str());
4018        //
4019        Row = pResult->Next();      
4020        if( !Row ){
4021          //
4022          if ( IsDebug() ) printf(" NO RUN NEED TO BE UNVALIDATED \n");
4023          //
4024        } else {
4025          myquery.str("");
4026          myquery << " SELECT ID from GL_RUN where RUNHEADER_TIME<"<< Row->GetField(1) <<" AND ";
4027          myquery << " RUNHEADER_TIME>=" <<runhtime;
4028          myquery << " order by RUNHEADER_TIME asc;";
4029          //
4030          if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4031          //
4032          pResult = conn->Query(myquery.str().c_str());
4033          //
4034          Row = pResult->Next();  
4035          while ( Row ){
4036            //      
4037            unv++;
4038            this->assignVALIDATION((UInt_t)atoll(Row->GetField(0)), false);
4039            Row = pResult->Next();  
4040            //
4041        };        };
4042        };
4043      };
4044      if ( IsDebug() ) printf(" %i runs have been unvalidated \n",unv);
4045    };
4046    
4047    /**
4048     *
4049     * Rearrange GL_RUN table and remove runs
4050     *
4051     **/
4052    void PamelaDBOperations::RemoveRUNS(){
4053      TSQLResult *pResult;
4054      TSQLRow *Row;
4055      stringstream myquery;
4056      UInt_t drun = 0;
4057      GL_RUN *delrun = new GL_RUN();
4058      //
4059      myquery.str("");
4060      myquery << " SELECT ID FROM GL_RUN where ID_RUN_FRAG=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4061      //
4062      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4063      //
4064      pResult = conn->Query(myquery.str().c_str());
4065      //
4066      Row = pResult->Next();    
4067      //
4068      //
4069      if ( !Row ){
4070        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG=0 belonged to this file \n");
4071      } else {
4072        if ( IsDebug() ) printf(" Deleting run from GL_RUN table \n");    
4073        while ( Row ){
4074          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4075          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4076          drun++;
4077          Row = pResult->Next();    
4078        };
4079      };
4080      //
4081      //
4082      myquery.str("");
4083      myquery << " SELECT ID,ID_RUN_FRAG FROM GL_RUN where ID_RUN_FRAG!=0 and ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4084      //
4085      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4086      //
4087      pResult = conn->Query(myquery.str().c_str());
4088      //
4089      Row = pResult->Next();    
4090      //
4091      if ( !Row ){
4092        if ( IsDebug() ) printf(" No run with ID_RUN_FRAG!=0 belonged to this file \n");
4093      } else {
4094        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN table \n");    
4095        while ( Row ){
4096          if ( IsDebug() ) printf(" restore run %i \n",(UInt_t)atoll(Row->GetField(1)));
4097          delrun->RestoreRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN_FRAGMENTS");
4098          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(1)));
4099          delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(1)),"GL_RUN");
4100          if ( (UInt_t)atoll(Row->GetField(1)) != (UInt_t)atoll(Row->GetField(0)) ){
4101            if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4102            delrun->DeleteRun(conn,(UInt_t)atoll(Row->GetField(0)),"GL_RUN");
4103        };        };
4104          drun++;
4105          Row = pResult->Next();    
4106      };      };
4107    };    };
4108        //
4109    adc[0]=adc[0]/128;    if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN table \n",drun);    
4110    adc[1]=adc[1]/128;    //
4111    adc[2]=adc[2]/128;    //
4112        //
4113    TGraph *fitpar = new TGraph (3, adc, mip);    drun = 0;
4114    TF1 *func = new TF1("fitf", fitf, -0., 1000., 2); // definizione della funzione, 2 = num. parametri    //
4115        myquery.str("");
4116    func->SetParameters(0.3,1.);        //inizializzazione dei parametri a 1    myquery << " SELECT ID_TRASH FROM GL_RUN_TRASH where BELONGED_TO='GL_RUN_FRAGMENTS' AND ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4117    func->SetParNames("m","q");      //definisce il nome dei parametri    //
4118    fitpar->Fit(func,"qr");          //fitta fitpar con la funzione func nel range definito nella funzione    pResult = conn->Query(myquery.str().c_str());
4119    //fitpar->Fit(func,"r");          //fitta fitpar con la funzione func nel range definito nella funzione    //
4120          Row = pResult->Next();    
4121    parametri[0] = func -> GetParameter(0);    //
4122    parametri[1] = func -> GetParameter(1);    if ( !Row ){
4123        if ( IsDebug() ) printf(" No run from GL_RUN_FRAGMENTS table in the trash table for this file \n");
4124      } else {
4125        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_TRASH table \n");    
4126        while ( Row ){
4127          if ( IsDebug() ) printf(" del run idtrash %i \n",(UInt_t)atoll(Row->GetField(0)));
4128          myquery.str("");
4129          myquery << " DELETE FROM GL_RUN_TRASH where ID_TRASH=" << Row->GetField(0) <<";";      
4130          conn->Query(myquery.str().c_str());
4131          drun++;
4132          Row = pResult->Next();    
4133        };
4134      };
4135      //  
4136      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_TRASH table \n",drun);    
4137      //
4138      //
4139      //
4140      drun = 0;
4141      //
4142      myquery.str("");
4143      myquery << " SELECT ID FROM GL_RUN_FRAGMENTS where ID_ROOT_L0=" <<this->GetID_ROOT() <<";";
4144      //
4145      pResult = conn->Query(myquery.str().c_str());
4146      //
4147      Row = pResult->Next();    
4148      //
4149      if ( !Row ){
4150        if ( IsDebug() ) printf(" No run in the GL_RUN_FRAGMENTS table for this file \n");
4151      } else {
4152        if ( IsDebug() ) printf(" Deleting run fragments from GL_RUN_FRAGMENTS table \n");    
4153        while ( Row ){
4154          if ( IsDebug() ) printf(" del run %i \n",(UInt_t)atoll(Row->GetField(0)));
4155          myquery.str("");
4156          myquery << " DELETE FROM GL_RUN_FRAGMENTS where ID=" << Row->GetField(0) <<";";
4157          conn->Query(myquery.str().c_str());
4158          drun++;
4159          Row = pResult->Next();    
4160        };
4161      };
4162      //  
4163      if ( IsDebug() ) printf(" Deleted %i run(s) from GL_RUN_FRAGMENTS table \n",drun);    
4164      //
4165      //
4166      //
4167      delete delrun;
4168      //
4169    };
4170    
   if ( parametri[0] < 0. || parametri[0] > 0.5 || parametri[1] < 0.8 || parametri[1] > 1. ) valid = 0;  
4171    
4172    if ( IsDebug() ) printf(" par1 = %g par2 = %g\n",parametri[0],parametri[1]);  /**
4173     *
4174    return parametri;   * Rearrange calibration tables
4175     *
4176     **/
4177    void PamelaDBOperations::RemoveFILES(){
4178      stringstream myquery;
4179      //
4180      myquery.str("");
4181      myquery << " DELETE FROM GL_RAW WHERE ID=" <<this->GetID_RAW() <<";";
4182      //
4183      if ( IsDebug() ) printf("  query is \n %s \n",myquery.str().c_str());
4184      //
4185      conn->Query(myquery.str().c_str());
4186      //
4187    };
4188    
4189    /**
4190     *
4191     * Rearrange calibration tables
4192     *
4193     **/
4194    void PamelaDBOperations::RemoveCALIBS(){
4195      TSQLResult *pResult;
4196      TSQLRow *Row;
4197      stringstream myquery;
4198      //  
4199      //
4200      // Calorimeter
4201      //
4202      for (Int_t section = 0; section < 4; section++){
4203        myquery.str("");
4204        myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_CALO_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<" AND ";
4205        myquery << " SECTION=" << section << ";";
4206        //
4207        pResult = conn->Query(myquery.str().c_str());
4208        //
4209        Row = pResult->Next();      
4210        if( !Row->GetField(0) || !Row->GetField(1) ){
4211          //
4212          if ( IsDebug() ) printf(" NO CALO CALIBRATION SECTION %i ASSOCIATED TO THIS FILE! \n",section);
4213          //
4214        } else {
4215          //
4216          myquery.str("");
4217          myquery << " UPDATE GL_CALO_CALIB SET TO_TIME=" << Row->GetField(1);
4218          myquery << " WHERE TO_TIME="<< Row->GetField(0) << " AND ";
4219          myquery << " SECTION=" << section << ";";
4220          //
4221          pResult = conn->Query(myquery.str().c_str());
4222          //
4223          if( !pResult ){
4224            //
4225            if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4226            //
4227            throw -4;
4228            //
4229          };
4230          //
4231        };
4232      };
4233      myquery.str("");
4234      myquery << " DELETE FROM GL_CALO_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4235      //
4236      pResult = conn->Query(myquery.str().c_str());
4237      //
4238      if( !pResult ){
4239        //
4240        if ( IsDebug() ) printf(" ERROR DELETING CALO CALIBRATIONS \n");
4241        //
4242        throw -4;
4243        //
4244      };
4245      //
4246      // Tracker
4247      //
4248      myquery.str("");
4249      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_TRK_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4250      //
4251      pResult = conn->Query(myquery.str().c_str());
4252      //
4253      Row = pResult->Next();      
4254      if( !Row->GetField(0) || !Row->GetField(1) ){
4255        //
4256        if ( IsDebug() ) printf(" NO TRK CALIBRATION ASSOCIATED TO THIS FILE! \n");
4257        //
4258      } else {
4259        //
4260        myquery.str("");
4261        myquery << " UPDATE GL_TRK_CALIB SET TO_TIME=" << Row->GetField(1);
4262        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4263        //
4264        pResult = conn->Query(myquery.str().c_str());
4265        //
4266        if( !pResult ){
4267          //
4268          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4269          //
4270          throw -4;
4271          //
4272        };
4273        //
4274        myquery.str("");
4275        myquery << " DELETE FROM GL_TRK_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4276        //
4277        pResult = conn->Query(myquery.str().c_str());
4278        //
4279        if( !pResult ){
4280          //
4281          if ( IsDebug() ) printf(" ERROR DELETING TRK CALIBRATIONS \n");
4282          //
4283          throw -4;
4284          //
4285        };
4286      };
4287      //
4288      //
4289      // S4
4290      //
4291      myquery.str("");
4292      myquery << " SELECT MIN(FROM_TIME),MAX(TO_TIME) FROM GL_S4_CALIB WHERE ID_ROOT_L0="<< this->GetID_ROOT() <<";";
4293      //
4294      pResult = conn->Query(myquery.str().c_str());
4295      //
4296      Row = pResult->Next();      
4297      if( !Row->GetField(0) || !Row->GetField(1) ){
4298        //
4299        if ( IsDebug() ) printf(" NO S4 CALIBRATION ASSOCIATED TO THIS FILE! \n");
4300        //
4301      } else {
4302        //
4303        myquery.str("");
4304        myquery << " UPDATE GL_S4_CALIB SET TO_TIME=" << Row->GetField(1);
4305        myquery << " WHERE TO_TIME="<< Row->GetField(0) << ";";
4306        //
4307        pResult = conn->Query(myquery.str().c_str());
4308        //
4309        if( !pResult ){
4310          //
4311          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4312          //
4313          throw -4;
4314          //
4315        };
4316        //
4317        myquery.str("");
4318        myquery << " DELETE FROM GL_S4_CALIB WHERE ID_ROOT_L0=" << this->GetID_ROOT();
4319        //
4320        pResult = conn->Query(myquery.str().c_str());
4321        //
4322        if( !pResult ){
4323          //
4324          if ( IsDebug() ) printf(" ERROR DELETING S4 CALIBRATIONS \n");
4325          //
4326          throw -4;
4327          //
4328        };
4329        //
4330      };
4331  };  };

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

  ViewVC Help
Powered by ViewVC 1.1.23