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