GooFit  v2.1.3
PdfBase.h
Go to the documentation of this file.
1 #pragma once
2 
4 
5 #include <goofit/Variable.h>
6 #include <goofit/Version.h>
7 #include <goofit/detail/Abort.h>
8 #include <goofit/detail/Macros.h>
9 
10 #include <algorithm>
11 #include <map>
12 #include <set>
13 #include <vector>
14 
15 #ifdef ROOT_FOUND
16 class TH1D;
17 #endif
18 
19 namespace ROOT {
20 namespace Minuit2 {
21 class FunctionMinimum;
22 }
23 } // namespace ROOT
24 
25 namespace GooFit {
26 
27 /* Future use, apperently:
28 #include <thrust/device_vector.h>
29 #include <thrust/iterator/constant_iterator.h>
30 
31 typedef thrust::counting_iterator<int> IndexIterator;
32 typedef thrust::constant_iterator<fptype*> DataIterator;
33 typedef thrust::constant_iterator<int> SizeIterator;
34 typedef thrust::tuple<IndexIterator, DataIterator, SizeIterator> EventTuple;
35 typedef thrust::zip_iterator<EventTuple> EventIterator;
36 */
37 
38 const int maxParams = GOOFIT_MAXPAR;
39 extern fptype *dev_event_array;
42 extern unsigned int host_indices[maxParams];
43 extern int totalParams;
44 extern int totalConstants;
45 
46 class FitControl;
47 
48 class DataSet;
49 class BinnedDataSet;
50 class UnbinnedDataSet;
51 
52 namespace {
54 void filter_arguments(std::vector<Observable> &oblist) {}
55 
56 template <typename... Args>
57 void filter_arguments(std::vector<Observable> &oblist, const Observable &obs, Args... args) {
58  oblist.push_back(obs);
59  return filter_arguments(oblist, args...);
60 }
61 
62 template <typename... Args>
63 void filter_arguments(std::vector<Observable> &oblist, const EventNumber &obs, Args... args) {
64  oblist.push_back(obs);
65  return filter_arguments(oblist, args...);
66 }
67 } // namespace
68 
69 class PdfBase {
70  public:
71  template <typename... Args>
72  explicit PdfBase(std::string n, Args... args)
73  : name(std::move(n)) {
74  std::vector<Observable> obs;
75  filter_arguments(obs, args...);
76  for(auto &ob : obs)
77  registerObservable(ob);
78  }
79 
80  virtual ~PdfBase() = default;
81 
82  enum Specials { ForceSeparateNorm = 1, ForceCommonNorm = 2 };
83 
84  __host__ virtual double calculateNLL() const = 0;
85  __host__ virtual fptype normalize() const = 0;
86  __host__ void initializeIndices(std::vector<unsigned int> pindices);
87 
88  __host__ void addSpecialMask(int m) { specialMask |= m; }
89  __host__ void copyParams(const std::vector<double> &pars) const;
90  __host__ void copyParams();
91  __host__ void copyNormFactors() const;
92  __host__ void generateNormRange();
93  __host__ std::string getName() const { return name; }
94 
95  __host__ virtual std::vector<Observable> getObservables() const;
96  __host__ virtual std::vector<Variable> getParameters() const;
97 
98  __host__ Variable *getParameterByName(std::string n);
99  __host__ int getSpecialMask() const { return specialMask; }
100 
101  __host__ void setData(DataSet *data);
102  __host__ DataSet *getData();
103 
104  __host__ virtual void setFitControl(std::shared_ptr<FitControl>) = 0;
105  __host__ virtual bool hasAnalyticIntegral() const { return false; }
106 
108  __host__ ROOT::Minuit2::FunctionMinimum fitTo(DataSet *data, int verbosity = 3);
109 
110  __host__ unsigned int getFunctionIndex() const { return functionIdx; }
111  __host__ unsigned int getParameterIndex() const { return parameters; }
112  __host__ unsigned int registerParameter(Variable var);
113  __host__ unsigned int registerConstants(unsigned int amount);
114  __host__ virtual void recursiveSetNormalisation(fptype norm = 1) const;
115  __host__ void unregisterParameter(Variable var);
116  __host__ void registerObservable(Observable obs);
117  __host__ void setIntegrationFineness(int i);
118  __host__ void printProfileInfo(bool topLevel = true);
119 
120  __host__ bool parametersChanged() const;
121 
122  __host__ void checkInitStatus(std::vector<std::string> &unInited) const;
123  void clearCurrentFit();
124  __host__ void SigGenSetIndices() { setIndices(); }
125 
126  protected:
127  DataSet *data_ = nullptr; //< Remember the original dataset
128  fptype numEvents{0}; //< Non-integer to allow weighted events
129  unsigned int numEntries{0}; //< Eg number of bins - not always the same as number of events, although it can be.
130  fptype *normRanges{
131  nullptr}; //< This is specific to functor instead of variable so that MetricTaker::operator needn't use indices.
132  unsigned int parameters{0}; //< Stores index, in 'paramIndices', where this functor's information begins.
133  unsigned int cIndex{1}; //< Stores location of constants.
134  std::vector<Observable> observables;
135  std::vector<Variable> parameterList;
136  std::shared_ptr<FitControl> fitControl;
137  std::vector<PdfBase *> components;
138  int integrationBins{-1};
139  int specialMask{0}; //< For storing information unique to PDFs, eg "Normalize me separately" for TddpPdf.
140  bool properlyInitialised{true}; //< Allows checking for required extra steps in, eg, Tddp and Convolution.
141 
142  unsigned int functionIdx{0}; //< Stores index of device function pointer.
143 
144  int m_iEventsPerTask{0};
145 
147  void setNumPerTask(PdfBase *p, const int &c);
148 
149  private:
150  std::string name;
151 
152  __host__ void recursiveSetIndices();
153  __host__ void setIndices();
154 };
155 
156 } // namespace GooFit
__host__ unsigned int getParameterIndex() const
Definition: PdfBase.h:111
void normalize(TH1F *dat)
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
Definition: PdfBase.h:19
__host__ void SigGenSetIndices()
Definition: PdfBase.h:124
std::vector< PdfBase * > components
Definition: PdfBase.h:137
Special class for observables. Used in DataSets.
Definition: Variable.h:109
__host__ unsigned int getFunctionIndex() const
Definition: PdfBase.h:110
UnbinnedDataSet * data
virtual __host__ bool hasAnalyticIntegral() const
Definition: PdfBase.h:105
std::vector< Variable > parameterList
Definition: PdfBase.h:135
TH1D * getData(DataSet *data, Observable var, std::string filename, size_t reduce=1, std::string keyw="data_hist")
Definition: zachFit.cpp:32
std::shared_ptr< FitControl > fitControl
Definition: PdfBase.h:136
PdfBase(std::string n, Args... args)
Definition: PdfBase.h:72
const int maxParams
Definition: PdfBase.h:38
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
__host__ void addSpecialMask(int m)
Definition: PdfBase.h:88
__host__ int getSpecialMask() const
Definition: PdfBase.h:99
fptype host_params[maxParams]
Definition: PdfBase.cpp:30