ATLAS Offline Software
copySelective.py
Go to the documentation of this file.
1 from __future__ import print_function
2 import sys, os, glob
3 import ROOT
4 import re
5 
6 def copyHist(indir, outdir, dirfilt):
7  print("Decending into directory", indir.GetName())
8  for key in indir.GetListOfKeys():
9  obj = indir.Get(key.GetName())
10 
11  isdir = obj.InheritsFrom("TDirectory")
12  ishist = obj.InheritsFrom("TH1")
13 
14  match = True
15  if isdir: match = re.match(dirfilt, obj.GetName()) != None
16 
17  #print(key.GetName(), match, isdir, ishist)
18  if not match: continue
19 
20  outdir.cd()
21  if isdir:
22  newoutdir = outdir.mkdir(obj.GetName())
23  newindir = obj
24  copyHist(newindir, newoutdir, dirfilt)
25 
26  if ishist:
27  obj.Write()
28 
29 
30 dirfilt = 'run_.*|GLOBAL|lb_.*|lowStat_LB.*|DQTGlobalWZFinder'
31 
32 source = 'test.root'
33 if len(sys.argv) > 1:
34  source = sys.argv[1]
35 
36 # output file
37 target = 'copy.root'
38 if len(sys.argv) > 2:
39  target = sys.argv[2]
40 
41 print ("Copy from", source, "to", target, "with directory filter", dirfilt)
42 
43 # open source file
44 infile = ROOT.TFile(source)
45 if not infile.IsOpen():
46  print ('Problem with input file', source)
47  exit(1)
48 # open output file
49 outfile = ROOT.TFile.Open(target, "RECREATE")
50 if not outfile.IsOpen():
51  print ('Problem with output file', target)
52  exit(1)
53 
54 copyHist(infile, outfile, dirfilt)
55 
56 infile.Close()
57 outfile.Close()
calibdata.exit
exit
Definition: calibdata.py:236
copySelective.copyHist
def copyHist(indir, outdir, dirfilt)
Definition: copySelective.py:6
dbg::print
void print(std::FILE *stream, std::format_string< Args... > fmt, Args &&... args)
Definition: SGImplSvc.cxx:70