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

Comparing UserCode/RootMacros/overlayHists.py (file contents):
Revision 1.8 by klukas, Tue Nov 24 22:18:26 2009 UTC vs.
Revision 1.9 by klukas, Wed Jan 6 21:18:09 2010 UTC

# Line 31 | Line 31 | marker_styles = [3, 4, 5, 25, 26, 27, 28
31   ## Import python libraries
32   import sys
33   import optparse
34 + import shutil
35   import os
36   import re
37  
# Line 47 | Line 48 | if '-h' not in sys.argv and len(sys.argv
48      sys.argv.remove('-b')
49      ROOT.gErrorIgnoreLevel = ROOT.kWarning
50      colors = [ROOT.TColor.GetColor(rgb[0], rgb[1], rgb[2]) for rgb in rgbvals]
51 <    c1 = ROOT.TCanvas()
51 >    canvas = ROOT.TCanvas()
52  
53   ## Parse options
54   parser = optparse.OptionParser(usage=usage)
# Line 55 | Line 56 | parser.add_option('-n', '--normalize', a
56                    help="area normalize all histograms")
57   parser.add_option('-m', '--markers', action="store_true", default=False,
58                    help="add markers to histograms")
59 + parser.add_option('--logx', action="store_true", default=False,
60 +                  help="Force log scale for X axis on all histograms")
61 + parser.add_option('--logy', action="store_true", default=False,
62 +                  help="Force log scale for Y axis on all histograms")
63   parser.add_option('-e', '--ext', default="pdf",
64                    help="choose an output extension; default is pdf")
65 < parser.add_option('-o', '--output', default="overlaidHists", metavar="NAME",
65 > parser.add_option('-o', '--opt', default="",
66 >                  help="argument to be passed to THStack::Draw")
67 > parser.add_option('--output', default="overlaidHists", metavar="NAME",
68                    help="name of output directory; default is 'overlaidHists'")
69   parser.add_option('--match', default="", metavar="REGEX",
70                    help="only make plots for paths containing the specified "
# Line 72 | Line 79 | regex = re.compile(options.match)
79  
80   class RootFile:
81      def __init__(self, file_name):
82 <        self.name = file_name[0:file_name.find(".root")]
82 >        self.name = file_name[0:-5]
83          self.file = ROOT.TFile(file_name, "read")
84          if self.file.IsZombie():
85              print "Error opening %s, exiting..." % file_name
# Line 118 | Line 125 | def process_directory(path, files):
125              y_title = hist.GetYaxis().GetTitle()
126              if "Norm" in name or options.normalize:
127                  y_title = "Fraction of Events in Bin"
121            hist.Draw()
128              stack = ROOT.THStack("st%.3i" % int(counter), title)
129              legend_height = 0.04 * len(files) + 0.02
130              legend = ROOT.TLegend(0.65, 0.89 - legend_height, 0.87, 0.89)
131 <            c1.SetLogx("Logx" in name)
132 <            c1.SetLogy("Logy" in name)
131 >            canvas.SetLogx("Logx" in name or options.logx)
132 >            canvas.SetLogy("Logy" in name or options.logy)
133              for i, file in enumerate(files):
134                  hist = file.file.GetDirectory(path).Get(name)
135                  if not hist: continue
130                hist.Draw()
136                  hist.SetTitle(file.name)
137                  color = colors[i % len(colors)]
138                  hist.SetLineColor(color)
# Line 136 | Line 141 | def process_directory(path, files):
141                      hist.SetMarkerStyle(marker_styles[i])
142                  else:
143                      hist.SetMarkerSize(0)
144 +                if "Overflow" in name:
145 +                    nbins = hist.GetNbinsX()
146 +                    overflow = hist.GetBinContent(nbins + 1)
147 +                    hist.AddBinContent(nbins, overflow)
148 +                if "Underflow" in name:
149 +                    underflow = hist.GetBinContent(0)
150 +                    hist.AddBinContent(1, underflow)
151                  if "Norm" in name or options.normalize:
152                      integral = hist.Integral()
153                      hist.Scale(1. / integral)
154                  stack.Add(hist)
155                  legend.AddEntry(hist)
156 <            stack.Draw("nostack p H")
156 >            stack.Draw("nostack p H" + options.opt)
157              stack.SetTitle("%s;%s;%s" % (title, x_title, y_title))
158              if "Eff" in name:
159                  stack.Draw("nostack e p")
160                  stack.SetMaximum(1.)
161                  stack.SetMinimum(0.)
162 +            if "Overflow" in name:
163 +                nbins = hist.GetNbinsX()
164 +                x = 0.5 * (hist.GetBinLowEdge(nbins) +
165 +                           hist.GetBinLowEdge(nbins + 1))
166 +                y = stack.GetMinimum("nostack")
167 +                overflow_text = ROOT.TText(x, y, "Overflow")
168 +                overflow_text.SetTextSize(min(1. / nbins, 0.04))
169 +                overflow_text.SetTextAlign(12)
170 +                overflow_text.SetTextAngle(90)
171 +                overflow_text.SetTextColor(13)
172 +                overflow_text.SetTextFont(42)
173 +                overflow_text.Draw()
174 +            if "Underflow" in name:
175 +                nbins = hist.GetNbinsX()
176 +                x = hist.GetBinLowEdge(1)
177 +                y = hist.GetMinimum()
178 +                underflow_text = ROOT.TText(x, y, "Underflow")
179 +                underflow_text.SetTextSize(1. / nbins)
180 +                underflow_text.SetTextAlign(13)
181 +                underflow_text.SetTextAngle(90)
182 +                underflow_text.SetTextColor(13)
183 +                underflow_text.Draw()
184              legend.Draw()
185              save_plot(stack, plot_dir, path, name, counter)
186              
187  
188  
189   def save_plot(stack, plot_dir, path, name, counter):
190 +    output_file_name = "%s/%s/%s.%s" % (plot_dir, path, name, options.ext)
191 +    canvas.SaveAs(output_file_name)
192      if options.ext == "pdf":
193 <        c1.SaveAs("%.3i.pdf" % counter)
194 <    c1.SaveAs("%s/%s/%s.%s" % (plot_dir, path, name, options.ext))
193 >        numbered_pdf_name = "%.3i.pdf" % counter
194 >        shutil.copy(output_file_name, numbered_pdf_name)
195      report_progress(counter, 1)
196  
197  
# Line 188 | Line 224 | next_counter = counter_generator().next
224  
225   if __name__ == "__main__":
226      if options.timing:
227 <        import cProfile
228 <        cProfile.run('main()', 'fooprof')
227 > ##         import cProfile
228 > ##         cProfile.run('main()', 'fooprof')
229 >        import profile
230 >        profile.run('main()', 'fooprof')
231          import pstats
232          p = pstats.Stats('fooprof')
233          p.sort_stats('cumulative').print_stats(15)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines