Belle II Software development
DQMHistAnalysisCDCMonObj.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// Own header.
10#include <dqm/analysis/modules/DQMHistAnalysisCDCMonObj.h>
11
12// CDC geometry
13#include <cdc/geometry/CDCGeometryPar.h>
14
15#include <TROOT.h>
16#include <TEllipse.h>
17#include <TF1.h>
18#include <TLine.h>
19#include <TStyle.h>
20
21#include <numeric>
22
23using namespace std;
24using namespace Belle2;
25
26//-----------------------------------------------------------------
27// Register module
28//-----------------------------------------------------------------
29
30REG_MODULE(DQMHistAnalysisCDCMonObj);
31
34{
35 // set module description (e.g. insert text)
36 addParam("HistDirectory", m_name_dir, "CDC Dir of DQM Histogram", std::string("CDC"));
37 addParam("HistBoardADC", m_hname_badc, "Board ADC Histogram Name", std::string("hADCBoard"));
38 addParam("HistBoardTDC", m_hname_btdc, "Board TDC Histogram Name", std::string("hTDC"));
39 addParam("HistHitsPhi", m_hname_hits, "Phi Hits Histogram Name", std::string("hHit"));
40 setDescription("Modify and analyze the data quality histograms of CDCMonObj");
42 for (int i = 0; i < 300; i++) {
43 m_hADCs[i] = nullptr;
44 m_hTDCs[i] = nullptr;
45 }
46 for (int i = 0; i < 56; i++) m_hHits[i] = nullptr;
47}
48
52
54{
55
57 if (!(*m_channelMapFromDB).isValid()) {
58 B2FATAL("Channel map is not valid");
59 }
60
62 if (m_cdcGeo == nullptr) {
63 B2FATAL("CDCGeometryp is not valid");
64 }
65
67
68 gStyle->SetOptStat(0);
69 gStyle->SetPalette(kViridis);
70 gStyle->SetPadTopMargin(0.1);
71 gStyle->SetPadRightMargin(0.05);
72 gStyle->SetPadBottomMargin(0.1);
73 gStyle->SetPadLeftMargin(0.15);
74
75 m_cMain = new TCanvas("cdc_main", "cdc_main", 1500, 1200);
76 m_monObj->addCanvas(m_cMain);
77
78 m_cADC = new TCanvas("cdc_adc", "cdc_adc", 2000, 10000);
79 m_monObj->addCanvas(m_cADC);
80 m_cTDC = new TCanvas("cdc_tdc", "cdc_tdc", 2000, 10000);
81 m_monObj->addCanvas(m_cTDC);
82 m_cHit = new TCanvas("cdc_hit", "cdc_hit", 1500, 6000);
83 m_monObj->addCanvas(m_cHit);
84
85 B2DEBUG(20, "DQMHistAnalysisCDCMonObj: initialized.");
86
87}
88
90{
91 for (const auto& cm : (*m_channelMapFromDB)) {
92 const int isl = cm.getISuperLayer();
93 const int il = cm.getILayer();
94 const int iw = cm.getIWire();
95 const int iBoard = cm.getBoardID();
96 const int iCh = cm.getBoardChannel();
97 const WireID wireId(isl, il, iw);
98 m_chMap.insert(std::make_pair(wireId, std::make_pair(iBoard, iCh)));
99 }
100
101
102 const CDCGeometry& geom = **m_cdcGeo;
103
104 for (const auto& sense : geom.getSenseLayers()) {
105 int i = sense.getId();
106 if (i < 0 || i > 55) {
107 B2FATAL("no such sense layer");
108 }
109 m_senseR[i] = sense.getR();
110 m_nSenseWires[i] = sense.getNWires();
111 m_offset[i] = sense.getOffset();
112 }
113
114 for (const auto& field : geom.getFieldLayers()) {
115 int i = field.getId();
116 if (i < 0 || i > 54) {
117 B2FATAL("no such sense layer");
118 }
119 m_fieldR[i + 1] = field.getR();
120 }
121 m_fieldR[56] = geom.getOuterWall(0).getRmin();
122 m_fieldR[0] = geom.getInnerWall(0).getRmax();
123
124}
125
126
128{
129 for (int ilayer = 0; ilayer < 56; ++ilayer) {
130 float dPhi = float(2 * M_PI / m_nSenseWires[ilayer]);
131 float r1 = m_fieldR[ilayer];
132 float r2 = m_fieldR[ilayer + 1];
133 for (int iwire = 0; iwire < m_nSenseWires[ilayer]; ++iwire) {
134 float phi = dPhi * (iwire + m_offset[ilayer]);
135 float phi1 = phi - dPhi * 0.5;
136 float phi2 = phi + dPhi * 0.5;
137 Double_t x_pos[] = {r1* (sin(phi)*tan(phi - phi1) + cos(phi)),
138 r2 * cos(phi1),
139 r2 * cos(phi2),
140 r1* (sin(phi)*tan(phi - phi2) + cos(phi))
141 };
142 Double_t y_pos[] = {r1* (-cos(phi)*tan(phi - phi1) + sin(phi)),
143 r2 * sin(phi1),
144 r2 * sin(phi2),
145 r1* (-cos(phi)*tan(phi - phi2) + sin(phi))
146 };
147 h->AddBin(4, x_pos, y_pos);
148 }
149 }
150}
151
152
154{
155 m_badChannels.clear();
156 for (int il = 0; il < 56; ++il) {
157 for (int iw = 0; iw < m_nSenseWires[il]; ++iw) {
158 const int y = m_hHits[il]->GetBinContent(iw + 1);
159 if (y == 0) {
160 m_badChannels.push_back(std::make_pair(il, iw));
161 }
162 }
163 }
164 B2DEBUG(20, "num bad wires " << m_badChannels.size());
165}
166
168{
169 TH1D* hist = (TH1D*)h->Clone();
170 hist->SetBinContent(1, 0.0); // Exclude 0-th bin
171 float m = hist->GetMean();
172 delete hist;
173 return m;
174}
175
177{
178 TH1D* hist = (TH1D*)h->Clone();
179 hist->SetBinContent(1, 0.0); // Exclude 0-th bin
180 if (hist->GetMean() == 0) {return 0.0;} // Avoid an error if only ADC=0 entries
181 double quantiles[1] = {0.0}; // One element to store median
182 double probSums[1] = {0.5}; // Median definition
183 hist->GetQuantiles(1, quantiles, probSums);
184 float median = quantiles[0];
185 delete hist;
186 return median;
187}
188
189std::pair<int, int> DQMHistAnalysisCDCMonObjModule::getBoardChannel(unsigned short layer, unsigned short wire)
190{
191 const WireID w(layer, wire);
192 decltype(m_chMap)::iterator it = m_chMap.find(w);
193 if (it != m_chMap.end()) {
194 return it->second;
195 } else {
196 B2ERROR("no corresponding board/channel found layer " << layer << " wire " << wire);
197 return std::make_pair(-1, -1);
198 }
199}
200
201
203{
204 B2DEBUG(20, "end run");
205
206 m_hADC = (TH2F*)findHist(m_name_dir + "/" + m_hname_badc);
207 m_hTDC = (TH2F*)findHist(m_name_dir + "/" + m_hname_btdc);
208 m_hHit = (TH2F*)findHist(m_name_dir + "/" + m_hname_hits);
209
210 if (m_hADC == nullptr) {
211 m_monObj->setVariable("comment", "No ADC histograms of CDC in file");
212 B2INFO("Histogram named m_hADC is not found.");
213 return;
214 }
215
216 TF1* fitFunc[300] = {};
217 for (int i = 0; i < 300; ++i) {
218 fitFunc[i] = new TF1(Form("f%d", i), "[0]+[6]*x+[1]*(exp([2]*(x-[3]))/(1+exp(-([4]-x)/[5])))",
219 4921 - 100, 4921 + 100);
220 fitFunc[i]->SetParLimits(6, 0, 0.1);
221 fitFunc[i]->SetParLimits(4, 4850., 5000.0);
222 fitFunc[i]->SetParLimits(5, 0, 50.0);
223 }
224
225 int neve = m_hTDC->GetEntries();
226 if (neve == 0)neve = 1;
227
228 B2DEBUG(20, "adc related");
229 int nDeadADC = -1; // bid 0 always empty
230 int nBadADC = 0;
231 TH1F* hADCMean = new TH1F("hADCMean", "ADC mean;board;adc mean", 300, 0, 300);
232 TH1F* hADC1000 = new TH1F("ADC1000", "ADC1000", 300, 0, 300);
233 TH1F* hADC0 = new TH1F("ADC0", "ADC0", 300, 0, 300);
234
235 // Collect ADC mean/median for each board
236 std::vector<float> means = {};
237 std::vector<float> medians = {};
238
239 for (int i = 0; i < 300; ++i) {
240 m_hADCs[i] = m_hADC->ProjectionY(Form("hADC%d", i), i + 1, i + 1, "");
241 m_hADCs[i]->SetTitle(Form("hADC%d", i));
242 float n = static_cast<float>(m_hADCs[i]->GetEntries());
243 if (m_hADCs[i]->Integral(0, m_hADCs[i]->GetNbinsX()) == 0) {
244 nDeadADC += 1;
245 hADC0->SetBinContent(i + 1, -0.1);
246 } else {
247 float n0 = static_cast<float>(m_hADCs[i]->GetBinContent(1));
248 if (n0 / n > 0.9) {
249 B2DEBUG(21, "bad adc bid " << i << " " << n0 << " " << n);
250 nBadADC += 1;
251 }
252 float bin1 = m_hADCs[i]->GetBinContent(1);
253 float m = getHistMean(m_hADCs[i]);
254 float md = getHistMedian(m_hADCs[i]);
255 means.push_back(m);
256 medians.push_back(md);
257 hADCMean->SetBinContent(i + 1, m);
258 hADCMean->SetBinError(i + 1, 0);
259 double overflow = m_hADCs[i]->GetBinContent(m_hADCs[i]->GetNbinsX() + 1);
260 hADC1000->SetBinContent(i + 1, overflow / (overflow + n));
261 hADC0->SetBinContent(i + 1, bin1 / (overflow + n));
263 }
264 }
265 // TDC related
266 B2DEBUG(20, "tdc related");
267 int nDeadTDC = -1; // bid 0 always empty
268 TH1F* hTDCEdge = new TH1F("hTDCEdge", "TDC edge;board;tdc edge [nsec]", 300, 0, 300);
269 TH1F* hTDCSlope = new TH1F("hTDCSlope", "TDC slope;board;tdc slope [nsec]", 300, 0, 300);
270 std::vector<float> tdcEdges = {};
271 std::vector<float> tdcSlopes = {};
272 for (int i = 0; i < 300; ++i) {
273 m_hTDCs[i] = m_hTDC->ProjectionY(Form("hTDC%d", i), i + 1, i + 1);
274 m_hTDCs[i]->SetTitle(Form("hTDC%d", i));
275 if (m_hTDCs[i]->Integral(0, m_hTDCs[i]->GetNbinsX()) == 0 || m_hTDCs[i] == nullptr) {
276 nDeadTDC += 1;
277 tdcEdges.push_back(0);
278 tdcSlopes.push_back(0);
279 } else {
280 double init_p0 = m_hTDCs[i]->GetBinContent(700 + 60);
281 fitFunc[i]->SetParameters(init_p0, 100, 0.01, 4700, 4900, 2, 0.01);
282 fitFunc[i]->SetParameter(6, 0.02);
283 fitFunc[i]->SetParLimits(0, init_p0 - 200, init_p0 + 200);
284 int TDCfitstatus = -1;
285 if (i < 28) {
286 TDCfitstatus = m_hTDCs[i]->Fit(fitFunc[i], "qM0", "", 4850, 5000);
287 } else {
288 TDCfitstatus = m_hTDCs[i]->Fit(fitFunc[i], "qM0", "", 4800, 5000);
289 }
290 float p4 = fitFunc[i]->GetParameter(4);
291 float p5 = fitFunc[i]->GetParameter(5);
292
293 if (TDCfitstatus != -1 && 4850 < p4 && p4 < 5000) {
294 hTDCEdge->SetBinContent(i + 1, p4);
295 hTDCEdge->SetBinError(i + 1, 0);
296 hTDCSlope->SetBinContent(i + 1, p5);
297 hTDCSlope->SetBinError(i + 1, 0);
298 }
299 tdcEdges.push_back(p4);
300 tdcSlopes.push_back(p5);
301 }
302
303 }
304
305 // Hit related
306 B2DEBUG(20, "hit related");
307 TH1F* hHitPerLayer = new TH1F("hHitPerLayer", "hit/Layer;layer", 56, 0, 56);
308 TH1F* hHitRatePerWire = new TH1F("hHitRatePerWire", "hit rate (kHz)/Wire;layer", 56, 0, 56);
309 int nHits = 0;
310 for (int i = 0; i < 56; ++i) {
311 int tdcwindow;
312 double tdcclock = 0.98255764; //unit: ns
313 if (i < 8) tdcwindow = 416;
314 else tdcwindow = 768;
315 m_hHits[i] = m_hHit->ProjectionY(Form("hHit%d", i), i + 1, i + 1);
316 m_hHits[i]->SetTitle(Form("hHit%d", i));
317 if (m_hHits[i]->GetEntries() > 0 && m_hHits[i] != nullptr) {
318 int nhitSumL = 0;
319 int nBins = m_nSenseWires[i];
320 for (int j = 0; j < nBins; ++j) {
321 nhitSumL += m_hHits[i]->GetBinContent(j + 1);
322 }
323 if (neve > 0) {
324 hHitPerLayer->SetBinContent(i + 1, 1.0 * nhitSumL / neve);
325 hHitRatePerWire->SetBinContent(i + 1, (1.0 * nhitSumL / neve) / (1.0 * nBins * tdcwindow * tdcclock * 1e-6));
326 } else {
327 hHitPerLayer->SetBinContent(i + 1, nhitSumL);
328 hHitRatePerWire->SetBinContent(i + 1, (1.0 * nhitSumL) / (1.0 * nBins * tdcwindow * tdcclock * 1e-6));
329 }
330 hHitPerLayer->SetBinError(i + 1, 0);
331 hHitRatePerWire->SetBinError(i + 1, 0);
332 nHits += nhitSumL;
333 }
334 }
335
336 // Bad wires related
337 B2DEBUG(20, "bad wire related");
338 hBadChannel = new TH2F("hbadch", "bad channel map;wire;layer", 400, 0, 400, 56, 0, 56);
339 for (int i = 0; i < 400; ++i) {
340 for (int j = 0; j < 56; ++j) {
341 hBadChannel->Fill(i, j, -1);
342 }
343 }
344
345 hBadChannelBC = new TH2F("hbadchBC", "bad channel map per board/channel;board;channel", 300, 0, 300, 48, 0, 48);
346 for (int i = 0; i < 300; ++i) {
347 for (int j = 0; j < 48; ++j) {
348 hBadChannelBC->Fill(i, j, -1);
349 }
350 }
351
352 h2p = new TH2Poly();
354 h2p->SetTitle("bad wires in xy view");
355 h2p->GetXaxis()->SetTitle("X [cm]");
356 h2p->GetYaxis()->SetTitle("Y [cm]");
358 for (const auto& lw : m_badChannels) {
359 const int l = lw.first;
360 const int w = lw.second;
361 B2DEBUG(21, "l " << l << " w " << w);
362 hBadChannel->Fill(w, l);
363 std::pair<int, int> bc = getBoardChannel(l, w);
364 hBadChannelBC->Fill(bc.first, bc.second);
365 float r = m_senseR[l];
366 float dPhi = static_cast<float>(2.0 * M_PI / m_nSenseWires[l]);
367 float phi = dPhi * (w + m_offset[l]);
368 float x = r * cos(phi);
369 float y = r * sin(phi);
370 h2p->Fill(x, y, 1.1);
371 }
372
373 B2DEBUG(20, "writing");
374 m_cMain->Divide(4, 3);
375
376 m_cMain->cd(1);
377 hADCMean->SetMinimum(0);
378 hADCMean->SetMaximum(300);
379 hADCMean->DrawCopy();
380
381 m_cMain->cd(2);
382 hTDCEdge->SetMinimum(4800);
383 hTDCEdge->SetMaximum(5000);
384 hTDCEdge->DrawCopy();
385
386 m_cMain->cd(3);
387 hTDCSlope->SetMinimum(0);
388 hTDCSlope->SetMaximum(50);
389 hTDCSlope->DrawCopy();
390
391 m_cMain->cd(4);
392 hBadChannel->DrawCopy("col");
393
394 m_cMain->cd(5);
395 hBadChannelBC->DrawCopy("col");
396
397 m_cMain->cd(7);
398 hADC1000->DrawCopy();
399
400 m_cMain->cd(8);
401 hADC0->DrawCopy();
402
403 m_cMain->cd(9);
404 hHitPerLayer->DrawCopy();
405
406 m_cMain->cd(10);
407 hHitRatePerWire->DrawCopy();
408
409 m_cHit->Divide(4, 14);
410 for (int i = 0; i < 56; i++) {
411 m_cHit->cd(i + 1);
412 m_hHits[i]->GetXaxis()->SetRangeUser(0, m_nSenseWires[i]);
413 m_hHits[i]->Draw("hist");
414 }
415
416 m_cADC->Divide(6, 50, 0.0002, 0.0002);
417 m_cTDC->Divide(6, 50, 0.0002, 0.0002);
418
419 for (int i = 0; i < 300; i++) {
420 m_cADC->cd(i + 1);
421 Double_t max = m_hADCs[i]->GetMaximum();
422 m_hADCs[i]->GetYaxis()->SetRangeUser(0, 3 * max);
423 m_hADCs[i]->Draw("hist");
424
425 m_cTDC->cd(i + 1);
426 m_hTDCs[i]->Draw("hist");
427 fitFunc[i]->SetLineColor(kRed);
428 fitFunc[i]->Draw("same");
429 max = m_hTDCs[i]->GetMaximum();
430 TLine* l1 = new TLine(tdcEdges[i], 0, tdcEdges[i], max * 1.05);
431 l1->SetLineColor(kRed);
432 TLine* l0 = new TLine(4910, 0, 4910, max * 1.05);
433 l0->Draw();
434 l1->Draw();
435 }
436
437 m_cMain->cd(6);
438 h2p->DrawCopy("col");
439 float superLayerR[10] = {16.3, 24.3, 35.66, 46.63, 57.55, 68.47,
440 79.39, 90.31, 101.23, 112.05
441 };
442
443 TEllipse* circs[10];
444 for (int i = 0; i < 10; ++i) {
445 circs[i] = new TEllipse(0, 0, superLayerR[i], superLayerR[i]);
446 circs[i]->SetFillStyle(4000);
447 circs[i]->SetLineStyle(kDashed);
448 circs[i]->SetLineColor(0);
449 circs[i]->Draw("same");
450 }
451
452 m_monObj->setVariable("nEvents", neve);
453 m_monObj->setVariable("nHits", nHits / neve);
454 m_monObj->setVariable("nBadWires", m_badChannels.size());
455 m_monObj->setVariable("adcMean", std::accumulate(means.begin(), means.end(), 0.0) / means.size());
456 m_monObj->setVariable("adcMeanMedianBoard", std::accumulate(medians.begin(), medians.end(), 0.0) / medians.size());
457 m_monObj->setVariable("nDeadADC", nDeadADC);
458 m_monObj->setVariable("nBadADC", nBadADC); //???? n_0/n_tot>0.9
459 m_monObj->setVariable("tdcEdge", std::accumulate(tdcEdges.begin(), tdcEdges.end(), 0.0) / (tdcEdges.size() - 1 - nDeadTDC));
460 m_monObj->setVariable("nDeadTDC", nDeadTDC);
461 m_monObj->setVariable("tdcSlope", std::accumulate(tdcSlopes.begin(), tdcSlopes.end(), 0.0) / (tdcSlopes.size() - 1 - nDeadTDC));
462
463 delete hADCMean;
464 delete hADC1000;
465 delete hADC0;
466 delete hTDCEdge;
467 delete hTDCSlope;
468 delete hHitPerLayer;
469 delete hHitRatePerWire;
470
471}
472
474{
475
476 B2DEBUG(20, "terminate called");
477}
478
The Class for CDC geometry.
Definition CDCGeometry.h:27
Class for accessing arrays of objects in the database.
Definition DBArray.h:26
Class for accessing objects in the database.
Definition DBObjPtr.h:21
std::map< WireID, std::pair< int, int > > m_chMap
Channel map retrieved.
void initialize() override final
Initialize the Module.
std::string m_hname_hits
Hits histogram names.
DBArray< CDCChannelMap > * m_channelMapFromDB
Channel map retrieved from DB.
std::vector< std::pair< int, int > > m_badChannels
bad wires list
int m_nSenseWires[56]
number of wires for each layer.
TH1D * m_hTDCs[300]
TDC histograms with track associated hits for each board (0-299)
MonitoringObject * m_monObj
monitoring object
void terminate() override final
Termination action.
DBObjPtr< CDCGeometry > * m_cdcGeo
Geometry of CDC.
TH2F * m_hTDC
Summary of TDC histograms with track associated hits.
TH1D * m_hADCs[300]
ADC histograms with track associated hits for each board (0-299)
TH2F * hBadChannel
bad channel map;wire;layer
std::pair< int, int > getBoardChannel(unsigned short layer, unsigned short wire)
Get board/channel from layer/wire.
TH1D * m_hHits[56]
hit histograms for each layer (0-55)
void endRun() override final
End-of-run action.
void configureBins(TH2Poly *h)
Configure bins of TH2Poly.
float getHistMean(TH1D *h) const
Get mean of ADC histogram excluding 0-th bin.
void beginRun() override final
Called when entering a new run.
TH2F * m_hADC
Summary of ADC histograms with track associated hits.
TH2F * hBadChannelBC
bad channel map per board/channel;board;channel
float getHistMedian(TH1D *h) const
Get median of ADC histogram excluding 0-th bin.
float m_senseR[56]
Radius of sense (+field) layer.
std::string m_hname_btdc
Board TDC histogram name.
std::string m_hname_badc
Board ADC histogram name.
static MonitoringObject * getMonitoringObject(const std::string &name)
Get MonitoringObject with given name (new object is created if non-existing)
static TH1 * findHist(const std::string &histname, bool onlyIfUpdated=false)
Get histogram from list (no other search).
DQMHistAnalysisModule()
Constructor / Destructor.
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
Class to identify a wire inside the CDC.
Definition WireID.h:34
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 tan(double a)
tan for double
Definition beamHelpers.h:31
Abstract base class for different kinds of events.
STL namespace.