ATLAS Offline Software
InDetUsedInFitTrackDecoratorTool.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 // Local include(s):
7 
8 // FrameWork include(s):
9 #include "AthLinks/ElementLink.h"
13 
14 // EDM include(s):
17 
18 // STL include(s):
19 #include <algorithm>
20 #include <numeric>
21 #include <utility>
22 #include <vector>
23 
24 // Constructor
26  asg::AsgTool(name),
27  m_vtxDecoName("TTVA_AMVFVertices"),
28  m_wgtDecoName("TTVA_AMVFWeights"),
29  m_trkContKey("InDetTrackParticles"),
30  m_vtxContKey("PrimaryVertices")
31 {
32  // Property declarations
33  declareProperty("AMVFVerticesDecoName", m_vtxDecoName, "Name of the per-track AMVF vertices decoration");
34  declareProperty("AMVFWeightsDecoName", m_wgtDecoName, "Name of the per-track AMVF weights decoration");
35  declareProperty("TrackContainer", m_trkContKey, "Name of the track particle container");
36  declareProperty("VertexContainer", m_vtxContKey, "Name of the primary vertex container");
37 }
38 
39 // Destructor
41 = default;
42 
43 // Initialize
45 {
46  // Print configuration
47  ATH_MSG_DEBUG("Initializing " << name() << "...");
48  ATH_MSG_DEBUG("Using AMVFVerticesDecoName: " << m_vtxDecoName);
49  ATH_MSG_DEBUG("Using AMVFWeightsDecoName: " << m_wgtDecoName);
50  ATH_MSG_DEBUG("Using TrackContainer: " << m_trkContKey);
51  ATH_MSG_DEBUG("Using VertexContainer: " << m_vtxContKey);
52 
53  // Instantiate and initialize our container reads
54  // For tracks:
56  // For vertices:
57  ATH_CHECK(m_vtxContKey.initialize());
58 
59  // Instantiate and initialize our decorator writes
60  // For vertices:
62  this->declare(m_vtxDecoKey);
63  m_vtxDecoKey.setOwner(&(*this));
65  // For weights:
67  this->declare(m_wgtDecoKey);
68  m_wgtDecoKey.setOwner(&(*this));
70 
71  return StatusCode::SUCCESS;
72 }
73 
74 // Finalize
76 {
77  ATH_MSG_DEBUG("Finalizing " << name() << "...");
78 
79  return StatusCode::SUCCESS;
80 }
81 
83 {
84  ATH_MSG_DEBUG("In decorate(...) for " << name() << "...");
85 
86  const EventContext& ctx = Gaudi::Hive::currentContext();
87 
88  // Decor handles for the AMVF vertices and weights
91  bool isVtxDeco_available = vtxDeco.isAvailable();
92  if(isVtxDeco_available) return;
93 
94  //vector to collect results
95  std::vector<std::pair<const xAOD::Vertex*,float> > vxWithWeight;
96 
97  // Iterate over our tracks:
98  for (const xAOD::TrackParticle* trk : *trkCont) {
99  if (!trk) continue;
100 
101  vxWithWeight.clear();
102 
103  // Iterate over our vertices
104  for (const auto *const vtx : *vtxCont) {
105  if (!vtx) continue;
106 
107  //Search for vertex linked to this track
108  const auto& trkLinks=vtx->trackParticleLinks();
109  const size_t nTrackLinks=trkLinks.size();
110  for (unsigned i=0;i<nTrackLinks;++i) {
111  if (trkLinks[i].isValid() && *(trkLinks[i]) == trk) {//ptr comparison
112  vxWithWeight.emplace_back(vtx,vtx->trackWeights()[i]);
113  break; //Found pointer, quit loop
114  }
115  }
116  }//end loop over vertices
117 
118  //sort by weight
119  std::sort(vxWithWeight.begin(),vxWithWeight.end(),
120  [](std::pair<const xAOD::Vertex*,float>& a, std::pair<const xAOD::Vertex*,float>& b){ return a.second > b.second; } );
121 
122  //split vector of pairs into two vectors in sync:
123 
124  std::vector<ElementLink<xAOD::VertexContainer>> AMVFVertices;
125  std::vector<float> AMVFWeights;
126  AMVFVertices.reserve(vxWithWeight.size());
127  AMVFWeights.reserve(vxWithWeight.size());
128 
129  for (const auto& p : vxWithWeight) {
130  AMVFVertices.emplace_back(p.first,*vtxCont,ctx);
131  AMVFWeights.emplace_back(p.second);
132  }
133 
134  // Actually perform the decoration
135  vtxDeco(*trk) = std::move(AMVFVertices);
136  wgtDeco(*trk) = std::move(AMVFWeights);
137 
138  } // end, loop over tracks
139 
140 }
141 
143 {
144  const EventContext& ctx = Gaudi::Hive::currentContext();
145 
146  // Open our track container
148  if (!trkCont.isValid()) {
149  ATH_MSG_WARNING("Unable to retrieve xAOD::TrackParticleContainer, \"" << m_trkContKey.key() << "\", returning without applying decorations!");
150  return;
151  }
152 
153  // Open our vertex container
155  if (!vtxCont.isValid()) {
156  ATH_MSG_WARNING("Unable to retrieve xAOD::VertexContainer, \"" << m_vtxContKey.key() << "\", returning without applying decorations!");
157  return;
158  }
159 
160  // Perform the decoration
161  decorate(trkCont.get(), vtxCont.get());
162 
163 }
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer >
InDet::InDetUsedInFitTrackDecoratorTool::decorate
virtual void decorate() const override
Definition: InDetUsedInFitTrackDecoratorTool.cxx:142
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
SG::WriteDecorHandle::isAvailable
bool isAvailable()
Test to see if this variable exists in the store, for the referenced object.
CurrentContext.h
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
AthCommonDataStore< AthCommonMsg< AlgTool > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
InDet::InDetUsedInFitTrackDecoratorTool::m_vtxDecoName
std::string m_vtxDecoName
Name of the per-track decoration for the AMVF fit vertices.
Definition: InDetUsedInFitTrackDecoratorTool.h:83
InDet::InDetUsedInFitTrackDecoratorTool::m_vtxContKey
SG::ReadHandleKey< xAOD::VertexContainer > m_vtxContKey
Name of the primary vertex container (needed for container-less function calls)
Definition: InDetUsedInFitTrackDecoratorTool.h:92
asg
Definition: DataHandleTestTool.h:28
InDet::InDetUsedInFitTrackDecoratorTool::InDetUsedInFitTrackDecoratorTool
InDetUsedInFitTrackDecoratorTool(const std::string &name)
Constructor.
Definition: InDetUsedInFitTrackDecoratorTool.cxx:25
InDet::InDetUsedInFitTrackDecoratorTool::m_wgtDecoKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_wgtDecoKey
Per-track decorator for the AMVF fit weights.
Definition: InDetUsedInFitTrackDecoratorTool.h:105
InDet::InDetUsedInFitTrackDecoratorTool::m_wgtDecoName
std::string m_wgtDecoName
Name of the per-track decoration for the AMVF fit weights.
Definition: InDetUsedInFitTrackDecoratorTool.h:86
isValid
bool isValid(const T &p)
Definition: AtlasPID.h:214
SG::VarHandleKey::key
const std::string & key() const
Return the StoreGate ID for the referenced object.
Definition: AthToolSupport/AsgDataHandles/Root/VarHandleKey.cxx:141
lumiFormat.i
int i
Definition: lumiFormat.py:92
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
SG::ReadHandle::get
const_pointer_type get() const
Dereference the pointer, but don't cache anything.
InDet::InDetUsedInFitTrackDecoratorTool::finalize
virtual StatusCode finalize() override
Function finalizing the tool.
Definition: InDetUsedInFitTrackDecoratorTool.cxx:75
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:99
InDet::InDetUsedInFitTrackDecoratorTool::m_trkContKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trkContKey
Name of the track particle container (needed for container-less function calls)
Definition: InDetUsedInFitTrackDecoratorTool.h:89
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
DataVector< xAOD::TrackParticle_v1 >
WriteDecorHandle.h
Handle class for adding a decoration to an object.
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
InDet::InDetUsedInFitTrackDecoratorTool::~InDetUsedInFitTrackDecoratorTool
~InDetUsedInFitTrackDecoratorTool()
Destructor.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:192
InDet::InDetUsedInFitTrackDecoratorTool::m_vtxDecoKey
SG::WriteDecorHandleKey< xAOD::TrackParticleContainer > m_vtxDecoKey
Per-track decorator for the AMVF fit vertices.
Definition: InDetUsedInFitTrackDecoratorTool.h:102
plotBeamSpotMon.b
b
Definition: plotBeamSpotMon.py:77
ReadHandle.h
Handle class for reading from StoreGate.
VertexContainer.h
a
TList * a
Definition: liststreamerinfos.cxx:10
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
xAOD::TrackParticle_v1
Class describing a TrackParticle.
Definition: TrackParticle_v1.h:43
InDetUsedInFitTrackDecoratorTool.h
TrackParticleContainer.h
InDet::InDetUsedInFitTrackDecoratorTool::initialize
virtual StatusCode initialize() override
Function initialising the tool.
Definition: InDetUsedInFitTrackDecoratorTool.cxx:44