ATLAS Offline Software
TruthMetaDataWriter.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 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
16 
17 // For accessing the tagInfo
19 
20 // Service for the weights
22 
23 // Constructor
25  const std::string& n,
26  const IInterface* p)
27  : AthAlgTool(t,n,p)
28  , m_metaStore( "MetaDataStore", n )
29  , m_weightSvc( "HepMCWeightSvc/HepMCWeightSvc" , n )
30 {
31  declareInterface<DerivationFramework::IAugmentationTool>(this);
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  // Initialize the service handles
45  CHECK( m_metaStore.retrieve() );
46  CHECK( m_weightSvc.retrieve() );
47  CHECK( m_tagInfoMgr.retrieve() );
48 
49  // Create an empty truth meta data container:
51  m_tmd = new xAOD::TruthMetaDataContainer();
52  m_tmd->setStore( aux );
53  // Record it in the metadata store
54  CHECK( m_metaStore->record( aux, m_metaName + "Aux." ) );
55  CHECK( m_metaStore->record( m_tmd, m_metaName ) );
56 
57  return StatusCode::SUCCESS;
58 }
59 
60 // Selection and collection creation
62 {
63 
64  //The mcChannelNumber is used as a unique identifier for which truth meta data belongs to
66  // If this fails, we are running on a datatype with no EventInfo. Such data types should
67  // definitely not be mixing MC samples, so this should be safe (will fall back to 0 above)
68  if (evtStore()->contains<xAOD::EventInfo>("EventInfo")){
69  const xAOD::EventInfo* eventInfo = nullptr;
70  CHECK( evtStore()->retrieve(eventInfo, "EventInfo") );
71  mcChannelNumber = eventInfo->mcChannelNumber();
72  }
73 
74  //Inserting in a (unordered_)set returns an <iterator, boolean> pair, where the boolean
75  //is used to check if the key already exists (returns false in the case it exists)
76  if( m_existingMetaDataChan.insert(mcChannelNumber).second ) {
78  m_tmd->push_back( md );
79 
80  // Get the list of weights from the metadata
81  std::map<std::string,std::size_t> weight_name_map = m_weightSvc->weightNames();
82 
83  std::vector<std::string> orderedWeightNameVec;
84  orderedWeightNameVec.reserve( weight_name_map.size() );
85  for (auto& entry: weight_name_map) {
86  orderedWeightNameVec.push_back(entry.first);
87  }
88 
89  //The map from the HepMC record pairs the weight names with a corresponding index,
90  //it is not guaranteed that the indices are ascending when iterating over the map
91  std::sort(orderedWeightNameVec.begin(), orderedWeightNameVec.end(),
92  [&](const std::string& i, const std::string& j){return weight_name_map.at(i) < weight_name_map.at(j);});
93 
95  md->setWeightNames( orderedWeightNameVec );
96 
98  md->setLhefGenerator( m_tagInfoMgr->findTag("lhefGenerator") );
99  md->setGenerators( m_tagInfoMgr->findTag("generators") );
100  md->setEvgenProcess( m_tagInfoMgr->findTag("evgenProcess") );
101  md->setEvgenTune( m_tagInfoMgr->findTag("evgenTune") );
102  md->setHardPDF( m_tagInfoMgr->findTag("hardPDF") );
103  md->setSoftPDF( m_tagInfoMgr->findTag("softPDF") );
104  // Done getting things from the TagInfo
105 
106  } // Done making the new truth metadata object
107  return StatusCode::SUCCESS;
108 }
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
DerivationFramework::TruthMetaDataWriter::m_metaName
std::string m_metaName
SG key and name for meta data.
Definition: TruthMetaDataWriter.h:46
IHepMCWeightSvc.h
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
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
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
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:40
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.
TruthMetaDataWriter.h
lumiFormat.i
int i
Definition: lumiFormat.py:92
xAOD::TruthMetaData_v1::setMcChannelNumber
void setMcChannelNumber(uint32_t value)
beamspotman.n
n
Definition: beamspotman.py:731
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:24
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)
EventInfo.h
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
xAOD::TruthMetaData_v1::setLhefGenerator
void setLhefGenerator(const std::string &value)
AthAlgTool
Definition: AthAlgTool.h:26
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
Pass the thinning service
Definition: TruthMetaDataWriter.cxx:61