MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
fitters.cpp
Go to the documentation of this file.
1// pybind includes
2#include <pybind11/pybind11.h>
3#include <pybind11/stl.h>
4// MaCh3 includes
6#include "Fitters/mcmc.h"
7#include "Fitters/MinuitFit.h"
8#include "Fitters/PSO.h"
9
10namespace py = pybind11;
11
13class PyFitterBase : public FitterBase {
14public:
15 /* Inherit the constructors */
17
18 /* Trampoline (need one for each virtual function) */
19 void RunMCMC() override {
20 PYBIND11_OVERRIDE_PURE_NAME(
21 void, /* Return type */
22 FitterBase, /* Parent class */
23 "run", /* Python name*/
24 RunMCMC /* Name of function in C++ (must match Python name) */
25 );
26 }
27
28 std::string GetName() const override {
29 PYBIND11_OVERRIDE_PURE_NAME(
30 std::string, /* Return type */
31 FitterBase, /* Parent class */
32 "get_name", /* Python name*/
33 GetName /* Name of function in C++ (must match Python name) */
34 );
35 }
36};
37
40public:
41 /* Inherit the constructors */
43
44 /* Trampoline (need one for each virtual function) */
45 void RunMCMC() override {
46 PYBIND11_OVERRIDE_PURE_NAME(
47 void, /* Return type */
48 LikelihoodFit,/* Parent class */
49 "run", /* Python name*/
50 RunMCMC /* Name of function in C++ (must match Python name) */
51 );
52 }
53};
54
55void initFitters(py::module &m){
56
57 auto m_fitters = m.def_submodule("fitters");
58 m_fitters.doc() =
59 "This is a Python binding of MaCh3s C++ fitters library.";
60
61
62 py::class_<FitterBase, PyFitterBase /* <--- trampoline*/>(m_fitters, "FitterBase")
63 .def(py::init<manager* const>())
64
65 .def(
66 "run",
68 "The implementation of the fitter, you should override this with your own desired fitting algorithm"
69 )
70
71 .def(
72 "get_name",
74 " The name of the algorithm, you should override this with something like:: \n"
75 "\n"
76 " return 'mySuperCoolAlgoName' \n"
77 )
78
79 .def(
80 "run_LLH_scan",
82 "Perform a 1D likelihood scan"
83 )
84
85 .def(
86 "get_step_scale_from_LLH_scan",
88 "LLH scan is good first estimate of step scale, this will get the rough estimates for the step scales based on running an LLH scan"
89 )
90
91 .def(
92 "run_2d_LLH_scan",
94 " Perform a 2D likelihood scan. \n"
95 " :param warning: This operation may take a significant amount of time, especially for complex models."
96 )
97
98 .def(
99 "run_sigma_var",
101 " Perform a 2D and 1D sigma var for all samples. \n"
102 " :param warning: Code uses TH2Poly"
103 )
104
105 .def(
106 "drag_race",
108 " Calculates the required time for each sample or covariance object in a drag race simulation. Inspired by Dan's feature \n"
109 " :param NLaps: number of laps, every part of Fitter will be tested with given number of laps and you will get total and average time",
110 py::arg("NLaps") = 100
111 )
112
113 // stuff for registering other objects with the fitter
114
115 .def(
116 "add_sample_handler",
118 " This function adds a sample handler object to the analysis framework. The sample handler object will be utilized in fitting procedures or likelihood scans. \n"
119 " :param sample: A sample handler object derived from SampleHandlerBase. ",
120 py::arg("sample")
121 )
122
123 .def(
124 "add_syst_object",
126 " This function adds a Covariance object to the analysis framework. The Covariance object will be utilized in fitting procedures or likelihood scans. \n"
127 " :param cov: A Covariance object derived from ParameterHandlerBase. ",
128 py::arg("cov")
129 )
130
131 ; // End of FitterBase class binding
132
133 py::class_<mcmc, FitterBase>(m_fitters, "MCMC")
134 .def(py::init<manager* const>())
135
136 .def(
137 "set_chain_length",
139 "Set how long chain should be.",
140 py::arg("length")
141 )
142 ; // end of MCMC class binding
143
144 py::class_<LikelihoodFit, PyLikelihoodFit /* <--- trampoline*/, FitterBase>(m_fitters, "LikelihoodFit")
145 .def(py::init<manager* const>())
146
147 .def(
148 "caluclate_chi2",
149 [](LikelihoodFit &self, const std::vector<double> &parameterVals)
150 {
151 return self.CalcChi2(parameterVals.data());
152 },
153 "Get the Chi2 calculation over all included samples and syst objects for the specified parameter_values \n\
154 :param parameter_valuse: The location to evaluate the chi2 at.",
155 py::arg("parameter_values")
156 )
157
158 .def(
159 "get_n_params",
161 "Get The total number of parameters across all known covariance objects associated with this LikelihoodFit object."
162 )
163 ; // end of LikelihoodFit class binding
164
165 py::class_<MinuitFit, LikelihoodFit>(m_fitters, "MinuitFit")
166 .def(py::init<manager* const>())
167
168 ; // end of MinuitFit class binding
169
170 py::class_<PSO, LikelihoodFit>(m_fitters, "PSO")
171 .def(py::init<manager* const>())
172
173 .def(
174 "init",
175 &PSO::init,
176 "Initialise the fitter"
177 )
178
179 ; // end of PSO class binding
180
181}
Base class for implementing fitting algorithms.
Definition: FitterBase.h:23
void RunLLHScan()
Perform a 1D likelihood scan.
Definition: FitterBase.cpp:516
void AddSystObj(ParameterHandlerBase *cov)
This function adds a Covariance object to the analysis framework. The Covariance object will be utili...
Definition: FitterBase.cpp:267
void AddSampleHandler(SampleHandlerBase *sample)
This function adds a sample PDF object to the analysis framework. The sample PDF object will be utili...
Definition: FitterBase.cpp:246
virtual std::string GetName() const
Get name of class.
Definition: FitterBase.h:66
virtual void RunMCMC()=0
The specific fitting algorithm implemented in this function depends on the derived class....
void GetStepScaleBasedOnLLHScan()
LLH scan is good first estimate of step scale.
Definition: FitterBase.cpp:826
void RunSigmaVar()
Perform a 2D and 1D sigma var for all samples.
void DragRace(const int NLaps=100)
Calculates the required time for each sample or covariance object in a drag race simulation....
Definition: FitterBase.cpp:418
void Run2DLLHScan()
Perform a 2D likelihood scan.
Definition: FitterBase.cpp:875
FitterBase(manager *const fitMan)
Constructor.
Definition: FitterBase.cpp:15
Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation whil...
Definition: LikelihoodFit.h:6
int GetNPars()
Get total number of params, this sums over all covariance objects.
Definition: LikelihoodFit.h:16
LikelihoodFit(manager *const fitMan)
Constructor.
virtual double CalcChi2(const double *x)
Chi2 calculation over all included samples and syst objects.
void init()
Definition: PSO.cpp:50
EW: As FitterBase is an abstract base class we have to do some gymnastics to get it to get it into py...
Definition: fitters.cpp:13
void RunMCMC() override
The specific fitting algorithm implemented in this function depends on the derived class....
Definition: fitters.cpp:19
std::string GetName() const override
Get name of class.
Definition: fitters.cpp:28
EW: As LikelihoodFit is an abstract base class we have to do some gymnastics to get it to get it into...
Definition: fitters.cpp:39
void RunMCMC() override
Implementation of fitting algorithm.
Definition: fitters.cpp:45
void setChainLength(unsigned int L)
Set how long chain should be.
Definition: mcmc.h:19
void initFitters(py::module &m)
Definition: fitters.cpp:55