ATLAS Offline Software
Loading...
Searching...
No Matches
initMcEventCollection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
5
6// HepMC includes
10
11// CLHEP includes
12#include "CLHEP/Units/SystemOfUnits.h"
13
19#include "GaudiKernel/ThreadLocalContext.h"
20
21#include "TestTools/initGaudi.h"
22
23namespace Athena_test {
24 bool initMcEventCollection(ISvcLocator*& pSvcLoc, std::vector<HepMC::GenParticlePtr>& genPartList, const bool initGaudi)
25 {
26 if (initGaudi && !Athena_test::initGaudi(pSvcLoc)) {
27 std::cerr << "This test can not be run" << std::endl;
28 return false;
29 }
30 // create dummy input McEventCollection with a name that
31 // HepMcParticleLink knows about
32 SG::WriteHandle<McEventCollection> inputTestDataHandle{"TruthEvent"};
33 inputTestDataHandle = std::make_unique<McEventCollection>();
34
35 // create a dummy EventContext
36 EventContext ctx;
37 ctx.setExtension( Atlas::ExtendedEventContext( SG::CurrentEventStore::store() ) );
38 Gaudi::Hive::setCurrentContext( ctx );
39
40 // Add a dummy GenEvent
41 const int process_id1(20);
42 const int event_number1(17);
43 inputTestDataHandle->push_back(HepMC::newGenEvent(process_id1, event_number1));
44 HepMC::GenEvent& ge1 = *(inputTestDataHandle->at(0));
45 populateGenEvent(ge1,-11,11,genPartList);
46 populateGenEvent(ge1,-13,13,genPartList);
47 populateGenEvent(ge1,-11,11,genPartList);
48 populateGenEvent(ge1,-13,13,genPartList);
49 populateGenEvent(ge1,-11,11,genPartList);
50 populateGenEvent(ge1,22,22,genPartList);
51 inputTestDataHandle->push_back(new HepMC::GenEvent(ge1));
52 HepMC::GenEvent& ge2 = *(inputTestDataHandle->at(1));
53 const int event_number2(89);
54 ge2.set_event_number(event_number2);
55 populateFilteredGenEvent(ge2,genPartList);
56 return true;
57 }
58
59 int maximumBarcode(std::vector<HepMC::GenParticlePtr>& genPartList)
60 {
61 int maxBarcode{0};
62 if (genPartList.empty()) { return maxBarcode; }
63 for (HepMC::GenParticlePtr genPart : genPartList)
64 {
65 maxBarcode = std::max(maxBarcode, HepMC::barcode(genPart));
66 }
67 return maxBarcode;
68 }
69
70 void populateGenEvent(HepMC::GenEvent & ge, int pdgid1, int pdgid2, std::vector<HepMC::GenParticlePtr>& genPartList)
71 {
72 int maxBarcode = maximumBarcode(genPartList);
73 HepMC::FourVector myPos( 0.0, 0.0, 0.0, 0.0);
74 HepMC::GenVertexPtr myVertex = HepMC::newGenVertexPtr( myPos, -1 );
75 HepMC::FourVector fourMomentum1( 0.0, 0.0, 1.0, 1.0*CLHEP::TeV);
76 HepMC::GenParticlePtr inParticle1 = HepMC::newGenParticlePtr(fourMomentum1, pdgid1, 2);
77 myVertex->add_particle_in(inParticle1);
78 HepMC::FourVector fourMomentum2( 0.0, 0.0, -1.0, 1.0*CLHEP::TeV);
79 HepMC::GenParticlePtr inParticle2 = HepMC::newGenParticlePtr(fourMomentum2, pdgid2, 2);
80 myVertex->add_particle_in(inParticle2);
81 HepMC::FourVector fourMomentum3( 0.0, 1.0, 0.0, 1.0*CLHEP::TeV);
82 HepMC::GenParticlePtr inParticle3 = HepMC::newGenParticlePtr(fourMomentum3, pdgid1, 1);
83 myVertex->add_particle_out(inParticle3);
84 genPartList.push_back(inParticle3);
85 HepMC::FourVector fourMomentum4( 0.0, -1.0, 0.0, 1.0*CLHEP::TeV);
86 HepMC::GenParticlePtr inParticle4 = HepMC::newGenParticlePtr(fourMomentum4, pdgid2, 1);
87 myVertex->add_particle_out(inParticle4);
88 genPartList.push_back(inParticle4);
89 ge.add_vertex( myVertex );
90 HepMC::suggest_barcode(inParticle1,maxBarcode+1);
91 HepMC::suggest_barcode(inParticle2,maxBarcode+2);
92 HepMC::suggest_barcode(inParticle3,maxBarcode+3);
93 HepMC::suggest_barcode(inParticle4,maxBarcode+4);
95 ge.set_beam_particles(std::move(inParticle1),std::move(inParticle2));
96 }
97
98 void populateFilteredGenEvent(HepMC::GenEvent & ge, std::vector<HepMC::GenParticlePtr>& genPartVector)
99 {
100 //.......Create new particle (geantino) to link hits from pileup
102 genPart->set_pdg_id(999); //Geantino
103 genPart->set_status(1);
105
107 genVertex->add_particle_out(genPart);
108 genPartVector.push_back(genPart);
109
110 //to set geantino vertex as a truth primary vertex
112 if (hScatVx!=nullptr) {
113 HepMC::FourVector pmvxpos=hScatVx->position();
114 genVertex->set_position(pmvxpos);
115 //to set geantino kinematic phi=eta=0, E=p=E_hard_scat
116 auto itrp =hScatVx->particles_in().cbegin();
117 if (hScatVx->particles_in().size()==2) {
118 HepMC::FourVector mom1=(*itrp)->momentum();
119 HepMC::FourVector mom2=(*(++itrp))->momentum();
120 HepMC::FourVector vxmom;
121 vxmom.setPx(mom1.e()+mom2.e());
122 vxmom.setPy(0.);
123 vxmom.setPz(0.);
124 vxmom.setE(mom1.e()+mom2.e());
125 genPart->set_momentum(vxmom);
126 }
127 }
128
129 if (!ge.vertices().empty()) {
130 std::vector<HepMC::GenVertexPtr> vtxvec;
131 for (const auto& vtx: ge.vertices()) {
132 vtxvec.push_back(vtx);
133 ge.remove_vertex(vtx);
134 }
135 vtxvec.clear();
136 }
137
138 //.....add new vertex with geantino
139 ge.add_vertex(std::move(genVertex));
141 }
142}
Handle class for recording to StoreGate.
minimal gaudi initialization for AthenaServices unit testing
minimal gaudi initialization and record an McEventCollection in StoreGate
functions & macros to test the difference between floats
int maximumBarcode(std::vector< HepMC::GenParticlePtr > &genPartList)
bool initMcEventCollection(ISvcLocator *&pSvcLoc, std::vector< HepMC::GenParticlePtr > &genPartVector, const bool initGaudi=true)
bool initGaudi(ISvcLocator *&pSvcLoc)
Minimal Gaudi initialization for unit testing without job options.
Definition initGaudi.cxx:24
void populateGenEvent(HepMC::GenEvent &ge, int pdgid1, int pdgid2, std::vector< HepMC::GenParticlePtr > &genPartVector)
void populateFilteredGenEvent(HepMC::GenEvent &ge, std::vector< HepMC::GenParticlePtr > &genPartVector)
ConstGenVertexPtr barcode_to_vertex(const GenEvent *e, int id)
Definition GenEvent.h:403
int barcode(const T *p)
Definition Barcode.h:15
HepMC3::FourVector FourVector
void set_signal_process_vertex(GenEvent *e, T &v)
Definition GenEvent.h:591
constexpr int SUPPRESSED_PILEUP_BARCODE(std::numeric_limits< int32_t >::max())
This barcode is used by objects matched to particles from pile-up interactions in standard MC Product...
GenParticlePtr newGenParticlePtr(const HepMC3::FourVector &mom=HepMC3::FourVector::ZERO_VECTOR(), int pid=0, int status=0)
Definition GenParticle.h:21
bool suggest_barcode(T &p, int i)
Definition GenEvent.h:607
HepMC3::GenParticlePtr GenParticlePtr
Definition GenParticle.h:19
GenVertexPtr newGenVertexPtr(const HepMC3::FourVector &pos=HepMC3::FourVector::ZERO_VECTOR(), const int i=0)
Definition GenVertex.h:25
HepMC3::GenVertexPtr GenVertexPtr
Definition GenVertex.h:23
GenEvent * newGenEvent(const int signal_process_id, const int event_number)
Definition GenEvent.h:360
HepMC3::GenEvent GenEvent
Definition GenEvent.h:39