ATLAS Offline Software
Loading...
Searching...
No Matches
G4DebuggerUtils.py
Go to the documentation of this file.
1import ROOT
2import operator
3import sys
4
5from G4DebuggerConfig import *
6
7if AtlasStyle:
8 print("setting ATLAS Style")
9 ROOT.SetAtlasStyle()
10
11def rename(label):
12 if label == "FCO":
13 return "FC Other"
14 elif label == "HCB":
15 return "Tile"
16 elif label == "Sev":
17 return "Services"
18 else:
19 return label
20
21def fillHisto(h,catDict,i,p,cat):
22 if cat in catDict.keys() and p in catDict[cat].keys():
23 h.SetBinContent(i,catDict[cat][p])
24 else:
25 h.SetBinContent(i,0)
26 if not cat in translate.keys():
27 h.GetXaxis().SetBinLabel(i, cat)
28 else:
29 h.GetXaxis().SetBinLabel(i, translate[cat])
30
31def setFillLineColor(h,p, hcolors):
32 h.SetFillColor(hcolors[p])
33 if p not in samecolor:
34 if p in linecolor:
35 h.SetLineColor(linecolor[p])
36 else:
37 h.SetLineColor(hcolors[p]+2)
38 else:
39 h.SetLineColor(hcolors[p])
40 if p in styles.keys():
41 h.SetFillStyle(styles[p])
42
43def divide(hs1,hs2):
44 h1 = hs1.GetStack().Last().Clone()
45 h2 = hs2.GetStack().Last().Clone()
46 name = h1.GetName()+"ratio"
47 nHalf = int(h1.GetNbinsX()/2)
48 h = ROOT.TH1F(name,name,nHalf,0,nHalf)
49 for i in range(nHalf):
50 denum = h1.GetBinContent(1+2*i) if h1.GetBinContent(1+2*i)!=0 else -1
51 h.SetBinContent(i+1,h2.GetBinContent(1+2*i+1)/denum)
52 binLabel = rename(h1.GetXaxis().GetBinLabel(1+2*i))
53 h.GetXaxis().SetBinLabel(1+i, binLabel)
54 return h
55
56
57def plotSummaryRatio(dict1, dict2, xaxis, v1, v2, directory="", name =""):
58 NCats = len(dict1)
59 integral1 = sum([sum(dict1[cat].values()) for cat in dict1])
60 integral2 = sum([sum(dict2[cat].values()) for cat in dict2])
61 print(NCats," ",integral1," ",integral2)
62 sorted1 = sorted(dict1.items(), key=lambda kv: sum(kv[1].values()), reverse=True)
63 uniqueName = name + sorted1[0][0]
64 hmap1 = {}
65 hmap2 = {}
66
67 nevents = 1000.
68
69 leg = ROOT.TLegend(0.7,0.89-0.07*len(particles),0.92,0.89)
70 leg.SetNColumns(1)
71 leg.SetBorderSize(0)
72 leg.SetFillColor(0)
73 leg.SetFillStyle(0)
74 leg.SetTextSize(0.06)
75
76 for p in particles:
77 h1 = ROOT.TH1F(uniqueName+"1"+p,uniqueName+"1"+p,2*len(sorted1),0,2*len(sorted1))
78 h2 = ROOT.TH1F(uniqueName+"2"+p,uniqueName+"2"+p,2*len(sorted1),0,2*len(sorted1))
79 i_adjusted = 0
80 for i,a in enumerate(sorted1):
81 cat = a[0]
82 if cat == "other":
83 continue
84 fillHisto(h1,dict1,2*i_adjusted+1,p,cat)
85 fillHisto(h2,dict2,2*i_adjusted+1+1,p,cat)
86 i_adjusted += 1
87 if not xaxis == "Processes":
88 fillHisto(h1,dict1,2*len(sorted1)-1,p,"other")
89 fillHisto(h2,dict2,2*len(sorted1),p,"other")
90 hmap1[p] = h1
91 hmap2[p] = h2
92
93 hs1 = ROOT.THStack()
94 hs2 = ROOT.THStack()
95 for p in particles:
96 hmap1[p].Scale(1./nevents)
97 hmap2[p].Scale(1./nevents)
98 hs1.Add(hmap1[p])
99 hs2.Add(hmap2[p])
100 setFillLineColor(hmap1[p],p, colors)
101 setFillLineColor(hmap2[p],p, colorsNew)
102 for p in reversed(particles):
103 if p in translate.keys() and p != 'other':
104 leg.AddEntry(hmap1[p],"#font[42]{%s}"%translate[p],"f")
105 else:
106 leg.AddEntry(hmap1[p],"#font[42]{%s}"%p,"f")
107
108 canv, pad1, pad2 = summary.getCanvas(uniqueName)
109
110 h2 = divide(hs1,hs2)
111
112 pad1.cd()
113
114 hs1.SetMaximum(hs1.GetStack().Last().GetBinContent(1)*1.2)
115 hs1.Draw("hist")
116 hs2.Draw("hist same")
117 if xaxis == "Processes":
118 hs1.GetXaxis().SetRangeUser(0,40)
119 hs2.GetXaxis().SetRangeUser(0,40)
120
121 hs1.GetYaxis().SetTitleOffset(0.7)
122 leg.Draw()
123 if AtlasStyle:
124 ROOT.ATLASLabel(0.3,0.84,"Simulation Preliminary",1,0.06)
125 ROOT.myText(0.3,0.77,1,"#sqrt{s}=13 TeV, 10k t#bar{t} events",0.06)
126 ROOT.myText(0.3,0.70,1,"NRR (E = 1 MeV, w = 10)",0.06)
127 ROOT.myText(0.3,0.63,1,"#oplus EM Range Cuts",0.06)
128
129 summary.configureUpperPad(hs1)
130
131 pad1.Update()
132 ymax = ROOT.gPad.GetUymax()*0.55
133 ymin = ROOT.gPad.GetUymin()
134 l1 = ROOT.TLine(6,ymin,6,ymax)
135 l1.SetLineStyle(2)
136 l1.Draw()
137 l2 = ROOT.TLine(7,ymin,7,ymax)
138 l2.SetLineStyle(2)
139 l2.Draw()
140 l3 = ROOT.TLine(8,ymin,8,ymax)
141 l3.SetLineStyle(2)
142 l3.Draw()
143 ROOT.myTextAbs(6.1, 0.8*ymax, 1, "Non optimized", 0.055)
144 ROOT.myTextAbs(7.1, 0.6*ymax, 1, "Optimized", 0.055)
145
146
147 pad2.cd()
148 pad2.SetGridy()
149 h2.Draw("hist")
150 if xaxis == "Processes":
151 h2.GetXaxis().SetRangeUser(0,20)
152 summary.configureLowerPad(h2, 0.5, 0.1, xaxis, v2+" / "+v1)
153
154 canv.Print(os.path.join(directory,("%s.pdf" % xaxis)))
155 canv.Print(os.path.join(directory,("%s.png" % xaxis)))
156
157
158def plotHistogramRatio(h1,h2,v1,v2,lab,ALL,hisType,directory):
159
160 leg = ROOT.TLegend(0.18,0.74-0.05*len(h1),0.4,0.74)
161 leg.SetNColumns(2)
162 leg.SetBorderSize(0)
163 leg.SetFillColor(0)
164 leg.SetFillStyle(0)
165 leg.SetTextSize(0.045)
166
167 hs = ROOT.THStack()
168 hsR = ROOT.THStack()
169
170 underflow1 = 0
171 underflow2 = 0
172 all1 = 0
173 all2 = 0
174
175 uniqueName = ""
176
177 for p in particles:
178 if not p in h1.keys(): continue
179 if not p in h2.keys(): continue
180 uniqueName = h1[p].GetName()
181 hs.Add(h2[p])
182 hs.Add(h1[p])
183 h1[p].SetLineColor(colors[p]+2)
184 h2[p].SetLineColor(colorsNew[p])
185 underflow1 += h1[p].GetBinContent(0)
186 underflow2 += h2[p].GetBinContent(0)
187 all1 += h1[p].GetEntries()
188 all2 += h2[p].GetEntries()
189 # print(h1[p]," ",all1)
190 hR = h2[p].Clone()
191 hR.Divide(h1[p])
192 # hR.Smooth()
193 hR.SetMarkerSize(0)
194 hR.SetMarkerStyle(0)
195 hR.SetBinContent(0,1)
196 hsR.Add(hR)
197 leg.AddEntry(h1[p],"#font[42]{%s}"%p,"l")
198 leg.AddEntry(h2[p],"#font[42]{%s}"%p,"l")
199
200 if not uniqueName:
201 return
202
203 nologx = True
204 if hisType in ["numberOfSteps"]:
205 nologx = False
206 nology = False
207 if hisType in ["CumulativeInitialE","CumulativeNumberOfStepsPerInitialE"]:
208 nology = True
209 # if hisType in ["averageNumberOfStepsPerInitialE"]:
210 # nology = True
211 canv, pad1, pad2 = histograms.getCanvas(uniqueName+"canv", nologx, nology)
212
213 frac = 100*all1/ALL
214 frac1 = 100*underflow1/all1
215 frac2 = 100*underflow2/all2
216
217 pad1.cd()
218 hs.Draw("nostack hist")
219 if not nology:
220 hs.SetMaximum(hs.GetStack().Last().GetMaximum()*1e3)
221 if AtlasStyle:
222 ROOT.ATLASLabel(0.18,0.87,"Simulation Internal",1)
223 ROOT.myText(0.18,0.81,1,"#sqrt{s}=13 TeV, %s (%2.1f%s)" % (lab,frac,"%"))
224 # ROOT.myText(0.18,0.75,1,"particle: %s" % p)
225 underTxt = "under: %1.2E (%2.2f%s) %1.2E (%2.2f%s)" % (underflow1, frac1, "%", underflow2, frac2, "%")
226 ROOT.myText(0.51,0.81,1,underTxt)
227 ROOT.myText(0.58,0.87,1,"%s %s" % (v1,v2))
228 if hisType in ["numberOfSteps"]:
229 shiftx = 0.5
230 legTemp = leg.Clone()
231 legTemp.SetX1(legTemp.GetX1()+shiftx)
232 legTemp.SetX2(legTemp.GetX2()+shiftx)
233 legTemp.Draw()
234 ROOT.myText(0.18+shiftx,0.75,1,"%s: %s:" % (v1,v2))
235 else:
236 leg.Draw()
237 ROOT.myText(0.18,0.75,1,"%s: %s:" % (v1,v2))
238 yaxis = ""
239 if hisType in ["numberOfSteps","InitialE"]:
240 yaxis = "Tracks"
241 elif hisType in ["CumulativeInitialE"]:
242 yaxis = "Cumulative tracks"
243 elif hisType in ["CumulativeNumberOfStepsPerInitialE"]:
244 yaxis = "Cumulative steps"
245 elif hisType == "averageNumberOfStepsPerInitialE":
246 yaxis = "Avg. number of steps in track"
247 histograms.configureUpperPad(hs, yaxis)
248
249
250 pad2.cd()
251 hsR.Draw("nostack hist")
252 histograms.configureLowerPad(hsR, 0.5 if "Leng" in hisType else 1.0, labelAll[hisType], v2+"/"+v1)
253
254 canv.Print(os.path.join(directory,hisType+".pdf"))
255 canv.Print(os.path.join(directory,hisType+".png"))
256
257def plot2D(h,G4Version,v,ALL_STEPS,yaxis,directory):
258 if not h:
259 return
260 uniqueName = h.GetName()+G4Version+v
261 canv = ROOT.TCanvas(uniqueName+"c",uniqueName+"c",800,600)
262 canv.SetRightMargin(0.20)
263 canv.SetTopMargin(0.07)
264 if not yaxis:
265 canv.SetLogz()
266 nx = h.GetNbinsX()
267 ny = h.GetNbinsY()
268 integral = h.Integral(0,nx+1,0,ny+1)
269 h.Draw("COLZ")
270 h.GetXaxis().SetTitle("z [mm]")
271 h.GetYaxis().SetTitle("r [mm]")
272 h.GetZaxis().SetTitle("Steps" if not yaxis else yaxis)
273 h.GetZaxis().SetTitleOffset(1.2)
274 h.GetXaxis().SetNdivisions(505)
275 if AtlasStyle:
276 ROOT.ATLASLabel(0.18,0.87,"Simulation Internal",1)
277 ROOT.myText(0.18,0.81,1,"#sqrt{s}=13 TeV, G4.%s" % G4Version)
278 ROOT.myText(0.18,0.75,1,v + (" (%2.1f%s)" % (100*integral/ALL_STEPS,"%")) if ALL_STEPS else "")
279 ROOT.gPad.RedrawAxis()
280 if not yaxis:
281 canv.Print(os.path.join(directory,"2DMapGeant4.%s.pdf" % G4Version))
282 canv.Print(os.path.join(directory,"2DMapGeant4.%s.png" % G4Version))
283 else:
284 canv.Print(os.path.join(directory,"2DMapDiff.pdf"))
285 canv.Print(os.path.join(directory,"2DMapDiff.png"))
void print(char *figname, TCanvas *c1)
TGraphErrors * GetEntries(TH2F *histo)
void Scale(TH1 *h, double d=1)
plotSummaryRatio(dict1, dict2, xaxis, v1, v2, directory="", name="")
plotHistogramRatio(h1, h2, v1, v2, lab, ALL, hisType, directory)
fillHisto(h, catDict, i, p, cat)
plot2D(h, G4Version, v, ALL_STEPS, yaxis, directory)