ATLAS Offline Software
BuildVertexPointingAlg.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2024 CERN for the benefit of the ATLAS collaboration
3 */
4 
6 
12 #include <TLorentzVector.h>
16 
17 #include <numeric>
18 #include <utility>
19 
21 
23  ISvcLocator* svcLoc)
24  : EL::AnaAlgorithm(name, svcLoc) {}
25 
27  // read
28  ATH_CHECK(m_photonContainerKey.initialize());
30 
31  ATH_CHECK(m_photon_zvertex_key.initialize());
32  ATH_CHECK(m_photon_errz_key.initialize());
33  ATH_CHECK(m_photon_HPV_zvertex_key.initialize());
34  ATH_CHECK(m_photon_HPV_errz_key.initialize());
35 
36  // write
38 
39  ATH_CHECK(m_nphotons_good_key.initialize());
40  ATH_CHECK(m_z_common_nphotons_key.initialize());
41  ATH_CHECK(m_photons_px_key.initialize());
42  ATH_CHECK(m_photons_py_key.initialize());
43  ATH_CHECK(m_photons_pz_key.initialize());
44  ATH_CHECK(m_photon_links_key.initialize());
45 
46  // tool
47  ATH_CHECK(m_selectionTool.retrieve());
48 
49  ATH_MSG_INFO("Use at maximum " << m_nphotons_to_use
50  << " photons for pointing");
51  ATH_MSG_INFO("Selecting photons for pointing with "
52  << m_selectionTool << " with "
53  << m_selectionTool->getAcceptInfo().getNCuts() << "cuts:");
54  for (unsigned int icut = 0;
55  icut < m_selectionTool->getAcceptInfo().getNCuts(); ++icut) {
57  "cut " << icut << ": "
58  << m_selectionTool->getAcceptInfo().getCutDescription(icut));
59  }
60  return StatusCode::SUCCESS;
61 }
62 
64  // the code has been written with photons in mind, but it is important
65  // the code to work also with electrons since electrons are used as a
66  // proxy of photons in performance studies
68  ATH_CHECK(original_photons.isValid());
69 
70  ATH_MSG_DEBUG("number of photon in the container ("
71  << m_photonContainerKey << "): " << original_photons->size());
72 
73  // these photons will be used to compute pointing variables
75  selectPhotons(*original_photons, photons_selected);
76 
78  const std::pair<float, float> z_common_and_error =
80  m_convPtCut);
81 
82  // create new vertex container
83  auto pointingVertices = std::make_unique<xAOD::VertexContainer>();
84  auto pointingVerticesAux = std::make_unique<xAOD::VertexAuxContainer>();
85  pointingVertices->setStore(pointingVerticesAux.get());
86 
87  // create new vertex
88  xAOD::Vertex* vtx_pointing =
89  (*pointingVertices).push_back(std::make_unique<xAOD::Vertex>());
90 
91  vtx_pointing->setVertexType(xAOD::VxType::PriVtx);
92 
93  vtx_pointing->setZ(z_common_and_error.first);
94  vtx_pointing->setX(0.0);
95  vtx_pointing->setY(0.0);
96  AmgSymMatrix(3) cov;
97  cov.setZero();
98  cov(2, 2) = z_common_and_error.second * z_common_and_error.second;
99  vtx_pointing->setCovariancePosition(cov);
100 
101  auto writeHandle = SG::makeHandle(m_pointingVertexContainerKey);
102  ANA_CHECK(writeHandle.record(std::move(pointingVertices),
103  std::move(pointingVerticesAux)));
104 
105  // below only decorations to the vertex
106 
107  const unsigned int n_good_photons =
108  std::count_if(photons_selected.begin(), photons_selected.end(),
109  [this](const xAOD::Egamma* photon) {
110  return m_goodPhotonSelectionTool->accept(photon);
111  });
112 
113  std::vector<ElementLink<xAOD::EgammaContainer>> ph_links;
114  ph_links.reserve(photons_selected.size());
115  for (const xAOD::Egamma* photon : photons_selected) {
116  ph_links.emplace_back(*original_photons, photon->index());
117  }
118 
119  auto dec_nphotons = SG::makeHandle<unsigned int>(m_z_common_nphotons_key);
120  auto dec_ph_links = SG::makeHandle<decltype(ph_links)>(m_photon_links_key);
121  auto dec_nphotons_good = SG::makeHandle<unsigned int>(m_nphotons_good_key);
122 
123  dec_nphotons(*vtx_pointing) = photons_selected.size();
124  dec_ph_links(*vtx_pointing) = ph_links;
125  dec_nphotons_good(*vtx_pointing) = n_good_photons;
126 
127  // compute common vertex from the pointing
128  const TLorentzVector p4_photons = std::accumulate(
129  photons_selected.begin(), photons_selected.end(),
130  TLorentzVector(0, 0, 0, 0),
131  [](TLorentzVector sum, const xAOD::Egamma* photon) {
132  TLorentzVector v_ph;
133  const xAOD::CaloCluster* cluster = photon->caloCluster();
134  v_ph.SetPtEtaPhiM(photon->e() / cosh(cluster->etaBE(2)),
135  cluster->etaBE(2), cluster->phiBE(2), 0.0);
136  sum += v_ph;
137  return sum;
138  });
139 
140  auto dec_photons_px = SG::makeHandle<float>(m_photons_px_key);
141  auto dec_photons_py = SG::makeHandle<float>(m_photons_py_key);
142  auto dec_photons_pz = SG::makeHandle<float>(m_photons_pz_key);
143 
144  dec_photons_px(*vtx_pointing) = p4_photons.X();
145  dec_photons_py(*vtx_pointing) = p4_photons.Y();
146  dec_photons_pz(*vtx_pointing) = p4_photons.Z();
147 
148  return StatusCode::SUCCESS;
149 }
150 
152  const xAOD::EgammaContainer& original_photons,
153  ConstDataVector<DataVector<xAOD::Egamma>>& photons_selected) const {
154  for (const xAOD::Egamma* photon : original_photons) {
155  if (m_selectionTool->accept(photon)) {
156  photons_selected.push_back(photon);
157  ATH_MSG_DEBUG("photon selected for pointing: pT = " << photon->pt()
158  << " GeV");
159  }
160  }
162  "number of photons selected for pointing: " << photons_selected.size());
163 
164  // limit the number of photons to use
165  if (m_nphotons_to_use >= 0 and
166  photons_selected.size() > static_cast<std::size_t>(m_nphotons_to_use)) {
167  std::sort(photons_selected.begin(), photons_selected.end(),
168  [](const xAOD::IParticle* a, const xAOD::IParticle* b) {
169  return a->pt() > b->pt(); // sort from the highest pT
170  });
171 
172  photons_selected.resize(m_nphotons_to_use);
173  }
174  ATH_MSG_DEBUG("number of photons selected for pointing after limiting: "
175  << photons_selected.size());
176  if (ATH_UNLIKELY(this->msgLvl(MSG::DEBUG))) {
177  for (const xAOD::Egamma* photon : photons_selected) {
178  ATH_MSG_DEBUG("photon selected for pointing: pT = " << photon->pt()
179  << " GeV");
180  }
181  }
182 }
WriteHandle.h
Handle class for recording to StoreGate.
BuildVertexPointingAlg::m_photon_zvertex_key
SG::ReadDecorHandleKey< xAOD::EgammaContainer > m_photon_zvertex_key
Definition: BuildVertexPointingAlg.h:63
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::VIEW_ELEMENTS
@ VIEW_ELEMENTS
this data object is a view, it does not own its elmts
Definition: OwnershipPolicy.h:18
ConstDataVector::end
iterator end() noexcept
Return an iterator pointing past the end of the collection.
ConstDataVector.h
DataVector adapter that acts like it holds const pointers.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
BuildVertexPointingAlg::m_photon_links_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photon_links_key
Definition: BuildVertexPointingAlg.h:58
BuildVertexPointingAlg::BuildVertexPointingAlg
BuildVertexPointingAlg(const std::string &name, ISvcLocator *svcLoc=nullptr)
Definition: BuildVertexPointingAlg.cxx:22
accumulate
bool accumulate(AccumulateMap &map, std::vector< module_t > const &modules, FPGATrackSimMatrixAccumulator const &acc)
Accumulates an accumulator (e.g.
Definition: FPGATrackSimMatrixAccumulator.cxx:22
xAOD::PVHelpers::getZCommonAndError
std::pair< float, float > getZCommonAndError(const xAOD::EventInfo *eventInfo, const xAOD::EgammaContainer *egammas, float convPtCut=2e3)
Return zCommon and zCommonError.
Definition: PhotonVertexHelpers.cxx:44
plotBeamSpotVxVal.cov
cov
Definition: plotBeamSpotVxVal.py:201
AthCommonMsg< Algorithm >::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
ANA_CHECK
#define ANA_CHECK(EXP)
check whether the given expression was successful
Definition: Control/AthToolSupport/AsgMessaging/AsgMessaging/MessageCheck.h:324
xAOD::CaloCluster_v1::phiBE
float phiBE(const unsigned layer) const
Get the phi in one layer of the EM Calo.
Definition: CaloCluster_v1.cxx:680
xAOD::Egamma_v1
Definition: Egamma_v1.h:56
ATH_UNLIKELY
#define ATH_UNLIKELY(x)
Definition: AthUnlikelyMacros.h:17
BuildVertexPointingAlg::m_convPtCut
Gaudi::Property< float > m_convPtCut
Definition: BuildVertexPointingAlg.h:80
BuildVertexPointingAlg::m_photon_errz_key
SG::ReadDecorHandleKey< xAOD::EgammaContainer > m_photon_errz_key
Definition: BuildVertexPointingAlg.h:65
BuildVertexPointingAlg::m_nphotons_good_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_nphotons_good_key
Definition: BuildVertexPointingAlg.h:48
xAOD::Vertex_v1::setX
void setX(float value)
Sets the x position.
xAOD::IParticle
Class providing the definition of the 4-vector interface.
Definition: Event/xAOD/xAODBase/xAODBase/IParticle.h:40
BuildVertexPointingAlg.h
AmgSymMatrix
#define AmgSymMatrix(dim)
Definition: EventPrimitives.h:52
ConstDataVector::asDataVector
const DV * asDataVector() const
Return a pointer to this object, as a const DataVector.
EgammaContainer.h
SG::makeHandle
SG::ReadCondHandle< T > makeHandle(const SG::ReadCondHandleKey< T > &key, const EventContext &ctx=Gaudi::Hive::currentContext())
Definition: ReadCondHandle.h:269
xAOD::CaloCluster_v1::etaBE
float etaBE(const unsigned layer) const
Get the eta in one layer of the EM Calo.
Definition: CaloCluster_v1.cxx:644
xAOD::Vertex_v1::setVertexType
void setVertexType(VxType::VertexType vType)
Set the type of the vertex.
BuildVertexPointingAlg::m_photons_px_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photons_px_key
Definition: BuildVertexPointingAlg.h:52
xAOD::CaloCluster_v1
Description of a calorimeter cluster.
Definition: CaloCluster_v1.h:59
xAOD::Vertex_v1::setZ
void setZ(float value)
Sets the z position.
BuildVertexPointingAlg::m_photons_py_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photons_py_key
Definition: BuildVertexPointingAlg.h:54
convertTimingResiduals.sum
sum
Definition: convertTimingResiduals.py:55
xAOD::Vertex_v1::setY
void setY(float value)
Sets the y position.
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
BuildVertexPointingAlg::m_nphotons_to_use
Gaudi::Property< int > m_nphotons_to_use
Definition: BuildVertexPointingAlg.h:81
xAOD::VxType::PriVtx
@ PriVtx
Primary vertex.
Definition: TrackingPrimitives.h:571
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
EL
This module defines the arguments passed from the BATCH driver to the BATCH worker.
Definition: AlgorithmWorkerData.h:24
BuildVertexPointingAlg::execute
virtual StatusCode execute()
Definition: BuildVertexPointingAlg.cxx:63
BuildVertexPointingAlg::m_selectionTool
ToolHandle< IAsgSelectionTool > m_selectionTool
Definition: BuildVertexPointingAlg.h:72
BuildVertexPointingAlg::m_photon_HPV_zvertex_key
SG::ReadDecorHandleKey< xAOD::EgammaContainer > m_photon_HPV_zvertex_key
Definition: BuildVertexPointingAlg.h:67
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
BuildVertexPointingAlg::m_photon_HPV_errz_key
SG::ReadDecorHandleKey< xAOD::EgammaContainer > m_photon_HPV_errz_key
Definition: BuildVertexPointingAlg.h:69
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
WriteDecorHandle.h
Handle class for adding a decoration to an object.
BuildVertexPointingAlg::m_pointingVertexContainerKey
SG::WriteHandleKey< xAOD::VertexContainer > m_pointingVertexContainerKey
Definition: BuildVertexPointingAlg.h:44
PhotonVertexHelpers.h
BuildVertexPointingAlg::selectPhotons
void selectPhotons(const xAOD::EgammaContainer &original_photons, ConstDataVector< DataVector< xAOD::Egamma >> &photons_selected) const
Definition: BuildVertexPointingAlg.cxx:151
EventPrimitives.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ReadHandle.h
Handle class for reading from StoreGate.
xAOD::EventInfo_v1
Class describing the basic event information.
Definition: EventInfo_v1.h:43
BuildVertexPointingAlg::m_photonContainerKey
SG::ReadHandleKey< xAOD::EgammaContainer > m_photonContainerKey
Definition: BuildVertexPointingAlg.h:39
VertexContainer.h
xAOD::photon
@ photon
Definition: TrackingPrimitives.h:199
a
TList * a
Definition: liststreamerinfos.cxx:10
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ConstDataVector
DataVector adapter that acts like it holds const pointers.
Definition: ConstDataVector.h:76
BuildVertexPointingAlg::m_photons_pz_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_photons_pz_key
Definition: BuildVertexPointingAlg.h:56
DEBUG
#define DEBUG
Definition: page_access.h:11
BuildVertexPointingAlg::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: BuildVertexPointingAlg.h:42
BuildVertexPointingAlg::m_z_common_nphotons_key
SG::WriteDecorHandleKey< xAOD::VertexContainer > m_z_common_nphotons_key
Definition: BuildVertexPointingAlg.h:50
BuildVertexPointingAlg::initialize
virtual StatusCode initialize()
Definition: BuildVertexPointingAlg.cxx:26
SG::get
const T * get(const ReadHandleKey< T > &key)
Convenience function to retrieve an object given a ReadHandleKey.
xAOD::Vertex_v1::setCovariancePosition
void setCovariancePosition(const AmgSymMatrix(3)&covariancePosition)
Sets the vertex covariance matrix.
ConstDataVector::begin
iterator begin() noexcept
Return an iterator pointing at the beginning of the collection.
VertexAuxContainer.h