pmtbaseline.cc

Go to the documentation of this file.
00001 #ifndef PMTBASELINE_CC
00002 #define PMTBASELINE_CC
00003 
00004 #include "pmtbaseline.hh"
00005 #include "pmt_waveform.hh"
00006 
00007 pmtbaseline::pmtbaseline(){
00008 
00009   _name="pmtbaseline"; 
00010   _fout=0;
00011 
00012   _study_tail=false;
00013   _use_tail=false;
00014   _bgpoints  = 1;
00015   _rdpoints  = 1;
00016   _nsigma   = 5.0;  // 5 times standard deviaiton ~ 2.5 ADC count
00017   _min_peak = 5.0;  // 2.5 ADC count above baseline
00018 
00019   clear_event();
00020 
00021 }
00022 
00023 void pmtbaseline::clear_event(){
00024   _fpedmean  = 0.0;
00025   _fpedrms   = 0.0;
00026   _ftailmean = 0.0;
00027   _ftailrms  = 0.0;
00028 
00029 }
00030 
00031 double pmtbaseline::mean(std::vector<UShort_t> adcs,UInt_t bgpoints){
00032   double mean = 0;
00033   UShort_t holder = 0;
00034   size_t index=0;
00035 
00036   for(index=0;index<bgpoints && index<adcs.size() ;index++)
00037     holder += adcs.at(index);
00038 
00039   if( index!=bgpoints )
00040     Message::send(MSG::WARNING,__FUNCTION__,
00041           Form("Waveform too short! Length: %d (need %d for baseline estimate).",(int)(adcs.size()),bgpoints));
00042 
00043   mean=((double)holder)/((double)index);
00044   return mean;
00045 }
00046 double pmtbaseline::rms(std::vector<UShort_t> adcs,UInt_t bgpoints, double mean){
00047   
00048   double rms    = 0;
00049   double holder = 0;
00050   size_t index  = 0;
00051   for(index=0;index<bgpoints && index<adcs.size();index++){
00052     holder += pow((double)adcs.at(index)-mean,2);
00053   }
00054 
00055   if( index!=bgpoints )
00056     Message::send(MSG::WARNING,__FUNCTION__,
00057           Form("Waveform too short! Length: %d (need %d for baseline estimate).",(int)(adcs.size()),bgpoints));
00058 
00059   holder=holder/((double)index);
00060   
00061   rms = sqrt(holder);
00062   return rms;
00063 }
00064 
00065 //calculate begin time
00066 double pmtbaseline::time_reconstructor(double fpedmean, PMT::ch_waveform_t::const_iterator adc_itr){
00067   double y1 = (*adc_itr)-fpedmean;
00068   adc_itr += 1;
00069   double y2 = (*adc_itr)-fpedmean;
00070   //x-axis measured in time_counter unit increments.
00071   //fine fraction of time_counter increments that designates start time
00072   double slope = (y2-y1)/1.0; //because Dx = 1 time_step
00073   //y = slope*x + x_0
00074   double time_reconstructed = y1/slope;
00075   return time_reconstructed;
00076 }
00077 
00078 
00079 double pmtbaseline::tailmean(std::vector<UShort_t> adcs,UInt_t rdpoints){
00080   double mean = 0;
00081   UShort_t holder = 0;
00082   size_t index=0;
00083   for(index=adcs.size()-1;index>=(adcs.size()-rdpoints); index--)
00084     holder += adcs.at(index);
00085   
00086   if( (adcs.size()-index-1)!=rdpoints )
00087     Message::send(MSG::WARNING,__FUNCTION__,
00088           Form("Waveform too short! Length: %d (need %d for baseline estimate).",(int)(adcs.size()),rdpoints));
00089 
00090   mean=(double)holder/(double)rdpoints; 
00091   return mean;
00092     
00093 }
00094 
00095 double pmtbaseline::tailrms(std::vector<UShort_t> adcs,UInt_t rdpoints, double mean){
00096   
00097   double rms    = 0;
00098   double holder = 0;
00099   size_t index  = 0;
00100   for(index=adcs.size()-1;index>=(adcs.size()-rdpoints);index--)
00101     holder += pow((double)adcs.at(index)-mean,2)/rdpoints;
00102 
00103   if( (adcs.size()-index-1)!=rdpoints )
00104     Message::send(MSG::WARNING,__FUNCTION__,
00105           Form("Waveform too short! Length: %d (need %d for baseline estimate).",(int)(adcs.size()),rdpoints));
00106   
00107   rms = sqrt(holder);
00108   return rms;
00109 }
00110 
00111 void pmtbaseline::histosetup(){
00112   
00113   pedMean     = new TH2D("bg_pedMean","Ped Mean (_bgpoints)",40,0,40,400,2000,2200);
00114   pedRMS      = new TH2D("bg_pedRMS","Ped RMS (bdpoints)",40,0,40,300,0,30);
00115   tailstudy   = new TH2D("rd_tailstudy","Tail Study",16,0,16,600,1900,2200);
00116 
00117   pedMeanAll  = new TH1D("bg_pedMeanAll","Ped Mean (all chan)",400 ,2000,2200 );
00118   pedRMSAll   = new TH1D("bg_pedRMSAll" ,"Ped RMS  (all chan)" ,300 ,0   ,30   );
00119 
00120   pedMeanCutrms  = new TH1D("bg_pedMeanCut","Ped Mean (rms>0.5 all chan)",400 ,2000,2200 );
00121   pedRMSCutrms   = new TH1D("bg_pedRMSCut" ,"Ped RMS  (rms>0.5 all chan)" ,300 ,0   ,30   );
00122  
00123   channels    = new TH1D("bg_channels","Beam Gate Channels",40,0,40); 
00124   tailMean    = new TH1D("rd_tailMean","Ped Tail Mean (_rdpoints)",400,2000,2200);
00125   tailRMS     = new TH1D("rd_tailRMS","Ped Tail RMS (_rdpoints)",100,0,10);
00126   
00127   tailMeanCutrms    = new TH1D("rd_tailMeanCut","Ped Tail Mean (rms>0.5)",400,2000,2200);
00128   tailRMSCutrms     = new TH1D("rd_tailRMSCut","Ped Tail RMS (rms>0.5)",100,0,10);
00129   reco_time = new TH1D("reco_time", "Reconstructed Start Time", 100, 0 , 50);
00130   reco_time_diff = new TH1D("reco_time_diff", "Reconstructed Start Time Diff: start - reco", 100, -5, 5);
00131   /*
00132     peakheights = new TH1D("peakheights","Subtracted Pulse Heights",500,0,1000);
00133     peakareas = new TH1D("peakareas","Areas",1125,0,5000);
00134     nptstaken = new TH1D("npointstake","Num. Points Taken for A.",40,0,80);
00135   */
00136 }
00137 
00138 bool pmtbaseline::initialize() {
00139   histosetup();  
00140   return true;
00141 }
00142 
00143 bool pmtbaseline::analyze(storage_manager* storage) {
00144   //New event
00145   clear_event();
00146   
00147   //Set points to hold number of kept points
00148   _max      = 15;
00149   
00150   double event_charge=0;
00151   double event_amplitude=0;
00152   int npulse=0;
00153 
00154   //Get event waveform from storage
00155   pmt_wf_collection   *ewform = (pmt_wf_collection*)(storage->get_data(DATA_STRUCT::PMT_WF_COLLECTION));
00156   pulse_collection *pulses = (pulse_collection*)(storage->get_data(DATA_STRUCT::PULSE_COLLECTION));
00157   
00158   //looping through all channels
00159   for(size_t i=0; i<ewform->size(); ++i){
00160     const pmt_waveform* pmt_data = &(ewform->at(i));
00161 
00162     if(pmt_data->size()<1){
00163       Message::send(MSG::ERROR,__FUNCTION__,
00164             Form("Found 0-length waveform: Event %d ... Ch. %d",ewform->event_id(),pmt_data->channel_number()));
00165       continue;
00166     }
00167 
00168     _fpedmean = mean((*pmt_data),_bgpoints         );
00169     _fpedrms  = rms ((*pmt_data),_bgpoints,_fpedmean);      
00170 
00171 
00172     if(_fpedrms>0.5 && _use_tail){
00173       _fpedmean = tailmean((*pmt_data),_bgpoints         );
00174       _fpedrms  = tailrms ((*pmt_data),_bgpoints,_fpedmean);      
00175     } 
00176     // Mean rms can be still bad! But we continue...(warning)
00177 
00178     pedMean     -> Fill( pmt_data->channel_number(), _fpedmean );
00179     pedRMS      -> Fill( pmt_data->channel_number(), _fpedrms  );
00180     pedMeanAll  -> Fill(  _fpedmean            );
00181     pedRMSAll   -> Fill(  _fpedrms             );
00182     channels    -> Fill( pmt_data->channel_number() );
00183     
00184     //Cut on high RMS
00185     if(_fpedrms>0.5){
00186       pedMeanCutrms->Fill(_fpedmean);
00187       pedRMSCutrms ->Fill(_fpedrms);
00188       
00189       for(UShort_t k=0;k<(*pmt_data).size();k++){
00190     bg_waveforms.push_back((double)(*pmt_data).at(k));
00191     bgticks.push_back((double)k);
00192       }
00193     }
00194     
00195     bool fire = false;
00196     int time_counter = 0;
00197     double t_start   = 0;
00198     double t_start_reco = 0;
00199     double t_start_diff = 0;
00200     double t_max     = 0;
00201     double t_end     = 0;
00202     double a_pulse   = 0;
00203     double q_pulse   = 0;
00204     
00205     //Going through all bins in waveform
00206     for(PMT::ch_waveform_t::const_iterator adc_itr((*pmt_data).begin());
00207     adc_itr!=(*pmt_data).end(); ++adc_itr) {
00208       if(!fire && (*adc_itr)>(_nsigma*_fpedrms+_fpedmean) && (*adc_itr)>(_min_peak+_fpedmean)) {
00209     fire    = true;
00210     t_start = time_counter;
00211 
00212     t_start_diff = time_reconstructor(_fpedmean, adc_itr);
00213     t_start_reco = t_start + time_reconstructor(_fpedmean, adc_itr);
00214 
00215     reco_time->Fill(t_start_reco);
00216     reco_time_diff->Fill(t_start_diff);
00217     q_pulse = 0;
00218     a_pulse = 0;
00219       }
00220       
00221       if(fire) {
00222     if((*adc_itr)<(_nsigma*_fpedrms+_fpedmean) || (*adc_itr)<(_min_peak+_fpedmean)) {
00223       npulse++;
00224       fire  = false;
00225       t_end = time_counter;       
00226     
00227       pulse_info pulse;
00228       pulse.set_ped_mean  ( _fpedmean );
00229       pulse.set_ped_rms   ( _fpedrms  );
00230       pulse.set_charge    ( q_pulse  );
00231       pulse.set_pulse_peak( a_pulse  );
00232       pulse.set_start_time( t_start  );
00233       pulse.set_start_time_reco( t_start_reco );
00234       pulse.set_end_time  ( t_end    );
00235       pulse.set_max_time  ( t_max    );
00236       pulse.set_channel_number(pmt_data->channel_number());
00237       pulse.set_timeslice((*pmt_data).timeslice());
00238       pulse.set_channel_frame_id((*pmt_data).channel_frame_id());
00239       pulse.set_disc_id((*pmt_data).disc_id());
00240       pulses->push_back(pulse);
00241       
00242       event_charge     += pulse.charge(); 
00243       event_amplitude  += pulse.pulse_peak();
00244       
00245     } else {
00246       q_pulse += (*adc_itr) - _fpedmean;
00247       if((*adc_itr) > (a_pulse + _fpedmean)) {
00248         a_pulse= (*adc_itr) - _fpedmean;
00249         t_max=time_counter;
00250       } 
00251     }
00252     
00253       }//end fire
00254       
00255       
00256       time_counter++;
00257     } //End ADCs
00258     
00259     
00260     //Study tail
00261     if(_study_tail && i == 6){
00262       for(int k=3;k<=_max;++k)
00263     {
00264       _ftailmean  = tailmean((*pmt_data),k);
00265       _ftailrms   = tailrms ((*pmt_data),k,_ftailmean);
00266       tailstudy -> Fill((double)k,_ftailmean);
00267     }
00268     }
00269     
00270     
00271   } //End Channels
00272   
00273   pulses->set_sum_charge(event_charge   );  
00274   pulses->set_sum_peak  (event_amplitude);  
00275   pulses->set_npulse    (npulse         );
00276   
00277   /*/// The following is for modifying pulse info ... NOT FOR CREATING!
00278     pulse_collection* pulse_group = ((storage_manager*)storage)->get_pulse_collection_writeable();
00279     for(pulse_collection::iterator iter(pulse_group->begin());
00280     iter!=pulse_group->end();
00281     ++iter)
00282     {
00283     (*iter).channel_number();
00284     (*iter).set_start_time(10);
00285     }*/
00286   
00287   
00288   return true;
00289 }
00290 
00291 bool pmtbaseline::finalize() {
00292 
00293   int n = rdticks.size();
00294   rdbadwaveforms = new TGraphErrors(n,&rdticks[0],&rd_waveforms[0]);
00295   rdbadwaveforms->SetName("rd_badwforms");
00296   rdbadwaveforms->SetTitle("ADC count vs. Time tick - Random");
00297   
00298   
00299   int t = bgticks.size();
00300   bgbadwaveforms = new TGraphErrors(t,&bgticks[0],&bg_waveforms[0]);
00301   bgbadwaveforms->SetName("bg_badwforms");
00302   bgbadwaveforms->SetTitle("ADC count vs. Time tick - Beam Gate");
00303   if(_fout){
00304   _fout          -> cd();
00305   pedMean        -> Write();
00306   pedRMS         -> Write();
00307   channels       -> Write();
00308   tailMean       -> Write();
00309   tailRMS        -> Write();
00310   tailstudy      -> Write();
00311   rdbadwaveforms -> Write();
00312   bgbadwaveforms -> Write();
00313   tailMeanCutrms -> Write();
00314   tailRMSCutrms  -> Write();
00315   reco_time      -> Write();
00316   reco_time_diff -> Write();
00317   /*  peakheights -> Write();
00318   peakareas   -> Write();
00319   nptstaken   -> Write();*/
00320   }else{
00321     Message::send(MSG::ERROR,__FUNCTION__,"Analysis output file not available!");
00322   }
00323   return true;
00324 }
00325 
00326 #endif

Generated on Mon Apr 7 15:35:12 2014 for MyProject by  doxygen 1.4.7