ATLAS Offline Software
GenEventVertexPositioner.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2021 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header include
7 
8 // HepMC includes
9 #include "AtlasHepMC/GenEvent.h"
10 #include "AtlasHepMC/GenVertex.h"
11 
12 // Framework includes
14 
15 // CLHEP includes
16 #include "CLHEP/Vector/LorentzVector.h"
17 
18 namespace Simulation
19 {
22  const std::string& n,
23  const IInterface* p )
24  : base_class(t,n,p)
25  {
26  }
27 
30  {
31  ATH_MSG_VERBOSE("Initializing ...");
32 
33  // retrieve Vertex Shifters
34  if ( !m_vertexShifters.empty() ) {
35  ATH_CHECK(m_vertexShifters.retrieve());
36  }
37 
38  return StatusCode::SUCCESS;
39  }
40 
43  {
44  ATH_MSG_VERBOSE("Finalizing ...");
45  return StatusCode::SUCCESS;
46  }
47 
49  StatusCode GenEventVertexPositioner::manipulate(HepMC::GenEvent& ge, const EventContext& ctx) const
50  {
51  // Grab signal_process_vertex pointer
52  auto signalProcVtx=HepMC::signal_process_vertex(&ge);
53  if(!signalProcVtx) {
54  ATH_MSG_ERROR("Expected GenEvent::signal_process_vertex() to already have been set at this point!");
55  return StatusCode::FAILURE;
56  }
57 
58  // loop over all given ILorentzVectorGenerator AlgTools
59  for (const auto& vertexShifter : m_vertexShifters) {
60 
61  // call VertexShifter and let it compute the current shift
62  CLHEP::HepLorentzVector *curShift = vertexShifter->generate(ctx);
63  if (!curShift) {
64  ATH_MSG_ERROR("Vertex Shifter AthenaTool returned zero-pointer! Ignore.");
65  continue;
66  }
67 
68  ATH_MSG_VERBOSE("Retrieved Vertex shift of: " << *curShift << " from " << vertexShifter->name());
69 
70  // As signal process vertex is a pointer, there is some risk
71  // that the pointer points to a vertex somewhere else in the
72  // event, rather than a unique / new vertex, in which case we
73  // will modify its position in the loop below and will not need
74  // to treat it separately.
75  bool modifySigVtx(true);
76 
77  // loop over the vertices in the event, they are in respect with another
78  // (code from Simulation/Fatras/FatrasAlgs/McEventPreProcessing.cxx)
79 #ifdef HEPMC3
80  for(auto& curVtx: ge.vertices()) {
81  // NB Doing this check to explicitly avoid the fallback mechanism in
82  // HepMC3::GenVertex::position() to return the position of
83  // another GenVertex in the event if the position isn't set (or is set to zero)!
84  const HepMC::FourVector &curPos = (curVtx->has_set_position()) ? curVtx->position() : HepMC::FourVector::ZERO_VECTOR();
85 #else
86  auto vtxIt = ge.vertices_begin();
87  auto vtxItEnd = ge.vertices_end();
88  for( ; vtxIt != vtxItEnd; ++vtxIt) {
89  // quick access:
90  auto curVtx = (*vtxIt);
91  const HepMC::FourVector &curPos = curVtx->position();
92 #endif
93 
94  // get a copy of the current vertex position
95  CLHEP::HepLorentzVector newPos( curPos.x(), curPos.y(), curPos.z(), curPos.t() );
96  // and update it with the given smearing
97  newPos += (*curShift);
98 
99  ATH_MSG_VERBOSE( "Original vtx position = " << curPos.x() << ", " << curPos.y() << ", " << curPos.z() );
100  ATH_MSG_VERBOSE( "Updated vtx position = " << newPos );
101 
102  // store the updated position in the vertex
103  curVtx->set_position( HepMC::FourVector(newPos.x(),newPos.y(),newPos.z(),newPos.t()));
104  if(modifySigVtx && signalProcVtx==curVtx) {
105  modifySigVtx=false;
106  }
107  }
108 
109  // Do the same for the signal process vertex if still required.
110  if (modifySigVtx) {
111  const HepMC::FourVector &curPos = HepMC::signal_process_vertex(&ge)->position();
112  CLHEP::HepLorentzVector newPos( curPos.x(), curPos.y(), curPos.z(), curPos.t() );
113  newPos += (*curShift);
114  (HepMC::signal_process_vertex(&ge))->set_position( HepMC::FourVector(newPos.x(),newPos.y(),newPos.z(),newPos.t()) );
115  }
116 
117  // memory cleanup
118  delete curShift;
119  }
120 
121  return StatusCode::SUCCESS;
122  }
123 
124 }
Simulation::GenEventVertexPositioner::GenEventVertexPositioner
GenEventVertexPositioner(const std::string &t, const std::string &n, const IInterface *p)
Constructor with parameters.
Definition: GenEventVertexPositioner.cxx:21
GenEvent.h
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
GenVertex.h
Simulation::GenEventVertexPositioner::finalize
StatusCode finalize() override final
Athena algtool's Hooks.
Definition: GenEventVertexPositioner.cxx:42
Simulation::GenEventVertexPositioner::m_vertexShifters
ToolHandleArray< ILorentzVectorGenerator > m_vertexShifters
Vertex Shifters applied in the given order.
Definition: GenEventVertexPositioner.h:44
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Simulation::GenEventVertexPositioner::initialize
StatusCode initialize() override final
Athena algtool's Hooks.
Definition: GenEventVertexPositioner.cxx:29
Simulation::GenEventVertexPositioner::manipulate
StatusCode manipulate(HepMC::GenEvent &ge, const EventContext &ctx) const override final
modifies (displaces) the given GenEvent
Definition: GenEventVertexPositioner.cxx:49
ILorentzVectorGenerator.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
GenEventVertexPositioner.h
beamspotman.n
n
Definition: beamspotman.py:731
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Simulation
Definition: BeamEffectsAlg.cxx:21
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:503