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