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 
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
diclist
Definition: diclist.py:1
resolutioncreator_cfi.object
object
Definition: resolutioncreator_cfi.py:4
dqmMemoryStats.float
float
Definition: dqmMemoryStats.py:127
counter.Counters.__getitem__
def __getitem__(self, name)
Definition: counter.py:108
join
static std::string join(char **cmd)
Definition: RemoteFile.cc:17
counter.Counter.__add__
def __add__(self, other)
Definition: counter.py:25
counter.Counter.__str__
def __str__(self)
Definition: counter.py:58
counter.Counter.name
name
Definition: counter.py:11
counter.Counter.write
def write(self, dirname)
Definition: counter.py:47
counter.Counters
Definition: counter.py:85
counter.Counters.__str__
def __str__(self)
Definition: counter.py:104
counter.Counters.counter
def counter(self, name)
Definition: counter.py:98
counter.Counter
Definition: counter.py:8
counter.Counters.write
def write(self, dirname)
Definition: counter.py:101
str
#define str(s)
Definition: TestProcessor.cc:51
counter.Counters.__init__
def __init__(self)
Definition: counter.py:90
counter.Counter.__init__
def __init__(self, name)
Definition: counter.py:10
SiStripPI::max
Definition: SiStripPayloadInspectorHelper.h:169
mps_setup.append
append
Definition: mps_setup.py:85
counter.Counter.register
def register(self, level)
Definition: counter.py:14
counter.Counters.counters
counters
Definition: counter.py:91
counter.Counters.addCounter
def addCounter(self, name)
Definition: counter.py:94
format
counter.Counters.ranks
ranks
Definition: counter.py:92
counter.Counter.__iadd__
def __iadd__(self, other)
Definition: counter.py:43
genParticles_cff.map
map
Definition: genParticles_cff.py:11
counter.Counter.inc
def inc(self, level, nentries=1)
Definition: counter.py:17