#include <HistogramFitFCN.h>
Public Member Functions | |
HistogramFitFCN (const TH1D *=NULL, const TH1D *=NULL) | |
~HistogramFitFCN () | |
void | SetTemplateHist (const TH1D *, double ped=-1) |
void | SetPulseHist (const TH1D *) |
void | SetRefineFactor (int refine_factor) |
void | SetTimeOffset (double time_offset) |
double | operator() (const std::vector< double > &par) const |
Used for calls with parameters The return value is the chi squared weighted by errors in fTemplateHist Parameters: 1. Pedestal 2. Amplitude 3. Timing. | |
double | Up () const |
Used for error... somehow? | |
int | GetNDoF () |
Private Attributes | |
int | fNDoF |
double | fTimeOffset |
the time offset to use | |
double | fTemplatePedestal |
int | fRefineFactor |
const TH1D * | fTemplateHist |
const TH1D * | fPulseHist |
Definition at line 9 of file HistogramFitFCN.h.
HistogramFitFCN::HistogramFitFCN | ( | const TH1D * | hTemplate = NULL , |
|
const TH1D * | hPulse = NULL | |||
) |
Definition at line 6 of file HistogramFitFCN.cpp.
00006 : fTemplateHist(hTemplate), fPulseHist(hPulse) { 00007 }
HistogramFitFCN::~HistogramFitFCN | ( | ) |
Definition at line 9 of file HistogramFitFCN.cpp.
int HistogramFitFCN::GetNDoF | ( | ) | [inline] |
Definition at line 34 of file HistogramFitFCN.h.
References fNDoF.
Referenced by TemplateFitter::FitPulseToTemplate().
00034 { return fNDoF; }
double HistogramFitFCN::operator() | ( | const std::vector< double > & | par | ) | const |
Used for calls with parameters The return value is the chi squared weighted by errors in fTemplateHist Parameters: 1. Pedestal 2. Amplitude 3. Timing.
Definition at line 29 of file HistogramFitFCN.cpp.
References fNDoF, fPulseHist, fRefineFactor, fTemplateHist, and fTimeOffset.
00029 { 00030 // Chi2 fit with pedestal P, amplitude A, and timing T 00031 // Warning: The time is truncated to an int, so if there's 00032 // so if the step size in MINUIT is smalled than 1, 00033 // Any value of T will probably be seen as minimized, which it 00034 // almost certainly will not be. 00035 double chi2 = 0.; 00036 double P = par[0]; 00037 double A = par[1]; 00038 int T_int = (int)fTimeOffset; // Integral part of time shift 00039 double T_flt = fTimeOffset - (double)T_int; // Floating point offset for linear interpolation 00040 00041 bool print_dbg = false; 00042 if (strcmp(fTemplateHist->GetName(), "hTemplate_ScL") == 0) { 00043 print_dbg = false; 00044 } 00045 00046 if (print_dbg) { 00047 std::cout << "HistogramFitFCN::operator() (start):" << std::endl; 00048 std::cout << "\tpedestal = " << P 00049 << ", amplitude = " << A 00050 << ", time (integer part) = " << T_int 00051 << " and time (float part) = " << T_flt << std::endl; 00052 } 00053 00054 int half_range = 10*fRefineFactor; // remove a few bins from the fit 00055 int bounds[2]; 00056 bounds[0] = half_range+1;//std::max(T_int - fTemplateHist->GetNbinsX() / 2, 1); 00057 bounds[1] = std::min(fTemplateHist->GetNbinsX(), fPulseHist->GetNbinsX()) - half_range-1; //std::min(T_int + fTemplateHist->GetNbinsX() / 2 - 1, fPulseHist->GetNbinsX()); 00058 00059 fNDoF = ((bounds[1] - bounds[0] + 1)/fRefineFactor) - par.size(); // +1 because we include both ends of the bounds when we loop through 00060 00061 if (print_dbg) { 00062 std::cout << "NBinsX: hTemplate = " << fTemplateHist->GetNbinsX() << ", hPulse = " << fPulseHist->GetNbinsX() << std::endl; 00063 std::cout << "Bound Defns: " << std::endl; 00064 std::cout << "\tbounds[0] = " << bounds[0] << std::endl; 00065 std::cout << "\tbounds[1] = " << bounds[1] << std::endl; 00066 } 00067 00068 // Chi2 will be zero if shift is too high 00069 if (print_dbg) { 00070 if (bounds[1] <= bounds[0]) 00071 std::cout << "ERROR: Fit of two histograms involves shifting one out-of-bounds (no overlap)!" << std::endl; 00072 } 00073 00074 double f; 00075 double temp_ped=fTemplateHist->GetBinContent(1); 00076 for (int i = bounds[0]+(fRefineFactor/2.0); i <= bounds[1]-(fRefineFactor/2.0); i += fRefineFactor) { 00077 // calculate the chi^2 based on the centre of the 5 bins to avoid getting 00078 // abonus from mathcing all 5. We shift and scale the template so that it 00079 // matches the pulse. This is because, when we have a normalised template, 00080 // we will get the actual amplitude, pedestal and time from the fit and not 00081 // just offsets 00082 f = fTemplateHist->GetBinContent(i - T_int) ; 00083 f += T_flt*(fTemplateHist->GetBinContent(i - T_int + 1) - f); // linear interpolation between the i'th and the (i+1)'th bin 00084 if (print_dbg) { 00085 std::cout << "i = " << i << ", i - T_int = " << i-T_int << std::endl; 00086 std::cout << "f (before) = " << f << std::endl; 00087 } 00088 f = A * (f - temp_ped) + P; // apply the transformation to this bin 00089 if (print_dbg) { 00090 std::cout << "f (after) = " << f << std::endl; 00091 } 00092 00093 00094 double delta = fPulseHist->GetBinContent(i) - f; 00095 if (print_dbg) { 00096 std::cout << "Pulse Value = " << fPulseHist->GetBinContent(i) << ", delta = " << delta << std::endl; 00097 } 00098 double hTemplate_bin_error = fTemplateHist->GetBinError(i - T_int); 00099 //double hPulse_bin_error = fPulseHist->GetBinError(i); 00100 chi2 += delta*delta / (A*hTemplate_bin_error*hTemplate_bin_error); 00101 if (print_dbg) { 00102 std::cout << "Template Error = " << hTemplate_bin_error << ", chi2 (added) = " << delta*delta/(hTemplate_bin_error*hTemplate_bin_error) << ", chi2 (total) = " << chi2 << std::endl; 00103 } 00104 } 00105 00106 if (print_dbg) { 00107 std::cout << "HistogramFitFCN::operator() (end): " << std::endl; 00108 std::cout << "\tFit:\tChi2 " << chi2 << "\tP " 00109 << P << "(" << par[0] << ")\tA " << A << "(" << par[1] << ")\tT " << T_int 00110 << std::endl << std::endl; 00111 } 00112 return chi2; 00113 }
void HistogramFitFCN::SetPulseHist | ( | const TH1D * | hPulse | ) |
Definition at line 21 of file HistogramFitFCN.cpp.
References fPulseHist.
Referenced by TemplateFitter::FitPulseToTemplate().
00021 { 00022 fPulseHist = hPulse; 00023 }
void HistogramFitFCN::SetRefineFactor | ( | int | refine_factor | ) | [inline] |
Definition at line 18 of file HistogramFitFCN.h.
References fRefineFactor.
Referenced by TemplateFitter::TemplateFitter().
00018 {fRefineFactor = refine_factor;}
void HistogramFitFCN::SetTemplateHist | ( | const TH1D * | hTemplate, | |
double | ped = -1 | |||
) |
Definition at line 12 of file HistogramFitFCN.cpp.
References fTemplateHist, and fTemplatePedestal.
Referenced by TemplateFitter::FitPulseToTemplate().
00012 { 00013 fTemplateHist = hTemplate; 00014 if(pedestal>0){ 00015 fTemplatePedestal = pedestal; 00016 } else { 00017 fTemplatePedestal = fTemplateHist->GetBinContent(1); 00018 } 00019 }
void HistogramFitFCN::SetTimeOffset | ( | double | time_offset | ) |
Definition at line 25 of file HistogramFitFCN.cpp.
References fTimeOffset.
Referenced by TemplateFitter::FitPulseToTemplate().
00025 { 00026 fTimeOffset = time_offset; 00027 }
double HistogramFitFCN::Up | ( | ) | const |
Used for error... somehow?
Definition at line 115 of file HistogramFitFCN.cpp.
int HistogramFitFCN::fNDoF [mutable, private] |
record this for TemplateFitter to retrieve later (NB mutable so that it can be set in operator(), which is const)
Definition at line 39 of file HistogramFitFCN.h.
Referenced by GetNDoF(), and operator()().
const TH1D* HistogramFitFCN::fPulseHist [private] |
Definition at line 48 of file HistogramFitFCN.h.
Referenced by operator()(), and SetPulseHist().
int HistogramFitFCN::fRefineFactor [private] |
Definition at line 45 of file HistogramFitFCN.h.
Referenced by operator()(), and SetRefineFactor().
const TH1D* HistogramFitFCN::fTemplateHist [private] |
Definition at line 47 of file HistogramFitFCN.h.
Referenced by operator()(), and SetTemplateHist().
double HistogramFitFCN::fTemplatePedestal [private] |
Definition at line 43 of file HistogramFitFCN.h.
Referenced by SetTemplateHist().
double HistogramFitFCN::fTimeOffset [private] |
the time offset to use
Definition at line 42 of file HistogramFitFCN.h.
Referenced by operator()(), and SetTimeOffset().