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