-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombine
executable file
·79 lines (68 loc) · 2.33 KB
/
combine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python
import sys
import os
from rootpy.extern.argparse import ArgumentParser
parser = ArgumentParser()
parser.add_argument('--name', default='combined')
parser.add_argument('--verbose', action='store_true', default=False)
parser.add_argument('--path', default=None)
parser.add_argument('files', nargs='+')
args = parser.parse_args()
root_file = '{0}.root'.format(args.name)
xml_path = args.name
if args.path is not None:
root_file = os.path.join(args.path, root_file)
xml_path = os.path.join(args.path, xml_path)
if os.path.exists(root_file):
sys.exit("output ROOT file already exists: {0}".format(root_file))
if os.path.exists(xml_path):
sys.exit("XML output directory already exists: {0}".format(xml_path))
from rootpy.stats.histfactory import (
measurements_from_xml, write_measurement,
make_measurement)
from statstools.fixups import find_measurements
# get measurements
measurements = []
for file in args.files:
# if file is a directory then find all measurement XMLs under it
if os.path.isdir(file):
meas = []
for dirpath, meas_file in find_measurements(file):
path = os.path.join(dirpath, meas_file)
meas.extend(measurements_from_xml(
path,
cd_parent=True,
collect_histograms=True,
silence=not args.verbose))
else:
meas = measurements_from_xml(
file,
cd_parent=True,
collect_histograms=True,
silence=not args.verbose)
if not meas:
sys.exit("No measurements in {0}".format(file))
measurements.extend(meas)
# combine all channels into a new measurement
channels = []
channel_names = {}
for meas in measurements:
for channel in meas.channels:
if channel.name in channel_names:
# ignore duplicate channels
continue
channels.append(channel)
channel_names[channel.name] = None
# use settings from the first measurement
# (POI and constant params)
combined_measurement = make_measurement(
args.name, channels,
POI=measurements[0].poi,
const_params=measurements[0].const_params)
# write out the new measurement and workspace
write_measurement(
combined_measurement,
root_file=root_file,
xml_path=xml_path,
write_workspaces=True,
silence=not args.verbose)