MaCh3  2.2.3
Reference Guide
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
5 #include "Fitters/FitterBase.h"
6 #include "Fitters/MR2T2.h"
7 #include "Fitters/DelayedMR2T2.h"
8 #include "Fitters/MinuitFit.h"
9 #include "Fitters/PSO.h"
10 
11 namespace py = pybind11;
12 
14 class PyFitterBase : public FitterBase {
15 public:
16  /* Inherit the constructors */
18 
19  /* Trampoline (need one for each virtual function) */
20  void RunMCMC() override {
21  PYBIND11_OVERRIDE_PURE_NAME(
22  void, /* Return type */
23  FitterBase, /* Parent class */
24  "run", /* Python name*/
25  RunMCMC /* Name of function in C++ (must match Python name) */
26  );
27  }
28 };
29 
32 public:
33  /* Inherit the constructors */
35 
36  /* Trampoline (need one for each virtual function) */
37  void RunMCMC() override {
38  PYBIND11_OVERRIDE_PURE_NAME(
39  void, /* Return type */
40  LikelihoodFit,/* Parent class */
41  "run", /* Python name*/
42  RunMCMC /* Name of function in C++ (must match Python name) */
43  );
44  }
45 };
46 
47 void initFitters(py::module &m){
48 
49  auto m_fitters = m.def_submodule("fitters");
50  m_fitters.doc() =
51  "This is a Python binding of MaCh3s C++ fitters library.";
52 
53 
54  py::class_<FitterBase, PyFitterBase /* <--- trampoline*/>(m_fitters, "FitterBase")
55  .def(py::init<manager* const>())
56 
57  .def(
58  "run",
60  "The implementation of the fitter, you should override this with your own desired fitting algorithm"
61  )
62 
63  .def(
64  "get_name",
66  " The name of the algorithm, you should override this with something like:: \n"
67  "\n"
68  " return 'mySuperCoolAlgoName' \n"
69  )
70 
71  .def(
72  "run_LLH_scan",
74  "Perform a 1D likelihood scan"
75  )
76 
77  .def(
78  "get_step_scale_from_LLH_scan",
80  "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"
81  )
82 
83  .def(
84  "run_2d_LLH_scan",
86  " Perform a 2D likelihood scan. \n"
87  " :param warning: This operation may take a significant amount of time, especially for complex models."
88  )
89 
90  .def(
91  "run_sigma_var",
93  " Perform a 2D and 1D sigma var for all samples. \n"
94  " :param warning: Code uses TH2Poly"
95  )
96 
97  .def(
98  "drag_race",
100  " Calculates the required time for each sample or covariance object in a drag race simulation. Inspired by Dan's feature \n"
101  " :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",
102  py::arg("NLaps") = 100
103  )
104 
105  // stuff for registering other objects with the fitter
106 
107  .def(
108  "add_sample_handler",
110  " 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"
111  " :param sample: A sample handler object derived from SampleHandlerBase. ",
112  py::arg("sample")
113  )
114 
115  .def(
116  "add_syst_object",
118  " This function adds a Covariance object to the analysis framework. The Covariance object will be utilized in fitting procedures or likelihood scans. \n"
119  " :param cov: A Covariance object derived from ParameterHandlerBase. ",
120  py::arg("cov")
121  )
122 
123  ; // End of FitterBase class binding
124 
125  py::class_<MR2T2, FitterBase>(m_fitters, "mcmc")
126  .def(py::init<manager *const>())
127 
128  .def(
129  "set_chain_length",
131  "Set how long chain should be.",
132  py::arg("length")); // end of MCMC class binding
133 
134  py::class_<DelayedMR2T2, FitterBase>(m_fitters, "DelayedMCMC")
135  .def(py::init<manager *const>())
136 
137  .def(
138  "set_chain_length",
140  "Set how long chain should be.",
141  py::arg("length")); // end of MCMC class binding
142 
143  py::class_<LikelihoodFit, PyLikelihoodFit /* <--- trampoline*/, FitterBase>(m_fitters, "LikelihoodFit")
144  .def(py::init<manager* const>())
145 
146  .def(
147  "caluclate_chi2",
148  [](LikelihoodFit &self, const std::vector<double> &parameterVals)
149  {
150  return self.CalcChi2(parameterVals.data());
151  },
152  "Get the Chi2 calculation over all included samples and syst objects for the specified parameter_values \n\
153  :param parameter_valuse: The location to evaluate the chi2 at.",
154  py::arg("parameter_values")
155  )
156 
157  .def(
158  "get_n_params",
160  "Get The total number of parameters across all known covariance objects associated with this LikelihoodFit object."
161  )
162  ; // end of LikelihoodFit class binding
163 
164  py::class_<MinuitFit, LikelihoodFit>(m_fitters, "MinuitFit")
165  .def(py::init<manager* const>())
166 
167  ; // end of MinuitFit class binding
168 
169  py::class_<PSO, LikelihoodFit>(m_fitters, "PSO")
170  .def(py::init<manager* const>())
171 
172  .def(
173  "init",
174  &PSO::init,
175  "Initialise the fitter"
176  )
177 
178  ; // end of PSO class binding
179 
180 }
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:276
std::string GetName() const
Get name of class.
Definition: FitterBase.h:70
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:255
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:16
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.
void setChainLength(unsigned int L)
Set how long chain should be.
Definition: MCMCBase.h:27
void init()
Definition: PSO.cpp:51
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:14
void RunMCMC() override
The specific fitting algorithm implemented in this function depends on the derived class....
Definition: fitters.cpp:20
EW: As LikelihoodFit is an abstract base class we have to do some gymnastics to get it to get it into...
Definition: fitters.cpp:31
void RunMCMC() override
The specific fitting algorithm implemented in this function depends on the derived class....
Definition: fitters.cpp:37
void initFitters(py::module &m)
Definition: fitters.cpp:47