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