diff --git a/Event/xAOD/xAODTruthCnv/CMakeLists.txt b/Event/xAOD/xAODTruthCnv/CMakeLists.txt index c3fd8795942cc11a21da1215f9d8b655dd828b21..023f8a01d15bcc50af6ecb584a5c3804cdd0265d 100644 --- a/Event/xAOD/xAODTruthCnv/CMakeLists.txt +++ b/Event/xAOD/xAODTruthCnv/CMakeLists.txt @@ -5,30 +5,6 @@ # Declare the package name: atlas_subdir( xAODTruthCnv ) -# Extra package dependencies, based on the build environment: -set( extra_dep ) -if( NOT XAOD_STANDALONE ) - set( extra_dep Control/AthenaKernel - PRIVATE - Control/AthLinks - Control/AthenaBaseComps - Control/StoreGate - Event/EventInfo - Event/xAOD/xAODEventInfo - Event/xAOD/xAODTruth - GaudiKernel - Generators/GeneratorObjects ) -endif() - -# Declare the package's dependencies: -atlas_depends_on_subdirs( - PUBLIC - Control/CxxUtils - Control/AthToolSupport/AsgTools - Event/xAOD/xAODTruth - Generators/AtlasHepMC - ${extra_dep} ) - # Component(s) in the package: atlas_add_library( xAODTruthCnvLib xAODTruthCnv/*.h Root/*.cxx @@ -39,7 +15,7 @@ if( NOT XAOD_STANDALONE ) atlas_add_component( xAODTruthCnv src/*.h src/*.cxx src/components/*.cxx LINK_LIBRARIES AthLinks AthenaBaseComps StoreGateLib EventInfo - xAODEventInfo xAODTruth GaudiKernel GeneratorObjects + xAODEventInfo xAODTruth GaudiKernel GeneratorObjects AthenaPoolUtilities IOVDbDataModel xAODTruthCnvLib ) endif() diff --git a/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.cxx b/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.cxx index 8cf4b77177b417d7f6118a0e28f76ff6d588b13d..5a8b51e03aec14d94ba0e0286795084f16554914 100644 --- a/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.cxx +++ b/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.cxx @@ -1,5 +1,5 @@ /* - Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration + Copyright (C) 2002-2020 CERN for the benefit of the ATLAS collaboration */ #include "AthenaKernel/errorcheck.h" @@ -11,8 +11,6 @@ #include "StoreGate/ReadHandle.h" #include "StoreGate/WriteHandle.h" -#include "EventInfo/EventStreamInfo.h" - #include "xAODTruth/TruthEvent.h" #include "xAODTruth/TruthEventContainer.h" #include "xAODTruth/TruthEventAuxContainer.h" @@ -32,6 +30,9 @@ #include "xAODTruth/TruthMetaDataAuxContainer.h" #include "xAODTruth/TruthMetaData.h" +#include "AthenaPoolUtilities/CondAttrListCollection.h" +#include "IOVDbDataModel/IOVMetaDataContainer.h" + #include "xAODTruthCnvAlg.h" @@ -41,7 +42,9 @@ namespace xAODMaker { xAODTruthCnvAlg::xAODTruthCnvAlg( const string& name, ISvcLocator* svcLoc ) - : AthReentrantAlgorithm( name, svcLoc ), m_metaStore( "MetaDataStore", name ), m_inputMetaStore( "StoreGateSvc/InputMetaDataStore",name) + : AthReentrantAlgorithm( name, svcLoc ) + , m_metaStore( "MetaDataStore", name ) + , m_firstBeginRun(true) { // leaving metadata alone for now--to be updated declareProperty( "MetaObjectName", m_metaName = "TruthMetaData" ); @@ -69,6 +72,11 @@ namespace xAODMaker { ATH_CHECK(m_xaodTruthParticleContainerKey.initialize()); ATH_CHECK(m_xaodTruthVertexContainerKey.initialize()); + ATH_CHECK(m_evtInfo.initialize()); + + ServiceHandle<IIncidentSvc> incSvc("IncidentSvc", name()); + ATH_CHECK(incSvc.retrieve()); + incSvc->addListener( this, "BeginRun", 10); ATH_MSG_DEBUG("Initializing; package version: " << PACKAGE_VERSION ); ATH_MSG_DEBUG("AODContainerName = " << m_aodContainerKey.key() ); @@ -196,17 +204,18 @@ namespace xAODMaker { if (m_writeMetaData) { //The mcChannelNumber is used as a unique identifier for which truth meta data belongs to - const EventStreamInfo* esi = nullptr; - std::vector<std::string> keys; - m_inputMetaStore->keys<EventStreamInfo>(keys); - if (keys.size() > 1) { // Multiple EventStreamInfo (default retrieve won't work), just take the first - CHECK( m_inputMetaStore->retrieve(esi, keys[0])); - } else { - CHECK( m_inputMetaStore->retrieve(esi)); + uint32_t mcChannelNumber = 0; + SG::ReadHandle<xAOD::EventInfo> evtInfo (m_evtInfo,ctx); + if (evtInfo.isValid()) { + mcChannelNumber = evtInfo->mcChannelNumber(); + if (mcChannelNumber==0) mcChannelNumber = evtInfo->runNumber(); } - uint32_t mcChannelNumber = esi->getEventTypes().begin()->mc_channel_number(); - - ATH_CHECK( m_meta.maybeWrite (mcChannelNumber, *genEvt) ); + else { + ATH_MSG_FATAL("Faied to retrieve EventInfo"); + return StatusCode::FAILURE; + } + + ATH_CHECK( m_meta.maybeWrite (mcChannelNumber, *genEvt, m_metaFields) ); } // Event weights vector<float> weights; @@ -400,7 +409,55 @@ namespace xAODMaker { return StatusCode::SUCCESS; } - + void xAODTruthCnvAlg::handle(const Incident& incident) { + if (m_firstBeginRun && incident.type()==IncidentType::BeginRun) { + m_firstBeginRun = false; + ServiceHandle<StoreGateSvc> inputStore("StoreGateSvc/InputMetaDataStore", name()); + if(inputStore.retrieve().isFailure()) { + ATH_MSG_ERROR("Failed to retrieve Input Metadata Store"); + return; + } + const IOVMetaDataContainer* tagInfo{nullptr}; + if(inputStore->retrieve(tagInfo,"/TagInfo").isFailure()) { + ATH_MSG_WARNING("Failed to retrieve /TagInfo metadata from the input store"); + return; + } + if(tagInfo->payloadContainer()->size()>0) { + CondAttrListCollection* tagInfoPayload = tagInfo->payloadContainer()->at(0); + if(tagInfoPayload->size()>0) { + const CondAttrListCollection::AttributeList& al = tagInfoPayload->attributeList(0); + if (al.exists("lhefGenerator")){ + m_metaFields.lhefGenerator = al["lhefGenerator"].data<std::string>(); + } + + if (al.exists("generators")){ + m_metaFields.generators = al["generators"].data<std::string>(); + } + + if (al.exists("evgenProcess")){ + m_metaFields.evgenProcess = al["evgenProcess"].data<std::string>(); + } + + if (al.exists("evgenTune")){ + m_metaFields.evgenTune = al["evgenTune"].data<std::string>(); + } + + if (al.exists("hardPDF")){ + m_metaFields.hardPDF = al["hardPDF"].data<std::string>(); + } + + if (al.exists("softPDF")){ + m_metaFields.softPDF = al["softPDF"].data<std::string>(); + } + } + } + else { + ATH_MSG_WARNING("Empty Tag Info metadata!"); + } + } + } + + // A helper to set up a TruthVertex (without filling the ELs) void xAODTruthCnvAlg::fillVertex(xAOD::TruthVertex* tv, HepMC::ConstGenVertexPtr gv) { tv->setId(gv->id()); @@ -459,7 +516,8 @@ namespace xAODMaker { StatusCode xAODTruthCnvAlg::MetaDataWriter::maybeWrite (uint32_t mcChannelNumber, - const HepMC::GenEvent& genEvt) + const HepMC::GenEvent& genEvt, + const MetadataFields& metaFields) { // This bit needs to be serialized. lock_t lock (m_mutex); @@ -494,6 +552,25 @@ namespace xAODMaker { md->setMcChannelNumber(mcChannelNumber); md->setWeightNames( std::move(orderedWeightNameVec) ); #endif + + if(!metaFields.lhefGenerator.empty()) { + md->setLhefGenerator(metaFields.lhefGenerator); + } + if(!metaFields.generators.empty()) { + md->setGenerators(metaFields.generators); + } + if(!metaFields.evgenProcess.empty()) { + md->setEvgenProcess(metaFields.evgenProcess); + } + if(!metaFields.evgenTune.empty()) { + md->setEvgenTune(metaFields.evgenTune); + } + if(!metaFields.hardPDF.empty()) { + md->setHardPDF(metaFields.hardPDF); + } + if(!metaFields.softPDF.empty()) { + md->setSoftPDF(metaFields.softPDF); + } } return StatusCode::SUCCESS; diff --git a/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.h b/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.h index eff80d6621f0cc775fb49bc65ca90ce3e382d59b..8c1808e14e0f0fbcc1ca2f487606ab0149eef731 100644 --- a/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.h +++ b/Event/xAOD/xAODTruthCnv/src/xAODTruthCnvAlg.h @@ -39,6 +39,7 @@ #include "StoreGate/ReadHandleKey.h" #include "StoreGate/WriteHandleKey.h" #include "CxxUtils/checker_macros.h" +#include "GaudiKernel/IIncidentListener.h" #include <unordered_set> @@ -48,6 +49,7 @@ #include "AtlasHepMC/GenVertex_fwd.h" #include "AtlasHepMC/GenParticle_fwd.h" +#include "xAODEventInfo/EventInfo.h" namespace xAODMaker { @@ -59,7 +61,10 @@ namespace xAODMaker { /// @author James Catmore <James.Catmore@cern.ch> /// @author Jovan Mitreski <Jovan.Mitreski@cern.ch> /// @author Andy Buckley <Andy.Buckley@cern.ch> - class xAODTruthCnvAlg : public AthReentrantAlgorithm { + class xAODTruthCnvAlg + : public AthReentrantAlgorithm + , virtual public IIncidentListener +{ public: /// Regular algorithm constructor @@ -70,8 +75,20 @@ namespace xAODMaker { /// Function executing the algorithm virtual StatusCode execute (const EventContext& ctx) const override; + /// Incident handler + virtual void handle(const Incident& incident) override; private: + // Truth metadata fields retrieved from TagInfo + struct MetadataFields { + std::string lhefGenerator; + std::string generators; + std::string evgenProcess; + std::string evgenTune; + std::string hardPDF; + std::string softPDF; + }; + /// Factor out the pieces dealing with writing to meta data. /// This will be non-const, so need to protect with a mutex. class MetaDataWriter @@ -80,8 +97,8 @@ namespace xAODMaker { StatusCode initialize (ServiceHandle<StoreGateSvc>& metaStore, const std::string& metaName); StatusCode maybeWrite (uint32_t mcChannelNumber, - const HepMC::GenEvent& genEvt); - + const HepMC::GenEvent& genEvt, + const MetadataFields& metaFields); private: /// Mutex to control access to meta data writing. @@ -136,13 +153,18 @@ namespace xAODMaker { /// Connection to the metadata store ServiceHandle< StoreGateSvc > m_metaStore; - ServiceHandle<StoreGateSvc> m_inputMetaStore; /// SG key and name for meta data std::string m_metaName; /// option to disable writing of metadata (e.g. if running a filter on xAOD in generators) Gaudi::Property<bool> m_writeMetaData{this, "WriteTruthMetaData", true}; + /// Event Info + SG::ReadHandleKey<xAOD::EventInfo> m_evtInfo {this, "EventInfo", "EventInfo", "" }; + + /// Tag Info + bool m_firstBeginRun; + MetadataFields m_metaFields; }; // class xAODTruthCnvAlg