Belle II Software development
DQMHistAnalysisRootFitExample.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8//+
9// File : DQMHistAnalysisRooFitExample.cc
10// Description : DQM Histogram analysis module, using roofit to fit the histogram
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisRootFitExample.h>
15#include <RooRealVar.h>
16
17using namespace std;
18using namespace Belle2;
19
20//-----------------------------------------------------------------
21// Register the Module
22//-----------------------------------------------------------------
23REG_MODULE(DQMHistAnalysisRooFitExample);
24
25//-----------------------------------------------------------------
26// Implementation
27//-----------------------------------------------------------------
28
31{
32 setDescription("DQM histogram analysis example module featuring RooFit");
33 // This module CAN NOT be run in parallel!
34
35 //Parameter definition
36 B2DEBUG(1, "DQMHistAnalysisRooFitExample: Constructor done.");
37}
38
40{
41 B2INFO("DQMHistAnalysisRooFitExample: initialized.");
42
43 registerEpicsPV("example/fit_value", "fit_value"); // in real usage, add a proper prefix
44
45 w = new RooWorkspace("w");
46 w->factory("Gaussian::f(x[-20,20],mean[0,-5,5],sigma[3,1,10])");
47 model = w->pdf("f");
48
49 m_c0 = new TCanvas("example0");
50}
51
52
54{
55 B2INFO("DQMHistAnalysisRooFitExample: beginRun called.");
56 m_c0->Clear();
57
58 auto hh1 = findHist("FirstDet/h_HitXPositionCh01");
59 if (hh1 != NULL) {
60
61 //RooDataHist::RooDataHist(const char *name, const char *title, const RooArgList& vars, const TH1* hist, Double_t wgt) :
62 x = w->var("x");
63 data = new RooDataHist("data", "dataset with x", *x, hh1);
64 plot = x->frame();
65 r = model->fitTo(*data);
66
67 // plot data and function
68
69 data->plotOn(plot);
70 model->plotOn(plot);
71
72 m_c0->cd();
73 plot->Draw();
74 } else {
75 B2WARNING("Histo not there ...");
76 }
77
78}
79
81{
82 TH1* hh1;
83
84 hh1 = findHist("FirstDet/h_HitXPositionCh01");
85
86 if (hh1 != NULL) {
87 if (data) delete data;
88
89 data = new RooDataHist("data", "dataset with x", *(w->var("x")), hh1);
90 data->Print();
91 r = model->fitTo(*data);
92 data->Print();
93
94// RooPlot *m_plot = x->frame();
95 plot = x->frame();
96
97 // while(plot->numItems()>0){
98 // plot->remove("",false);
99 // }
100 // printf("5\n");
101
102 data->plotOn(plot);
103 model->plotOn(plot);
104
105 m_c0->cd();
106 plot->Draw();
107
108 m_c0->Modified();
109 m_c0->Update();
110
111 }
112
113 double fitdata = 0;
114 setEpicsPV("fit_value", fitdata);
115}
116
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
DQMHistAnalysisModule()
Constructor / Destructor.
void setEpicsPV(const std::string &keyname, double value)
Write value to a EPICS PV.
static TH1 * findHist(const std::string &dirname, const std::string &histname="", bool onlyIfUpdated=false)
Find histogram.
RooDataHist * data
The data of histogram for fitting.
TCanvas * m_c0
The drawing canvas for plotting the fitting result.
RooPlot * plot
The plot of the fitting result.
void beginRun(void) override final
Called when entering a new run.
void event(void) override final
This method is called for each event.
void setDescription(const std::string &description)
Sets the description of the module.
Definition Module.cc:214
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
Abstract base class for different kinds of events.
STL namespace.