ATLAS Offline Software
BeamEffectsAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 // class header
6 #include "BeamEffectsAlg.h"
7 
8 // Athena headers
10 #include "StoreGate/ReadHandle.h"
11 #include "StoreGate/WriteHandle.h"
12 
13 // HepMC includes
14 #include "AtlasHepMC/GenEvent.h"
15 #include "AtlasHepMC/GenVertex.h"
16 
17 // CLHEP includes
18 #include "CLHEP/Vector/LorentzVector.h"
19 
20 namespace Simulation
21 {
22 
23  BeamEffectsAlg::BeamEffectsAlg( const std::string& name, ISvcLocator* pSvcLocator )
24  : AthReentrantAlgorithm( name, pSvcLocator )
25  {
26  }
27 
30  {
31  // the Tools for event manipulation
32  if (!m_genEventManipulators.empty()) {
33  ATH_CHECK( m_genEventManipulators.retrieve() );
34  }
35  // This will check that the properties were initialized
36  // properly by job configuration.
40  return StatusCode::SUCCESS;
41  }
42 
43  StatusCode BeamEffectsAlg::execute(const EventContext& ctx) const
44  {
45  // Construct handles from the keys.
46  SG::ReadHandle<McEventCollection> h_inputMcEventCollection (m_inputMcEventCollection, ctx);
47  SG::WriteHandle<McEventCollection> h_outputMcEventCollection (m_outputMcEventCollection, ctx);
48  if(!h_inputMcEventCollection.isValid()) {
49  ATH_MSG_FATAL("No input McEventCollection called " << h_inputMcEventCollection.name() << " in " << h_inputMcEventCollection.store());
50  return StatusCode::FAILURE;
51  }
52  auto outputMcEventCollection = std::make_unique<McEventCollection>(*h_inputMcEventCollection);
53  ATH_CHECK( h_outputMcEventCollection.record ( std::move (outputMcEventCollection)) );
54  // loop over the events in the mc collection
55  for (HepMC::GenEvent* currentGenEvent : *h_outputMcEventCollection) {
56  // skip empty events
57  if ( !currentGenEvent ) continue;
58  ATH_CHECK(this->patchSignalProcessVertex(*currentGenEvent));
59  // manipulate the GenEvent if needed : validating, filtering, smearing, dispersion, ...
60  for (const auto& manipulator : m_genEventManipulators) {
61  // call manipulate(..) in the current GenEventManipulator
62  ATH_CHECK(manipulator->manipulate(*currentGenEvent, ctx));
63  }
64  }
65 
66  return StatusCode::SUCCESS;
67  }
68 
70  {
71  //Ensure that we have a valid signal_process_vertex
72 #ifdef HEPMC3
73  if( !HepMC::signal_process_vertex(&ge) ) {
74  if(m_ISFRun) {
75  ATH_MSG_DEBUG("No signal_process_vertex found - creating a dummy GenVertex.");
76  HepMC::FourVector signalPos( 0.0, 0.0, 0.0, 0.0);
77  HepMC::GenVertexPtr signalVertex = HepMC::newGenVertexPtr( signalPos );
78  // ge will now take ownership of the signal process vertex
79  HepMC::set_signal_process_vertex(&ge, signalVertex );
80  }
81  else {
82  if (!ge.vertices().empty()) {
83  ATH_MSG_DEBUG("No signal_process_vertex found - using the first GenVertex in the event.");
84  HepMC::GenVertexPtr signalVertex = ge.vertices().front();
85  HepMC::set_signal_process_vertex(&ge,signalVertex );
86  }
87  }
88  if( !HepMC::signal_process_vertex(&ge) ) { // Insanity check
89  if (!ge.vertices().empty()) {
90  ATH_MSG_ERROR("Failed to set signal_process_vertex for GenEvent!!");
91  return StatusCode::FAILURE;
92  }
93  ATH_MSG_WARNING("No signal_process_vertex found. Empty GenEvent!");
94  }
95  }
96  else {
97  ATH_MSG_DEBUG("signal_process_vertex set by Generator.");
98  }
99 #else
100  if( !ge.signal_process_vertex() ) {
101  if(m_ISFRun) {
102  ATH_MSG_DEBUG("No signal_process_vertex found - creating a dummy GenVertex.");
103  HepMC::FourVector signalPos( 0.0, 0.0, 0.0, 0.0);
104  HepMC::GenVertexPtr signalVertex = HepMC::newGenVertexPtr( signalPos );
105  // ge will now take ownership of the signal process vertex
106  ge.set_signal_process_vertex( signalVertex );
107  }
108  else {
109  if (!ge.vertices_empty()) {
110  ATH_MSG_DEBUG("No signal_process_vertex found - using the first GenVertex in the event.");
111  HepMC::GenVertexPtr signalVertex = *(ge.vertices_begin());
112  ge.set_signal_process_vertex( signalVertex );
113  }
114  }
115  if( !HepMC::signal_process_vertex(&ge) ) { // Insanity check
116  if (!ge.vertices_empty()) {
117  ATH_MSG_ERROR("Failed to set signal_process_vertex for GenEvent!!");
118  return StatusCode::FAILURE;
119  }
120  ATH_MSG_WARNING("No signal_process_vertex found. Empty GenEvent!");
121  }
122  }
123  else {
124  ATH_MSG_DEBUG("signal_process_vertex set by Generator.");
125  }
126 #endif
127  return StatusCode::SUCCESS;
128  }
129 
130 } // namespace Simulation
HepMC::GenVertexPtr
HepMC::GenVertex * GenVertexPtr
Definition: GenVertex.h:59
Simulation::BeamEffectsAlg::BeamEffectsAlg
BeamEffectsAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: BeamEffectsAlg.cxx:23
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
GenEvent.h
SG::ReadHandle< McEventCollection >
SG::VarHandleBase::name
const std::string & name() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleBase.cxx:75
GenVertex.h
Simulation::BeamEffectsAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Athena algorithm's interface method execute()
Definition: BeamEffectsAlg.cxx:43
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
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:650
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
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
Simulation::BeamEffectsAlg::patchSignalProcessVertex
StatusCode patchSignalProcessVertex(HepMC::GenEvent &ge) const
Ensure that the GenEvent::signal_process_vertex has been set.
Definition: BeamEffectsAlg.cxx:69
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
SG::VarHandleBase::store
std::string store() const
Return the name of the store holding the object we are proxying.
Definition: StoreGate/src/VarHandleBase.cxx:376
Simulation::BeamEffectsAlg::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: BeamEffectsAlg.h:69
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
SG::VarHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:103
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
Simulation::BeamEffectsAlg::m_inputMcEventCollection
SG::ReadHandleKey< McEventCollection > m_inputMcEventCollection
Definition: BeamEffectsAlg.h:65
SG::WriteHandle< McEventCollection >
Simulation::BeamEffectsAlg::m_genEventManipulators
ToolHandleArray< IGenEventManipulator > m_genEventManipulators
Event manipulator tools (executed in given order)
Definition: BeamEffectsAlg.h:72
SG::WriteHandle::record
StatusCode record(std::unique_ptr< T > data)
Record a const object to the store.
Simulation::BeamEffectsAlg::initialize
virtual StatusCode initialize() override final
Athena algorithm's interface method initialize()
Definition: BeamEffectsAlg.cxx:29
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Simulation
Definition: BeamEffectsAlg.cxx:21
Simulation::BeamEffectsAlg::m_ISFRun
Gaudi::Property< bool > m_ISFRun
Temporary property so that we don't change the output in the initial switch to this code.
Definition: BeamEffectsAlg.h:75
BeamEffectsAlg.h
ReadHandle.h
Handle class for reading from StoreGate.
Simulation::BeamEffectsAlg::m_outputMcEventCollection
SG::WriteHandleKey< McEventCollection > m_outputMcEventCollection
Definition: BeamEffectsAlg.h:66
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:625