GooFit  v2.1.3
UnbinnedDataSet.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <goofit/DataSet.h>
4 
5 #include <initializer_list>
6 #include <map>
7 #include <vector>
8 
9 #include <Eigen/Dense>
10 
11 namespace GooFit {
12 
13 class UnbinnedDataSet : public DataSet {
14  // Class for unbinned datasets.
15 
16  public:
17  using DataSet::addEvent;
18 
19  UnbinnedDataSet(const Observable &var, std::string n = "");
20  UnbinnedDataSet(const std::vector<Observable> &vars, std::string n = "");
21  UnbinnedDataSet(const std::set<Observable> &vars, std::string n = "");
22  UnbinnedDataSet(std::initializer_list<Observable> vars, std::string n = "");
23 
24  ~UnbinnedDataSet() override = default;
25 
26  void addEvent() override;
27 
29  void fillWithGrid();
30 
32  fptype getValue(const Observable &var, size_t idx) const;
33 
35  void loadEvent(size_t idx);
36 
38  void setValueForAllEvents(const Observable &var);
39 
41  template <typename M>
42  void from_matrix(const M &input, bool filter = false) {
43  size_t optional_index = getNumEvents(); // Only used if index not included
44 
45  if(observables.size() != input.rows() && observables.size() != input.rows() + 1)
46  throw GeneralError(
47  "The wrong number of rows, expected {}, but matrix had {}", observables.size(), input.rows());
48 
49  for(int i = 0; i < input.cols(); i++) { // Loop over events
50  for(int j = 0; j < input.rows(); j++) { // Loop over variables
51  observables.at(j).setValue(input(j, i));
52  }
53 
54  // Special override for counting variables (final param)
55  if(observables.size() == input.rows() + 1)
56  observables.at(input.rows()).setValue(optional_index++);
57 
58  if(!filter
59  || std::all_of(std::begin(observables), std::end(observables), [](Observable var) { return bool(var); }))
60  addEvent();
61  }
62  }
63 
65  template <typename M>
66  M to_matrix() const {
67  size_t rows = data.size();
68  size_t cols = data.at(0).size();
69  M mat(rows, cols);
70  for(int i = 0; i < rows; i++) {
71  for(int j = 0; j < cols; j++)
72  mat(i, j) = data[i].at(j);
73  }
74  return mat;
75  }
76 
77  private:
78  std::vector<std::vector<fptype>> data;
79 };
80 } // namespace GooFit
Thrown when a general error is encountered.
Definition: Error.h:10
size_t getNumEvents() const
Definition: DataSet.h:47
double fptype
void from_matrix(const M &input, bool filter=false)
Input an eigen matrix.
void fillWithGrid()
Replace the current dataset with a grid.
std::vector< Observable > observables
Definition: DataSet.h:66
UnbinnedDataSet(const Observable &var, std::string n="")
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.
~UnbinnedDataSet() override=default
void setValueForAllEvents(const Observable &var)
Set all entries to a constant value (note: this is kind of ugly)
virtual void addEvent()=0
void loadEvent(size_t idx)
Set all the variables to the current event values.
M to_matrix() const
Produce an eigen Matrix.