Belle II Software  release-08-01-10
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 
17 using namespace std;
18 using namespace Belle2;
19 
20 //-----------------------------------------------------------------
21 // Register the Module
22 //-----------------------------------------------------------------
23 REG_MODULE(DQMHistAnalysisRooFitExample);
24 
25 //-----------------------------------------------------------------
26 // Implementation
27 //-----------------------------------------------------------------
28 
29 DQMHistAnalysisRooFitExampleModule::DQMHistAnalysisRooFitExampleModule()
31 {
32  // This module CAN NOT be run in parallel!
33 
34  //Parameter definition
35  B2DEBUG(1, "DQMHistAnalysisRooFitExample: Constructor done.");
36 }
37 
39 {
40 #ifdef _BELLE2_EPICS
41  if (ca_current_context()) ca_context_destroy();
42 #endif
43 }
44 
46 {
47  B2INFO("DQMHistAnalysisRooFitExample: initialized.");
48 
49 #ifdef _BELLE2_EPICS
50  if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback), "ca_context_create");
51  SEVCHK(ca_create_channel("fit_value", NULL, NULL, 10, &mychid), "ca_create_channel failure");
52  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
53 #endif
54 
55  w = new RooWorkspace("w");
56  w->factory("Gaussian::f(x[-20,20],mean[0,-5,5],sigma[3,1,10])");
57  model = w->pdf("f");
58 
59  m_c0 = new TCanvas("example0");
60 }
61 
62 
64 {
65  //m_serv->SetTimer(100, kFALSE);
66  B2INFO("DQMHistAnalysisRooFitExample: beginRun called.");
67  m_c0->Clear();
68 
69  TH1* hh1 = findHist("FirstDet/h_HitXPositionCh01");
70  if (hh1 != NULL) {
71 
72  //RooDataHist::RooDataHist(const char *name, const char *title, const RooArgList& vars, const TH1* hist, Double_t wgt) :
73  x = w->var("x");
74  data = new RooDataHist("data", "dataset with x", *x, (const TH1*) hh1);
75  plot = x->frame();
76  r = model->fitTo(*data);
77 
78  // plot data and function
79 
80  data->plotOn(plot);
81  model->plotOn(plot);
82 
83  m_c0->cd();
84  plot->Draw();
85  } else {
86  B2FATAL("Histo now there ... -> zero pointer crash");
87  }
88 
89 }
90 
92 {
93  TH1* hh1;
94 
95  printf("0\n");
96  hh1 = findHist("FirstDet/h_HitXPositionCh01");
97 
98  if (hh1 != NULL) {
99  if (data) delete data;
100 
101  data = new RooDataHist("data", "dataset with x", *(w->var("x")), hh1);
102  data->Print();
103  r = model->fitTo(*data);
104  data->Print();
105 
106 // RooPlot *m_plot = x->frame();
107  plot = x->frame();
108 
109  // while(plot->numItems()>0){
110  // plot->remove("",false);
111  // }
112  // printf("5\n");
113 
114  data->plotOn(plot);
115  model->plotOn(plot);
116 
117  m_c0->cd();
118  plot->Draw();
119 
120  m_c0->Modified();
121  m_c0->Update();
122 
123  }
124 
125 #ifdef _BELLE2_EPICS
126  double fitdata = 0;
127 
128  SEVCHK(ca_put(DBR_DOUBLE, mychid, (void*)&fitdata), "ca_set failure");
129  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
130 #endif
131 }
132 
134 {
135  B2INFO("DQMHistAnalysisRooFitExample: endRun called");
136 }
137 
138 
140 {
141 #ifdef _BELLE2_EPICS
142  SEVCHK(ca_clear_channel(mychid), "ca_clear_channel failure");
143  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
144 #endif
145  B2INFO("DQMHistAnalysisRooFitExample: terminate called");
146 }
147 
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 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:650
Abstract base class for different kinds of events.