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