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 // Add a dummy GenEvent
36 const int process_id1(20);
37 const int event_number1(17);
38 inputTestDataHandle->push_back(HepMC::newGenEvent(process_id1, event_number1));
39 HepMC::GenEvent& ge1 = *(inputTestDataHandle->at(0));
40 populateGenEvent(ge1,-11,11,genPartList);
41 populateGenEvent(ge1,-13,13,genPartList);
42 populateGenEvent(ge1,-11,11,genPartList);
43 populateGenEvent(ge1,-13,13,genPartList);
44 populateGenEvent(ge1,-11,11,genPartList);
45 populateGenEvent(ge1,22,22,genPartList);
46 inputTestDataHandle->push_back(new HepMC::GenEvent(ge1));
47 HepMC::GenEvent& ge2 = *(inputTestDataHandle->at(1));
48 const int event_number2(89);
49 ge2.set_event_number(event_number2);
50 populateFilteredGenEvent(ge2,genPartList);
51 return true;
52 }
53
54 int maximumBarcode(std::vector<HepMC::GenParticlePtr>& genPartList)
55 {
56 int maxBarcode{0};
57 if (genPartList.empty()) { return maxBarcode; }
58 for (HepMC::GenParticlePtr genPart : genPartList)
59 {
60 maxBarcode = std::max(maxBarcode, HepMC::barcode(genPart));
61 }
62 return maxBarcode;
63 }
64
65 void populateGenEvent(HepMC::GenEvent & ge, int pdgid1, int pdgid2, std::vector<HepMC::GenParticlePtr>& genPartList)
66 {
67 int maxBarcode = maximumBarcode(genPartList);
68 HepMC::FourVector myPos( 0.0, 0.0, 0.0, 0.0);
69 HepMC::GenVertexPtr myVertex = HepMC::newGenVertexPtr( myPos, -1 );
70 HepMC::FourVector fourMomentum1( 0.0, 0.0, 1.0, 1.0*CLHEP::TeV);
71 HepMC::GenParticlePtr inParticle1 = HepMC::newGenParticlePtr(fourMomentum1, pdgid1, 2);
72 myVertex->add_particle_in(inParticle1);
73 HepMC::FourVector fourMomentum2( 0.0, 0.0, -1.0, 1.0*CLHEP::TeV);
74 HepMC::GenParticlePtr inParticle2 = HepMC::newGenParticlePtr(fourMomentum2, pdgid2, 2);
75 myVertex->add_particle_in(inParticle2);
76 HepMC::FourVector fourMomentum3( 0.0, 1.0, 0.0, 1.0*CLHEP::TeV);
77 HepMC::GenParticlePtr inParticle3 = HepMC::newGenParticlePtr(fourMomentum3, pdgid1, 1);
78 myVertex->add_particle_out(inParticle3);
79 genPartList.push_back(inParticle3);
80 HepMC::FourVector fourMomentum4( 0.0, -1.0, 0.0, 1.0*CLHEP::TeV);
81 HepMC::GenParticlePtr inParticle4 = HepMC::newGenParticlePtr(fourMomentum4, pdgid2, 1);
82 myVertex->add_particle_out(inParticle4);
83 genPartList.push_back(inParticle4);
84 ge.add_vertex( myVertex );
85 HepMC::suggest_barcode(inParticle1,maxBarcode+1);
86 HepMC::suggest_barcode(inParticle2,maxBarcode+2);
87 HepMC::suggest_barcode(inParticle3,maxBarcode+3);
88 HepMC::suggest_barcode(inParticle4,maxBarcode+4);
90 ge.set_beam_particles(std::move(inParticle1),std::move(inParticle2));
91 }
92
93 void populateFilteredGenEvent(HepMC::GenEvent & ge, std::vector<HepMC::GenParticlePtr>& genPartVector)
94 {
95 //.......Create new particle (geantino) to link hits from pileup
97 genPart->set_pdg_id(999); //Geantino
98 genPart->set_status(1);
100
102 genVertex->add_particle_out(genPart);
103 genPartVector.push_back(genPart);
104
105 //to set geantino vertex as a truth primary vertex
107 if (hScatVx!=nullptr) {
108 HepMC::FourVector pmvxpos=hScatVx->position();
109 genVertex->set_position(pmvxpos);
110 //to set geantino kinematic phi=eta=0, E=p=E_hard_scat
111 auto itrp =hScatVx->particles_in().cbegin();
112 if (hScatVx->particles_in().size()==2) {
113 HepMC::FourVector mom1=(*itrp)->momentum();
114 HepMC::FourVector mom2=(*(++itrp))->momentum();
115 HepMC::FourVector vxmom;
116 vxmom.setPx(mom1.e()+mom2.e());
117 vxmom.setPy(0.);
118 vxmom.setPz(0.);
119 vxmom.setE(mom1.e()+mom2.e());
120 genPart->set_momentum(vxmom);
121 }
122 }
123
124 if (!ge.vertices().empty()) {
125 std::vector<HepMC::GenVertexPtr> vtxvec;
126 for (const auto& vtx: ge.vertices()) {
127 vtxvec.push_back(vtx);
128 ge.remove_vertex(vtx);
129 }
130 vtxvec.clear();
131 }
132
133 //.....add new vertex with geantino
134 ge.add_vertex(std::move(genVertex));
136 }
137}
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:25
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