From b8028e34d2c834816904625727db4989880126fd Mon Sep 17 00:00:00 2001
From: Frank Berghaus <frank.berghaus@cern.ch>
Date: Tue, 22 Dec 2020 13:59:53 +0100
Subject: [PATCH] Update generation parameter channel number on EVNT-to-EVNT
 filtering

Cange the channel number where the generation parameters are found in
the conditions attributes when updating run and MC channel numbers
during generator level filtering. Also update the MC channel number in
the /TagInfo if it exists.
---
 Generators/EvgenProdTools/CMakeLists.txt      |  4 +-
 .../EvgenProdTools/CountHepMC.h               |  2 +
 Generators/EvgenProdTools/src/CountHepMC.cxx  | 64 +++++++++++++++++++
 3 files changed, 68 insertions(+), 2 deletions(-)

diff --git a/Generators/EvgenProdTools/CMakeLists.txt b/Generators/EvgenProdTools/CMakeLists.txt
index a15920cd613c..6ff81b8bc62b 100644
--- a/Generators/EvgenProdTools/CMakeLists.txt
+++ b/Generators/EvgenProdTools/CMakeLists.txt
@@ -12,11 +12,11 @@ atlas_add_library( EvgenProdToolsLib
                    PUBLIC_HEADERS EvgenProdTools
                    INCLUDE_DIRS ${ROOT_INCLUDE_DIRS} 
                    LINK_LIBRARIES ${ROOT_LIBRARIES} AtlasHepMCLib AtlasHepMCsearchLib AthenaBaseComps TruthHelper GeneratorModulesLib GenInterfacesLib
-                   PRIVATE_LINK_LIBRARIES AthenaKernel EventInfo GaudiKernel TruthUtils )
+                   PRIVATE_LINK_LIBRARIES AthenaKernel EventInfo GaudiKernel TruthUtils IOVDbDataModel AthenaPoolUtilities )
 
 atlas_add_component( EvgenProdTools
                      src/components/*.cxx
-                     LINK_LIBRARIES EvgenProdToolsLib )
+                     LINK_LIBRARIES EvgenProdToolsLib IOVDbDataModel AthenaPoolUtilities )
 
 # Install files from the package:
 atlas_install_python_modules( python/*.py POST_BUILD_CMD ${ATLAS_FLAKE8} )
diff --git a/Generators/EvgenProdTools/EvgenProdTools/CountHepMC.h b/Generators/EvgenProdTools/EvgenProdTools/CountHepMC.h
index 1e4e196cfd59..16bab10dfbdc 100644
--- a/Generators/EvgenProdTools/EvgenProdTools/CountHepMC.h
+++ b/Generators/EvgenProdTools/EvgenProdTools/CountHepMC.h
@@ -28,6 +28,8 @@ public:
 
 private:
 
+  ServiceHandle< StoreGateSvc > m_metaDataStore{
+    "StoreGateSvc/MetaDataStore", name()};
   int m_nPass;
   int m_nCount;
 
diff --git a/Generators/EvgenProdTools/src/CountHepMC.cxx b/Generators/EvgenProdTools/src/CountHepMC.cxx
index 4f9d5a38afba..c0f8d0ff2660 100644
--- a/Generators/EvgenProdTools/src/CountHepMC.cxx
+++ b/Generators/EvgenProdTools/src/CountHepMC.cxx
@@ -15,6 +15,9 @@
 #include "EventInfo/EventInfo.h"
 #include "EventInfo/EventID.h"
 #include "EventInfo/EventType.h"
+#include "IOVDbDataModel/IOVMetaDataContainer.h"
+#include "IOVDbDataModel/IOVPayloadContainer.h"
+#include "AthenaPoolUtilities/CondAttrListCollection.h"
 #include <cmath>
 #include <cassert>
 
@@ -100,10 +103,12 @@ else{
     // Change the EventID in the eventinfo header
     const EventInfo* pInputEvt(0);
     ATH_MSG_INFO("Set new run number called !!" << m_newRunNumber);
+    unsigned int oldRunNumber = 0;
     if (evtStore()->retrieve(pInputEvt).isSuccess()) {
       assert(pInputEvt);
       EventID* eventID = const_cast<EventID*>(pInputEvt->event_ID());
       ATH_MSG_INFO("git eventid !! " );
+      oldRunNumber = eventID->run_number();
       eventID->set_run_number(m_newRunNumber);
       ATH_MSG_INFO("Set new run number" << m_newRunNumber);
       ATH_MSG_DEBUG("Set new run number in event_ID");
@@ -118,6 +123,65 @@ else{
       ATH_MSG_ERROR("No EventInfo object found");
       return StatusCode::SUCCESS;
     }
+
+    {
+      // change the channel number where /Generation/Parameters are found
+      auto newChannelNumber =
+          static_cast< const CondAttrListCollection::ChanNum >(m_newRunNumber);
+      auto oldChannelNumber = 
+          static_cast< const CondAttrListCollection::ChanNum >(oldRunNumber);
+
+      const char* key = "/Generation/Parameters";
+      const IOVMetaDataContainer * iovContainer = nullptr;
+      if (m_metaDataStore->retrieve(iovContainer, key).isSuccess()
+          && iovContainer) {
+        // get a hold of the payload
+        const IOVPayloadContainer * payloadContainer =
+            iovContainer->payloadContainer();
+
+        // Grab the attribute list
+        for (CondAttrListCollection* collection : *payloadContainer) {
+          for(unsigned int index = 0; index < collection->size(); ++index) {
+            if (collection->chanNum(index) != oldChannelNumber) {
+              ATH_MSG_INFO("Not updating \"" << key << "\" on channel number "
+                           << collection->chanNum(index));
+              continue;
+            }
+
+            if (collection->fixChanNum(oldChannelNumber, newChannelNumber))
+              ATH_MSG_INFO("Updated \"" << key << "\" channel number from "
+                           << oldChannelNumber << " to " << newChannelNumber);
+            else
+              ATH_MSG_ERROR("Channel number update from " << oldChannelNumber
+                            << " to " << newChannelNumber << " on \"" << key
+                            << "\" FAILED");
+          }
+        }
+
+        {
+          // Update the MC channel number in the "/TagInfo"
+          const char* key = "/TagInfo";
+          const IOVMetaDataContainer * iovContainer = nullptr;
+          if (m_metaDataStore->retrieve(iovContainer, key).isSuccess()
+              && iovContainer) {
+            // get a hold of the payload
+            const IOVPayloadContainer * payloadContainer =
+              iovContainer->payloadContainer();
+
+            // Grab the attribute list
+            for (CondAttrListCollection* collection : *payloadContainer) {
+              for (auto pair : *collection) {
+                // pair is a pair of Channel number and AttributeList
+                if (pair.second.exists("mc_channel_number"))
+                  pair.second["mc_channel_number"].setValue(m_newRunNumber);
+              }
+            }
+          }
+        }
+      } else {
+        ATH_MSG_INFO("Could not retrieve \"" << key << "\" from MetaDataStore");
+      }
+    }
   }
 
 
-- 
GitLab