Module G15 Controlled Flight Into Terrain CFIT 1G15- CFIT Version 08/2009.
cfit - lphe.epfl.ch · 18 compilation of c++ code EXERCISE The cfit tutorial repository contains a...
-
Upload
hoangkhanh -
Category
Documents
-
view
223 -
download
1
Transcript of cfit - lphe.epfl.ch · 18 compilation of c++ code EXERCISE The cfit tutorial repository contains a...
cfitdescription and tutorial
Jordi Garra Ticó
2
git clone http://git.cern.ch/pub/tutcfit
Preliminary work
● Checkout the tutorial git repository
● Run the installation script
sh bin/installpkg.sh
3
introduction
4
Introduction
● In minuit, a fit is accomplished as the minimization of a function.
● Examples:● 2
●
5
2 fit
● Example: linear regression:
DIY
6
2 fit
7
fit
● Example: Gaussian
DIY
8
fit
It is fundamental that the pdf is normalized.
9
Yield problem
● Try to fit the yield of this pdf:
DIY
10
Yield problem
Well, we did not build the likelihood as a product of normalized pdfs.
11
Yield problem● To solve this problem, the likelihood must
contain the pdf of the number of events, which is assumed to be Poisson distributed.
● Defining Expression of pdfspdf
12
Yield problem
● Again, try to fit the yield of this pdf:
DIY
13
Yield problem
14
compilation
15
compilation of c++ code
● c++ code contains:● header files (.hh,.h,.hpp)
– They declare functions and objects.
● source files (.cc,.cpp)– They implement the functions and the objects behavior.
● A program may be implemented in one or more source files.
● A typical standalone compilation command is
g++ std=c++0x o program main.cc
16
compilation of c++ code
● Compilation has 2 steps:● Compilation
– Produces object files (.o) from source files.
– It requires all the headers each source file depends on.● Including external packages.
– Header paths are specified after the I argument.
– Compiler is invoked with c option.
g++ std=c++0x c o main.o main.cc I include
17
compilation of c++ code
● Compilation has 2 steps:● Linking
– Produces executables from object files.– It requires all the libraries each source file depends on.– Libraries (.so, .a) are also the result of a linking step, and
they do not require other libraries.– Library paths are specified with L, and each library that
the executable depends on is specified with l.
g++ std=c++0x o prog main.o L path lcfit
18
compilation of c++ code
● EXERCISE● The cfit tutorial repository contains a compile directory.● The sha.cc file uses the openssl package.
● You can use the pkgconfig command to figure out which libraries and headers must be specified.
● The hist.cc file uses root's TH1D and TCanvas.
● You can use the rootconfig to figure out which libraries and headers must be specified.
DIY
19
compilation of c++ code
● It is not necessary to specify header or library paths in certain conditions. The compiler automatically looks for● headers in:
– /usr/include, /usr/local/include, and others.
– All paths in CPLUS_INCLUDE_PATH.
● libraries in:– /usr/lib, /usr/lib64, /lib, and others.
– All paths in LIBRARY_PATH.
● This saves us from typing I or L paths, but not l library lists.
20
compilation of c++ code
● The cfit tutorial repository contains a script that sets up the CPLUS_INCLUDE_PATH, LIBRARY_PATH and LD_LIBRARY_PATH.
source bin/libs
● Very useful in our case:● Use libraries that are not installed in the
system.
21
compilation of c++ code
● Sourcing the bin/libs script does several things:● Reads the cfg/versions file to figure out which
package versions to use.– Very useful to keep track of all the versions used, just by
having the cfg/versions file under version control (git).
● Runs LbLogin if needed.
● Runs SetupProject for the required project (libs sets up the root project).
● Sets up the CPLUS_INCLUDE_PATH, LIBRARY_PATH and LD_LIBRARY_PATH variables.
22
compilation of c++ code
● If you install the proposed libraries in the default directory, you can do either of these 2 options:● Option A:
– Add the following line to your ~/.bashrc
– Run source bin/libs every time you want to work on a project that uses these packages.
● Option B:– Add to your ~/.bashrc the lines advised after the
execution of the bin/installpkg.sh script.
– Manually keep track of the package versions you use.
export pkgdir=${HOME}/pkg DIY
23
cfit
24
Description
● A simple and easy-to-use object-oriented c++ library to fit complex pdfs to datasets.
● Interface to Minuit. It produces a Minuit object of class FCNBase.
● Code clarity is a priority.● Implements MPI parallelization of the evaluation
of the minimizer (2 or NLL)
25
What cfit is not
● A graphics library.● A configuration file reader.● An interface to root objects.● However:
● There are tools available to do these tasks (atools).
26
Library classes
PdfModel
Gauss
Argus
CrystalBall
......
PdfExpr
Variable Parameter
FCNBase
Chi2
Nll
MinimizerExpr
Dataset
ParameterExpr
Fit recipe:● Fill a dataset● Define the variables (dataset columns to be used)● Define the parameters● Define the pdf (either as a model or expression)● Feed the dataset and pdf to the minimizer● Minimize
Minimizer
27
Library classes
● PdfModel represents normalized pdf models.
● PdfExpr represents an expression of models, and may not be normalized.
● Inconsistent expressions are forbidden.● Multiplying 2 pdfs that depend on the same
variables.● Adding or subtracting 2 pdfs that don't depend on
the same variables.● Dividing 2 pdfs.● ......
28
Dataset data;
std::ifstream input( “data.dat” );while ( input >> value1 >> value2 ){ data.push( “x”, value1 ); // Optional error data.push( “y”, value2 );}input.close();
Variable x( “x” );Variable y( “y” );
Parameter area ( “area” , 2.0e5, 300.0 );Parameter mu ( “mu” , 1.0 , 0.1 );Parameter sigma( “sigma”, 2.0 , 0.1 );
Gauss gaussian( x, mu, sigma );
Chi2 chi2( area * gaussian, y, data );
const FunctionMinimum& min = chi2.minimize();
Fill a dataset
Define the variables
Define the parameters
Define the pdf
Feed the pdf and dataset to minimizer
Minimize
29
Variable x( “x” );Variable y( “y” );
Parameter area ( “area” , 2.0e5, 300.0 );Parameter mu ( “mu” , 1.0 , 0.003 );Parameter sigma ( “sigma” , 2.0 , 0.003 );
Gauss g ( x, mu , sigma );
Chi2 chi2( area * g, y, data );
const FunctionMinimum& min = chi2.minimize();
30
Variable x( “x” );Variable y( “y” );
Parameter area1 ( "area1" , 1.0e5, 300.0 );Parameter area2 ( "area2" , 1.0e5, 300.0 );Parameter mu1 ( "mu1" , 1.0 , 0.003 );Parameter mu2 ( "mu2" , 2.0 , 0.003 );Parameter sigma1( "sigma1", 2.0 , 0.003 );Parameter sigma2( "sigma2", 1.0 , 0.003 );
Gauss g1( x, mu1, sigma1 );Gauss g2( x, mu2, sigma2 );
PdfExpr total = area1 * g1 + area2 * g2;
Chi2 chi2( total, y, data );
const FunctionMinimum& min = chi2.minimize();
Expressions of pdfs simplify the clarity of the code
31
Variable x( “x” );Variable y( “y” );
Parameter area ( "area" , 2.0e5, 300.0 );Parameter phi ( "phi" , 0.5 , 0.1 );Parameter mu1 ( "mu1" , 1.0 , 0.003 );Parameter mu2 ( "mu2" , 2.0 , 0.003 );Parameter sigma1( "sigma1", 2.0 , 0.003 );Parameter sigma2( "sigma2", 1.0 , 0.003 );
Gauss g1( x, mu1, sigma1 );Gauss g2( x, mu2, sigma2 );
PdfExpr total = area * ( pow( sin( phi ), 2 ) * g1 + pow( cos( phi ), 2 ) * g2 );
Chi2 chi2( total, y, data );
const FunctionMinimum& min = chi2.minimize();
Just writenormally
32
Simple example
● The cfit tutorial repository contains a gauss directory● It generates 10000 normal events and fits a
Gaussian to them● What would be the command to compile it?● Run the source bin/libs command and
examine the variables CPLUS_INCLUDE_PATH, LIBRARY_PATH and LD_LIBRARY_PATH.
● Adapt it to use a sum of 2 Gaussians as pdf, where one of the 2 Gaussians has twice as much yield as the other. DIY
DIY
DIY
33
Working with amplitudes
● is the phase of the Vub
element of the CKM matrix.
● In B DK decays, the D meson is not a flavor eigenstate, but a combination of both.
Important LHCb example:
34
Dealing with amplitudes
Time-dependent amplitude (used in D mixing)
Amplitude for mixture state from B+ D K+
Defining
35
Dealing with amplitudes
Coef
CoefExpr
DecayModel
Decay3Body
DecayMixing3Body
......
PdfExprPdfModel
Resonance Amplitude
RelBreitWigner
GounarisSakurai
gLASS
......
PhaseSpace
36
Dealing with amplitudes
● The same amplitude can be useful for several decay models.● Amplitude in Decay3Body produces a squared
amplitude (useful for analysis).
● Amplitude in DecayMixing3Body produces a lifetime dependent pdf (shown a few slides above).
● These decay models are pdf models and, therefore, are normalized.
● The limits of the decay model pdf are determined by PhaseSpace objects.
37
Parameter mKst( "mKst", 0.8937010, 0.1 ); // MassesParameter mRho( "mRho", 0.7758000, 0.1 ); //Parameter wKst( "wKst", 0.0467401, 0.1 ); // WidthsParameter wRho( "wRho", 0.1464000, 0.1 ); //Parameter rBW ( "rBW" , 1.5 , 0.5 ); // BlattWeisskopf radius.
Parameter reCoefKstm( "reCoefKstm", 1.196090, 0.005755 );Parameter reCoefRho ( "reCoefRho" , 1.0 , 0.1 );Parameter imCoefKstm( "imCoefKstm", 1.256890, 0.006278 );Parameter imCoefRho ( "imCoefRho" , 0.0 , 0.1 );
Coef coefKstm( reCoefKstm, imCoefKstm );Coef coefRho ( reCoefRho , imCoefRho );
RelBreitWigner propKstm( 1, 3, mKst, wKst, rBW, 1 );GounarisSakurai propRho ( 2, 3, mRho, wRho, rBW, 1 );
Amplitude amp;
amp += coefKstm * propKstm;amp += coefRho * propRho;
PhaseSpace ps( mD0, mKs, mPi, mPi );
Decay3Body decayModel( mSq12, mSq13, mSq23, amp, ps );
Resonance parameters
Resonance coefficients
Propagators
Amplitude(propagator expression)
Resonant pair Angular momentum
Pdf model
Pdf limits
38
Dealing with amplitudes● In the tutorial, you will be able to fit the resonance
coefficients and produce these plots.
● Compile and run the project in the model directory.
DIY
39
Simultaneous fits
● What is a simultaneous fit?● What exactly is simultaneous in a simultaneous fit?
DIY
40
Simultaneous fits
● What is a simultaneous fit?● What exactly is simultaneous in a simultaneous fit?
● Consider:● Several different data sets (e.g. different decays).● Several pdfs to describe these datasets, but with
some common parameters.
● What we need to minimize is the sum of all the minimizers involved (either 2 or ).
DIY
41
Simultaneous fits● Recall the design of the minimizers.
FCNBase
Chi2
Nll
MinimizerExprMinimizer
MinimizerExpr minimizer = nll1 + nll2;minimizer += nll3;min = minimizer.minimize();
42
Model design
● Constructor must push(...) all variables and parameters.● It makes cfit aware of them.
● Mandatory definition of
● For pdfs of 1 variable, good practice to define
const double evaluate( const std::vector< double >& pars ) const throw( PdfException )
const double evaluate( const double& pars ) const throw( PdfException )
NewModel* copy() const { return new NewModel( *this ); }
43
Model design
● Voluntary functionsconst double evaluate( const std::vector< double >& pars , const std::vector< double >& cacheR, const std::vector< std::complex< double > >& cacheC ) const throw( PdfException )
void cache()
const std::map< std::string, double > generate() const throw( PdfException )
const double area( const double& min, const double& max ) const throw( PdfException )
● The cfit tutorial repository contains a newmodel directory that implements a simple new model.● Check the implementation and try to design a new
model for a pdf of your choice. DIY