Belle II Software  release-05-01-25
b2hlt_combine_results.py
1 #!/usr/bin/env python3
2 
3 from ROOT import PyConfig
4 PyConfig.IgnoreCommandLineOptions = True
5 PyConfig.StartGuiThread = False
6 
7 from argparse import ArgumentParser
8 import os
9 import uproot
10 import root_pandas
11 import pandas as pd
12 
13 __author__ = "Sam Cunliffe"
14 __email__ = "sam.cunliffe@desy.de"
15 
16 
17 def get_parser():
18  """Get the command line options
19 
20  Returns:
21  argparse.ArgumentParser for this tool
22  """
23  parser = ArgumentParser(
24  description="Combines several ``software_trigger_result`` files.")
25  parser.add_argument("input", nargs='*',
26  help="Wildcard to select ``software_trigger_results`` files.")
27  parser.add_argument("--output",
28  help="The combined output ``software_trigger_result`` file name.",
29  default="software_trigger_results_combined.root")
30  return parser
31 
32 
33 if __name__ == "__main__":
34 
35  args = get_parser().parse_args()
36 
37  # get input file list
38  if not all([os.path.exists(f) for f in args.input]):
39  raise FileNotFoundError("Could not find input files: %s" % args.input)
40 
41  # loop over SWTRs
42  sum_out = pd.DataFrame()
43  for fi in args.input:
44 
45  # might have swtr files with no events selected: skip these
46  swtr = uproot.open(fi)["software_trigger_results"].pandas.df()
47  if not swtr['total_events'][0].any():
48  continue
49 
50  # add up all non-zero dataframes
51  if sum_out.empty:
52  sum_out = swtr
53  else:
54  sum_out = sum_out.add(swtr)
55 
56  root_pandas.to_root(sum_out, key='software_trigger_results', path=args.output)
57  # uproot.newtree doesn't work in the current externals version but when it does this can be root free
58  print("Created file %s" % args.output)