From 4ccf5cc9f8142929adb412783e18a2c2f0719c0c Mon Sep 17 00:00:00 2001
From: Walter Lampl <Walter.Lampl@cern.ch>
Date: Fri, 26 Mar 2021 12:47:10 +0100
Subject: [PATCH] Make EventCounterAlg reentrant

---
 .../GaudiSequencer/src/AthEventCounter.cxx    | 40 +++++--------------
 Control/GaudiSequencer/src/AthEventCounter.h  | 32 +++++----------
 .../share/RecExCommon_topOptions.py           |  6 +--
 3 files changed, 25 insertions(+), 53 deletions(-)

diff --git a/Control/GaudiSequencer/src/AthEventCounter.cxx b/Control/GaudiSequencer/src/AthEventCounter.cxx
index 93b1135dde40..f35878fadc73 100644
--- a/Control/GaudiSequencer/src/AthEventCounter.cxx
+++ b/Control/GaudiSequencer/src/AthEventCounter.cxx
@@ -10,26 +10,7 @@
 ///////////////////////////////////////////////////////////////////
 
 #include "AthEventCounter.h"
-
-/**
- ** Constructor(s)
- **/
-AthEventCounter::AthEventCounter(const std::string& name, ISvcLocator* pSvcLocator) :
-  AthAlgorithm( name, pSvcLocator ),
-  m_skip ( 0 ),
-  m_total( 0 )
-{
-  declareProperty( "Frequency", m_frequency=1,
-                   "The frequency with which the number of events should be "
-                   "reported. The default is 1, corresponding to every event" );
-  m_frequency.verifier().setBounds( 0, 1000 );
-}
-
-/**
- ** Destructor
- **/
-AthEventCounter::~AthEventCounter( )
-{}
+#include <cstdlib>
 
 StatusCode
 AthEventCounter::initialize()
@@ -39,16 +20,17 @@ AthEventCounter::initialize()
 }
 
 StatusCode
-AthEventCounter::execute()
+AthEventCounter::execute(const EventContext& ) const 
 {
-  m_total++;
-  int freq = m_frequency;
-  if ( freq > 0 ) {
-    m_skip++;
-    if ( m_skip >= freq ) {
-      ATH_MSG_INFO ("execute ==> seen events: " << m_total);
-      m_skip = 0;
-    }
+  int currTotal=(m_total++); 
+  if (m_frequency==0) {
+    return StatusCode::SUCCESS;
+  }
+
+  int rem=std::div(currTotal,m_frequency).rem;
+
+  if (rem==0) {
+      ATH_MSG_INFO ("execute ==> seen events: " << currTotal);
   }
   return StatusCode::SUCCESS;
 }
diff --git a/Control/GaudiSequencer/src/AthEventCounter.h b/Control/GaudiSequencer/src/AthEventCounter.h
index eda3a324da6f..573ee85aac47 100644
--- a/Control/GaudiSequencer/src/AthEventCounter.h
+++ b/Control/GaudiSequencer/src/AthEventCounter.h
@@ -1,7 +1,7 @@
 ///////////////////////// -*- C++ -*- /////////////////////////////
 
 /*
-  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
+  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
 */
 
 // AthEventCounter.h
@@ -11,29 +11,23 @@
 #ifndef GAUDISEQUENCER_ATHEVENTCOUNTER_H
 #define GAUDISEQUENCER_ATHEVENTCOUNTER_H
 
-#include "AthenaBaseComps/AthAlgorithm.h"
+#include "AthenaBaseComps/AthReentrantAlgorithm.h"
 #include "Gaudi/Property.h"
+#include <atomic>
 
-class AthEventCounter : public AthAlgorithm 
+class AthEventCounter : public AthReentrantAlgorithm 
 {
 public:
 
-  /**
-   ** Constructor(s)
-   **/
-  AthEventCounter( const std::string& name, ISvcLocator* pSvcLocator );
-
-  /**
-   ** Destructor
-   **/
-  ~AthEventCounter( );
+  using AthReentrantAlgorithm::AthReentrantAlgorithm;
+  ~AthEventCounter( ) = default;
 
   /*****************************
    ** Public Function Members **
    *****************************/
 
   StatusCode initialize();
-  StatusCode execute();
+  StatusCode execute(const EventContext& ctx) const ;
   StatusCode finalize();
 
 private:
@@ -47,18 +41,14 @@ private:
    ** should be reported. The default is 1, corresponding
    ** to every event.
    **/
-  Gaudi::CheckedProperty<int> m_frequency;
-
-  /**
-   ** The number of events skipped since the last time
-   ** the count was reported.
-   **/
-  int m_skip;
+  Gaudi::Property<int> m_frequency{this, "Frequency", 1,
+      "The frequency with which the number of events should be "
+      "reported. The default is 1, corresponding to every event" };
 
   /**
    ** The total events seen.
    **/
-  int m_total;
+  mutable std::atomic<int> m_total{0};
 };
 
 #endif // GAUDISEQUENCER_ATHEVENTCOUNTER_H
diff --git a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
index 7548b782cd65..f28933f2cb8d 100644
--- a/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
+++ b/Reconstruction/RecExample/RecExCommon/share/RecExCommon_topOptions.py
@@ -419,13 +419,13 @@ if rec.doJiveXML() and DetFlags.detdescr.ID_on() :
 # put quasi empty first algorithm so that the first real
 # algorithm does not see the memory change due to event manipulation
 #from AthenaPoolTools.AthenaPoolToolsConf import EventCounter
-from GaudiAlg.GaudiAlgConf import EventCounter
-
+#from GaudiAlg.GaudiAlgConf import EventCounter
+from GaudiSequencer.GaudiSequencerConf import AthEventCounter as EventCounter
 
 import PerfMonComps.DomainsRegistry as pdr
 pdr.flag_domain('admin')
 # one print every 100 event
-topSequence+=EventCounter(Frequency=100)
+topSequence+=EventCounter("EventCounter",Frequency=100)
 
 #Temporary: Schedule conversion algorithm for EventInfo object:
 # Note that we need to check whether the HLT already added this algorithm to the
-- 
GitLab