ATLAS Offline Software
initMcEventCollection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
5 
6 // HepMC includes
7 #include "AtlasHepMC/GenEvent.h"
9 #include "AtlasHepMC/GenVertex.h"
10 
11 // CLHEP includes
12 #include "CLHEP/Units/SystemOfUnits.h"
13 
15 #include "StoreGate/WriteHandle.h"
17 
18 #include "TestTools/initGaudi.h"
19 
20 namespace Athena_test {
21  bool initMcEventCollection(ISvcLocator*& pSvcLoc, std::vector<HepMC::GenParticlePtr>& genPartList, const bool initGaudi)
22  {
23  if (initGaudi && !Athena_test::initGaudi(pSvcLoc)) {
24  std::cerr << "This test can not be run" << std::endl;
25  return false;
26  }
27  // create dummy input McEventCollection with a name that
28  // HepMcParticleLink knows about
29  SG::WriteHandle<McEventCollection> inputTestDataHandle{"TruthEvent"};
30  inputTestDataHandle = std::make_unique<McEventCollection>();
31 
32  // create a dummy EventContext
33  EventContext ctx;
35  Gaudi::Hive::setCurrentContext( ctx );
36 
37  // Add a dummy GenEvent
38  const int process_id1(20);
39  const int event_number1(17);
40  inputTestDataHandle->push_back(HepMC::newGenEvent(process_id1, event_number1));
41  HepMC::GenEvent& ge1 = *(inputTestDataHandle->at(0));
42  populateGenEvent(ge1,-11,11,genPartList);
43  populateGenEvent(ge1,-13,13,genPartList);
44  populateGenEvent(ge1,-11,11,genPartList);
45  populateGenEvent(ge1,-13,13,genPartList);
46  populateGenEvent(ge1,-11,11,genPartList);
47  populateGenEvent(ge1,22,22,genPartList);
48  return true;
49  }
50 
51  int maximumBarcode(std::vector<HepMC::GenParticlePtr>& genPartList)
52  {
53  int maxBarcode{0};
54  if (genPartList.empty()) { return maxBarcode; }
55  for (HepMC::GenParticlePtr genPart : genPartList)
56  {
57  maxBarcode = std::max(maxBarcode, HepMC::barcode(genPart));
58  }
59  return maxBarcode;
60  }
61 
62  void populateGenEvent(HepMC::GenEvent & ge, int pdgid1, int pdgid2, std::vector<HepMC::GenParticlePtr>& genPartList)
63  {
64  int maxBarcode = maximumBarcode(genPartList);
65  HepMC::FourVector myPos( 0.0, 0.0, 0.0, 0.0);
66  HepMC::GenVertexPtr myVertex = HepMC::newGenVertexPtr( myPos, -1 );
67  HepMC::FourVector fourMomentum1( 0.0, 0.0, 1.0, 1.0*CLHEP::TeV);
68  HepMC::GenParticlePtr inParticle1 = HepMC::newGenParticlePtr(fourMomentum1, pdgid1, 2);
69  myVertex->add_particle_in(inParticle1);
70  HepMC::FourVector fourMomentum2( 0.0, 0.0, -1.0, 1.0*CLHEP::TeV);
71  HepMC::GenParticlePtr inParticle2 = HepMC::newGenParticlePtr(fourMomentum2, pdgid2, 2);
72  myVertex->add_particle_in(inParticle2);
73  HepMC::FourVector fourMomentum3( 0.0, 1.0, 0.0, 1.0*CLHEP::TeV);
74  HepMC::GenParticlePtr inParticle3 = HepMC::newGenParticlePtr(fourMomentum3, pdgid1, 1);
75  myVertex->add_particle_out(inParticle3);
76  genPartList.push_back(inParticle3);
77  HepMC::FourVector fourMomentum4( 0.0, -1.0, 0.0, 1.0*CLHEP::TeV);
78  HepMC::GenParticlePtr inParticle4 = HepMC::newGenParticlePtr(fourMomentum4, pdgid2, 1);
79  myVertex->add_particle_out(inParticle4);
80  genPartList.push_back(inParticle4);
81  ge.add_vertex( myVertex );
82  HepMC::suggest_barcode(inParticle1,maxBarcode+1);
83  HepMC::suggest_barcode(inParticle2,maxBarcode+2);
84  HepMC::suggest_barcode(inParticle3,maxBarcode+3);
85  HepMC::suggest_barcode(inParticle4,maxBarcode+4);
86  HepMC::set_signal_process_vertex(&ge, myVertex );
87  ge.set_beam_particles(inParticle1,inParticle2);
88  }
89 }
HepMC::GenVertexPtr
HepMC::GenVertex * GenVertexPtr
Definition: GenVertex.h:59
HepMC::suggest_barcode
bool suggest_barcode(T &p, int i)
Definition: GenEvent.h:548
GenEvent.h
max
#define max(a, b)
Definition: cfImp.cxx:41
GenVertex.h
HepMC::GenParticlePtr
GenParticle * GenParticlePtr
Definition: GenParticle.h:37
HepMC::newGenEvent
GenEvent * newGenEvent(const int a, const int b)
Definition: GenEvent.h:502
SG::CurrentEventStore::store
static IProxyDict * store()
Fetch the current store.
python.SystemOfUnits.TeV
int TeV
Definition: SystemOfUnits.py:158
GenParticle.h
WriteHandle.h
Handle class for recording to StoreGate.
HepMC::set_signal_process_vertex
void set_signal_process_vertex(GenEvent *e, T v)
Definition: GenEvent.h:528
McEventCollection.h
HepMC::newGenVertexPtr
GenVertexPtr newGenVertexPtr(const HepMC::FourVector &pos=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), const int i=0)
Definition: GenVertex.h:64
Atlas::ExtendedEventContext
Definition: ExtendedEventContext.h:23
HepMC::barcode
int barcode(const T *p)
Definition: Barcode.h:16
Athena_test
functions & macros to test the difference between floats
Definition: InitGaudiGoogleTest.h:30
initMcEventCollection.h
minimal gaudi initialization and record an McEventCollection in StoreGate
initGaudi.h
minimal gaudi initialization for AthenaServices unit testing
Athena_test::initGaudi
bool initGaudi(ISvcLocator *&pSvcLoc)
minimal gaudi initialization for AthenaServices unit testing
Definition: initGaudi.cxx:25
Athena_test::populateGenEvent
void populateGenEvent(HepMC::GenEvent &ge, int pdgid1, int pdgid2, std::vector< HepMC::GenParticlePtr > &genPartVector)
Definition: initMcEventCollection.cxx:62
Athena_test::initMcEventCollection
bool initMcEventCollection(ISvcLocator *&pSvcLoc, std::vector< HepMC::GenParticlePtr > &genPartVector, const bool initGaudi=true)
Definition: initMcEventCollection.cxx:21
HepMcParticleLinkCnv_p1.h
SG::WriteHandle< McEventCollection >
HepMC::newGenParticlePtr
GenParticlePtr newGenParticlePtr(const HepMC::FourVector &mom=HepMC::FourVector(0.0, 0.0, 0.0, 0.0), int pid=0, int status=0)
Definition: GenParticle.h:39
Athena_test::maximumBarcode
int maximumBarcode(std::vector< HepMC::GenParticlePtr > &genPartList)
Definition: initMcEventCollection.cxx:51