ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/bm409/autoPlotter.py
Revision: 1.6
Committed: Tue Aug 10 08:12:40 2010 UTC (14 years, 8 months ago) by bm409
Content type: text/x-python
Branch: MAIN
CVS Tags: pre1
Changes since 1.5: +5 -7 lines
Log Message:
Stable working commented version

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     from time import strftime
9     import os, commands
10 bm409 1.5 Root.gROOT.SetBatch(True) # suppress the creation of canvases on the screen.. much much faster if over a remote connection
11 bm409 1.1 Root.gROOT.SetStyle("Plain") #To set plain bkgds for slides
12     Root.gStyle.SetTitleBorderSize(0)
13     Root.gStyle.SetCanvasBorderMode(0)
14     Root.gStyle.SetCanvasColor(0)#Sets canvas colour white
15     Root.gStyle.SetOptStat(1110)#set no title on Stat box
16     Root.gStyle.SetLabelOffset(0.001)
17     Root.gStyle.SetLabelSize(0.05)
18     Root.gStyle.SetLabelSize(0.05,"Y")#Y axis
19     Root.gStyle.SetTitleSize(0.06)
20     Root.gStyle.SetTitleW(0.7)
21     Root.gStyle.SetTitleH(0.07)
22     Root.gStyle.SetOptTitle(1)
23     Root.gStyle.SetOptStat(0)
24 bm409 1.4 Root.gStyle.SetAxisColor(1, "XYZ");
25     Root.gStyle.SetStripDecimals(Root.kTRUE);
26     Root.gStyle.SetTickLength(0.03, "XYZ");
27     Root.gStyle.SetNdivisions(510, "XYZ");
28     Root.gStyle.SetPadTickX(1);
29     Root.gStyle.SetPadTickY(1);
30     Root.gStyle.SetLabelColor(1, "XYZ");
31     Root.gStyle.SetLabelFont(42, "XYZ");
32     Root.gStyle.SetLabelOffset(0.007, "XYZ");
33     Root.gStyle.SetLabelSize(0.05, "XYZ");
34 bm409 1.5 Root.gStyle.SetHatchesLineWidth(3)
35    
36    
37     #Please enter the integrated lumi for the plots here:
38    
39     intlumi = 0.316 #inv pico barns
40     print "The Integrated Luminosity your plots are being scaled to is: ", intlumi , "pb^{-1}"
41 bm409 1.6
42    
43    
44 bm409 1.5 outputfile = "../pdfs/"
45     print "you are outputting to: " ,outputfile
46    
47 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.
48 bm409 1.1
49 bm409 1.4 def GetHist(DataSetName,col,norm,Legend):
50 bm409 1.5 a = Root.TFile.Open(DataSetName) #open the file
51     closeList.append(a) # append the file to the close list
52     b = a.Get(DirKeys[dir].GetTitle()) #open the directory in the root file
53     Hist = b.Get(hist) # get your histogram by name
54     leg.AddEntry(Hist,Legend,"LP") # add a legend entry
55 bm409 1.4 Hist.SetLineWidth(3)
56 bm409 1.5 Hist.SetLineColor(col) #set colour
57 bm409 1.4 if norm != 0:
58 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!
59 bm409 1.4 return Hist
60    
61    
62     def HistogramMaxY(H):
63     Nbins = H.GetNbinsX()
64     Entries = [H.GetBinContent(i) for i in range(1,Nbins+1)]
65     return max(Entries)
66 bm409 1.1
67     def HistogramMinX(H):
68     Nbins = H.GetNbinsX()
69     for x in range(0,Nbins):
70     if H.GetBinContent(x) != 0:
71     return H.GetBinLowEdge(x-2)
72     return 0
73    
74     def HistogramMaxX(H):
75     Nbins = H.GetNbinsX()
76     BackItr = range(0,Nbins)
77     BackItr.reverse()
78     for x in BackItr :
79     if H.GetBinContent(x) != 0:
80     return H.GetBinLowEdge(x+1)
81    
82 bm409 1.4
83    
84    
85 bm409 1.1
86    
87     time = strftime("%Y_%m_%d")
88     resultsDir = " ../results/"
89     print time
90     mergedPlots = Root.TFile("StandardPlots.root", "RECREATE")
91    
92    
93 bm409 1.4 #List of files from which you wish to plot
94 bm409 1.5 RootFileList = ["../results/AK5Calo_JetMET_Compleate.root","../results/AK5Calo_ZJets-madgraph.root","../results/AK5Calo_Zinvisible_jets.root","../results/AK5Calo_WJets-madgraph.root","../results/AK5Calo_ttbarTauola.root","../results/AK5Calo_LM0.root","../results/AK5Calo_TotalBackgrounds.root","../results/AK5Calo_QCD_AllPtBins_7TeV_Pythia6.root"]
95 bm409 1.1
96 bm409 1.5 Webpage = open(outputfile+"HadronicPlots"+time+".html",'w')
97     #put your name in the contact email field
98 bm409 1.1 header = '''
99     <body>
100     <CENTER>
101     <h1>Comparison of Basic quantities for N=2 and N>=3 Jets</h1>
102     <P>Data = JetMetTau 268nb, contact email bryn.mathias AT cern DOT ch</P>
103     <table border="1" bordercolor="#FFCC00" style="background-color:#FFFFCC" width="1200" cellpadding="3" cellspacing="3">
104     <tr>
105     <td>DiJet</td>
106     <td>>=3 Jet</td>
107     </tr>'''
108     footer = ''' </table>
109     </CENTER>
110     </body>'''
111     Webpage.write(header)
112    
113 bm409 1.4
114    
115 bm409 1.1 temp = Root.TFile.Open(RootFileList[1])
116     DirKeys = temp.GetListOfKeys()
117    
118     HistKeys = [ (dir.ReadObj()).GetListOfKeys() for dir in DirKeys]
119     HistNames = [ [k.GetName() for k in D] for D in HistKeys]
120 bm409 1.5 #Loops though all the histograms that have been read from the first input file, this is done by histogram name
121 bm409 1.1
122     closeList = []
123     for dir in range(0,len(DirKeys)):
124     for hist in HistNames[dir]:
125     if "/" in hist : continue
126 bm409 1.3 leg = Root.TLegend(0.6, 0.6, 0.8, 0.8)
127 bm409 1.1 leg.SetShadowColor(0)
128     leg.SetFillColor(0)
129     leg.SetLineColor(0)
130 bm409 1.4 if "PFMetVsMHT" in hist: continue #This is the 2d Histogram in the file, dont want to plot this!
131 bm409 1.1 col=2
132     fileno = 1
133 bm409 1.5 MaxY = 1.0#These three are the low terms for the axis and are reset for each histogram see below
134     MaxX = 1.0# to see the numbers set by the functions that are defined in the beginning of the program
135     MinX = 1000.#
136 bm409 1.3 c1 = Root.TCanvas("canvas"+hist,"canname"+hist,1200,1200)
137 bm409 1.5 #Make two pads, one small for the ratio plot and one large for the plot its self
138 bm409 1.3 mainPad = Root.TPad("","",0.01,0.21,0.99,0.99);
139 bm409 1.1 mainPad.SetNumber(1);
140     mainPad.Draw();
141 bm409 1.3 ratioPad = Root.TPad("","",0.01,0.01,0.99,0.2);
142 bm409 1.1 ratioPad.SetNumber(2);
143     ratioPad.Draw();
144    
145 bm409 1.4 c1.cd(1)
146 bm409 1.5 #make your histograms form each file, read in the files you want below
147     # GetHist("RootFile",Colour, scale to lumi 0=false anything else = true, Legend entry)
148     #NB the order in which you book the histos is the order in which they appear in the legend
149     Data = GetHist("../results/AK5Calo_JetMET_Compleate.root",1,0,"Data")
150 bm409 1.4 Data.SetMarkerStyle(20)
151     Data.SetLineWidth(3)
152     Data.SetLineColor(1)
153     Data.SetFillColor(1)
154 bm409 1.5 ZJets = GetHist("../results/AK5Calo_ZJets-madgraph.root",Root.kTeal-7,1,"Z+Jets (Madgraph)")
155     Zinv = GetHist("../results/AK5Calo_Zinvisible_jets.root",91,1,"Z->Invisiable")
156     WJets = GetHist("../results/AK5Calo_WJets-madgraph.root",Root.kPink+7,1,"W+Jets")
157     ttbar = GetHist("../results/AK5Calo_ttbarTauola.root",Root.kBlue+1,1,"TTBar")
158     LM0 = GetHist("../results/AK5Calo_LM0.root",2,1,"LM0")
159     QCD = GetHist("../results/AK5Calo_QCD_AllPtBins_7TeV_Pythia6.root",Root.kPink+4,1,"QCD")
160 bm409 1.4 # Total = GetHist("../results/AK5Calo_TotalBackgrounds.root",Root.kGreen-3,1,"Standard Model Backgrounds")
161    
162 bm409 1.5 #Make a histogram of the sum of all the SM backgrounds
163 bm409 1.4 Total = QCD.Clone()
164     Total.Add(Zinv)
165     Total.Add(ZJets)
166     Total.Add(WJets)
167     Total.Add(ttbar)
168 bm409 1.5
169     #Some funkey stuff from Henning for drawing the total background with some interesting lines around it
170     hcen=Total.Clone()
171     herr=Total.Clone()
172     herr.SetLineColor(Root.kTeal+3)
173     herr.SetMarkerColor(Root.kAzure+6)
174     herr.SetFillColor(Root.kAzure+6)
175    
176     herr.SetLineWidth(3)
177     Total.SetLineWidth(3)
178    
179     Total.SetFillColor(Root.kAzure+2)
180     Total.SetLineColor(Root.kAzure+2)
181     Total.SetFillStyle(3245)
182     hcen.SetFillColor(0)
183     hcen.SetMarkerColor(Root.kTeal+3)
184     hcen.SetLineColor(Root.kTeal+3)
185     hcen.SetLineWidth(3)
186    
187 bm409 1.4 leg.AddEntry(Total,"Standard Model Backgrounds","LPf")
188    
189 bm409 1.5 #Defind the ranges of the histogram for the two highest histograms ie the data and the total
190 bm409 1.4 if HistogramMinX(Total) < MinX :
191     MinX = HistogramMinX(Total)
192     if HistogramMaxX(Total) > MaxX :
193     MaxX = HistogramMaxX(Total)
194     if HistogramMaxY(Total) > MaxY :
195     MaxY = HistogramMaxY(Total)
196    
197     if HistogramMinX(Data) < MinX :
198     MinX = HistogramMinX(Data)
199     if HistogramMaxX(Data) > MaxX :
200     MaxX = HistogramMaxX(Data)
201     if HistogramMaxY(Data) > MaxY :
202     MaxY = HistogramMaxY(Data)
203    
204    
205 bm409 1.5 herr.GetXaxis().SetRangeUser(MinX,MaxX)
206     herr.GetYaxis().SetRangeUser(0.00001,MaxY*1.2)
207 bm409 1.6 #Draw order is important!
208 bm409 1.5 herr.Draw("hist")
209     hcen.Draw("histsame")
210     Total.Draw("9E2SAME")
211 bm409 1.4 ZJets.Draw("9Sameh")
212     Zinv.Draw("9SAMEh")
213     WJets.Draw("9SAMEh")
214     ttbar.Draw("9SAMEh")
215     QCD.Draw("9SAMEh")
216     LM0.Draw("9SAMEh")
217 bm409 1.5 #Draw Data last to get the points above everything else
218 bm409 1.4 Data.Draw("9SAMEP")
219    
220 bm409 1.5 #Draw Text for histogram titile and for lumi
221     prelim = Root.TLatex(0.5,0.75,"CMS preliminary 2010");
222     lumi = Root.TLatex(0.4,.85,"#int L dt = " + str(intlumi) + "pb^{-1}, #sqrt{s} = 7 TeV");
223     prelim.SetNDC()
224     lumi.SetNDC()
225     # prelim.Draw()
226     lumi.Draw()
227     title = Root.TLatex(0.05,0.95,(DirKeys[dir].GetTitle())+"_"+hist[0:-4])
228 bm409 1.4 title.SetNDC()
229     title.Draw()
230 bm409 1.5 lumi.Draw("Same")
231    
232 bm409 1.4
233 bm409 1.5 #Ratio plot is between the sum of the background and the data - clone the two relevant histograms
234 bm409 1.4 RatioBottom = Total.Clone()
235     RatioTop = Data.Clone()
236 bm409 1.5 RatioTop.GetYaxis().SetTitle("data / sim")
237 bm409 1.1 RatioTop.Divide(RatioBottom)
238 bm409 1.4 c1.cd(1).Update()
239     # c1.cd(1).SetLogy()
240     leg.Draw()
241     # c1.cd(1).Update()
242 bm409 1.1 c1.cd(2)
243 bm409 1.4 RatioTop.GetXaxis().SetRangeUser(MinX,MaxX)
244     RatioTop.GetYaxis().SetRangeUser(0.,2.0)
245     RatioTop.Draw()
246 bm409 1.5 #Draw a line though the perfectly matching point
247 bm409 1.1 unity = Root.TLine();
248     unity.SetLineWidth(2);
249     # unity.SetLineStyle(Root.kDashed);
250     unity.SetLineColor(2);
251 bm409 1.4 unity.DrawLine(MinX, 1,MaxX, 1);
252    
253    
254    
255 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
256 bm409 1.1 if "all" in hist :
257    
258 bm409 1.5 # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
259 bm409 1.1 if (DirKeys[dir].GetTitle())[0] != "n":
260    
261     Images ='''<tr>
262 bm409 1.5 <td><a href="'''+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\"><img src=\"'+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\" width =\"598\" 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 =\"598\" height=\"286\" /></a></td>\n'
263 bm409 1.1 Webpage.write(Images)
264    
265     # mergedPlots.writeTObject(c1)
266     # c1.cd(1)
267     c1.cd(1).SetLogy(1)
268     c1.Update()
269     # c1.cd(2)
270     # RatioTop.Draw()
271    
272 bm409 1.5 c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
273     c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
274 bm409 1.1
275     if "AlphaT_0" in hist :
276     # c1.cd(2)
277     # RatioTop.Draw()
278 bm409 1.5 # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
279 bm409 1.1 if (DirKeys[dir].GetTitle())[0] != "n":
280    
281     Images ='''<tr>
282 bm409 1.5 <td><a href="'''+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\"><img src=\"'+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td>'"<td><a href=\""+"n"+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\"><img src=\"'+"n"+(DirKeys[dir].GetTitle())+"_Log_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td>'
283 bm409 1.1 Webpage.write(Images)
284     # mergedPlots.writeTObject(c1)
285     c1.cd(1).SetLogy()
286     # c1.cd(2)
287     # RatioTop.Draw()
288    
289     c1.Update()
290 bm409 1.5 c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
291     c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
292    
293 bm409 1.1 c1.cd(1).SetLogy(0)
294    
295     for a in closeList :
296     a.Close()
297     closeList = []
298     Webpage.write(footer)
299