ATLAS Offline Software
PrepareReferenceFile.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 # Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
4 
5 import argparse
6 import ROOT
7 import re
8 import typing
9 
10 def iterate(thisdir: ROOT.TDirectory, targetdir: ROOT.TDirectory, prefix: str, regex: typing.Pattern,
11  excludeTrees: bool):
12  """Recurse to copy all objects except those of regex"""
13  for k in thisdir.GetListOfKeys():
14  if k.GetClassName().startswith('TDirectory'):
15  targetsubdirname = thisdir.GetPath()[len(prefix):] + '/' + k.GetName()
16  if regex.fullmatch(targetsubdirname):
17  print('Now skipping', targetsubdirname)
18  continue
19  targetsubdir = targetdir.mkdir(k.GetName())
20  iterate(o := k.ReadObj(), targetsubdir, prefix, regex, excludeTrees)
21  ROOT.SetOwnership(o, True)
22  else:
23  if k.GetClassName().startswith('TTree'):
24  if not args.excludeTrees or k.GetName() == 'metadata':
25  targetdir.WriteObject(o := k.ReadObj().CloneTree(), k.GetName())
26  ROOT.SetOwnership(o, True)
27  else:
28  targetdir.WriteObject(o := k.ReadObj(), k.GetName())
29  ROOT.SetOwnership(o, True)
30 
31 if __name__ == '__main__':
32 
33  parser = argparse.ArgumentParser()
34  parser.add_argument('infile')
35  parser.add_argument('outfile')
36  parser.add_argument('--excludeRegex', default='^run_\d+/lb_\d+')
37  parser.add_argument('--excludeTrees', default=True)
38 
39  args = parser.parse_args()
40 
41  infile = ROOT.TFile.Open(args.infile, "READ")
42  outfile = ROOT.TFile.Open(args.outfile, "RECREATE")
43  regex = re.compile(args.excludeRegex)
44  iterate(infile, outfile, infile.GetPath(), regex, args.excludeTrees)
45  outfile.Write()
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70
PrepareReferenceFile.iterate
def iterate(ROOT.TDirectory thisdir, ROOT.TDirectory targetdir, str prefix, typing.Pattern regex, bool excludeTrees)
Definition: PrepareReferenceFile.py:10