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 // This module CAN NOT be run in parallel!
33
34 //Parameter definition
35 B2DEBUG(1, "DQMHistAnalysisRooFitExample: Constructor done.");
36}
37
39{
40}
41
43{
44 B2INFO("DQMHistAnalysisRooFitExample: initialized.");
45
46 registerEpicsPV("example/fit_value", "fit_value"); // in real usage, add a proper prefix
47
48 w = new RooWorkspace("w");
49 w->factory("Gaussian::f(x[-20,20],mean[0,-5,5],sigma[3,1,10])");
50 model = w->pdf("f");
51
52 m_c0 = new TCanvas("example0");
53}
54
55
57{
58 B2INFO("DQMHistAnalysisRooFitExample: beginRun called.");
59 m_c0->Clear();
60
61 TH1* hh1 = findHist("FirstDet/h_HitXPositionCh01");
62 if (hh1 != NULL) {
63
64 //RooDataHist::RooDataHist(const char *name, const char *title, const RooArgList& vars, const TH1* hist, Double_t wgt) :
65 x = w->var("x");
66 data = new RooDataHist("data", "dataset with x", *x, (const TH1*) hh1);
67 plot = x->frame();
68 r = model->fitTo(*data);
69
70 // plot data and function
71
72 data->plotOn(plot);
73 model->plotOn(plot);
74
75 m_c0->cd();
76 plot->Draw();
77 } else {
78 B2WARNING("Histo not there ...");
79 }
80
81}
82
84{
85 TH1* hh1;
86
87 hh1 = findHist("FirstDet/h_HitXPositionCh01");
88
89 if (hh1 != NULL) {
90 if (data) delete data;
91
92 data = new RooDataHist("data", "dataset with x", *(w->var("x")), hh1);
93 data->Print();
94 r = model->fitTo(*data);
95 data->Print();
96
97// RooPlot *m_plot = x->frame();
98 plot = x->frame();
99
100 // while(plot->numItems()>0){
101 // plot->remove("",false);
102 // }
103 // printf("5\n");
104
105 data->plotOn(plot);
106 model->plotOn(plot);
107
108 m_c0->cd();
109 plot->Draw();
110
111 m_c0->Modified();
112 m_c0->Update();
113
114 }
115
116 double fitdata = 0;
117 setEpicsPV("fit_value", fitdata);
118}
119
121{
122 B2INFO("DQMHistAnalysisRooFitExample: endRun called");
123}
124
125
127{
128 B2INFO("DQMHistAnalysisRooFitExample: terminate called");
129}
130
The base class for the histogram analysis module.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
void setEpicsPV(std::string keyname, double value)
Write value to a EPICS PV.
int registerEpicsPV(std::string pvname, std::string keyname="")
EPICS related Functions.
void terminate(void) override final
This method is called at the end of the event processing.
void initialize(void) override final
Initializer.
RooDataHist * data
The data of histogram for fitting.
void endRun(void) override final
This method is called if the current run ends.
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.
#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.