6 #include <pybind11/pybind11.h>
7 #include <pybind11/stl.h>
8 #include <pybind11/numpy.h>
16 namespace py = pybind11;
27 PYBIND11_OVERRIDE_PURE(
36 PYBIND11_OVERRIDE_PURE(
46 PYBIND11_OVERRIDE_PURE(
56 PYBIND11_OVERRIDE_PURE_NAME(
67 PYBIND11_OVERRIDE_PURE_NAME(
70 "get_sample_likelihood",
78 PYBIND11_OVERRIDE_PURE_NAME(
81 "clean_memory_before_fit",
88 PYBIND11_OVERRIDE_PURE(
97 std::string
GetKinVarName(
const int iSample,
const int Dimension)
const override {
98 PYBIND11_OVERRIDE_PURE(
109 PYBIND11_OVERRIDE_PURE(
119 PYBIND11_OVERRIDE_PURE(
128 PYBIND11_OVERRIDE_PURE(
137 PYBIND11_OVERRIDE_PURE(
146 PYBIND11_OVERRIDE_PURE_NAME(
156 const std::string& ProjectionVar_Str,
157 const int kModeToFill = -1,
158 const int kChannelToFill = -1,
159 const int WeightStyle = 0)
override {
161 (void) ProjectionVar_Str;
163 (void) kChannelToFill;
169 const std::string& ProjectionVar_StrX,
170 const std::string& ProjectionVar_StrY,
171 const int kModeToFill = -1,
172 const int kChannelToFill = -1,
173 const int WeightStyle = 0)
override {
175 (void) ProjectionVar_StrX;
176 (void) ProjectionVar_StrY;
178 (void) kChannelToFill;
184 const std::string &ProjectionVar,
185 const std::vector<KinematicCut> &EventSelectionVec = {},
186 const int WeightStyle = 0,
187 const std::vector<KinematicCut> &SubEventSelectionVec = {})
override {
189 (void) ProjectionVar;
190 (void) EventSelectionVec;
192 (void) SubEventSelectionVec;
197 const std::string& ProjectionVarX,
198 const std::string& ProjectionVarY,
199 const std::vector<KinematicCut>& EventSelectionVec = {},
200 const int WeightStyle = 0,
201 const std::vector<KinematicCut>& SubEventSelectionVec = {})
override {
203 (void) ProjectionVarX;
204 (void) ProjectionVarY;
205 (void) EventSelectionVec;
207 (void) SubEventSelectionVec;
211 int GetNDim(
const int Sample)
const override {
212 PYBIND11_OVERRIDE_PURE(
221 const int iChannel)
const override {
222 PYBIND11_OVERRIDE_PURE(
241 PYBIND11_OVERRIDE_PURE_NAME(
244 "add_additional_weight_pointers",
252 PYBIND11_OVERRIDE_PURE(
261 PYBIND11_OVERRIDE_PURE_NAME(
272 PYBIND11_OVERRIDE_PURE_NAME(
283 PYBIND11_OVERRIDE_PURE_NAME(
286 "setup_experiment_MC",
293 PYBIND11_OVERRIDE_PURE_NAME(
303 PYBIND11_OVERRIDE_PURE_NAME(
312 PYBIND11_OVERRIDE_PURE_NAME(
315 "get_event_kinematic_value",
323 PYBIND11_OVERRIDE_PURE_NAME(
326 "get_event_kinematic_value_reference",
334 PYBIND11_OVERRIDE_PURE_NAME(
337 "register_functional_parameters",
346 py::enum_<TestStatistic>(m_samples,
"TestStatistic")
360 "reweight the MC events in this sample. You will need to override this.")
365 "Get the total number of samples"
372 "Get the dimension of a given sample"
379 auto hist_original =
M3::Clone(
self.GetMCHist(sample));
385 py::return_value_policy::reference_internal,
387 "Get MC histogram as numpy arrays.\n"
388 "For 1D: Returns (contents, edges)\n"
389 "For 2D: Returns (contents, edgesX, edgesY)\n"
390 "where contents is shape (nbinsY, nbinsX) for 2D")
395 "Get the sample likelihood at the current point in your model space. You will need to override this.")
400 "Set the test statistic that should be used when calculating likelihoods. \n\
401 :param test_stat: The new test statistic to use",
402 py::arg(
"test_stat"))
407 "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.SampleHandlerInterface.set_test_stat` \n\
408 :param data: The data content of the bin. \n\
409 :param mc: The mc content of the bin \n\
410 :param w2: The Sum(w_{i}^2) (sum of weights squared) in the bin, which is sigma^2_{MC stats}",
420 py::init<std::string, ParameterHandlerGeneric *>(),
421 "This should never be called directly as SampleHandlerBase is an abstract base class. \n\
422 However when creating a derived class, in the __init__() method, you should call the parent constructor i.e. this one by doing:: \n\
424 \tsuper(<your derived SampleHandler class>, self).__init__(*args) \n\
426 py::arg(
"mc_version"), py::arg(
"xsec_cov"))
432 py::arg(
"data_array"),
433 "Set the data for your sample handler (assumes the binning is the same as your MC!)"
443 "Get the title for a given sample"
449 "Returns the contents of the MC histogram as a flat list"
456 "Returns the contents of the MC histogram as a flat list"
463 "Returns the contents of the W2 histogram as a flat list"
470 auto hist_original =
M3::Clone(
self.GetDataHist(sample));
476 py::arg(
"Dimension"),
477 "Get Data histogram as numpy arrays.\n"
478 "For 1D: Returns (contents, edges)\n"
479 "For 2D: Returns (contents, edgesX, edgesY)\n"
480 "where contents is shape (nbinsY, nbinsX) for 2D")
486 auto hist_original =
M3::Clone(
self.GetW2Hist(sample));
494 "Get W2 histogram as numpy arrays.\n"
495 "For 1D: Returns (contents, edges)\n"
496 "For 2D: Returns (contents, edgesX, edgesY)\n"
497 "where contents is shape (nbinsY, nbinsX) for 2D")
501 const std::string& ProjectionVarX,
502 const std::string& ProjectionVarY=
"",
503 const std::vector<KinematicCut> &EventSelectionVec = {},
505 const std::vector< KinematicCut >& SubEventSelectionVec = {})
508 py::array_t<M3::float_t> edgesY, edgesX, contents;
509 std::unique_ptr<TH1> hist;
511 if(ProjectionVarY==
""){
512 hist =
self.Get1DVarHist(iSample,
516 SubEventSelectionVec);
518 hist =
self.Get2DVarHist(iSample,
523 SubEventSelectionVec);
531 py::class_<KinematicCut>(m_samples,
"KinematicCut")
532 .def(py::init<>(),
"Simple wrapper around Kinematic cuts")
@ kNTestStatistics
Number of test statistics.
@ kPearson
Standard Pearson likelihood .
@ kBarlowBeeston
Barlow-Beeston () following Conway approximation ()
@ kDembinskiAbdelmotteleb
Based on .
@ kPoisson
Standard Poisson likelihood .
As SampleHandlerBase is an abstract base class we have to do some gymnastics to get it to get it into...
void SetupMC() override
Function which translates experiment struct into core struct.
void InititialiseData() override
Function responsible for loading data from file or loading from file.
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
void Init() override
Initialise any variables that your experiment specific SampleHandler needs.
int SetupExperimentMC() override
Experiment specific setup, returns the number of events which were loaded.
const double * GetPointerToKinematicParameter(int, int) const override
void SetupSplines() override
initialise your splineXX object and then use InitialiseSplineObject to conviently setup everything up
double ReturnKinematicParameter(int, int) const override
void RegisterFunctionalParameters() override
HH - a experiment-specific function where the maps to actual functions are set up.
void AddAdditionalWeightPointers() override
DB Function to determine which weights apply to which types of samples.
EW: As SampleHandlerBase is an abstract base class we have to do some gymnastics to get it to get it ...
void Reweight() override
main routine modifying MC prediction based on proposed parameter values
std::string GetKinVarName(const int iSample, const int Dimension) const override
Return Kinematic Variable name for specified sample and dimension for example "Reconstructed_Neutrino...
std::unique_ptr< TH2 > Get2DVarHist(const int iSample, const std::string &ProjectionVarX, const std::string &ProjectionVarY, const std::vector< KinematicCut > &EventSelectionVec={}, const int WeightStyle=0, const std::vector< KinematicCut > &SubEventSelectionVec={}) override
Build a 2D projection of MC events into specified variables.
std::unique_ptr< TH1 > Get1DVarHistByModeAndChannel(const int iSample, const std::string &ProjectionVar_Str, const int kModeToFill=-1, const int kChannelToFill=-1, const int WeightStyle=0) override
Build a 1D histogram for a given variable, optionally filtered by mode and channel.
std::vector< double > ReturnKinematicParameterBinning(const int Sample, const std::string &KinematicParameter) const override
Return the binning used to draw a kinematic parameter.
TH1 * GetDataHist(const int Sample) override
Get Data histogram.
void CleanMemoryBeforeFit() override
Allow to clean not used memory before fit starts.
std::string GetSampleTitle(const int iSample) const override
Get fancy title for specified samples.
double GetSampleLikelihood(const int iSample) const override
Get likelihood (-logL) for a single sample.
int GetNOscChannels(const int iSample) const override
Get number of oscillation channels for a single sample.
std::string GetName() const override
Get name for Sample Handler.
TH1 * GetMCHist(const int Sample) override
Get MC histogram.
std::string GetFlavourName(const int iSample, const int iChannel) const override
Get the flavour name for a given sample and oscillation channel.
std::unique_ptr< TH1 > Get1DVarHist(const int iSample, const std::string &ProjectionVar, const std::vector< KinematicCut > &EventSelectionVec={}, const int WeightStyle=0, const std::vector< KinematicCut > &SubEventSelectionVec={}) override
Return 1D projection of MC into given 1D variable (doesn't have to be variable used in the fit)
int GetNDim(const int Sample) const override
DB Get what dimensionality binning for given sample has.
std::unique_ptr< TH2 > Get2DVarHistByModeAndChannel(const int iSample, const std::string &ProjectionVar_StrX, const std::string &ProjectionVar_StrY, const int kModeToFill=-1, const int kChannelToFill=-1, const int WeightStyle=0) override
Build a 2D histogram for given variables, optionally filtered by mode and channel.
void PrintRates(const bool DataOnly=false) override
Helper function to print rates for the samples with LLH.
double GetLikelihood() const override
Return likelihood (-logL) for all samples.
TH1 * GetW2Hist(const int Sample) override
Get W2 histogram.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
SampleHandlerBase(std::string ConfigFileName, ParameterHandlerGeneric *xsec_cov, const std::shared_ptr< OscillationHandler > &OscillatorObj_=nullptr)
Constructor.
void AddData(const int Sample, TH1 *Data)
DB: Add data for a given sample from a ROOT histogram.
auto GetDataArray() const
Return array storing data entries for every bin.
std::string GetSampleTitle(const int Sample) const final
Get fancy title for specified samples.
auto GetMCArray() const
Return array storing MC entries for every bin.
auto GetW2Array() const
Return array storing W2 entries for every bin.
Class responsible for handling implementation of samples used in analysis, reweighting and returning ...
virtual double GetLikelihood() const =0
Return likelihood (-logL) for all samples.
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....
virtual M3::int_t GetNSamples()
returns total number of samples
virtual void Reweight()=0
main routine modifying MC prediction based on proposed parameter values
SampleHandlerInterface()
The main constructor.
void SetTestStatistic(TestStatistic testStat)
Set the test statistic to be used when calculating the binned likelihoods.
virtual int GetNDim(const int Sample) const =0
DB Get what dimensionality binning for given sample has.
HW: Histogram utilities for converting ROOT histograms to numpy arrays for use in Python.
py::tuple HistToNumpy(std::unique_ptr< TH1 > &hist)
std::unique_ptr< ObjectType > Clone(const ObjectType *obj, const std::string &name="")
KS: Creates a copy of a ROOT-like object and wraps it in a smart pointer.
void initSamplesModule(py::module &m_samples)
double UpperBound
Upper bound on which we apply cut.
double LowerBound
Lower bound on which we apply cut.
int ParamToCutOnIt
Index or enum value identifying the kinematic variable to cut on.