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