ATLAS Offline Software
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 
18 #include "Particle/TrackParticle.h"
20 #include "TrkTrack/Track.h"
21 #include "TrkTrack/LinkToTrack.h"
26 #include "VxVertex/VxContainer.h"
27 #include "VxVertex/VxCandidate.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),
43  m_addToVxMatched(0),
44  m_addToVxUnmatch(0),
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;
70  m_addToVxMatched = 1;
71  m_addToVxUnmatch = 1;
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;
175  if (evtStore()->contains<Trk::TrackParticleBaseCollection>(thisKey) &&
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 
python.PyKernel.retrieve
def retrieve(aClass, aKey=None)
Definition: PyKernel.py:110
LinkToTrack.h
TrackParticleBaseCollection.h
DataModel_detail::const_iterator
Const iterator class for DataVector/DataList.
Definition: DVLIterator.h:82
InDet::InDetVertexSplitter::m_priOnly
bool m_priOnly
Definition: InDetVertexSplitter.h:59
PowhegControl_ttHplus_NLO.ss
ss
Definition: PowhegControl_ttHplus_NLO.py:83
TrackParticle.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::Track
The ATLAS Track class.
Definition: Tracking/TrkEvent/TrkTrack/TrkTrack/Track.h:73
AthCommonDataStore< AthCommonMsg< Algorithm > >::declareProperty
Gaudi::Details::PropertyBase & declareProperty(Gaudi::Property< T > &t)
Definition: AthCommonDataStore.h:145
Trk::ParametersT
Dummy class used to allow special convertors to be called for surfaces owned by a detector element.
Definition: EMErrorDetail.h:25
TrackParticleBase.h
xAOD::JetInput::Track
@ Track
Definition: JetContainerInfo.h:61
InDet::InDetVertexSplitter::m_savetpb
bool m_savetpb
Definition: InDetVertexSplitter.h:60
InDet::InDetVertexSplitter::m_isMatchedOdd
bool m_isMatchedOdd
Definition: InDetVertexSplitter.h:46
AthenaPoolTestRead.sc
sc
Definition: AthenaPoolTestRead.py:27
Track.h
Trk::TrackParticleBase
Definition: TrackParticleBase.h:41
AthCommonDataStore< AthCommonMsg< Algorithm > >::evtStore
ServiceHandle< StoreGateSvc > & evtStore()
The standard StoreGateSvc (event store) Returns (kind of) a pointer to the StoreGateSvc.
Definition: AthCommonDataStore.h:85
InDet::InDetVertexSplitter::finalize
StatusCode finalize()
Finalize - delete any memory allocation from the heap.
Definition: InDetVertexSplitter.cxx:95
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:92
TrackCollection
DataVector< Trk::Track > TrackCollection
This typedef represents a collection of Trk::Track objects.
Definition: TrackCollection.h:19
InDet::InDetVertexSplitter::~InDetVertexSplitter
~InDetVertexSplitter()
Destructor - check up memory allocation delete any memory allocation on the heap.
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
InDet::InDetVertexSplitter::InDetVertexSplitter
InDetVertexSplitter(const std::string &name, ISvcLocator *pSvcLocator)
Author: Peter V.
Definition: InDetVertexSplitter.cxx:38
TrackCollection.h
InDet::InDetVertexSplitter::m_addToVxMatched
int m_addToVxMatched
Definition: InDetVertexSplitter.h:48
VxTrackAtVertex.h
InDet::InDetVertexSplitter::m_maxVtx
int m_maxVtx
Definition: InDetVertexSplitter.h:58
VxContainer.h
Trk::ParametersBase
Definition: ParametersBase.h:55
VxContainer
Definition: VxContainer.h:28
InDet::InDetVertexSplitter::execute
StatusCode execute()
Execute - on event by event.
Definition: InDetVertexSplitter.cxx:105
DataVector< Trk::Track >
AthAlgorithm
Definition: AthAlgorithm.h:47
InDet::InDetVertexSplitter::m_isUnmatchOdd
bool m_isUnmatchOdd
Definition: InDetVertexSplitter.h:47
LinkToTrackParticleBase.h
VxCandidate.h
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
InDet::InDetVertexSplitter::split_vertices
StatusCode split_vertices()
Definition: InDetVertexSplitter.cxx:122
Rec::TrackParticleContainer
Definition: Reconstruction/Particle/Particle/TrackParticleContainer.h:33
Rec::TrackParticle
Definition: Reconstruction/Particle/Particle/TrackParticle.h:47
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
InDetVertexSplitter.h
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
InDet::InDetVertexSplitter::initialize
StatusCode initialize()
Initialize initialize StoreGate.
Definition: InDetVertexSplitter.cxx:66
Trk::VxCandidate
Definition: VxCandidate.h:27
InDet::InDetVertexSplitter::m_trackKeys
std::vector< std::string > m_trackKeys
Definition: InDetVertexSplitter.h:44
Trk::TrackParticleBaseCollection
DataVector< TrackParticleBase > TrackParticleBaseCollection
Definition: TrackParticleBaseCollection.h:14
InDet::InDetVertexSplitter::m_vertexContainerName
std::string m_vertexContainerName
containers to retrieve
Definition: InDetVertexSplitter.h:55
InDet::InDetVertexSplitter::m_eventN
int m_eventN
Definition: InDetVertexSplitter.h:50
InDet::InDetVertexSplitter::m_tpbContainerName
std::string m_tpbContainerName
Definition: InDetVertexSplitter.h:56
DataVector::size
size_type size() const noexcept
Returns the number of elements in the collection.
InDet::InDetVertexSplitter::m_addToVxUnmatch
int m_addToVxUnmatch
Definition: InDetVertexSplitter.h:49
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
InDet::InDetVertexSplitter::m_trackContainerName
std::string m_trackContainerName
Definition: InDetVertexSplitter.h:57
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37
TrackParticleContainer.h