ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/bm409/autoPlotter.py
Revision: 1.11
Committed: Thu Aug 26 11:32:04 2010 UTC (14 years, 8 months ago) by bm409
Content type: text/x-python
Branch: MAIN
Changes since 1.10: +1 -1 lines
Log Message:
autoPlotter.py

File Contents

# User Rev Content
1 bm409 1.5 #!/usr/bin/env python
2    
3     '''Created by Bryn Mathias
4     bryn.mathias@cern.ch
5     '''
6    
7 bm409 1.1 import ROOT as Root
8 bm409 1.7 import math
9 bm409 1.1 from time import strftime
10     import os, commands
11 bm409 1.5 Root.gROOT.SetBatch(True) # suppress the creation of canvases on the screen.. much much faster if over a remote connection
12 bm409 1.1 Root.gROOT.SetStyle("Plain") #To set plain bkgds for slides
13     Root.gStyle.SetTitleBorderSize(0)
14     Root.gStyle.SetCanvasBorderMode(0)
15     Root.gStyle.SetCanvasColor(0)#Sets canvas colour white
16     Root.gStyle.SetOptStat(1110)#set no title on Stat box
17     Root.gStyle.SetLabelOffset(0.001)
18     Root.gStyle.SetLabelSize(0.05)
19     Root.gStyle.SetLabelSize(0.05,"Y")#Y axis
20     Root.gStyle.SetTitleSize(0.06)
21     Root.gStyle.SetTitleW(0.7)
22     Root.gStyle.SetTitleH(0.07)
23     Root.gStyle.SetOptTitle(1)
24     Root.gStyle.SetOptStat(0)
25 bm409 1.4 Root.gStyle.SetAxisColor(1, "XYZ");
26     Root.gStyle.SetStripDecimals(Root.kTRUE);
27     Root.gStyle.SetTickLength(0.03, "XYZ");
28     Root.gStyle.SetNdivisions(510, "XYZ");
29     Root.gStyle.SetPadTickX(1);
30     Root.gStyle.SetPadTickY(1);
31     Root.gStyle.SetLabelColor(1, "XYZ");
32     Root.gStyle.SetLabelFont(42, "XYZ");
33     Root.gStyle.SetLabelOffset(0.007, "XYZ");
34     Root.gStyle.SetLabelSize(0.05, "XYZ");
35 bm409 1.5 Root.gStyle.SetHatchesLineWidth(3)
36    
37    
38     #Please enter the integrated lumi for the plots here:
39 bm409 1.8 algo = "PF"
40 bm409 1.10 intlumi = 1.071 #inv pico barns
41 bm409 1.5 print "The Integrated Luminosity your plots are being scaled to is: ", intlumi , "pb^{-1}"
42 bm409 1.7 #Set your output file here
43 bm409 1.8 outputfile = "../pdfs/"+algo+"/"
44 bm409 1.5 print "you are outputting to: " ,outputfile
45 bm409 1.7 #SetWhere your results from your analysis are
46     resultsDir = " ../results/"
47 bm409 1.5
48 bm409 1.8 print "you are using the " , algo , " Jet collection"
49 bm409 1.6 #close list is a global variable that has the file name appended to it so at the end of each hist loop the open files are closed. It is cleared each time a new hist is called.
50 bm409 1.1
51 bm409 1.4 def GetHist(DataSetName,col,norm,Legend):
52 bm409 1.5 a = Root.TFile.Open(DataSetName) #open the file
53     closeList.append(a) # append the file to the close list
54     b = a.Get(DirKeys[dir].GetTitle()) #open the directory in the root file
55     Hist = b.Get(hist) # get your histogram by name
56 bm409 1.7 if Legend != 0:
57     leg.AddEntry(Hist,Legend,"LP") # add a legend entry
58 bm409 1.4 Hist.SetLineWidth(3)
59 bm409 1.5 Hist.SetLineColor(col) #set colour
60 bm409 1.4 if norm != 0:
61 bm409 1.5 Hist.Scale(intlumi/100.) #if not data normilse to the data by lumi, MC is by default weighted to 100pb-1, if you have changed this change here!
62 bm409 1.4 return Hist
63    
64    
65     def HistogramMaxY(H):
66     Nbins = H.GetNbinsX()
67     Entries = [H.GetBinContent(i) for i in range(1,Nbins+1)]
68     return max(Entries)
69 bm409 1.1
70     def HistogramMinX(H):
71     Nbins = H.GetNbinsX()
72     for x in range(0,Nbins):
73     if H.GetBinContent(x) != 0:
74     return H.GetBinLowEdge(x-2)
75     return 0
76    
77     def HistogramMaxX(H):
78     Nbins = H.GetNbinsX()
79     BackItr = range(0,Nbins)
80     BackItr.reverse()
81     for x in BackItr :
82     if H.GetBinContent(x) != 0:
83     return H.GetBinLowEdge(x+1)
84    
85 bm409 1.4
86    
87 bm409 1.7 def Systematics(H1,H2,H3):
88     Standard = H1.Clone()
89     UpperError = H2.Clone()
90     LowerError = H3.Clone()
91     for bin in range(1,Standard.GetNbinsX()):
92     StandardUpper = math.sqrt((Standard.GetBinError(bin))**2 + ((UpperError.GetBinContent(bin) - LowerError.GetBinContent(bin))/2)**2)
93     Standard.SetBinError(bin, StandardUpper)
94     return Standard
95    
96    
97     def PassingCutNumbers(Hist, lowerBound):
98     lowbin = Hist.FindBin(lowerBound)
99     errorVal = Root.Double(0)
100     passingCut = Hist.IntegralAndError(lowbin, Hist.GetNbinsX(), errorVal)
101     print passingCut, errorVal
102 bm409 1.11 textLine = "Sample =" + DirKeys[dir].GetTitle() + hist + " , Number passing cut of" + str(lowerBound) + " " + str(passingCut) + " is "+ str(errorVal) + "\n"
103 bm409 1.7 CutNumbers.write(textLine)
104     #+ str(passingCut) + " pm "
105 bm409 1.1
106    
107 bm409 1.8
108     # def MakeWebSiteEntry(hist):
109    
110    
111    
112 bm409 1.1 time = strftime("%Y_%m_%d")
113     print time
114     mergedPlots = Root.TFile("StandardPlots.root", "RECREATE")
115    
116    
117 bm409 1.7 #A file to open and scan for histograms inside directories
118 bm409 1.8 RootFileList = ["../results/AK5"+algo+"_JetMET_ALL_upto230810.root"]
119 bm409 1.7
120     CutNumbers = open(outputfile+"CutTable.txt",'w')
121 bm409 1.1
122 bm409 1.7 Webpage = open(outputfile+"StandardPlots"+time+".html",'w')
123 bm409 1.5 #put your name in the contact email field
124 bm409 1.1 header = '''
125     <body>
126     <CENTER>
127     <h1>Comparison of Basic quantities for N=2 and N>=3 Jets</h1>
128     <P>Data = JetMetTau 268nb, contact email bryn.mathias AT cern DOT ch</P>
129     <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="1200" cellpadding="3" cellspacing="3">
130     <tr>
131     <td>DiJet</td>
132 bm409 1.7 <td>=3 Jet</td>
133     <td>>=4 Jet</td>
134     <td>>=2 Jet</td>
135    
136 bm409 1.1 </tr>'''
137     footer = ''' </table>
138     </CENTER>
139     </body>'''
140     Webpage.write(header)
141    
142 bm409 1.4
143    
144 bm409 1.7 temp = Root.TFile.Open(RootFileList[0])
145 bm409 1.1 DirKeys = temp.GetListOfKeys()
146    
147     HistKeys = [ (dir.ReadObj()).GetListOfKeys() for dir in DirKeys]
148     HistNames = [ [k.GetName() for k in D] for D in HistKeys]
149 bm409 1.5 #Loops though all the histograms that have been read from the first input file, this is done by histogram name
150 bm409 1.1
151     closeList = []
152     for dir in range(0,len(DirKeys)):
153     for hist in HistNames[dir]:
154     if "/" in hist : continue
155 bm409 1.7 leg = Root.TLegend(0.7, 0.6, 0.89, 0.85)
156 bm409 1.1 leg.SetShadowColor(0)
157 bm409 1.7 leg.SetBorderSize(0)
158     leg.SetFillStyle(4100)
159 bm409 1.1 leg.SetFillColor(0)
160     leg.SetLineColor(0)
161 bm409 1.4 if "PFMetVsMHT" in hist: continue #This is the 2d Histogram in the file, dont want to plot this!
162 bm409 1.1 col=2
163     fileno = 1
164 bm409 1.5 MaxY = 1.0#These three are the low terms for the axis and are reset for each histogram see below
165     MaxX = 1.0# to see the numbers set by the functions that are defined in the beginning of the program
166     MinX = 1000.#
167 bm409 1.3 c1 = Root.TCanvas("canvas"+hist,"canname"+hist,1200,1200)
168 bm409 1.5 #Make two pads, one small for the ratio plot and one large for the plot its self
169 bm409 1.7 mainPad = Root.TPad("","",0.01,0.25,0.99,0.99);
170 bm409 1.1 mainPad.SetNumber(1);
171     mainPad.Draw();
172 bm409 1.7 ratioPad = Root.TPad("","",0.01,0.01,0.99,0.25);
173 bm409 1.1 ratioPad.SetNumber(2);
174     ratioPad.Draw();
175    
176 bm409 1.4 c1.cd(1)
177 bm409 1.5 #make your histograms form each file, read in the files you want below
178     # GetHist("RootFile",Colour, scale to lumi 0=false anything else = true, Legend entry)
179     #NB the order in which you book the histos is the order in which they appear in the legend
180 bm409 1.8 Data = GetHist("../results/AK5"+algo+"_JetMET_ALL_upto230810.root",1,0,"Data")
181     QCD = GetHist("../results/AK5"+algo+"_QCD_AllPtBins_7TeV_Pythia.root",Root.kPink+4,1,"QCD")
182 bm409 1.4 Data.SetMarkerStyle(20)
183     Data.SetLineWidth(3)
184     Data.SetLineColor(1)
185     Data.SetFillColor(1)
186 bm409 1.8 ZJets = GetHist("../results/AK5"+algo+"_ZJets_madgraph.root",Root.kTeal-7,1,"Z+Jets (Madgraph)")
187 bm409 1.9 Zinv = GetHist("../results/AK5Calo_zinvisible_jets.root",91,1,"Z->#nu#nu + Jets")
188 bm409 1.8 WJets = GetHist("../results/AK5"+algo+"_WJets_madgraph.root",Root.kPink+7,1,"W+Jets")
189     ttbar = GetHist("../results/AK5"+algo+"_ttbarTauola.root",Root.kBlue+1,1,"TTBar")
190     LM0 = GetHist("../results/AK5"+algo+"_LM0.root",2,1,"LM0")
191 bm409 1.4
192 bm409 1.7
193     # ScaledUp = GetHist("../results/AK5Calo_Scale1.1_QCD_Pythia6.root",Root.kTeal-7,1,0)
194     # ScaledDown = GetHist("../results/AK5Calo_Scale0.9_QCD_Pythia6.root",Root.kTeal-7,1,0)
195 bm409 1.5 #Make a histogram of the sum of all the SM backgrounds
196 bm409 1.4 Total = QCD.Clone()
197 bm409 1.8 # Total.Add(Zinv)
198 bm409 1.4 Total.Add(ZJets)
199     Total.Add(WJets)
200     Total.Add(ttbar)
201 bm409 1.10 if "AlphaT_all" in hist :
202     print PassingCutNumbers(LM0, 0.55)
203     print PassingCutNumbers(Data, 0.55)
204     print PassingCutNumbers(ttbar, 0.55)
205     print PassingCutNumbers(ZJets, 0.55)
206 bm409 1.5 #Some funkey stuff from Henning for drawing the total background with some interesting lines around it
207     hcen=Total.Clone()
208     herr=Total.Clone()
209 bm409 1.7 # Total = Systematics(Total,ScaledUp,ScaledDown)
210 bm409 1.5 herr.SetLineColor(Root.kTeal+3)
211     herr.SetMarkerColor(Root.kAzure+6)
212     herr.SetFillColor(Root.kAzure+6)
213    
214     herr.SetLineWidth(3)
215     Total.SetLineWidth(3)
216    
217     Total.SetFillColor(Root.kAzure+2)
218     Total.SetLineColor(Root.kAzure+2)
219     Total.SetFillStyle(3245)
220     hcen.SetFillColor(0)
221     hcen.SetMarkerColor(Root.kTeal+3)
222     hcen.SetLineColor(Root.kTeal+3)
223     hcen.SetLineWidth(3)
224    
225 bm409 1.8 leg.AddEntry(Total,"Standard Model","LCalo")
226 bm409 1.4
227 bm409 1.5 #Defind the ranges of the histogram for the two highest histograms ie the data and the total
228 bm409 1.4 if HistogramMinX(Total) < MinX :
229     MinX = HistogramMinX(Total)
230     if HistogramMaxX(Total) > MaxX :
231     MaxX = HistogramMaxX(Total)
232     if HistogramMaxY(Total) > MaxY :
233     MaxY = HistogramMaxY(Total)
234    
235     if HistogramMinX(Data) < MinX :
236     MinX = HistogramMinX(Data)
237     if HistogramMaxX(Data) > MaxX :
238     MaxX = HistogramMaxX(Data)
239     if HistogramMaxY(Data) > MaxY :
240     MaxY = HistogramMaxY(Data)
241    
242    
243 bm409 1.5 herr.GetXaxis().SetRangeUser(MinX,MaxX)
244 bm409 1.7 herr.GetYaxis().SetRangeUser(0.00001,MaxY*5.)
245 bm409 1.6 #Draw order is important!
246 bm409 1.5 herr.Draw("hist")
247     hcen.Draw("histsame")
248 bm409 1.7 Total.Draw("9E2same")
249 bm409 1.4 ZJets.Draw("9Sameh")
250 bm409 1.8 # Zinv.Draw("9SAMEh")
251 bm409 1.4 WJets.Draw("9SAMEh")
252     ttbar.Draw("9SAMEh")
253     QCD.Draw("9SAMEh")
254     LM0.Draw("9SAMEh")
255 bm409 1.5 #Draw Data last to get the points above everything else
256 bm409 1.4 Data.Draw("9SAMEP")
257    
258 bm409 1.5 #Draw Text for histogram titile and for lumi
259     prelim = Root.TLatex(0.5,0.75,"CMS preliminary 2010");
260 bm409 1.7 lumi = Root.TLatex(0.4,.85,"#scale[0.8]{#int L dt = " + str(intlumi) + "pb^{-1}, #sqrt{s} = 7 TeV}");
261 bm409 1.5 prelim.SetNDC()
262     lumi.SetNDC()
263     # prelim.Draw()
264     lumi.Draw()
265     title = Root.TLatex(0.05,0.95,(DirKeys[dir].GetTitle())+"_"+hist[0:-4])
266 bm409 1.4 title.SetNDC()
267 bm409 1.9 # title.Draw()
268 bm409 1.5 lumi.Draw("Same")
269    
270 bm409 1.4
271 bm409 1.5 #Ratio plot is between the sum of the background and the data - clone the two relevant histograms
272 bm409 1.4 RatioBottom = Total.Clone()
273     RatioTop = Data.Clone()
274 bm409 1.5 RatioTop.GetYaxis().SetTitle("data / sim")
275 bm409 1.1 RatioTop.Divide(RatioBottom)
276 bm409 1.4 c1.cd(1).Update()
277     # c1.cd(1).SetLogy()
278     leg.Draw()
279     # c1.cd(1).Update()
280 bm409 1.1 c1.cd(2)
281 bm409 1.7
282     RatioTop.SetTitleSize(0.1, "XYZ");
283     RatioTop.SetTitleOffset(0.55, "X");
284     RatioTop.SetTitleOffset(0.3, "Y");
285     RatioTop.SetLabelSize(0.06,"XY")
286    
287     # RatioTop.SetTitleXOffset(0.9);
288     # RatioTop.SetTitleYOffset(0.9);
289 bm409 1.4 RatioTop.GetXaxis().SetRangeUser(MinX,MaxX)
290     RatioTop.GetYaxis().SetRangeUser(0.,2.0)
291     RatioTop.Draw()
292 bm409 1.5 #Draw a line though the perfectly matching point
293 bm409 1.9 unity = Root.TBox(RatioTop.GetXaxis().GetBinLowEdge(RatioTop.GetXaxis().GetFirst()), 0.89,MaxX, 1.11);
294 bm409 1.1 unity.SetLineWidth(2);
295     # unity.SetLineStyle(Root.kDashed);
296     unity.SetLineColor(2);
297 bm409 1.9 unity.SetFillColor(2);
298     unity.SetFillStyle(3002)
299     unity.Draw();
300 bm409 1.4
301 bm409 1.10 if "all" in hist:
302 bm409 1.8 c1.cd(1).SetLogy()
303     c1.Update()
304     c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
305     c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
306 bm409 1.5 #Do not save all of the histograms produced by the analysis framework, only the ones containing all objects in your selection, if this is not what you want to do then remove the if "all in hist" line
307 bm409 1.1 if "all" in hist :
308 bm409 1.5 # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
309 bm409 1.1 if (DirKeys[dir].GetTitle())[0] != "n":
310    
311 bm409 1.9
312    
313     Images ="<tr> <td>"+ (DirKeys[dir].GetTitle()) + "_" + hist[0:-4] + "</td> <td> n"+ (DirKeys[dir].GetTitle()) + "_" + hist[0:-4] +"</td> </tr>"+ "<tr><td><a href=" + (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\"><img src=\"' + (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\" width =\"286\"' + 'height=\"286\" /></a></td>\n ' + "<td><a href=\""+"n"+ (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\"><img src=\"' + "n" + (DirKeys[dir].GetTitle()) + "_Log_"+ hist +'.png\" width =\"286\" height=\"286\" /></a></td>\n '
314 bm409 1.1 Webpage.write(Images)
315    
316     # mergedPlots.writeTObject(c1)
317     # c1.cd(1)
318     c1.cd(1).SetLogy(1)
319     c1.Update()
320     # c1.cd(2)
321     # RatioTop.Draw()
322    
323 bm409 1.8
324 bm409 1.1
325     if "AlphaT_0" in hist :
326     # c1.cd(2)
327     # RatioTop.Draw()
328 bm409 1.5 # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
329 bm409 1.1 if (DirKeys[dir].GetTitle())[0] != "n":
330    
331 bm409 1.9 Images ="<tr> <td>"+ (DirKeys[dir].GetTitle()) + "_" + hist[0:-4] + "</td> <td> n"+ (DirKeys[dir].GetTitle()) + "_" + hist[0:-4] +"</td> </tr>"+ "<tr><td><a href=" + (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\"><img src=\"' + (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\" width =\"286\"' + 'height=\"286\" /></a></td>\n ' + "<td><a href=\""+"n"+ (DirKeys[dir].GetTitle()) + "_Log_" + hist + '.png\"><img src=\"' + "n" + (DirKeys[dir].GetTitle()) + "_Log_"+ hist +'.png\" width =\"286\" height=\"286\" /></a></td>\n '
332     Webpage.write(Images)
333 bm409 1.1 # mergedPlots.writeTObject(c1)
334     c1.cd(1).SetLogy()
335     # c1.cd(2)
336     # RatioTop.Draw()
337    
338     c1.Update()
339 bm409 1.8 # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
340     # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
341 bm409 1.5
342 bm409 1.1 c1.cd(1).SetLogy(0)
343    
344     for a in closeList :
345     a.Close()
346     closeList = []
347     Webpage.write(footer)
348