ATLAS Offline Software
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
9 #include "StoreGate/ReadHandle.h"
10 #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  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.
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;
46  if(m_input_beam_sigma_z<0) {
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.");
153  return HepMC::signal_process_vertex(&ge);
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
Simulation::BeamSpotReweightingAlg::execute
virtual StatusCode execute(const EventContext &ctx) const override final
Athena algorithm's interface method execute()
Definition: BeamSpotReweightingAlg.cxx:43
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
GenEvent.h
SG::WriteDecorHandle::isAvailable
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
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::BeamSpotReweightingAlg::m_beamSpotKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition: BeamSpotReweightingAlg.h:62
AthCommonMsg< Gaudi::Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
Simulation::BeamSpotReweightingAlg::m_eventInfo
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfo
Definition: BeamSpotReweightingAlg.h:64
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
Simulation::BeamSpotReweightingAlg::GetSignalProcessVertex
HepMC::ConstGenVertexPtr GetSignalProcessVertex(const HepMC::GenEvent &ge) const
Ensure that the GenEvent::signal_process_vertex has been set.
Definition: BeamSpotReweightingAlg.cxx:132
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
dqt_zlumi_pandas.weight
int weight
Definition: dqt_zlumi_pandas.py:189
WriteHandle.h
Handle class for recording to StoreGate.
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
Simulation::BeamSpotReweightingAlg::initialize
virtual StatusCode initialize() override final
Athena algorithm's interface method initialize()
Definition: BeamSpotReweightingAlg.cxx:29
Simulation::BeamSpotReweightingAlg::BeamSpotReweightingAlg
BeamSpotReweightingAlg(const std::string &name, ISvcLocator *pSvcLocator)
Definition: BeamSpotReweightingAlg.cxx:23
z
#define z
Simulation::BeamSpotReweightingAlg::m_inputMcEventCollection
SG::ReadHandleKey< McEventCollection > m_inputMcEventCollection
Definition: BeamSpotReweightingAlg.h:58
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::WriteDecorHandle
Handle class for adding a decoration to an object.
Definition: StoreGate/StoreGate/WriteDecorHandle.h:100
WriteDecorHandle.h
Handle class for adding a decoration to an object.
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
BeamSpotReweightingAlg.h
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
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
Simulation::BeamSpotReweightingAlg::m_input_beam_sigma_z
Gaudi::Property< float > m_input_beam_sigma_z
Definition: BeamSpotReweightingAlg.h:66
Simulation::BeamSpotReweightingAlg::m_beamSpotWeight
SG::WriteDecorHandleKey< xAOD::EventInfo > m_beamSpotWeight
Definition: BeamSpotReweightingAlg.h:60
SG::WriteDecorHandleKey::initialize
StatusCode initialize(bool used=true)
If this object is used as a property, then this should be called during the initialize phase.
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
DEBUG
#define DEBUG
Definition: page_access.h:11
Simulation
Definition: BeamEffectsAlg.cxx:21
HepMC::ConstGenVertexPtr
const HepMC::GenVertex * ConstGenVertexPtr
Definition: GenVertex.h:60
ReadHandle.h
Handle class for reading from StoreGate.
SG::WriteDecorHandle::isPresent
bool isPresent() const
Is the referenced container present in SG?
HepMC::signal_process_vertex
GenVertex * signal_process_vertex(const GenEvent *e)
Definition: GenEvent.h:625