Belle II Software  release-06-02-00
plotUtils.py
1 # !/usr/bin/env python3
2 
3 
10 
11 """
12 <header>
13 <noexecute>Used as library.</noexecute>
14 </header>
15 """
16 
17 import ROOT as R
18 
19 
20 def create1DHist(name, title, n_bins, x_min, x_max, x_label, y_label):
21  h = R.TH1F(name, title, n_bins, x_min, x_max)
22  h.GetXaxis().SetTitle(x_label)
23  h.GetYaxis().SetTitle(y_label)
24  return h
25 
26 
27 def addDetails(h, descr, check, contact_str, isShifter):
28  h.GetListOfFunctions().Add(R.TNamed("Description", descr))
29  h.GetListOfFunctions().Add(R.TNamed("Check", check))
30  h.GetListOfFunctions().Add(R.TNamed("Contact", contact_str))
31  if isShifter:
32  h.GetListOfFunctions().Add(R.TNamed("MetaOptions", "shifter"))
33 
34 
35 # constants
36 SVDContact = "SVD Software Group, svd-software@belle2.org"
37 
38 
39 # selection of different parts of detector
40 cut_L3 = R.TCut('layer==3')
41 cut_L4 = R.TCut('layer==4')
42 cut_L5 = R.TCut('layer==5')
43 cut_L6 = R.TCut('layer==6')
44 cut_L456 = R.TCut('(layer==4)||(layer==5)||(layer==6)')
45 cut_s = R.TCut('sensor_type==0') # slanted
46 cut_b = R.TCut('sensor_type==1') # barrel
47 cut_U = R.TCut('strip_dir==0') # U_P
48 cut_V = R.TCut('strip_dir==1') # V_N
49 cut_noU = R.TCut('strip_dir!=0') # V_P or -1
50 cut_noV = R.TCut('strip_dir!=1') # U_N or -1
51 cut_size1 = R.TCut('(cluster_size==1)')
52 cut_size2 = R.TCut('(cluster_size==2)')
53 cut_size3plus = R.TCut('(cluster_size>2)')
54 cut_oneTH = R.TCut('cluster_truehits_number==1') # one TrueHit associated with SVDCluster
55 cut_noUV = R.TCut('strip_dir==-1') # no U, no V
56 
57 
58 # default granurality
59 gD = ((cut_L3 + cut_b + cut_U, 'L3_barrel_U_side'),
60  (cut_L3 + cut_b + cut_V, 'L3_barrel_V_side'),
61  (cut_L456 + cut_b + cut_U, 'L456_barrel_U_side'),
62  (cut_L456 + cut_b + cut_V, 'L456_barrel_V_side'),
63  (cut_L456 + cut_s + cut_U, 'L456_slanted_U_side'),
64  (cut_L456 + cut_s + cut_V, 'L456_slanted_V_side'))
65 
66 gD2 = ((cut_L3 + cut_b, 'L3_barrel'),
67  (cut_L456 + cut_b, 'L456_barrel'),
68  (cut_L456 + cut_s, 'L456_slanted'))
69 
70 # granurality taking into account layers and type of sensor;
71 granulesLayersTypes = ((cut_L3 + cut_b, 'L3_barrel'),
72  (cut_L4 + cut_b, 'L4_barrel'),
73  (cut_L4 + cut_s, 'L4_slanted'),
74  (cut_L5 + cut_b, 'L5_barrel'),
75  (cut_L5 + cut_s, 'L5_slanted'),
76  (cut_L6 + cut_b, 'L6_barrel'),
77  (cut_L6 + cut_s, 'L6_slanted'))
78 
79 # granularity for time differences between neighbour layers
80 granulesTD = ((cut_L3, 'L3-L4'),
81  (cut_L4, 'L4-L5'),
82  (cut_L5, 'L5-L6'))
83 
84 granulesL3456 = ((cut_L3, 'L3456'),) # characteristic of track saved in 3rd layer cluster
85 
86 g_L3_V = ((cut_L3 + cut_V, 'L3_V'),)
87 
88 
89 def plotter(name, title, nbins, xmin, xmax, x_label, y_label,
90  granules,
91  tree, expr, cut,
92  descr, check, contact_str=SVDContact, isShifter=False):
93  for g in granules:
94  hName = f'{name}_{g[1]}'
95  h = create1DHist(hName, title, nbins, xmin, xmax, x_label, y_label)
96  if cut == "":
97  selection = g[0]
98  else:
99  selection = g[0] + cut
100  tree.Draw(f'{expr}>>{hName}', selection, 'goff')
101  addDetails(h, descr, check, contact_str, isShifter)
102  h.SetTitle(f'{title} ({g[1]})')
103  h.Write(hName)
104 
105 
106 def plotRegions(name, title, x_label, y_label,
107  granules,
108  tree, expr, cutALL, cut,
109  descr, check, contact_str=SVDContact, isShifter=False):
110  hName = f'{name}'
111  h = create1DHist(hName, title, len(granules), 1, len(granules) + 1, x_label, y_label)
112  h.GetYaxis().SetRangeUser(0, 1.4)
113  for i, g in enumerate(granules, 1):
114  h.GetXaxis().SetBinLabel(i, g[1])
115  if cutALL == "":
116  selectionALL = g[0]
117  else:
118  selectionALL = g[0] + cutALL
119  n_all = tree.Draw(f'{expr}', selectionALL, 'goff')
120  if cut == "":
121  selection = g[0]
122  else:
123  selection = g[0] + cut
124  n_selected = tree.Draw(f'{expr}', selectionALL + selection, 'goff')
125  h.SetBinContent(i, n_selected / n_all)
126  h.SetBinError(i, (n_selected / n_all) * (1 / n_selected + 1 / n_all)**0.5)
127  addDetails(h, descr, check, contact_str, isShifter)
128  h.SetTitle(f'{title}')
129  h.Write(hName)