-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAutoDQM.py
executable file
·414 lines (316 loc) · 11.4 KB
/
AutoDQM.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import ROOT
import os
import sys
import json
# Global variables to be filled by fill_vars
chi2_cut = None
pull_cap = None
pull_cut = None
ks_cut = None
min_entries = None
always_draw = None
norm_type = None
def fill_vars(histPair):
# Retrieve global variables
global chi2_cut
global pull_cap
global pull_cut
global ks_cut
global min_entries
global always_draw
global norm_type
# Fill global variables from hist or with defaults
chi2_cut = histPair.chi2_cut
pull_cap = histPair.pull_cap
pull_cut = histPair.pull_cut
ks_cut = histPair.ks_cut
min_entries = histPair.min_entries
always_draw = histPair.always_draw
norm_type = histPair.norm_type
return
# Parsing Functions -------
# Scan 2D Hist, plot Pull hist and compute Chi^2
def scan_2D(f_hist, r_hist, name, data_id, ref_id, targ_dir):
# Set up canvas
c = ROOT.TCanvas('c', 'Pull')
ROOT.gStyle.SetOptStat(0)
ROOT.gStyle.SetPalette(ROOT.kLightTemperature)
ROOT.gStyle.SetNumberContours(255)
# Variable declarations
is_good = True
chi2 = 0
is_outlier = False
max_pull = 0
nBins = 0
# Get empty clone of reference histogram for pull hist
pull_hist = r_hist.Clone("pull_hist")
pull_hist.Reset()
# Reject empty histograms
if f_hist.GetEntries() == 0 or f_hist.GetEntries() < min_entries:
is_good = False
# Normalize f_hist
if norm_type == "row":
normalize_rows(f_hist, r_hist)
else:
if f_hist.GetEntries() > 0:
f_hist.Scale(r_hist.GetEntries()/f_hist.GetEntries())
for x in range(1, r_hist.GetNbinsX()+1):
for y in range(1, r_hist.GetNbinsY()+1):
# Bin 1 data
bin1 = f_hist.GetBinContent(x, y)
# Bin 2 data
bin2 = r_hist.GetBinContent(x, y)
# Proper Poisson error
bin1err, bin2err = get_errors(bin1, bin2)
# Count bins for chi2 calculation
nBins += 1
# Ensure that divide-by-zero error is not thrown when calculating pull
if bin1err == 0 and bin2err == 0:
continue
# Calculate pull
new_pull = pull(bin1, bin1err, bin2, bin2err)
# Sum pulls
chi2 += new_pull**2
# Check if max_pull
if abs(new_pull) > abs(max_pull):
max_pull = new_pull
# Cap pull values displayed on histogram (max pull calculated before this)
if new_pull > pull_cap:
new_pull = pull_cap
if new_pull < -(pull_cap):
new_pull = -(pull_cap)
# Fill Pull Histogram
pull_hist.SetBinContent(x, y, new_pull)
# Compute chi2
chi2 = (chi2/nBins)
# Chi2 Cut
if (is_good and (chi2 > chi2_cut or abs(max_pull) > pull_cut)) or always_draw:
# Used in outliers count
if not always_draw:
is_outlier = True
# Plot pull hist
pull_hist.GetZaxis().SetRangeUser(-(pull_cap), pull_cap)
pull_hist.SetTitle(pull_hist.GetTitle()+" Pull Values")
pull_hist.Draw("colz")
# Text box
data_text = ROOT.TLatex(.52,.91,"#scale[0.6]{Data: "+data_id+"}")
ref_text = ROOT.TLatex(.72,.91,"#scale[0.6]{Ref: "+ref_id+"}")
data_text.SetNDC(ROOT.kTRUE);
ref_text.SetNDC(ROOT.kTRUE);
data_text.Draw()
ref_text.Draw()
c.SaveAs("{0}/pdfs/{1}_D{2}R{3}.pdf".format(targ_dir, name, data_id, ref_id))
# Write text file
new_txt = open("{0}/txts/{1}_D{2}R{3}.txt".format(targ_dir, name, data_id, ref_id), "w")
new_txt.writelines(["Run: {0}\n".format(data_id),
"Max Pull Value: {0}\n".format(max_pull),
"Chi^2: {0}\n".format(chi2),
"Data Entries: {0}\n".format(int(f_hist.GetEntries())),
"Reference Entries: {0}\n".format(int(r_hist.GetEntries()))])
new_txt.close()
return is_good, chi2, max_pull, is_outlier
# End Parsing Functions ------
# Comparison Plots ------
def draw_same(f_hist, r_hist, name, data_id, ref_id, targ_dir):
# Set up canvas
c = ROOT.TCanvas('c', 'c')
# Variable declarations
is_good = True
chi2 = 0
is_outlier = False
max_pull = 0
nBins = 0
ks = 0
# Reject empty histograms
if f_hist.GetEntries() == 0 or f_hist.GetEntries() < min_entries:
is_good = False
# Normalize f_hist
if f_hist.GetEntries() > 0:
f_hist.Scale(r_hist.GetEntries()/f_hist.GetEntries())
# Ensure plot accounts for maximum value
r_hist.SetMaximum(max(f_hist.GetMaximum(), r_hist.GetMaximum())*1.1)
ks = f_hist.KolmogorovTest(r_hist, "M")
if (is_good and ks > ks_cut) or always_draw:
# Used in outliers count
if not always_draw:
is_outlier = True
# Stat boxes only for hists that are always drawn
if always_draw:
ROOT.gStyle.SetOptStat(1)
r_hist.SetStats(True)
f_hist.SetStats(True)
else:
ROOT.gStyle.SetOptStat(0)
# Set hist style
r_hist.SetLineColor(28)
r_hist.SetFillColor(20)
r_hist.SetLineWidth(1)
f_hist.SetLineColor(ROOT.kRed)
f_hist.SetLineWidth(1)
# Plot hist
r_hist.Draw()
f_hist.Draw("sames hist e")
if always_draw:
# Draw stats boxes
r_hist.SetName("Reference")
f_hist.SetName("Data")
c.Update()
r_stats = r_hist.FindObject("stats")
f_stats = f_hist.FindObject("stats")
r_stats.SetY1NDC(0.15)
r_stats.SetY2NDC(0.30)
r_stats.SetTextColor(28)
r_stats.Draw()
f_stats.SetY1NDC(0.35)
f_stats.SetY2NDC(0.50)
f_stats.SetTextColor(ROOT.kRed)
f_stats.Draw()
# Text box
data_text = ROOT.TLatex(.52,.91,"#scale[0.6]{Data: "+data_id+"}")
ref_text = ROOT.TLatex(.72,.91,"#scale[0.6]{Ref: "+ref_id+"}")
data_text.SetNDC(ROOT.kTRUE);
ref_text.SetNDC(ROOT.kTRUE);
data_text.Draw()
ref_text.Draw()
c.SaveAs("{0}/pdfs/{1}_D{2}R{3}.pdf".format(targ_dir, name, data_id, ref_id))
# Write text file
new_txt = open("{0}/txts/{1}_D{2}R{3}.txt".format(targ_dir, name, data_id, ref_id), "w")
new_txt.writelines(["Run: {0}\n".format(data_id),
"Data Entries: {0}\n".format(int(f_hist.GetEntries())),
"Reference Entries: {0}\n".format(int(r_hist.GetEntries()))])
new_txt.close()
return is_good, ks, is_outlier
# End Comparison Plots
# Analysis Functions ------
def normalize_rows(f_hist, r_hist):
for y in range(1, r_hist.GetNbinsY()+1):
# Stores sum of row elements
rrow = 0
frow = 0
# Sum over row elements
for x in range(1, r_hist.GetNbinsX()+1):
# Bin data
rbin = r_hist.GetBinContent(x, y)
fbin = f_hist.GetBinContent(x, y)
rrow += rbin
frow += fbin
# Scaling factors
# Prevent divide-by-zero error
if frow == 0:
frow = 1
if frow > 0:
sf = float(rrow)/frow
else:
sf = 1
# Prevent scaling everything to zero
if sf == 0:
sf = 1
# Normalization
for x in range(1, f_hist.GetNbinsX()+1):
# Bin data
fbin = f_hist.GetBinContent(x, y)
fbin_err = f_hist.GetBinError(x, y)
# Normalize bin
f_hist.SetBinContent(x, y, (fbin * sf))
f_hist.SetBinError(x, y, (fbin_err * sf))
return
def pull(bin1, binerr1, bin2, binerr2):
'''
pull = [(data - expected)^2]/(sum of errors in quadrature)
data = |bin1 - bin2|, expected = 0
'''
pull = ((bin1 - bin2))/((binerr1**2 + binerr2**2)**(0.5))
return pull
def get_errors(bin1, bin2):
'''
bin1 = data
bin2 = reference
'''
alpha = 1-0.6827
if bin1 == 0:
m_error1 = 0
p_error1 = ROOT.Math.gamma_quantile_c(alpha/2, bin1+1, 1)
else:
m_error1 = ROOT.Math.gamma_quantile(alpha/2, bin1, 1)
p_error1 = ROOT.Math.gamma_quantile_c(alpha/2, bin1+1, 1)
if bin2 == 0:
m_error2 = 0
p_error2 = ROOT.Math.gamma_quantile_c(alpha/2, bin2+1, 1)
else:
m_error2 = ROOT.Math.gamma_quantile(alpha/2, bin2, 1)
p_error2 = ROOT.Math.gamma_quantile_c(alpha/2, bin2+1, 1)
if bin1 > bin2:
bin1err = bin1 - m_error1
bin2err = p_error2 - bin2
else:
bin2err = bin2 - m_error2
bin1err = p_error1 - bin1
return bin1err, bin2err
# End Analysis Functions ------
# AutoDQM
def autodqm(hists, data_id, ref_id, targ_dir):
if not os.path.exists(targ_dir):
os.makedirs(targ_dir)
for d in ['pdfs', 'txts']:
if not os.path.exists(targ_dir + d):
os.makedirs(targ_dir + d)
# Ensure no graphs are drawn to screen and no root messages are sent to terminal
ROOT.gROOT.SetBatch(ROOT.kTRUE)
ROOT.gErrorIgnoreLevel = ROOT.kWarning
max_1D = 0
max_2D = 0
C = ROOT.TCanvas("C", "Chi2")
ks_1D = ROOT.TH1F("ks_1D", "Kolmogorov-Smirnov Test for TH1F's", 20, 0, 1)
chi2_2D = ROOT.TH1F("chi2_2D", "#Chi^{2} for 2D Histograms", 60, 0, 300)
outliers = 0
skip = 0
# Main loop over histogram config objects in input
for histPair in hists:
fill_vars(histPair)
if type(histPair.data) == ROOT.TH1F:
is_good, ks, is_outlier = draw_same(histPair.data, histPair.ref, histPair.name_out, data_id, ref_id, targ_dir)
if is_good:
ks_1D.Fill(ks)
if ks > max_1D:
max_1D = ks
if is_outlier:
outliers += 1
elif type(histPair.data) == ROOT.TH2F:
is_good, chi2, max_pull, is_outlier = scan_2D(histPair.data, histPair.ref, histPair.name_out, data_id, ref_id, targ_dir)
if is_good:
chi2_2D.Fill(chi2)
if chi2 > max_2D:
max_2D = chi2
if is_outlier:
outliers += 1
else:
skip += 1
ks_1D.GetXaxis().SetTitle("KS Value")
ks_1D.GetYaxis().SetTitle("Entries")
ks_1D.Draw("hist")
C.SaveAs("{0}/pdfs/ks_1D.pdf".format(targ_dir))
chi2_2D.GetXaxis().SetTitle("#Chi^{2}")
chi2_2D.GetYaxis().SetTitle("Entries")
chi2_2D.Draw("hist")
C.SaveAs("{0}/pdfs/chi2_2D.pdf".format(targ_dir))
return
if __name__ == "__main__":
# targ_dir = "/home/users/jguiang/public_html/dqm/pdfs"
# # Location of root files
# tfiles_dir = "/nfs-6/userdata/bemarsh/CSC_DQM/Run2017/SingleMuon/"
# tfiles = os.listdir(tfiles_dir)
# total_tfiles = (len(os.listdir(tfiles_dir)) - 1)
# # tfiles_dir = "/home/users/jguiang/projects/AutoDQM/test_files/"
# # tfiles = os.listdir(tfiles_dir)
# # Main dir = location of plots
# main_dir = "DQMData/Run {0}/CSC/Run summary/CSCOfflineMonitor/"
# # Reference file run number
# r_num = "301531"
targ_dir = "/home/users/jguiang/public_html/dqm_test/pdfs"
# Location of root files
tfiles_dir = "root_files/"
# Reference file run number
r_num = "301531"
auto_dqm(targ_dir, tfiles_dir, r_num)