10 def iterate(thisdir: ROOT.TDirectory, targetdir: ROOT.TDirectory, prefix: str, regex: typing.Pattern,
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)
19 targetsubdir = targetdir.mkdir(k.GetName())
20 iterate(o := k.ReadObj(), targetsubdir, prefix, regex, excludeTrees)
21 ROOT.SetOwnership(o,
True)
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)
28 targetdir.WriteObject(o := k.ReadObj(), k.GetName())
29 ROOT.SetOwnership(o,
True)
31 if __name__ ==
'__main__':
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)
39 args = parser.parse_args()
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)