ATLAS Offline Software
Loading...
Searching...
No Matches
HistUtils.py
Go to the documentation of this file.
2# Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3#
4# create all the histograms for each analysis - this will get called once for each
5# configured chain - we can set the HistPath either already here, or from the c++
6# code
7
8
9# actually create the monTool to go along with a specific chain
10# stores the histogram, binning, creates all the histograms etc
11
12# wrapper around montool.defineHistogram to simplify the required histogram names
13# eg for a TProfile, etc automatically add the second variable, and create the histogram
14# alias to avoid having to write pages of tedious boiler plate functions
15
16def _th1(m,n): return f"{m};{n}"
17def _tprof(m,n): return f"{m},{m}_weight;{n}"
18def _txy(m,n): return f"{m}__x,{m}__y;{n}"
19def _tyx(m,n): return f"{m}__y,{m}__x;{n}"
20
21_FORMATTERS = {
22 "TH1F": _th1,
23 "TProfile": _tprof,
24 "TH2F": _txy,
25 "TEfficiency": _tyx
26}
27
28
29
30# wrap the GeneriMonitoringTool calls, so we can properly mangle the
31# names automatically
32
33def defineHisto( montool, name, namespace="", **args ) :
34
35 # here we auto-generate the x and y variable names, so that we can
36 # also auto-generated them for a fill without needing to parse
37 # anything
38
39 mname = name
40 if namespace : mname = f"{namespace}_{name}"
41
42 htype = str(args.get("type", "")).strip()
43
44 # NB, we swap the default x and y variables for a TEfficiency,
45 # in order to avoid the egregious bool-before-float root
46 # TEfficiency::Fill() convention
47
48 formatter = _FORMATTERS.get(htype, _th1)
49 pname = formatter(mname, name)
50
51 # make sure that the histogram is created, even if it is never filled
52 args["opt"] = "kAlwaysCreate"
53
54 montool.defineHistogram(pname, **args)
55
56
57
58 # create the required histograms for the differential resolutions
59# NB: should probably also include the chi2 and the uncertainty histograms,
60# and maybe the slices, although they can be left up to the c++ code
61
62def resplot( montool, namespace, path, label, xbins=0, xmin=None, xmax=None, ybins=0, ymin=None, ymax=None):
63
64 vxbins = _getbins( namespace, xbins, xmin, xmax )
65 vybins = _getbins( namespace, ybins, ymin, ymax )
66
67 subpath = f"{path}/{namespace}"
68
69 defineHisto( montool, "2D", namespace=namespace, path=subpath, type="TH2F", title=f"2D ({label})", xbins=vxbins, ybins=vybins )
70
71 defineHisto( montool, "1D", path=subpath, type="TH1F", title=f"1D ({label})", xbins=vybins )
72 defineHisto( montool, "mean", path=subpath, type="TH1F", title=f"mean {label}", xbins=vxbins )
73 defineHisto( montool, "sigma", path=subpath, type="TH1F", title=f"sigma {label}", xbins=vxbins )
74
75
76
77
78def _getbins( name, xbins, xmin=None, xmax=None ):
79
80 if isinstance(xbins,list): return xbins
81 else:
82 if xmin >= xmax : print( name, "BINS OUT OF RANGE !", xbins, xmin, xmax )
83 width = (xmax - xmin) / xbins
84 limits = []
85 for i in range(xbins):
86 limits.append( xmin + i*width )
87
88 # return [xmin + i * width for i in range(bins + 1)]
89
90 return limits
91
92
93
94
95
96# generate the mongroup hist path from the analysis string
97
98def monGroup( analysis_chain ) :
99
100 from TrigInDetAnalysisExample.chainString import chainString
101
102 chain = chainString( analysis_chain )
103
104 mg = ""
105
106 if chain.head == "" : mg = "/Fullscan"
107 else : mg = "/"+chain.head
108
109 mg += "/"+chain.tail
110
111 if chain.roi : mg += "_"+chain.roi
112 if chain.vtx : mg += "_"+chain.vtx
113 if chain.element : mg += "_" + chain.element
114 if chain.extra : mg += "_" + chain.extra
115 if chain.passed : mg += "/DTE"
116
117 return mg
118
119
120
121# superior binning for numerous histograms ...
122# NB: these should probably go into their own file, rather than
123# hijack the HistUtils, but they are useful everywhere so
124# leave them here for the time being
125
126
127ptbins_mb = [ 0.2, 0.4, 0.6, 0.8,
128 1, 1.20226, 1.44544, 1.7378, 2.0893, 2.51189, 3.01995, 3.63078, 4.36516, 5.24807, 6.30957,
129 7.58578, 9.12011, 10.9648, 13.1826, 15.8489, 19.0546, 22.9087, 27.5423, 33.1131, 39.8107,
130 47.863, 57.544, 69.1831, 83.1764, 100 ]
131
132ptbins_default = [ 1, 1.20226, 1.44544, 1.7378, 2.0893, 2.51189, 3.01995, 3.63078, 4.36516, 5.24807, 6.30957,
133 7.58578, 9.12011, 10.9648, 13.1826, 15.8489, 19.0546, 22.9087, 27.5423, 33.1131, 39.8107,
134 47.863, 57.544, 69.1831, 83.1764, 100 ]
135
136
137vnbins_full = [ -0.5,
138 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5, 10.5, 11.5, 12.5,
139 13.5, 14.5, 15.5, 17.5, 18.5, 19.5, 21.5, 23.5, 24.5, 26.5, 28.5, 30.5, 32.5,
140 35.5, 37.5, 40.5, 43.5, 46.5, 50.5, 53.5, 57.5, 61.5, 66.5, 71.5, 76.5, 81.5,
141 87.5, 93.5, 100.5, 107.5, 114.5, 123.5, 131.5, 141.5, 150.5, 161.5, 172.5, 185.5, 198.5,
142 211.5, 226.5, 242.5, 259.5, 277.5, 297.5,
143 317.5, 340.5, 363.5, 389.5, 416.5, 445.5, 476.5, 509.5,
144 544.5, 582.5, 623.5, 666.5, 713.5, 762.5, 815.5, 872.5, 933.5, 998.5, 1067.5,
145 1141.5, 1221.5, 1305.5, 1396.5, 1493.5, 1597.5,
146 1708.5, 1827.5, 1953.5, 2089.5 ]
147
148
149vnbins_short = [
150 -0.5,
151 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5,
152 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 17.5, 18.5, 19.5,
153 21.5, 23.5, 24.5, 26.5, 28.5,
154 30.5, 32.5, 35.5, 37.5,
155 40.5, 43.5, 46.5,
156 50.5, 53.5, 57.5,
157 61.5, 66.5,
158 71.5, 76.5,
159 81.5, 87.5,
160 93.5,
161 100.5, 107.5, 114.5, 123.5, 131.5, 141.5, 150.5, 161.5, 172.5, 185.5, 198.5,
162 211.5, 226.5, 242.5, 259.5, 277.5, 297.5,
163 317.5, 340.5, 363.5, 389.5,
164 416.5, 445.5, 476.5,
165 509.5, 544.5, 582.5,
166 623.5, 666.5,
167 713.5, 762.5,
168 815.5, 872.5,
169 933.5, 998.5,
170 1067.5
171 ]
172
173
174d0bins_default = [ -5.0, -4.0, -3.0, -2.5,
175 -2.0, -1.8, -1.6, -1.4, -1.2,
176 -1.05, -0.95, -0.85, -0.75, -0.65, -0.55, -0.45, -0.35, -0.25, -0.15, -0.05,
177 0.05, 0.15, 0.25, 0.35, 0.45, 0.55, 0.65, 0.75, 0.85, 0.95, 1.05,
178 1.2, 1.4, 1.6, 1.8, 2.0,
179 2.5, 3.0, 4.0, 5.0 ]
180
181d0bins_lrt = [ -100.5, -97.5, -93.5, -90.5,
182 -86.5, -83.5, -80.5,
183 -77.5, -74.5, -71.5,
184 -69.5, -66.5, -63.5, -61.5,
185 -59.5, -57.5, -54.5, -52.5, -50.5,
186 -49.5, -47.5, -45.5, -43.5, -42.5, -40.5,
187 -39.5, -37.5, -36.5, -34.5, -33.5, -32.5, -31.5,
188 -29.5, -28.5, -27.5, -26.5, -25.5, -24.5, -23.5, -22.5, -21.5, -20.5,
189 -19.5, -18.5, -17.5, -16.5, -15.5, -14.5, -13.5, -12.5, -11.5, -10.5,
190 -9.5, -8.5, -7.5, -6.5, -5.5, -4.5, -3.5, -2.5, -1.5, -0.5,
191 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5,
192 10.5, 11.5, 12.5, 13.5, 14.5, 15.5, 16.5, 17.5, 18.5, 19.5,
193 20.5, 21.5, 22.5, 23.5, 24.5, 25.5, 26.5, 27.5, 28.5, 29.5,
194 31.5, 32.5, 33.5, 34.5, 36.5, 37.5, 39.5,
195 40.5, 42.5, 43.5, 45.5, 47.5, 49.5,
196 50.5, 52.5, 54.5, 57.5, 59.5,
197 61.5, 63.5, 66.5, 69.5,
198 71.5, 74.5, 77.5,
199 80.5, 83.5, 86.5,
200 90.5, 93.5, 97.5,
201 100.5 ]
202
203
204
void print(char *figname, TCanvas *c1)
resplot(montool, namespace, path, label, xbins=0, xmin=None, xmax=None, ybins=0, ymin=None, ymax=None)
Definition HistUtils.py:62
_getbins(name, xbins, xmin=None, xmax=None)
Definition HistUtils.py:78
monGroup(analysis_chain)
Definition HistUtils.py:98
defineHisto(montool, name, namespace="", **args)
Definition HistUtils.py:33