/[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.4 by mocchiut, Fri Sep 8 12:44:27 2006 UTC revision 1.7 by mocchiut, Mon Sep 11 14:04:51 2006 UTC
# Line 320  Int_t PamelaDBOperations::SetUpperLimits Line 320  Int_t PamelaDBOperations::SetUpperLimits
320    // 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)
321    //    //
322    PacketType *pctp=0;    PacketType *pctp=0;
   T->GetEntry(upperentry);  
   code = eh->GetCounter();  
   Int_t lasttrail = code->Get(pctp->RunTrailer);  
   Int_t lasthead = code->Get(pctp->RunHeader);  
323    TTree *rh=(TTree*)file->Get("RunHeader");    TTree *rh=(TTree*)file->Get("RunHeader");
324    if ( !rh || rh->IsZombie() ) throw -17;    if ( !rh || rh->IsZombie() ) throw -17;
325    TTree *rt=(TTree*)file->Get("RunTrailer");    TTree *rt=(TTree*)file->Get("RunTrailer");
# Line 346  Int_t PamelaDBOperations::SetUpperLimits Line 342  Int_t PamelaDBOperations::SetUpperLimits
342    UInt_t pkth = 0;    UInt_t pkth = 0;
343    ULong64_t obth = 0;    ULong64_t obth = 0;
344    //    //
345      T->GetEntry(upperentry);
346      code = eh->GetCounter();
347      Int_t lasttrail = code->Get(pctp->RunTrailer);
348      Int_t lasthead = code->Get(pctp->RunHeader);
349    if ( lasttrail < rtev ){    if ( lasttrail < rtev ){
350      rt->GetEntry(lasttrail);      rt->GetEntry(lasttrail);
351      pht = eht->GetPscuHeader();      pht = eht->GetPscuHeader();
# Line 356  Int_t PamelaDBOperations::SetUpperLimits Line 356  Int_t PamelaDBOperations::SetUpperLimits
356    if ( lasthead < rhev ){    if ( lasthead < rhev ){
357      rh->GetEntry(lasthead);      rh->GetEntry(lasthead);
358      phh = ehh->GetPscuHeader();      phh = ehh->GetPscuHeader();
359      pkth = PKT(pht->GetCounter());      pkth = PKT(phh->GetCounter());
360      obth = OBT(pht->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
361    };    };
362    //    //
363    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 401  Int_t PamelaDBOperations::SetUpperLimits Line 401  Int_t PamelaDBOperations::SetUpperLimits
401      pkth = PKT(phh->GetCounter());      pkth = PKT(phh->GetCounter());
402      obth = OBT(phh->GetOrbitalTime());      obth = OBT(phh->GetOrbitalTime());
403      //      //
404    //    if ( IsDebug() ) printf(" k %i rhev before %i ph %u upperp %u oh %u uppero %u \n",k,rhev,pkth,spkth,obth,sobth);
405        //
406      if ( pkth < spkth && obth < sobth ){      if ( pkth < spkth && obth < sobth ){
       if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);  
407        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);        if ( IsDebug() ) printf(" RH PROBLEMS determining the event repetition at the end of the file lasthead %i  \n",rhev);
408        //        //
409        rhev--;        rhev = k-1;
410        rh->GetEntry(rhev);        rh->GetEntry(rhev);
411        pkth = spkth;        pkth = spkth;
412        obth = sobth;        obth = sobth;
       if ( IsDebug() ) printf(" lasthead %i pt %i p1 %i ot %u o1 %u \n",rhev,pkth,spkth,obth,sobth);  
413        //        //
414        UInt_t evbefh = 0;        UInt_t evbefh = 0;
415        code = ehh->GetCounter();        code = ehh->GetCounter();
# Line 419  Int_t PamelaDBOperations::SetUpperLimits Line 419  Int_t PamelaDBOperations::SetUpperLimits
419          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
420          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
421          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
422          if ( t_pktlast < spkth && t_obtlast < sobth ){ // jump          if ( t_pktlast <= spkth && t_obtlast <= sobth ){ // jump
423            upperpkt = pkth;            upperpkt = pkth;
424            upperobt = obth;            upperobt = obth;
425            upperentry = evbefh-1;            upperentry = evbefh-1;
# Line 439  Int_t PamelaDBOperations::SetUpperLimits Line 439  Int_t PamelaDBOperations::SetUpperLimits
439          };              };    
440        };        };
441        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);
442          goto kikko0;
443      };      };
444    };    };
445     kikko0:
446    //    //
447    //    //
448    //    //
# Line 458  Int_t PamelaDBOperations::SetUpperLimits Line 460  Int_t PamelaDBOperations::SetUpperLimits
460      pktt = PKT(pht->GetCounter());      pktt = PKT(pht->GetCounter());
461      obtt = OBT(pht->GetOrbitalTime());      obtt = OBT(pht->GetOrbitalTime());
462      //      //
463    //    if ( IsDebug() ) printf(" k %i rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",k,rtev,pktt,spktt,obtt,sobtt);
464        //
465      if ( pktt < spktt && obtt < sobtt ){      if ( pktt < spktt && obtt < sobtt ){
       if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);  
466        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);        if ( IsDebug() ) printf(" RT PROBLEMS determining the event repetition at the end of the file lasttrail %i \n",rtev);
467        //        //
468        rtev--;        rtev = k-1;
469        rt->GetEntry(rtev);        rt->GetEntry(rtev);
470        pktt = spktt;        pktt = spktt;
471        obtt = sobtt;        obtt = sobtt;
# Line 476  Int_t PamelaDBOperations::SetUpperLimits Line 479  Int_t PamelaDBOperations::SetUpperLimits
479          ph = eh->GetPscuHeader();          ph = eh->GetPscuHeader();
480          t_pktlast = PKT(ph->GetCounter());          t_pktlast = PKT(ph->GetCounter());
481          t_obtlast = OBT(ph->GetOrbitalTime());          t_obtlast = OBT(ph->GetOrbitalTime());
482          if ( t_pktlast < spktt && t_obtlast < sobtt ){ // jump          if ( t_pktlast <= spktt && t_obtlast <= sobtt ){ // jump
483            upperpkt = pktt;            upperpkt = pktt;
484            upperobt = obtt;            upperobt = obtt;
485            upperentry = evbeft-1;            upperentry = evbeft-1;
# Line 496  Int_t PamelaDBOperations::SetUpperLimits Line 499  Int_t PamelaDBOperations::SetUpperLimits
499          };          };
500        };        };
501        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);
502        break;        goto kikko;
503          //      break;
504        //        //
505      };      };
506      //        //  
507    };    };
508    //    //
509    // kikko:   kikko:
510      //
511      T->GetEntry(upperentry);
512      code = eh->GetCounter();
513      lasttrail = code->Get(pctp->RunTrailer);
514      lasthead = code->Get(pctp->RunHeader);
515      if ( lasttrail < rtev ){
516        rt->GetEntry(lasttrail);
517        pht = eht->GetPscuHeader();
518        pktt = PKT(pht->GetCounter());
519        obtt = OBT(pht->GetOrbitalTime());
520      };
521      //
522      if ( lasthead < rhev ){
523        rh->GetEntry(lasthead);
524        phh = ehh->GetPscuHeader();
525        pkth = PKT(phh->GetCounter());
526        obth = OBT(phh->GetOrbitalTime());
527      };
528      //
529      if ( IsDebug() ) printf(" rhev before %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
530      if ( pkth > upperpkt && obth > upperobt ){
531        if ( IsDebug() ) printf(" Upper limits extended to include last header: ph %i upperp %i oh %llu uppero %llu \n",pkth,upperpkt,obth,upperobt);
532        upperpkt = pkth;
533        upperobt = obth;
534        rhev = lasthead+1;
535      } else {
536        rhev = lasthead;
537      };
538      if ( IsDebug() ) printf(" rhev after %i ph %i upperp %i oh %llu uppero %llu \n",rhev,pkth,upperpkt,obth,upperobt);
539      //
540      if ( IsDebug() ) printf(" rtev beforev %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
541      if ( pktt > upperpkt && obtt > upperobt ){
542        if ( IsDebug() ) printf(" Upper limits extended to include last trailer: pt %i upperp %i ot %llu uppero %llu \n",pktt,upperpkt,obtt,upperobt);
543        upperpkt = pktt;
544        upperobt = obtt;
545        rtev = lasttrail+1;
546      } else {
547        rtev = lasttrail;
548      };
549      if ( IsDebug() ) printf(" rtev after %i  pt %i upperp %i ot %llu uppero %llu \n",rtev,pktt,upperpkt,obtt,upperobt);
550    //    //
551    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);
552    //    //
# Line 3245  Int_t PamelaDBOperations::CleanGL_RUN_FR Line 3289  Int_t PamelaDBOperations::CleanGL_RUN_FR
3289  Int_t PamelaDBOperations::ValidateRuns(){  Int_t PamelaDBOperations::ValidateRuns(){
3290    //    //
3291    TSQLResult *result = 0;    TSQLResult *result = 0;
3292    // TSQLRow    *row    = 0;    TSQLRow    *row    = 0;
3293    //    //
3294    UInt_t timelim = 0;    UInt_t timelim = 0;
3295    TDatime *time = new TDatime();    TDatime *time = new TDatime();
# Line 3265  Int_t PamelaDBOperations::ValidateRuns() Line 3309  Int_t PamelaDBOperations::ValidateRuns()
3309    time->Set();    time->Set();
3310    timelim =  (UInt_t)time->Convert() - olderthan;    timelim =  (UInt_t)time->Convert() - olderthan;
3311    time->Set(timelim,false);    time->Set(timelim,false);
3312    //  
3313    // First of all validate runs with default calibration:    printf("Validate runs older than %s \n",time->AsSQLString());
3314    //  
3315    oss.str("");    // =======================================================
3316    oss << " UPDATE GL_RUN SET VALIDATION=1 WHERE"    // validate runs by checking missing calibrations
3317        << " VALIDATION = 0 AND TRK_CALIB_USED=104 AND "    // =======================================================
3318        << " INSERT_TIME <= '" << time->AsSQLString() << "';";    UInt_t t_stop  = 0;
3319    //    UInt_t t_start = 0;
3320    if ( IsDebug() ) printf(" Validate runs with trk default calibration inserted before %s : query is \n %s \n",time->AsSQLString(),oss.str().c_str());    // --------------------------------------------------------------
3321      // 1) get the OBT of the last run inserted after clean-time limit
3322      // --------------------------------------------------------------
3323      oss.str("");
3324      oss << " SELECT * FROM GL_RUN  WHERE INSERT_TIME <= '" << time->AsSQLString()
3325          << "' ORDER BY INSERT_TIME DESC LIMIT 1;";
3326      printf(" Get start validation-time: query is \n %s \n",oss.str().c_str());
3327    result = conn->Query(oss.str().c_str());    result = conn->Query(oss.str().c_str());
   //  
3328    if ( !result ) throw -4;    if ( !result ) throw -4;
3329      if ( !result->GetRowCount() ) {
3330              printf(" No runs to validate \n");
3331              return(1);
3332      }else{
3333            row = result->Next();
3334            t_start = (UInt_t)atoll(row->GetField(4));
3335            printf("t_start %i\n",t_start);
3336      };  
3337      // --------------------------------------------------------------
3338      // 2) get the OBT of the last validated run
3339      // --------------------------------------------------------------
3340      oss.str("");
3341      oss << " SELECT * FROM GL_RUN  WHERE VALIDATION=1 AND RUNHEADER_TIME<="<< t_start
3342          <<" ORDER BY RUNHEADER_TIME DESC LIMIT 1;";
3343      printf(" Get stop validation-time: query is \n %s \n",oss.str().c_str());
3344      result = conn->Query(oss.str().c_str());
3345      if ( !result ) throw -4;
3346      if ( result->GetRowCount() ){
3347              row = result->Next();
3348              t_stop = (UInt_t)atoll(row->GetField(4));
3349      };
3350      printf("t_stop %i\n",t_stop);
3351      // --------------------------------------------------------------
3352      // now retrieves runs to be validated
3353      // --------------------------------------------------------------
3354      oss.str("");
3355      oss << " SELECT * FROM GL_RUN  WHERE  RUNHEADER_TIME <=" << t_start;
3356      oss << " AND RUNHEADER_TIME >="<< t_stop;
3357      oss << " ORDER BY RUNHEADER_TIME DESC;";
3358    //  if ( IsDebug() )
3359      printf(" Check runs for validation: query is \n %s \n",oss.str().c_str());
3360      result = conn->Query(oss.str().c_str());
3361      if ( !result ) throw -4;
3362      if ( !result->GetRowCount() ) printf(" No runs to validate \n");
3363      printf("------------------------------------------------------------------------------- \n");
3364      
3365      Int_t nrow = 0;
3366      GL_RUN* this_run = new GL_RUN();
3367      GL_RUN* next_run = new GL_RUN();
3368      Int_t   nseq_max = 100;
3369    //  UInt_t* sequence = new UInt_t[100];
3370      vector<UInt_t> sequence(nseq_max);
3371      Int_t   nseq = 0;
3372      Bool_t CHECK = false;
3373      Bool_t this_ONLINE = false;
3374      Bool_t next_ONLINE = false;
3375      UInt_t t1=0,t2=0;
3376      // ---------------------------------------------------------------------------------
3377      // - loop over runs, back in time,
3378      // - select sequences of runs close in time (less than 60 s apart),
3379      //   which could be preceeded by a calibration
3380      // - check if there might be a missing calibration
3381      // ---------------------------------------------------------------------------------
3382      while(1){
3383              
3384              row = result->Next();
3385              if( row == NULL ) break;
3386              
3387              //------------
3388              //get run info
3389              //------------
3390              this_run->Set_GL_RUN(row);
3391              
3392              printf(" RUN ID %i --- TRK_CALIB_USED %i --- RM_ACQ_AFTER_CALIB %i --- TIME %i %i \n",this_run->ID,this_run->TRK_CALIB_USED,this_run->RM_ACQ_AFTER_CALIB,this_run->RUNHEADER_TIME, this_run->RUNTRAILER_TIME);
3393              
3394              Bool_t this_BAD = false;
3395              if(this_run->GetTRK_CALIB_USED() == 1 || this_run->GetTRK_CALIB_USED() == 2) this_ONLINE = true;
3396              else if (this_run->GetTRK_CALIB_USED() == 104)                          this_ONLINE = false;
3397              else{
3398                      printf("Missing or corrupted header!! \n");
3399                      this_ONLINE = false;
3400                      this_BAD = true;
3401              };
3402    
3403              //-----------------------------------
3404              //compare with previous(next in time)
3405              //-----------------------------------
3406              CHECK = false;
3407              UInt_t interval=0;
3408              
3409              if( nrow != 0){
3410            
3411                      
3412                      t1 = this_run->GetRUNTRAILER_TIME();
3413                      t2 = next_run->GetRUNHEADER_TIME();
3414                      interval = (t2-t1);
3415                      
3416                      if(this_ONLINE && next_ONLINE){                               // this: ON-LINE + next: ON-LINE
3417                              
3418                              if( this_run->ID == next_run->ID_RUN_FRAG ) interval = 0;     //=> run fragments
3419                              
3420                              if( interval >= 60 )CHECK = true;                     //more than 60 s => there might be a calibration
3421                              
3422    /*                        if( CHECK && !next_run->GetRM_ACQ_AFTER_CALIB() )
3423                                      printf(" ValidateRuns ***WARNING*** : DT = %i but RM_ACQ_AFTER_CALIB = %i \n",(t2-t1),next_run->GetRM_ACQ_AFTER_CALIB());*/
3424                                                      
3425                              
3426                              if( !CHECK && this_run->VALIDATION ){
3427                                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],true);
3428                                      nseq=0;
3429                              }
3430                      
3431                      }else if( !this_ONLINE && next_ONLINE) {              // this: DEFAULT + next:ON-LINE
3432                              
3433                              CHECK = true;
3434    //                        if( interval < 60 ) printf(" ValidateRuns ***WARNING*** : kkkkkk DT = %i \n",interval);
3435    
3436                      }else if( !next_ONLINE ){                                             // this:ANY + next:DEFAULT
3437                              
3438                              assignVALIDATION(next_run->ID,true);
3439                              nseq=0;
3440                      }
3441              }
3442    
3443              //----------------------------
3444              //check run sequence for calib
3445              //----------------------------
3446              if( CHECK ){
3447                      // check if calibration exists
3448                      printf("DT %i ===> CHECK Missing calibration\n",interval);
3449                      Bool_t MISSING = MissingTRK_CALIB(t1,t2);
3450                      for (Int_t irun = 0; irun < nseq; irun++)assignVALIDATION(sequence[irun],!MISSING);
3451                      nseq=0;
3452              } else printf("DT %i\n",interval);
3453              //--------------
3454              //store run info
3455              //--------------
3456              *next_run   = *this_run;
3457              next_ONLINE = this_ONLINE;
3458              if( !this_BAD ){
3459                      if(nseq < nseq_max){
3460                              sequence[nseq] = this_run->ID;
3461                              nseq++;
3462                      }else printf("ValidateRuns ***WARNING*** : run sequence exceed assumed size (%i) \n",nseq_max);
3463              };
3464              
3465              nrow++;
3466              
3467      };
3468      delete this_run;
3469      delete next_run;
3470    //    //
3471    return(0);    return(0);
3472  };  };
3473    /**
3474     * Check if there might be a missing tracker calibration in a given time interval
3475     * @param t1 From absolute time
3476     * @param t2 To absolute time
3477     * @return true if there might be a missing calibration
3478     */
3479    Bool_t PamelaDBOperations::MissingTRK_CALIB(UInt_t t1,UInt_t t2){
3480            
3481            GL_TRK_CALIB* trkcalib = new GL_TRK_CALIB();
3482            
3483            // get the closest VALIDATED calibration before the run start (t2)
3484            if ( trkcalib->Query_GL_TRK_CALIB(t2, conn) )return(true);      //>>> missing
3485            
3486            if ( trkcalib->TO_TIME  < t2 ) return(true);                                    //>>> missing
3487            
3488            //==============================================================
3489            // Check is done first on the basis of time between calibration,
3490            // which should be equal to the time between ascending-nodes.
3491            //==============================================================
3492            if ( t2 - trkcalib->FROM_TIME > 5700) {
3493                    printf("Long time to previous calib %i :-( \n",t2 - trkcalib->FROM_TIME);
3494            //==============================================================
3495            // there might be a missing calibration, due to:
3496            // - MM full
3497            // - corrupted packets
3498            // - loss of data
3499            // There is an exception in case a download was done during ascending node
3500            //==============================================================
3501                    Bool_t DOWNLOAD = false;
3502                    // check if the calib was skipped becouse of download .... DA FARE!!
3503                    if(DOWNLOAD)return(false);
3504                    
3505                    return(true);                                   //>>> missing
3506                    
3507            };
3508            
3509            //==============================================================
3510            // If the last calibration is close to the run less than this time,
3511            // it is enough to say that there are no missing calibrations
3512            //==============================================================
3513            // the long time interval bewteen runs might be due to download
3514            printf("Short time to previous calib %i :-) \n",t2 - trkcalib->FROM_TIME);
3515            return(false);
3516            
3517    };
3518    /**
3519     * Assign VALIDATION value to a GL_RUN entry
3520     * @param idrun Run ID
3521     * @param validation true/false
3522     */
3523    Int_t PamelaDBOperations::assignVALIDATION(UInt_t idrun, Bool_t validation){
3524            TSQLResult *result = 0;
3525            stringstream oss;
3526            oss.str("");
3527            oss << " UPDATE GL_RUN SET VALIDATION="<< (UInt_t)validation <<" WHERE ID= " << idrun << "';";
3528            //
3529    //      if ( IsDebug() )
3530            printf(" Set VALIDATION = %i for run %i \n",validation,idrun);
3531    //      printf(" Set VALIDATION = %i for run %i : query is \n %s \n",validation,idrun,oss.str().c_str());
3532    //      result = conn->Query(oss.str().c_str());
3533    //      if ( !result ) throw -4;
3534            return(0);
3535    }
3536    
3537    
3538    

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.23