ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
PhysicsAnalysis
D3PDMaker
D3PDMakerTest
python
dumptuple_any.py
Go to the documentation of this file.
1
# Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
3
#
4
# File: D3PDMakerTest/python/dumptuple_any.py
5
# Author: snyder@bnl.gov
6
# Date: Feb, 2010
7
# Purpose: Test D3PD making.
8
#
9
10
# Always run in batch mode.
11
import
os
12
if
'DISPLAY'
in
os.environ:
13
del os.environ[
'DISPLAY'
]
14
os.environ[
'TERM'
] =
'dumb'
15
16
17
import
ROOT
18
import
cppyy
19
# Autoloading doesn't work correctly for vector<unsigned long long>,
20
# since root will munge the name to vector<ULong64_t> before trying
21
# to look up the autoload. (cf R__FindSTLClass in TROOT.cxx).
22
# So need to make sure that the reflex dictionary for these classes
23
# is loaded.
24
cppyy.load_library(
'libAtlasSTLAddReflexDict'
)
25
ROOT.TClass(
'vector<float>'
)
26
ROOT.TClass(
'vector<vector<unsigned int> >'
)
27
from
ordereddict
import
OrderedDict
28
29
30
# Work around a library load order issue...
31
ROOT.D3PDTest.MapDumper
32
33
def
topy
(o):
34
if
type
(o).__name__
in
[
'map<string,string>'
,
35
'map<string,float>'
,
36
'map<string,int>'
]:
37
keys = ROOT.D3PDTest.MapDumper.keys (o)
38
values = ROOT.D3PDTest.MapDumper.values (o)
39
return
OrderedDict (sorted (zip (keys, values), key=
lambda
t: t[0]))
40
if
(
type
(o).__name__.startswith (
'vector<char'
)
or
41
type
(o).__name__.startswith (
'vector<unsigned char'
)):
42
ll = list(o)
43
lens = [len(x)
for
x
in
ll]
44
if
lens
and
min
(lens) == 1
and
max
(lens) == 1:
45
ll = [ord(x)
for
x
in
ll]
46
return
ll
47
if
type
(o).__name__.startswith (
'vector<'
):
48
return
[
topy
(x)
for
x
in
o]
49
if
type
(o).__name__
in
[
'PyFloatBuffer'
,
50
'PyIntBuffer'
]:
51
return
list(o)
52
return
o
53
54
55
def
alleq
(l):
56
if
len(l) == 0:
return
True
57
for
i
in
range(1, len(l)):
58
if
l[i] != l[0]:
return
False
59
return
True
60
61
62
def
isint
(o):
63
try
:
64
_ = int(o)
65
except
ValueError:
66
return
False
67
return
True
68
69
70
def
form_list
(l):
71
if
not
isinstance(l, list):
72
return
l
73
return
'['
+
','
.join(l) +
']'
74
def
squash_list
(l):
75
if
not
l:
return
l
76
frags=[]
77
nextfrag = []
78
i = 0
79
while
i < len (l):
80
if
i < len(l)-1
and
l[i] == l[i+1]:
81
if
nextfrag: frags.append(nextfrag)
82
nextfrag = []
83
j = i+2
84
while
j < len(l)
and
l[i] == l[j]:
85
j += 1
86
frags.append (
'%d*[%s]'
% (j-i, l[i]))
87
i = j
88
#elif (i < len(l)-2 and isint(l[i]) and
89
# isint(l[i+1]) and isint(l[i+2]) and
90
# int(l[i+1]) == int(l[i])+1 and int(l[i+2]) == int(l[i])+2):
91
# nextfrag = []
92
# j = i+3
93
# while j < len(l) and isint(l[j]) and int(l[j]) == int(l[i]) + (j-i):
94
# j += 1
95
# frags.append ('range(%s,%d)' % (l[i], int(l[i]) + (j-i)))
96
# i = j
97
else
:
98
nextfrag.append (l[i])
99
i += 1
100
if
nextfrag: frags.append (nextfrag)
101
frags = [
form_list
(s)
for
s
in
frags]
102
return
'+'
.join (frags)
103
104
105
106
def
tostr
(o):
107
if
isinstance(o, list):
108
l = [
tostr
(x)
for
x
in
o]
109
if
len(l) > 1:
110
return
squash_list
(l)
111
return
'['
+
', '
.join (l) +
']'
112
if
isinstance(o, str):
113
s = repr(o)
114
else
:
115
s = str(o)
116
if
s.endswith (
'.0'
): s = s[:-2]
117
return
s
118
119
120
def
common_length
(s1, s2):
121
i = 0
122
while
i < len(s1)
and
i < len(s2)
and
s1[i] == s2[i]:
123
i += 1
124
return
i
125
126
def
merge_names
(blist):
127
out = []
128
lasttag =
''
129
for
(i,b)
in
enumerate(blist):
130
if
i < len(blist)-1:
131
b2 = blist[i+1]
132
else
:
133
b2 =
''
134
comm1 = common_length (lasttag, b)
135
comm2 = common_length (b, b2)
136
if
comm1 >= comm2:
137
if
comm1 != len(lasttag):
138
while
comm1 > 0
and
b[comm1-1] !=
'_'
:
139
comm1 -= 1
140
if
b[:comm1] != lasttag:
141
lasttag = b[:comm1]
142
out.append (
':'
+ lasttag)
143
else
:
144
while
comm2 > 0
and
b[comm2-1] !=
'_'
:
145
comm2 -= 1
146
if
b[:comm2] != lasttag:
147
lasttag = b[:comm2]
148
out.append (
':'
+ lasttag)
149
150
nm = b[len(lasttag):]
151
if
len(nm) == 0: nm =
'$'
152
out.append (nm)
153
return
out
154
155
156
157
def
dumptree
(tt, by_branch):
158
n = tt.GetEntries()
159
if
tt.GetName().startswith (
'dum_'
)
and
n > 10000000:
160
print
(
'[Dummy tree skipped]'
)
161
return
162
bb = [b.GetName()
for
b
in
tt.GetListOfBranches()]
163
bb.sort()
164
if
by_branch:
165
bb = merge_names (bb)
166
print
(
'\nBranches'
)
167
print
(
'-----------'
)
168
lasttag =
''
169
for
b
in
bb:
170
if
b[0] ==
':'
:
171
print
(b)
172
lasttag = b[1:]
173
continue
174
if
b ==
'$'
:
175
bname = lasttag
176
else
:
177
bname = lasttag + b
178
br = tt.GetBranch(bname)
179
data = []
180
for
i
in
range(n):
181
br.GetEntry(i)
182
data.append (topy (getattr(tt, bname)))
183
print
(b, tostr (data))
184
else
:
185
for
i
in
range(n):
186
tt.GetEntry(i)
187
print
(
'\nEvent'
, i)
188
print
(
'-----------'
)
189
for
b
in
bb:
190
print
(b, tostr (topy (getattr (tt, b))))
191
return
192
193
194
def
dumphist
(h):
195
print
(
'bins: '
, [h.GetBinContent(i)
for
i
in
range(h.GetSize())])
196
if
h.GetSumw2N():
197
print
(
'errs: '
, [h.GetBinError(i)
for
i
in
range(h.GetSize())])
198
return
199
200
201
202
203
def
dumpdir
(f, by_branch, pref=''):
204
kk = [k.GetName()
for
k
in
f.GetListOfKeys()]
205
kk.sort()
206
for
k
in
kk:
207
o = f.Get (k)
208
if
k ==
'Schema'
:
209
pass
210
elif
isinstance (o, ROOT.TTree):
211
print
(
'\n\n\nTree'
, pref+k)
212
dumptree (o, by_branch)
213
elif
isinstance (o, ROOT.TH1):
214
print
(
'\n\n\nTH1'
, pref+k)
215
dumphist (o)
216
elif
isinstance (o, ROOT.TDirectory):
217
dumpdir (o, by_branch, pref + k +
'/'
)
218
elif
isinstance (o, ROOT.TObjString):
219
print
(
'\n\n\nString'
, pref+k)
220
if
k ==
'_pickle'
:
221
print
(
'[pickle data skipped]'
)
222
else
:
223
print
(o, end=
''
)
224
print
(
'__END_OF_STRING__'
)
225
else
:
226
print
(
'\n\n\nKey'
, pref+k)
227
print
(o)
228
return
229
230
231
232
def
dumpit
(file, by_branch):
233
f=ROOT.TFile(file)
234
dumpdir (f, by_branch=by_branch)
235
return
236
237
238
if
__name__ ==
'__main__'
:
239
import
sys
240
file =
'test1.root'
241
by_branch =
False
242
if
len(sys.argv) > 1
and
sys.argv[1] ==
'--by-branch'
:
243
by_branch =
True
244
del sys.argv[1]
245
if
len(sys.argv) > 1:
246
file = sys.argv[1]
247
dumpit (file, by_branch)
248
min
#define min(a, b)
Definition
cfImp.cxx:40
max
#define max(a, b)
Definition
cfImp.cxx:41
python.dumptuple_any.merge_names
merge_names(blist)
Definition
dumptuple_any.py:126
python.dumptuple_any.alleq
alleq(l)
Definition
dumptuple_any.py:55
python.dumptuple_any.dumptree
dumptree(tt, by_branch)
Definition
dumptuple_any.py:157
python.dumptuple_any.dumpdir
dumpdir(f, by_branch, pref='')
Definition
dumptuple_any.py:203
python.dumptuple_any.form_list
form_list(l)
Definition
dumptuple_any.py:70
python.dumptuple_any.dumpit
dumpit(file, by_branch)
Definition
dumptuple_any.py:232
python.dumptuple_any.squash_list
squash_list(l)
Definition
dumptuple_any.py:74
python.dumptuple_any.dumphist
dumphist(h)
Definition
dumptuple_any.py:194
python.dumptuple_any.isint
isint(o)
Definition
dumptuple_any.py:62
python.dumptuple_any.tostr
tostr(o)
Definition
dumptuple_any.py:106
python.dumptuple_any.common_length
common_length(s1, s2)
Definition
dumptuple_any.py:120
python.dumptuple_any.topy
topy(o)
Definition
dumptuple_any.py:33
type
Generated on
for ATLAS Offline Software by
1.17.0