From 9873a69e861146c0833397dd3332af50d30ec0bf Mon Sep 17 00:00:00 2001
From: scott snyder <snyder@bnl.gov>
Date: Fri, 12 May 2017 23:57:56 +0200
Subject: [PATCH] IOVSvc: Workaround for conditions access in MC.

For MC files, need to use conditionsRun, not the run number,
to look up conditions.  Eventually should be able to get conditionsRun
from the event context, but for now, look it up from the event context.



Former-commit-id: 9378b2db8ff6b8852436f559e4d5cd870a5afbed
---
 Control/IOVSvc/CMakeLists.txt          |  7 ++++---
 Control/IOVSvc/src/CondInputLoader.cxx | 18 ++++++++++++++++--
 Control/IOVSvc/src/CondInputLoader.h   |  5 +++--
 3 files changed, 23 insertions(+), 7 deletions(-)

diff --git a/Control/IOVSvc/CMakeLists.txt b/Control/IOVSvc/CMakeLists.txt
index 169f039d03e..579dd412950 100644
--- a/Control/IOVSvc/CMakeLists.txt
+++ b/Control/IOVSvc/CMakeLists.txt
@@ -14,6 +14,7 @@ atlas_depends_on_subdirs( PUBLIC
                           GaudiKernel
                           PRIVATE
                           AtlasTest/TestTools
+                          Database/PersistentDataModel
                           Event/EventInfo 
 			  Event/xAOD/xAODEventInfo )
 
@@ -25,19 +26,19 @@ atlas_add_library( IOVSvcLib
                    src/*.cxx
                    PUBLIC_HEADERS IOVSvc
                    INCLUDE_DIRS ${Boost_INCLUDE_DIRS}
-                   LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools GaudiKernel StoreGateLib SGtests xAODEventInfo
+                   LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools GaudiKernel StoreGateLib SGtests xAODEventInfo PersistentDataModel
                    PRIVATE_LINK_LIBRARIES TestTools EventInfo )
 
 atlas_add_component( IOVSvc
                      src/components/*.cxx
                      INCLUDE_DIRS ${Boost_INCLUDE_DIRS}
-                     LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools StoreGateLib SGtests GaudiKernel TestTools EventInfo IOVSvcLib xAODEventInfo )
+                     LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools StoreGateLib SGtests GaudiKernel TestTools EventInfo IOVSvcLib xAODEventInfo PersistentDataModel )
 
 atlas_add_test( IOVSvcTool_test
                 SOURCES
                 test/IOVSvcTool_test.cxx
                 INCLUDE_DIRS ${Boost_INCLUDE_DIRS}
-                LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools StoreGateLib SGtests GaudiKernel TestTools EventInfo IOVSvcLib xAODEventInfo
+                LINK_LIBRARIES ${Boost_LIBRARIES} AthenaBaseComps AthenaKernel SGTools StoreGateLib SGtests GaudiKernel TestTools EventInfo IOVSvcLib xAODEventInfo PersistentDataModel
                 EXTRA_PATTERNS "^HistogramPersis.* INFO|^IOVSvc +DEBUG|^IOVSvcTool +DEBUG" 
                 ENVIRONMENT "JOBOPTSEARCHPATH=${CMAKE_CURRENT_SOURCE_DIR}/share" )
 
diff --git a/Control/IOVSvc/src/CondInputLoader.cxx b/Control/IOVSvc/src/CondInputLoader.cxx
index 2f9f5e92842..756c5826b28 100644
--- a/Control/IOVSvc/src/CondInputLoader.cxx
+++ b/Control/IOVSvc/src/CondInputLoader.cxx
@@ -12,6 +12,7 @@
 
 // FrameWork includes
 #include "GaudiKernel/Property.h"
+#include "StoreGate/ReadHandle.h"
 #include "AthenaKernel/errorcheck.h"
 #include "AthenaKernel/IOVTime.h"
 #include "AthenaKernel/IOVRange.h"
@@ -40,7 +41,7 @@ CondInputLoader::CondInputLoader( const std::string& name,
   declareProperty( "Load", m_load); 
   //->declareUpdateHandler(&CondInputLoader::loader, this);
   declareProperty( "ShowEventDump", m_dump=false);
-
+  declareProperty( "InputKey", m_inputKey="Input" );
 }
 
 // Destructor
@@ -82,6 +83,8 @@ CondInputLoader::initialize()
     return StatusCode::FAILURE;
   }
 
+  ATH_CHECK( m_inputKey.initialize() );
+
   std::vector<std::string> keys = idb->getKeyList();
   std::string folderName, tg;
   IOVRange range;
@@ -188,7 +191,7 @@ CondInputLoader::execute()
       if ( ! m_condStore->contains<CondContBase>( itr->key() ) ){
         ATH_MSG_INFO("ConditionStore does not contain a CondCont<> of "
                      << *itr
-                     << ". Either a ReadCondHandle was not initailized or "
+                     << ". Either a ReadCondHandle was not initialized or "
                      << "no other Algorithm is using this Handle");
         itr = m_load.erase(itr);
       } else {
@@ -225,6 +228,17 @@ CondInputLoader::execute()
     now.set_time_stamp(getContext()->eventID().time_stamp());
 #endif
   }
+
+  // For a MC event, the run number we need to use to look up the conditions
+  // may be different from that of the event itself.  If we have
+  // a ConditionsRun attribute defined, use that to override
+  // the event number.
+  SG::ReadHandle<AthenaAttributeList> input (m_inputKey, getContext());
+  if (input.isValid()) {
+    if (input->exists ("ConditionsRun"))
+      now.set_run_number ((*input)["ConditionsRun"].data<unsigned int>());
+  }
+
   IOVTime t(now.run_number(), now.event_number(), now.time_stamp());
 
   EventIDRange r;
diff --git a/Control/IOVSvc/src/CondInputLoader.h b/Control/IOVSvc/src/CondInputLoader.h
index ea584ff862a..b66b57a482f 100644
--- a/Control/IOVSvc/src/CondInputLoader.h
+++ b/Control/IOVSvc/src/CondInputLoader.h
@@ -20,7 +20,9 @@
 
 #include "AthenaKernel/IIOVSvc.h"
 #include "StoreGate/StoreGateSvc.h"
+#include "StoreGate/ReadHandleKey.h"
 #include "AthenaBaseComps/AthAlgorithm.h"
+#include "PersistentDataModel/AthenaAttributeList.h"
 
 #include <string>
 #include <map>
@@ -80,8 +82,7 @@ class CondInputLoader
   ServiceHandle<IIOVSvc> m_IOVSvc;
   
   std::map<std::string,std::string> m_keyFolderMap;
-  
-
+  SG::ReadHandleKey<AthenaAttributeList> m_inputKey;
 }; 
 
 
-- 
GitLab