CounterNewAlg.cpp 4.13 KB
Newer Older
1
2
3
4
5
// ============================================================================
// Include files
// ============================================================================
// GaudiKernel
// ============================================================================
Frank Winklmeier's avatar
Frank Winklmeier committed
6
7
#include "Gaudi/Timers.h"

8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "GaudiKernel/RndmGenerators.h"
// ============================================================================
// GaudiAlg
// ============================================================================
#include "GaudiAlg/Producer.h"
// ============================================================================

// ============================================================================
/** @file
 *  Simple example showing the usage of the new 'counter' facilities
 *  (with a simple 'producer' type of functional algorithm)
 *
 *  @see GaudiAlgorithm
 *  @see StatEntity
 *
 *  @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
 *  @date 2008-08-06
 *
 *  @author Andrea Valassi
 *  @date 2018-02-13
 */
// ============================================================================

// ============================================================================
/** @class CounterNewAlg
 *
 *  Simple example showing the usage of the new 'counter' facilities
 *  (with a simple 'producer' type of functional algorithm)
 *
 *  @see GaudiAlgorithm
 *  @see StatEntity
 *
 *  @author Vanya BELYAEV Ivan.Belyaev@lapp.in2p3.fr
 *  @date 2008-08-06
 *
 *  @author Andrea Valassi
 *  @date 2018-02-13
 */
// ============================================================================
47

Gitlab CI's avatar
Gitlab CI committed
48
class CounterNewAlg : public Gaudi::Functional::Producer<int()> {
49
50
51
52
public:
  int operator()() const override;

  CounterNewAlg( const std::string& name, ISvcLocator* pSvc )
Gitlab CI's avatar
Gitlab CI committed
53
      : Producer( name, pSvc, KeyValue( "OutputLocation", "dummy" ) ) {
54
55
56
57
58
    setProperty( "StatPrint", "true" ).ignore();
  }

private:
  // counters
Frank Winklmeier's avatar
Frank Winklmeier committed
59
60
61
62
63
64
65
66
67
68
  mutable StatEntity   m_assign_counter{this, "assign"};
  mutable StatEntity   m_eff_counter{this, "eff"};
  mutable StatEntity   m_executed_counter{this, "executed"};
  mutable StatEntity   m_G_counter{this, "G"};
  mutable StatEntity   m_g2_counter{this, "g2"};
  mutable StatEntity   m_gauss_counter{this, "gauss"};
  mutable StatEntity   m_Gneg_counter{this, "Gneg"};
  mutable StatEntity   m_Gpos_counter{this, "Gpos"};
  mutable StatEntity   m_NG_counter{this, "NG"};
  mutable Gaudi::Timer m_timer{this, "time"};
69
70
71
72
73
74
75
76
};
// ============================================================================

// ============================================================================
DECLARE_COMPONENT( CounterNewAlg )
// ============================================================================

// ============================================================================
Gitlab CI's avatar
Gitlab CI committed
77
int CounterNewAlg::operator()() const {
78

Frank Winklmeier's avatar
Frank Winklmeier committed
79
  auto timeit = m_timer();
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  // count overall number of executions:
  ++m_executed_counter;

  Rndm::Numbers gauss( randSvc(), Rndm::Gauss( 0.0, 1.0 ) );
  Rndm::Numbers poisson( randSvc(), Rndm::Poisson( 5.0 ) );

  // 'accumulate' gauss
  const double value = gauss();

  m_gauss_counter += value;
  m_g2_counter += value * value;

  ( 0 < value ) ? ++m_Gpos_counter : ++m_Gneg_counter;

94
95
  StatEntity& stat1 = m_NG_counter;
  StatEntity& stat2 = m_G_counter;
96
97
98
99
100
101
102
103
104
105
106
107
108
109

  const int num = (int)poisson();
  for ( int i = 0; i < num; ++i ) {
    stat1++;
    stat2 += gauss();
  }

  // assignement
  m_assign_counter = value;

  // counter of efficiency
  m_eff_counter += ( 0 < value );

  // print the statistics every 1000 events
110
111
  StatEntity& executed = m_executed_counter;
  const int   print    = (int)executed.flag();
112
113
  if ( 0 == print % 1000 ) {
    info() << " Event number " << print << endmsg;
114
115
116
117
    always() << "Number of counters : " << nCounters();
    this->forEachCounter(
        [&]( const std::string& label, const auto& counter ) { counter.print( this->always() << '\n', label ); } );
    always() << endmsg;
118
119
120
121
122
123
124
    info() << " Efficiency (binomial counter: \"eff\"): (" << m_eff_counter.eff() * 100.0 << " +- "
           << m_eff_counter.effErr() * 100.0 << ")%" << endmsg;
  }

  return 42;
}
// ============================================================================