-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement average calling hadd and scale
- Loading branch information
Showing
1 changed file
with
44 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,55 @@ | ||
#!/usr/bin/env python3 | ||
# | ||
# https://github.com/kbat/mc-tools | ||
# | ||
|
||
from sys import argv, exit | ||
from string import ascii_lowercase, digits | ||
from os import system | ||
from random import choice | ||
import argparse, tempfile | ||
import logging | ||
import math | ||
from sys import exit | ||
import os | ||
import ROOT | ||
ROOT.PyConfig.IgnoreCommandLineOptions = True | ||
|
||
def main(): | ||
""" Merge and average ROOT files | ||
""" | ||
hadd + average | ||
""" | ||
|
||
outfile = argv[1] | ||
tmpfname = '/tmp/hadd-av' + ''.join(choice(ascii_lowercase + digits) for x in range(8)) + '.root' | ||
print(tmpfname) | ||
parser = argparse.ArgumentParser(description=main.__doc__, | ||
formatter_class=argparse.ArgumentDefaultsHelpFormatter, | ||
epilog="Homepage: https://github.com/kbat/mc-tools") | ||
|
||
parser.add_argument('-f', '--force', action='store_true', default=False, dest='overwrite', | ||
help='overwrite the target output ROOT file') | ||
parser.add_argument('-o', dest='target', type=str, help='target file') | ||
parser.add_argument('sources', type=str, help='source files', nargs='+') | ||
parser.add_argument('-v', '--verbose', action='store_true', default=False, dest='verbose', help='explain what is being done ') | ||
|
||
args = parser.parse_args() | ||
|
||
if not args.overwrite and os.path.exists(args.target): | ||
logging.critical("File %s exists. Use -f to overwrite" % args.target) | ||
exit(1) | ||
|
||
sources = args.sources | ||
|
||
for f in sources: | ||
if not os.path.exists(f): | ||
print(f"WARNING: {f} does not exits - skipping") | ||
sources.remove(f) | ||
|
||
# open(tmpfile).close() | ||
files = argv[2:] | ||
nfiles = len(files) | ||
if args.verbose: | ||
print(sources) | ||
|
||
if system("hadd %s %s" % (tmpfname, ' '.join(files))): exit(1) | ||
n = len(sources) | ||
|
||
print('number of files:', nfiles) | ||
with tempfile.TemporaryDirectory() as tmp: | ||
unscaled = os.path.join(tmp, "unscaled.root") | ||
print(unscaled) | ||
cmd = "hadd %s %s" % (unscaled, " ".join(sources)) | ||
os.system(cmd) | ||
cmd = f"scale -n {n} {unscaled} -o {args.target}" | ||
os.system(cmd) | ||
|
||
if __name__ == '__main__': | ||
if __name__ == "__main__": | ||
exit(main()) |