ATLAS Offline Software
Loading...
Searching...
No Matches
histSizes.py
Go to the documentation of this file.
1#!/usr/bin/env python
2
3# Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration
4"""Print number of histograms in ROOT file"""
5
6import sys
7import ROOT
8
9opts = None
10
12 N = h.GetSize()
13 return sum([1 for i in range(N) if h.At(i)!=0])
14
15def hasLabels(h):
16 return (h.GetXaxis().GetLabels() is not None)
17
19 l = h.GetXaxis().GetLabels()
20 return (l is not None and h.GetXaxis().GetNbins()!=l.GetSize())
21
22def addDirList(dir,path,hists):
23 list=dir.GetListOfKeys()
24 for key in list:
25 name=key.GetName()
26 cname=key.GetClassName()
27 if cname=='TDirectoryFile':
28 addDirList(dir.GetDirectory(name),path+name+'/',hists)
29 else:
30 h = key.ReadObj()
31 if not h.InheritsFrom('TH1'): continue
32 if opts.labeled is True and not hasLabels(h): continue
33 if opts.misslabel is True and not missingLabels(h): continue
34 if opts.empty is True and h.GetEntries()>0: continue
35 b = filledBins(h) if opts.filled else h.GetSize()
36 hists[path+name]=(cname,b)
37
38def list(name,path='/'):
39 file=ROOT.TFile(name)
40 file.cd(path)
41 hists={}
42 addDirList(ROOT.gDirectory,path,hists)
43 return hists
44
45def byName(hists, nameFunc):
46 """Return dict {Alg : (nHists,Bins)}"""
47 algs = {}
48 for entry in sorted(hists):
49 name = nameFunc(entry)
50 bins = hists[entry][1]
51
52 if name in algs:
53 algs[name][0] += 1
54 algs[name][1] += bins
55 else:
56 algs[name] = [1,bins]
57
58 return algs
59
60def byAlg(hists):
61 return byName(hists, lambda h : h.split('/',2)[1])
62
63
64def main():
65 import optparse
66 parser = optparse.OptionParser(description=__doc__,
67 usage='%prog [Options] file [path]')
68
69 parser.add_option('-t', '--total', action='store_true',
70 help='Print total number of histograms and bins')
71
72 parser.add_option('-b', '--bins', action='store_true',
73 help='Sort by number of bins')
74
75 parser.add_option('-n', '--byName', action='store_true',
76 help='Show histograms [default]')
77
78 parser.add_option('-a', '--byAlg', action='store_true',
79 help='Show total histograms per algorithm')
80
81 parser.add_option('-f', '--filled', action='store_true',
82 help='Show number of filled bins instead of total bins')
83
84 parser.add_option('-l', '--labeled', action='store_true',
85 help='Only show histograms with text labels')
86
87 parser.add_option('-m', '--misslabel', action='store_true',
88 help='Only show labeled histograms with at least one unlabeled bin')
89
90 parser.add_option('-e', '--empty', action='store_true',
91 help='Only show histograms with zero entries')
92
93 global opts
94 (opts, args) = parser.parse_args()
95
96 if len(args)==1:
97 path = '/'
98 elif len(args)==2:
99 path = args[1]
100 else:
101 parser.print_help()
102 return 1
103
104 hists = list(args[0],path)
105 if opts.bins: sortKey = lambda x : x[1][1]
106 else: sortKey = None
107
108 if not opts.byAlg: opts.byName = True
109
110 if opts.byName is True:
111 for h,v in sorted(hists.items(), key=sortKey):
112 print('%-80s %10s %10s' % (h,v[0],v[1]))
113
114 if opts.byAlg is True:
115 algs = byAlg(hists)
116 for h,v in sorted(algs.items(), key=sortKey):
117 print('%-80s %10s %10s' % (h,v[0],v[1]))
118
119 if opts.total:
120 print()
121 print("Total histograms: %15s" % len(hists))
122 print("Total %sbins: %15s" % ("filled " if opts.filled else "", sum([h[1] for h in hists.values()])))
123
124
125if __name__ == '__main__':
126 try:
127 sys.exit(main())
128 except IOError as e:
129 (code, msg) = e
130 if (code==32): pass # ignore broken pipe exception
131 else: raise e
132 except KeyboardInterrupt:
133 sys.exit(1)
134
void print(char *figname, TCanvas *c1)
byAlg(hists)
Definition histSizes.py:60
list(name, path='/')
Definition histSizes.py:38
hasLabels(h)
Definition histSizes.py:15
byName(hists, nameFunc)
Definition histSizes.py:45
addDirList(dir, path, hists)
Definition histSizes.py:22
filledBins(h)
Definition histSizes.py:11
missingLabels(h)
Definition histSizes.py:18