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

Generated on Fri Nov 19 2010 17:02:53 for Bayesian Analysis Toolkit by  doxygen 1.7.1