9#include <pxd/calibration/PXDHotPixelMaskCalibrationAlgorithm.h>
10#include <pxd/dbobjects/PXDMaskedPixelPar.h>
11#include <pxd/dbobjects/PXDDeadPixelPar.h>
12#include <pxd/dbobjects/PXDOccupancyInfoPar.h>
14#include <boost/format.hpp>
26 forceContinueMasking(false), minEvents(10000), minHits(20), pixelMultiplier(7), maskDrains(true),
27 drainMultiplier(7), maskRows(true), rowMultiplier(7),
28 deadPixelPayloadName(
"PXDDeadPixelPar"),
29 inefficientPixelMultiplier(0), minInefficientPixels(250)
32 " -------------------------- PXDHotPixelMak Calibration Algorithm ------------------------\n"
34 " Algorithm which masks hot pixels with too large occupancy and dead pixels w/o no hits. \n"
35 " ----------------------------------------------------------------------------------------\n"
46 TDirectory* currentDir = gDirectory;
50 std::string fileName = (this->
getPrefix()) +
"debug.root";
51 B2INFO(
"Creating file " << fileName);
52 m_file = std::make_shared<TFile>(fileName.c_str(),
"RECREATE");
56 string iov_str = str(format(
"E%1%_R%2%_E%3%_R%4%") % expRuns.front().first
57 % expRuns.front().second % expRuns.back().first % expRuns.back().second);
58 m_file->mkdir(iov_str.c_str());
59 m_file->cd(iov_str.c_str());
62 auto collector_pxdhits = getObjectPtr<TH1I>(
"PXDHits");
63 auto collector_pxdhitcounts = getObjectPtr<TH1I>(
"PXDHitCounts");
65 auto nevents = collector_pxdhits->GetEntries();
67 for (
auto sensBin = 1; sensBin <= collector_pxdhitcounts->GetXaxis()->GetNbins(); sensBin++) {
68 string sensorDescr = collector_pxdhitcounts->GetXaxis()->GetBinLabel(sensBin);
69 VxdID id(sensorDescr);
72 string name = str(format(
"PXD_%1%_PixelHitmap") %
id.
getID());
73 auto collector_pxdhitmap = getObjectPtr<TH1I>(name.c_str());
74 if (collector_pxdhitmap ==
nullptr) {
77 TH1I* debugHisto =
new TH1I(*collector_pxdhitmap);
79 debugHisto->SetBinContent(debugHisto->GetNbinsX() + 1, nevents);
85 if (expRuns.back() == expRunsAll.back()) {
86 B2INFO(
"Reached Final ExpRun: (" << expRuns.back().first <<
", " << expRuns.back().second <<
")");
96 auto collector_pxdhits = getObjectPtr<TH1I>(
"PXDHits");
97 auto collector_pxdhitcounts = getObjectPtr<TH1I>(
"PXDHitCounts");
100 if (!collector_pxdhits) {
108 auto nevents = collector_pxdhits->GetEntries();
111 B2INFO(
"Not enough data: Only " << nevents <<
" events were collected!");
114 B2WARNING(
"Not enough data: Only " << nevents <<
" events were collected! The masking continous but the mask may be empty.");
118 B2RESULT(
"Found total of " << nevents <<
" events in collected data.");
121 unsigned long long int nPXDHits = 0;
123 for (
auto sensBin = 1; sensBin <= collector_pxdhitcounts->GetXaxis()->GetNbins(); sensBin++) {
125 string sensorDescr = collector_pxdhitcounts->GetXaxis()->GetBinLabel(sensBin);
126 VxdID id(sensorDescr);
130 unsigned long long int nSensorHits = collector_pxdhitcounts->GetBinContent(sensBin);
131 nPXDHits += nSensorHits;
133 B2RESULT(
"Number of hits for sensor sensor=" <<
id <<
" is " << nSensorHits);
138 if (nPXDHits < minPXDHits) {
140 B2INFO(
"Not enough data: Only " << nPXDHits <<
" raw hits were collected!");
143 B2WARNING(
"Not enough data: Only " << nPXDHits <<
" raw hits were collected! The masking continous but the mask may be empty.");
147 B2RESULT(
"Found total of " << nPXDHits <<
" raw hits in collected data.");
150 for (
auto sensBin = 1; sensBin <= collector_pxdhitcounts->GetXaxis()->GetNbins(); sensBin++) {
152 string sensorDescr = collector_pxdhitcounts->GetXaxis()->GetBinLabel(sensBin);
153 VxdID id(sensorDescr);
158 string name = str(format(
"PXD_%1%_PixelHitmap") %
id.
getID());
159 auto collector_pxdhitmap = getObjectPtr<TH1I>(name.c_str());
162 if (collector_pxdhitmap ==
nullptr)
continue;
165 int nBins = collector_pxdhitmap->GetXaxis()->GetNbins();
167 vector<double> hitVec(nBins);
169 for (
auto bin = 1; bin <= nBins; bin++) {
170 hitVec[bin - 1] = (double) collector_pxdhitmap->GetBinContent(bin);
172 double medianNumberOfHits;
173 TMath::Quantiles(nBins, 1, &hitVec[0], &medianNumberOfHits, &prob, kFALSE);
174 if (medianNumberOfHits <= 0) {
175 B2WARNING(
"Median number of hits per senor is smaller <1. Raise median to 1 instead.");
176 medianNumberOfHits = 1;
178 B2RESULT(
"Median of hits is " << medianNumberOfHits <<
" for sensor " <<
id);
185 if (medianNumberOfHits <
minHits) {
187 B2INFO(
"Not enough data: Median number of his is only " << medianNumberOfHits <<
"!");
190 B2WARNING(
"Not enough data: Median number of hits is only " << medianNumberOfHits <<
191 "! The masking continous but the mask may be empty.");
209 for (
auto sensBin = 1; sensBin <= collector_pxdhitcounts->GetXaxis()->GetNbins(); sensBin++) {
211 string sensorDescr = collector_pxdhitcounts->GetXaxis()->GetBinLabel(sensBin);
212 VxdID id(sensorDescr);
215 int nSensorHits = collector_pxdhitcounts->GetBinContent(sensBin);
216 if (nSensorHits == 0) {
222 string name = str(format(
"PXD_%1%_PixelHitmap") %
id.
getID());
223 auto collector_pxdhitmap = getObjectPtr<TH1I>(name.c_str());
224 if (collector_pxdhitmap ==
nullptr) {
225 B2WARNING(
"Cannot find PixelHitmap although there should be hits. This is strange!");
230 int nBins = collector_pxdhitmap->GetXaxis()->GetNbins();
233 if (medianNumberOfHits >=
minHits) {
237 vector<int> hitsAlongDrain(
c_nDrains, 0);
242 B2RESULT(
"Pixel hit threshold for dead rows is " << inefficientPixelHitThr <<
" for sensor " <<
id);
244 for (
auto bin = 1; bin <= nBins; bin++) {
249 int drainID = uCell * 4 + vCell % 4;
250 int nhits = collector_pxdhitmap->GetBinContent(bin);
251 hitsAlongDrain[drainID] += nhits;
252 hitsAlongRow[vCell] += nhits;
254 nDeadAlongRow[vCell] += 1;
258 B2INFO(
"Entering masking of dead rows ...");
259 for (
auto vCell = 0; vCell <
c_nVCells; vCell++) {
261 int nhits = hitsAlongRow[vCell];
262 int nDeadPixels = nDeadAlongRow[vCell];
266 B2RESULT(
"Dead row with vCell=" << vCell <<
" on sensor " <<
id);
272 B2INFO(
"Entering masking of dead drains ...");
273 for (
auto drainID = 0; drainID <
c_nDrains; drainID++) {
275 int nhits = hitsAlongDrain[drainID];
279 B2RESULT(
"Dead drain line at drainID=" << drainID <<
" on sensor " <<
id);
284 B2INFO(
"Entering masking of single dead pixels ...");
285 for (
auto bin = 1; bin <= nBins; bin++) {
287 int nhits = collector_pxdhitmap->GetBinContent(bin);
291 int drainID = uCell * 4 + vCell % 4;
293 if (nhits == 0 && !deadPixelsPar->
isDeadRow(
id.getID(), vCell) && !deadPixelsPar->
isDeadDrain(
id.getID(), drainID)) {
296 B2RESULT(
"Dead single pixel with ucell=" << uCell <<
", vcell=" << vCell <<
" on sensor " <<
id);
304 vector<float> unmaskedHitsAlongDrain(
c_nDrains, 0);
305 vector<int> unmaskedCellsAlongDrain(
c_nDrains, 0);
308 vector<float> unmaskedHitsAlongRow(
c_nVCells, 0);
309 vector<int> unmaskedCellsAlongRow(
c_nVCells, 0);
313 B2RESULT(
"Pixel hit threshold is " << pixelHitThr <<
" for sensor " <<
id);
316 for (
auto bin = 1; bin <= nBins; bin++) {
321 int drainID = uCell * 4 + vCell % 4;
324 float nhits = collector_pxdhitmap->GetBinContent(bin);
327 if (nhits > pixelHitThr) {
331 B2RESULT(
"Masking single pixel with ucell=" << uCell <<
", vcell=" << vCell <<
" on sensor " <<
id);
337 ++unmaskedCellsAlongDrain[drainID];
338 unmaskedHitsAlongDrain[drainID] += nhits;
339 ++unmaskedCellsAlongRow[vCell];
340 unmaskedHitsAlongRow[vCell] += nhits;
346 B2RESULT(
"Drain hit threshold is " << drainHitThr <<
" for sensor " <<
id);
348 for (
auto drainID = 0; drainID <
c_nDrains; drainID++) {
349 if (unmaskedCellsAlongDrain[drainID] > 0) {
351 float nhits = unmaskedHitsAlongDrain[drainID] / unmaskedCellsAlongDrain[drainID];
353 if (nhits > drainHitThr) {
354 for (
auto iGate = 0; iGate < 192; iGate++) {
355 int uCell = drainID / 4;
356 int vCell = drainID % 4 + iGate * 4;
359 B2RESULT(
"Masking drain line with drainID=" << drainID <<
" on sensor " <<
id);
367 B2RESULT(
"Row hit threshold is " << rowHitThr <<
" for sensor " <<
id);
369 for (
auto vCell = 0; vCell <
c_nVCells; vCell++) {
370 if (unmaskedCellsAlongRow[vCell] > 0) {
372 float nhits = unmaskedHitsAlongRow[vCell] / unmaskedCellsAlongRow[vCell];
374 if (nhits > rowHitThr) {
375 for (
auto uCell = 0; uCell <
c_nUCells; uCell++)
378 B2RESULT(
"Masking complete row with vCell=" << vCell <<
" on sensor " <<
id);
388 int numberOfUnmaskedHits = 0;
390 for (
auto bin = 1; bin <= nBins; bin++) {
394 if (maskedPixelsPar->
pixelOK(
id.getID(), pixID)) {
395 numberOfUnmaskedHits += collector_pxdhitmap->GetBinContent(bin);
400 float meanOccupancyAfterMasking = (float)numberOfUnmaskedHits / nevents /
c_nVCells /
c_nUCells;
401 B2RESULT(
"Hotpixel filtered occupancy sensor=" <<
id <<
" is " << meanOccupancyAfterMasking);
407 auto id = elem.first;
408 auto singles = elem.second;
409 B2RESULT(
"SensorID " <<
VxdID(
id) <<
" has filtered occupancy of " << occupancyInfoPar->
getOccupancy(
id));
410 B2RESULT(
"SensorID " <<
VxdID(
id) <<
" has fraction of masked pixels of " << (
float)singles.size() / (
c_nVCells *
c_nUCells));
422 B2INFO(
"PXDHotPixelMask Calibration Successful");
Base class for calibration algorithms.
void saveCalibration(TClonesArray *data, const std::string &name)
Store DBArray payload with given name with default IOV.
std::vector< Calibration::ExpRun > getRunListFromAllData() const
Get the complete list of runs from inspection of collected data.
void setDescription(const std::string &description)
Set algorithm description (in constructor)
const std::vector< Calibration::ExpRun > & getRunList() const
Get the list of runs for which calibration is called.
std::string getPrefix() const
Get the prefix used for getting calibration data.
EResult
The result of calibration.
@ c_OK
Finished successfully =0 in Python.
@ c_NotEnoughData
Needs more data =2 in Python.
The payload telling which PXD pixel is dead (=Readout system does not receive signals)
void maskSensor(unsigned short sensorID)
Mask sensor.
void maskRow(unsigned short sensorID, unsigned int vCellID)
Mask single row.
void maskSinglePixel(unsigned short sensorID, unsigned int pixID)
Mask single pixel.
void maskDrain(unsigned short sensorID, unsigned int drainID)
Mask single drain.
bool isDeadDrain(unsigned short sensorID, unsigned int drainID) const
Check whether a drain is dead.
bool isDeadRow(unsigned short sensorID, unsigned int vCellID) const
Check whether a row is dead.
float inefficientPixelMultiplier
The occupancy threshold for inefficient (or dead) pixels is the median occupancy x inefficientPixelMu...
bool maskRows
Mask hot rows with too high average occupancy after single pixel masking.
int minEvents
Minimum number of collected events.
std::map< VxdID, double > m_medianOfHitsMap
map of VxdID to median hits of each sensor
float drainMultiplier
The occupancy threshold for masking hot drains is the median occupancy x drainMultiplier.
int minInefficientPixels
The minimum number of inefficient (or dead) pixels per row to define an inefficient row.
const unsigned short c_nDrains
Number of drain lines of Belle II PXD sensors.
bool maskDrains
Mask hot drain lines with too high average occupancy after single pixel masking.
const unsigned short c_nVCells
Number of vCells of Belle II PXD sensors.
int m_debugHisto
Set if a debugging histogram should be created in the algorithm output directory.
float pixelMultiplier
The occupancy threshold for masking hot single pixels is the median occupancy x pixelMultiplier.
const unsigned short c_nUCells
Number of uCells of Belle II PXD sensors.
std::shared_ptr< TFile > m_file
Pointer for TFile.
PXDHotPixelMaskCalibrationAlgorithm()
Constructor set the prefix to PXDHotPixelMaskCalibrationAlgorithm.
float rowMultiplier
The occupancy threshold for masking hot rows is the median occupancy x rowMultiplier.
virtual EResult calibrate() override
Run algo on data.
bool forceContinueMasking
Force continue masking in almost empty runs instead of returning c_NotEnoughData.
int minHits
Minimum median number of hits per pixel needed for dead pixel masking.
std::string deadPixelPayloadName
Payload name for PXDDeadPixelPar used for more defective pixels from damaged gates.
void createDebugHistogram()
Perform debug histogram file creation.
The payload telling which PXD pixel to mask (ignore)
bool pixelOK(unsigned short sensorID, unsigned int pixID) const
Check whether a pixel on a given sensor is OK or not.
const std::unordered_map< unsigned short, MaskedSinglePixelsSet > & getMaskedPixelMap() const
Return unordered_map with all masked single pixels in PXD.
void maskSinglePixel(unsigned short sensorID, unsigned int pixID)
Mask single pixel.
The payload collecting some meta information from running the masking algorithm.
void setOccupancy(unsigned short sensorID, float occupancy)
Set occupancy.
void setNumberOfEvents(int nEvents)
Set number of events used for occupancy estimation.
float getOccupancy(unsigned short sensorID) const
Get occupancy.
Class to uniquely identify a any structure of the PXD and SVD.
int getID(const std::vector< double > &breaks, double t)
get id of the time point t
Abstract base class for different kinds of events.