Fitter.cc

00001 #include "Fitter.hh"
00002 #include "EventHandler.hh"
00003 #include "SumChannels.hh"
00004 #include "PulseFinder.hh"
00005 #include "TGraph.h"
00006 #include "RootWriter.hh"
00007 #include "intarray.hh"
00008 #include "TF1.h"
00009 #include <vector>
00010 #include <algorithm>
00011 #include <math.h>
00012 
00013 Fitter::Fitter():
00014   ChannelModule(GetDefaultName(), 
00015                 "Fit the pulse to the known scintillation shape")
00016 {
00017   AddDependency<PulseFinder>();
00019   //Register all the config handler parameters
00020   RegisterParameter("start_fit", start_fit = -100);
00021   RegisterParameter("end_fit", end_fit = 80000);
00022   RegisterParameter("slow_sample_rate", slow_sample_rate = 10);
00023   RegisterParameter("start_slow_sample_rate", start_slow_sample_rate = 500);
00024 }
00025 
00026 Fitter::~Fitter()
00027 {
00028   Finalize();
00029 }
00030 
00031 int Fitter::Initialize()
00032 {
00033   return 0;
00034 }
00035 
00036 int Fitter::Process(ChannelData* chdata)
00037 {
00038   if( chdata->pulses.size()==0) 
00039     return 0;
00040   
00041   double* wave = chdata->GetWaveform();
00042   
00043   for(size_t j=0; j < chdata->pulses.size(); j++){
00044     Pulse& pulse = chdata->pulses[j];
00045     if(!chdata->baseline.found_baseline || !pulse.found_start || 
00046        !pulse.found_peak || pulse.peak_saturated)
00047       return 0;
00048     
00049     PulseFit& fit = pulse.fit;
00050     fit.start_index = std::max(pulse.start_index+start_fit,0);
00051     fit.end_index = std::min(pulse.start_index+end_fit, pulse.end_index);
00052     
00053     int npts = pulse.start_index + start_slow_sample_rate - fit.start_index + 
00054       (int)ceil((fit.end_index - pulse.start_index - start_slow_sample_rate)/slow_sample_rate) + 1;
00055     int x[npts];
00056     int y[npts];
00057     
00058     int counter = 0;
00059     for(int samp=fit.start_index; samp< fit.end_index; samp++)
00060       {
00061         if(samp < pulse.start_index + start_slow_sample_rate || 
00062            samp%slow_sample_rate == 0)
00063           {
00064             x[counter] = samp;
00065             y[counter] = (int)wave[samp];
00066             counter++;
00067           }
00068       }
00069     TGraph graph(npts,x,y);
00070     
00071     
00072     //perform the fit
00073     
00074     
00075     TF1* func = fit.GetTF1();
00076     
00077     func->SetRange(fit.start_index, fit.end_index);
00078     func->SetParameters(1.09 * pulse.peak_amplitude, 
00079                         0.3, 0.7, 160., 1., 12000., 
00080                         chdata->baseline.mean, pulse.start_index + 0.5, 7500);
00081     func->SetParLimits(1, 0, 1);
00082     func->FixParameter(2, 0.70);
00083     func->FixParameter(6, chdata->baseline.mean);
00084     
00085     fit.fit_result = graph.Fit(func,"RWQN");
00086     
00087     if(fit.fit_result != 0){
00088       func->SetParameter(2,0.8);
00089       fit.fit_result = graph.Fit(func,"RWQN");
00090     }
00091     
00092     fit.fit_done = true;
00093     fit.StoreParams(func);
00094     
00095     delete func;
00096   }//end loop over pulses
00097   return 0;
00098 }
00099 
00100 int Fitter::Finalize()
00101 {   
00102   return 0;
00103 }
00104 
00105 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines

Generated on 20 Jun 2014 for daqman by  doxygen 1.6.1