ATLAS Offline Software
TruthMetaDataWriter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 // TruthMetaDataWriter.cxx
7 // Author: James Catmore (James.Catmore@cern.ch)
8 
9 // Header for this class
10 #include "TruthMetaDataWriter.h"
11 
12 // EDM Objects that we need
15 
16 #include "StoreGate/ReadHandle.h"
17 
18 // For accessing the tagInfo
20 
21 // Service for the weights
23 
24 // Constructor
26  const std::string& n,
27  const IInterface* p)
28  : base_class(t,n,p)
29  , m_metaStore( "MetaDataStore", n )
30  , m_weightSvc( "HepMCWeightSvc/HepMCWeightSvc" , n )
31 {
32  declareProperty( "MetaObjectName", m_metaName = "TruthMetaData" );
33  declareProperty( "MetaDataStore", m_metaStore );
34 }
35 
36 // Destructor
38 }
39 
40 // Athena initialize and finalize
42 {
43  ATH_MSG_VERBOSE("initialize() ...");
44 
45  ATH_CHECK( m_eventInfoKey.initialize() );
46 
47  // Initialize the service handles
48  CHECK( m_metaStore.retrieve() );
49  CHECK( m_weightSvc.retrieve() );
50  CHECK( m_tagInfoMgr.retrieve() );
51 
52  // Create an empty truth meta data container:
54  m_tmd = new xAOD::TruthMetaDataContainer();
55  m_tmd->setStore( aux );
56  // Record it in the metadata store
57  CHECK( m_metaStore->record( aux, m_metaName + "Aux." ) );
58  CHECK( m_metaStore->record( m_tmd, m_metaName ) );
59 
60  return StatusCode::SUCCESS;
61 }
62 
63 // Selection and collection creation
65 {
66  const EventContext& ctx = Gaudi::Hive::currentContext();
67 
68  //The mcChannelNumber is used as a unique identifier for which truth meta data belongs to
70  // If this fails, we are running on a datatype with no EventInfo. Such data types should
71  // definitely not be mixing MC samples, so this should be safe (will fall back to 0 above)
72  SG::ReadHandle<xAOD::EventInfo> eventInfo(m_eventInfoKey, ctx);
73  if (eventInfo.isPresent()) {
74  mcChannelNumber = eventInfo->mcChannelNumber();
75  }
76 
77  //Inserting in a (unordered_)set returns an <iterator, boolean> pair, where the boolean
78  //is used to check if the key already exists (returns false in the case it exists)
79  if( m_existingMetaDataChan.insert(mcChannelNumber).second ) {
81  m_tmd->push_back( md );
82 
83  // Get the list of weights from the metadata
84  std::map<std::string,std::size_t> weight_name_map = m_weightSvc->weightNames();
85 
86  std::vector<std::string> orderedWeightNameVec;
87  orderedWeightNameVec.reserve( weight_name_map.size() );
88  for (auto& entry: weight_name_map) {
89  orderedWeightNameVec.push_back(entry.first);
90  }
91 
92  //The map from the HepMC record pairs the weight names with a corresponding index,
93  //it is not guaranteed that the indices are ascending when iterating over the map
94  std::sort(orderedWeightNameVec.begin(), orderedWeightNameVec.end(),
95  [&](const std::string& i, const std::string& j){return weight_name_map.at(i) < weight_name_map.at(j);});
96 
98  md->setWeightNames( orderedWeightNameVec );
99 
101  md->setLhefGenerator( m_tagInfoMgr->findTag("lhefGenerator") );
102  md->setGenerators( m_tagInfoMgr->findTag("generators") );
103  md->setEvgenProcess( m_tagInfoMgr->findTag("evgenProcess") );
104  md->setEvgenTune( m_tagInfoMgr->findTag("evgenTune") );
105  md->setHardPDF( m_tagInfoMgr->findTag("hardPDF") );
106  md->setSoftPDF( m_tagInfoMgr->findTag("softPDF") );
107  // Done getting things from the TagInfo
108 
109  } // Done making the new truth metadata object
110  return StatusCode::SUCCESS;
111 }
DerivationFramework::TruthMetaDataWriter::m_metaName
std::string m_metaName
SG key and name for meta data.
Definition: TruthMetaDataWriter.h:48
IHepMCWeightSvc.h
xAOD::TruthMetaDataAuxContainer_v1
Auxiliary store for the truth meta data.
Definition: TruthMetaDataAuxContainer_v1.h:30
CondAttrListCollection.h
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
xAOD::uint32_t
setEventNumber uint32_t
Definition: EventInfo_v1.cxx:127
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:67
xAOD::TruthMetaDataContainer
TruthMetaDataContainer_v1 TruthMetaDataContainer
Declare the latest version of the truth vertex container.
Definition: TruthMetaDataContainer.h:17
xAOD::TruthMetaData_v1::setSoftPDF
void setSoftPDF(const std::string &value)
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
xAOD::mcChannelNumber
mcChannelNumber
Definition: EventInfo_v1.cxx:197
DerivationFramework::TruthMetaDataWriter::m_metaStore
ServiceHandle< StoreGateSvc > m_metaStore
Connection to the metadata store.
Definition: TruthMetaDataWriter.h:42
TruthMetaData.h
xAOD::TruthMetaData
TruthMetaData_v1 TruthMetaData
Typedef to implementation.
Definition: TruthMetaData.h:15
DerivationFramework::TruthMetaDataWriter::~TruthMetaDataWriter
~TruthMetaDataWriter()
Definition: TruthMetaDataWriter.cxx:37
xAOD::TruthMetaData_v1::setWeightNames
void setWeightNames(const std::vector< std::string > &value)
xAOD::EventInfo_v1::mcChannelNumber
uint32_t mcChannelNumber() const
The MC generator's channel number.
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:209
TruthMetaDataWriter.h
lumiFormat.i
int i
Definition: lumiFormat.py:85
xAOD::TruthMetaData_v1::setMcChannelNumber
void setMcChannelNumber(uint32_t value)
beamspotman.n
n
Definition: beamspotman.py:727
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
TruthMetaDataAuxContainer.h
DerivationFramework::TruthMetaDataWriter::TruthMetaDataWriter
TruthMetaDataWriter(const std::string &t, const std::string &n, const IInterface *p)
Definition: TruthMetaDataWriter.cxx:25
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DerivationFramework::TruthMetaDataWriter::initialize
virtual StatusCode initialize() override
Definition: TruthMetaDataWriter.cxx:41
xAOD::TruthMetaData_v1::setGenerators
void setGenerators(const std::string &value)
GetAllXsec.entry
list entry
Definition: GetAllXsec.py:132
xAOD::TruthMetaData_v1
Class describing meta data for truth records.
Definition: TruthMetaData_v1.h:28
xAOD::TruthMetaDataAuxContainer
TruthMetaDataAuxContainer_v1 TruthMetaDataAuxContainer
Declare the latest version of the truth vertex auxiliary container.
Definition: TruthMetaDataAuxContainer.h:16
xAOD::TruthMetaData_v1::setHardPDF
void setHardPDF(const std::string &value)
SG::VarHandleBase::isPresent
bool isPresent() const
Is the referenced object present in SG?
Definition: StoreGate/src/VarHandleBase.cxx:400
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::TruthMetaData_v1::setLhefGenerator
void setLhefGenerator(const std::string &value)
xAOD::TruthMetaData_v1::setEvgenTune
void setEvgenTune(const std::string &value)
xAOD::TruthMetaData_v1::setEvgenProcess
void setEvgenProcess(const std::string &value)
DerivationFramework::TruthMetaDataWriter::addBranches
virtual StatusCode addBranches() const override
Definition: TruthMetaDataWriter.cxx:64