5 from PhysicsTools.HeppyCore.utils.diclist
import diclist
14 self.add( level, [level, 0] )
16 def inc(self, level, nentries=1):
17 '''Call this function to create a level for this counter,
18 or to increment an existing level.
20 if level
not in self.dico:
21 raise ValueError(
'level', level,
'has not been registered')
24 self[level][1] += nentries
27 '''Add two counters (+).'''
28 size =
max( len(self), len(other))
30 for i
in range(0, size):
36 self.
inc( other[i][0], other[i][1] )
39 if self[i][0] != other[i][0]:
40 err = [
'cannot add these counters:', str(self), str(other)]
41 raise ValueError(
'\n'.
join(err))
43 self.
inc( other[i][0], other[i][1] )
47 '''Add two counters (+=).'''
51 '''Dump the counter to a pickle file and to a text file in dirname.'''
52 pckfname =
'{d}/{f}.pck'.
format(d=dirname, f=self.
name)
53 pckfname = pckfname.replace(
'*',
'STAR')
54 pckfile = open( pckfname,
'w' )
55 pickle.dump(self, pckfile)
56 txtfile = open( pckfname.replace(
'.pck',
'.txt'),
'w')
57 txtfile.write( str(self) )
62 retstr =
'Counter %s :\n' % self.
name
65 for level, count
in self:
72 eff1 = float(count)/prev
76 eff2 = float(count)/init
77 retstr +=
'\t {level:<40} {count:>9} \t {eff1:4.2f} \t {eff2:6.4f}\n'.
format(
90 TODO: could be a diclist?
99 self.counters.append(
Counter(name) )
109 return '\n'.
join(prints)
114 if __name__ ==
'__main__':
123 cs.addCounter(
'test')
124 cs.counter(
'test').register(
'a')
125 cs.counter(
'test').register(
'b')
126 cs.addCounter(
'test2')
127 cs.counter(
'test2').register(
'a')
128 cs.counter(
'test').inc(
'a')
129 cs.counter(
'test').inc(
'a')
130 cs.counter(
'test').inc(
'b')
131 cs.counter(
'test2').inc(
'a')
137 file = open(
'test.pck')
138 lcs = pickle.load(file)
141 c1 = cs.counter(
'test2')
143 print 'test addition, adding test and test2'
145 c2 = cs.counter(
'test')
149 print 'test addition : incompatible'
static std::string join(char **cmd)