13 This module contains classes for plotting calibration constants.
16 __all__ = [
"ConditionCheckerBase",
"PXDMaskedPixelsChecker",
"PXDDeadPixelsChecker",
"PXDOccupancyInfoChecker"]
18 from abc
import ABC, abstractmethod
19 from ROOT
import Belle2
22 from root_numpy
import array2hist
23 from pxd.utils import get_sensor_graphs, get_sensor_maps, sensorID_list
24 from pxd.utils import latex_r, nPixels, nVCells, nUCells
37 type_list = [
'hot',
'dead',
'hot_dead',
'occ_no_mask',
'occ_masked']
39 label_list = [
'Hot pixels',
'Dead pixels',
'Hot/Dead pixels',
'No Mask',
'With Mask']
40 color_list = [ROOT.kRed + 1, ROOT.kAzure - 5, ROOT.kGray + 2, ROOT.kOrange + 1, ROOT.kSpring + 5]
41 max_list = [4., 4., 20., 3., 3.]
43 _type: {
'label': _label,
'color': _color,
'max': _max}
44 for (_type, _label, _color, _max)
in zip(type_list, label_list, color_list, max_list)
51 """Abstract base class describing interfaces to make plots from condition db."""
53 def __init__(self, name, objType, tfile, rundir="", use_hist=False):
69 if self.
rundirrundir !=
"":
92 DBObj type (read only)
112 Call functions at beginning of a run
119 Method to define TGraph
121 ytitle (str): Label for TGraph y-axis
124 self.
graphsgraphs.update(get_sensor_graphs(ytitle))
130 name (str): name for TH2, to which sensor name will be attached
131 title (str): title for TH2
132 ztitle (str): title for z-axis (color bar)
133 kwargs: additional arguments
137 self.
histshists.update(get_sensor_maps(name, title, ztitle, self.
runrun, **kwargs))
142 Abstract method to get content of a payload
143 Should return a Dictionary with sensorID.getID() as key and relaated calibration results as value
149 Abstract method to get a value for each TGraph
151 sensor_db_content (Any): Calibration results of a module
156 Method to get a value for each TGraph
158 h2 (TH2): If not none, get value from h2
164 Method to set TH2 bins
166 h2 (TH2): TH2F object for handling values of a pixel matrix
167 sensor_db_content (Any): Calibration results of a module
173 Method to fill plot objects
175 if self.exp < self.expstart:
176 self.expstart = self.exp
177 if self.run < self.runstart:
178 self.runstart = self.run
179 if self.run > self.runend:
180 self.runend = self.run
182 db_content = self.get_db_content()
185 for sensorID
in sensorID_list:
186 sensor_db_content = db_content[sensorID.getID()]
188 if not self.use_hist:
189 values_dict[sensorID.getID()] = self.get_graph_value(sensor_db_content)
190 gr = self.graphs[sensorID.getID()]
191 pre_values_dict[sensorID.getID()] = gr.GetPointY(gr.GetN() - 1)
198 if self.rundir !=
"" and self.hists:
199 h2 = self.hists[sensorID.getID()]
200 self.set_hist_content(h2, sensor_db_content)
202 h2.SetTitle(h2.GetTitle() + self.hist_title_suffix)
203 self.tfile.cd(self.rundir)
206 values_dict[sensorID.getID()] = self.get_graph_value_from_hist(h2)
208 if values_dict != pre_values_dict:
209 for sensorID
in sensorID_list:
210 gr = self.graphs[sensorID.getID()]
211 gr.SetPoint(gr.GetN(), self.run, values_dict[sensorID.getID()])
213 def draw_plots(self, canvas=None, cname="", ymin=0., ymax=None, logy=False):
215 Method to draw plots on a TCanvas
217 canvas (TCanvas): ROOT TCanvas for plotting
218 canme (str): Name of the canvas
219 ymin (float): Minimum value of y-axis for plotting
220 ymax (float): Maximum of y-axis for plotting
221 logy (bool): Flag to use log scale for y-axis
227 canvas.SetWindowSize(1000, 400)
228 canvas.SetLeftMargin(0.09)
229 canvas.SetRightMargin(0.05)
231 for i, sensorID
in enumerate(sensorID_list):
232 sensor_id = sensorID.getID()
235 ymax = np.array([np.array(gr.GetY())
for gr
in list(self.
graphsgraphs.values())
236 if isinstance(gr, ROOT.TGraph)]).max()
237 self.
graphsgraphs[sensor_id].SetMaximum(ymax)
238 self.
graphsgraphs[sensor_id].SetMinimum(ymin)
239 self.
graphsgraphs[sensor_id].Draw(
"AP")
241 self.
graphsgraphs[sensor_id].Draw(
"P")
242 if self.
graphsgraphs[
"TLegends"]:
243 for leg
in self.
graphsgraphs[
"TLegends"]:
245 self.
save_canvassave_canvas(canvas, cname, logy=logy)
249 Save TCanvas to png/pdf format and do not write it to the ROOT file by default
251 canvas (TCanvas): ROOT TCanvas for plotting
252 canme (str): Name of the canvas
253 logy (bool): Flag to use log scale for y-axis
256 canvas.SetName(cname)
257 exp_run = f
"e{self.expstart:05}_r{self.runstart:04}-{self.runend:04}"
259 latex_r.DrawLatex(0.95, 0.92,
"Belle II Experiment: " + exp_run.replace(
"_",
" "))
265 canvas.Print(f
"{exp_run}_{cname}_vs_run_logy.png")
266 canvas.Print(f
"{exp_run}_{cname}_vs_run_logy.pdf")
269 canvas.Print(f
"{exp_run}_{cname}_vs_run.png")
270 canvas.Print(f
"{exp_run}_{cname}_vs_run.pdf")
276 Checker for PXDOccupancyInfoPar
286 Method to define TGraph
292 for sensorID
in sensorID_list:
293 numID = sensorID.getID()
294 raw_occ = self.
dbObjdbObj.getOccupancy(numID)
296 content_dic[numID] = raw_occ
if raw_occ >= 0
else 0
301 return sensor_db_content * 100
305 Method to draw plots on a TCanvas
312 class PXDMaskedPixelsChecker(ConditionCheckerBase):
314 Checker for PXDMaskedPixelPar
324 Method to define TGraph
332 super().
define_hists(name=
"MaskedPixels", title=
"Masked Pixels", ztitle=
"isMasked")
335 return self.
dbObjdbObj.getMaskedPixelMap()
338 hotcount = len(sensor_db_content)
341 return hotcount * 100. / nPixels
345 for pixelID
in sensor_db_content:
346 uCell = int(pixelID / nVCells)
347 vCell = pixelID % nVCells
348 h2.SetBinContent(int(uCell + 1), int(vCell + 1), 1)
350 def draw_plots(self, canvas=None, cname="PXDHotPixel", ymin=0., ymax=plot_type_dict["hot"]["max"], **kwargs):
352 Method to draw plots on a TCanvas
354 super().
draw_plots(canvas=canvas, cname=cname, ymin=ymin, ymax=ymax, **kwargs)
359 Checker for PXDDeadPixelPar
362 def __init__(self, name, tfile, rundir="maps", use_hist=True):
369 Method to define TGraph
377 super().
define_hists(name=
"DeadPixels", title=
"Dead Pixels", ztitle=
"isDead")
380 deadsensormap = self.
dbObjdbObj.getDeadSensorMap()
381 deaddrainmap = self.
dbObjdbObj.getDeadDrainMap()
382 deadrowmap = self.
dbObjdbObj.getDeadRowMap()
383 deadsinglesmap = self.
dbObjdbObj.getDeadSinglePixelMap()
386 for sensorID
in sensorID_list:
387 numID = sensorID.getID()
388 content_dic[numID] = {}
389 if numID
in deadsensormap:
390 content_dic[numID][
"deadsensor"] =
True
392 content_dic[numID][
"deadsensor"] =
False
393 content_dic[numID][
"deaddrains"] = deaddrainmap[numID]
394 content_dic[numID][
"deadrows"] = deadrowmap[numID]
395 content_dic[numID][
"deadsingles"] = deadsinglesmap[numID]
400 if sensor_db_content[
"deadsensor"]:
403 n_deaddrains = len(sensor_db_content[
"deaddrains"])
404 n_deadrows = len(sensor_db_content[
"deadrows"])
405 if n_deaddrains == 0
or n_deadrows == 0:
407 deadcount = n_deaddrains * 192
411 deadcount += n_deadrows * 250
413 deadcount += len(sensor_db_content[
"deadsingles"])
416 h2 = list(get_sensor_maps(sensorID_list=sensorID_list[0:1]).values())[0]
418 deadcount = h2.Integral()
421 return min(deadcount, nPixels) * 100. / nPixels
424 return h2.Integral() * 100. / nPixels
428 if sensor_db_content[
"deadsensor"]:
429 ones = np.ones(nPixels)
432 for drainID
in sensor_db_content[
"deaddrains"]:
433 for iGate
in range(192):
435 vCell = drainID % 4 + iGate * 4
436 h2.SetBinContent(int(uCell + 1), int(vCell + 1), 1)
438 for vCell
in sensor_db_content[
"deadrows"]:
439 for uCell
in range(nUCells):
440 h2.SetBinContent(int(uCell + 1), int(vCell + 1), 1)
442 for pixelID
in sensor_db_content[
"deadsingles"]:
443 uCell = int(pixelID / nVCells)
444 vCell = pixelID % nVCells
445 h2.SetBinContent(int(uCell + 1), int(vCell + 1), 1)
447 def draw_plots(self, canvas=None, cname="PXDDeadPixel", ymin=0., ymax=plot_type_dict["dead"]["max"], **kwargs):
449 Method to draw plots on a TCanvas
451 super().
draw_plots(canvas=canvas, cname=cname, ymin=ymin, ymax=ymax, **kwargs)
456 Checker for PXDGainMapPar
466 Method to define TGraph
474 nU = self.
dbObjdbObj.getBinsU()
475 nV = self.
dbObjdbObj.getBinsV()
476 super().
define_hists(name=
"GainMap", title=
"Gain / MC default", ztitle=
"Relative gain", nUCells=nU, nVCells=nV)
479 gainMap = self.
dbObjdbObj.getCalibrationMap()
481 for sensorID
in sensorID_list:
482 numID = sensorID.getID()
483 content_dic[numID] = np.array(list(gainMap[numID]))
488 sensor_db_content (np.array): Array of gain factors of a module
490 return sensor_db_content.mean()
493 array2hist(sensor_db_content.reshape(h2.GetNbinsX(), h2.GetNbinsY()), h2)
495 def draw_plots(self, canvas=None, cname="PXDGain", ymin=0.5, ymax=2.5, **kwargs):
497 Method to draw plots on a TCanvas
499 super().
draw_plots(canvas=canvas, cname=cname, ymin=ymin, ymax=ymax, **kwargs)
The payload telling which PXD pixel is dead (=Readout system does not receive signals)
The payload class for PXD gain corrections.
The payload telling which PXD pixel to mask (ignore)
The payload collecting some meta information from running the masking algorithm.
Class to access a DBObjPtr from Python.
a (simplified) python wrapper for StoreObjPtr.
eventMetaData
pointer to the event metadata
def set_hist_content(self, h2, sensor_db_content)
graphs
Dictionary of plots (TGraph) summarizing variables vs run.
dbObj
DBObjPtr for the condition.
use_hist
Flag to get TGraph values from a histogram (TH2F)
rundir
Directory for writing histograms of each run.
def save_canvas(self, canvas, cname, logy=False)
def draw_plots(self, canvas=None, cname="", ymin=0., ymax=None, logy=False)
def define_hists(self, name=None, title=None, ztitle=None, **kwargs)
hist_title_suffix
Hist title suffix.
def define_graphs(self, ytitle="")
def get_graph_value_from_hist(self, h2=None)
tfile
TFile for saving plots.
def get_graph_value(self, sensor_db_content)
hists
Dictionary of plots (TH1) to be saved for each run.
def __init__(self, name, objType, tfile, rundir="", use_hist=False)
def set_hist_content(self, h2, sensor_db_content)
hist_title_suffix
Hist title suffix.
def get_graph_value_from_hist(self, h2)
def draw_plots(self, canvas=None, cname="PXDDeadPixel", ymin=0., ymax=plot_type_dict["dead"]["max"], **kwargs)
def __init__(self, name, tfile, rundir="maps", use_hist=True)
def get_graph_value(self, sensor_db_content)
def set_hist_content(self, h2, sensor_db_content)
def __init__(self, name, tfile, rundir="maps")
def draw_plots(self, canvas=None, cname="PXDGain", ymin=0.5, ymax=2.5, **kwargs)
def get_graph_value(self, sensor_db_content)
def set_hist_content(self, h2, sensor_db_content)
def __init__(self, name, tfile, rundir="maps")
hist_title_suffix
Hist title suffix.
def draw_plots(self, canvas=None, cname="PXDHotPixel", ymin=0., ymax=plot_type_dict["hot"]["max"], **kwargs)
def get_graph_value(self, sensor_db_content)
def __init__(self, name, tfile, rundir="")
def draw_plots(self, canvas=None, **kwargs)
def get_graph_value(self, sensor_db_content)
static ExpRun getRun(map< ExpRun, pair< double, double >> runs, double t)
Get exp number + run number from time.