• Main Page
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

BCTemplateFitter.h

Go to the documentation of this file.
00001 #ifndef __BCTEMPLATEFITTER__H
00002 #define __BCTEMPLATEFITTER__H
00003 
00004 /*!
00005  * \class BCTemplateFitter
00006  * This class can be used for fitting several template
00007  * histograms to a data histogram. The templates are assumed to have
00008  * no statistical uncertainty whereas the data are assumed to have
00009  * Poissonian fluctuations in each bin. Several methods to judge the
00010  * validity of the model are available.
00011  * \brief A class for fitting several templates to a data set.
00012  * \author Daniel Kollar
00013  * \author Kevin Kröninger
00014  * \date 10.04.2010
00015  */
00016 
00017 /*
00018  * Copyright (C) 2008, 2009, 2010, Daniel Kollar and Kevin Kroeninger.
00019  * All rights reserved.
00020  *
00021  * For the licensing terms see doc/COPYING.
00022  */
00023 
00024 // ---------------------------------------------------------
00025 
00026 #include <BAT/BCModel.h>
00027 
00028 #include <TH1D.h>
00029 
00030 class TH2D;
00031 
00032 // ---------------------------------------------------------
00033 
00034 class BCTemplateFitter : public BCModel
00035 {
00036    public:
00037 
00038       /**
00039        * The default constructor.
00040        */
00041       BCTemplateFitter();
00042 
00043       /**
00044        * A constructor.
00045        */
00046       BCTemplateFitter(const char * name);
00047 
00048       /**
00049        * The default destructor.
00050        */
00051       ~BCTemplateFitter();
00052 
00053       /**
00054        * Return the number of templates.
00055        */
00056       int GetNTemplates()
00057          { return int(fTemplateParIndexContainer.size()); }
00058 
00059       /**
00060        * Return the number of sources of systematic uncertainties.
00061        */
00062       int GetNSystErrors()
00063          { return int(fSystErrorParIndexContainer.size()); }
00064 
00065       /**
00066        * Return the number of degrees of freedom.
00067        */
00068       int GetNDF()
00069          { return fHistData.GetNbinsX() - GetNParameters(); }
00070 
00071       /**
00072        * Return the number of ratios to calculate.
00073        */
00074       int GetNRatios()
00075          { return int(fHistRatios1D.size()); }
00076 
00077       /**
00078        * Return the vector of histgrams containing the ratios.
00079        */
00080       std::vector<TH1D> GetHistRatios1D()
00081          { return fHistRatios1D; }
00082 
00083       /**
00084        * Return a ratio histogram
00085        */
00086       TH1D GetHistRatio1D(int index)
00087          { return fHistRatios1D.at(index); }
00088 
00089       /**
00090        * Return the index of a template.
00091        * @param name The template name.
00092        */
00093       int GetIndexTemplate(const char * name);
00094 
00095       /**
00096        * Return the index of a source of systematic uncertainty.
00097        * @param name The name of the source.
00098        */
00099       int GetIndexSystError(const char * name);
00100 
00101       /**
00102        * Return the parameter index corresponding to a template.
00103        * @param name The template name.
00104        */
00105       int GetParIndexTemplate(const char * name);
00106 
00107       /**
00108        * Return the parameter index corresponding to a template.
00109        * @param index The template index.
00110        */
00111       int GetParIndexTemplate(int index);
00112 
00113       /**
00114        * Return the parameter index corresponding to an efficiency.
00115        * @param name The name of the template associated with the efficiency.
00116        */
00117       int GetParIndexEff(const char * name);
00118 
00119       /**
00120        * Return the parameter index corresponding to an efficiency.
00121        * @param index The index of the template associated with the efficiency.
00122        */
00123       int GetParIndexSystError(const char * name);
00124 
00125       /**
00126        * Return a template histogram.
00127        * @param name The template name.
00128        */
00129       //    TH1D GetTemplate(const char * name);
00130 
00131       /**
00132        * Return the histogram containing the data.
00133        */
00134       TH1D GetData()
00135          { return fHistData; }
00136 
00137       /**
00138        * Set a flag for having physical limits (expectation values greater
00139        * or equal to 0).
00140        * @param flag The flag.
00141        */
00142       void SetFlagPhysicalLimits(bool flag)
00143          { fFlagPhysicalLimits = flag; }
00144 
00145       /**
00146        * Set the flag for using a fixed normalization (true) or floating
00147        * normalization (false).
00148        */
00149       void SetFlagFixNorm(bool flag)
00150          { fFlagFixNorm = flag; }
00151 
00152       /**
00153        * Set normalization constant.
00154        */
00155       void SetNorm(double norm)
00156          { fNorm = norm; }
00157 
00158       /**
00159        * Set the histogram containing the data.
00160        * @param hist The data histogram.
00161        * @return An error code.
00162        */
00163       int SetData(const TH1D& hist);
00164 
00165       /**
00166        * Initialize the fitting procedure.
00167        * @return An error code.
00168        */
00169       int Initialize();
00170 
00171       /**
00172        * Add a template histogram. The templates do not have to be
00173        * normalized. The histogram has to have the same number of bins
00174        * and cover the same region as the data histogram.
00175        * @param hist The template histogram
00176        * @param name The name of the template
00177        * @param Nmin The lower limit of the normalization.
00178        * @param Nmax The upper limit of the normalization.
00179        */
00180       int AddTemplate(TH1D hist, const char * name, double Nmin=0, double Nmax=0);
00181 
00182       /**
00183        * Set a Gaussian prior on the template.
00184        * @param name The name of the template.
00185        * @param mean The mean value of the prior.
00186        * @param rms The standard deviation of the prior.
00187        * @return An error code.
00188        */
00189       int SetTemplatePrior(const char * name, double mean, double sigma);
00190 
00191       /**
00192        * Set an arbitrary prior on the template
00193        * @param name The name of the template.
00194        * @param prior A histogram describing the prior.
00195        * @return An error code.
00196        */
00197       int SetTemplatePrior(const char * name, TH1D prior);
00198 
00199       /**
00200        * Describe the efficiency and the uncertainty for all bins.
00201        * @param name The template name.
00202        * @param effmean The mean value of the efficiency.
00203        * @param errsigma The uncertainty on the efficiency.
00204        */
00205       int SetTemplateEfficiency(const char * name, double effmean = 1., double effsigma = 0.);
00206 
00207       /**
00208        * Describe the efficiency and the uncertainty for each bin.
00209        * @param name The template name.
00210        * @param eff A histogram describing the efficieny.
00211        * @param efferr A histogram describing the uncertainty on the efficiency.
00212        * @return An error code.
00213        */
00214       int SetTemplateEfficiency(const char * name, TH1D eff, TH1D efferr);
00215 
00216       /**
00217        * Add a source of systematic uncertainty.
00218        * @param errorname The name of the source.
00219        * @param errortype The shape of the uncertainty in each bin.
00220        * @return An error code.
00221        */
00222       int AddSystError(const char * errorname, const char * errtype = "gauss");
00223 
00224       /**
00225        * Set the systematic uncertainty on a template.
00226        * @param errorname The name of the source.
00227        * @param templatename The template name.
00228        * @param parerror A histogram describing the uncertainty.
00229        * @return An error code.
00230        */
00231       int SetTemplateSystError(const char * errorname, const char * templatename, TH1D parerror);
00232 
00233       /**
00234        * Add a correlation among two sources of systematic uncertainty.
00235        * @param errorname1 The name of the first source.
00236        * @param errorname2 The name of the second source.
00237        * @param corr The correlation coefficiency.
00238        * @return An error code.
00239        */
00240       //    int AddSystErrorCorrelation(const char * errorname1, const char * errnorame2, double corr);
00241 
00242       /**
00243        * Constrains a sum of contributions. Assume a Gaussian prior.
00244        * @param indices The vector of indicies for the contributions.
00245        * @param mean The mean value of the prior.
00246        * @param rms The standard deviation of the prior.
00247        * @return An error code.
00248        */
00249       int ConstrainSum(std::vector <int> indices, double mean, double rms);
00250 
00251       /**
00252        * Add the calculation of a certain ratio.
00253        * @param index Index in the numerator.
00254        * @param indices Vector of indices in the denominator.
00255        * @param rmin The minimum ratio
00256        * @param rmax The maximum ratio
00257        * @return An error code.
00258        */
00259       int CalculateRatio(int index, std::vector<int> indices, double rmin = -1.0, double rmax = 1.0);
00260 
00261       /**
00262        * Checks if two histograms have the same properties.
00263        * @return 0 if not, 1 if they have the same properties.
00264        */
00265       int CompareHistogramProperties(TH1D hist1, TH1D hist2);
00266 
00267       /**
00268        * Calculates and returns the chi2 value. The chi2 is calculated
00269        * using the expectation value for the uncertainty.
00270        */
00271       double CalculateChi2();
00272 
00273       /**
00274        * Calculates and returns the chi2-probability.
00275        */
00276       double CalculateChi2Prob();
00277 
00278       /**
00279        * Calculates and returns the Likelihood at the global mode.
00280        */
00281       double CalculateMaxLike();
00282 
00283       /**
00284        * Calculates and returns the p-value for the global mode.
00285        */
00286       double CalculatePValue();
00287 
00288       /**
00289        * Calculates and returns the Kolmogorov-Smirnov-probability.
00290        */
00291       double CalculateKSProb();
00292 
00293       /**
00294        * Overloaded from BCIntegrate.
00295        */
00296       void MCMCUserIterationInterface();
00297 
00298       /**
00299        * Prints the stack of templates scaled with the global mode. The
00300        * data is plotted on top. The following options are available:\n\n
00301        * "L"  : adds a legend\n\n
00302        * "E0" : symmetric errorbars on the data points with a length of sqrt(obs).\n\n
00303        * "E1" : symmetric errorbars on the sum of templates with a length of sqrt(exp).\n\n
00304        * "E2" : asymmetric errorbars on the sum of templates from error
00305        * propagation of the parameters.\n\n
00306        * "E3" : asymmetric errorbars on the data points. The errorbars
00307        * mark the 16% and 85% quantiles of the Poisson distribution
00308        * rounded to the lower or upper integer, respectively.
00309        * @param filename The name of the file the output is printed to.
00310        * @param options Plotting options
00311        */
00312       void PrintStack(const char * filename = "stack.ps", const char * options="LE2E0D");
00313 
00314       /**
00315        * Print the ratios and the norm.
00316        * @param filename The filename.
00317        * @param option Plot options
00318        * @param pvalue Value which goes with plot options (see BAT manual).
00319        */
00320       void PrintRatios(const char * filename = "ratio.ps", int option = 0, double ovalue = 0.);
00321 
00322       /**
00323        * Print a template to a file.
00324        * @param name The template name.
00325        * @param filename The filename.
00326        * @return An error code.
00327        */
00328       int PrintTemplate(const char * name, const char * filename);
00329 
00330       /**
00331        * Temporary entry. Used for debugging.
00332        */
00333       void PrintTemp();
00334 
00335       /**
00336        * Create a histogram with specified uncertainties.
00337        * @param hist The histogram to be copied.
00338        * @param histerr The uncertainties of the new histogram.
00339        * @return A histogram with uncertainties.
00340        */
00341       TH1D CreateErrorHist(TH1D hist, TH1D histerr);
00342 
00343       /**
00344        * Calculates and returns the log of the prior probability at a
00345        * given point in parameter space.
00346        */
00347       double LogAPrioriProbability(std::vector <double> parameters);
00348 
00349       /**
00350        * Calculates and returns the log of the Likelihood at a given point
00351        * in parameter space.
00352        */
00353       double LogLikelihood(std::vector <double> parameters);
00354 
00355       /**
00356        * Perform the template fit.
00357        * @return An error code.
00358        */
00359       int PerformFit();
00360 
00361       /**
00362        * Cobine all sources of systematic uncertainties (including
00363        * efficiency) by summing the squares.
00364        * @param name The name of the template
00365        * @return A histogram with the total uncertainty
00366        */
00367       TH1D CombineUncertainties(const char * name);
00368 
00369     protected:
00370 
00371       /**
00372        * The data histogram.
00373        */
00374       TH1D fHistData;
00375 
00376       // histogram container
00377 
00378       /**
00379        * A container of template histograms. */
00380       std::vector<TH1D> fTemplateHistogramContainer;
00381 
00382       /**
00383        * A container of efficiency histograms. */
00384       std::vector<TH1D> fEffHistogramContainer;
00385 
00386       /**
00387        * A container of efficiency uncertainty histograms. */
00388       std::vector<TH1D> fEffErrHistogramContainer;
00389 
00390       /**
00391        * A matrix of histograms describing systematic uncertainties. */
00392       std::vector< std::vector<TH1D> > fSystErrorHistogramContainer;
00393 
00394       /**
00395        * A container of prior histograms. */
00396       std::vector <TH1D> fPriorContainer;
00397 
00398       // name container
00399 
00400       /**
00401        * A container of template names. */
00402       std::vector<std::string> fTemplateNameContainer;
00403 
00404       /**
00405        * A container of names of sources of systematic uncertainties. */
00406       std::vector<std::string> fSystErrorNameContainer;
00407 
00408       // index container
00409 
00410       /**
00411        * A container of parameter indeces for templates. */
00412       std::vector<int> fTemplateParIndexContainer;
00413 
00414       /**
00415        * A container of parameter indeces for efficiencies. */
00416       std::vector<int> fEffParIndexContainer;
00417 
00418       /**
00419        * A container of parameter indeces for sources of systematic
00420        * uncertainty. */
00421       std::vector<int> fSystErrorParIndexContainer;
00422 
00423       // error type container
00424 
00425       /**
00426        * A container of error types. */
00427       std::vector<std::string> fSystErrorTypeContainer;
00428 
00429       /**
00430        * A container for constrained sums: indices.
00431        */
00432       std::vector< std::vector<int> > fConstraintSumIndices;
00433 
00434       /**
00435        * A container for constrained sums: mean values.
00436        */
00437       std::vector< double > fConstraintSumMean;
00438 
00439       /**
00440        * A container for constrained sums: mean values.
00441        */
00442       std::vector< double > fConstraintSumRMS;
00443 
00444       /**
00445        * Histogram containing the overall number of expected events.
00446        */
00447       TH1D fHistNorm;
00448 
00449       /**
00450        * Vector of indices for the calculation of ratios.
00451        */
00452       std::vector< std::vector<int> > fIndicesRatios1D;
00453 
00454       /**
00455        * 1-D histograms containing ratios.
00456        */
00457       std::vector <TH1D> fHistRatios1D;
00458 
00459       /**
00460        * Flag for fixing the normalization or not
00461        */
00462       bool fFlagFixNorm;
00463 
00464       /**
00465        * Flag for having physical limits (expectation values greater or
00466        * equal to 0).
00467        */
00468       bool fFlagPhysicalLimits;
00469 
00470       /**
00471        * A 2-d histogram for calculating the error bars
00472        */
00473       TH2D * fUncertaintyHistogramExp;
00474 
00475       /**
00476        * A 2-d histogram for calculating the error bars
00477        */
00478       TH2D * fUncertaintyHistogramObsPosterior;
00479 
00480       /**
00481        * Normalization constant
00482        */
00483       double fNorm;
00484 
00485       /**
00486        * The number of bins in the data. */
00487       int fNBins;
00488 
00489       /**
00490        * The minimum value of the data range. */
00491       double fXmin;
00492 
00493       /**
00494        * The maximum value of the data range. */
00495       double fXmax;
00496 
00497       /**
00498        * The number of bins for a prior distribution. */
00499       int fPriorNBins;
00500 
00501 };
00502 
00503 // ---------------------------------------------------------
00504 
00505 #endif
00506 

Generated on Mon Aug 30 2010 22:14:54 for Bayesian Analysis Toolkit by  doxygen 1.7.1