GooFit  v2.1.3
Variable.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <limits>
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
10 #include <goofit/Log.h>
11 
12 // Declaring friends
13 
14 namespace GooFit {
15 
16 class Params;
17 class Minuit1;
18 
22 class Indexable {
23  protected:
25  std::string name;
26 
28  std::shared_ptr<fptype> value;
29 
31  std::shared_ptr<fptype> lowerlimit{std::make_shared<fptype>(0)};
32 
34  std::shared_ptr<fptype> upperlimit{std::make_shared<fptype>(0)};
35 
37  std::shared_ptr<int> index{std::make_shared<int>(-1)};
38 
39  public:
40  Indexable(std::string n, fptype val = 0)
41  : name(n)
42  , value(std::make_shared<fptype>(val)) {}
43 
45  : name(n)
46  , value(std::make_shared<fptype>(val))
47  , lowerlimit(std::make_shared<fptype>(lowerlimit))
48  , upperlimit(std::make_shared<fptype>(upperlimit)) {}
49 
50  Indexable &operator=(const Indexable &) = delete;
51 
52  virtual ~Indexable() {
53  if(value.use_count() == 1)
54  GOOFIT_DEBUG("Destroying Indexable: {}", name);
55  }
56 
58  int getIndex() const { return *index; }
60  void setIndex(int value) { *index = value; }
61 
63  const std::string &getName() const { return name; }
64  // The name cannot be changed
65  // void setName(const std::string &val) { name = val; }
66 
68  fptype getValue() const { return *value; }
70  void setValue(fptype val) { *value = val; }
71 
73  fptype getUpperLimit() const { return *upperlimit; }
75  void setUpperLimit(fptype val) { *upperlimit = val; }
76 
78  fptype getLowerLimit() const { return *lowerlimit; }
80  void setLowerLimit(fptype val) { *lowerlimit = val; }
81 
83  const Indexable &operator=(const fptype &val) {
84  setValue(val);
85  return *this;
86  }
87 
88  // Utilities
89 
91  operator fptype() const { return getValue(); }
92 
94  bool operator<(const Indexable &other) const { return value.get() < other.value.get(); }
95 
97  bool operator==(const Indexable &other) const { return value.get() == other.value.get(); }
98 
100  bool operator!=(const Indexable &other) const { return value.get() != other.value.get(); }
101 
103  operator bool() const {
104  return getUpperLimit() == getLowerLimit() || (getValue() <= getUpperLimit() && getValue() >= getLowerLimit());
105  }
106 };
107 
109 class Observable : public Indexable {
110  friend std::istream &operator>>(std::istream &o, Observable &var);
111 
112  protected:
114  std::shared_ptr<size_t> numbins{std::make_shared<size_t>(100)};
115 
117  bool is_event_number{false};
118 
119  public:
121  : GooFit::Indexable(name, 0, lowerlimit, upperlimit) {}
122 
123  ~Observable() override = default;
124 
125  Observable &operator=(const Observable &) = delete;
126 
128  void setNumBins(size_t num) { *numbins = num; }
130  size_t getNumBins() const { return *numbins; }
131 
133  fptype getBinSize() const { return (getUpperLimit() - getLowerLimit()) / getNumBins(); }
134 
136  bool isEventNumber() const { return is_event_number; }
137 
139  const Observable &operator=(const fptype &val) {
140  setValue(val);
141  return *this;
142  }
143 };
144 
149 class Variable : public Indexable {
150  friend std::ostream &operator<<(std::ostream &o, const Variable &var);
151 
152  public:
154  class Key {
155  friend Params;
156  friend Minuit1;
157 
159  Key() = default;
160  };
161 
162  // These classes can now be duplicated safely. No pointers needed.
163  Variable(const Variable &) = default;
164  Variable(Variable &&) = default;
165 
167  Variable(std::string n, fptype v)
168  : Indexable(n, v, v + 0.01, v - 0.01)
169  , error(std::make_shared<fptype>(0.002)) {
170  *fixed = true;
171  }
172 
174  Variable(std::string n, fptype v, fptype dn, fptype up)
175  : Indexable(n, v, dn, up)
176  , error(std::make_shared<fptype>(0.1 * (up - dn))) {}
177 
179  Variable(std::string n, fptype v, fptype e, fptype dn, fptype up, bool fix = false)
180  : Indexable(n, v, dn, up)
181  , error(std::make_shared<fptype>(e))
182  , fixed(std::make_shared<bool>(fix)) {}
183 
184  ~Variable() override = default;
185 
186  Variable &operator=(const Variable &) = delete;
187 
189  fptype getError() const { return *error; }
191  void setError(fptype val) { *error = val; }
192 
194  int getFitterIndex() const { return *fitter_index; }
196  void setFitterIndex(int value) { *fitter_index = value; }
197 
199  bool getChanged() const { return *changed_; }
200 
202  bool IsFixed() const { return *fixed; }
203 
205  void setFixed(bool fix) { *fixed = fix; }
206 
208  void setChanged(bool val = true) { *changed_ = val; }
209 
211  void setBlind(fptype val) { *blind = val; }
212 
214  fptype getBlind(const Key &) { return *blind; }
215 
217  const Variable &operator=(const fptype &val) {
218  setValue(val);
219  return *this;
220  }
221 
222  protected:
224  std::shared_ptr<fptype> error;
225 
227  std::shared_ptr<fptype> blind{std::make_shared<fptype>(0)};
228 
230  std::shared_ptr<bool> changed_{std::make_shared<bool>(true)};
231 
233  std::shared_ptr<bool> fixed{std::make_shared<bool>(false)};
234 
236  std::shared_ptr<int> fitter_index{std::make_shared<int>(-1)};
237 };
238 
242 class EventNumber : public Observable {
243  public:
244  static constexpr fptype maxint{1L << std::numeric_limits<fptype>::digits};
245 
246  EventNumber(std::string name, fptype min = 0, fptype max = EventNumber::maxint)
247  : GooFit::Observable(name, min, max) {
248  is_event_number = true;
249  *value = 0; // Start with 0 event number by default
250  }
251 
252  ~EventNumber() override = default;
253 
254  EventNumber &operator=(const EventNumber &) = delete;
255 
257  const EventNumber &operator=(const fptype &val) {
258  setValue(val);
259  return *this;
260  }
261 };
262 
264 int max_index(const std::vector<Variable> &vars);
265 int max_index(const std::vector<Observable> &vars);
266 
268 int max_fitter_index(const std::vector<Variable> &vars);
269 
271 std::ostream &operator<<(std::ostream &o, const GooFit::Variable &var);
272 
274 std::ostream &operator<<(std::ostream &o, const GooFit::Observable &var);
275 
277 std::istream &operator>>(std::istream &i, GooFit::Observable &var);
278 } // namespace GooFit
bool operator!=(const Indexable &other) const
Support for comparison - only if really the same object, not just the same value. ...
Definition: Variable.h:100
void setFitterIndex(int value)
Set the index (should be done by the fitter)
Definition: Variable.h:196
fptype getBinSize() const
Get the bin size, (upper-lower) / bins.
Definition: Variable.h:133
void setFixed(bool fix)
Set the fixedness of a variable.
Definition: Variable.h:205
double fptype
void setNumBins(size_t num)
Set the number of bins.
Definition: Variable.h:128
bool operator<(const Indexable &other) const
Support for using as key in map - Notice this does NOT sort by index!
Definition: Variable.h:94
bool isEventNumber() const
Check to see if this is an event number.
Definition: Variable.h:136
int getFitterIndex() const
Get the index from the fitter.
Definition: Variable.h:194
const Variable & operator=(const fptype &val)
Support var = 3.
Definition: Variable.h:217
void setIndex(int value)
Set the GooFit index.
Definition: Variable.h:60
const EventNumber & operator=(const fptype &val)
Support var = 3.
Definition: Variable.h:257
bool getChanged() const
Check to see if the value has changed this iteration (always true the first time) ...
Definition: Variable.h:199
std::shared_ptr< fptype > value
The value of the variable.
Definition: Variable.h:28
void setValue(fptype val)
Set the value.
Definition: Variable.h:70
bool operator==(const Indexable &other) const
Support for comparison - only if really the same object, not just the same value. ...
Definition: Variable.h:97
Special class for observables. Used in DataSets.
Definition: Variable.h:109
std::shared_ptr< int > index
The goofit index, -1 if unset.
Definition: Variable.h:37
EventNumber(std::string name, fptype min=0, fptype max=EventNumber::maxint)
Definition: Variable.h:246
virtual ~Indexable()
Definition: Variable.h:52
size_t getNumBins() const
Get the number of bins.
Definition: Variable.h:130
Observable(std::string name, fptype lowerlimit, fptype upperlimit)
Definition: Variable.h:120
std::shared_ptr< fptype > lowerlimit
The lower limit.
Definition: Variable.h:31
void setUpperLimit(fptype val)
Set the upper limit.
Definition: Variable.h:75
std::string name
The variable name. Should be unique.
Definition: Variable.h:25
#define GOOFIT_DEBUG(...)
Definition: Log.h:96
Variable(std::string n, fptype v, fptype dn, fptype up)
This is a normal variable, with value and upper/lower limits.
Definition: Variable.h:174
int max_fitter_index(const std::vector< Variable > &vars)
Get the max fitter index of a variable from a list.
Definition: Variable.cpp:28
const Observable & operator=(const fptype &val)
Support var = 3.
Definition: Variable.h:139
const Indexable & operator=(const fptype &val)
Support var = 3.
Definition: Variable.h:83
void setChanged(bool val=true)
Check to see if this has been changed since last iteration.
Definition: Variable.h:208
int getIndex() const
Get the GooFit index.
Definition: Variable.h:58
fptype getLowerLimit() const
Get the lower limit.
Definition: Variable.h:78
int max_index(const std::vector< Variable > &vars)
Get the max index of a variable from a list.
Definition: Variable.cpp:8
fptype getValue() const
Get the value.
Definition: Variable.h:68
Variable(std::string n, fptype v, fptype e, fptype dn, fptype up, bool fix=false)
This is a full variable with error scale as well (Fix added to allow single interface to fixed/free v...
Definition: Variable.h:179
fptype getUpperLimit() const
Get the upper limit.
Definition: Variable.h:73
Indexable(std::string n, fptype val=0)
Definition: Variable.h:40
void setBlind(fptype val)
Hides the number; the real value is the result minus this value. Cannot be retreived once set...
Definition: Variable.h:211
static constexpr fptype maxint
Definition: Variable.h:244
bool IsFixed() const
Check to see if this is a constant.
Definition: Variable.h:202
std::istream & operator>>(std::istream &i, GooFit::Observable &var)
Allow Observable to be read in.
Definition: Variable.cpp:59
Indexable & operator=(const Indexable &)=delete
std::shared_ptr< fptype > error
The error.
Definition: Variable.h:224
void setError(fptype val)
Set the error.
Definition: Variable.h:191
std::ostream & operator<<(std::ostream &stream, Uncertain value)
Simple << output.
Definition: Uncertain.h:96
Indexable(std::string n, fptype val, fptype lowerlimit, fptype upperlimit)
Definition: Variable.h:44
void setLowerLimit(fptype val)
Set the lower limit.
Definition: Variable.h:80
Variable(std::string n, fptype v)
This is a constant variable.
Definition: Variable.h:167
This provides a key for some special classes to access blind info (passkey)
Definition: Variable.h:154
std::shared_ptr< fptype > upperlimit
The upper limit.
Definition: Variable.h:34
fptype getError() const
Get the error.
Definition: Variable.h:189
fptype getBlind(const Key &)
Protected by special locked key, only a few classes have access to create a Key.
Definition: Variable.h:214
const std::string & getName() const
Get the name.
Definition: Variable.h:63