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! |
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 |
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 |
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")] |
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 |
|
|
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): |
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") |
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: |
156 |
|
|
157 |
|
|
158 |
|
|
156 |
– |
|
159 |
|
if __name__ == "__main__": |
160 |
|
sys.exit(main()) |
161 |
|
|