00001 #ifndef __BCEFFICIENCYFITTER__H 00002 #define __BCEFFICIENCYFITTER__H 00003 00004 /*! 00005 * \class BCEfficiencyFitter 00006 * \brief A class for fitting histograms with functions 00007 * \author Daniel Kollar 00008 * \author Kevin Kröninger 00009 * \version 1.0 00010 * \date 11.2008 00011 * \detail This class allows fitting of efficiencies defined as 00012 * a ratio of two TH1D histograms using a TF1 function. It uses 00013 * binomial probabilities calculated based on the number of entries 00014 * in histograms. This is only applicable if the numerator is 00015 * a subset of the denominator. 00016 */ 00017 00018 /* 00019 * Copyright (C) 2008, Daniel Kollar and Kevin Kroeninger. 00020 * All rights reserved. 00021 * 00022 * For the licensing terms see doc/COPYING. 00023 */ 00024 00025 // --------------------------------------------------------- 00026 00027 #include <vector> 00028 00029 #include <BAT/BCModel.h> 00030 00031 // ROOT classes 00032 class TH1D; 00033 class TF1; 00034 class TGraphAsymmErrors; 00035 00036 // --------------------------------------------------------- 00037 00038 class BCEfficiencyFitter : public BCModel 00039 { 00040 public: 00041 00042 /** \name Constructors and destructors */ 00043 /* @{ */ 00044 00045 /** 00046 * The default constructor. */ 00047 BCEfficiencyFitter(); 00048 00049 /** 00050 * A constructor. 00051 * @param hist1 The histogram with the larger numbers 00052 * @param hist2 The histogram with the smaller numbers 00053 * @param func The fit function. */ 00054 BCEfficiencyFitter(TH1D * hist1, TH1D * hist2, TF1 * func); 00055 00056 /** 00057 * The default destructor. */ 00058 ~BCEfficiencyFitter(); 00059 00060 /* @} */ 00061 /** \name Member functions (get) */ 00062 /* @{ */ 00063 00064 /** 00065 * @return The histogram 1 */ 00066 TH1D * GetHistogram1() 00067 { return fHistogram1; }; 00068 00069 /** 00070 * @return The histogram 2 */ 00071 TH1D * GetHistogram2() 00072 { return fHistogram2; }; 00073 00074 /** 00075 * @return The histogram ratio */ 00076 TGraphAsymmErrors * GetHistogramRatio() 00077 { return fHistogramRatio; }; 00078 00079 /** 00080 * @return The fit function */ 00081 TF1 * GetFitFunction() 00082 { return fFitFunction; }; 00083 00084 /** 00085 * @return pointer to the error band */ 00086 TGraph * GetErrorBand() 00087 { return fErrorBand; }; 00088 00089 /** 00090 * @return pointer to a graph for the fit function */ 00091 TGraph * GetGraphFitFunction() 00092 { return fGraphFitFunction; }; 00093 00094 /** 00095 * Calculates the lower and upper limits for a given probability. 00096 * @param n n for the binomial. 00097 * @param k k for the binomial. 00098 * @param p The central probability defining the limits. 00099 * @param xmin The lower limit. 00100 * @param xmax The upper limit. 00101 * @return A flag (=1 plot point, !=1 do not plot point). */ 00102 int GetUncertainties(int n, int k, double p, double &xmin, double &xmax); 00103 00104 /* @} */ 00105 /** \name Member functions (set) */ 00106 /* @{ */ 00107 00108 /** 00109 * @param hist The histogram 1 00110 * @param hist The histogram 2 00111 * @ return An error code (1:pass, 0:fail). */ 00112 int SetHistograms(TH1D * hist1, TH1D * hist2); 00113 00114 /** 00115 * @param func The fit function 00116 * @ return An error code (1:pass, 0:fail). */ 00117 int SetFitFunction(TF1 * func); 00118 00119 /** 00120 * Sets the flag for integration. \n 00121 * true: use ROOT's TH1D::Integrate() \n 00122 * false: use linear interpolation */ 00123 void SetFlagIntegration(bool flag) 00124 { fFlagIntegration = flag; }; 00125 00126 /* @} */ 00127 /** \name Member functions (miscellaneous methods) */ 00128 /* @{ */ 00129 00130 /** 00131 * The log of the prior probability. Overloaded from BCModel. 00132 * @param parameters A vector of doubles containing the parameter values. */ 00133 virtual double LogAPrioriProbability(std::vector <double> parameters); 00134 00135 /** 00136 * The log of the conditional probability. Overloaded from BCModel. 00137 * @param parameters A vector of doubles containing the parameter values. */ 00138 virtual double LogLikelihood(std::vector <double> parameters); 00139 00140 /** 00141 * Returns the y-value of the 1-dimensional fit function at an x and 00142 * for a set of parameters. 00143 * @param x A vector with the x-value. 00144 * @param parameters A set of parameters. */ 00145 double FitFunction(std::vector <double> x, std::vector <double> parameters); 00146 00147 /** 00148 * Performs the fit. 00149 * @return An error code. */ 00150 int Fit() 00151 { return this -> Fit(fHistogram1, fHistogram2, fFitFunction); }; 00152 00153 /** 00154 * Performs the fit. 00155 * @param hist1 The histogram with the larger number. 00156 * @param hist2 The histogram with the smaller number. 00157 * @param func The fit function. 00158 * @return An error code. */ 00159 int Fit(TH1D * hist1, TH1D * hist2, TF1 * func); 00160 00161 /** 00162 * Draw the fit in the current pad. */ 00163 void DrawFit(const char * options = "", bool flaglegend = false); 00164 00165 /** 00166 * Calculate the p-value using fast-MCMC. 00167 * @param par A set of parameter values 00168 * @param pvalue The pvalue 00169 * @return An error code */ 00170 int CalculatePValueFast(std::vector<double> par, double &pvalue); 00171 00172 /* @} */ 00173 00174 private: 00175 00176 /** 00177 * The histogram containing the larger numbers. */ 00178 TH1D * fHistogram1; 00179 00180 /** 00181 * The histogram containing the smaller numbers. */ 00182 TH1D * fHistogram2; 00183 00184 /** 00185 * The efficiency histogram. */ 00186 TGraphAsymmErrors * fHistogramRatio; 00187 00188 /** 00189 * The fit function */ 00190 TF1 * fFitFunction; 00191 00192 /** 00193 * Flag for using the ROOT TH1D::Integral method (true), or linear 00194 * interpolation (false) */ 00195 bool fFlagIntegration; 00196 00197 /** 00198 * Pointer to the error band (for legend) */ 00199 TGraph * fErrorBand; 00200 00201 /** 00202 * Pointer to a graph for displaying the fit function */ 00203 TGraph * fGraphFitFunction; 00204 00205 /** 00206 * Temporary histogram for calculating the binomial qunatiles */ 00207 TH1D * fHistogramBinomial; 00208 00209 }; 00210 00211 // --------------------------------------------------------- 00212 00213 #endif