The Gaudi Framework  v36r1 (3e2fb5a8)
Counter.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
13 """
14 *******************************************************************************
15 * *
16 * Simple example which illustrates the usage of the useful base class *
17 * GaudiAlgo (python version of C++ GaudiAlgorithm) for "easy" manipulations *
18 * with 'counters' *
19 * *
20 * The example is equivalent to C++ example 'CounterEx', see *
21 * GaudiExamples/src/CounterEx/*.cpp and *
22 * GaudiExamples/options/CounterEx.opts *
23 * *
24 *******************************************************************************
25 """
26 from __future__ import print_function
27 import six
28 # =============================================================================
29 __author__ = 'Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr'
30 # =============================================================================
31 
32 import GaudiPython
33 
34 from GaudiPython.GaudiAlgs import GaudiAlgo
35 
36 Rndm = GaudiPython.gbl.Rndm
37 Numbers = Rndm.Numbers
38 SUCCESS = GaudiPython.SUCCESS
39 
40 Numbers.__call__ = Numbers.shoot
41 
42 # =============================================================================
43 # Simple algorithm which manipulates with counters
44 # =============================================================================
45 
46 
48  """ Simple algorithm which manipulates with counters """
49 
50  def __init__(self, name='Counter'):
51  """ Constructor """
52  GaudiAlgo.__init__(self, name)
53 
54  def execute(self):
55  """ The major method 'execute', it is invoked for each event """
56 
57  executed = self.counter('executed')
58  executed += 1.
59 
60  gauss = Numbers(self.randSvc(), Rndm.Gauss(0.0, 1.0))
61  poisson = Numbers(self.randSvc(), Rndm.Poisson(5.0))
62 
63  # 'accuulate gauss'
64  value = gauss.shoot()
65 
66  g1 = self.counter('gauss')
67  g2 = self.counter('g2')
68 
69  g1 += value
70  g2 += value * value
71 
72  if 0 < value:
73  gp = self.counter('Gpos')
74  gp += 1.
75  else:
76  gn = self.counter('Gneg')
77  gn += 1.
78 
79  stat1 = self.counter('NG')
80  stat2 = self.counter('G')
81  for i in range(0, int(poisson())):
82  stat1 += 1.
83  stat2 += gauss()
84 
85  stat3 = self.counter('eff')
86  stat3 += value > 0
87 
88  # print statistics every 1000 events
89  executed = self.counter('executed')
90  prnt = int(executed.flag())
91  if 0 == prnt % 1000:
92  six.print_(" Event number %s " % prnt, flush=True)
93  bc = self.counter('eff')
94  line = "(%.12g += %.12g)%s" % (bc.eff() * 100, bc.effErr() * 100,
95  '%')
96  six.print_(
97  ' Efficiency (binomial counter "eff"): %s' % line, flush=True)
98 
99  return SUCCESS
100 
101 
102 # =============================================================================
103 # job configuration
104 # =============================================================================
105 def configure(gaudi=None):
106  """ Configuration of the job """
107 
108  if not gaudi:
109  gaudi = GaudiPython.AppMgr()
110 
111  gaudi.JobOptionsType = 'NONE'
112  gaudi.EvtSel = 'NONE'
113 
114  gaudi.config()
115 
116  alg = Counter()
117  gaudi.setAlgorithms([alg])
118  gaudi.ExtSvc += ['Gaudi::Monitoring::MessageSvcSink']
119 
120  return SUCCESS
121 
122 
123 # =============================================================================
124 # The actual job excution
125 # =============================================================================
126 if '__main__' == __name__:
127  print(__doc__ + __author__)
129  configure(gaudi)
130  gaudi.run(5400)
131 
132 # =============================================================================
133 # The END
134 # =============================================================================
Counter.configure
def configure(gaudi=None)
Definition: Counter.py:105
GaudiPython.Bindings.AppMgr
Definition: Bindings.py:842
GaudiPython.GaudiAlgs.GaudiAlgo
Definition: GaudiAlgs.py:639
Counter.Numbers
Numbers
Definition: Counter.py:37
Rndm::Gauss
Parameters for the Gauss random number generation.
Definition: RndmGenerators.h:32
Rndm::Numbers
Random number accessor This small class encapsulates the use of the random number generator.
Definition: RndmGenerators.h:359
HistoUtilsEx.gauss
gauss
Definition: HistoUtilsEx.py:66
Counter.Counter.execute
def execute(self)
Definition: Counter.py:54
Rndm::Poisson
Parameters for the Poisson distributed random number generation with a given mean.
Definition: RndmGenerators.h:209
GaudiPython.GaudiAlgs
Definition: GaudiAlgs.py:1
Counter.Counter.__init__
def __init__(self, name='Counter')
Definition: Counter.py:50
Gaudi::Functional::details::zip::range
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
Definition: FunctionalDetails.h:97
Counter.Counter
Definition: Counter.py:47