GooFit  v2.1.3
Functions
addition.cpp File Reference
#include <goofit/Application.h>
#include <goofit/FitManager.h>
#include <goofit/PDFs/basic/GaussianPdf.h>
#include <goofit/PDFs/basic/PolynomialPdf.h>
#include <goofit/PDFs/combine/AddPdf.h>
#include <goofit/UnbinnedDataSet.h>
#include <goofit/Variable.h>
#include <TCanvas.h>
#include <TH1F.h>
#include <TH2F.h>
#include <TRandom.h>
#include <TStyle.h>
#include <iostream>
#include <sys/time.h>
#include <sys/times.h>

Go to the source code of this file.

Functions

int main (int argc, char **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 22 of file addition.cpp.

References GooFit::UnbinnedDataSet::addEvent(), data, GooFit::FitManagerMinuit2::fit(), foo, GooFit::DataSet::getNumEvents(), GOOFIT_PARSE, GooFit::UnbinnedDataSet::loadEvent(), and GooFit::PdfBase::setData().

22  {
23  GooFit::Application app("Addition example", argc, argv);
24 
25  GOOFIT_PARSE(app);
26 
27  gStyle->SetCanvasBorderMode(0);
28  gStyle->SetCanvasColor(10);
29  gStyle->SetFrameFillColor(10);
30  gStyle->SetFrameBorderMode(0);
31  gStyle->SetPadColor(0);
32  gStyle->SetTitleColor(1);
33  gStyle->SetStatColor(0);
34  gStyle->SetFillColor(0);
35  gStyle->SetFuncWidth(1);
36  gStyle->SetLineWidth(1);
37  gStyle->SetLineColor(1);
38  gStyle->SetPalette(1, 0);
39 
40  Observable xvar{"xvar", -5, 5};
41 
42  UnbinnedDataSet data(xvar);
43 
44  TH1F xvarHist("xvarHist", "", xvar.getNumBins(), xvar.getLowerLimit(), xvar.getUpperLimit());
45 
46  xvarHist.SetStats(false);
47 
48  TRandom donram(42);
49  double totalData = 0;
50 
51  for(int i = 0; i < 100000; ++i) {
52  xvar.setValue(donram.Gaus(0.2, 1.1));
53 
54  if(donram.Uniform() < 0.1)
55  xvar.setValue(donram.Uniform(xvar.getLowerLimit(), xvar.getUpperLimit()));
56 
57  if(fabs(xvar.getValue()) > 5) {
58  --i;
59  continue;
60  }
61 
62  data.addEvent();
63  xvarHist.Fill(xvar.getValue());
64  totalData++;
65  }
66 
67  Variable xmean{"xmean", 0, 1, -10, 10};
68  Variable xsigm{"xsigm", 1, 0.5, 1.5};
69  GaussianPdf signal{"signal", xvar, xmean, xsigm};
70 
71  Variable constant{"constant", 1.0};
72  PolynomialPdf backgr{"backgr", xvar, {constant}};
73 
74  Variable sigFrac{"sigFrac", 0.9, 0.75, 1.00};
75 
76  AddPdf total{"total", {sigFrac}, {&signal, &backgr}};
77 
78  total.setData(&data);
79  FitManager fitter(&total);
80  fitter.fit();
81 
82  TH1F pdfHist("pdfHist", "", xvar.getNumBins(), xvar.getLowerLimit(), xvar.getUpperLimit());
83  TH1F sigHist("sigHist", "", xvar.getNumBins(), xvar.getLowerLimit(), xvar.getUpperLimit());
84  TH1F bkgHist("bkgHist", "", xvar.getNumBins(), xvar.getLowerLimit(), xvar.getUpperLimit());
85 
86  pdfHist.SetStats(false);
87  sigHist.SetStats(false);
88  bkgHist.SetStats(false);
89 
90  UnbinnedDataSet grid(xvar);
91 
92  for(int i = 0; i < xvar.getNumBins(); ++i) {
93  double step = (xvar.getUpperLimit() - xvar.getLowerLimit()) / xvar.getNumBins();
94  xvar.setValue(xvar.getLowerLimit() + (i + 0.5) * step);
95  grid.addEvent();
96  }
97 
98  total.setData(&grid);
99  std::vector<std::vector<double>> pdfVals = total.getCompProbsAtDataPoints();
100 
101  TCanvas foo;
102 
103  double totalPdf = 0;
104 
105  for(int i = 0; i < grid.getNumEvents(); ++i) {
106  grid.loadEvent(i);
107  pdfHist.Fill(xvar.getValue(), pdfVals[0][i]);
108  sigHist.Fill(xvar.getValue(), pdfVals[1][i]);
109  bkgHist.Fill(xvar.getValue(), pdfVals[2][i]);
110  totalPdf += pdfVals[0][i];
111  }
112 
113  for(int i = 0; i < xvar.getNumBins(); ++i) {
114  double val = pdfHist.GetBinContent(i + 1);
115  val /= totalPdf;
116  val *= totalData;
117  pdfHist.SetBinContent(i + 1, val);
118  val = sigHist.GetBinContent(i + 1);
119  val /= totalPdf;
120  val *= sigFrac.getValue();
121  val *= totalData;
122  sigHist.SetBinContent(i + 1, val);
123  val = bkgHist.GetBinContent(i + 1);
124  val /= totalPdf;
125  val *= (1.0 - sigFrac.getValue());
126  val *= totalData;
127  bkgHist.SetBinContent(i + 1, val);
128  }
129 
130  xvarHist.SetMarkerStyle(8);
131  xvarHist.SetMarkerSize(0.5);
132  xvarHist.Draw("p");
133  pdfHist.SetLineColor(kBlue);
134  pdfHist.SetLineWidth(3);
135  pdfHist.Draw("lsame");
136  sigHist.SetLineColor(kBlue);
137  sigHist.SetLineStyle(kDashed);
138  sigHist.SetLineWidth(3);
139  sigHist.Draw("lsame");
140  bkgHist.SetLineColor(kRed);
141  bkgHist.SetLineWidth(3);
142  bkgHist.Draw("lsame");
143  foo.SaveAs("xhist.png");
144 
145  return fitter;
146 }
Special class for observables. Used in DataSets.
Definition: Variable.h:109
UnbinnedDataSet * data
__host__ void setData(DataSet *data)
TCanvas foo
Definition: chisquare.cpp:22
#define GOOFIT_PARSE(app,...)
Definition: Application.h:11