GooFit  v2.1.3
DalitzPlotter.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <goofit/PDFs/GooPdf.h>
6 #include <goofit/Version.h>
7 
8 #include <algorithm>
9 #include <numeric>
10 #include <random>
11 
12 #if GOOFIT_ROOT_FOUND
13 #include <TH2.h>
14 #endif
15 
16 namespace GooFit {
17 
20  std::vector<size_t> xbins;
21  std::vector<size_t> ybins;
22  std::vector<std::vector<fptype>> pdfValues;
23  Observable m12;
24  Observable m13;
25  EventNumber eventNumber;
26  UnbinnedDataSet data;
27  fptype mother;
28 
29  public:
31  : m12(signalDalitz->_m12)
32  , m13(signalDalitz->_m13)
33  , eventNumber(signalDalitz->_eventNumber)
34  , data({m12, m13, eventNumber})
35  , mother(signalDalitz->decayInfo.motherMass) {
36  eventNumber.setValue(0);
37 
38  for(size_t i = 0; i < m12.getNumBins(); ++i) {
39  m12.setValue(m12.getLowerLimit() + m12.getBinSize() * (i + 0.5));
40  for(size_t j = 0; j < m13.getNumBins(); ++j) {
41  m13.setValue(m13.getLowerLimit() + m13.getBinSize() * (j + 0.5));
42  if(inDalitz(m12.getValue(),
43  m13.getValue(),
44  signalDalitz->decayInfo.motherMass,
45  signalDalitz->decayInfo.daug1Mass,
46  signalDalitz->decayInfo.daug2Mass,
47  signalDalitz->decayInfo.daug3Mass)) {
48  xbins.push_back(i);
49  ybins.push_back(j);
50  data.addEvent();
51  eventNumber.setValue(eventNumber.getValue() + 1);
52  }
53  }
54  }
55 
56  auto old = overallSignal->getData();
57  overallSignal->setData(&data);
58  signalDalitz->setDataSize(data.getNumEvents());
59  pdfValues = overallSignal->getCompProbsAtDataPoints();
60  overallSignal->setData(old);
61  }
62 
64  void fillDataSetMC(UnbinnedDataSet &dataset, size_t nTotal) {
65  // Setup random numbers
66  std::random_device rd;
67  std::mt19937 gen(rd());
68 
69  // Uniform distribution
70  std::uniform_real_distribution<> unihalf(-.5, .5);
71  std::uniform_real_distribution<> uniwhole(0.0, 1.0);
72 
73  // CumSum in other languages
74  std::vector<double> integral(pdfValues[0].size());
75  std::partial_sum(pdfValues[0].begin(), pdfValues[0].end(), integral.begin());
76 
77  // Make this a 0-1 fraction by dividing by the end value
78  std::for_each(integral.begin(), integral.end(), [&integral](double &val) { val /= integral.back(); });
79 
80  for(size_t i = 0; i < nTotal; i++) {
81  double r = uniwhole(gen);
82 
83  // Binary search for integral[cell-1] < r < integral[cell]
84  size_t j = std::lower_bound(integral.begin(), integral.end(), r) - integral.begin();
85 
86  // Fill in the grid square randomly
87  double currm12 = data.getValue(m12, j) + m12.getBinSize() * unihalf(gen);
88  double currm13 = data.getValue(m13, j) + m13.getBinSize() * unihalf(gen);
89 
90  m12.setValue(currm12);
91  m13.setValue(currm13);
92  eventNumber.setValue(i);
93  dataset.addEvent();
94  }
95  }
96 
97  size_t getNumEvents() const { return data.getNumEvents(); }
98 
99  size_t getX(size_t event) const { return xbins.at(event); }
100 
101  size_t getY(size_t event) const { return ybins.at(event); }
102 
103  fptype getXval(size_t event) const { return data.getValue(m12, event); }
104 
105  fptype getYval(size_t event) const { return data.getValue(m13, event); }
106 
107  fptype getZval(size_t event) const { return POW2(mother) - POW2(getXval(event)) - POW2(getYval(event)); }
108 
109  fptype getVal(size_t event, size_t num = 0) const { return pdfValues.at(num).at(event); }
110 
111  UnbinnedDataSet *getDataSet() { return &data; }
112 
113  const Observable &getM12() const { return m12; }
114  const Observable &getM13() const { return m13; }
115 
116 #if GOOFIT_ROOT_FOUND
117  TH2F *make2D(std::string name = "dalitzplot", std::string title = "") {
119  auto *dalitzplot = new TH2F(name.c_str(),
120  title.c_str(),
121  m12.getNumBins(),
122  m12.getLowerLimit(),
123  m12.getUpperLimit(),
124  m13.getNumBins(),
125  m13.getLowerLimit(),
126  m13.getUpperLimit());
127 
128  for(unsigned int j = 0; j < getNumEvents(); ++j) {
129  size_t currm12 = getX(j);
130  size_t currm13 = getY(j);
131  double val = getVal(j);
132 
133  dalitzplot->SetBinContent(1 + currm12, 1 + currm13, val);
134  }
135 
136  return dalitzplot;
137  }
138 #endif
139 };
140 
141 } // namespace GooFit
size_t getNumEvents() const
Definition: DataSet.h:47
fptype getBinSize() const
Get the bin size, (upper-lower) / bins.
Definition: Variable.h:133
UnbinnedDataSet * getDataSet()
double fptype
void fillDataSetMC(UnbinnedDataSet &dataset, size_t nTotal)
Fill a dataset with MC events.
Definition: DalitzPlotter.h:64
size_t getX(size_t event) const
Definition: DalitzPlotter.h:99
void setValue(fptype val)
Set the value.
Definition: Variable.h:70
Special class for observables. Used in DataSets.
Definition: Variable.h:109
fptype getValue(const Observable &var, size_t idx) const
Get the value at a specific variable and event number.
DalitzPlotter(GooPdf *overallSignal, DalitzPlotPdf *signalDalitz)
Definition: DalitzPlotter.h:30
size_t getY(size_t event) const
size_t getNumBins() const
Get the number of bins.
Definition: Variable.h:130
TddpPdf * signalDalitz
__host__ __device__ bool inDalitz(const fptype &m12, const fptype &m13, const fptype &bigM, const fptype &dm1, const fptype &dm2, const fptype &dm3)
fptype getVal(size_t event, size_t num=0) const
fptype getLowerLimit() const
Get the lower limit.
Definition: Variable.h:78
fptype getZval(size_t event) const
fptype getValue() const
Get the value.
Definition: Variable.h:68
fptype getXval(size_t event) const
#define POW2(x)
fptype getUpperLimit() const
Get the upper limit.
Definition: Variable.h:73
size_t getNumEvents() const
Definition: DalitzPlotter.h:97
fptype getYval(size_t event) const
const Observable & getM13() const
This class makes it easy to make plots over 3 body Dalitz PDFs. You can use ROOT style value access o...
Definition: DalitzPlotter.h:19
const Observable & getM12() const