Main Page
Namespaces
Classes
Package Documentation
RecoTracker
DeDx
src
UnbinnedLikelihoodFit.cc
Go to the documentation of this file.
1
#include "
RecoTracker/DeDx/interface/UnbinnedLikelihoodFit.h
"
2
#include <TMath.h>
3
4
// a class to perform a likelihood fit
5
// Author: Christophe Delaere
6
7
/* Example of a Landau fit:
8
* ------------------------
9
* UnbinnedLikelihoodFit myfit;
10
* double x[4] = {89,110,70,80};
11
* myfit.setData(4,x);
12
* TF1* myfunction = new TF1("myLandau","TMath::Landau(x,[0],[1],1)",0,255);
13
* myfunction->SetParameters(100,10);
14
* myfit.setFunction(myfunction);
15
* myfit.fit();
16
* myfit.getFunction()->Print();
17
* double MPV = myfit.getFunction()->GetParameter(0);
18
* double MPVerror = myfit.getFunction()->GetParError(0);
19
*/
20
21
// the function passed to minuit
22
void
UnbinnedLL
(Int_t&, Double_t*, Double_t &
val
, Double_t *par, Int_t) {
23
// retrieve the data object (it's also the fitter)
24
// - sign to have a minimum
25
// factor 2 to have the right errors (see for example the pdg)
26
val = -2*((
dynamic_cast<
const
UnbinnedLikelihoodFit
*
>
((TVirtualFitter::GetFitter())->GetObjectFit()))->logL(par));
27
}
28
29
// the constructor
30
UnbinnedLikelihoodFit::UnbinnedLikelihoodFit
() {
31
nparameters_
= 0;
32
datasize_
= 0;
33
x_
=
NULL
;
34
min
=
NULL
;
35
tolerance_
= 0.01;
36
maxIterations_
= 1000;
37
}
38
39
// the destructor
40
UnbinnedLikelihoodFit::~UnbinnedLikelihoodFit
() {
41
}
42
43
// sets the data
44
// the class is not owner of the data... it only keeps a pointer to it.
45
void
UnbinnedLikelihoodFit::setData
(uint32_t
n
,
double
*
x
) {
46
datasize_
=
n
;
47
x_
=
x
;
48
}
49
50
// sets the function for the fit
51
void
UnbinnedLikelihoodFit::setFunction
(TF1*
f
) {
52
function_
=
f
;
53
nparameters_
=
function_
?
function_
->GetNpar() : 0;
54
}
55
56
// The fit itself
57
int32_t
UnbinnedLikelihoodFit::fit
(int32_t
verbosity
) {
58
// creates a fitter
59
min
=
TVirtualFitter::Fitter
(
this
,
nparameters_
);
60
min
->SetFCN(
UnbinnedLL
);
61
62
// set print level: no output
63
arglist_
[0] = 0;
64
min
->ExecuteCommand(
"SET NOWarnings"
,
arglist_
,1);
65
arglist_
[0] =
verbosity
;
66
min
->ExecuteCommand(
"SET PRINT"
,
arglist_
,1);
67
68
// initial values, error, range
69
double
parmin,parmax;
70
for
(uint32_t
i
=0;
i
<
nparameters_
;++
i
) {
71
function_
->GetParLimits(
i
, parmin, parmax);
72
min
->SetParameter(
i
,
73
function_
->GetParName(
i
),
74
function_
->GetParameter(
i
),
75
tolerance_
,
76
parmin, parmax);
77
}
78
79
// run MIGRAD
80
arglist_
[0] =
maxIterations_
;
// number of function calls
81
arglist_
[1] =
tolerance_
;
// tolerance
82
int32_t
status
=
min
->ExecuteCommand(
"MIGRAD"
,
arglist_
,2);
83
84
// get fit parameters and errors
85
for
(uint32_t
i
=0;
i
<
nparameters_
;++
i
) {
86
function_
->SetParameter(
i
,
min
->GetParameter(
i
));
87
function_
->SetParError(
i
,
min
->GetParError(
i
) );
88
}
89
90
// returns the status
91
return
status
;
92
}
93
94
// the log-likelihood function
95
double
UnbinnedLikelihoodFit::logL
(
const
double
*
parameters
)
const
{
96
double
val
=0;
97
if
(!
function_
)
return
val
;
98
for
(uint32_t
i
=0;
i
<
datasize_
;++
i
){
99
val += TMath::Log(
function_
->EvalPar(&(
x_
[
i
]),parameters));
100
}
101
return
val
;
102
}
103
mps_fire.i
i
Definition:
mps_fire.py:156
UnbinnedLL
void UnbinnedLL(Int_t &, Double_t *, Double_t &val, Double_t *par, Int_t)
Definition:
UnbinnedLikelihoodFit.cc:22
UnbinnedLikelihoodFit
Definition:
UnbinnedLikelihoodFit.h:24
metProducer_cfi.parameters
parameters
Definition:
metProducer_cfi.py:59
UnbinnedLikelihoodFit::setData
void setData(uint32_t n, double *x)
Definition:
UnbinnedLikelihoodFit.cc:45
UnbinnedLikelihoodFit::logL
double logL(const double *x) const
Definition:
UnbinnedLikelihoodFit.cc:95
UnbinnedLikelihoodFit::UnbinnedLL
friend void UnbinnedLL(Int_t &npar, Double_t *gin, Double_t &val, Double_t *par, Int_t iflag)
Definition:
UnbinnedLikelihoodFit.cc:22
UnbinnedLikelihoodFit::x_
double * x_
Definition:
UnbinnedLikelihoodFit.h:55
NULL
#define NULL
Definition:
scimark2.h:8
egammaCTFFinalFitWithMaterial_cff.Fitter
Fitter
Definition:
egammaCTFFinalFitWithMaterial_cff.py:22
HIPAlignmentAlgorithm_cfi.verbosity
verbosity
Definition:
HIPAlignmentAlgorithm_cfi.py:47
mps_update.status
status
Definition:
mps_update.py:57
UnbinnedLikelihoodFit::tolerance_
double tolerance_
Definition:
UnbinnedLikelihoodFit.h:62
f
double f[11][100]
Definition:
MuScleFitUtils.cc:78
UnbinnedLikelihoodFit::maxIterations_
uint32_t maxIterations_
Definition:
UnbinnedLikelihoodFit.h:61
UnbinnedLikelihoodFit.h
UnbinnedLikelihoodFit::UnbinnedLikelihoodFit
UnbinnedLikelihoodFit()
Definition:
UnbinnedLikelihoodFit.cc:30
gen::n
int n
Definition:
Cascade2Hadronizer.cc:79
UnbinnedLikelihoodFit::setFunction
void setFunction(TF1 *f)
Definition:
UnbinnedLikelihoodFit.cc:51
UnbinnedLikelihoodFit::fit
int32_t fit(int32_t verbosity=-1)
Definition:
UnbinnedLikelihoodFit.cc:57
UnbinnedLikelihoodFit::nparameters_
uint32_t nparameters_
Definition:
UnbinnedLikelihoodFit.h:58
UnbinnedLikelihoodFit::~UnbinnedLikelihoodFit
~UnbinnedLikelihoodFit()
Definition:
UnbinnedLikelihoodFit.cc:40
DDAxes::x
heppy_batch.val
val
Definition:
heppy_batch.py:349
UnbinnedLikelihoodFit::function_
TF1 * function_
Definition:
UnbinnedLikelihoodFit.h:57
UnbinnedLikelihoodFit::datasize_
uint32_t datasize_
Definition:
UnbinnedLikelihoodFit.h:54
UnbinnedLikelihoodFit::arglist_
double arglist_[10]
Definition:
UnbinnedLikelihoodFit.h:60
UnbinnedLikelihoodFit::min
TVirtualFitter * min
Definition:
UnbinnedLikelihoodFit.h:64
Generated for CMSSW Reference Manual by
1.8.11