ATLAS Offline Software
Loading...
Searching...
No Matches
BeamSpotReweightingAlg.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
7
8// Athena headers
12
13// HepMC includes
14#include "AtlasHepMC/GenEvent.h"
16
17// CLHEP includes
18#include "CLHEP/Vector/LorentzVector.h"
19
20namespace Simulation
21{
22
23 BeamSpotReweightingAlg::BeamSpotReweightingAlg( const std::string& name, ISvcLocator* pSvcLocator )
24 : AthReentrantAlgorithm( name, pSvcLocator )
25 {
26 }
27
30 {
31 // This will check that the properties were initialized
32 // properly by job configuration.
33 ATH_CHECK( m_inputMcEventCollection.initialize() );
34 ATH_CHECK( m_beamSpotWeight.initialize() );
35 ATH_CHECK( m_beamSpotKey.initialize() );
36 ATH_CHECK( m_eventInfo.initialize() );
37
38 ATH_MSG_INFO("Input_beam_sigma_z="<<m_input_beam_sigma_z);
39
40 return StatusCode::SUCCESS;
41 }
42
43 StatusCode BeamSpotReweightingAlg::execute(const EventContext& ctx) const
44 {
45 float weight=1;
47 ATH_MSG_DEBUG("Input_beam_sigma_z="<<m_input_beam_sigma_z<<"<0, do nothing");
48 return StatusCode::SUCCESS;
49 }
50
51 if(msgLvl(MSG::DEBUG)) {
53 if (!evt.isValid()) {
54 ATH_MSG_FATAL( "Could not find event info" );
55 return(StatusCode::FAILURE);
56 }
57 ATH_MSG_DEBUG( "EventInfo before adding the weight: " << evt->eventNumber()
58 << " run: " << evt->runNumber()
59 << " hasBeamSpotWeight: "<< evt->hasBeamSpotWeight()
60 << " beamSpotWeight: "<< evt->beamSpotWeight() );
61 }
62
63 // Construct handles from the keys.
64 SG::ReadHandle<McEventCollection> h_inputMcEventCollection (m_inputMcEventCollection, ctx);
65 if(!h_inputMcEventCollection.isValid()) {
66 ATH_MSG_FATAL("No input McEventCollection called " << h_inputMcEventCollection.name() << " in StoreGate.");
67 return StatusCode::FAILURE;
68 }
69
70 // loop over the event in the mc collection
71 for (const auto currentGenEvent : *h_inputMcEventCollection) {
72 // skip empty events
73 if ( !currentGenEvent ) {
74 //the hard scatter event is always the first one. If not present, do nothing
75 ATH_MSG_WARNING("No first event found in the McEventCollection input collection, use default weight="<<weight);
76 break;
77 }
78 auto signalVertex = GetSignalProcessVertex(*currentGenEvent);
79
80 if(signalVertex) {
81 //now calculate weight
82 weight=1.0;
83 float z=signalVertex->position().z();
85 float beamz=beamSpotHandle->beamPos().z();
86 float newsigmaz=beamSpotHandle->beamSigma(2);
87 float pullold=(z-beamz)/m_input_beam_sigma_z;
88 float pullnew=(z-beamz)/newsigmaz;
89
90 ATH_MSG_DEBUG("Beamspot z="<<beamz<<", z="<<z<<"; old sigma_z="<<m_input_beam_sigma_z<<", old pull="<<pullold<<"; new sigma_z="<<newsigmaz<<", new pull="<<pullnew);
91
92 if(std::abs(pullold)<10 && std::abs(pullnew)<10) {
93 //Use Gauss probability ratio to calculate the weight:
94 //weight=TMath::Gaus(z,0,35,true)/TMath::Gaus(z,0,42,true);
95 //This can be simplified to:
96 weight=m_input_beam_sigma_z/newsigmaz * std::exp(0.5*(pullold*pullold - pullnew*pullnew));
97 } else {
98 ATH_MSG_WARNING("Large pull of beamspot: Beamspot z="<<beamz<<", z="<<z<<"; old sigma_z="<<m_input_beam_sigma_z<<", old pull="<<pullold<<"; new sigma_z="<<newsigmaz<<", new pull="<<pullnew<<" => use default weight="<<weight);
99 }
100 } else {
101 ATH_MSG_WARNING("No signal vertex found, use default weight="<<weight);
102 }
103
104 ATH_MSG_DEBUG("Beam weight="<<weight);
105 break;
106 }
107
109 if (!BeamSpotWeight.isPresent()) {
110 ATH_MSG_ERROR( "BeamSpotWeight.isPresent check fails" );
111 return StatusCode::FAILURE;
112 }
113 if (!BeamSpotWeight.isAvailable()) {
114 BeamSpotWeight(0) = weight;
115 }
116
117 if(msgLvl(MSG::DEBUG)) {
119 if (!evt.isValid()) {
120 ATH_MSG_FATAL( "Could not find event info" );
121 return(StatusCode::FAILURE);
122 }
123 ATH_MSG_DEBUG( "EventInfo after adding the weight: " << evt->eventNumber()
124 << " run: " << evt->runNumber()
125 << " hasBeamSpotWeight: "<< evt->hasBeamSpotWeight()
126 << " beamSpotWeight: "<< evt->beamSpotWeight() );
127 }
128
129 return StatusCode::SUCCESS;
130 }
131
133 {
134 //Ensure that we have a valid signal_process_vertex
135#ifdef HEPMC3
136 if( !HepMC::signal_process_vertex(&ge) ) {
137 if (!ge.vertices().empty()) {
138 ATH_MSG_DEBUG("No signal_process_vertex found - using the first GenVertex in the event.");
139 auto signalVertex = ge.vertices().front();
140 return signalVertex;
141 }
142 if( !HepMC::signal_process_vertex(&ge) ) { // Insanity check
143 if (!ge.vertices().empty()) {
144 ATH_MSG_ERROR("Failed to set signal_process_vertex for GenEvent!!");
145 return nullptr;
146 }
147 ATH_MSG_WARNING("No signal_process_vertex found. Empty GenEvent!");
148 return nullptr;
149 }
150 }
151 else {
152 ATH_MSG_DEBUG("signal_process_vertex set by Generator.");
154 }
155#else
156 if( !ge.signal_process_vertex() ) {
157 if (!ge.vertices_empty()) {
158 ATH_MSG_DEBUG("No signal_process_vertex found - using the first GenVertex in the event.");
159 HepMC::GenVertex *signalVertex = *(ge.vertices_begin());
160 return signalVertex;
161 }
162 if( !ge.signal_process_vertex() ) { // Insanity check
163 if (!ge.vertices_empty()) {
164 ATH_MSG_ERROR("Failed to set signal_process_vertex for GenEvent!!");
165 return nullptr;
166 }
167 ATH_MSG_WARNING("No signal_process_vertex found. Empty GenEvent!");
168 return nullptr;
169 }
170 }
171 else {
172 ATH_MSG_DEBUG("signal_process_vertex set by Generator.");
173 return ge.signal_process_vertex();
174 }
175#endif
176 return nullptr;
177 }
178
179} // namespace Simulation
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
Handle class for reading from StoreGate.
Handle class for adding a decoration to an object.
Handle class for recording to StoreGate.
#define z
bool msgLvl(const MSG::Level lvl) const
An algorithm that can be simultaneously executed in multiple threads.
virtual bool isValid() override final
Can the handle be successfully dereferenced?
const std::string & name() const
Return the StoreGate ID for the referenced object.
Handle class for adding a decoration to an object.
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
bool isPresent() const
Is the referenced container present in SG?
SG::ReadHandleKey< McEventCollection > m_inputMcEventCollection
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo
virtual StatusCode execute(const EventContext &ctx) const override final
Athena algorithm's interface method execute()
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
virtual StatusCode initialize() override final
Athena algorithm's interface method initialize()
Gaudi::Property< float > m_input_beam_sigma_z
HepMC::ConstGenVertexPtr GetSignalProcessVertex(const HepMC::GenEvent &ge) const
Ensure that the GenEvent::signal_process_vertex has been set.
BeamSpotReweightingAlg(const std::string &name, ISvcLocator *pSvcLocator)
SG::WriteDecorHandleKey< xAOD::EventInfo > m_beamSpotWeight
GenVertex * signal_process_vertex(const GenEvent *e)
Definition GenEvent.h:627
const HepMC::GenVertex * ConstGenVertexPtr
Definition GenVertex.h:60