ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/bm409/autoPlotter.py
(Generate patch)

Comparing UserCode/bm409/autoPlotter.py (file contents):
Revision 1.3 by bm409, Thu Aug 5 15:41:35 2010 UTC vs.
Revision 1.6 by bm409, Tue Aug 10 08:12:40 2010 UTC

# Line 1 | Line 1
1 + #!/usr/bin/env python
2 +
3 + '''Created by Bryn Mathias
4 + bryn.mathias@cern.ch
5 + '''
6 +
7   import ROOT as Root
8   from time import strftime
9   import os, commands
10 < Root.gROOT.SetBatch(True)
10 > Root.gROOT.SetBatch(True) # suppress the creation of canvases on the screen.. much much faster if over a remote connection
11   Root.gROOT.SetStyle("Plain") #To set plain bkgds for slides
12   Root.gStyle.SetTitleBorderSize(0)
13   Root.gStyle.SetCanvasBorderMode(0)
# Line 15 | Line 21 | Root.gStyle.SetTitleW(0.7)
21   Root.gStyle.SetTitleH(0.07)
22   Root.gStyle.SetOptTitle(1)
23   Root.gStyle.SetOptStat(0)
24 + 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 + 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 +
42  
43  
44 < def DrawHist(H,col,norm):
45 <        Hist =
46 < def HistogramMaxYmum(H):
47 <        Nbins = H.GetNbinsX()
48 <        Entries = [H.GetBinContent(i) for i in range(1,Nbins+1)]
49 <        return max(Entries)
44 > outputfile = "../pdfs/"
45 > print "you are outputting to: " ,outputfile
46 >
47 > #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 >
49 > def GetHist(DataSetName,col,norm,Legend):
50 >    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 >    Hist.SetLineWidth(3)
56 >    Hist.SetLineColor(col) #set colour
57 >    if norm != 0:
58 >       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 >    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  
67   def HistogramMinX(H):
68    Nbins = H.GetNbinsX()
# Line 39 | Line 79 | def HistogramMaxX(H):
79      if H.GetBinContent(x) != 0:
80        return H.GetBinLowEdge(x+1)
81  
82 < intlumi = 0.2683 #inv pico barns
82 >
83 >
84 >
85  
86  
87   time = strftime("%Y_%m_%d")
# Line 47 | Line 89 | resultsDir = " ../results/"
89   print time
90   mergedPlots = Root.TFile("StandardPlots.root", "RECREATE")
91  
50 tmp = commands.getstatusoutput("ls" + resultsDir + "*.root")
92  
93 < RootFileList = tmp[1].split('\n')
93 > #List of files from which you wish to plot
94 > 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  
96 < Webpage = open("../pdfs/HadronicPlots"+time+".html",'w')
96 > Webpage = open(outputfile+"HadronicPlots"+time+".html",'w')
97 > #put your name in the contact email field
98   header = '''
99   <body>
100   <CENTER>
# Line 67 | Line 110 | footer = '''  </table>
110    </body>'''
111   Webpage.write(header)
112  
70 temp = Root.TFile.Open(RootFileList[1])
71 DirKeys = temp.GetListOfKeys()
113  
114  
115 + 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 <
120 > #Loops though all the histograms that have been read from the first input file, this is done by histogram name
121  
122   closeList = []
123   for dir in range(0,len(DirKeys)):
# Line 84 | Line 127 | for dir in range(0,len(DirKeys)):
127      leg.SetShadowColor(0)
128      leg.SetFillColor(0)
129      leg.SetLineColor(0)
130 <    if "PFMetVsMHT" in hist: continue
130 >    if "PFMetVsMHT" in hist: continue #This is the 2d Histogram in the file, dont want to plot this!
131      col=2
132      fileno = 1
133 <    MaxY = 1.0
134 <    MaxX = 1.0
135 <    XMin = 1000.
133 >    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      c1 = Root.TCanvas("canvas"+hist,"canname"+hist,1200,1200)
137 <    # c1.Divide(1,2)#,0.4,0.4)
137 >    #Make two pads, one small for the ratio plot and one large for the plot its self
138      mainPad = Root.TPad("","",0.01,0.21,0.99,0.99);
139      mainPad.SetNumber(1);
140      mainPad.Draw();
98
141      ratioPad = Root.TPad("","",0.01,0.01,0.99,0.2);
142      ratioPad.SetNumber(2);
143      ratioPad.Draw();
144  
145 <    for file in RootFileList:
146 <      c1.cd(1)
147 <      a = Root.TFile.Open(file)
148 <      closeList.append(a)
149 <      b = a.Get(DirKeys[dir].GetTitle())
150 <      if "AK5Calo_JetMETTau_Run2010A_Compleate" in file :
151 <        c1.SetLogy()
152 <        c = b.Get(hist)
153 <        if c.IsA().InheritsFrom("TH2") : continue
154 <        c.SetMarkerStyle(20)
155 <        c.SetLineWidth(3)
156 <        RatioTop = c.Clone()
157 <        c.Draw("P")
158 <        if HistogramMinX(c) < XMin:
159 <          XMin = HistogramMinX(c)
160 <        if HistogramMaxX(c) > MaxX :
161 <          MaxX = HistogramMaxX(c)
162 <        title = Root.TLatex(0.2,0.95,(DirKeys[dir].GetTitle())+"_"+hist[0:-4])
163 <        title.SetNDC()
164 <        title.Draw()
165 <        leg.AddEntry(c,(RootFileList[0])[19:-5],"Pl")
166 <      if "JetMETTau" not in file :
167 <        d = b.Get(hist)
168 <        if d.IsA().InheritsFrom("TH2") : continue
169 <        if col == 10 or col == 9 : col+=1
170 <        d.SetLineColor(col)
171 <        d.SetLineWidth(3)
172 <        if d.Integral() > 0:
173 <          Norm = 100.
174 <          d.Scale(intlumi/Norm)
175 <          # d.Scale(c.Integral()/d.Integral())
176 <        if "QCD" in file:
177 <          d.SetFillColor(Root.kRed)
178 <          d.SetLineColor(Root.kRed)
179 <          d.SetFillStyle(3002)
180 <
181 <        if "AK5Calo_TotalBackgrounds" in file :
182 <          d.SetLineColor(Root.kAzure)
183 <          RatioBottom = d.Clone()
184 <        leg.AddEntry(d,(RootFileList[fileno])[19:-5],"lpf")
185 <        leg.Draw("same")
186 <        d.Draw("Sameh")
187 <        if HistogramMaxYmum(d) > MaxY :
188 <          MaxY = HistogramMaxYmum(d)
189 <        if HistogramMaxX(d) > MaxX :
190 <          MaxX = HistogramMaxX(d)
191 <        if HistogramMinX(d) < XMin:
192 <          XMin = HistogramMinX(d)
193 <        col+=1
194 <        fileno+=1
195 <    c.GetXaxis().SetRangeUser(XMin,MaxX)
196 <    c.GetYaxis().SetRangeUser(0.0001,1.2*MaxY)
197 <    RatioTop.Divide(RatioBottom)
198 <    RatioTop.GetYaxis().SetRangeUser(0.,2.)
145 >    c1.cd(1)
146 >    #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 >    Data.SetMarkerStyle(20)
151 >    Data.SetLineWidth(3)
152 >    Data.SetLineColor(1)
153 >    Data.SetFillColor(1)
154 >    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 >    # Total = GetHist("../results/AK5Calo_TotalBackgrounds.root",Root.kGreen-3,1,"Standard Model Backgrounds")
161 >
162 >    #Make a histogram of the sum of all the SM backgrounds
163 >    Total = QCD.Clone()
164 >    Total.Add(Zinv)
165 >    Total.Add(ZJets)
166 >    Total.Add(WJets)
167 >    Total.Add(ttbar)
168 >
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 >    leg.AddEntry(Total,"Standard Model Backgrounds","LPf")
188 >
189 >    #Defind the ranges of the histogram for the two highest histograms ie the data and the total
190 >    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 >    herr.GetXaxis().SetRangeUser(MinX,MaxX)
206 >    herr.GetYaxis().SetRangeUser(0.00001,MaxY*1.2)
207 >    #Draw order is important!
208 >    herr.Draw("hist")
209 >    hcen.Draw("histsame")
210 >    Total.Draw("9E2SAME")
211 >    ZJets.Draw("9Sameh")
212 >    Zinv.Draw("9SAMEh")
213 >    WJets.Draw("9SAMEh")
214 >    ttbar.Draw("9SAMEh")
215 >    QCD.Draw("9SAMEh")
216 >    LM0.Draw("9SAMEh")
217 >    #Draw Data last to get the points above everything else
218 >    Data.Draw("9SAMEP")
219 >
220 >    #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 >    title.SetNDC()
229 >    title.Draw()
230 >    lumi.Draw("Same")
231 >
232 >
233 >    #Ratio plot is between the sum of the background and the data - clone the two relevant histograms
234 >    RatioBottom = Total.Clone()
235 >    RatioTop = Data.Clone()
236      RatioTop.GetYaxis().SetTitle("data / sim")
237 <    RatioTop.GetXaxis().SetTitle(d.GetXaxis().GetTitle())
237 >    RatioTop.Divide(RatioBottom)
238 >    c1.cd(1).Update()
239 >    # c1.cd(1).SetLogy()
240 >    leg.Draw()
241 >    # c1.cd(1).Update()
242      c1.cd(2)
243 +    RatioTop.GetXaxis().SetRangeUser(MinX,MaxX)
244 +    RatioTop.GetYaxis().SetRangeUser(0.,2.0)
245 +    RatioTop.Draw()
246 +    #Draw a line though the perfectly matching point
247      unity = Root.TLine();
248      unity.SetLineWidth(2);
249      # unity.SetLineStyle(Root.kDashed);
250      unity.SetLineColor(2);
251 <    RatioTop.GetXaxis().SetRangeUser(XMin,MaxX)
165 <    RatioTop.Draw()
166 <    unity.DrawLine(XMin, 1,MaxX, 1);
251 >    unity.DrawLine(MinX, 1,MaxX, 1);
252  
253 +
254 +
255 +    #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      if "all" in hist :
257  
258 <      c1.SaveAs("../pdfs/"+(DirKeys[dir].GetTitle())+"_"+hist+".png")
258 >      # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
259        if (DirKeys[dir].GetTitle())[0] != "n":
260  
261          Images ='''<tr>
262 <          <td><a href="'''+(DirKeys[dir].GetTitle())+"_"+hist+'.png\"><img src=\"'+(DirKeys[dir].GetTitle())+"_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td> \n' + "<td><a href=\""+"n"+(DirKeys[dir].GetTitle())+"_"+hist+'.png\"><img src=\"'+"n"+(DirKeys[dir].GetTitle())+"_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td> <tr> \n' + "<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'
262 >          <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          Webpage.write(Images)
264  
265        # mergedPlots.writeTObject(c1)
# Line 181 | Line 269 | for dir in range(0,len(DirKeys)):
269        # c1.cd(2)
270        # RatioTop.Draw()
271  
272 <      c1.SaveAs("../pdfs/"+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
272 >      c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
273 >      c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
274  
275      if "AlphaT_0" in hist :
276        # c1.cd(2)
277        # RatioTop.Draw()
278 <      c1.SaveAs("../pdfs/"+(DirKeys[dir].GetTitle())+"_"+hist+".png")
278 >      # c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_"+hist+".png")
279        if (DirKeys[dir].GetTitle())[0] != "n":
280  
281          Images ='''<tr>
282 <          <td><a href="'''+(DirKeys[dir].GetTitle())+"_"+hist+'.png\"><img src=\"'+(DirKeys[dir].GetTitle())+"_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td>' + "<td><a href=\""+"n"+(DirKeys[dir].GetTitle())+"_"+hist+'.png\"><img src=\"'+"n"+(DirKeys[dir].GetTitle())+"_"+hist+'.png\" width =\"598\" height=\"286\" /></a></td> <tr>' + "<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>'
282 >          <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          Webpage.write(Images)
284        # mergedPlots.writeTObject(c1)
285        c1.cd(1).SetLogy()
# Line 198 | Line 287 | for dir in range(0,len(DirKeys)):
287        # RatioTop.Draw()
288  
289        c1.Update()
290 <      c1.SaveAs("../pdfs/"+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
290 >      c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".png")
291 >      c1.SaveAs(outputfile+(DirKeys[dir].GetTitle())+"_Log_"+hist+".pdf")
292 >
293        c1.cd(1).SetLogy(0)
294  
295      for a in closeList :
296        a.Close()
297      closeList = []
298   Webpage.write(footer)
208 # mergedPlots.write()
209 # mergedPlots.Close()
210
211
299  
213 #
214 #
215 #
216 #
217 #
218 # File = Root.TFile.Open(RootFileList[1])
219 #
220 #
221 # dirlist = File.GetListOfKeys()
222 # FileList = [dir.GetTitle() for dir in dirlist]
223 # histlist = (dirlist[1].ReadObj()).GetListOfKeys()
224 # histName = [hist.GetName() for hist in histlist]
225 #
226 #
227 #
228 # for Dirs in DirList:
229 #   for hist in histName:
230 #     for Sample in RootFileList:
231 #       Sample = Root.TFile.Open(Sample)
232 #       a = Sample.Get(File)
233 #       b = a.Get(hist)
234 #       b.Draw()

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines