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