MaCh3  2.4.2
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  auto m_fitters = m.def_submodule("fitters");
49  m_fitters.doc() =
50  "This is a Python binding of MaCh3s C++ fitters library.";
51 
52 
53  py::class_<FitterBase, PyFitterBase /* <--- trampoline*/>(m_fitters, "FitterBase")
54  .def(py::init<Manager* const>())
55 
56  .def(
57  "run",
59  "The implementation of the fitter, you should override this with your own desired fitting algorithm"
60  )
61 
62  .def(
63  "get_name",
65  " The name of the algorithm, you should override this with something like:: \n"
66  "\n"
67  " return 'mySuperCoolAlgoName' \n"
68  )
69 
70  .def(
71  "run_LLH_scan",
73  "Perform a 1D likelihood scan"
74  )
75 
76  .def(
77  "get_step_scale_from_LLH_scan",
79  "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"
80  )
81 
82  .def(
83  "run_2d_LLH_scan",
85  " Perform a 2D likelihood scan. \n"
86  " :param warning: This operation may take a significant amount of time, especially for complex models."
87  )
88 
89  .def(
90  "run_sigma_var",
92  " Perform a 2D and 1D sigma var for all samples. \n"
93  " :param warning: Code uses TH2Poly"
94  )
95 
96  .def(
97  "drag_race",
99  " Calculates the required time for each sample or covariance object in a drag race simulation. Inspired by Dan's feature \n"
100  " :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",
101  py::arg("NLaps") = 100
102  )
103 
104  // stuff for registering other objects with the fitter
105 
106  .def(
107  "add_sample_handler",
109  " 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"
110  " :param sample: A sample handler object derived from SampleHandlerBase. ",
111  py::arg("sample")
112  )
113 
114  .def(
115  "add_syst_object",
117  " This function adds a Covariance object to the analysis framework. The Covariance object will be utilized in fitting procedures or likelihood scans. \n"
118  " :param cov: A Covariance object derived from ParameterHandlerBase. ",
119  py::arg("cov")
120  )
121 
122  ; // End of FitterBase class binding
123 
124  py::class_<MR2T2, FitterBase>(m_fitters, "mcmc")
125  .def(py::init<Manager *const>())
126 
127  .def(
128  "set_chain_length",
130  "Set how long chain should be.",
131  py::arg("length")); // end of MCMC class binding
132 
133  py::class_<DelayedMR2T2, FitterBase>(m_fitters, "DelayedMCMC")
134  .def(py::init<Manager *const>())
135 
136  .def(
137  "set_chain_length",
139  "Set how long chain should be.",
140  py::arg("length")); // end of MCMC class binding
141 
142  py::class_<LikelihoodFit, PyLikelihoodFit /* <--- trampoline*/, FitterBase>(m_fitters, "LikelihoodFit")
143  .def(py::init<Manager* const>())
144 
145  .def(
146  "caluclate_chi2",
147  [](LikelihoodFit &self, const std::vector<double> &parameterVals)
148  {
149  return self.CalcChi2(parameterVals.data());
150  },
151  "Get the Chi2 calculation over all included samples and syst objects for the specified parameter_values \n\
152  :param parameter_valuse: The location to evaluate the chi2 at.",
153  py::arg("parameter_values")
154  )
155 
156  .def(
157  "get_n_params",
159  "Get The total number of parameters across all known covariance objects associated with this LikelihoodFit object."
160  )
161  ; // end of LikelihoodFit class binding
162 
163  py::class_<MinuitFit, LikelihoodFit>(m_fitters, "MinuitFit")
164  .def(py::init<Manager* const>())
165 
166  ; // end of MinuitFit class binding
167 
168  py::class_<PSO, LikelihoodFit>(m_fitters, "PSO")
169  .def(py::init<Manager* const>())
170 
171  .def(
172  "init",
173  &PSO::init,
174  "Initialise the fitter"
175  )
176 
177  ; // end of PSO class binding
178 }
Base class for implementing fitting algorithms.
Definition: FitterBase.h:26
void RunLLHScan()
Perform a 1D likelihood scan.
Definition: FitterBase.cpp:622
FitterBase(Manager *const fitMan)
Constructor.
Definition: FitterBase.cpp:16
void AddSystObj(ParameterHandlerBase *cov)
This function adds a Covariance object to the analysis framework. The Covariance object will be utili...
Definition: FitterBase.cpp:298
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:262
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:887
void RunSigmaVar()
Perform a 1D/2D 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:461
void Run2DLLHScan()
Perform a 2D likelihood scan.
Definition: FitterBase.cpp:936
Implementation of base Likelihood Fit class, it is mostly responsible for likelihood calculation whil...
Definition: LikelihoodFit.h:6
LikelihoodFit(Manager *const fitMan)
Constructor.
int GetNPars()
Get total number of params, this sums over all covariance objects.
Definition: LikelihoodFit.h:16
void setChainLength(unsigned int L)
Set how long chain should be.
Definition: MCMCBase.h:26
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