Belle II Software development
DQMHistAnalysisPhysics.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 : DQMHistAnalysisPhysics.cc
10// Description : DQM module, for the physics histograms at hlt level
11//-
12
13
14#include <dqm/analysis/modules/DQMHistAnalysisPhysics.h>
15#include <TROOT.h>
16#include <RooRealVar.h>
17#include <RooDataHist.h>
18#include "RooCBShape.h"
19#include "RooCrystalBall.h"
20#include "RooChebychev.h"
21#include "RooAddPdf.h"
22#include "RooPlot.h"
23#include "RooFitResult.h"
24
25using namespace std;
26using namespace Belle2;
27
28//-----------------------------------------------------------------
29// Register the Module
30//-----------------------------------------------------------------
31REG_MODULE(DQMHistAnalysisPhysics);
32
33//-----------------------------------------------------------------
34// Implementation
35//-----------------------------------------------------------------
36
38{
39 // Description
40 // Parameter definition
41 setDescription("DQM Analysis for Physics histograms");
43 addParam("PVPrefix", m_pvPrefix, "PV Prefix", std::string("Physics:"));
44 addParam("minEntriesUPSmumu", m_minEntriesUPSmumu, "minimum number of new entries for last time slot for Ups(mumu)", 1000);
45 addParam("minEntriesUPSee", m_minEntriesUPSee, "minimum number of new entries for last time slot for Ups(ee)", 1000);
46
47 B2DEBUG(20, "DQMHistAnalysisPhysics: Constructor done.");
48}
49
51{
52 gROOT->cd();
53 m_monObj = getMonitoringObject("physics_hlt");
54 //new text for plots
55 m_cmUPSmumu_text = new TPaveText(0.7, 0.8, 0.9, 0.9, "NDC");
56 m_cmUPSmumu_text->SetFillColor(0);
57 m_cmUPSmumu_text->SetFillStyle(0);
58 m_cmUPSmumu_text->SetTextAlign(12);
59 m_cmUPSmumu_text->SetBorderSize(0);
60 m_cmUPSmumu_text->SetTextSize(0.026);
61 m_cmUPSee_text = new TPaveText(0.7, 0.8, 0.9, 0.9, "NDC");
62 m_cmUPSee_text->SetFillColor(0);
63 m_cmUPSee_text->SetFillStyle(0);
64 m_cmUPSee_text->SetTextAlign(12);
65 m_cmUPSee_text->SetBorderSize(0);
66 m_cmUPSee_text->SetTextSize(0.026);
67 m_ratio_text = new TPaveText(0.55, 0.6, 0.85, 0.9, "NDC");
68 m_ratio_text->SetFillColor(0);
69 m_ratio_text->SetFillStyle(0);
70 m_ratio_text->SetTextAlign(12);
71 m_ratio_text->SetBorderSize(0);
72 m_ratio_text->SetTextSize(0.026);
73
74 addDeltaPar("PhysicsObjects", "mUPSe", HistDelta::c_Entries, m_minEntriesUPSee, 1);
75 registerEpicsPV(m_pvPrefix + "mUPSee_mean", "mUPSee_mean");
76 registerEpicsPV(m_pvPrefix + "mUPSee_width", "mUPSee_width");
77 m_cmUPSee = new TCanvas("PhysicsObjects/fit_mUPSee");
78
79 addDeltaPar("PhysicsObjects", "mUPS", HistDelta::c_Entries, m_minEntriesUPSmumu, 1);
80 registerEpicsPV(m_pvPrefix + "mUPSmumu_mean", "mUPSmumu_mean");
81 registerEpicsPV(m_pvPrefix + "mUPSmumu_width", "mUPSmumu_width");
82 m_cmUPSmumu = new TCanvas("PhysicsObjects/fit_mUPSmumu");
83
84 //new ratio hadronb2_tight/bhabha_trk_ecl
85 addDeltaPar("PhysicsObjects", "physicsresults", HistDelta::c_Events, 3000000, 1); // update each 10000 events
86 registerEpicsPV(m_pvPrefix + "hadronb2_tight_over_bhabha_trk_ecl", "hadronb2_tight_over_bhabha_trk_ecl");
87
88 B2DEBUG(20, "DQMHistAnalysisPhysics: initialized.");
89}
90
91
93{
94 B2DEBUG(20, "DQMHistAnalysisPhysics: beginRun called.");
95}
96
97void DQMHistAnalysisPhysicsModule::fitUpsilonFromHisto(TH1* histo, TPaveText* text, std::string parts, std::string prefix,
98 std::string pvname)
99{
100 double xMin = histo->GetXaxis()->GetXmin();
101 double xMax = histo->GetXaxis()->GetXmax();
102 RooRealVar m("m", parts.c_str(), xMin, xMax);
103
104 RooDataHist data("data", "histogram", m, histo);
105
106 RooRealVar mean("mean", "mass", 10.579, 10.5, 10.7);
107 RooRealVar sigma("sigma", "resolution", 0.05, 0.01, 0.15);
108 RooRealVar alphaL("alphaL", "left tail alpha", 1.5, 0.1, 5);
109 RooRealVar nL("nL", "left tail n", 1.0, 0.1, 10);
110 RooRealVar alphaR("alphaR", "right tail alpha", 2.0, 0.1, 5);
111 RooRealVar nR("nR", "right tail n", 1.0, 0.1, 10);
112
113 RooCrystalBall signal("signal", "Double CB", m, mean, sigma, sigma, alphaL, nL, alphaR, nR);
114
115 RooRealVar a0("a0", "poly constant", 0.0, -1.0, 1.0);
116 RooChebychev bkg("bkg", "Background PDF", m, RooArgList(a0));
117
118 // Set the max to 2x the total entries to ensure the fit never hits a ceiling
119 double nMax = histo->GetEntries() * 2.0;
120 RooRealVar nsig("nsig", "yield of signal", histo->GetEntries() * 0.3, 0, nMax);
121 RooRealVar nbkg("nbkg", "yield of background", histo->GetEntries() * 0.7, 0, nMax);
122
123 RooAddPdf model("model", "sig+bkg", RooArgList(signal, bkg), RooArgList(nsig, nbkg));
124
125 model.fitTo(data, RooFit::Extended(kTRUE));
126
127 RooPlot* frame = m.frame();
128 data.plotOn(frame, RooFit::DrawOption("B"), RooFit::FillColor(kGray), RooFit::LineWidth(1), RooFit::MarkerSize(0),
129 RooFit::XErrorSize(0), RooFit::DataError(RooAbsData::None), RooFit::LineStyle(kSolid), RooFit::LineColor(kBlack));
130 model.plotOn(frame, RooFit::LineColor(kBlue), RooFit::LineWidth(1));
131 frame->SetTitle("Upsilon(4S) Mass Fit");
132 frame->GetXaxis()->SetTitle((parts).c_str());
133 frame->Draw();
134
135 auto measuredMass = mean.getVal();
136 auto massUncertainty = mean.getError();
137 auto measuredWidth = sigma.getVal();
138
139 text->Clear();
140 double mean_mUPS = histo->GetMean();
141 text->AddText(Form("mean : %.3f", float(mean_mUPS)));
142 text->AddText(Form("fit mean : %.3f +-%.3f", float(measuredMass), float(massUncertainty)));
143 text->AddText(Form("fit width : %.3f", float(measuredWidth)));
144 text->Draw();
145 setEpicsPV(pvname + "_mean", measuredMass);
146 setEpicsPV(pvname + "_width", measuredWidth);
147 m_monObj->setVariable(prefix + "mass", measuredMass, massUncertainty);
148 m_monObj->setVariable(prefix + "width", measuredWidth);
149
150}
151
153{
154
155
156 bool m_IsPhysicsRun = (getRunType() == "physics") || (getRunType() == "debug");
157 if (m_IsPhysicsRun == true) {
158
159 m_ratio_text->Clear();
160 auto m_hphysicsresults = findHist("PhysicsObjects/physicsresults", true);// check if updated
161 if (m_hphysicsresults) {
162 double had_ntot = m_hphysicsresults->GetBinContent(2);
163 double hadb2_ntot = m_hphysicsresults->GetBinContent(3);
164 double hadb2_tight_ntot = m_hphysicsresults->GetBinContent(4);
165 double mumu_tight_ntot = m_hphysicsresults->GetBinContent(5);
166 double bhabha_trk_ecl_ntot = m_hphysicsresults->GetBinContent(6);
167
168 double ratio_hadron_bhabha = 0.;
169 double ratio_hadronb2_bhabha = 0.;
170 double ratio_hadronb2_tight_bhabha = 0.;
171 double ratio_mumu_tight_bhabha = 0.;
172 double error_hadron_bhabha = -10.;
173 double error_hadronb2_bhabha = -10.;
174 double error_hadronb2_tight_bhabha = -10.;
175 double error_mumu_tight_bhabha = -10.;
176
177
178 if (bhabha_trk_ecl_ntot != 0) {
179 ratio_hadron_bhabha = had_ntot / bhabha_trk_ecl_ntot;
180 error_hadron_bhabha = ratio_hadron_bhabha * sqrt((1 / had_ntot) + (1 / bhabha_trk_ecl_ntot));
181 ratio_hadronb2_bhabha = hadb2_ntot / bhabha_trk_ecl_ntot;
182 error_hadronb2_bhabha = ratio_hadronb2_bhabha * sqrt((1 / hadb2_ntot) + (1 / bhabha_trk_ecl_ntot));
183 ratio_hadronb2_tight_bhabha = hadb2_tight_ntot / bhabha_trk_ecl_ntot;
184 error_hadronb2_tight_bhabha = ratio_hadronb2_tight_bhabha * sqrt((1 / hadb2_tight_ntot) + (1 / bhabha_trk_ecl_ntot));
185 ratio_mumu_tight_bhabha = mumu_tight_ntot / bhabha_trk_ecl_ntot;
186 error_mumu_tight_bhabha = ratio_mumu_tight_bhabha * sqrt((1 / mumu_tight_ntot) + (1 / bhabha_trk_ecl_ntot));
187 }
188 m_ratio_text->AddText(Form("hadronb2_tight/bhabha: %.4f +/- %.4f", float(ratio_hadronb2_tight_bhabha),
189 float(error_hadronb2_tight_bhabha)));
190 m_ratio_text->AddText(Form("hadronb2/bhabha: %.4f +/- %.4f", float(ratio_hadronb2_bhabha), float(error_hadronb2_bhabha)));
191 m_ratio_text->AddText(Form("mumu_tight/bhabha: %.4f +/- %.4f", float(ratio_mumu_tight_bhabha), float(error_mumu_tight_bhabha)));
192 m_ratio_text->AddText(Form("hadron/bhabha: %.4f +/- %.4f", float(ratio_hadron_bhabha), float(error_hadron_bhabha)));
193
194 }
195
196 // for pv #new hadronb2_tight/#bhabha_trk_ecl
197 auto hist_hadronb2_tight_over_bhabha_trk_ecl = getDelta("PhysicsObjects", "physicsresults", 0, true);// only if updated
198 if (hist_hadronb2_tight_over_bhabha_trk_ecl) {
199 if (hist_hadronb2_tight_over_bhabha_trk_ecl->GetBinContent(6) != 0) {
200 double hadronb2_tight_over_bhabha_trk_ecl = hist_hadronb2_tight_over_bhabha_trk_ecl->GetBinContent(4) /
201 hist_hadronb2_tight_over_bhabha_trk_ecl->GetBinContent(6);
202 B2DEBUG(1, "hadronb2_tight_over_bhabha_trk_ecl:" << hadronb2_tight_over_bhabha_trk_ecl);
203 setEpicsPV("hadronb2_tight_over_bhabha_trk_ecl", hadronb2_tight_over_bhabha_trk_ecl);
204 }
205 }
206
207 if (m_cmUPSmumu) {
208 auto hmUPSmumu = getDelta("PhysicsObjects/mUPS");// check if updated
209 if (hmUPSmumu) {
210 m_cmUPSmumu->cd();
211 fitUpsilonFromHisto(hmUPSmumu, m_cmUPSmumu_text, "M(#mu#mu) [GeV/c^2]", "UPSmumu", m_pvPrefix + "mUPSmumu");
212 m_cmUPSmumu->Modified();
213 m_cmUPSmumu->Update();
215 } else {
216 hmUPSmumu = findHist("PhysicsObjects/mUPS", true);// only if updated
217 if (hmUPSmumu and hmUPSmumu->GetEntries() < m_minEntriesUPSmumu) {
218 // only if integral plot is below delta entries
219 m_cmUPSmumu->cd();
220 m_cmUPSmumu->Clear();
221 hmUPSmumu->Draw("hist");
222 }
223 }
224 }
225 if (m_cmUPSee) {
226 auto hmUPSee = getDelta("PhysicsObjects/mUPSe");// check if updated
227 if (hmUPSee) {
228 m_cmUPSee->cd();
229 fitUpsilonFromHisto(hmUPSee, m_cmUPSee_text, "M(ee) [GeV/c^2]", "UPSee", m_pvPrefix + "mUPSee");
230 m_cmUPSee->Modified();
231 m_cmUPSee->Update();
233 } else {
234 hmUPSee = findHist("PhysicsObjects/mUPSe", true);// only if updated
235 if (hmUPSee and hmUPSee->GetEntries() < m_minEntriesUPSee) {
236 // only if integral plot is below delta entries
237 m_cmUPSee->cd();
238 m_cmUPSee->Clear();
239 hmUPSee->Draw("hist");
240 }
241 }
242 }
243 auto* m_cphysicsresults = findCanvas("PhysicsObjects/c_physicsresults");
244 if (m_cphysicsresults) {
245 m_cphysicsresults->cd();
246 m_ratio_text->Draw();
247 m_cphysicsresults->Modified();
248 m_cphysicsresults->Update();
249 }
250 }
251}
253{
254 auto m_hphysicsresults = findHist("PhysicsObjects/physicsresults");
255 if (m_hphysicsresults) {
256 double had_ntot = m_hphysicsresults->GetBinContent(2);
257 double hadb2_ntot = m_hphysicsresults->GetBinContent(3);
258 double hadb2_tight_ntot = m_hphysicsresults->GetBinContent(4);
259 double mumu_tight_ntot = m_hphysicsresults->GetBinContent(5);
260 double bhabha_trk_ecl_ntot = m_hphysicsresults->GetBinContent(6);
261 double ratio_hadron_bhabha_final = 0.;
262 double ratio_hadronb2_bhabha_final = 0.;
263 double ratio_hadronb2_tight_bhabha_final = 0.;
264 double ratio_mumu_tight_bhabha_final = 0.;
265
266 if (bhabha_trk_ecl_ntot != 0) {
267 ratio_hadron_bhabha_final = had_ntot / bhabha_trk_ecl_ntot;
268 ratio_hadronb2_bhabha_final = hadb2_ntot / bhabha_trk_ecl_ntot;
269 ratio_hadronb2_tight_bhabha_final = hadb2_tight_ntot / bhabha_trk_ecl_ntot;
270 ratio_mumu_tight_bhabha_final = mumu_tight_ntot / bhabha_trk_ecl_ntot;
271 }
272 m_monObj->setVariable("ratio_hadron_bhabha_hlt", ratio_hadron_bhabha_final);
273 m_monObj->setVariable("ratio_hadronb2_bhabha_hlt", ratio_hadronb2_bhabha_final);
274 m_monObj->setVariable("ratio_hadronb2_tight_bhabha_hlt", ratio_hadronb2_tight_bhabha_final);
275 m_monObj->setVariable("ratio_mumu_tight_bhabha_hlt", ratio_mumu_tight_bhabha_final);
276 m_monObj->setVariable("hadronb2_tight_hlt", hadb2_tight_ntot);
277 m_monObj->setVariable("bhabha_trk_ecl_hlt", bhabha_trk_ecl_ntot);
278
279 }
280}
281
283{
284
285 B2DEBUG(20, "DQMHistAnalysisPhysics: terminate called");
286}
287
static TCanvas * findCanvas(TString cname)
Find canvas by name.
int registerEpicsPV(const std::string &pvname, const std::string &keyname="")
EPICS related Functions.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
static void addDeltaPar(const std::string &dirname, const std::string &histname, HistDelta::EDeltaType t, int p, unsigned int a=1)
Add Delta histogram parameters.
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
static void UpdateCanvas(const std::string &name, bool updated=true)
Mark canvas as updated (or not)
static const std::string & getRunType(void)
Get the list of the reference histograms.
TH1 * getDelta(const std::string &fullname, int n=0, bool onlyIfUpdated=true)
Get Delta histogram.
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.
TPaveText * m_ratio_text
TPaveText, ratios physics results.
int m_minEntriesUPSee
min entries for Ups(ee)
void initialize(void) override final
Initializer.
void endRun(void) override final
End-of-run action.
std::string m_pvPrefix
prefix for EPICS PVs
void fitUpsilonFromHisto(TH1 *histo, TPaveText *text, std::string parts, std::string prefix, std::string pvname)
fit histogram with UPS mass
MonitoringObject * m_monObj
Monitoring Object.
TCanvas * m_cmUPSmumu
Tcanvas for mUPSmumu.
int m_minEntriesUPSmumu
min entries for Ups(mumu)
TPaveText * m_cmUPSee_text
TPaveText, Ups ee Invariant Mass.
TPaveText * m_cmUPSmumu_text
TPaveText, Ups Invariant Mass (mumu)
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
void setPropertyFlags(unsigned int propertyFlags)
Sets the flags for the module properties.
Definition Module.cc:208
@ c_ParallelProcessingCertified
This module can be run in parallel processing mode safely (All I/O must be done through the data stor...
Definition Module.h:80
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition Module.h:559
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition Module.h:649
double sqrt(double a)
sqrt for double
Definition beamHelpers.h:28
Abstract base class for different kinds of events.
STL namespace.