MaCh3 2.2.1
Reference Guide
Loading...
Searching...
No Matches
samples.cpp
Go to the documentation of this file.
1#include <pybind11/pybind11.h>
2#include <pybind11/stl.h>
3#include <pybind11/numpy.h>
4
6
7namespace py = pybind11;
8
11public:
12 /* Inherit the constructors */
14
15 /* Trampoline (need one for each virtual function) */
16 std::string GetSampleName(int iSample) const override {
17 PYBIND11_OVERRIDE_PURE(
18 std::string, /* Return type */
19 SampleHandlerBase, /* Parent class */
20 GetSampleName, /* Name of function in C++ (must match Python name) */
21 iSample /* Argument(s) */
22 );
23 }
24
25 /* Trampoline (need one for each virtual function) */
26 void Reweight() override {
27 PYBIND11_OVERRIDE_PURE(
28 void, /* Return type */
29 SampleHandlerBase, /* Parent class */
30 Reweight /* Name of function in C++ (must match Python name) */
31 );
32 }
33
34 /* Trampoline (need one for each virtual function) */
35 void CleanMemoryBeforeFit() override {
36 PYBIND11_OVERRIDE_PURE(
37 void, /* Return type */
38 SampleHandlerBase, /* Parent class */
39 CleanMemoryBeforeFit /* Name of function in C++ (must match Python name) */
40 );
41 }
42
43 double GetLikelihood() override {
44 PYBIND11_OVERRIDE_PURE_NAME(
45 double, /* Return type */
46 SampleHandlerBase, /* Parent class */
47 "get_likelihood", /* Python name*/
48 GetLikelihood /* Name of function in C++ (must match Python name) */
49 /* Argument(s) */
50 );
51 }
52};
53
54
57public:
58 /* Inherit the constructors */
60
61 /* Trampoline (need one for each virtual function) */
62 void SetupWeightPointers() override {
63 PYBIND11_OVERRIDE_PURE_NAME(
64 void, /* Return type */
65 SampleHandlerFD, /* Parent class */
66 "setup_weight_pointers", /*python name*/
67 SetupWeightPointers, /* Name of function in C++ */
68 /* Argument(s) */
69 );
70 }
71
72 /* Trampoline (need one for each virtual function) */
73 void CleanMemoryBeforeFit() override {
74 PYBIND11_OVERRIDE_PURE(
75 void, /* Return type */
76 SampleHandlerFD, /* Parent class */
77 CleanMemoryBeforeFit /* Name of function in C++ (must match Python name) */
78 );
79 }
80
81 /* Trampoline (need one for each virtual function) */
82 void SetupSplines() override {
83 PYBIND11_OVERRIDE_PURE_NAME(
84 void, /* Return type */
85 SampleHandlerFD, /* Parent class */
86 "setup_splines", /*python name*/
87 SetupSplines, /* Name of function in C++ */
88 /* Argument(s) */
89 );
90 }
91
92 /* Trampoline (need one for each virtual function) */
93 void Init() override {
94 PYBIND11_OVERRIDE_PURE_NAME(
95 void, /* Return type */
96 SampleHandlerFD, /* Parent class */
97 "init", /*python name*/
98 Init, /* Name of function in C++ */
99 /* Argument(s) */
100 );
101 }
102
103 /* Trampoline (need one for each virtual function) */
104 int SetupExperimentMC() override {
105 PYBIND11_OVERRIDE_PURE_NAME(
106 int, /* Return type */
107 SampleHandlerFD, /* Parent class */
108 "setup_experiment_MC", /*python name*/
109 SetupExperimentMC, /* Name of function in C++ */
110 );
111 }
112
113 /* Trampoline (need one for each virtual function) */
114 void SetupFDMC() override {
115 PYBIND11_OVERRIDE_PURE_NAME(
116 void, /* Return type */
117 SampleHandlerFD, /* Parent class */
118 "setup_FD_MC", /*python name*/
119 SetupFDMC, /* Name of function in C++ */
120 );
121 }
122
124 PYBIND11_OVERRIDE_PURE_NAME(
125 int, /* Return type */
126 SampleHandlerFD, /* Parent class */
127 "get_kinematic_by_name", /* python name*/
128 ReturnKinematicParameterFromString, /* Name of function in C++ (must match Python name) */
129 py::arg("variable_name")
130 );
131 }
132
134 PYBIND11_OVERRIDE_PURE_NAME(
135 std::string, /* Return type */
136 SampleHandlerFD, /* Parent class */
137 "get_kinematic_name", /* python name*/
138 ReturnStringFromKinematicParameter, /* Name of function in C++ (must match Python name) */
139 py::arg("variable_id")
140 );
141 }
142
143 double ReturnKinematicParameter(std::string, int) override {
144 PYBIND11_OVERRIDE_PURE_NAME(
145 double, /* Return type */
146 SampleHandlerFD, /* Parent class */
147 "get_event_kinematic_value",/* python name*/
148 ReturnKinematicParameter, /* Name of function in C++ (must match Python name) */
149 py::arg("variable"),
150 py::arg("event") /* Argument(s) */
151 );
152 }
153
154 double ReturnKinematicParameter(int, int) override {
155 PYBIND11_OVERRIDE_PURE_NAME(
156 double, /* Return type */
157 SampleHandlerFD, /* Parent class */
158 "get_event_kinematic_value",/* python name*/
159 ReturnKinematicParameter, /* Name of function in C++ (must match Python name) */
160 py::arg("variable"),
161 py::arg("event") /* Argument(s) */
162 );
163 }
164
165 const double *GetPointerToKinematicParameter(std::string, int) override {
166 PYBIND11_OVERRIDE_PURE_NAME(
167 const double *, /* Return type */
168 SampleHandlerFD, /* Parent class */
169 "get_event_kinematic_value_reference",/* python name*/
170 GetPointerToKinematicParameter, /* Name of function in C++ (must match Python name) */
171 py::arg("variable"),
172 py::arg("event") /* Argument(s) */
173 );
174 }
175 const double *GetPointerToKinematicParameter(double, int) override {
176 PYBIND11_OVERRIDE_PURE_NAME(
177 const double *, /* Return type */
178 SampleHandlerFD, /* Parent class */
179 "get_event_kinematic_value_reference",/* python name*/
180 GetPointerToKinematicParameter, /* Name of function in C++ (must match Python name) */
181 py::arg("variable"),
182 py::arg("event") /* Argument(s) */
183 );
184 }
185
187 PYBIND11_OVERRIDE_PURE_NAME(
188 void,
190 "register_functional_parameters",
192 );
193 }
194};
195
196void initSamples(py::module &m){
197
198 auto m_samples = m.def_submodule("samples");
199 m_samples.doc() =
200 "This is a Python binding of MaCh3s C++ based samples library.";
201
202 // Bind the systematic type enum that lets us set different types of systematics
203 py::enum_<TestStatistic>(m_samples, "TestStatistic")
204 .value("Poisson", TestStatistic::kPoisson)
205 .value("Barlow_Beeston", TestStatistic::kBarlowBeeston)
206 .value("Ice_Cube", TestStatistic::kIceCube)
207 .value("Pearson", TestStatistic::kPearson)
208 .value("Dembinski_Abdelmottele", TestStatistic::kDembinskiAbdelmotteleb)
209 .value("N_Test_Statistics", TestStatistic::kNTestStatistics);
210
211 py::class_<SampleHandlerBase, PySampleHandlerBase /* <--- trampoline*/>(m_samples, "SampleHandlerBase")
212 .def(py::init())
213
214 .def(
215 "reweight",
217 "reweight the MC events in this sample. You will need to override this."
218 )
219
220 .def(
221 "get_likelihood",
223 "Get the sample likelihood at the current point in your model space. You will need to override this."
224 )
225
226 .def(
227 "set_test_stat",
229 "Set the test statistic that should be used when calculating likelihoods. \n\
230 :param test_stat: The new test statistic to use",
231 py::arg("test_stat")
232 )
233
234 .def(
235 "get_bin_LLH",
236 py::overload_cast<double, double, double>(&SampleHandlerBase::GetTestStatLLH, py::const_),
237 "Get the LLH for a bin by comparing the data and MC. The result depends on having previously set the test statistic using :py:meth:`pyMaCh3.samples.SampleHandlerBase.set_test_stat` \n\
238 :param data: The data content of the bin. \n\
239 :param mc: The mc content of the bin \n\
240 :param w2: The Sum(w_{i}^2) (sum of weights squared) in the bin, which is sigma^2_{MC stats}",
241 py::arg("data"),
242 py::arg("mc"),
243 py::arg("w2")
244 )
245 ; // End of SampleHandlerBase binding
246
247 py::class_<SampleHandlerFD, PySampleHandlerFD /* <--- trampoline*/, SampleHandlerBase>(m_samples, "SampleHandlerFD")
248 .def(
249 py::init<std::string, ParameterHandlerGeneric*>(),
250 "This should never be called directly as SampleHandlerFD is an abstract base class. \n\
251 However when creating a derived class, in the __init__() method, you should call the parent constructor i.e. this one by doing:: \n\
252 \n\
253 \tsuper(<your derived SampleHandler class>, self).__init__(*args) \n\
254 \n ",
255 py::arg("mc_version"),
256 py::arg("xsec_cov")
257 )
258 ;
259
260 /* Not sure if this will be needed in future versions of MaCh3 so leaving commented for now
261 py::class_<fdmc_base>(m_samples, "MCstruct")
262 .def(py::init())
263
264 // Because a lot of the variables in fdmc_base use c style arrays,
265 // we need to provide some setter functions to be able to set them using more
266 // "pythony" objects, e.g. lists and numpy arrays
267 .def(
268 "set_event_variable_values",
269 [](fdmc_base &self, int dim, py::array_t<double, py::array::c_style> &array)
270 {
271 py::buffer_info bufInfo = array.request();
272
273 if ( dim > 2 )
274 throw MaCh3Exception(__FILE__, __LINE__, "Currently only dimensions of 1 or 2 are supported sorry :(");
275
276 if ( bufInfo.ndim != 1 )
277 throw MaCh3Exception(__FILE__, __LINE__, "Number of dimensions in parameter array must be one if setting only one of the event variable arrays!");
278
279 if( dim ==1 )
280 self.x_var = array.data();
281
282 else if ( dim == 2)
283 self.y_var = array.data();
284 }
285 )
286 ;
287 */
288}
@ kNTestStatistics
Number of test statistics.
@ kPearson
Standard Pearson likelihood .
@ kBarlowBeeston
Barlow-Beeston () following Conway approximation ()
@ kIceCube
Based on .
@ kDembinskiAbdelmotteleb
Based on .
@ kPoisson
Standard Poisson likelihood .
EW: As SampleHandlerBase is an abstract base class we have to do some gymnastics to get it to get it ...
Definition: samples.cpp:10
std::string GetSampleName(int iSample) const override
Definition: samples.cpp:16
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
Definition: samples.cpp:35
void Reweight() override
Definition: samples.cpp:26
double GetLikelihood() override
Definition: samples.cpp:43
As SampleHandlerFD is an abstract base class we have to do some gymnastics to get it to get it into p...
Definition: samples.cpp:56
void SetupSplines() override
initialise your splineXX object and then use InitialiseSplineObject to conviently setup everything up
Definition: samples.cpp:82
void SetupWeightPointers() override
DB Function to determine which weights apply to which types of samples pure virtual!...
Definition: samples.cpp:62
double ReturnKinematicParameter(int, int) override
Definition: samples.cpp:154
const double * GetPointerToKinematicParameter(double, int) override
Definition: samples.cpp:175
int SetupExperimentMC() override
Experiment specific setup, returns the number of events which were loaded.
Definition: samples.cpp:104
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
Definition: samples.cpp:73
void Init() override
Initialise any variables that your experiment specific SampleHandler needs.
Definition: samples.cpp:93
int ReturnKinematicParameterFromString(std::string)
Definition: samples.cpp:123
const double * GetPointerToKinematicParameter(std::string, int) override
Definition: samples.cpp:165
void SetupFDMC() override
Function which translates experiment struct into core struct.
Definition: samples.cpp:114
double ReturnKinematicParameter(std::string, int) override
Return the value of an assocaited kinematic parameter for an event.
Definition: samples.cpp:143
void RegisterFunctionalParameters() override
HH - a experiment-specific function where the maps to actual functions are set up.
Definition: samples.cpp:186
std::string ReturnStringFromKinematicParameter(int)
Definition: samples.cpp:133
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
virtual void Reweight()=0
SampleHandlerBase()
The main constructor.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
SampleHandlerFD(std::string ConfigFileName, ParameterHandlerGeneric *xsec_cov, const std::shared_ptr< OscillationHandler > &OscillatorObj_=nullptr)
Constructor.
void SetTestStatistic(TestStatistic testStat)
Set the test statistic to be used when calculating the binned likelihoods.
virtual double GetLikelihood()=0
double GetTestStatLLH(double data, double mc) const
Calculate test statistic for a single bin using Poisson.
void initSamples(py::module &m)
Definition: samples.cpp:196