ATLAS Offline Software
Loading...
Searching...
No Matches
InDetVertexSplitter.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5
10
11#include "GaudiKernel/MsgStream.h"
12
14#include "InDetVertexSplitter.h"
15
18#include "TrkTrack/Track.h"
27
28#include <algorithm>
29#include <cmath>
30#include <map>
31#include <sstream>
32
35
37 ISvcLocator* pSvcLocator) :
38 AthAlgorithm(name, pSvcLocator),
39 m_isMatchedOdd(false),
40 m_isUnmatchOdd(false),
43 m_eventN(0){
44
46 declareProperty("TPBContainerName", m_tpbContainerName = "TrackParticleCandidate");
47 declareProperty("TrackContainerName", m_trackContainerName = "Tracks");
48 declareProperty("VertexContainerName", m_vertexContainerName = "VxPrimaryCandidate");
49 declareProperty("MaxVertexNumber", m_maxVtx = 1); //this should not be changed until a more robust handling of multi vertices is implemented
50 declareProperty("PrimaryOnly",m_priOnly = true); //this should not be changed presently
51 declareProperty("UseTrackParticleBase",m_savetpb = true); //is this needed?
52}
53
57
59
63
65
66 m_isMatchedOdd = false;
67 m_isUnmatchOdd = false;
70 m_eventN = 0;
71 std::stringstream ss;
72 for (int i = 1; i <=m_maxVtx; i++){
73 ss.str("");
74 ss << "odd_" << i << "_Tracks";
75 m_trackKeys.push_back(ss.str());
76 ss.str("");
77 ss << "even_" << i << "_Tracks";
78 m_trackKeys.push_back(ss.str());
79 ss.str("");
80 ss << "all_" << i << "_Tracks";
81 m_trackKeys.push_back(ss.str());
82 ss.str("");
83 }
84
85 ATH_MSG_INFO ("Initializing InDetVertexSplitter");
86
87 return StatusCode::SUCCESS;
88}
89
92
94 ATH_MSG_DEBUG("in finalize()");
95
96 return StatusCode::SUCCESS;
97
98}
99
102
104
105 ATH_MSG_DEBUG("in execute()");
106
107 StatusCode sc = StatusCode::SUCCESS;
108
109 sc = split_vertices();
110 if (sc.isFailure()) {
111 ATH_MSG_ERROR("InDetVertexSplitter Failed");
112 return sc;
113 }
114
115 return sc;
116}
117
119
121
122 ATH_MSG_DEBUG("in split_vertices()");
123
124 StatusCode sc = StatusCode::SUCCESS;
125
126 const VxContainer* vtxTES=nullptr;
127 sc=evtStore()->retrieve( vtxTES, m_vertexContainerName);
128 if( sc.isFailure() || !vtxTES ) {
129 ATH_MSG_WARNING("No VxContainer container found in TDS tried " << m_vertexContainerName);
130 return StatusCode::SUCCESS;
131 }
132
133 ATH_MSG_DEBUG("Vertex successfully retrieved");
134
135 const Rec::TrackParticleContainer* tpbTES=nullptr;
136 const TrackCollection* trkTES=nullptr;
137
138 if (m_savetpb){
139 sc=evtStore()->retrieve( tpbTES, m_tpbContainerName);
140 if( sc.isFailure() || !tpbTES ) {
141 ATH_MSG_WARNING("No TrackParticleBase container found in TDS tried " << m_tpbContainerName);
142 return StatusCode::SUCCESS;
143 }
144 ATH_MSG_DEBUG("TrackParticleCandidate Collection successfully retrieved");
145 }
146 else {
147 sc=evtStore()->retrieve( trkTES, m_trackContainerName);
148 if( sc.isFailure() || !trkTES ) {
149 ATH_MSG_WARNING("No TrackCollection container found in TDS tried " << m_trackContainerName);
150 return StatusCode::SUCCESS;
151 }
152 ATH_MSG_DEBUG("TrackParticleCandidate Collection successfully retrieved");
153 }
154
155 std::map<std::string,TrackCollection*> trackmap;
156 std::map<std::string,Trk::TrackParticleBaseCollection*> tpbmap;
157
158// We need to create every container for each event, even if we don't write to them
159
160 for (const auto & thisKey : m_trackKeys){
161 TrackCollection* tempTracks = nullptr;
162 trackmap[thisKey] = tempTracks;
163 if (evtStore()->contains<TrackCollection>(thisKey) && (evtStore()->retrieve(trackmap[thisKey],thisKey)).isSuccess()){
164 //nop
165 } else {
166 trackmap[thisKey] = new TrackCollection;
167 }
168 }
169
170 for (const auto & thisKey : m_trackKeys){
171 Trk::TrackParticleBaseCollection* tempTpbs = nullptr;
172 tpbmap[thisKey] = tempTpbs;
174 (evtStore()->retrieve(tpbmap[thisKey],thisKey)).isSuccess()){
175 } else {
176 tpbmap[thisKey] = new Trk::TrackParticleBaseCollection;
177 }
178 }
179
180 if (m_savetpb and tpbTES){
181 //we loop over that list
182 for (const Rec::TrackParticle* tpb: *tpbTES){
183 const Trk::TrackParameters* trkPerigee = &(tpb->definingParameters());
184 bool trackmatched = false;
185 //we compare it to the tracks already associated with vertices
186
187 ATH_MSG_DEBUG("Found "<<vtxTES->size()<<" vertices");
188
189 int i_vtx = 0;
190 // We're relying here on the implicit sorting of vertices by sqrt(N_tracks)*Sum Pt_track^2
191 // This should pick out the most interesting N vertices
192 // Hopefully we have only 1 primary vertex, but if there is > 1 we can grab all of those too
193 std::stringstream sss;
194 std::string oeNameString;
195 oeNameString.reserve(20);
196 for (const Trk::VxCandidate* vtx : *vtxTES){
197 if ( (!m_priOnly || vtx->vertexType() == 1) && (i_vtx < m_maxVtx) ){
198 i_vtx++;
199 const std::vector<Trk::VxTrackAtVertex*> & vertexTracks = *vtx->vxTrackAtVertex();
200 ATH_MSG_DEBUG("parent vertex has "<<vertexTracks.size()<<" tracks, at position: "<<vtx->recVertex().position().x());
201 std::vector<Trk::VxTrackAtVertex*>::const_iterator tavI = vertexTracks.begin();
202 std::vector<Trk::VxTrackAtVertex*>::const_iterator tavIe= vertexTracks.end();
203 for (; tavI != tavIe; ++tavI){
204 const Trk::TrackParameters* vxTrkPerigee = (*tavI)->initialPerigee();
205 if (trkPerigee == vxTrkPerigee) {trackmatched = true;}
206 }
207 Trk::TrackParticleBase *trkCopy1 = new Trk::TrackParticleBase((*tpb));
208 Trk::TrackParticleBase *trkCopy2 = new Trk::TrackParticleBase((*tpb));
209 if (!trackmatched){
210 oeNameString.clear();
211 if (m_isUnmatchOdd) oeNameString = "odd";
212 if (!m_isUnmatchOdd) oeNameString = "even";
213 sss.str("");
214 sss << oeNameString << "_" << m_addToVxUnmatch << "_Tracks";
215 std::string oecontainerName = sss.str();
216 std::string allNameString = "all";
217 sss.str("");
218 sss << allNameString << "_" << m_addToVxUnmatch << "_Tracks";
219 std::string allcontainerName = sss.str();
220 ATH_MSG_DEBUG("found an unmatched trackparticlebase, giving it the key: "<< oecontainerName);
221 tpbmap[oecontainerName]->push_back(trkCopy1);
222 ATH_MSG_DEBUG("found an unmatched trackparticlebase, giving it the key: "<< allcontainerName);
223 tpbmap[allcontainerName]->push_back(trkCopy2);
227 }
228 if (trackmatched){
229 oeNameString.clear();
230 if (m_isMatchedOdd) oeNameString = "odd";
231 if (!m_isMatchedOdd) oeNameString = "even";
232 sss.str("");
233 sss << oeNameString << "_" << m_addToVxMatched << "_Tracks";
234 std::string oecontainerName = sss.str();
235 std::string allNameString = "all";
236 sss.str("");
237 sss << allNameString << "_" << m_addToVxMatched << "_Tracks";
238 std::string allcontainerName = sss.str();
239 ATH_MSG_DEBUG("found a matched trackparticlebase, giving it the key: "<< oecontainerName);
240 tpbmap[oecontainerName]->push_back(trkCopy1);
241 ATH_MSG_DEBUG("found a matched trackparticlebase, giving it the key: "<< allcontainerName);
242 tpbmap[allcontainerName]->push_back(trkCopy2);
246 }
247 }
248 }
249 }
250 }
251 if (!m_savetpb and trkTES){
252 TrackCollection::const_iterator trkItr = trkTES->begin();
253 TrackCollection::const_iterator trkItrE = trkTES->end();
254 //we loop over that list
255 for (; trkItr != trkItrE; ++trkItr){
256 const Trk::Perigee* trkPerigee = (*trkItr)->perigeeParameters();
257 bool trackmatched = false;
258 //we compare it to the tracks already associated with vertices
259
260 VxContainer::const_iterator vtxItr = vtxTES->begin();
261 VxContainer::const_iterator vtxItrE = vtxTES->end();
262
263 ATH_MSG_DEBUG("Found "<<vtxTES->size()<<" vertices");
264
265 int i_vtx = 0;
266 // We're relying here on the implicit sorting of vertices by sqrt(N_tracks)*Sum Pt_track^2
267 // This should pick out the most interesting N vertices
268 // Hopefully we have only 1 primary vertex, but if there is > 1 we can grab all of those too
269 std::stringstream sss;
270 std::string oeNameString;
271 oeNameString.reserve(20);
272 for (; vtxItr != vtxItrE; ++vtxItr){
273 if ( (!m_priOnly || (*vtxItr)->vertexType() == 1) && (i_vtx < m_maxVtx) ){
274 i_vtx++;
275 const std::vector<Trk::VxTrackAtVertex*> & vertexTracks = (*(*vtxItr)->vxTrackAtVertex());
276 ATH_MSG_DEBUG("parent vertex has "<<vertexTracks.size()<<" tracks, at position: "<<(*vtxItr)->recVertex().position().x());
277 std::vector<Trk::VxTrackAtVertex*>::const_iterator tavI = vertexTracks.begin();
278 std::vector<Trk::VxTrackAtVertex*>::const_iterator tavIe= vertexTracks.end();
279 for (; tavI != tavIe; ++tavI){
280 const Trk::TrackParameters* vxTrkPerigee = (*tavI)->initialPerigee();
281 if (trkPerigee == vxTrkPerigee) {trackmatched = true;}
282 }
283 Trk::Track *trkCopy1 = new Trk::Track((*(*trkItr)));
284 Trk::Track *trkCopy2 = new Trk::Track((*(*trkItr)));
285 if (!trackmatched){
286 oeNameString.clear();
287 if (m_isUnmatchOdd) oeNameString = "odd";
288 if (!m_isUnmatchOdd) oeNameString = "even";
289 sss.str("");
290 sss << oeNameString << "_" << m_addToVxUnmatch << "_Tracks";
291 std::string oecontainerName = sss.str();
292 std::string allNameString = "all";
293 sss.str("");
294 sss << allNameString << "_" << m_addToVxUnmatch << "_Tracks";
295 std::string allcontainerName = sss.str();
296 ATH_MSG_DEBUG("found an unmatched track, giving it the key: "<< oecontainerName);
297 trackmap[oecontainerName]->push_back(trkCopy1);
298 ATH_MSG_DEBUG("found an unmatched track, giving it the key: "<< allcontainerName);
299 trackmap[allcontainerName]->push_back(trkCopy2);
303 }
304 if (trackmatched){
305 oeNameString.clear();
306 if (m_isMatchedOdd) oeNameString = "odd";
307 if (!m_isMatchedOdd) oeNameString = "even";
308 sss.str("");
309 sss << oeNameString << "_" << m_addToVxMatched << "_Tracks";
310 std::string oecontainerName = sss.str();
311 std::string allNameString = "all";
312 sss.str("");
313 sss << allNameString << "_" << m_addToVxMatched << "_Tracks";
314 std::string allcontainerName = sss.str();
315 ATH_MSG_DEBUG("found a matched track, giving it the key: "<< oecontainerName);
316 trackmap[oecontainerName]->push_back(trkCopy1);
317 ATH_MSG_DEBUG("found a matched track, giving it the key: "<< allcontainerName);
318 trackmap[allcontainerName]->push_back(trkCopy2);
322 }
323 }
324 }
325 }
326 }
327 if (m_savetpb){
328 for (const auto & key: m_trackKeys){
329 if(evtStore()->record(tpbmap[key],key,false).isFailure() ){
330 ATH_MSG_ERROR("Could not save the "<< key);
331 }
332 }
333 } else {
334 for (const auto & key: m_trackKeys){
335 if(evtStore()->record(trackmap[key],key,false).isFailure() ){
336 ATH_MSG_ERROR("Could not save the "<< key);
337 }
338 }
339 }
340 ATH_MSG_DEBUG("split_vertices() succeeded");
341 m_eventN++;
342 return StatusCode::SUCCESS;
343}
344
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_WARNING(x)
#define ATH_MSG_DEBUG(x)
static Double_t ss
static Double_t sc
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
AthAlgorithm(const std::string &name, ISvcLocator *pSvcLocator)
Constructor with parameters:
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T, V, H > &t)
DataModel_detail::const_iterator< DataVector > const_iterator
Definition DataVector.h:838
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
size_type size() const noexcept
Returns the number of elements in the collection.
StatusCode finalize()
Finalize - delete any memory allocation from the heap.
~InDetVertexSplitter()
Destructor - check up memory allocation delete any memory allocation on the heap.
InDetVertexSplitter(const std::string &name, ISvcLocator *pSvcLocator)
Author: Peter V.
StatusCode initialize()
Initialize initialize StoreGate.
std::string m_vertexContainerName
containers to retrieve
std::vector< std::string > m_trackKeys
StatusCode execute()
Execute - on event by event.
bool contains(const std::string &s, const std::string &regx)
does a string contain the substring
Definition hcg.cxx:114
ParametersT< TrackParametersDim, Charged, PerigeeSurface > Perigee
DataVector< TrackParticleBase > TrackParticleBaseCollection
ParametersBase< TrackParametersDim, Charged > TrackParameters