7#include "AthLinks/ElementLink.h"
9#include "GaudiKernel/MsgStream.h"
10#include "GaudiKernel/DataSvc.h"
11#include "GaudiKernel/PhysicalConstants.h"
45 const int eventNumber = genEvt->event_number();
46 return (pid==0 && eventNumber==-1);
67 ATH_MSG_FATAL(
"Contradictory xAOD truth pile-up setting: all pile-up AND in-time alone requested simultaneously. Check settings." );
68 return StatusCode::FAILURE;
92 incSvc->addListener(
this,
"BeginRun", 10);
107 return StatusCode::SUCCESS;
114 ATH_CHECK(truthLinkVec.
record(std::make_unique<xAODTruthParticleLinkVector>()));
121 return StatusCode::FAILURE;
131 ATH_CHECK(xTruthEventContainer.
record(std::make_unique<xAOD::TruthEventContainer>(),
132 std::make_unique<xAOD::TruthEventAuxContainer>()));
139 ATH_CHECK(xTruthPileupEventContainer.
record(std::make_unique<xAOD::TruthPileupEventContainer>(),
140 std::make_unique<xAOD::TruthPileupEventAuxContainer>()));
146 ATH_CHECK(xTruthParticleContainer.
record(std::make_unique<xAOD::TruthParticleContainer>(),
147 std::make_unique<xAOD::TruthParticleAuxContainer>()));
151 ATH_CHECK(xTruthVertexContainer.
record(std::make_unique<xAOD::TruthVertexContainer>(),
152 std::make_unique<xAOD::TruthVertexAuxContainer>()));
156 bool hadLHERecord =
false;
189 ATH_MSG_DEBUG(
"Number of GenEvents in this Athena event = " << mcColl->size());
190 bool newAttributesPresent(
false);
191 for (
unsigned int cntr = 0; cntr < mcColl->size(); ++cntr) {
193 bool isSignalProcess(
false);
195 isSignalProcess=
true;
197 if (bunchCrossingTime) {
198 newAttributesPresent =
true;
208 isSignalProcess=
false;
210 if (bunchCrossingTime) {
224 if (newAttributesPresent) {
241 if (isSignalProcess) {
242 xTruthEvent = xTruthEventContainer->push_back( std::make_unique<xAOD::TruthEvent>() );
244 auto crossSection = genEvt->cross_section();
245 xTruthEvent->
setCrossSection(crossSection ? (
float)crossSection->xsec() : -1);
250 uint32_t mcChannelNumber = 0;
253 mcChannelNumber = evtInfo->mcChannelNumber();
254 if (mcChannelNumber==0) mcChannelNumber = evtInfo->runNumber();
258 return StatusCode::FAILURE;
265 for (
const double& w : genEvt->weights())
weights.push_back((
float)(w));
271 auto const hiInfo = genEvt->heavy_ion();
293 auto const pdfInfo = genEvt->pdf_info();
314 ATH_CHECK(xTruthLHEParticleContainer.
record(std::make_unique<xAOD::TruthParticleContainer>(),
315 std::make_unique<xAOD::TruthParticleAuxContainer>()));
318 for (
int nPart=0;nPart<lhe_record_attribute->NUP;++nPart) {
322 xTruthLHEParticleContainer->push_back( xTruthParticle );
324 xTruthParticle->
setPdgId( lhe_record_attribute->IDUP[nPart] );
325 xTruthParticle->
setUid( nPart+1 );
326 xTruthParticle->
setStatus( lhe_record_attribute->ISTUP[nPart] );
327 xTruthParticle->
setPx( lhe_record_attribute->PUP[nPart][0] );
328 xTruthParticle->
setPy( lhe_record_attribute->PUP[nPart][1] );
329 xTruthParticle->
setPz( lhe_record_attribute->PUP[nPart][2] );
330 xTruthParticle->
setE( lhe_record_attribute->PUP[nPart][3] );
331 xTruthParticle->
setM( lhe_record_attribute->PUP[nPart][4] );
334 else if (hadLHERecord){
335 ATH_MSG_WARNING(
"Truth record appeared to have two LHE records; this should not be possible");
338 xTruthPileupEvent = xTruthPileupEventContainer->push_back( std::make_unique<xAOD::TruthPileupEvent>() );
346 VertexMap::iterator mapItr;
347 vector<HepMC::ConstGenVertexPtr> vertices;
352 if (disconnectedSignalProcessVtx) {
353 if (disconnectedSignalProcessVtx->particles_in_size() == 0 && disconnectedSignalProcessVtx->particles_out_size() == 0 ) {
355 vertices.push_back (std::move(disconnectedSignalProcessVtx));
358 ATH_MSG_WARNING(
"Signal process vertex pointer not valid in HepMC Collection for GenEvent #" << cntr <<
" / " << mcColl->size());
362 pair<HepMC::ConstGenParticlePtr,HepMC::ConstGenParticlePtr> beamParticles;
363 bool genEvt_valid_beam_particles=
false;
364 auto beamParticles_vec = genEvt->beams();
365 genEvt_valid_beam_particles=(beamParticles_vec.size()>1);
366 if (genEvt_valid_beam_particles){beamParticles.first=beamParticles_vec[0]; beamParticles.second=beamParticles_vec[1]; }
370 ATH_MSG_ERROR(
"TruthParticleCnvTool.cxx: Event does not contain barcodes attribute");
371 return StatusCode::FAILURE;
374 xTruthParticleContainer->reserve(bcmap.size());
375 for (
const auto &[genPartBarcode,part]: bcmap) {
380 xTruthParticleContainer->push_back( xTruthParticle );
392 if (genEvt_valid_beam_particles) {
393 if (isSignalProcess) {
399 auto productionVertex = part->production_vertex();
403 if (productionVertex && productionVertex->parent_event() !=
nullptr) {
405 if (parts.incoming.empty() && parts.outgoing.empty())
406 vertices.push_back (std::move(productionVertex));
407 parts.outgoingEL.push_back(eltp);
408 parts.outgoing.push_back(xTruthParticle);
414 auto decayVertex = part->end_vertex();
417 if (parts.incoming.empty() && parts.outgoing.empty())
418 vertices.push_back (std::move(decayVertex));
419 parts.incomingEL.push_back(eltp);
420 parts.incoming.push_back(xTruthParticle);
427 xTruthVertexContainer->reserve(vertices.size());
428 for (
const auto& vertex : vertices) {
429 const auto& parts = vertexMap[vertex];
433 xTruthVertexContainer->push_back( xTruthVertex );
455 ATH_MSG_VERBOSE(
"Summarizing truth link size: " << truthLinkVec->size() );
457 return StatusCode::SUCCESS;
464 if(inputStore.retrieve().isFailure()) {
469 if(inputStore->retrieve(tagInfo,
"/TagInfo").isFailure()) {
470 ATH_MSG_WARNING(
"Failed to retrieve /TagInfo metadata from the input store");
475 if(tagInfoPayload->
size()>0) {
477 if (
al.exists(
"lhefGenerator")){
478 m_metaFields.lhefGenerator =
al[
"lhefGenerator"].data<std::string>();
481 if (
al.exists(
"generators")){
485 if (
al.exists(
"evgenProcess")){
486 m_metaFields.evgenProcess =
al[
"evgenProcess"].data<std::string>();
489 if (
al.exists(
"evgenTune")){
493 if (
al.exists(
"hardPDF")){
497 if (
al.exists(
"softPDF")){
514 tv->
setX(gv->position().x());
515 tv->
setY(gv->position().y());
516 tv->
setZ(gv->position().z());
517 tv->
setT(gv->position().t());
528 if (pol.is_defined()) {
533 tp->
setM(gp->generated_mass());
534 tp->
setPx(gp->momentum().px());
535 tp->
setPy(gp->momentum().py());
536 tp->
setPz(gp->momentum().pz());
537 tp->
setE(gp->momentum().e());
543 const std::string& metaName)
547 auto md = std::make_unique<xAOD::TruthMetaDataContainer>();
550 auto aux = std::make_unique<xAOD::TruthMetaDataAuxContainer>();
551 md->setStore( aux.get() );
554 CHECK( metaStore->record( std::move (aux), metaName +
"Aux." ) );
555 CHECK( metaStore->record( std::move (md), metaName ) );
556 return StatusCode::SUCCESS;
571 m_tmd->push_back (std::make_unique <xAOD::TruthMetaData>());
576 std::vector<std::string> orderedWeightNameVec;
577 if (!genEvt.run_info()) {
578 for (
size_t i=0; i<genEvt.weights().
size();i++) orderedWeightNameVec.push_back(std::to_string(i));
580 if (!genEvt.run_info()->weight_names().empty()) {
581 orderedWeightNameVec = genEvt.weight_names();
601 if(!metaFields.
hardPDF.empty()) {
604 if(!metaFields.
softPDF.empty()) {
609 return StatusCode::SUCCESS;
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_VERBOSE(x)
#define ATH_MSG_WARNING(x)
This file defines the class for a collection of AttributeLists where each one is associated with a ch...
Helpers for checking error return status codes and reporting errors.
#define CHECK(...)
Evaluate an expression and check for errors.
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
Handle class for reading from StoreGate.
Handle class for recording to StoreGate.
size_t size() const
Number of registered mappings.
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
An algorithm that can be simultaneously executed in multiple threads.
This class is a collection of AttributeLists where each one is associated with a channel number.
const AttributeList & attributeList(ChanNum chanNum) const
attribute list for a given channel number
size_type size() const
number of Chan/AttributeList pairs
coral::AttributeList AttributeList
ElementLink implementation for ROOT usage.
std::map< int, ConstGenParticlePtr > barcode_to_particle_map() const
a link optimized in size for a GenParticle in a McEventCollection
size_type size() const
size of payload vector
CondAttrListCollection * at(unsigned int i) const
Element access.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
SG::WriteHandleKey< xAOD::TruthVertexContainer > m_xaodTruthVertexContainerKey
MetadataFields m_metaFields
Gaudi::Property< bool > m_doAllPileUp
Pile-up options.
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_lheTruthParticleContainerKey
SG::WriteHandleKey< xAODTruthParticleLinkVector > m_truthLinkContainerKey
SG::ReadHandleKey< McEventCollection > m_aodContainerKey
The key of the input AOD truth container.
static void fillVertex(xAOD::TruthVertex *tv, const HepMC::ConstGenVertexPtr &gv)
These functions do not set up ELs, just the other variables.
xAODTruthCnvAlg(const std::string &name, ISvcLocator *svcLoc)
Regular algorithm constructor.
SG::WriteHandleKey< xAOD::TruthParticleContainer > m_xaodTruthParticleContainerKey
Gaudi::Property< bool > m_writeMetaData
option to disable writing of metadata (e.g. if running a filter on xAOD in generators)
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm.
std::map< HepMC::ConstGenVertexPtr, VertexParticles > VertexMap
Convenience handle for a map of vtx ptrs -> connected particles.
ServiceHandle< StoreGateSvc > m_metaStore
Connection to the metadata store.
SG::ReadHandleKey< xAOD::EventInfo > m_evtInfo
Event Info.
Gaudi::Property< bool > m_doInTimePileUp
virtual void handle(const Incident &incident) override
Incident handler.
static void fillParticle(xAOD::TruthParticle *tp, const HepMC::ConstGenParticlePtr &gp)
bool m_firstBeginRun
Tag Info.
std::string m_metaName
SG key and name for meta data.
SG::WriteHandleKey< xAOD::TruthEventContainer > m_xaodTruthEventContainerKey
The key for the output xAOD truth containers.
SG::WriteHandleKey< xAOD::TruthPileupEventContainer > m_xaodTruthPUEventContainerKey
virtual StatusCode initialize() override
Function initialising the algorithm.
void addTruthVertexLink(const TruthVertexLink_t &vlink)
Add one truth vertex.
void addTruthParticleLink(const TruthParticleLink_t &plink)
Add one truth particle.
void setWeights(const std::vector< float > &weights)
Set the event weights.
void setSignalProcessVertexLink(const TruthVertexLink_t &link)
Set pointer to a vertex representing the primary beam interaction point.
void setCrossSectionError(float value)
Set the cross-section error.
void setCrossSection(float value)
Set the cross-section.
bool setHeavyIonParameter(int value, HIParam parameter)
Set an integer HI parameter.
bool setPdfInfoParameter(int value, PdfParam parameter)
Set an integer PDF info parameter.
@ NWOUNDEDNWOUNDEDCOLLISIONS
[int]
@ NNWOUNDEDCOLLISIONS
[int]
@ NWOUNDEDNCOLLISIONS
[int]
void setBeamParticle1Link(const TruthParticleLink_t &pcl1)
Set one incoming beam particle.
void setBeamParticle2Link(const TruthParticleLink_t &pcl2)
Set one incoming beam particle.
void setPy(float value)
Set the y component of the particle's momentum.
void setUid(int value)
Set unique ID.
void setM(float value)
Also store the mass.
void setE(float value)
Set the energy of the particle.
void setPz(float value)
Set the z component of the particle's momentum.
bool setPolarizationParameter(float value, PolParam parameter)
Set method for polarization parameter values.
void setStatus(int value)
Set status code.
void setPdgId(int pid)
Set PDG ID code.
void setPx(float value)
Set the x component of the particle's momentum.
@ polarizationPhi
Polarization in ( ).
@ polarizationTheta
Polarization in ( ).
void setStatus(int value)
Set the vertex status.
void setZ(float value)
Set the vertex's longitudinal distance from the origin.
void setUid(int value)
Set the vertex unique ID.
void setOutgoingParticleLinks(const TPLinks_t &links)
Set all the outgoing particles.
void setIncomingParticleLinks(const TPLinks_t &links)
Set all the incoming particles.
void setT(float value)
Set the vertex time.
void setX(float value)
Set the x displacement of the vertex.
void setY(float value)
Set the y displacement of the vertex.
const std::string BunchCrossingTime
const std::string barcodes
const std::string LHERecord
int signal_process_id(const GenEvent &evt)
ConstGenVertexPtr signal_process_vertex(const GenEvent *e)
Polarization polarization(const T &a)
HepMC3::ConstGenParticlePtr ConstGenParticlePtr
HepMC3::ConstGenVertexPtr ConstGenVertexPtr
HepMC3::GenEvent GenEvent
void stable_sort(DataModel_detail::iterator< DVL > beg, DataModel_detail::iterator< DVL > end)
Specialization of stable_sort for DataVector/List.
TruthVertex_v1 TruthVertex
Typedef to implementation.
TruthEvent_v1 TruthEvent
Typedef to implementation.
TruthParticle_v1 TruthParticle
Typedef to implementation.
TruthPileupEvent_v1 TruthPileupEvent
Typedef to implementation.
TruthMetaData_v1 TruthMetaData
Typedef to implementation.
Type for tracking particles connected to a single vertex.
bool isSeparatorGenEvent(const HepMC::GenEvent *genEvt)
std::pair< HepMcParticleLink, ElementLink< xAOD::TruthParticleContainer > > xAODTruthParticleLink