ATLAS Offline Software
GaussianDensityTestAlg.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 // GaussianDensityTestAlg.cxx
6 // Implementation file for class GaussianDensityTestAlg
7 // Author: Dave Casper <dcasper@uci.edu>
9 
10 // TrkVertexSeedFinderUtils includes
11 #include "GaussianDensityTestAlg.h"
12 
13 // STL includes
14 
15 // FrameWork includes
16 #include "Gaudi/Property.h"
17 
19 #include "xAODTracking/Vertex.h"
20 
21 #include <limits>
22 
23 namespace Trk
24 {
26 // Public methods:
28 
29 // Constructors
32  ISvcLocator* pSvcLocator ) :
33  ::AthAlgorithm( name, pSvcLocator ),
34  m_useBeamConstraint(true),
35  m_firstEvent(true),
36  m_iTHistSvc("THistSvc", name)
37 {
38  //
39  // Property declaration
40  //
41 }
42 
43 // Destructor
46 = default;
47 
48 // Athena Algorithm's Hooks
51 {
52  ATH_MSG_INFO ("Initializing " << name() << "...");
53 
55  ATH_CHECK( m_truthEventsKey.initialize() );
56  ATH_CHECK( m_pileupEventsKey.initialize() );
57 
58  ATH_CHECK( m_estimator.retrieve() );
59  ATH_CHECK( m_trackFilter.retrieve() );
60  ATH_CHECK( m_ipEstimator.retrieve() );
62  // setup histograms/trees
63  m_h_density = new TH1F("Density", "Density", 800, -200.0, 200.0);
64  m_h_truthDensity = new TH1F("Truth", "Truth", 800, -200.0, 200.0);
65  m_h_truthVertices = new TH1F("TruthVertices", "TruthVertices", 800, -200.0, 200.0);
66  m_h_modeCheck = new TH1F("ModeOffset", "ModeOffset", 100, -2.0, 2.0);
67 
68  CHECK( m_iTHistSvc->regHist("/file1/h/density", m_h_density) );
69  CHECK( m_iTHistSvc->regHist("/file1/h/truth", m_h_truthDensity) );
70  CHECK( m_iTHistSvc->regHist("/file1/h/truthvertices", m_h_truthVertices) );
71  CHECK( m_iTHistSvc->regHist("/file1/h/modeoffset", m_h_modeCheck) );
72 
73  return StatusCode::SUCCESS;
74 }
75 
77 {
78  ATH_MSG_INFO ("Finalizing " << name() << "...");
79  return StatusCode::SUCCESS;
80 }
81 
83 {
84  ATH_MSG_DEBUG ("Executing " << name() << "...");
85 
87 
88  ATH_MSG_VERBOSE("Selecting tracks");
89  std::vector<Trk::ITrackLink*> trackVector;
90  selectTracks(trackParticles.cptr(), trackVector);
91 
92  std::vector<const Trk::TrackParameters*> perigeeList;
93  analyzeTracks(trackVector, perigeeList);
94 
95  ATH_MSG_VERBOSE("Using density estimator");
96  std::unique_ptr<Trk::IVertexTrackDensityEstimator::ITrackDensity> dens;
97  double mode = m_estimator->globalMaximum (perigeeList, dens);
98 
99  if (m_firstEvent)
100  {
101  for (int i = 0; i < 800; i++)
102  {
103  double z = -200.0 + 0.25 + i*0.5;
104  double density = dens->trackDensity(z);
105  m_h_density->Fill((float) z, (float) density);
106  }
107  }
108  ATH_MSG_VERBOSE("Analyzing MC truth");
109  std::vector<Amg::Vector3D> truth;
110  ATH_CHECK( findTruth(mode, trackVector, truth, m_h_truthDensity, m_h_modeCheck) );
111  if (m_firstEvent)
112  {
113  ATH_MSG_VERBOSE("Filling truth vertex histogram");
114  for (auto& v : truth) m_h_truthVertices->Fill( v[2] );
115  }
116  m_firstEvent = false;
117  return StatusCode::SUCCESS;
118 }
119 
121 // Non-const methods:
123 
124 void GaussianDensityTestAlg::analyzeTracks(const std::vector<Trk::ITrackLink*>& trackVector,
125  std::vector<const Trk::TrackParameters*>& perigeeList)
126 {
127  for (auto *seedtrkAtVtxIter : trackVector)
128  {
129  perigeeList.push_back( seedtrkAtVtxIter->parameters() );
130  }
131 }
132 
134  std::vector<Trk::ITrackLink*>& trackVector)
135 {
136  bool selectionPassed{false};
137  const InDet::BeamSpotData* beamspot = nullptr;
140  if(beamSpotHandle.isValid()) beamspot = beamSpotHandle.retrieve();
141  }
142  for (auto itr = trackParticles->begin(); itr != trackParticles->end(); ++itr) {
143  if (m_useBeamConstraint) {
144  xAOD::Vertex beamposition;
145  beamposition.makePrivateStore();
146  beamposition.setPosition(beamspot->beamVtx().position());
147  beamposition.setCovariancePosition(beamspot->beamVtx().covariancePosition());
148  selectionPassed=static_cast<bool>(m_trackFilter->accept(**itr,&beamposition));
149  }
150  else
151  {
152  xAOD::Vertex null;
153  null.makePrivateStore();
154  null.setPosition(Amg::Vector3D(0,0,0));
155  AmgSymMatrix(3) vertexError;
156  vertexError.setZero();
157  null.setCovariancePosition(vertexError);
158  selectionPassed=static_cast<bool>(m_trackFilter->accept(**itr,&null));
159  }
160  if (selectionPassed)
161  {
163  link.setElement(*itr);
165  linkTT->setStorableObject(*trackParticles);
166  trackVector.push_back(linkTT);
167  }
168  }
169 }
170 
172 // Const methods:
176  const std::vector<Trk::ITrackLink*>& trackVector,
177  std::vector<Amg::Vector3D>& truth,
178  TH1* h_truthDensity,
179  TH1* h_modeCheck) const
180 {
181  double modeClosestDistance = std::numeric_limits<double>::max();
182 
184 
186  if ( signalEvents.isValid() )
187  {
188  ATH_MSG_VERBOSE("Found signalEvents");
189  for (const xAOD::TruthEventBase* evt : *signalEvents)
190  {
191  const xAOD::TruthVertex* vLink = *(evt->truthVertexLink(0));
192  if (vLink == nullptr){
193  ATH_MSG_ERROR("Invalid truthVertexLink from signalEvents in GaussianDensityTestAlg::findTruth");
194  return StatusCode::FAILURE;
195  }
196  Amg::Vector3D vTruth(vLink->x(),vLink->y(),vLink->z());
197  int nGoodTracks = 0;
198  for (auto *trk : trackVector)
199  {
201  if (lxtp)
202  {
203  bool isAssoc = truthParticleAssoc(**(*lxtp)).isValid();
204  if (isAssoc)
205  {
206  const auto& assocParticle = truthParticleAssoc(**(*lxtp));
207  ATH_MSG_VERBOSE("Found associated truth particle");
208  for (const auto& truthParticle : evt->truthParticleLinks())
209  {
210  if (!truthParticle.isValid()) continue;
211  if (assocParticle == truthParticle)
212  {
213  ATH_MSG_VERBOSE("Calling ipSignificance");
214  double significance = ipSignificance(trk->parameters(), &vTruth);
215  ATH_MSG_VERBOSE("ipSignificance returned " << significance);
216  if (significance <= m_significanceTruthCut)
217  {
218  ATH_MSG_VERBOSE("Adding good track");
219  nGoodTracks++;
220  const Trk::Perigee* perigee = dynamic_cast<const Trk::Perigee*>(trk->parameters());
221  if (perigee == nullptr) ATH_MSG_ERROR("Invalid Perigee");
222  if (m_firstEvent)
223  {
224  h_truthDensity->Fill(vLink->z());
225  ATH_MSG_VERBOSE("Filled truth density histogram");
226  }
227  }
228  break;
229  }
230  }
231  }
232  else
233  {
234  ATH_MSG_VERBOSE("No associated truth particle found");
235  }
236  }
237  }
238  if (nGoodTracks >= m_truthVertexTracks)
239  {
240  truth.push_back(vTruth);
241  if (abs(modeClosestDistance) > abs(mode - vTruth[2]))
242  modeClosestDistance = mode - vTruth[2];
243  }
244  }
245  }
246  else
247  {
248  ATH_MSG_WARNING("No TruthEventContainer found");
249  }
250 
252  if ( pileupEvents.isValid() )
253  {
254  ATH_MSG_VERBOSE("Found pileupEvents");
255  for (const xAOD::TruthEventBase* evt : *pileupEvents)
256  {
257  const xAOD::TruthVertex* vLink = *(evt->truthVertexLink(0));
258  if (vLink == nullptr) {
259  ATH_MSG_ERROR("Invalid truthVertexLink from pileupEvents");
260  return StatusCode::FAILURE;
261  }
262  Amg::Vector3D vTruth(vLink->x(),vLink->y(),vLink->z());
263  int nGoodTracks = 0;
264  for (auto *trk : trackVector)
265  {
267  if (lxtp)
268  {
269  bool isAssoc = truthParticleAssoc(**(*lxtp)).isValid();
270  if (isAssoc)
271  {
272  const auto& assocParticle = truthParticleAssoc(**(*lxtp));
273  ATH_MSG_VERBOSE("Found associated truth particle");
274  for (const auto& truthParticle : evt->truthParticleLinks())
275  {
276  if (!truthParticle.isValid()) continue;
277  if (assocParticle == truthParticle)
278  {
279  ATH_MSG_VERBOSE("Calling ipSignificance");
280  double significance = ipSignificance(trk->parameters(), &vTruth);
281  ATH_MSG_VERBOSE("ipSignificance returned " << significance);
282  if (significance <= m_significanceTruthCut)
283  {
284  ATH_MSG_VERBOSE("Adding good track");
285  nGoodTracks++;
286  const Trk::Perigee* perigee = dynamic_cast<const Trk::Perigee*>(trk->parameters());
287  if (perigee == nullptr) ATH_MSG_ERROR("Invalid Perigee");
288  if (m_firstEvent)
289  {
290  h_truthDensity->Fill(vLink->z());
291  ATH_MSG_VERBOSE("Filled truth density histogram");
292  }
293  }
294  break;
295  }
296  }
297  }
298  else
299  {
300  ATH_MSG_VERBOSE("No associated truth particle found");
301  }
302  }
303  }
304  if (nGoodTracks >= m_truthVertexTracks)
305  {
306  truth.push_back(vTruth);
307  if (abs(modeClosestDistance) > abs(mode - vTruth[2]))
308  modeClosestDistance = mode - vTruth[2];
309  }
310  }
311  }
312  else
313  {
314  ATH_MSG_WARNING("No TruthPileupEventContainer found");
315  }
316 
317  h_modeCheck->Fill( modeClosestDistance );
318  return StatusCode::SUCCESS;
319  }
320 
322  const Amg::Vector3D * vertex ) const
323  {
324  xAOD::Vertex v;
325  v.makePrivateStore();
326  v.setPosition(*vertex);
327  v.setCovariancePosition(AmgSymMatrix(3)::Zero(3,3));
328  v.setFitQuality(0., 0.);
329 
330  double significance = 0.0;
331  std::unique_ptr<ImpactParametersAndSigma> ipas = m_ipEstimator->estimate( params, &v );
332  if ( ipas != nullptr )
333  {
334  if ( ipas->sigmad0 > 0 && ipas->sigmaz0 > 0)
335  {
336  significance = sqrt( pow(ipas->IPd0/ipas->sigmad0,2) + pow(ipas->IPz0/ipas->sigmaz0,2) );
337  }
338  }
339  return significance;
340  }
341 }
xAOD::Vertex_v1::setPosition
void setPosition(const Amg::Vector3D &position)
Sets the 3-position.
Trk::GaussianDensityTestAlg::m_h_modeCheck
TH1 * m_h_modeCheck
Definition: GaussianDensityTestAlg.h:140
Trk::GaussianDensityTestAlg::finalize
virtual StatusCode finalize()
Definition: GaussianDensityTestAlg.cxx:76
Trk::GaussianDensityTestAlg::selectTracks
void selectTracks(const xAOD::TrackParticleContainer *trackParticles, std::vector< Trk::ITrackLink * > &trackVector)
Definition: GaussianDensityTestAlg.cxx:133
max
#define max(a, b)
Definition: cfImp.cxx:41
Trk::z
@ z
global position (cartesian)
Definition: ParamDefs.h:63
SG::ReadCondHandle
Definition: ReadCondHandle.h:44
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
SG::ReadHandle::cptr
const_pointer_type cptr()
Dereference the pointer.
SG::ReadHandle
Definition: StoreGate/StoreGate/ReadHandle.h:70
Trk::GaussianDensityTestAlg::m_h_truthVertices
TH1 * m_h_truthVertices
Definition: GaussianDensityTestAlg.h:139
Trk::ParametersT
Dummy class used to allow special convertors to be called for surfaces owned by a detector element.
Definition: EMErrorDetail.h:25
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
Trk::GaussianDensityTestAlg::m_trackFilter
ToolHandle< InDet::IInDetTrackSelectionTool > m_trackFilter
Definition: GaussianDensityTestAlg.h:110
LArG4FSStartPointFilter.evt
evt
Definition: LArG4FSStartPointFilter.py:42
SG::ConstAccessor
Helper class to provide constant type-safe access to aux data.
Definition: ConstAccessor.h:54
InDet::BeamSpotData::beamVtx
const Trk::RecVertex & beamVtx() const noexcept
Definition: BeamSpotData.h:79
Trk::IVertexTrackDensityEstimator::ITrackDensity::trackDensity
virtual double trackDensity(double z) const =0
Evaluate the density function at the specified coordinate along the beamline.
Trk::GaussianDensityTestAlg::m_useBeamConstraint
bool m_useBeamConstraint
Definition: GaussianDensityTestAlg.h:106
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
Trk::GaussianDensityTestAlg::m_estimator
ToolHandle< Trk::IVertexTrackDensityEstimator > m_estimator
Definition: GaussianDensityTestAlg.h:114
xAOD::TruthVertex_v1::y
float y() const
Vertex y displacement.
GaussianDensityTestAlg.h
Trk::GaussianDensityTestAlg::execute
virtual StatusCode execute()
Definition: GaussianDensityTestAlg.cxx:82
Trk::GaussianDensityTestAlg::analyzeTracks
static void analyzeTracks(const std::vector< Trk::ITrackLink * > &trackVector, std::vector< const Trk::TrackParameters * > &perigeeList)
Definition: GaussianDensityTestAlg.cxx:124
Trk::GaussianDensityTestAlg::m_iTHistSvc
ServiceHandle< ITHistSvc > m_iTHistSvc
Definition: GaussianDensityTestAlg.h:123
Trk::AmgSymMatrix
AmgSymMatrix(5) &GXFTrackState
Definition: GXFTrackState.h:156
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
LinkToXAODTrackParticle.h
Trk::GaussianDensityTestAlg::m_trackParticlesKey
SG::ReadHandleKey< xAOD::TrackParticleContainer > m_trackParticlesKey
Data handle keys.
Definition: GaussianDensityTestAlg.h:126
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
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition: AthMsgStreamMacros.h:29
Trk::LinkToXAODTrackParticle
Element link to XAOD TrackParticle.
Definition: LinkToXAODTrackParticle.h:33
Trk::GaussianDensityTestAlg::GaussianDensityTestAlg
GaussianDensityTestAlg()
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Preparation.mode
mode
Definition: Preparation.py:95
CHECK
#define CHECK(...)
Evaluate an expression and check for errors.
Definition: Control/AthenaKernel/AthenaKernel/errorcheck.h:422
Trk::ParametersBase
Definition: ParametersBase.h:55
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
Trk::Vertex::position
const Amg::Vector3D & position() const
return position of vertex
Definition: Vertex.cxx:72
Trk::GaussianDensityTestAlg::m_truthEventsKey
SG::ReadHandleKey< xAOD::TruthEventContainer > m_truthEventsKey
Definition: GaussianDensityTestAlg.h:129
TH1::Fill
int Fill(double)
Definition: rootspy.cxx:285
DataVector< xAOD::TrackParticle_v1 >
Vertex.h
AthAlgorithm
Definition: AthAlgorithm.h:47
SG::ReadHandle::isValid
virtual bool isValid() override final
Can the handle be successfully dereferenced?
Trk::GaussianDensityTestAlg::m_significanceTruthCut
Gaudi::Property< double > m_significanceTruthCut
Definition: GaussianDensityTestAlg.h:96
xAOD::TruthEventBase_v1
Base class describing a pile-up or signal truth event in the MC record.
Definition: TruthEventBase_v1.h:36
xAOD::TruthVertex_v1
Class describing a truth vertex in the MC record.
Definition: TruthVertex_v1.h:41
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:195
SG::CondHandleKey::initialize
StatusCode initialize(bool used=true)
SG::AuxElement::makePrivateStore
void makePrivateStore()
Create a new (empty) private store for this object.
Definition: AuxElement.cxx:172
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
Trk::GaussianDensityTestAlg::m_truthVertexTracks
Gaudi::Property< int > m_truthVertexTracks
Definition: GaussianDensityTestAlg.h:101
xAOD::TruthVertex_v1::x
float x() const
Vertex x displacement.
DataVector::end
const_iterator end() const noexcept
Return a const_iterator pointing past the end of the collection.
Trk::GaussianDensityTestAlg::findTruth
StatusCode findTruth(double mode, const std::vector< Trk::ITrackLink * > &trackVector, std::vector< Amg::Vector3D > &truth, TH1 *h_truthDensity, TH1 *h_modeCheck) const
Definition: GaussianDensityTestAlg.cxx:175
Trk::GaussianDensityTestAlg::m_beamSpotKey
SG::ReadCondHandleKey< InDet::BeamSpotData > m_beamSpotKey
Definition: GaussianDensityTestAlg.h:122
Trk::vertex
@ vertex
Definition: MeasurementType.h:21
InDet::BeamSpotData
Definition: BeamSpotData.h:21
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
Trk::GaussianDensityTestAlg::m_pileupEventsKey
SG::ReadHandleKey< xAOD::TruthPileupEventContainer > m_pileupEventsKey
Definition: GaussianDensityTestAlg.h:132
Trk::GaussianDensityTestAlg::m_h_truthDensity
TH1 * m_h_truthDensity
Definition: GaussianDensityTestAlg.h:138
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
TH1
Definition: rootspy.cxx:268
Trk::GaussianDensityTestAlg::~GaussianDensityTestAlg
virtual ~GaussianDensityTestAlg()
Destructor:
xAOD::TruthVertex_v1::z
float z() const
Vertex longitudinal distance along the beam line form the origin.
Trk::GaussianDensityTestAlg::m_ipEstimator
ToolHandle< Trk::ITrackToVertexIPEstimator > m_ipEstimator
Definition: GaussianDensityTestAlg.h:117
Trk::GaussianDensityTestAlg::m_h_density
TH1 * m_h_density
Histograms and trees.
Definition: GaussianDensityTestAlg.h:137
python.TrigEgammaMonitorHelper.TH1F
def TH1F(name, title, nxbins, bins_par2, bins_par3=None, path='', **kwargs)
Definition: TrigEgammaMonitorHelper.py:24
PowhegControl_ttFCNC_NLO.params
params
Definition: PowhegControl_ttFCNC_NLO.py:226
Trk::GaussianDensityTestAlg::m_firstEvent
bool m_firstEvent
Definition: GaussianDensityTestAlg.h:107
xAOD::Vertex_v1::setCovariancePosition
void setCovariancePosition(const AmgSymMatrix(3)&covariancePosition)
Sets the vertex covariance matrix.
Trk::v
@ v
Definition: ParamDefs.h:84
Trk::GaussianDensityTestAlg::initialize
virtual StatusCode initialize()
Definition: GaussianDensityTestAlg.cxx:50
DataVector::begin
const_iterator begin() const noexcept
Return a const_iterator pointing at the beginning of the collection.
Trk::GaussianDensityTestAlg::ipSignificance
double ipSignificance(const Trk::TrackParameters *params, const Amg::Vector3D *vertex) const
Definition: GaussianDensityTestAlg.cxx:321