GooFit  v2.1.3
UnbinnedDataSet.cpp
Go to the documentation of this file.
1 #include <goofit/Error.h>
2 #include <goofit/Log.h>
4 #include <goofit/Variable.h>
5 
6 #include <Eigen/Dense>
7 
8 namespace GooFit {
9 
10 // Special constructor for one variable
11 UnbinnedDataSet::UnbinnedDataSet(const Observable &var, std::string n)
12  : DataSet(var, n) {
13  data.resize(1);
14 }
15 
16 UnbinnedDataSet::UnbinnedDataSet(const std::vector<Observable> &vars, std::string n)
17  : DataSet(vars, n) {
18  data.resize(vars.size());
19 }
20 
21 UnbinnedDataSet::UnbinnedDataSet(const std::set<Observable> &vars, std::string n)
22  : DataSet(vars, n) {
23  data.resize(vars.size());
24 }
25 
26 UnbinnedDataSet::UnbinnedDataSet(std::initializer_list<Observable> vars, std::string n)
27  : DataSet(vars, n) {
28  data.resize(vars.size());
29 }
30 
31 fptype UnbinnedDataSet::getValue(const Observable &var, size_t idx) const {
32  if(idx >= getNumEvents()) {
33  throw GooFit::GeneralError("UnbinnedDataSet: Attepted to find {} in event {} when only {} events exits",
34  var.getName(),
35  idx,
36  getNumEvents());
37  }
38 
39  size_t var_idx = indexOfVariable(var);
40 
41  return data[var_idx].at(idx);
42 }
43 
44 void UnbinnedDataSet::loadEvent(size_t idx) {
45  size_t i = 0;
46  for(Observable &v : observables) {
47  v.setValue(data.at(i++).at(idx));
48  }
49 }
50 
52  size_t ivar = indexOfVariable(var);
53  for(size_t i = 0; i < getNumEvents(); i++) {
54  data[ivar][i] = var.getValue();
55  }
56 }
57 
59  checkAllVars();
60  size_t i = 0;
61  for(const Observable &v : observables)
62  data.at(i++).push_back(v.getValue());
64 }
65 
66 // Utility function to make a grid of any dimisinion
67 void make_a_grid(std::vector<Observable> ret, UnbinnedDataSet *grid, Observable *eventNum) {
68  if(ret.empty()) {
69  grid->addEvent();
70 
71  if(eventNum != nullptr)
72  eventNum->setValue(eventNum->getValue() + 1);
73 
74  return;
75  }
76 
77  Observable var = ret.back();
78  ret.pop_back(); // safe because this is a copy
79 
80  if(var.isEventNumber()) { // Skip
81  make_a_grid(ret, grid, eventNum);
82  } else {
83  for(int i = 0; i < var.getNumBins(); ++i) {
84  double step = (var.getUpperLimit() - var.getLowerLimit()) / var.getNumBins();
85  var.setValue(var.getLowerLimit() + (i + 0.5) * step);
86  make_a_grid(ret, grid, eventNum);
87  }
88  }
89 }
90 
92  for(const auto &dat : data)
93  if(!dat.empty())
94  throw GeneralError("The dataset must be empty to fill with grid!");
95 
96  std::vector<Observable> ret = getObservables();
97 
98  Observable *evt_num = nullptr;
99 
100  for(Observable &val : ret)
101  if(val.isEventNumber())
102  evt_num = &val;
103 
104  make_a_grid(ret, this, evt_num);
105 }
106 
107 } // namespace GooFit
Thrown when a general error is encountered.
Definition: Error.h:10
size_t getNumEvents() const
Definition: DataSet.h:47
double fptype
bool isEventNumber() const
Check to see if this is an event number.
Definition: Variable.h:136
void fillWithGrid()
Replace the current dataset with a grid.
std::vector< Observable > observables
Definition: DataSet.h:66
UnbinnedDataSet(const Observable &var, std::string n="")
void setValue(fptype val)
Set the value.
Definition: Variable.h:70
void checkAllVars() const
Throw an error if any variables are out of range, call in addEvent.
Definition: DataSet.cpp:71
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.
size_t getNumBins() const
Get the number of bins.
Definition: Variable.h:130
size_t indexOfVariable(const Observable &var) const
Definition: DataSet.cpp:51
fptype getLowerLimit() const
Get the lower limit.
Definition: Variable.h:78
const std::vector< Observable > & getObservables() const
Definition: DataSet.cpp:49
fptype getValue() const
Get the value.
Definition: Variable.h:68
fptype getUpperLimit() const
Get the upper limit.
Definition: Variable.h:73
void setValueForAllEvents(const Observable &var)
Set all entries to a constant value (note: this is kind of ugly)
void loadEvent(size_t idx)
Set all the variables to the current event values.
size_t numEventsAdded
Definition: DataSet.h:54
void make_a_grid(std::vector< Observable > ret, UnbinnedDataSet *grid, Observable *eventNum)
const std::string & getName() const
Get the name.
Definition: Variable.h:63