GooFit  v2.1.3
PdfBase.cpp
Go to the documentation of this file.
1 #include <goofit/Color.h>
2 #include <goofit/Error.h>
4 #include <goofit/Log.h>
5 #include <goofit/PdfBase.h>
6 #include <goofit/Variable.h>
7 
8 #include <algorithm>
9 #include <set>
10 #include <utility>
11 
12 #include <goofit/BinnedDataSet.h>
13 #include <goofit/FitManager.h>
14 #include <goofit/UnbinnedDataSet.h>
15 
16 #include <Minuit2/FunctionMinimum.h>
17 
18 namespace {
19 
20 template <typename T>
21 bool find_in(std::vector<T> list, T item) {
22  return std::find_if(std::begin(list), std::end(list), [item](T p) { return p == item; }) != std::end(list);
23 }
24 } // namespace
25 
26 namespace GooFit {
27 
31 unsigned int host_indices[maxParams];
32 
34 int totalParams = 0;
35 int totalConstants = 1; // First constant is reserved for number of events.
36 
37 __host__ void PdfBase::checkInitStatus(std::vector<std::string> &unInited) const {
39  unInited.push_back(getName());
40 
41  for(auto component : components) {
42  component->checkInitStatus(unInited);
43  }
44 }
45 
46 __host__ void PdfBase::recursiveSetNormalisation(fptype norm) const {
47  host_normalisation[parameters] = norm;
48 
49  for(auto component : components) {
50  component->recursiveSetNormalisation(norm);
51  }
52 }
53 
54 __host__ unsigned int PdfBase::registerParameter(Variable var) {
55  static int unique_param = 0;
56 
57  if(find_in(parameterList, var))
58  return static_cast<unsigned int>(var.getIndex());
59 
60  if(var.getIndex() < 0) {
61  GOOFIT_DEBUG("{}: Registering p:{} for {}", getName(), unique_param, var.getName());
62  var.setIndex(unique_param++);
63  }
64 
65  parameterList.push_back(var);
66  return static_cast<unsigned int>(var.getIndex());
67 }
68 
70  GOOFIT_DEBUG("{}: Removing {}", getName(), var.getName());
71 
72  for(PdfBase *comp : components) {
73  comp->unregisterParameter(var);
74  }
75 
76  var.setIndex(-1);
77  // Once copies are used, this might able to be unregistred from a lower PDF only
78  // For now, it gets completely cleared.
79 }
80 
81 __host__ std::vector<Variable> PdfBase::getParameters() const {
82  std::vector<Variable> ret;
83  for(const Variable &param : parameterList)
84  ret.push_back(param);
85 
86  for(const PdfBase *comp : components) {
87  for(const Variable &sub_comp : comp->getParameters())
88  if(!find_in(ret, sub_comp))
89  ret.push_back(sub_comp);
90  }
91 
92  return ret;
93 }
94 
95 __host__ Variable *PdfBase::getParameterByName(std::string n) {
96  for(Variable &p : parameterList) {
97  if(p.getName() == n)
98  return &p;
99  }
100 
101  for(auto component : components) {
102  Variable *cand = component->getParameterByName(n);
103 
104  if(cand != nullptr)
105  return cand;
106  }
107 
108  return nullptr;
109 }
110 
111 __host__ std::vector<Observable> PdfBase::getObservables() const {
112  std::vector<Observable> ret;
113  for(const Observable &obs : observables)
114  ret.push_back(obs);
115 
116  for(const PdfBase *comp : components) {
117  for(const Observable &sub_comp : comp->getObservables())
118  if(!find_in(ret, sub_comp))
119  ret.push_back(sub_comp);
120  }
121 
122  return ret;
123 }
124 
125 __host__ unsigned int PdfBase::registerConstants(unsigned int amount) {
126  if(totalConstants + amount >= maxParams)
127  throw GooFit::GeneralError(
128  "totalConstants {} + amount {} can not be more than {}", totalConstants, amount, maxParams);
130  totalConstants += amount;
131  return cIndex;
132 }
133 
135  if(find_in(observables, obs))
136  return;
137 
138  GOOFIT_DEBUG("{}: Registering o:{} for {}", getName(), observables.size(), obs.getName());
139  observables.push_back(obs);
140 }
141 
142 __host__ void PdfBase::setIntegrationFineness(int i) {
143  integrationBins = i;
145 }
146 
147 __host__ bool PdfBase::parametersChanged() const {
148  return std::any_of(
149  std::begin(parameterList), std::end(parameterList), [](const Variable &v) { return v.getChanged(); });
150 }
151 
152 __host__ void PdfBase::setNumPerTask(PdfBase *p, const int &c) {
153  if(p == nullptr)
154  return;
155 
156  m_iEventsPerTask = c;
157 }
158 
159 __host__ ROOT::Minuit2::FunctionMinimum PdfBase::fitTo(DataSet *data, int verbosity) {
160  setData(data);
161  FitManager fitter{this};
162  fitter.setVerbosity(verbosity);
163  return fitter.fit();
164 }
165 } // namespace GooFit
Thrown when a general error is encountered.
Definition: Error.h:10
__host__ void unregisterParameter(Variable var)
Definition: PdfBase.cpp:69
double fptype
unsigned int host_indices[maxParams]
Definition: PdfBase.cpp:31
int totalConstants
Definition: PdfBase.cpp:35
std::vector< Observable > observables
Definition: PdfBase.h:134
__host__ Variable * getParameterByName(std::string n)
Definition: PdfBase.cpp:95
__host__ void checkInitStatus(std::vector< std::string > &unInited) const
Definition: PdfBase.cpp:37
__host__ void setIntegrationFineness(int i)
Definition: PdfBase.cpp:142
void setIndex(int value)
Set the GooFit index.
Definition: Variable.h:60
int integrationBins
Definition: PdfBase.h:138
bool getChanged() const
Check to see if the value has changed this iteration (always true the first time) ...
Definition: Variable.h:199
std::vector< PdfBase * > components
Definition: PdfBase.h:137
__host__ unsigned int registerConstants(unsigned int amount)
Definition: PdfBase.cpp:125
Special class for observables. Used in DataSets.
Definition: Variable.h:109
unsigned int parameters
Definition: PdfBase.h:132
void setNumPerTask(PdfBase *p, const int &c)
This needs to be set before a call to setData.
Definition: PdfBase.cpp:152
__host__ void registerObservable(Observable obs)
Definition: PdfBase.cpp:134
UnbinnedDataSet * data
__host__ void generateNormRange()
void setVerbosity(int value)
Set the fitting verbosity.
__host__ void setData(DataSet *data)
std::vector< Variable > parameterList
Definition: PdfBase.h:135
__host__ bool parametersChanged() const
Definition: PdfBase.cpp:147
#define GOOFIT_DEBUG(...)
Definition: Log.h:96
unsigned int cIndex
Definition: PdfBase.h:133
virtual __host__ std::vector< Observable > getObservables() const
Definition: PdfBase.cpp:111
int getIndex() const
Get the GooFit index.
Definition: Variable.h:58
__host__ ROOT::Minuit2::FunctionMinimum fitTo(DataSet *data, int verbosity=3)
RooFit style fitting shortcut.
Definition: PdfBase.cpp:159
bool properlyInitialised
Definition: PdfBase.h:140
int m_iEventsPerTask
Definition: PdfBase.h:144
const int maxParams
Definition: PdfBase.h:38
virtual __host__ void recursiveSetNormalisation(fptype norm=1) const
Definition: PdfBase.cpp:46
__host__ unsigned int registerParameter(Variable var)
Definition: PdfBase.cpp:54
fptype host_normalisation[maxParams]
Definition: PdfBase.cpp:29
fptype * dev_event_array
Definition: PdfBase.cpp:28
int totalParams
Definition: PdfBase.cpp:34
__host__ std::string getName() const
Definition: PdfBase.h:93
int host_callnumber
Definition: PdfBase.cpp:33
fptype host_params[maxParams]
Definition: PdfBase.cpp:30
virtual __host__ std::vector< Variable > getParameters() const
Definition: PdfBase.cpp:81
const std::string & getName() const
Get the name.
Definition: Variable.h:63