CMS 3D CMS Logo

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