Belle II Software  release-05-02-19
DQMHistAnalysisRootFitExample.cc
1 //+
2 // File : DQMHistAnalysisRooFitExample.cc
3 // Description : DQM Histogram analysis module, using roofit to fit the histogram
4 //
5 // Author : B. Spruck
6 // Date : 25 - Mar - 2017
7 // based on work from Tomoyuki Konno, Tokyo Metropolitan Univerisity
8 //-
9 
10 
11 #include <dqm/analysis/modules/DQMHistAnalysisRootFitExample.h>
12 #include <RooRealVar.h>
13 
14 using namespace std;
15 using namespace Belle2;
16 
17 //-----------------------------------------------------------------
18 // Register the Module
19 //-----------------------------------------------------------------
20 REG_MODULE(DQMHistAnalysisRooFitExample)
21 
22 //-----------------------------------------------------------------
23 // Implementation
24 //-----------------------------------------------------------------
25 
28 {
29  // This module CAN NOT be run in parallel!
30 
31  //Parameter definition
32  B2DEBUG(1, "DQMHistAnalysisRooFitExample: Constructor done.");
33 }
34 
35 DQMHistAnalysisRooFitExampleModule::~DQMHistAnalysisRooFitExampleModule()
36 {
37 #ifdef _BELLE2_EPICS
38  if (ca_current_context()) ca_context_destroy();
39 #endif
40 }
41 
42 void DQMHistAnalysisRooFitExampleModule::initialize()
43 {
44  B2INFO("DQMHistAnalysisRooFitExample: initialized.");
45 
46 #ifdef _BELLE2_EPICS
47  if (!ca_current_context()) SEVCHK(ca_context_create(ca_disable_preemptive_callback), "ca_context_create");
48  SEVCHK(ca_create_channel("fit_value", NULL, NULL, 10, &mychid), "ca_create_channel failure");
49  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
50 #endif
51 
52  w = new RooWorkspace("w");
53  w->factory("Gaussian::f(x[-20,20],mean[0,-5,5],sigma[3,1,10])");
54  model = w->pdf("f");
55 
56  m_c0 = new TCanvas("example0");
57 }
58 
59 
60 void DQMHistAnalysisRooFitExampleModule::beginRun()
61 {
62  //m_serv->SetTimer(100, kFALSE);
63  B2INFO("DQMHistAnalysisRooFitExample: beginRun called.");
64  m_c0->Clear();
65 
66  TH1* hh1 = findHist("FirstDet/h_HitXPositionCh01");
67  if (hh1 != NULL) {
68 
69  //RooDataHist::RooDataHist(const char *name, const char *title, const RooArgList& vars, const TH1* hist, Double_t wgt) :
70  x = w->var("x");
71  data = new RooDataHist("data", "dataset with x", *x, (const TH1*) hh1);
72  plot = x->frame();
73  r = model->fitTo(*data);
74 
75  // plot data and function
76 
77  data->plotOn(plot);
78  model->plotOn(plot);
79 
80  m_c0->cd();
81  plot->Draw();
82  } else {
83  B2FATAL("Histo now there ... -> zero pointer crash");
84  }
85 
86 }
87 
88 void DQMHistAnalysisRooFitExampleModule::event()
89 {
90  TH1* hh1;
91 
92  printf("0\n");
93  hh1 = findHist("FirstDet/h_HitXPositionCh01");
94 
95  if (hh1 != NULL) {
96  if (data) delete data;
97 
98  data = new RooDataHist("data", "dataset with x", *(w->var("x")), hh1);
99  data->Print();
100  r = model->fitTo(*data);
101  data->Print();
102 
103 // RooPlot *m_plot = x->frame();
104  plot = x->frame();
105 
106  // while(plot->numItems()>0){
107  // plot->remove("",false);
108  // }
109  // printf("5\n");
110 
111  data->plotOn(plot);
112  model->plotOn(plot);
113 
114  m_c0->cd();
115  plot->Draw();
116 
117  m_c0->Modified();
118  m_c0->Update();
119 
120  }
121 
122 #ifdef _BELLE2_EPICS
123  double fitdata = 0;
124 
125  SEVCHK(ca_put(DBR_DOUBLE, mychid, (void*)&fitdata), "ca_set failure");
126  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
127 #endif
128 }
129 
130 void DQMHistAnalysisRooFitExampleModule::endRun()
131 {
132  B2INFO("DQMHistAnalysisRooFitExample: endRun called");
133 }
134 
135 
136 void DQMHistAnalysisRooFitExampleModule::terminate()
137 {
138 #ifdef _BELLE2_EPICS
139  SEVCHK(ca_clear_channel(mychid), "ca_clear_channel failure");
140  SEVCHK(ca_pend_io(5.0), "ca_pend_io failure");
141 #endif
142  B2INFO("DQMHistAnalysisRooFitExample: terminate called");
143 }
144 
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::DQMHistAnalysisRooFitExampleModule
Class definition for the output module of Sequential ROOT I/O.
Definition: DQMHistAnalysisRootFitExample.h:33
Belle2::DQMHistAnalysisModule
The base class for the histogram analysis module.
Definition: DQMHistAnalysis.h:27