ATLAS Offline Software
InDetPriVxFinder.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 /***************************************************************************
6  InDetPriVxFinder.cxx - Description
7  -------------------
8  begin : 28-01-2004
9  authors : Andreas Wildauer (CERN PH-ATC), Fredrik Akesson (CERN PH-ATC)
10  email : andreas.wildauer@cern.ch, fredrik.akesson@cern.ch
11  changes :
12  ***************************************************************************/
14 #include "xAODTracking/Vertex.h"
18 
19 // normal includes
21 
22 namespace InDet
23 {
24 
26  (const std::string& name,ISvcLocator* pSvcLocator) : AthReentrantAlgorithm(name, pSvcLocator)
27  { }
28 
29 
31  {
32  /* Get the VertexFinderTool */
33  if ( m_VertexFinderTool.retrieve().isFailure() )
34  {
35  ATH_MSG_FATAL("Failed to retrieve tool " << m_VertexFinderTool);
36  return StatusCode::FAILURE;
37  }
38  ATH_MSG_DEBUG("Retrieved tool " << m_VertexFinderTool);
39 
40  /*Get the Vertex Merging Tool*/
41  if (m_doVertexMerging) {
42  if ( m_VertexMergingTool.retrieve().isFailure() )
43  {
44  ATH_MSG_FATAL("Failed to retrieve tool " << m_VertexMergingTool);
45  return StatusCode::FAILURE;
46  }
47  ATH_MSG_DEBUG("Retrieved tool " << m_VertexMergingTool);
48  } else {
49  m_VertexMergingTool.disable();
50  }
51 
52  /*Get the Vertex Collection Sorting Tool*/
53  if (m_doVertexSorting) {
54  if ( m_VertexCollectionSortingTool.retrieve().isFailure() )
55  {
56  ATH_MSG_FATAL("Failed to retrieve tool " << m_VertexCollectionSortingTool);
57  return StatusCode::FAILURE;
58  }
59  else
60  {
61  ATH_MSG_VERBOSE("Retrieved tool " << m_VertexCollectionSortingTool);
62  }
63  } else {
64  m_VertexCollectionSortingTool.disable();
65  }
66  if (!m_monTool.empty()) CHECK(m_monTool.retrieve());
67 
68  ATH_CHECK(m_trkTracksName.initialize(!m_useTrackParticles));
69  ATH_CHECK(m_tracksName.initialize(m_useTrackParticles));
70  ATH_CHECK(m_vxCandidatesOutputName.initialize());
71  return StatusCode::SUCCESS;
72  }
73 
74 
75  StatusCode InDetPriVxFinder::execute(const EventContext& ctx) const
76  {
77 
78  SG::WriteHandle<xAOD::VertexContainer> outputVertices (m_vxCandidatesOutputName, ctx);
79 
80  xAOD::VertexContainer* vertexContainer = nullptr;
81  xAOD::VertexAuxContainer* vertexAuxContainer = nullptr;
82  std::pair< xAOD::VertexContainer*, xAOD::VertexAuxContainer* > vertexContainerPair
83  = std::make_pair( vertexContainer, vertexAuxContainer );
84 
85  if(m_useTrackParticles){
86  SG::ReadHandle<xAOD::TrackParticleContainer> trackParticleCollection(m_tracksName, ctx);
87  if(trackParticleCollection.isValid()){
88 
89  vertexContainerPair = m_VertexFinderTool->findVertex ( trackParticleCollection.cptr() );
90  }
91  else{
92  ATH_MSG_ERROR("No TrackParticle Collection with key "<<m_tracksName.key()<<" exists in StoreGate. No Vertexing Possible");
93  return StatusCode::FAILURE;
94  }
95  } else {
96  SG::ReadHandle<TrackCollection> trackCollection(m_trkTracksName, ctx);
97  if(trackCollection.isValid()){
98  vertexContainerPair = m_VertexFinderTool->findVertex ( trackCollection.cptr() );
99  } else {
100  ATH_MSG_ERROR("No Trk::Track Collection with key "<<m_trkTracksName.key()<<" exists in StoreGate. No Vertexing Possible");
101  return StatusCode::FAILURE;
102  }
103  }
104 
105  // now re-merge and resort the vertex container and store to SG
106  std::pair<xAOD::VertexContainer*, xAOD::VertexAuxContainer* >
107  myVertexContainerPair{nullptr, nullptr};
108  auto deletePair = [](std::pair<xAOD::VertexContainer*, xAOD::VertexAuxContainer* > &p){
109  delete p.first; p.first = nullptr;
110  delete p.second; p.second = nullptr;
111  };
112 
113  if (vertexContainerPair.first) {
114  //sort xAOD::Vertex container
115  if(m_doVertexMerging && vertexContainerPair.first->size() > 1) {
116  myVertexContainerPair = m_VertexMergingTool->mergeVertexContainer( *vertexContainerPair.first );
117  deletePair(vertexContainerPair); //also cleans up the aux store
118  vertexContainerPair = myVertexContainerPair;
119  }
120 
121  if (m_doVertexSorting) {
122  myVertexContainerPair = m_VertexCollectionSortingTool->sortVertexContainer(*vertexContainerPair.first);
123  deletePair(vertexContainerPair);
124  }
125 
126  if (myVertexContainerPair.first == nullptr) {
127  ATH_MSG_ERROR("Vertex container has no associated store.");
128  return StatusCode::FAILURE;
129  }
130 
131  if (not myVertexContainerPair.first->hasStore()) {
132  ATH_MSG_ERROR("Vertex container has no associated store.");
133  return StatusCode::FAILURE;
134  }
135 
136  ATH_MSG_DEBUG("Successfully reconstructed " << myVertexContainerPair.first->size()-1 << " vertices (excluding dummy)");
137  }
138 
139  ATH_CHECK(outputVertices.record(std::unique_ptr<xAOD::VertexContainer>(myVertexContainerPair.first),std::unique_ptr<xAOD::VertexAuxContainer>(myVertexContainerPair.second)));
140 
141  auto NVertices = Monitored::Scalar<int>( "NVertices" , 0 );
142  for ( xAOD::VertexContainer::iterator vertexIter = myVertexContainerPair.first->begin();
143  vertexIter != myVertexContainerPair.first->end(); ++vertexIter ) {
144  if((*vertexIter)->nTrackParticles() > 0 and (*vertexIter)->vertexType() != 0 ){
145  NVertices++;
146  monitor_vertex( "allVertex", **vertexIter);
147  //This expects that vertices are already sorted by SumpT(or different criteria)!!!
148  if( vertexIter == myVertexContainerPair.first->begin() ) monitor_vertex( "primVertex", **vertexIter);
149  }
150  }
151  auto mon = Monitored::Group( m_monTool, NVertices);
152 
153  return StatusCode::SUCCESS;
154  }
155 
157  {
158  return StatusCode::SUCCESS;
159  }
160 
161  void InDetPriVxFinder::monitor_vertex( const std::string &prefix, const xAOD::Vertex& vertex ) const {
162  if (prefix == "allVertex"){
163  auto x = Monitored::Scalar<double>( "allVertexX", vertex.x() );
164  auto y = Monitored::Scalar<double>( "allVertexY", vertex.y() );
165  auto z = Monitored::Scalar<double>( "allVertexZ", vertex.z() );
166  auto chi2 = Monitored::Scalar<double>( "allVertexChi2", vertex.chiSquared() );
167  auto nDoF = Monitored::Scalar<double>( "allVertexnDoF", vertex.numberDoF() );
168  auto NTracks = Monitored::Scalar<int> ( "allVertexNTracks", vertex.nTrackParticles() );
169  auto mon = Monitored::Group(m_monTool, x, y, z, chi2, nDoF, NTracks );
170  }
171  else if (prefix == "primVertex"){
172  auto x = Monitored::Scalar<double>( "primVertexX", vertex.x() );
173  auto y = Monitored::Scalar<double>( "primVertexY", vertex.y() );
174  auto z = Monitored::Scalar<double>( "primVertexZ", vertex.z() );
175  auto chi2 = Monitored::Scalar<double>( "primVertexChi2", vertex.chiSquared() );
176  auto nDoF = Monitored::Scalar<double>( "primVertexnDoF", vertex.numberDoF() );
177  auto NTracks = Monitored::Scalar<int> ( "primVertexNTracks", vertex.nTrackParticles() );
178  auto mon = Monitored::Group(m_monTool, x, y, z, chi2, nDoF, NTracks );
179  }
180  }
181 
182 } // end namespace InDet
Trk::y
@ y
Definition: ParamDefs.h:56
TrackParticleBaseCollection.h
python.tests.PyTestsLib.finalize
def finalize(self)
_info( "content of StoreGate..." ) self.sg.dump()
Definition: PyTestsLib.py:50
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
TrigDefs::Group
Group
Properties of a chain group.
Definition: GroupProperties.h:13
xAOD::VertexAuxContainer_v1
Temporary container used until we have I/O for AuxStoreInternal.
Definition: VertexAuxContainer_v1.h:32
Trk::z
@ z
global position (cartesian)
Definition: ParamDefs.h:57
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
InDet
Primary Vertex Finder.
Definition: VP1ErrorUtils.h:36
initialize
void initialize()
Definition: run_EoverP.cxx:894
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
AthReentrantAlgorithm
An algorithm that can be simultaneously executed in multiple threads.
Definition: AthReentrantAlgorithm.h:83
LArG4FSStartPointFilterLegacy.execute
execute
Definition: LArG4FSStartPointFilterLegacy.py:20
InDet::InDetPriVxFinder::InDetPriVxFinder
InDetPriVxFinder(const std::string &name, ISvcLocator *pSvcLocator)
Definition: InDetPriVxFinder.cxx:33
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
TrackParticleAuxContainer.h
DataModel_detail::iterator
(Non-const) Iterator class for DataVector/DataList.
Definition: DVLIterator.h:184
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
InDetPriVxFinder.h
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
checkCorrelInHIST.prefix
dictionary prefix
Definition: checkCorrelInHIST.py:391
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
DataVector
Derived DataVector<T>.
Definition: DataVector.h:581
Vertex.h
Monitored.h
Header file to be included by clients of the Monitored infrastructure.
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:221
TrackParticle.h
SG::WriteHandle
Definition: StoreGate/StoreGate/WriteHandle.h:76
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
plotBeamSpotMon.mon
mon
Definition: plotBeamSpotMon.py:67
Monitored::Scalar
Declare a monitored scalar variable.
Definition: MonitoredScalar.h:34
Trk::x
@ x
Definition: ParamDefs.h:55