laura is hosted by Hepforge, IPPP Durham
Laura++  3.6.0
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 "LauSumPdf.hh"
30 
31 #include "LauConstants.hh"
32 #include "LauParameter.hh"
33 
34 #include "TMath.h"
35 #include "TSystem.h"
36 
37 #include <iostream>
38 #include <vector>
39 
41  LauAbsPdf( pdf1 ? pdf1->varNames() : std::vector<TString>(),
42  std::vector<LauAbsRValue*>(),
43  pdf1 ? pdf1->getMinAbscissas() : LauFitData(),
44  pdf1 ? pdf1->getMaxAbscissas() : LauFitData() ),
45  pdf1_( pdf1 ),
46  pdf2_( pdf2 ),
47  frac_( frac )
48 {
49  // Constructor for the sum PDF.
50  // We are defining the sum as:
51  // f x (PDF1/S(PDF1)) + (1-f) x (PDF2/S(PDF2))
52  // where f is the fraction, x is multiplication, PDFi is the i'th PDF,
53  // and S(PDFi) is the integral of the i'th PDF.
54  //
55  // The last two arguments specify the range in which the PDF is defined, and the PDF
56  // will be normalised w.r.t. these limits.
57 
58  // So the first thing we have to do is check the pointers are all valid.
59  if ( ! frac || ! pdf1 || ! pdf2 ) {
60  std::cerr << "ERROR in LauSumPdf constructor : one of the 3 pointers is null." << std::endl;
61  gSystem->Exit( EXIT_FAILURE );
62  }
63 
64  // Next check that the abscissa ranges are the same for each PDF
65  if ( pdf1->getMinAbscissa() != pdf2->getMinAbscissa() ) {
66  std::cerr << "ERROR in LauSumPdf constructor : minimum abscissa values not the same for the two PDFs."
67  << std::endl;
68  gSystem->Exit( EXIT_FAILURE );
69  }
70  if ( pdf1->getMaxAbscissa() != pdf2->getMaxAbscissa() ) {
71  std::cerr << "ERROR in LauSumPdf constructor : maximum abscissa values not the same for the two PDFs."
72  << std::endl;
73  gSystem->Exit( EXIT_FAILURE );
74  }
75 
76  // Also check that both PDFs are expecting the same number of input variables
77  if ( pdf1->nInputVars() != pdf2->nInputVars() ) {
78  std::cerr << "ERROR in LauSumPdf constructor : number of input variables not the same for the two PDFs."
79  << std::endl;
80  gSystem->Exit( EXIT_FAILURE );
81  }
82 
83  // Also check that both PDFs are expecting the same variable name
84  if ( pdf1->varNames() != pdf2->varNames() ) {
85  std::cerr << "ERROR in LauSumPdf constructor : variable name not the same for the two PDFs."
86  << std::endl;
87  gSystem->Exit( EXIT_FAILURE );
88  }
89 
90  // Then we need to grab all the parameters and pass them to the base class.
91  // This is so that when we are asked for them they can be put into the fit.
92  // The number of parameters is the number in PDF1 + the number in PDF2 + 1 for the fraction.
93  UInt_t nPar( pdf1->nParameters() + pdf2->nParameters() + 1 );
94  std::vector<LauAbsRValue*> params;
95  params.reserve( nPar );
96  std::vector<LauAbsRValue*>& pdf1pars = pdf1->getParameters();
97  std::vector<LauAbsRValue*>& pdf2pars = pdf2->getParameters();
98  params.push_back( frac );
99  for ( std::vector<LauAbsRValue*>::iterator iter = pdf1pars.begin(); iter != pdf1pars.end();
100  ++iter ) {
101  params.push_back( *iter );
102  }
103  for ( std::vector<LauAbsRValue*>::iterator iter = pdf2pars.begin(); iter != pdf2pars.end();
104  ++iter ) {
105  params.push_back( *iter );
106  }
107  this->addParameters( params );
108 
109  // Now check that we can find the fraction parameter ok
110  frac_ = this->findParameter( "frac" );
111  if ( frac_ == 0 ) {
112  std::cerr << "ERROR in LauSumPdf constructor : parameter \"frac\" not found." << std::endl;
113  gSystem->Exit( EXIT_FAILURE );
114  }
115 
116  // Cache the normalisation factor
117  this->calcNorm();
118 }
119 
121 {
122  // Destructor
123 }
124 
126 {
127  // Check that the given abscissa is within the allowed range
128  if ( ! this->checkRange( abscissas ) ) {
129  gSystem->Exit( EXIT_FAILURE );
130  }
131 
132  // Get the up to date parameter values
133  Double_t frac = frac_->unblindValue();
134 
135  // Evaluate the normalised PDF values
136  pdf1_->calcLikelihoodInfo( abscissas );
137  pdf2_->calcLikelihoodInfo( abscissas );
138  Double_t result1 = pdf1_->getLikelihood();
139  Double_t result2 = pdf2_->getLikelihood();
140 
141  // Add them together
142  Double_t result = frac * result1 + ( 1.0 - frac ) * result2;
143  this->setUnNormPDFVal( result );
144 }
145 
147 {
148  // Nothing to do here, since it is already normalized
149  this->setNorm( 1.0 );
150 }
151 
152 void LauSumPdf::calcPDFHeight( const LauKinematics* kinematics )
153 {
154  if ( this->heightUpToDate() && ! this->isDPDependent() ) {
155  return;
156  }
157 
158  // This method gives you the maximum possible height of the PDF.
159  // It combines the maximum heights of the two individual PDFs.
160  // So it would give the true maximum if the two individual maxima coincided.
161  // It is guaranteed to always be >= the true maximum.
162 
163  // Update the heights of the individual PDFs
164  pdf1_->calcPDFHeight( kinematics );
165  pdf2_->calcPDFHeight( kinematics );
166 
167  // Get the up to date parameter values
168  Double_t frac = frac_->unblindValue();
169 
170  // Find the (un-normalised) individual PDF maxima
171  Double_t height1 = pdf1_->getMaxHeight();
172  Double_t height2 = pdf2_->getMaxHeight();
173 
174  // Get the individual PDF normalisation factors
175  Double_t norm1 = pdf1_->getNorm();
176  Double_t norm2 = pdf2_->getNorm();
177 
178  // Calculate the normalised individual PDF maxima
179  height1 /= norm1;
180  height2 /= norm2;
181 
182  // Combine these heights together
183  Double_t height = frac * height1 + ( 1 - frac ) * height2;
184  this->setMaxHeight( height );
185 }
186 
187 // Override the base class methods for cacheInfo and calcLikelihoodInfo(UInt_t iEvt).
188 // For both of these we delegate to the two constituent PDFs.
189 
190 void LauSumPdf::cacheInfo( const LauFitDataTree& inputData )
191 {
192  pdf1_->cacheInfo( inputData );
193  pdf2_->cacheInfo( inputData );
194 
195  // determine whether we are caching our PDF value
196  Bool_t doCaching( this->nFixedParameters() == this->nParameters() );
197  this->cachePDF( doCaching );
198 
199  // Set whether the height is up-to-date only if it is true for both of the sub-PDFs
200  Bool_t hutd = pdf1_->heightUpToDate() && pdf2_->heightUpToDate();
201  this->heightUpToDate( hutd );
202 }
203 
204 void LauSumPdf::calcLikelihoodInfo( UInt_t iEvt )
205 {
206  // Get the up to date parameter values
207  Double_t frac = frac_->unblindValue();
208 
209  // Evaluate the normalised PDF values
210  pdf1_->calcLikelihoodInfo( iEvt );
211  pdf2_->calcLikelihoodInfo( iEvt );
212  Double_t result1 = pdf1_->getLikelihood();
213  Double_t result2 = pdf2_->getLikelihood();
214 
215  // Add them together
216  Double_t result = frac * result1 + ( 1.0 - frac ) * result2;
217  this->setUnNormPDFVal( result );
218 }
virtual UInt_t nFixedParameters() const
Retrieve the number of fixed PDF parameters.
Definition: LauAbsPdf.cc:133
Class for defining the fit parameter objects.
Definition: LauParameter.hh:49
virtual Double_t getMaxAbscissa() const
Retrieve the maximum value of the (primary) abscissa.
Definition: LauAbsPdf.hh:141
virtual void calcNorm()
Calculate the normalisation.
Definition: LauSumPdf.cc:146
virtual Double_t getMaxHeight() const
Retrieve the maximum height.
Definition: LauAbsPdf.hh:244
LauSumPdf(LauAbsPdf *pdf1, LauAbsPdf *pdf2, LauParameter *frac1)
Constructor.
Definition: LauSumPdf.cc:40
virtual Double_t getMinAbscissa() const
Retrieve the minimum value of the (primary) abscissa.
Definition: LauAbsPdf.hh:135
virtual void calcPDFHeight(const LauKinematics *kinematics)
Calculate the PDF height.
Definition: LauSumPdf.cc:152
virtual Bool_t cachePDF() const
Check if the PDF is to be cached.
Definition: LauAbsPdf.hh:299
std::vector< Double_t > LauAbscissas
The type used for containing multiple abscissa values.
Definition: LauAbsPdf.hh:58
File containing declaration of LauParameter class.
virtual ~LauSumPdf()
Destructor.
Definition: LauSumPdf.cc:120
virtual void calcPDFHeight(const LauKinematics *kinematics)=0
Calculate the maximum height of the PDF.
virtual LauAbsRValue * findParameter(const TString &parName)
Retrieve the specified parameter.
Definition: LauAbsPdf.cc:431
virtual void cacheInfo(const LauFitDataTree &inputData)
Cache information from data.
Definition: LauAbsPdf.cc:275
Class to store the input fit variables.
virtual Bool_t isDPDependent() const
Boolean for the DP dependence of PDFs in the sum.
Definition: LauSumPdf.hh:69
virtual UInt_t nParameters() const
Retrieve the number of PDF parameters.
Definition: LauAbsPdf.hh:109
virtual void setNorm(Double_t norm)
Set the normalisation factor.
Definition: LauAbsPdf.hh:348
virtual Double_t unblindValue() const =0
The unblinded value of the parameter.
virtual std::vector< TString > varNames() const
Retrieve the names of the abscissas.
Definition: LauAbsPdf.cc:122
virtual void cacheInfo(const LauFitDataTree &inputData)
Cache information from data.
Definition: LauSumPdf.cc:190
virtual UInt_t nInputVars() const
Retrieve the number of abscissas.
Definition: LauAbsPdf.hh:121
virtual void setUnNormPDFVal(Double_t unNormPDFVal)
Set the unnormalised likelihood.
Definition: LauAbsPdf.hh:394
File containing declaration of LauSumPdf class.
virtual void calcLikelihoodInfo(const LauAbscissas &abscissas)=0
Calculate the likelihood (and all associated information) given value(s) of the abscissa(s)
Class for defining the abstract interface for PDF classes.
Definition: LauAbsPdf.hh:54
LauAbsPdf * pdf1_
First PDF.
Definition: LauSumPdf.hh:111
virtual Double_t getLikelihood() const
Retrieve the normalised likelihood value.
Definition: LauAbsPdf.cc:403
virtual void calcLikelihoodInfo(const LauAbscissas &abscissas)
Calculate the likelihood (and intermediate info) for a given abscissa.
Definition: LauSumPdf.cc:125
Pure abstract base class for defining a parameter containing an R value.
Definition: LauAbsRValue.hh:45
File containing LauConstants namespace.
std::map< TString, Double_t > LauFitData
Type for holding event data.
virtual void setMaxHeight(Double_t maxHeight)
Set the maximum height.
Definition: LauAbsPdf.hh:354
virtual Double_t getNorm() const
Retrieve the normalisation factor.
Definition: LauAbsPdf.hh:224
virtual void addParameters(std::vector< LauAbsRValue * > &params)
Add parameters to the PDF.
Definition: LauAbsPdf.cc:478
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:262
Class for calculating 3-body kinematic quantities.
LauAbsPdf * pdf2_
Second PDF.
Definition: LauSumPdf.hh:114
LauAbsRValue * frac_
Fractional contribution of first PDF to the final PDF.
Definition: LauSumPdf.hh:117
virtual Bool_t heightUpToDate() const
Check if the maximum height of the PDF is up to date.
Definition: LauAbsPdf.hh:287
virtual Bool_t checkRange(const LauAbscissas &abscissas) const
Check that all abscissas are within their allowed ranges.
Definition: LauAbsPdf.cc:243