MaCh3  2.8.0
Reference Guide
SampleStructs.h
Go to the documentation of this file.
1 #pragma once
2 
3 // C++ includes
4 #include <set>
5 #include <list>
6 #include <unordered_map>
7 
8 // MaCh3 includes
10 #include "Manager/MaCh3Logger.h"
11 #include "Manager/Core.h"
13 #include "Utils/PhysicsUtils.h"
14 
16 // ROOT include
17 #include "TSpline.h"
18 #include "TObjString.h"
19 #include "TFile.h"
20 #include "TF1.h"
21 #include "TH2Poly.h"
22 #include "TH1.h"
23 // NuOscillator includes
24 #include "Constants/OscillatorConstants.h"
26 
34 
35 
36 // *****************
38 enum TargetMat {
39 // *****************
40  kTarget_H = 1,
41  kTarget_C = 12,
42  kTarget_N = 14,
43  kTarget_O = 16,
44  kTarget_Al = 27,
45  kTarget_Ar = 40,
46  kTarget_Ti = 48,
47  kTarget_Fe = 56,
48  kTarget_Pb = 207
49 };
50 
51 // *****************
53 inline std::string TargetMat_ToString(const TargetMat i) {
54 // *****************
55  std::string name;
56 
57  switch(i) {
58  case kTarget_H:
59  name = "Hydrogen";
60  break;
61  case kTarget_C:
62  name = "Carbon";
63  break;
64  case kTarget_N:
65  name = "Nitrogen";
66  break;
67  case kTarget_O:
68  name = "Oxygen";
69  break;
70  case kTarget_Al:
71  name = "Aluminium";
72  break;
73  case kTarget_Ar:
74  name = "Argon";
75  break;
76  case kTarget_Ti:
77  name = "Titanium";
78  break;
79  case kTarget_Fe:
80  name = "Iron";
81  break;
82  case kTarget_Pb:
83  name = "Lead";
84  break;
85  default:
86  name = "TargetMat_Undefined";
87  break;
88  }
89 
90  return name;
91 }
92 
93 // *****************
95 enum NuPDG {
96 // *****************
97  kNue = 12,
98  kNumu = 14,
99  kNutau = 16,
100  kNueBar = -12,
101  kNumuBar = -14,
102  kNutauBar = -16
103 };
104 
113 };
114 
115 // **************************************************
117 inline std::string TestStatistic_ToString(const TestStatistic TestStat) {
118 // **************************************************
119  std::string name = "";
120 
121  switch(TestStat) {
123  name = "Poisson";
124  break;
126  name = "Barlow-Beeston";
127  break;
129  name = "IceCube";
130  break;
132  name = "Pearson";
133  break;
135  name = "Dembinski-Abdelmotteleb";
136  break;
138  MACH3LOG_ERROR("kNTestStatistics is not a valid TestStatistic!");
139  throw MaCh3Exception(__FILE__, __LINE__);
140  default:
141  MACH3LOG_ERROR("UNKNOWN LIKELIHOOD SPECIFIED!");
142  MACH3LOG_ERROR("You gave test-statistic {}", static_cast<int>(TestStat));
143  throw MaCh3Exception(__FILE__ , __LINE__ );
144  }
145  return name;
146 }
147 
148 // **************************************************
150 inline TestStatistic TestStatFromString(const std::string& likelihood) {
151 // **************************************************
152  for(int i = 0; i < kNTestStatistics; i++) {
153  if(likelihood == TestStatistic_ToString(TestStatistic(i))) {
154  return TestStatistic(i);
155  }
156  }
157 
158  MACH3LOG_ERROR("Wrong form of test-statistic specified!");
159  MACH3LOG_ERROR("You gave {} and I only support:", likelihood);
160  for(int i = 0; i < kNTestStatistics; i++)
161  {
163  }
164  throw MaCh3Exception(__FILE__ , __LINE__ );
165 }
166 
167 // ***************************
169 struct KinematicCut {
170 // ***************************
177 };
178 
179 // ***************************
183 // ***************************
192 };
193 
194 // ***************************
196 struct BinInfo {
197 // ***************************
201  std::vector<std::array<double, 2>> Extent;
203  bool IsEventInside(const std::vector<double>& KinVars) const _noexcept_ {
204  bool inside = true;
205  const size_t N = KinVars.size();
206  #ifdef MULTITHREAD
207  #pragma omp simd reduction(&:inside)
208  #endif
209  for(size_t i = 0; i < N; ++i) {
210  const double Var = KinVars[i];
211  const bool in_bin = (Var > Extent[i][0]) & (Var <= Extent[i][1]);
212  inside &= in_bin;
213  }
214  return inside;
215  }
217  bool IsEventInside(const std::vector<const double*>& KinVars) const _noexcept_ {
218  bool inside = true;
219  const size_t N = KinVars.size();
220  #ifdef MULTITHREAD
221  #pragma omp simd reduction(&:inside)
222  #endif
223  for (size_t i = 0; i < N; ++i) {
224  const double Var = *KinVars[i];
225  const bool in_bin = (Var > Extent[i][0]) & (Var <= Extent[i][1]);
226  inside &= in_bin;
227  }
228  return inside;
229  }
230 };
231 
232 // ***************************
243 // ***************************
245  std::vector<std::vector<double>> BinEdges;
247  std::vector<int> AxisNBins;
248 
254  std::vector <std::vector<BinShiftLookup> > BinLookup;
256  std::vector<int> Strides;
258  bool Uniform = true;
260  std::vector<BinInfo> Bins;
262  std::vector<std::vector<int>> BinGridMapping;
263 
266  // all bin edges along that dimension. Get 1 less bin than number of edges.
267  void InitUniform(const std::vector<std::vector<double>>& InputEdges) {
268  BinEdges = InputEdges;
269  Uniform = true;
270  AxisNBins.resize(BinEdges.size());
271 
272  nBins = 1;
273  for(size_t iDim = 0; iDim < BinEdges.size(); iDim++)
274  {
275  const auto& Edges = BinEdges[iDim];
276  if (!std::is_sorted(Edges.begin(), Edges.end())) {
277  MACH3LOG_ERROR("Bin edges for Dim {} must be in increasing order in sample config. Bin edges passed: [{}]",
278  iDim, fmt::join(Edges, ", "));
279  throw MaCh3Exception(__FILE__, __LINE__);
280  }
281 
282  //Sanity check that some binning has been specified
283  if(BinEdges[iDim].size() == 0){
284  MACH3LOG_ERROR("No binning specified for Dim {} of sample binning, please add some binning to the sample config", iDim);
285  MACH3LOG_ERROR("Please ensure BinEdges are correctly configured for all dimensions");
286  throw MaCh3Exception(__FILE__, __LINE__);
287  }
288 
289  // Set the number of bins for this dimension
290  AxisNBins[iDim] = static_cast<int>(BinEdges[iDim].size()) - 1;
291  // Update total number of bins
292  nBins *= AxisNBins[iDim];
293 
294  MACH3LOG_INFO("{}-Dim Binning: [{:.2f}]", iDim, fmt::join(BinEdges[iDim], ", "));
295  }
296  GlobalOffset = 0;
298  InitialiseBinMigrationLookUp(static_cast<int>(BinEdges.size()));
299  }
300 
302  void CheckBinsDoNotOverlap(const std::vector<BinInfo>& TestedBins) const {
303  if (TestedBins.empty()) return;
304 
305  const size_t ExtentDim = TestedBins[0].Extent.size();
306 
307  for (size_t i = 0; i < TestedBins.size(); ++i) {
308  for (size_t j = i + 1; j < TestedBins.size(); ++j) {
309  bool OverlapsInAllDims = true;
310 
311  for (size_t iDim = 0; iDim < ExtentDim; ++iDim) {
312  const double a_lo = TestedBins[i].Extent[iDim][0];
313  const double a_hi = TestedBins[i].Extent[iDim][1];
314  const double b_lo = TestedBins[j].Extent[iDim][0];
315  const double b_hi = TestedBins[j].Extent[iDim][1];
316 
317  // [low, high) overlap check
318  if (!(a_lo < b_hi && b_lo < a_hi)) {
319  OverlapsInAllDims = false;
320  break;
321  }
322  }
323 
324  if (OverlapsInAllDims) {
325  MACH3LOG_ERROR("Overlapping non-uniform bins detected: Bin {} and Bin {}", i, j);
326  for (size_t iDim = 0; iDim < ExtentDim; ++iDim) {
327  MACH3LOG_ERROR(" Dim {}: Bin {} [{}, {}), Bin {} [{}, {})",
328  iDim,
329  i, TestedBins[i].Extent[iDim][0], TestedBins[i].Extent[iDim][1],
330  j, TestedBins[j].Extent[iDim][0], TestedBins[j].Extent[iDim][1]);
331  }
332  throw MaCh3Exception(__FILE__, __LINE__);
333  }
334  }
335  }
336  }
337 
344  void CheckBinsHaveNoGaps(const std::vector<BinInfo>& TestedBins,
345  const std::vector<double>& MinVal,
346  const std::vector<double>& MaxVal,
347  size_t ValidationBinsPerDim = 100) const {
348  bool gap_found = false;
349  if (TestedBins.empty()) return;
350  const size_t Dim = TestedBins[0].Extent.size();
351  if (MinVal.size() != Dim || MaxVal.size() != Dim) {
352  MACH3LOG_ERROR("MinVal/MaxVal size does not match dimension of bins");
353  throw MaCh3Exception(__FILE__, __LINE__);
354  }
355 
356  // Build a fine validation grid from the provided min/max
357  std::vector<std::vector<double>> TestGridEdges(Dim);
358  for (size_t d = 0; d < Dim; ++d) {
359  TestGridEdges[d].resize(ValidationBinsPerDim + 1);
360  const double width = (MaxVal[d] - MinVal[d]) / static_cast<double>(ValidationBinsPerDim);
361  for (size_t i = 0; i <= ValidationBinsPerDim; ++i)
362  TestGridEdges[d][i] = MinVal[d] + static_cast<double>(i) * width;
363  }
364  // Precompute midpoints of each test cell
365  std::vector<size_t> indices(Dim, 0);
366 
367  std::function<void(size_t)> scan = [&](size_t d) {
368  if (gap_found) return; // stop recursion
369 
370  // Base case: we have selected a cell in every dimension
371  if (d == Dim) {
372  std::vector<double> point(Dim);
373 
374  // Compute the midpoint of the current N-D grid cell
375  for (size_t i = 0; i < Dim; ++i) {
376  const double lo = TestGridEdges[i][indices[i]];
377  const double hi = TestGridEdges[i][indices[i] + 1];
378  point[i] = 0.5 * (lo + hi);
379  }
380 
381  // Check coverage
382  for (const auto& bin : TestedBins) {
383  if (bin.IsEventInside(point)) {
384  return; // covered
385  }
386  }
387 
388  // Not covered by any bin → gap
389  MACH3LOG_WARN("Gap detected in non-uniform binning at point [{:.2f}]", fmt::join(point, ", "));
390  gap_found = true;
391  return;
392  }
393 
394  for (size_t i = 0; i + 1 < TestGridEdges[d].size(); ++i) {
395  indices[d] = i;
396  scan(d + 1);
397  }
398  };
399  // Start recursion at dimension 0
400  scan(0);
401  }
402 
405  const size_t Dim = BinEdges.size();
406 
407  // Compute total number of "mega bins"
408  int NGridBins = 1;
409  for (size_t d = 0; d < Dim; ++d) {
410  NGridBins *= AxisNBins[d];
411  }
412  BinGridMapping.resize(NGridBins);
413 
414  // Helper: convert linear index to multi-dimensional index
415  std::vector<int> MultiIndex(Dim, 0);
416 
417  std::function<void(size_t, int)> scan;
418  scan = [&](size_t d, int LinearIndex) {
419  if (d == Dim) {
420  // Compute the edges of the current mega-bin
421  std::vector<std::array<double, 2>> CellEdges(Dim);
422  for (size_t i = 0; i < Dim; ++i) {
423  CellEdges[i][0] = BinEdges[i][MultiIndex[i]];
424  CellEdges[i][1] = BinEdges[i][MultiIndex[i] + 1];
425  }
426 
427  // Check overlap with all non-uniform bins
428  for (size_t iBin = 0; iBin < Bins.size(); ++iBin) {
429  auto& bin = Bins[iBin];
430  bool overlap = true;
431  for (size_t i = 0; i < Dim; ++i) {
432  const double a_lo = bin.Extent[i][0];
433  const double a_hi = bin.Extent[i][1];
434  const double b_lo = CellEdges[i][0];
435  const double b_hi = CellEdges[i][1];
436  if (!(a_hi > b_lo && a_lo < b_hi)) { // overlap condition
437  overlap = false;
438  break;
439  }
440  }
441  if (overlap) {
442  BinGridMapping[LinearIndex].push_back(static_cast<int>(iBin));
443 
444  // Debug statement: show both small bin extent AND mega-bin edges
445  std::vector<std::string> bin_extent_str(Dim);
446  std::vector<std::string> mega_edges_str(Dim);
447 
448  for (size_t i = 0; i < Dim; ++i) {
449  bin_extent_str[i] = fmt::format("[{:.3f}, {:.3f}]", bin.Extent[i][0], bin.Extent[i][1]);
450  mega_edges_str[i] = fmt::format("[{:.3f}, {:.3f}]", CellEdges[i][0], CellEdges[i][1]);
451  }
452 
453  MACH3LOG_DEBUG("MegaBin {} (multi-index [{}], edges {}) assigned Bin {} with extents {}",
454  LinearIndex, fmt::join(MultiIndex, ","), fmt::join(mega_edges_str, ", "),
455  iBin, fmt::join(bin_extent_str, ", "));
456  }
457  }
458  return;
459  }
460 
461  // Loop over all bins along this dimension
462  for (int i = 0; i < AxisNBins[d]; ++i) {
463  MultiIndex[d] = i;
464  int NewLinearIndex = LinearIndex;
465  if (d > 0) {
466  int stride = 1;
467  for (size_t s = 0; s < d; ++s) stride *= AxisNBins[s];
468  NewLinearIndex += i * stride;
469  } else {
470  NewLinearIndex = i;
471  }
472  scan(d + 1, NewLinearIndex);
473  }
474  };
475  // Start the recursive scan over all dimensions, beginning at dimension 0 with linear index 0
476  scan(0, 0);
477  }
478 
480  void InitNonUniform(const std::vector<std::vector<std::vector<double>>>& InputBins) {
481  Uniform = false;
482  nBins = 0;
483  Bins.resize(InputBins.size());
484 
485  size_t ExtentDim = InputBins[0].size();
486  if (ExtentDim == 1) {
487  MACH3LOG_ERROR("Trying to initialise Non-Uniform binning for single dimension, this is silly...");
488  throw MaCh3Exception(__FILE__, __LINE__);
489  }
490  for(size_t iBin = 0; iBin < InputBins.size(); iBin++) {
491  const auto& NewExtent = InputBins[iBin];
492  if (NewExtent.size() != ExtentDim) {
493  MACH3LOG_ERROR("Dimension of Bin {} is {}, while others have {}", iBin, NewExtent.size(), ExtentDim);
494  throw MaCh3Exception(__FILE__, __LINE__);
495  }
496 
497  BinInfo NewBin;
498  for (const auto& extent : NewExtent) {
499  if (extent.size() != 2) {
500  MACH3LOG_ERROR("Extent size is not 2 for Bin {}", iBin);
501  throw MaCh3Exception(__FILE__, __LINE__);
502  }
503  NewBin.Extent.push_back({extent[0], extent[1]});
504  MACH3LOG_DEBUG("Adding extent for Bin {} Dim {}: [{:.2f}, {:.2f}]",
505  iBin, NewBin.Extent.size()-1, NewBin.Extent.back()[0], NewBin.Extent.back()[1]);
506  }
507  Bins[iBin] = std::move(NewBin);
508  nBins++;
509  }
510  // Ensure we do not have weird overlaps
513  constexpr int BinsPerDimension = 10;
514  // Now we create huge map, which will allow to easily find non uniform bins
515  BinEdges.resize(ExtentDim);
516  AxisNBins.resize(ExtentDim);
517  std::vector<double> MinVal(ExtentDim), MaxVal(ExtentDim);
518  for (size_t iDim = 0; iDim < ExtentDim; iDim++) {
519  MinVal[iDim] = std::numeric_limits<double>::max();
520  MaxVal[iDim] = std::numeric_limits<double>::lowest();
521 
522  // Find min and max for this dimension
523  for (const auto& bin : Bins) {
524  MinVal[iDim] = std::min(MinVal[iDim], bin.Extent[iDim][0]);
525  MaxVal[iDim] = std::max(MaxVal[iDim], bin.Extent[iDim][1]);
526  }
527 
528  MACH3LOG_DEBUG("Mapping binning: Dim {} Min = {:.2f}, Max = {:.2f}", iDim, MinVal[iDim], MaxVal[iDim]);
529  BinEdges[iDim].resize(BinsPerDimension + 1);
530  double BinWidth = (MaxVal[iDim] - MinVal[iDim]) / static_cast<double>(BinsPerDimension);
531  for (size_t iEdge = 0; iEdge <= BinsPerDimension; iEdge++) {
532  BinEdges[iDim][iEdge] = MinVal[iDim] + static_cast<double>(iEdge) * BinWidth;
533  }
534  AxisNBins[iDim] = BinsPerDimension;
535  MACH3LOG_DEBUG("Mapping binning: Dim {} BinEdges = [{:.2f}]", iDim, fmt::join(BinEdges[iDim], ", "));
536  }
537  CheckBinsHaveNoGaps(Bins, MinVal, MaxVal, 200);
538  GlobalOffset = 0;
540  InitialiseBinMigrationLookUp(static_cast<int>(BinEdges.size()));
543  }
544 
548  int GetBinSafe(const std::vector<int>& BinIndices) const {
549  for(int iDim = 0; iDim < static_cast<int>(BinIndices.size()); iDim++){
550  if (BinIndices[iDim] < 0 || BinIndices[iDim] >= AxisNBins[iDim]) {
551  MACH3LOG_ERROR("{}: Bin indices out of range: Dim = {}, Bin={}, max Ndim Bin={}",
552  __func__, iDim, BinIndices[iDim], AxisNBins[iDim]);
553  throw MaCh3Exception(__FILE__, __LINE__);
554  }
555  }
556  return GetBin(BinIndices);
557  }
558 
566  int GetBin(const std::vector<int>& BinIndices) const {
567  int BinNumber = 0;
568  for(size_t i = 0; i < BinIndices.size(); ++i) {
569  BinNumber += BinIndices[i]*Strides[i];
570  }
571  return BinNumber;
572  }
573 
575  int FindBin(const int Dimension, const double Var, const int NomBin) const _noexcept_ {
576  return FindBin(Var, NomBin, AxisNBins[Dimension], BinEdges[Dimension], BinLookup[Dimension]);
577  }
578 
587  int FindBin(const double KinVar,
588  const int NomBin,
589  const int N_Bins,
590  const std::vector<double>& Bin_Edges,
591  const std::vector<BinShiftLookup>& Bin_Lookup) const _noexcept_ {
592  //DB Check to see if momentum shift has moved bins
593  //DB - First , check to see if the event is outside of the binning range and skip event if it is
594  if (KinVar < Bin_Edges[0] || KinVar >= Bin_Edges[N_Bins]) {
595  return M3::UnderOverFlowBin;
596  }
597  // KS: If NomBin is UnderOverFlowBin we must do binary search :(
598  if(NomBin > M3::UnderOverFlowBin) {
599  // KS: Get reference to avoid repeated indexing and help with performance
600  const BinShiftLookup& _restrict_ Bin = Bin_Lookup[NomBin];
601  const double lower = Bin.lower_binedge;
602  const double upper = Bin.upper_binedge;
603  const double lower_lower = Bin.lower_lower_binedge;
604  const double upper_upper = Bin.upper_upper_binedge;
605 
606  //DB - Second, check to see if the event is still in the nominal bin
607  if (KinVar < upper && KinVar >= lower) {
608  return NomBin;
609  }
610  //DB - Thirdly, check the adjacent bins first as Eb+CC+EScale shifts aren't likely to move an Erec more than 1bin width
611  //Shifted down one bin from the event bin at nominal
612  if (KinVar < lower && KinVar >= lower_lower) {
613  return NomBin-1;
614  }
615  //Shifted up one bin from the event bin at nominal
616  if (KinVar < upper_upper && KinVar >= upper) {
617  return NomBin+1;
618  }
619  }
620  //DB - If we end up in this loop, the event has been shifted outside of its nominal bin, but is still within the allowed binning range
621  // KS: Perform binary search to find correct bin. We already checked if isn't outside of bounds
622  return static_cast<int>(std::distance(Bin_Edges.begin(), std::upper_bound(Bin_Edges.begin(), Bin_Edges.end(), KinVar)) - 1);
623  }
624 
629  void InitialiseLookUpSingleDimension(std::vector<BinShiftLookup>& Bin_Lookup, const std::vector<double>& Bin_Edges, const int TotBins) {
630  Bin_Lookup.resize(TotBins);
631  //Set rw_pdf_bin and upper_binedge and lower_binedge for each skmc_base
632  for(int bin_i = 0; bin_i < TotBins; bin_i++){
633  double low_lower_edge = M3::_DEFAULT_RETURN_VAL_;
634  double low_edge = Bin_Edges[bin_i];
635  double upper_edge = Bin_Edges[bin_i+1];
636  double upper_upper_edge = M3::_DEFAULT_RETURN_VAL_;
637 
638  if (bin_i == 0) {
639  low_lower_edge = Bin_Edges[0];
640  } else {
641  low_lower_edge = Bin_Edges[bin_i-1];
642  }
643 
644  if (bin_i + 2 < TotBins) {
645  upper_upper_edge = Bin_Edges[bin_i + 2];
646  } else if (bin_i + 1 < TotBins) {
647  upper_upper_edge = Bin_Edges[bin_i + 1];
648  }
649 
650  Bin_Lookup[bin_i].lower_binedge = low_edge;
651  Bin_Lookup[bin_i].upper_binedge = upper_edge;
652  Bin_Lookup[bin_i].lower_lower_binedge = low_lower_edge;
653  Bin_Lookup[bin_i].upper_upper_binedge = upper_upper_edge;
654  }
655  }
656 
666  void InitialiseStrides(const int Dimension) {
667  Strides.resize(Dimension);
668  int stride = 1;
669  for (int i = 0; i < Dimension; ++i) {
670  Strides[i] = stride;
671  // Multiply stride by the number of bins in this axis
672  stride *= AxisNBins[i];
673  }
674  }
675 
678  void InitialiseBinMigrationLookUp(const int Dimension) {
679  BinLookup.resize(Dimension);
680  for (int i = 0; i < Dimension; ++i) {
682  }
683 
684  InitialiseStrides(Dimension);
685  }
686 };
687 
692 inline int GetSampleFromGlobalBin(const std::vector<SampleBinningInfo>& BinningInfo, const int GlobalBin) {
693  for (size_t iSample = 0; iSample < BinningInfo.size(); ++iSample) {
694  const SampleBinningInfo& info = BinningInfo[iSample];
695  if (GlobalBin >= info.GlobalOffset && GlobalBin < info.GlobalOffset + info.nBins) {
696  return static_cast<int>(iSample);
697  }
698  }
699 
700  MACH3LOG_ERROR("Couldn't find sample corresponding to bin {}", GlobalBin);
701  throw MaCh3Exception(__FILE__, __LINE__);
702 
703  // GlobalBin is out of range for all samples
704  return M3::_BAD_INT_;
705 }
706 
711 inline int GetLocalBinFromGlobalBin(const std::vector<SampleBinningInfo>& BinningInfo,
712  const int GlobalBin) {
713  for (size_t iSample = 0; iSample < BinningInfo.size(); ++iSample) {
714  const SampleBinningInfo& info = BinningInfo[iSample];
715 
716  if (GlobalBin >= info.GlobalOffset &&
717  GlobalBin < info.GlobalOffset + info.nBins)
718  {
719  return GlobalBin - info.GlobalOffset;
720  }
721  }
722 
723  MACH3LOG_ERROR("Couldn't find local bin corresponding to bin {}", GlobalBin);
724  throw MaCh3Exception(__FILE__, __LINE__);
725 
726  return M3::_BAD_INT_;
727 }
728 
729 // ***************************
730 // A handy namespace for variables extraction
731 namespace M3 {
732 namespace Utils {
733 // ***************************
734  // ***************************
738  inline int PDGToNuOscillatorFlavour(const int NuPdg) {
739  int NuOscillatorFlavour = M3::_BAD_INT_;
740  switch(std::abs(NuPdg)){
741  case NuPDG::kNue:
742  NuOscillatorFlavour = NuOscillator::kElectron;
743  break;
744  case NuPDG::kNumu:
745  NuOscillatorFlavour = NuOscillator::kMuon;
746  break;
747  case NuPDG::kNutau:
748  NuOscillatorFlavour = NuOscillator::kTau;
749  break;
750  default:
751  MACH3LOG_ERROR("Unknown Neutrino PDG {}, cannot convert to NuOscillator type", NuPdg);
752  break;
753  }
754 
755  //This is very cheeky but if the PDG is negative then multiply the PDG by -1
756  // This is consistent with the treatment that NuOscillator expects as enums only
757  // exist for the generic matter flavour and not the anti-matter version
758  if(NuPdg < 0){NuOscillatorFlavour *= -1;}
759 
760  return NuOscillatorFlavour;
761  }
762  // ***************************
763 
764  // ***************************
766  inline std::string FormatDouble(const double value, const int precision) {
767  // ***************************
768  std::ostringstream oss;
769  oss << std::fixed << std::setprecision(precision) << value;
770  return oss.str();
771  }
772 } // end Utils namespace
773 } // end M3 namespace
KS: Core MaCh3 definitions and compile-time configuration utilities.
#define _noexcept_
KS: noexcept can help with performance but is terrible for debugging, this is meant to help easy way ...
Definition: Core.h:96
#define _MaCh3_Safe_Include_Start_
KS: Avoiding warning checking for headers.
Definition: Core.h:126
#define _MaCh3_Safe_Include_End_
#define _restrict_
KS: Using restrict limits the effects of pointer aliasing, aiding optimizations. While reading I foun...
Definition: Core.h:108
Defines the custom exception class used throughout MaCh3.
MaCh3 Logging utilities built on top of SPDLOG.
#define MACH3LOG_DEBUG
Definition: MaCh3Logger.h:34
#define MACH3LOG_ERROR
Definition: MaCh3Logger.h:37
#define MACH3LOG_INFO
Definition: MaCh3Logger.h:35
#define MACH3LOG_WARN
Definition: MaCh3Logger.h:36
Definitions of generic parameter structs and utility templates for MaCh3.
NuPDG
Enum to track the incoming neutrino species.
Definition: SampleStructs.h:95
@ kNutauBar
Tau antineutrino.
@ kNutau
Tau neutrino.
Definition: SampleStructs.h:99
@ kNumuBar
Muon antineutrino.
@ kNueBar
Electron antineutrino.
@ kNue
Electron neutrino.
Definition: SampleStructs.h:97
@ kNumu
Muon neutrino.
Definition: SampleStructs.h:98
TestStatistic TestStatFromString(const std::string &likelihood)
Convert a string to a TestStatistic enum.
std::string TestStatistic_ToString(const TestStatistic TestStat)
Convert a LLH type to a string.
std::string TargetMat_ToString(const TargetMat i)
Converted the Target Mat to a string.
Definition: SampleStructs.h:53
int GetSampleFromGlobalBin(const std::vector< SampleBinningInfo > &BinningInfo, const int GlobalBin)
Get the sample index corresponding to a global bin number.
TargetMat
Enum to track the target material.
Definition: SampleStructs.h:38
@ kTarget_Fe
Iron (Atomic number 26)
Definition: SampleStructs.h:47
@ kTarget_C
Carbon 12 (Atomic number 6)
Definition: SampleStructs.h:41
@ kTarget_Al
Aluminum (Atomic number 13)
Definition: SampleStructs.h:44
@ kTarget_H
Hydrogen (Atomic number 1)
Definition: SampleStructs.h:40
@ kTarget_Ti
Titanium (Atomic number 22)
Definition: SampleStructs.h:46
@ kTarget_Ar
Argon (Atomic number 18)
Definition: SampleStructs.h:45
@ kTarget_N
Nitrogen (Atomic number 7)
Definition: SampleStructs.h:42
@ kTarget_Pb
Lead (Atomic number 82)
Definition: SampleStructs.h:48
@ kTarget_O
Oxygen 16 (Atomic number 8)
Definition: SampleStructs.h:43
TestStatistic
Make an enum of the test statistic that we're using.
@ 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 .
int GetLocalBinFromGlobalBin(const std::vector< SampleBinningInfo > &BinningInfo, const int GlobalBin)
Get the local (sample) bin index from a global bin number.
Custom exception class used throughout MaCh3.
int PDGToNuOscillatorFlavour(const int NuPdg)
Convert from PDG flavour to NuOscillator type beware that in the case of anti-neutrinos the NuOscilla...
std::string FormatDouble(const double value, const int precision)
Convert double into string for precision, useful for playing with yaml if you don't want to have in c...
std::string join(const Container &values, std::string_view separator=" ")
Join all values in a container into a single string.
Definition: TablePrinter.h:36
Main namespace for MaCh3 software.
constexpr static const double _BAD_DOUBLE_
Default value used for double initialisation.
Definition: Core.h:53
constexpr static const int UnderOverFlowBin
Mark bin which is overflow or underflow in MaCh3 binning.
Definition: Core.h:91
constexpr static const int _BAD_INT_
Default value used for int initialisation.
Definition: Core.h:55
constexpr static const double _DEFAULT_RETURN_VAL_
Definition: Core.h:56
KS: This hold bin extents in N-Dimensions allowing to check if Bin falls into.
bool IsEventInside(const std::vector< const double * > &KinVars) const _noexcept_
Checks if a given event (point) falls inside the bin using pointer array.
bool IsEventInside(const std::vector< double > &KinVars) const _noexcept_
Checks if a given event (point) falls inside the bin.
std::vector< std::array< double, 2 > > Extent
KS: Store bin lookups allowing to quickly find bin after migration.
double lower_lower_binedge
lower to check if shift has moved the event to different bin
double upper_upper_binedge
upper to check if shift has moved the event to different bin
double lower_binedge
lower to check if shift has moved the event to different bin
double upper_binedge
upper to check if shift has moved the event to different bin
KS: Small struct used for applying kinematic cuts.
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.
KS: Struct storing all information required for sample binning.
void InitialiseLookUpSingleDimension(std::vector< BinShiftLookup > &Bin_Lookup, const std::vector< double > &Bin_Edges, const int TotBins)
Initializes lookup arrays for efficient bin migration in a single dimension.
void InitUniform(const std::vector< std::vector< double >> &InputEdges)
Initialise Uniform Binning.
std::vector< BinInfo > Bins
Bins used only for non-uniform.
int GlobalOffset
If you have binning for multiple samples and trying to define 1D vector let's.
std::vector< std::vector< int > > BinGridMapping
This grid tells what bins are associated with with what BinEdges of Grid Binnings.
int GetBinSafe(const std::vector< int > &BinIndices) const
Get linear bin index from ND bin indices with additional checks.
void InitNonUniform(const std::vector< std::vector< std::vector< double >>> &InputBins)
Initialise Non-Uniform Binning.
void InitialiseBinMigrationLookUp(const int Dimension)
Initialise special lookup arrays allowing to more efficiently perform bin-migration These arrays stor...
int FindBin(const int Dimension, const double Var, const int NomBin) const _noexcept_
DB Find the relevant bin in the PDF for each event.
void InitialiseGridMapping()
Initialise Non-Uniform Binning.
void InitialiseStrides(const int Dimension)
Initialise stride factors for linear bin index calculation.
int GetBin(const std::vector< int > &BinIndices) const
Convert N-dimensional bin indices to a linear bin index.
std::vector< std::vector< double > > BinEdges
Vector to hold N-axis bin-edges.
std::vector< int > Strides
Stride factors for converting N-dimensional bin indices to a linear index.
void CheckBinsDoNotOverlap(const std::vector< BinInfo > &TestedBins) const
Check that non-uniform bin extents do not overlap.
std::vector< int > AxisNBins
Number of N-axis bins in the histogram used for likelihood calculation.
bool Uniform
Tells whether to use inform binning grid or non-uniform.
int nBins
Number of total bins.
int FindBin(const double KinVar, const int NomBin, const int N_Bins, const std::vector< double > &Bin_Edges, const std::vector< BinShiftLookup > &Bin_Lookup) const _noexcept_
DB Find the relevant bin in the PDF for each event.
void CheckBinsHaveNoGaps(const std::vector< BinInfo > &TestedBins, const std::vector< double > &MinVal, const std::vector< double > &MaxVal, size_t ValidationBinsPerDim=100) const
Check that non-uniform bins fully cover the bounding box (no gaps)
std::vector< std::vector< BinShiftLookup > > BinLookup
Bin lookups for all dimensions.