Skip to content

Commit

Permalink
implement average calling hadd and scale
Browse files Browse the repository at this point in the history
  • Loading branch information
kbat committed Dec 9, 2024
1 parent 8286709 commit 256bde1
Showing 1 changed file with 44 additions and 15 deletions.
59 changes: 44 additions & 15 deletions mctools/common/average.py
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())

0 comments on commit 256bde1

Please sign in to comment.