ATLAS Offline Software
Loading...
Searching...
No Matches
root_lsr_rank.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
5
11
12import ROOT
13import sys
14import os
15import argparse
16
17parser=argparse.ArgumentParser()
18parser.add_argument('filename',
19 help='Input HIST file name')
20parser.add_argument('-r', '--rankorder', default='onfile',
21 choices=['onfile', 'uncompressed', 'name'],
22 help='rankorder is "onfile" (default), "uncompressed" or "name"')
23parser.add_argument('-p', '--path',
24 help='Only look under this directory')
25parser.add_argument('--hash', action='store_true',
26 help='Print hashes of objects')
27parser.add_argument('--metadata', action='store_true',
28 help='Include metadata trees')
29args=parser.parse_args()
30
31ordering = args.rankorder
32
33accounting = {}
34hashes = {}
35types = {}
36
37ROOT.gInterpreter.LoadText("UInt_t bufferhash(TKey* key) { key->SetBuffer(); key->ReadFile(); UInt_t rv = TString::Hash(key->GetBuffer()+key->GetKeylen(), key->GetNbytes()-key->GetKeylen()); key->DeleteBuffer(); return rv; }")
38ROOT.gInterpreter.LoadText("void* getbuffer(TKey* key) { key->SetBuffer(); key->ReadFile(); return (void*) (key->GetBuffer()+key->GetKeylen()); }")
39ROOT.gInterpreter.LoadText("UInt_t bufferhash2(TKey* key) { TObject* obj = key->ReadObj(); TMessage msg(kMESS_OBJECT); msg.WriteObject(obj); UInt_t rv = TString::Hash(msg.Buffer(), msg.Length()); delete obj; return rv; }")
40
41
42def dumpdir(d):
43 thispath = d.GetPath()
44 if ':' in thispath:
45 thispath = thispath.split(':', 1)[1]
46 #print (thispath)
47 subdirs = []
48 for k in d.GetListOfKeys():
49 if not args.metadata and k.GetName() == 'metadata' and k.GetClassName() == 'TTree':
50 continue
51 if k.GetClassName().startswith('TDirectory'):
52 subdirs.append(k)
53 else:
54 if args.hash:
55 #lhash = ROOT.bufferhash(k)
56 #objsize = (k.GetNbytes()-k.GetKeylen())/8
57 #print ((k.GetNbytes()-k.GetKeylen())/8.)
58 #buf = ROOT.getbuffer(k); buf.SetSize(objsize)
59 #print (buf[objsize-1], objsize)
60 #lhash = zlib.adler32(str(buf))
61 #k.DeleteBuffer()
62 #obj=k.ReadObj();
63 #tm=ROOT.TMessage(ROOT.TMessage.kMESS_OBJECT)
64 #tm.WriteObject(obj)
65 lhash = ROOT.bufferhash2(k)
66 #obj.IsA().Destructor(obj)
67 #lhash = 0
68 else:
69 lhash = 0
70 idxname = os.path.join(thispath, k.GetName())
71 accounting[idxname] = (k.GetObjlen(), k.GetNbytes()-k.GetKeylen())
72 hashes[idxname] = lhash
73 types[idxname] = k.GetClassName()
74 #print ('%s,' % os.path.join(thispath, k.GetName()), end='')
75 #obj = k.ReadObj(); obj.IsA().Destructor(obj)
76 #print ('OK')
77 for k in subdirs:
78 dumpdir(k.ReadObj())
79
80f = ROOT.TFile.Open(args.filename)
81if args.path:
82 d = f.Get(args.path.rstrip('/'))
83 if not d:
84 print ("Can't access path", args.path, "- exiting")
85 sys.exit(1)
86else:
87 d = f
88dumpdir(d)
89
90#sortedl = sorted(accounting.items(), key=operator.itemgetter(0,1), reverse=True)
91if ordering == 'onfile':
92 key=lambda x: (x[1][1], x[1][0], x[0]) # noqa: E731
93elif ordering == 'uncompressed':
94 key=lambda x: (x[1][0], x[1][1], x[0]) # noqa: E731
95else:
96 key=lambda x: (x[0], x[1][1], x[1][0]) # noqa: E731
97sortedl = sorted(accounting.items(), key=key, reverse=True)
98if args.hash:
99 print ('\n'.join('%s %s: %d uncompressed, %d on file (hash %s)' % (types[a], a, b, c, hashes[a]) for a, (b, c) in sortedl))
100else:
101 print ('\n'.join('%s %s: %d uncompressed, %d on file' % (types[a], a, b, c) for a, (b, c) in sortedl))
102