Belle II Software  release-08-01-10
plotGainMaps.py
1 #!/usr/bin/env python3
2 
3 
10 
11 # Creates overview plots for gain calibrations
12 #
13 # At first, you can extract the gain calibration payloads from a localdb/centraldb using the tool
14 #
15 # b2conditionsdb-extract --exp 3 --runs 0-5614 --tag Calibration_Offline_Development --output gain_payloads.root PXDGainMapPar
16 #
17 # Secondly, execute the script as
18 #
19 # basf2 plotGainMaps.py
20 #
21 # basf2 plotGainMaps.py -- --maps
22 
23 
24 import basf2 as b2
25 import ROOT
26 from ROOT import Belle2
27 from array import array
28 
29 import argparse
30 parser = argparse.ArgumentParser(description="Plot gain maps")
31 parser.add_argument('--maps', dest='maps', action="store_true", help='Create maps from payloads. This can be slow!')
32 args = parser.parse_args()
33 
34 
35 sensor_list = [
36  Belle2.VxdID("1.1.1"), Belle2.VxdID("1.1.2"),
37  Belle2.VxdID("1.2.1"), Belle2.VxdID("1.2.2"),
38  Belle2.VxdID("1.3.1"), Belle2.VxdID("1.3.2"),
39  Belle2.VxdID("1.4.1"), Belle2.VxdID("1.4.2"),
40  Belle2.VxdID("1.5.1"), Belle2.VxdID("1.5.2"),
41  Belle2.VxdID("1.6.1"), Belle2.VxdID("1.6.2"),
42  Belle2.VxdID("1.7.1"), Belle2.VxdID("1.7.2"),
43  Belle2.VxdID("1.8.1"), Belle2.VxdID("1.8.2"),
44  Belle2.VxdID("2.4.1"), Belle2.VxdID("2.4.2"),
45  Belle2.VxdID("2.5.1"), Belle2.VxdID("2.5.2")]
46 
47 # Create output file wíth histos and plots
48 histofile = ROOT.TFile('gain_histos.root', 'RECREATE')
49 histofile.cd()
50 histofile.mkdir("maps")
51 
52 # Open file with extracted payloads
53 rfile = ROOT.TFile("gain_payloads.root", "READ")
54 b2.conditions = rfile.Get("conditions")
55 
56 gain_table = dict()
57 for sensorID in sensor_list:
58  gain_table[sensorID.getID()] = list()
59 run_list = list()
60 
61 for condition in b2.conditions:
62  if condition.PXDGainMapPar_valid:
63  run_list.append(condition.run)
64  for sensorID in sensor_list:
65 
66  nBinsU = condition.PXDGainMapPar.getBinsU()
67  nBinsV = condition.PXDGainMapPar.getBinsV()
68 
69  layer = sensorID.getLayerNumber()
70  ladder = sensorID.getLadderNumber()
71  sensor = sensorID.getSensorNumber()
72 
73  mean_gain = 0.0
74  for guID in range(nBinsU):
75  for gvID in range(nBinsV):
76  mean_gain += condition.PXDGainMapPar.getContent(sensorID.getID(), guID, gvID)
77 
78  mean_gain /= (nBinsU * nBinsV)
79  gain_table[sensorID.getID()].append(mean_gain)
80 
81  if args.maps:
82  name = f"Gains_{layer:d}_{ladder:d}_{sensor:d}_run_{condition.run:d}"
83  title = f"Relative energy calibration Sensor={layer:d}.{ladder:d}.{sensor:d} run={condition.run:d}"
84  gain_map = ROOT.TH2F(name, title, nBinsU, 0, nBinsU, nBinsV, 0, nBinsV)
85  gain_map.GetXaxis().SetTitle("uBin")
86  gain_map.GetYaxis().SetTitle("vBin")
87  gain_map.GetZaxis().SetTitle("rel. gain factor")
88  gain_map.SetStats(0)
89 
90  for guID in range(nBinsU):
91  for gvID in range(nBinsV):
92  gain = condition.PXDGainMapPar.getContent(sensorID.getID(), guID, gvID)
93  gain_map.SetBinContent(int(guID + 1), int(gvID + 1), gain)
94 
95  histofile.cd("maps")
96  gain_map.Write()
97 
98 histofile.cd()
99 c = ROOT.TCanvas('gain_vs_runno', 'Gain evolution vs. run number', 200, 10, 700, 500)
100 c.SetGrid()
101 
102 for sensorID in sensor_list:
103 
104  n = len(run_list)
105  x, y = array('d'), array('d')
106  for value in gain_table[sensorID.getID()]:
107  y.append(value)
108  for run in run_list:
109  x.append(run)
110 
111  gr = ROOT.TGraph(n, x, y)
112  gr.SetLineColor(ROOT.kBlue)
113  gr.SetLineWidth(4)
114  gr.SetName(f"graph_{sensorID.getID()}")
115  gr.SetMarkerColor(ROOT.kBlue)
116  gr.SetMarkerStyle(21)
117  gr.SetTitle(f'Gain evolution Sensor={sensorID}')
118  gr.GetXaxis().SetTitle('run number')
119  gr.GetYaxis().SetTitle('gain')
120  gr.Draw('AP')
121 
122  c.Update()
123  c.Modified()
124  c.Print(f'gains_vs_runno_{sensorID.getID()}.png')
125  c.Write()
126 
127 
128 rfile.Close()
129 histofile.Close()
Class to uniquely identify a any structure of the PXD and SVD.
Definition: VxdID.h:33