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.2 by klukas, Thu Nov 19 15:21:39 2009 UTC vs.
Revision 1.6 by klukas, Fri Nov 20 20:16:30 2009 UTC

# Line 8 | Line 8
8   ## Define usage string for help option
9   usage="""usage: %prog [options] file1.root file2.root file3.root ...
10  
11 < function: overlays histograms from several files with identical structure
11 > function: overlays corresponding histograms from several files, dumping the
12 >          images into an identical directory structure in the local directory
13 >          and also merging all images into a single file (if output is pdf)
14  
15   naming: histograms whose names contain certain key terms will be handled
16          specially.  Use this to your advantage!
# Line 18 | Line 20 | naming: histograms whose names contain c
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]]
23 > rgbvals = [[82, 124, 219],
24 >           [145, 83, 207],
25 >           [231, 139, 77],
26 >           [114, 173, 117],
27 >           [67, 77, 83]]
28  
29   ## Import python libraries
30   import sys
# Line 37 | Line 39 | if '-h' not in sys.argv:
39      if os.path.exists('rootlogon.C'): ROOT.gROOT.Macro('rootlogon.C')
40      sys.argv.remove('-b')
41      ROOT.gErrorIgnoreLevel = ROOT.kWarning
42 <    colors = []
41 <    for rgb in rgbcolors:
42 <        colors.append(ROOT.TColor.GetColor(rgb[0], rgb[1], rgb[2]))
42 >    colors = [ROOT.TColor.GetColor(rgb[0], rgb[1], rgb[2]) for rgb in rgbvals]
43      c1 = ROOT.TCanvas()
44  
45   ## Parse options
# Line 47 | Line 47 | parser = optparse.OptionParser(usage=usa
47   parser.add_option('-n', '--normalize', action="store_true", default=False,
48                    help="area normalize all histograms")
49   parser.add_option('-e', '--ext', default="pdf",
50 <                  help="specify the type (extension) of the output files")
50 >                  help="choose an output extension; default is pdf")
51   parser.add_option('-o', '--output', default="overlaidHists", metavar="NAME",
52 <                  help="specify the name of the output file/directory")
52 >                  help="name of output directory; default is 'overlaidHists'")
53   parser.add_option('-m', '--match', default="", metavar="REGEX",
54 <                  help="only make plots for paths containing REGEX")
54 >                  help="only make plots for paths containing the specified "
55 >                  "regular expression (use '.*' for wildcard)")
56   options, arguments = parser.parse_args()
57   plot_dir = "%s/%s" % (os.path.abspath('.'), options.output)
58   regex = re.compile(options.match)
59  
60  
61 +
62   class RootFile:
63      def __init__(self, file_name):
64          self.name = file_name[0:file_name.find(".root")]
# Line 70 | Line 72 | class RootFile:
72  
73  
74   def main():
75 <    files = []
74 <    for filename in arguments: files.append(RootFile(filename))
75 >    files = [RootFile(filename) for filename in arguments]
76      if len(files) == 0:
77          parser.print_help()
78          sys.exit(0)
79      process_directory("", files)
80 +    print
81      if options.ext == "pdf":
82 +        print "Writing merged pdf..."
83          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")
85    print "Wrote %i plots to %s" % (next_counter() - 1, options.output)
88  
89  
90  
# Line 111 | Line 113 | def process_directory(path, files):
113              if "Norm" in name or options.normalize:
114                  y_title = "Fraction of Events in Bin"
115              hist.Draw()
114            hists = []
116              stack = ROOT.THStack("st%.3i" % int(counter), title)
117 <            legend = ROOT.TLegend(0.65, 0.77, 0.87, 0.89)
117 >            legend_height = 0.04 * len(files) + 0.02
118 >            legend = ROOT.TLegend(0.65, 0.89 - legend_height, 0.87, 0.89)
119              c1.SetLogx("Logx" in name)
120              c1.SetLogy("Logy" in name)
121              for i, file in enumerate(files):
# Line 123 | Line 125 | def process_directory(path, files):
125                  hist.SetTitle(file.name)
126                  color = colors[i % len(colors)]
127                  hist.SetLineColor(color)
128 <                hist.SetMarkerColor(color)
129 <                hist.SetMarkerStyle(i + 1)
128 >                ## hist.SetMarkerColor(color)
129 >                ## hist.SetMarkerStyle(i + 1)
130                  if "Norm" in name or options.normalize:
131                      integral = hist.Integral()
132 <                    hist.Scale(1 / integral)
132 >                    hist.Scale(1. / integral)
133                  stack.Add(hist)
134                  legend.AddEntry(hist)
135              stack.Draw("nostack p H")
# Line 140 | Line 142 | def process_directory(path, files):
142              if options.ext == "pdf":
143                  c1.SaveAs("%.3i.pdf" % counter)
144              c1.SaveAs("%s/%s/%s.%s" % (plot_dir, path, name, options.ext))
145 +            print "\r%i plots written to %s" % (counter, options.output),
146 +            sys.stdout.flush()
147              
148  
149  
146
150   def counter_generator():
151      k = 0
152      while True:
# Line 153 | Line 156 | next_counter = counter_generator().next
156  
157  
158  
156
159   if __name__ == "__main__":
160      sys.exit(main())
161  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines