laura is hosted by Hepforge, IPPP Durham
Laura++  v3r5
A maximum likelihood fitting package for performing Dalitz-plot analysis.
LauSumPdf.cc
Go to the documentation of this file.
1 
2 /*
3 Copyright 2006 University of Warwick
4 
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
8 
9  http://www.apache.org/licenses/LICENSE-2.0
10 
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 */
17 
18 /*
19 Laura++ package authors:
20 John Back
21 Paul Harrison
22 Thomas Latham
23 */
24 
29 #include <iostream>
30 #include <vector>
31 
32 #include "TMath.h"
33 #include "TSystem.h"
34 
35 #include "LauConstants.hh"
36 #include "LauParameter.hh"
37 #include "LauSumPdf.hh"
38 
40 
41 
43  LauAbsPdf(pdf1 ? pdf1->varNames() : std::vector<TString>(), std::vector<LauAbsRValue*>(), pdf1 ? pdf1->getMinAbscissas() : LauFitData(), pdf1 ? pdf1->getMaxAbscissas() : LauFitData()),
44  pdf1_(pdf1),
45  pdf2_(pdf2),
46  frac_(frac)
47 {
48  // Constructor for the sum PDF.
49  // We are defining the sum as:
50  // f x (PDF1/S(PDF1)) + (1-f) x (PDF2/S(PDF2))
51  // where f is the fraction, x is multiplication, PDFi is the i'th PDF,
52  // and S(PDFi) is the integral of the i'th PDF.
53  //
54  // The last two arguments specify the range in which the PDF is defined, and the PDF
55  // will be normalised w.r.t. these limits.
56 
57  // So the first thing we have to do is check the pointers are all valid.
58  if (!frac || !pdf1 || !pdf2) {
59  std::cerr << "ERROR in LauSumPdf constructor : one of the 3 pointers is null." << std::endl;
60  gSystem->Exit(EXIT_FAILURE);
61  }
62 
63  // Next check that the abscissa ranges are the same for each PDF
64  if (pdf1->getMinAbscissa() != pdf2->getMinAbscissa()) {
65  std::cerr << "ERROR in LauSumPdf constructor : minimum abscissa values not the same for the two PDFs." << std::endl;
66  gSystem->Exit(EXIT_FAILURE);
67  }
68  if (pdf1->getMaxAbscissa() != pdf2->getMaxAbscissa()) {
69  std::cerr << "ERROR in LauSumPdf constructor : maximum abscissa values not the same for the two PDFs." << std::endl;
70  gSystem->Exit(EXIT_FAILURE);
71  }
72 
73  // Also check that both PDFs are expecting the same number of input variables
74  if (pdf1->nInputVars() != pdf2->nInputVars()) {
75  std::cerr << "ERROR in LauSumPdf constructor : number of input variables not the same for the two PDFs." << std::endl;
76  gSystem->Exit(EXIT_FAILURE);
77  }
78 
79  // Also check that both PDFs are expecting the same variable name
80  if (pdf1->varNames() != pdf2->varNames()) {
81  std::cerr << "ERROR in LauSumPdf constructor : variable name not the same for the two PDFs." << std::endl;
82  gSystem->Exit(EXIT_FAILURE);
83  }
84 
85  // Then we need to grab all the parameters and pass them to the base class.
86  // This is so that when we are asked for them they can be put into the fit.
87  // The number of parameters is the number in PDF1 + the number in PDF2 + 1 for the fraction.
88  UInt_t nPar(pdf1->nParameters()+pdf2->nParameters()+1);
89  std::vector<LauAbsRValue*> params; params.reserve(nPar);
90  std::vector<LauAbsRValue*>& pdf1pars = pdf1->getParameters();
91  std::vector<LauAbsRValue*>& pdf2pars = pdf2->getParameters();
92  params.push_back(frac);
93  for (std::vector<LauAbsRValue*>::iterator iter = pdf1pars.begin(); iter != pdf1pars.end(); ++iter) {
94  params.push_back(*iter);
95  }
96  for (std::vector<LauAbsRValue*>::iterator iter = pdf2pars.begin(); iter != pdf2pars.end(); ++iter) {
97  params.push_back(*iter);
98  }
99  this->addParameters(params);
100 
101  // Now check that we can find the fraction parameter ok
102  frac_ = this->findParameter("frac");
103  if (frac_ == 0) {
104  std::cerr << "ERROR in LauSumPdf constructor : parameter \"frac\" not found." << std::endl;
105  gSystem->Exit(EXIT_FAILURE);
106  }
107 
108  // Cache the normalisation factor
109  this->calcNorm();
110 }
111 
113 {
114  // Destructor
115 }
116 
118 {
119  // Check that the given abscissa is within the allowed range
120  if (!this->checkRange(abscissas)) {
121  gSystem->Exit(EXIT_FAILURE);
122  }
123 
124  // Get the up to date parameter values
125  Double_t frac = frac_->unblindValue();
126 
127  // Evaluate the normalised PDF values
128  pdf1_->calcLikelihoodInfo(abscissas);
129  pdf2_->calcLikelihoodInfo(abscissas);
130  Double_t result1 = pdf1_->getLikelihood();
131  Double_t result2 = pdf2_->getLikelihood();
132 
133  // Add them together
134  Double_t result = frac * result1 + (1.0-frac) * result2;
135  this->setUnNormPDFVal(result);
136 }
137 
139 {
140  // Nothing to do here, since it is already normalized
141  this->setNorm(1.0);
142 }
143 
144 void LauSumPdf::calcPDFHeight( const LauKinematics* kinematics )
145 {
146  if ( this->heightUpToDate() && ! this->isDPDependent() ) {
147  return;
148  }
149 
150  // This method gives you the maximum possible height of the PDF.
151  // It combines the maximum heights of the two individual PDFs.
152  // So it would give the true maximum if the two individual maxima coincided.
153  // It is guaranteed to always be >= the true maximum.
154 
155  // Update the heights of the individual PDFs
156  pdf1_->calcPDFHeight( kinematics );
157  pdf2_->calcPDFHeight( kinematics );
158 
159  // Get the up to date parameter values
160  Double_t frac = frac_->unblindValue();
161 
162  // Find the (un-normalised) individual PDF maxima
163  Double_t height1 = pdf1_->getMaxHeight();
164  Double_t height2 = pdf2_->getMaxHeight();
165 
166  // Get the individual PDF normalisation factors
167  Double_t norm1 = pdf1_->getNorm();
168  Double_t norm2 = pdf2_->getNorm();
169 
170  // Calculate the normalised individual PDF maxima
171  height1 /= norm1;
172  height2 /= norm2;
173 
174  // Combine these heights together
175  Double_t height = frac * height1 + (1-frac) * height2;
176  this->setMaxHeight(height);
177 }
178 
179 // Override the base class methods for cacheInfo and calcLikelihoodInfo(UInt_t iEvt).
180 // For both of these we delegate to the two constituent PDFs.
181 
182 void LauSumPdf::cacheInfo(const LauFitDataTree& inputData)
183 {
184  pdf1_->cacheInfo(inputData);
185  pdf2_->cacheInfo(inputData);
186 
187  // determine whether we are caching our PDF value
188  Bool_t doCaching ( this->nFixedParameters() == this->nParameters() );
189  this->cachePDF( doCaching );
190 
191  // Set whether the height is up-to-date only if it is true for both of the sub-PDFs
192  Bool_t hutd = pdf1_->heightUpToDate() && pdf2_->heightUpToDate();
193  this->heightUpToDate(hutd);
194 }
195 
197 {
198  // Get the up to date parameter values
199  Double_t frac = frac_->unblindValue();
200 
201  // Evaluate the normalised PDF values
202  pdf1_->calcLikelihoodInfo(iEvt);
203  pdf2_->calcLikelihoodInfo(iEvt);
204  Double_t result1 = pdf1_->getLikelihood();
205  Double_t result2 = pdf2_->getLikelihood();
206 
207  // Add them together
208  Double_t result = frac * result1 + (1.0-frac) * result2;
209  this->setUnNormPDFVal(result);
210 }
211 
File containing declaration of LauSumPdf class.
virtual void setUnNormPDFVal(Double_t unNormPDFVal)
Set the unnormalised likelihood.
Definition: LauAbsPdf.hh:383
virtual Bool_t heightUpToDate() const
Check if the maximum height of the PDF is up to date.
Definition: LauAbsPdf.hh:278
ClassImp(LauAbsCoeffSet)
virtual UInt_t nParameters() const
Retrieve the number of PDF parameters.
Definition: LauAbsPdf.hh:106
virtual ~LauSumPdf()
Destructor.
Definition: LauSumPdf.cc:112
virtual void calcPDFHeight(const LauKinematics *kinematics)=0
Calculate the maximum height of the PDF.
virtual void setNorm(Double_t norm)
Set the normalisation factor.
Definition: LauAbsPdf.hh:339
virtual Bool_t checkRange(const LauAbscissas &abscissas) const
Check that all abscissas are within their allowed ranges.
Definition: LauAbsPdf.cc:227
std::map< TString, Double_t > LauFitData
Type for holding event data.
LauAbsPdf * pdf1_
First PDF.
Definition: LauSumPdf.hh:109
virtual Double_t unblindValue() const =0
The unblinded value of the parameter.
virtual Double_t getMaxHeight() const
Retrieve the maximum height.
Definition: LauAbsPdf.hh:235
File containing declaration of LauParameter class.
virtual void calcNorm()
Calculate the normalisation.
Definition: LauSumPdf.cc:138
virtual void setMaxHeight(Double_t maxHeight)
Set the maximum height.
Definition: LauAbsPdf.hh:345
Class for defining the fit parameter objects.
Definition: LauParameter.hh:49
virtual const std::vector< LauAbsRValue * > & getParameters() const
Retrieve the parameters of the PDF, e.g. so that they can be loaded into a fit.
Definition: LauAbsPdf.hh:253
Class for defining a PDF that is the sum of two other PDFs.
Definition: LauSumPdf.hh:46
virtual Bool_t cachePDF() const
Check if the PDF is to be cached.
Definition: LauAbsPdf.hh:290
virtual void calcLikelihoodInfo(const LauAbscissas &abscissas)
Calculate the likelihood (and intermediate info) for a given abscissa.
Definition: LauSumPdf.cc:117
virtual Double_t getLikelihood() const
Retrieve the normalised likelihood value.
Definition: LauAbsPdf.cc:368
virtual void calcLikelihoodInfo(const LauAbscissas &abscissas)=0
Calculate the likelihood (and all associated information) given value(s) of the abscissa(s) ...
LauAbsPdf * pdf2_
Second PDF.
Definition: LauSumPdf.hh:112
File containing LauConstants namespace.
LauAbsRValue * frac_
Fractional contribution of first PDF to the final PDF.
Definition: LauSumPdf.hh:115
Class for defining the abstract interface for PDF classes.
Definition: LauAbsPdf.hh:55
virtual void calcPDFHeight(const LauKinematics *kinematics)
Calculate the PDF height.
Definition: LauSumPdf.cc:144
Class for calculating 3-body kinematic quantities.
virtual UInt_t nFixedParameters() const
Retrieve the number of fixed PDF parameters.
Definition: LauAbsPdf.cc:127
virtual void cacheInfo(const LauFitDataTree &inputData)
Cache information from data.
Definition: LauAbsPdf.cc:255
virtual Bool_t isDPDependent() const
Boolean for the DP dependence of PDFs in the sum.
Definition: LauSumPdf.hh:70
Pure abstract base class for defining a parameter containing an R value.
Definition: LauAbsRValue.hh:43
virtual Double_t getNorm() const
Retrieve the normalisation factor.
Definition: LauAbsPdf.hh:216
Class to store the input fit variables.
std::vector< Double_t > LauAbscissas
The type used for containing multiple abscissa values.
Definition: LauAbsPdf.hh:59
virtual void cacheInfo(const LauFitDataTree &inputData)
Cache information from data.
Definition: LauSumPdf.cc:182