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.3 by klukas, Thu Nov 19 19:32:52 2009 UTC vs.
Revision 1.8 by klukas, Tue Nov 24 22:18:26 2009 UTC

# Line 19 | Line 19 | naming: histograms whose names contain c
19    'Logx': x-axis will be on log scale
20    'Logy': y-axis will be on log scale"""
21  
22 < ## Define colors
23 < rgbcolors = [[82, 124, 219],
24 <             [145, 83, 207],
25 <             [231, 139, 77],
26 <             [114, 173, 117],
27 <             [67, 77, 83]]
22 > ## Define colors and styles
23 > rgbvals = [[82, 124, 219],
24 >           [212,58,143],
25 >           [231, 139, 77],
26 >           [145, 83, 207],
27 >           [114, 173, 117],
28 >           [67, 77, 83]]
29 > marker_styles = [3, 4, 5, 25, 26, 27, 28, 30]
30  
31   ## Import python libraries
32   import sys
# Line 33 | Line 35 | import os
35   import re
36  
37   ## Import ROOT in batch mode
38 < if '-h' not in sys.argv:
38 > if '-h' not in sys.argv and len(sys.argv) > 1:
39      sys.argv.append('-b')
40      import ROOT
41 <    if os.path.exists('rootlogon.C'): ROOT.gROOT.Macro('rootlogon.C')
41 >    if os.path.exists('rootlogon.C'):
42 >        ROOT.gROOT.Macro('rootlogon.C')
43 >    else:
44 >        os.system('echo -e "{\n}\n" >> rootlogon.C')
45 >        ROOT.gROOT.Macro('rootlogon.C')
46 >        os.remove('rootlogon.C')
47      sys.argv.remove('-b')
48      ROOT.gErrorIgnoreLevel = ROOT.kWarning
49 <    colors = []
43 <    for rgb in rgbcolors:
44 <        colors.append(ROOT.TColor.GetColor(rgb[0], rgb[1], rgb[2]))
49 >    colors = [ROOT.TColor.GetColor(rgb[0], rgb[1], rgb[2]) for rgb in rgbvals]
50      c1 = ROOT.TCanvas()
51  
52   ## Parse options
53   parser = optparse.OptionParser(usage=usage)
54   parser.add_option('-n', '--normalize', action="store_true", default=False,
55                    help="area normalize all histograms")
56 + parser.add_option('-m', '--markers', action="store_true", default=False,
57 +                  help="add markers to histograms")
58   parser.add_option('-e', '--ext', default="pdf",
59                    help="choose an output extension; default is pdf")
60   parser.add_option('-o', '--output', default="overlaidHists", metavar="NAME",
61                    help="name of output directory; default is 'overlaidHists'")
62 < parser.add_option('-m', '--match', default="", metavar="REGEX",
62 > parser.add_option('--match', default="", metavar="REGEX",
63                    help="only make plots for paths containing the specified "
64                    "regular expression (use '.*' for wildcard)")
65 + parser.add_option('--timing', action="store_true", default=False,
66 +                  help="output timing information")
67   options, arguments = parser.parse_args()
68   plot_dir = "%s/%s" % (os.path.abspath('.'), options.output)
69   regex = re.compile(options.match)
70  
71  
72 +
73   class RootFile:
74      def __init__(self, file_name):
75          self.name = file_name[0:file_name.find(".root")]
# Line 73 | Line 83 | class RootFile:
83  
84  
85   def main():
86 <    files = []
77 <    for filename in arguments: files.append(RootFile(filename))
86 >    files = [RootFile(filename) for filename in arguments]
87      if len(files) == 0:
88          parser.print_help()
89          sys.exit(0)
90      process_directory("", files)
91 +    print ""
92      if options.ext == "pdf":
93 <        os.system("gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite "
84 <                  "-dAutoRotatePages=/All "
85 <                  "-sOutputFile=%s.pdf " % options.output +
86 <                  "[0-9][0-9][0-9].pdf")
87 <        os.system("rm [0-9]*.pdf")
88 <    print "Wrote %i plots to %s" % (next_counter() - 1, options.output)
93 >        merge_pdf()
94  
95  
96  
# Line 114 | Line 119 | def process_directory(path, files):
119              if "Norm" in name or options.normalize:
120                  y_title = "Fraction of Events in Bin"
121              hist.Draw()
117            hists = []
122              stack = ROOT.THStack("st%.3i" % int(counter), title)
123 <            legend = ROOT.TLegend(0.65, 0.77, 0.87, 0.89)
123 >            legend_height = 0.04 * len(files) + 0.02
124 >            legend = ROOT.TLegend(0.65, 0.89 - legend_height, 0.87, 0.89)
125              c1.SetLogx("Logx" in name)
126              c1.SetLogy("Logy" in name)
127              for i, file in enumerate(files):
# Line 126 | Line 131 | def process_directory(path, files):
131                  hist.SetTitle(file.name)
132                  color = colors[i % len(colors)]
133                  hist.SetLineColor(color)
134 <                hist.SetMarkerColor(color)
135 <                hist.SetMarkerStyle(i + 1)
134 >                if options.markers:
135 >                    hist.SetMarkerColor(color)
136 >                    hist.SetMarkerStyle(marker_styles[i])
137 >                else:
138 >                    hist.SetMarkerSize(0)
139                  if "Norm" in name or options.normalize:
140                      integral = hist.Integral()
141 <                    hist.Scale(1 / integral)
141 >                    hist.Scale(1. / integral)
142                  stack.Add(hist)
143                  legend.AddEntry(hist)
144              stack.Draw("nostack p H")
# Line 140 | Line 148 | def process_directory(path, files):
148                  stack.SetMaximum(1.)
149                  stack.SetMinimum(0.)
150              legend.Draw()
151 <            if options.ext == "pdf":
144 <                c1.SaveAs("%.3i.pdf" % counter)
145 <            c1.SaveAs("%s/%s/%s.%s" % (plot_dir, path, name, options.ext))
151 >            save_plot(stack, plot_dir, path, name, counter)
152              
153  
154  
155 + def save_plot(stack, plot_dir, path, name, counter):
156 +    if options.ext == "pdf":
157 +        c1.SaveAs("%.3i.pdf" % counter)
158 +    c1.SaveAs("%s/%s/%s.%s" % (plot_dir, path, name, options.ext))
159 +    report_progress(counter, 1)
160 +
161 +
162 +
163 + def report_progress(counter, divisor):
164 +    if counter % divisor == 0:
165 +        print "\r%i plots written to %s" % (counter, options.output),
166 +        sys.stdout.flush()
167 +
168 +
169 +
170 + def merge_pdf():
171 +    print "Writing merged pdf..."
172 +    os.system("gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite "
173 +              "-dAutoRotatePages=/All "
174 +              "-sOutputFile=%s.pdf " % options.output +
175 +              "[0-9][0-9][0-9].pdf")
176 +    os.system("rm [0-9]*.pdf")
177 +
178 +
179  
180   def counter_generator():
181      k = 0
# Line 156 | Line 186 | next_counter = counter_generator().next
186  
187  
188  
159
189   if __name__ == "__main__":
190 <    sys.exit(main())
190 >    if options.timing:
191 >        import cProfile
192 >        cProfile.run('main()', 'fooprof')
193 >        import pstats
194 >        p = pstats.Stats('fooprof')
195 >        p.sort_stats('cumulative').print_stats(15)
196 >    else:
197 >        sys.exit(main())
198  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines