CMS 3D CMS Logo

counter.py
Go to the documentation of this file.
1  # Copyright (C) 2014 Colin Bernet
2 # https://github.com/cbernet/heppy/blob/master/LICENSE
3 
4 from builtins import range
5 import pickle
6 from PhysicsTools.HeppyCore.utils.diclist import diclist
7 
8 class Counter(diclist):
9 
10  def __init__(self, name):
11  self.name = name
12  super(Counter, self).__init__()
13 
14  def register(self, level):
15  self.add( level, [level, 0] )
16 
17  def inc(self, level, nentries=1):
18  '''increment an existing level
19  '''
20  if level not in self.dico:
21  raise ValueError('level', level, 'has not been registered')
22  else:
23  self[level][1] += nentries
24 
25  def __add__(self, other):
26  '''Add two counters (+).'''
27  size = max( len(self), len(other))
28  for i in range(0, size):
29  if i>=len(other):
30  # this line exists only in this counter, leave it as is
31  continue
32  elif i>=len(self):
33  self.register( other[i][0])
34  self.inc( other[i][0], other[i][1] )
35  else:
36  if self[i][0] != other[i][0]:
37  err = ['cannot add these counters:', str(self), str(other)]
38  raise ValueError('\n'.join(err))
39  else:
40  self.inc( other[i][0], other[i][1] )
41  return self
42 
43  def __iadd__(self, other):
44  '''Add two counters (+=).'''
45  return self.__add__(other)
46 
47  def write(self, dirname):
48  '''Dump the counter to a pickle file and to a text file in dirname.'''
49  pckfname = '{d}/{f}.pck'.format(d=dirname, f=self.name)
50  pckfname = pckfname.replace('*','STAR')
51  pckfile = open( pckfname, 'w' )
52  pickle.dump(self, pckfile)
53  txtfile = open( pckfname.replace('.pck', '.txt'), 'w')
54  txtfile.write( str(self) )
55  txtfile.write( '\n' )
56  txtfile.close()
57 
58  def __str__(self):
59  retstr = 'Counter %s :\n' % self.name
60  prev = None
61  init = None
62  for level, count in self:
63  if prev == None:
64  prev = count
65  init = count
66  if prev == 0:
67  eff1 = -1.
68  else:
69  eff1 = float(count)/prev
70  if init == 0:
71  eff2 = -1.
72  else:
73  eff2 = float(count)/init
74  retstr += '\t {level:<40} {count:>9} \t {eff1:4.2f} \t {eff2:6.4f}\n'.format(
75  level=level,
76  count=count,
77  eff1=eff1,
78  eff2=eff2 )
79  prev = count
80  return retstr
81 
82 
83 
84 
86  '''
87  TODO: could be a diclist?
88  '''
89 
90  def __init__( self ):
91  self.counters = []
92  self.ranks = {}
93 
94  def addCounter(self, name):
95  self.ranks[ name ] = len( self.counters )
96  self.counters.append( Counter(name) )
97 
98  def counter(self, name):
99  return self.counters[ self.ranks[name] ]
100 
101  def write(self, dirname):
102  map( lambda x: x.write(dirname), self.counters)
103 
104  def __str__(self):
105  prints = map( str, self.counters )
106  return '\n'.join(prints)
107 
108  def __getitem__(self, name):
109  return self.counter(name)
110 
def __init__(self, name)
Definition: counter.py:10
def __getitem__(self, name)
Definition: counter.py:108
def write(self, dirname)
Definition: counter.py:47
def counter(self, name)
Definition: counter.py:98
def write(self, dirname)
Definition: counter.py:101
def __init__(self)
Definition: counter.py:90
def addCounter(self, name)
Definition: counter.py:94
def __str__(self)
Definition: counter.py:58
def inc(self, level, nentries=1)
Definition: counter.py:17
static std::string join(char **cmd)
Definition: RemoteFile.cc:21
def register(self, level)
Definition: counter.py:14
def __str__(self)
Definition: counter.py:104
def __add__(self, other)
Definition: counter.py:25
def __iadd__(self, other)
Definition: counter.py:43
#define str(s)