ATLAS Offline Software
Loading...
Searching...
No Matches
create_input.py
Go to the documentation of this file.
1# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
2
3# this file do not work out of the box
4
5import shutil
6import ROOT
7from array import array
8import numpy as np
9
10try:
11 ROOT.gROOT.ProcessLine(".x ~/atlasstyle/AtlasStyle.C")
12 ROOT.SetAtlasStyle()
13except AttributeError:
14 pass
15
16old_filename = "/home/turra/simon/latest_simon.root"
17old_file = ROOT.TFile.Open(old_filename)
18old_scale_histogram = old_file.Get("Scales/es2012c/alphaZee_errStat")
19old_ct_histogram = old_file.Get("Resolution/es2012c/ctZee_errStat")
20old_scale_sys = old_file.Get("Scales/es2012c/alphaZee_errSyst")
21old_ct_sys = old_file.Get("Resolution/es2012c/ctZee_errSyst")
22
23assert old_scale_histogram
24assert old_ct_histogram
25assert old_scale_sys
26assert old_ct_sys
27
28file_christophe = ROOT.TFile("~/ElectronEnergyScaleFactor.root")
29scales_christophe = file_christophe.Get("alpha68")
30ct_christophe = file_christophe.Get("sigma24")
31# sum of 8 / 13 TeV diff + 64 / 32 bins diff
32scales_sys_christophe = file_christophe.Get("systAlpha")
33ct_sys_christophe = file_christophe.Get("systSigma") # 8 / 13 TeV diff
34
35assert scales_christophe
36assert ct_christophe
37assert scales_sys_christophe
38assert ct_sys_christophe
39
40
41def qsum_histograms(histo1, histo2):
42 new_histo = histo1.Clone()
43 new_histo.Reset()
44 for ibin in range(histo1.GetNbinsX() + 2):
45 value1 = histo1.GetBinContent(ibin)
46 central_value = histo1.GetBinCenter(ibin)
47 ibin2 = histo2.FindBin(central_value)
48 value2 = histo2.GetBinContent(ibin2)
49 new_value = np.sqrt(value1 * value1 + value2 * value2)
50 new_histo.SetBinContent(ibin, new_value)
51 return new_histo
52
53
54def copy_bin(histo, eta_range):
55 """ avoid bin with value == 0 """
56 empty_bins_in_transition = set()
57 for eta in eta_range:
58 bin = histo.FindBin(eta)
59 if (histo.GetBinContent(bin) == 0):
60 empty_bins_in_transition.add(bin)
61 assert len(empty_bins_in_transition) == 1
62 empty_bin = list(empty_bins_in_transition)[0]
63 if eta_range[-1] > 0:
64 assert(histo_scale.GetBinContent(empty_bin - 1) != 0)
65 histo.SetBinContent(empty_bin, histo.GetBinContent(empty_bin - 1))
66 histo.SetBinError(empty_bin, histo.GetBinError(empty_bin - 1))
67 else:
68 assert(histo_scale.GetBinContent(empty_bin + 1) != 0)
69 histo.SetBinContent(empty_bin, histo.GetBinContent(empty_bin + 1))
70 histo.SetBinError(empty_bin, histo.GetBinError(empty_bin + 1))
71
72
73def copy_dir(source, destination):
74 # equivalent of cp -r source/* destination
75 savdir = ROOT.gDirectory
76 destination.cd()
77 # loop on all entries of this directory
78 for key in source.GetListOfKeys():
79 classname = key.GetClassName()
80 cl = ROOT.gROOT.GetClass(classname)
81 if (not cl):
82 continue
83 if (cl.InheritsFrom(ROOT.TDirectory.Class())):
84 d = destination.mkdir(key.GetName())
85 copy_dir(source.GetDirectory(key.GetName()), d)
86 destination.cd()
87 elif (cl.InheritsFrom(ROOT.TTree.Class())):
88 T = source.Get(key.GetName())
89 destination.cd()
90 newT = T.CloneTree(-1, "fast")
91 newT.Write()
92 elif (cl.InheritsFrom(ROOT.TList.Class())):
93 source.cd()
94 obj = key.ReadObj()
95 destination.cd()
96 obj.Write(obj.GetName(), ROOT.TObject.kSingleKey)
97 del obj
98 else:
99 source.cd()
100 obj = key.ReadObj()
101 destination.cd()
102 obj.Write()
103 del obj
104
105 destination.SaveSelf(True)
106 savdir.cd()
107
108
110 output_file = ROOT.TFile(path, "recreate")
111 output_file.mkdir("Scales")
112 output_file.mkdir("Resolution")
113 output_file.mkdir("PSRecalibration")
114 output_file.mkdir("S1Recalibration")
115 return output_file
116
117
118def create_new_directories(output_file):
119 output_file.Get("Scales").mkdir("es2015PRE")
120 output_file.Get("Resolution").mkdir("es2015PRE")
121 output_file.Get("PSRecalibration").mkdir("es2015PRE")
122 output_file.Get("S1Recalibration").mkdir("es2015PRE")
123
124
125def merge_histograms(old, new, merge_error=True):
126 new_binning = []
127 new_values = []
128 new_errors = []
129 UNDERFLOW = 0
130 OVERFLOW = new.GetNbinsX() + 1
131
132 for iold in range(1, old.GetNbinsX()):
133 low = old.GetBinLowEdge(iold)
134 r = low + old.GetBinWidth(iold)
135
136 il_new = new.FindFixBin(low)
137 ir_new = new.FindFixBin(r)
138 remainer = None
139
140 if il_new == UNDERFLOW and ir_new == UNDERFLOW:
141 new_binning.append((low, r))
142 new_values.append(old.GetBinContent(iold))
143 new_errors.append(old.GetBinError(iold))
144
145 elif il_new == UNDERFLOW and ir_new > UNDERFLOW and ir_new < OVERFLOW:
146 if abs(new.GetBinLowEdge(1) - low) < 1E-100:
147 continue
148 new_binning.append((low, new.GetBinLowEdge(1)))
149 new_values.append(old.GetBinContent(iold))
150 new_errors.append(old.GetBinError(iold))
151 if ir_new == OVERFLOW:
152 remainer = iold
153 break
154 last_old = iold
155
156 for inew in range(1, new.GetNbinsX() + 1):
157 low = new.GetBinLowEdge(inew)
158 r = low + new.GetBinWidth(inew)
159 new_binning.append((low, r))
160 new_values.append(new.GetBinContent(inew))
161 new_errors.append(new.GetBinError(inew))
162
163 if remainer is not None:
164 new_binning.append((new.GetBinLowEdge(new.GetNbinsX()),
165 old.GetBinLowEdge(remainer)
166 + old.GetBinWidth(remainer)))
167 new_values.append(old.GetBinContent(remainer))
168 new_errors.append(old.GetBinError(remainer))
169
170 for iold in range(last_old, old.GetNbinsX() + 1):
171 low = old.GetBinLowEdge(iold)
172 r = low + old.GetBinWidth(iold)
173
174 il_new = new.FindFixBin(low)
175 ir_new = new.FindFixBin(r)
176
177 if il_new == OVERFLOW and ir_new == OVERFLOW:
178 new_binning.append((low, r))
179 new_values.append(old.GetBinContent(iold))
180 new_errors.append(old.GetBinError(iold))
181 elif il_new < OVERFLOW and ir_new == OVERFLOW:
182 if abs(new.GetBinLowEdge(new.GetNbinsX() + 1) - r) < 1E-100:
183 continue
184 new_binning.append((new.GetBinLowEdge(new.GetNbinsX() + 1), r))
185 new_values.append(old.GetBinContent(iold))
186 new_errors.append(old.GetBinError(iold))
187
188 new_edges = array('f', [x[0] for x in new_binning] + [new_binning[-1][1]])
189 histo_type = type(new)
190 result = histo_type(new.GetName(), new.GetTitle(),
191 len(new_edges) - 1, new_edges)
192 for i, (v, e) in enumerate(zip(new_values, new_errors), 1):
193 result.SetBinContent(i, v)
194 if merge_error:
195 result.SetBinError(i, e)
196 return result
197
198
199histo_scale = merge_histograms(old_scale_histogram, scales_christophe)
200histo_ct = merge_histograms(old_ct_histogram, ct_christophe)
201
202
203copy_bin(histo_scale, np.arange(2, 2.7, 0.001))
204copy_bin(histo_scale, np.arange(-2.7, -2, 0.001))
205
206copy_bin(histo_ct, np.arange(2, 2.7, 0.001))
207copy_bin(histo_ct, np.arange(-2.7, -2, 0.001))
208
209
210histo_scale.SetTitle("Zee energy scale factor - es2015 prerecommendation")
211histo_ct.SetTitle("Zee additional constant term - es2015 prerecommendation")
212
213for h in histo_scale, histo_ct:
214 h.SetMarkerColor(ROOT.kBlack)
215 h.SetMarkerStyle(20)
216
217canvas1 = ROOT.TCanvas()
218histo_scale.Draw("")
219old_scale_histogram.Draw("sameL")
220scales_christophe.Draw("sameL")
221
222canvas2 = ROOT.TCanvas()
223histo_ct.Draw("L")
224old_ct_histogram.Draw("sameL")
225ct_christophe.Draw("sameL")
226
227histo_scale.SetName("alphaZee_errStat")
228histo_ct.SetName("ctZee_errStat")
229
230# this created a file structured as the official one, with empty directories
231# output_file = create_structured_file("calibration_constants_run2.root")
232print(old_filename)
233shutil.copy2(old_filename, "xxx.root")
234output_file = ROOT.TFile("xxx.root", "update")
235create_new_directories(output_file)
236
237output_file.GetDirectory("Scales").GetDirectory(
238 "es2015PRE").WriteObject(histo_scale, "alphaZee_errStat")
239output_file.GetDirectory("Resolution").GetDirectory(
240 "es2015PRE").WriteObject(histo_ct, "ctZee_errStat")
241
242# systematics
243
244new_scale_sys = qsum_histograms(scales_sys_christophe, old_scale_sys)
245new_ct_sys = qsum_histograms(ct_sys_christophe, old_ct_sys)
246new_scale_sys = merge_histograms(old_scale_sys, new_scale_sys, False)
247new_ct_sys = merge_histograms(old_ct_sys, new_ct_sys, False)
248new_scale_sys.SetTitle(
249 "es2015PRE scales sys = es2012c sys + 7/13 TeV diff + 34/68 bin diff")
250new_ct_sys.SetTitle("es2015 ct sys = es2012c sys + 7/13 TeV diff")
251output_file.GetDirectory("Scales").GetDirectory(
252 "es2015PRE").WriteObject(new_scale_sys, "alphaZee_errSyst")
253output_file.GetDirectory("Resolution").GetDirectory(
254 "es2015PRE").WriteObject(new_ct_sys, "ctZee_errSyst")
255
256legend3 = ROOT.TLegend(0.6, 0.7, 0.9, 0.9)
257legend3.SetBorderSize(0)
258canvas3 = ROOT.TCanvas()
259old_scale_sys.GetXaxis().SetTitle("#eta")
260old_scale_sys.GetYaxis().SetTitle("scale systematics")
261old_scale_sys.SetLineColor(ROOT.kRed)
262old_scale_sys.Draw()
263scales_sys_christophe.SetLineColor(ROOT.kBlue)
264scales_sys_christophe.Draw("same")
265new_scale_sys.Draw("same")
266legend3.AddEntry(scales_sys_christophe, "8/13 TeV diff + 34/68 bin diff", "L")
267legend3.AddEntry(old_scale_sys, "2012c systematics", "L")
268legend3.AddEntry(new_scale_sys, "sum (2015PRE)", "L")
269legend3.Draw()
270
271legend4 = ROOT.TLegend(0.6, 0.7, 0.9, 0.9)
272legend4.SetBorderSize(0)
273canvas4 = ROOT.TCanvas()
274old_ct_sys.GetXaxis().SetTitle("#eta")
275old_ct_sys.GetYaxis().SetTitle("ct systematics")
276old_ct_sys.SetLineColor(ROOT.kRed)
277old_ct_sys.Draw()
278ct_sys_christophe.SetLineColor(ROOT.kBlue)
279ct_sys_christophe.Draw("same")
280new_ct_sys.Draw("same")
281legend4.AddEntry(ct_sys_christophe, "8/13 TeV diff", "L")
282legend4.AddEntry(old_ct_sys, "2012c systematics", "L")
283legend4.AddEntry(new_ct_sys, "sum (2015PRE)", "L")
284legend4.Draw()
285
286# stefano input for sensitivity
287
288stefano_file = ROOT.TFile("egammaEnergyCorrectionDataMC15.root")
289copy_dir(stefano_file.GetDirectory("PSRecalibration"),
290 output_file.GetDirectory("PSRecalibration").GetDirectory("es2015PRE"))
291copy_dir(stefano_file.GetDirectory("S1Recalibration"),
292 output_file.GetDirectory("S1Recalibration").GetDirectory("es2015PRE"))
293
294# correction for uA2MeV first week 2015
295
296f_ua2mev = ROOT.TFile.Open("~/uA2MeV.root")
297histo_ua2mev = f_ua2mev.Get("histo_uA2MeV_week12")
298output_file.Get("Scales").Get("es2015PRE").WriteTObject(histo_ua2mev)
299
300input("Press Key")
void print(char *figname, TCanvas *c1)
STL class.
STL class.
T * Get(TFile &f, const std::string &n, const std::string &dir="", const chainmap_t *chainmap=0, std::vector< std::string > *saved=0)
get a histogram given a path, and an optional initial directory if histogram is not found,...
create_new_directories(output_file)
copy_bin(histo, eta_range)
copy_dir(source, destination)
create_structured_file(path)
merge_histograms(old, new, merge_error=True)
qsum_histograms(histo1, histo2)