GooFit  v2.1.3
convolution.cpp
Go to the documentation of this file.
1 #include <goofit/Application.h>
2 #include <goofit/FitManager.h>
7 #include <goofit/Variable.h>
8 
9 #include <TCanvas.h>
10 #include <TFile.h>
11 #include <TH1.h>
12 #include <TRandom.h>
13 
14 using namespace std;
15 using namespace GooFit;
16 
17 double cpu_bw(double x, double x0, double gamma) {
18  double ret = gamma;
19  ret /= (2 * sqrt(M_PI));
20  ret /= ((x - x0) * (x - x0) + 0.25 * gamma * gamma);
21  return ret;
22 }
23 
24 int main(int argc, char **argv) {
25  GooFit::Application app("Convolution example", argc, argv);
26 
27  GOOFIT_PARSE(app);
28 
29  // Independent variable.
30  Observable xvar{"xvar", -10, 10};
31 
32  Variable gamma{"gamma", 2, 0.1, 0.1, 5};
33  Variable sigma{"sigma", 1.5, 0.1, 0.1, 5};
34  Variable x0{"x0", 0.2, 0.05, -1, 1};
35  Variable zero{"zero", 0};
36 
37  TRandom donram(42);
38  // Data set
39  UnbinnedDataSet data(xvar);
40 
41  // Generate toy events.
42  for(int i = 0; i < 100000; ++i) {
43  xvar.setValue(donram.Uniform(20) - 10);
44 
45  double bwvalue = cpu_bw(xvar.getValue(), x0.getValue(), gamma.getValue());
46  double roll = donram.Uniform() * (2.0 / (sqrt(M_PI) * gamma.getValue()));
47 
48  if(roll > bwvalue) {
49  --i;
50  continue;
51  }
52 
53  xvar.setValue(xvar.getValue() + donram.Gaus(0, sigma.getValue()));
54 
55  if((xvar.getValue() < xvar.getLowerLimit()) || (xvar.getValue() > xvar.getUpperLimit())) {
56  --i;
57  continue;
58  }
59 
60  data.addEvent();
61  }
62 
63  BWPdf breit{"breit", xvar, x0, gamma};
64  GaussianPdf gauss{"gauss", xvar, zero, sigma};
65  ConvolutionPdf convolution{"convolution", xvar, &breit, &gauss};
66  convolution.setData(&data);
67 
68  FitManager fitter(&convolution);
69  fitter.fit();
70 
71  TFile f("output.root", "RECREATE");
72  auto toroot = convolution.plotToROOT(xvar);
73  toroot->Write();
74  f.Write();
75 
76  return fitter;
77 }
Special class for observables. Used in DataSets.
Definition: Variable.h:109
int main(int argc, char **argv)
Definition: convolution.cpp:24
UnbinnedDataSet * data
__host__ void setData(DataSet *data)
Observable * sigma
fptype getValue() const
Get the value.
Definition: Variable.h:68
ROOT::Minuit2::FunctionMinimum fit()
This runs the fit.
#define GOOFIT_PARSE(app,...)
Definition: Application.h:11
double cpu_bw(double x, double x0, double gamma)
Definition: convolution.cpp:17