43
cfit description and tutorial Jordi Garra Ticó

cfit - lphe.epfl.ch · 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

Embed Size (px)

Citation preview

Page 1: cfit - lphe.epfl.ch · 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

cfitdescription and tutorial

Jordi Garra Ticó

Page 2: cfit - lphe.epfl.ch · 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

2

git clone http://git.cern.ch/pub/tutcfit

Preliminary work

● Checkout the tutorial git repository

● Run the installation script

sh bin/installpkg.sh

Page 3: cfit - lphe.epfl.ch · 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

3

introduction

Page 4: cfit - lphe.epfl.ch · 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

4

Introduction

● In minuit, a fit is accomplished as the minimization of a function.

● Examples:● 2

Page 5: cfit - lphe.epfl.ch · 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

5

2 fit

● Example: linear regression:

DIY

Page 6: cfit - lphe.epfl.ch · 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

6

2 fit

Page 7: cfit - lphe.epfl.ch · 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

7

fit

● Example: Gaussian

DIY

Page 8: cfit - lphe.epfl.ch · 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

8

fit

It is fundamental that the pdf is normalized.

Page 9: cfit - lphe.epfl.ch · 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

9

Yield problem

● Try to fit the yield of this pdf:

DIY

Page 10: cfit - lphe.epfl.ch · 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

10

Yield problem

Well, we did not build the likelihood as a product of normalized pdfs.

Page 11: cfit - lphe.epfl.ch · 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

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

Page 12: cfit - lphe.epfl.ch · 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

12

Yield problem

● Again, try to fit the yield of this pdf:

DIY

Page 13: cfit - lphe.epfl.ch · 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

13

Yield problem

Page 14: cfit - lphe.epfl.ch · 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

14

compilation

Page 15: cfit - lphe.epfl.ch · 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

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

Page 16: cfit - lphe.epfl.ch · 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

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

Page 17: cfit - lphe.epfl.ch · 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

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

Page 18: cfit - lphe.epfl.ch · 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

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 pkg­config 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 root­config to figure out which libraries and headers must be specified.

DIY

Page 19: cfit - lphe.epfl.ch · 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

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.

Page 20: cfit - lphe.epfl.ch · 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

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.

Page 21: cfit - lphe.epfl.ch · 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

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.

Page 22: cfit - lphe.epfl.ch · 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

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

Page 23: cfit - lphe.epfl.ch · 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

23

cfit

Page 24: cfit - lphe.epfl.ch · 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

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)

Page 25: cfit - lphe.epfl.ch · 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

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).

Page 26: cfit - lphe.epfl.ch · 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

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

Page 27: cfit - lphe.epfl.ch · 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

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.● ......

Page 28: cfit - lphe.epfl.ch · 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

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

Page 29: cfit - lphe.epfl.ch · 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

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();

Page 30: cfit - lphe.epfl.ch · 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

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

Page 31: cfit - lphe.epfl.ch · 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

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

Page 32: cfit - lphe.epfl.ch · 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

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

Page 33: cfit - lphe.epfl.ch · 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

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:

Page 34: cfit - lphe.epfl.ch · 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

34

Dealing with amplitudes

Time-dependent amplitude (used in D mixing)

Amplitude for mixture state from B+ D K+

Defining

Page 35: cfit - lphe.epfl.ch · 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

35

Dealing with amplitudes

Coef

CoefExpr

DecayModel

Decay3Body

DecayMixing3Body

......

PdfExprPdfModel

Resonance Amplitude

RelBreitWigner

GounarisSakurai

gLASS

......

PhaseSpace

Page 36: cfit - lphe.epfl.ch · 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

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.

Page 37: cfit - lphe.epfl.ch · 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

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 ); // Blatt­Weisskopf 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

Page 38: cfit - lphe.epfl.ch · 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

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

Page 39: cfit - lphe.epfl.ch · 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

39

Simultaneous fits

● What is a simultaneous fit?● What exactly is simultaneous in a simultaneous fit?

DIY

Page 40: cfit - lphe.epfl.ch · 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

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

Page 41: cfit - lphe.epfl.ch · 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

41

Simultaneous fits● Recall the design of the minimizers.

FCNBase

Chi2

Nll

MinimizerExprMinimizer

MinimizerExpr minimizer = nll1 + nll2;minimizer += nll3;min = minimizer.minimize();

Page 42: cfit - lphe.epfl.ch · 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

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 ); }

Page 43: cfit - lphe.epfl.ch · 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

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