MaCh3  2.4.2
Reference Guide
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 
7 namespace py = pybind11;
8 
11 public:
12  /* Inherit the constructors */
14 
15  /* Trampoline (need one for each virtual function) */
16  std::string GetName() const override {
17  PYBIND11_OVERRIDE_PURE(
18  std::string, /* Return type */
19  SampleHandlerBase, /* Parent class */
20  GetName, /* Name of function in C++ (must match Python name) */
21  );
22  }
23 
24  /* Trampoline (need one for each virtual function) */
25  std::string GetSampleTitle(const int iSample) const override {
26  PYBIND11_OVERRIDE_PURE(
27  std::string, /* Return type */
28  SampleHandlerBase, /* Parent class */
29  GetSampleTitle, /* Name of function in C++ (must match Python name) */
30  iSample /* Argument(s) */
31  );
32  }
33 
34  /* Trampoline (need one for each virtual function) */
35  int GetNOscChannels(const int iSample) const override {
36  PYBIND11_OVERRIDE_PURE(
37  int, /* Return type */
38  SampleHandlerBase, /* Parent class */
39  GetNOscChannels, /* Name of function in C++ (must match Python name) */
40  iSample /* Argument(s) */
41  );
42  }
43 
44  /* Trampoline (need one for each virtual function) */
45  void Reweight() override {
46  PYBIND11_OVERRIDE_PURE_NAME(
47  void, /* Return type */
48  SampleHandlerBase, /* Parent class */
49  "reweight",
50  Reweight /* Name of function in C++ (must match Python name) */
51  );
52  }
53 
54 
55  /* Trampoline (need one for each virtual function) */
56  double GetSampleLikelihood(const int iSample) const override {
57  PYBIND11_OVERRIDE_PURE_NAME(
58  double, /* Return type */
59  SampleHandlerBase, /* Parent class */
60  "get_sample_likelihood",
61  GetSampleLikelihood, /* Name of function in C++ (must match Python name) */
62  iSample /* Argument(s) */
63  );
64  }
65 
66  /* Trampoline (need one for each virtual function) */
67  void CleanMemoryBeforeFit() override {
68  PYBIND11_OVERRIDE_PURE_NAME(
69  void, /* Return type */
70  SampleHandlerBase, /* Parent class */
71  "clean_memory_before_fit",
72  CleanMemoryBeforeFit /* Name of function in C++ (must match Python name) */
73  );
74  }
75 
76  /* Trampoline (need one for each virtual function) */
77  void PrintRates(const bool DataOnly = false) override {
78  PYBIND11_OVERRIDE_PURE(
79  void, /* Return type */
80  SampleHandlerBase, /* Parent class */
81  PrintRates, /* Name of function in C++ (must match Python name) */
82  DataOnly /* Argument(s) */
83  );
84  }
85 
86  /* Trampoline (need one for each virtual function) */
87  std::string GetKinVarName(const int iSample, const int Dimension) const override {
88  PYBIND11_OVERRIDE_PURE(
89  std::string, /* Return type */
90  SampleHandlerBase, /* Parent class */
91  GetKinVarName, /* Name of function in C++ (must match Python name) */
92  iSample, /* Argument(s) */
93  Dimension /* Argument(s) */
94  );
95  }
96 
97  /* Trampoline (need one for each virtual function) */
98  std::vector<double> ReturnKinematicParameterBinning(const int Sample, const std::string &KinematicParameter) const override {
99  PYBIND11_OVERRIDE_PURE(
100  std::vector<double>, /* Return type */
101  SampleHandlerBase, /* Parent class */
102  GetKinVarName, /* Name of function in C++ (must match Python name) */
103  Sample, /* Argument(s) */
104  KinematicParameter /* Argument(s) */
105  );
106  }
107 
108  TH1* GetDataHist(const int Sample) override {
109  PYBIND11_OVERRIDE_PURE(
110  TH1*, /* Return type */
111  SampleHandlerBase, /* Parent class */
112  GetDataHist, /* Name of function in C++ (must match Python name) */
113  Sample /* Argument(s) */
114  );
115  }
116 
117  TH1* GetMCHist(const int Sample) override {
118  PYBIND11_OVERRIDE_PURE(
119  TH1*, /* Return type */
120  SampleHandlerBase, /* Parent class */
121  GetMCHist, /* Name of function in C++ (must match Python name) */
122  Sample /* Argument(s) */
123  );
124  }
125 
126  TH1* GetW2Hist(const int Sample) override {
127  PYBIND11_OVERRIDE_PURE(
128  TH1*, /* Return type */
129  SampleHandlerBase, /* Parent class */
130  GetW2Hist, /* Name of function in C++ (must match Python name) */
131  Sample /* Argument(s) */
132  );
133  }
134 
135  double GetLikelihood() const override {
136  PYBIND11_OVERRIDE_PURE_NAME(
137  double, /* Return type */
138  SampleHandlerBase, /* Parent class */
139  "get_likelihood", /* Python name*/
140  GetLikelihood /* Name of function in C++ (must match Python name) */
141  /* Argument(s) */
142  );
143  }
144 
145  TH1* Get1DVarHistByModeAndChannel(const int iSample,
146  const std::string& ProjectionVar_Str,
147  int kModeToFill = -1,
148  int kChannelToFill = -1,
149  int WeightStyle = 0,
150  TAxis* Axis = nullptr) override {
151  PYBIND11_OVERRIDE_PURE(
152  TH1*, /* Return type */
153  SampleHandlerBase, /* Parent class */
154  Get1DVarHistByModeAndChannel, /* Name of function in C++ */
155  iSample,
156  ProjectionVar_Str,
157  kModeToFill,
158  kChannelToFill,
159  WeightStyle,
160  Axis
161  );
162  }
163 
164  TH2* Get2DVarHistByModeAndChannel(const int iSample,
165  const std::string& ProjectionVar_StrX,
166  const std::string& ProjectionVar_StrY,
167  int kModeToFill = -1,
168  int kChannelToFill = -1,
169  int WeightStyle = 0,
170  TAxis* AxisX = nullptr,
171  TAxis* AxisY = nullptr) override {
172  PYBIND11_OVERRIDE_PURE(
173  TH2*, /* Return type */
174  SampleHandlerBase, /* Parent class */
175  Get2DVarHistByModeAndChannel, /* Name of function in C++ */
176  iSample,
177  ProjectionVar_StrX,
178  ProjectionVar_StrY,
179  kModeToFill,
180  kChannelToFill,
181  WeightStyle,
182  AxisX,
183  AxisY
184  );
185  }
186 
187  TH1* Get1DVarHist(const int iSample,
188  const std::string &ProjectionVar,
189  const std::vector<KinematicCut> &EventSelectionVec = {},
190  int WeightStyle = 0,
191  TAxis *Axis = nullptr,
192  const std::vector<KinematicCut> &SubEventSelectionVec = {}) override {
193  PYBIND11_OVERRIDE_PURE(
194  TH1*, /* Return type */
195  SampleHandlerBase, /* Parent class */
196  Get1DVarHist, /* Name of function in C++ */
197  iSample,
198  ProjectionVar,
199  EventSelectionVec,
200  WeightStyle,
201  Axis,
202  SubEventSelectionVec
203  );
204  }
205 
206  TH2* Get2DVarHist(const int iSample,
207  const std::string& ProjectionVarX,
208  const std::string& ProjectionVarY,
209  const std::vector<KinematicCut>& EventSelectionVec = {},
210  int WeightStyle = 0,
211  TAxis* AxisX = nullptr,
212  TAxis* AxisY = nullptr,
213  const std::vector<KinematicCut>& SubEventSelectionVec = {}) override {
214  PYBIND11_OVERRIDE_PURE(
215  TH2*, /* Return type */
216  SampleHandlerBase, /* Parent class */
217  Get2DVarHist, /* Name of function in C++ */
218  iSample,
219  ProjectionVarX,
220  ProjectionVarY,
221  EventSelectionVec,
222  WeightStyle,
223  AxisX,
224  AxisY,
225  SubEventSelectionVec
226  );
227  }
228 
229  int GetNDim(const int Sample) const override {
230  PYBIND11_OVERRIDE_PURE(
231  int, /* Return type */
232  SampleHandlerBase, /* Parent class */
233  GetNDim, /* Name of function in C++ */
234  Sample
235  );
236  }
237 
238  std::string GetFlavourName(const int iSample,
239  const int iChannel) const override {
240  PYBIND11_OVERRIDE_PURE(
241  std::string, /* Return type */
242  SampleHandlerBase, /* Parent class */
243  GetFlavourName, /* Name of function in C++ */
244  iSample,
245  iChannel
246  );
247  }
248 };
249 
250 
253 public:
254  /* Inherit the constructors */
256 
257  /* Trampoline (need one for each virtual function) */
258  void AddAdditionalWeightPointers() override {
259  PYBIND11_OVERRIDE_PURE_NAME(
260  void, /* Return type */
261  SampleHandlerFD, /* Parent class */
262  "add_additional_weight_pointers", /*python name*/
263  AddAdditionalWeightPointers, /* Name of function in C++ */
264  /* Argument(s) */
265  );
266  }
267 
268  /* Trampoline (need one for each virtual function) */
269  void CleanMemoryBeforeFit() override {
270  PYBIND11_OVERRIDE_PURE(
271  void, /* Return type */
272  SampleHandlerFD, /* Parent class */
273  CleanMemoryBeforeFit /* Name of function in C++ (must match Python name) */
274  );
275  }
276 
277  /* Trampoline (need one for each virtual function) */
278  void SetupSplines() override {
279  PYBIND11_OVERRIDE_PURE_NAME(
280  void, /* Return type */
281  SampleHandlerFD, /* Parent class */
282  "setup_splines", /*python name*/
283  SetupSplines, /* Name of function in C++ */
284  /* Argument(s) */
285  );
286  }
287 
288  /* Trampoline (need one for each virtual function) */
289  void Init() override {
290  PYBIND11_OVERRIDE_PURE_NAME(
291  void, /* Return type */
292  SampleHandlerFD, /* Parent class */
293  "init", /*python name*/
294  Init, /* Name of function in C++ */
295  /* Argument(s) */
296  );
297  }
298 
299  /* Trampoline (need one for each virtual function) */
300  int SetupExperimentMC() override {
301  PYBIND11_OVERRIDE_PURE_NAME(
302  int, /* Return type */
303  SampleHandlerFD, /* Parent class */
304  "setup_experiment_MC", /*python name*/
305  SetupExperimentMC, /* Name of function in C++ */
306  );
307  }
308 
309  /* Trampoline (need one for each virtual function) */
310  void SetupFDMC() override {
311  PYBIND11_OVERRIDE_PURE_NAME(
312  void, /* Return type */
313  SampleHandlerFD, /* Parent class */
314  "setup_FD_MC", /*python name*/
315  SetupFDMC, /* Name of function in C++ */
316  );
317  }
318 
320  PYBIND11_OVERRIDE_PURE_NAME(
321  int, /* Return type */
322  SampleHandlerFD, /* Parent class */
323  "get_kinematic_by_name", /* python name*/
324  ReturnKinematicParameterFromString, /* Name of function in C++ (must match Python name) */
325  py::arg("variable_name")
326  );
327  }
328 
330  PYBIND11_OVERRIDE_PURE_NAME(
331  std::string, /* Return type */
332  SampleHandlerFD, /* Parent class */
333  "get_kinematic_name", /* python name*/
334  ReturnStringFromKinematicParameter, /* Name of function in C++ (must match Python name) */
335  py::arg("variable_id")
336  );
337  }
338 
339  double ReturnKinematicParameter(std::string, int) override {
340  PYBIND11_OVERRIDE_PURE_NAME(
341  double, /* Return type */
342  SampleHandlerFD, /* Parent class */
343  "get_event_kinematic_value",/* python name*/
344  ReturnKinematicParameter, /* Name of function in C++ (must match Python name) */
345  py::arg("variable"),
346  py::arg("event") /* Argument(s) */
347  );
348  }
349 
350  double ReturnKinematicParameter(int, int) override {
351  PYBIND11_OVERRIDE_PURE_NAME(
352  double, /* Return type */
353  SampleHandlerFD, /* Parent class */
354  "get_event_kinematic_value",/* python name*/
355  ReturnKinematicParameter, /* Name of function in C++ (must match Python name) */
356  py::arg("variable"),
357  py::arg("event") /* Argument(s) */
358  );
359  }
360 
361  const double *GetPointerToKinematicParameter(std::string, int) override {
362  PYBIND11_OVERRIDE_PURE_NAME(
363  const double *, /* Return type */
364  SampleHandlerFD, /* Parent class */
365  "get_event_kinematic_value_reference",/* python name*/
366  GetPointerToKinematicParameter, /* Name of function in C++ (must match Python name) */
367  py::arg("variable"),
368  py::arg("event") /* Argument(s) */
369  );
370  }
371  const double *GetPointerToKinematicParameter(double, int) override {
372  PYBIND11_OVERRIDE_PURE_NAME(
373  const double *, /* Return type */
374  SampleHandlerFD, /* Parent class */
375  "get_event_kinematic_value_reference",/* python name*/
376  GetPointerToKinematicParameter, /* Name of function in C++ (must match Python name) */
377  py::arg("variable"),
378  py::arg("event") /* Argument(s) */
379  );
380  }
381 
383  PYBIND11_OVERRIDE_PURE_NAME(
384  void,
386  "register_functional_parameters",
388  );
389  }
390 };
391 
392 void initSamples(py::module &m) {
393  auto m_samples = m.def_submodule("samples");
394  m_samples.doc() =
395  "This is a Python binding of MaCh3s C++ based samples library.";
396 
397  // Bind the systematic type enum that lets us set different types of systematics
398  py::enum_<TestStatistic>(m_samples, "TestStatistic")
399  .value("Poisson", TestStatistic::kPoisson)
400  .value("Barlow_Beeston", TestStatistic::kBarlowBeeston)
401  .value("Ice_Cube", TestStatistic::kIceCube)
402  .value("Pearson", TestStatistic::kPearson)
403  .value("Dembinski_Abdelmottele", TestStatistic::kDembinskiAbdelmotteleb)
404  .value("N_Test_Statistics", TestStatistic::kNTestStatistics);
405 
406  py::class_<SampleHandlerBase, PySampleHandlerBase /* <--- trampoline*/>(m_samples, "SampleHandlerBase")
407  .def(py::init())
408 
409  .def(
410  "reweight",
412  "reweight the MC events in this sample. You will need to override this."
413  )
414 
415  .def(
416  "get_likelihood",
418  "Get the sample likelihood at the current point in your model space. You will need to override this."
419  )
420 
421  .def(
422  "set_test_stat",
424  "Set the test statistic that should be used when calculating likelihoods. \n\
425  :param test_stat: The new test statistic to use",
426  py::arg("test_stat")
427  )
428 
429  .def(
430  "get_bin_LLH",
431  py::overload_cast<double, double, double>(&SampleHandlerBase::GetTestStatLLH, py::const_),
432  "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\
433  :param data: The data content of the bin. \n\
434  :param mc: The mc content of the bin \n\
435  :param w2: The Sum(w_{i}^2) (sum of weights squared) in the bin, which is sigma^2_{MC stats}",
436  py::arg("data"),
437  py::arg("mc"),
438  py::arg("w2")
439  )
440  ; // End of SampleHandlerBase binding
441 
442  py::class_<SampleHandlerFD, PySampleHandlerFD /* <--- trampoline*/, SampleHandlerBase>(m_samples, "SampleHandlerFD")
443  .def(
444  py::init<std::string, ParameterHandlerGeneric*>(),
445  "This should never be called directly as SampleHandlerFD is an abstract base class. \n\
446  However when creating a derived class, in the __init__() method, you should call the parent constructor i.e. this one by doing:: \n\
447  \n\
448  \tsuper(<your derived SampleHandler class>, self).__init__(*args) \n\
449  \n ",
450  py::arg("mc_version"),
451  py::arg("xsec_cov")
452  )
453  ;
454 
455  /* Not sure if this will be needed in future versions of MaCh3 so leaving commented for now
456  py::class_<fdmc_base>(m_samples, "MCstruct")
457  .def(py::init())
458 
459  // Because a lot of the variables in fdmc_base use c style arrays,
460  // we need to provide some setter functions to be able to set them using more
461  // "pythony" objects, e.g. lists and numpy arrays
462  .def(
463  "set_event_variable_values",
464  [](fdmc_base &self, int dim, py::array_t<double, py::array::c_style> &array)
465  {
466  py::buffer_info bufInfo = array.request();
467 
468  if ( dim > 2 )
469  throw MaCh3Exception(__FILE__, __LINE__, "Currently only dimensions of 1 or 2 are supported sorry :(");
470 
471  if ( bufInfo.ndim != 1 )
472  throw MaCh3Exception(__FILE__, __LINE__, "Number of dimensions in parameter array must be one if setting only one of the event variable arrays!");
473 
474  if( dim ==1 )
475  self.x_var = array.data();
476 
477  else if ( dim == 2)
478  self.y_var = array.data();
479  }
480  )
481  ;
482  */
483 }
@ 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
TH1 * GetMCHist(const int Sample) override
Get MC histogram.
Definition: samples.cpp:117
std::string GetName() const override
Definition: samples.cpp:16
TH1 * Get1DVarHist(const int iSample, const std::string &ProjectionVar, const std::vector< KinematicCut > &EventSelectionVec={}, int WeightStyle=0, TAxis *Axis=nullptr, const std::vector< KinematicCut > &SubEventSelectionVec={}) override
Definition: samples.cpp:187
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
Definition: samples.cpp:67
double GetLikelihood() const override
Definition: samples.cpp:135
void Reweight() override
Definition: samples.cpp:45
double GetSampleLikelihood(const int iSample) const override
Definition: samples.cpp:56
std::string GetKinVarName(const int iSample, const int Dimension) const override
Return Kinematic Variable name for specified sample and dimension for example "Reconstructed_Neutrino...
Definition: samples.cpp:87
int GetNDim(const int Sample) const override
DB Function to differentiate 1D or 2D binning.
Definition: samples.cpp:229
std::vector< double > ReturnKinematicParameterBinning(const int Sample, const std::string &KinematicParameter) const override
Return the binning used to draw a kinematic parameter.
Definition: samples.cpp:98
int GetNOscChannels(const int iSample) const override
Definition: samples.cpp:35
TH1 * GetW2Hist(const int Sample) override
Get W2 histogram.
Definition: samples.cpp:126
std::string GetSampleTitle(const int iSample) const override
Definition: samples.cpp:25
std::string GetFlavourName(const int iSample, const int iChannel) const override
Definition: samples.cpp:238
TH1 * GetDataHist(const int Sample) override
Get Data histogram.
Definition: samples.cpp:108
TH2 * Get2DVarHist(const int iSample, const std::string &ProjectionVarX, const std::string &ProjectionVarY, const std::vector< KinematicCut > &EventSelectionVec={}, int WeightStyle=0, TAxis *AxisX=nullptr, TAxis *AxisY=nullptr, const std::vector< KinematicCut > &SubEventSelectionVec={}) override
Definition: samples.cpp:206
void PrintRates(const bool DataOnly=false) override
Helper function to print rates for the samples with LLH.
Definition: samples.cpp:77
TH1 * Get1DVarHistByModeAndChannel(const int iSample, const std::string &ProjectionVar_Str, int kModeToFill=-1, int kChannelToFill=-1, int WeightStyle=0, TAxis *Axis=nullptr) override
Definition: samples.cpp:145
TH2 * Get2DVarHistByModeAndChannel(const int iSample, const std::string &ProjectionVar_StrX, const std::string &ProjectionVar_StrY, int kModeToFill=-1, int kChannelToFill=-1, int WeightStyle=0, TAxis *AxisX=nullptr, TAxis *AxisY=nullptr) override
Definition: samples.cpp:164
As SampleHandlerFD is an abstract base class we have to do some gymnastics to get it to get it into p...
Definition: samples.cpp:252
void SetupSplines() override
initialise your splineXX object and then use InitialiseSplineObject to conviently setup everything up
Definition: samples.cpp:278
const double * GetPointerToKinematicParameter(std::string, int) override
Definition: samples.cpp:361
double ReturnKinematicParameter(int, int) override
Definition: samples.cpp:350
int SetupExperimentMC() override
Experiment specific setup, returns the number of events which were loaded.
Definition: samples.cpp:300
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
Definition: samples.cpp:269
void Init() override
Initialise any variables that your experiment specific SampleHandler needs.
Definition: samples.cpp:289
int ReturnKinematicParameterFromString(std::string)
Definition: samples.cpp:319
void SetupFDMC() override
Function which translates experiment struct into core struct.
Definition: samples.cpp:310
double ReturnKinematicParameter(std::string, int) override
Return the value of an associated kinematic parameter for an event.
Definition: samples.cpp:339
void RegisterFunctionalParameters() override
HH - a experiment-specific function where the maps to actual functions are set up.
Definition: samples.cpp:382
const double * GetPointerToKinematicParameter(double, int) override
Definition: samples.cpp:371
std::string ReturnStringFromKinematicParameter(int)
Definition: samples.cpp:329
void AddAdditionalWeightPointers() override
DB Function to determine which weights apply to which types of samples.
Definition: samples.cpp:258
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
void SetTestStatistic(TestStatistic testStat)
Set the test statistic to be used when calculating the binned likelihoods.
virtual void Reweight()=0
double GetTestStatLLH(const double data, const double mc, const double w2) const
Calculate test statistic for a single bin. Calculation depends on setting of fTestStatistic....
SampleHandlerBase()
The main constructor.
virtual double GetLikelihood() const =0
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 initSamples(py::module &m)
Definition: samples.cpp:392