ATLAS Offline Software
ZScanSeedFinder.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  ZScanSeedFinder.cxx - Description in header file
7 *********************************************************************/
8 
10 
12 
14 #include "TrkTrack/Track.h"
16 
17 //Amg
19 
20 #include <algorithm>
21 #include <cmath>
22 
23 namespace Trk
24 {
25 
26  ZScanSeedFinder::ZScanSeedFinder(const std::string& t, const std::string& n, const IInterface* p) :
27  base_class(t,n,p),
28  m_disableAllWeights(false),
29  m_constraintcutoff(9.),
30  m_constrainttemp(1.),
31  m_useLogPt(true),
32  m_minPt(400.),
33  m_usePt(false),
34  m_expPt(1.),
35  m_cacheWeights(true)
36  {
37  declareProperty("disableAllWeights", m_disableAllWeights);
38  declareProperty("constrainttemp", m_constrainttemp);
39  declareProperty("constraintcutoff", m_constraintcutoff);
40  declareProperty("UsePt", m_usePt);
41  declareProperty("ExpPt", m_expPt);
42  declareProperty("UseLogPt", m_useLogPt);
43  declareProperty("MinPt", m_minPt);
44  declareProperty("CacheWeights", m_cacheWeights);
45  }
46 
47 
49  = default;
50 
51 
53  {
55  if ( m_mode1dfinder.retrieve().isFailure() ) {
56  ATH_MSG_FATAL("Failed to retrieve tool " << m_mode1dfinder);
57  return StatusCode::FAILURE;
58  } else if ( m_IPEstimator.retrieve().isFailure() ) {
59  ATH_MSG_FATAL("Failed to retrieve tool " << m_IPEstimator);
60  } else {
61  ATH_MSG_INFO("Retrieved tools " << m_mode1dfinder << " and " << m_IPEstimator);
62  }
63 
64  if ( m_usePt && m_useLogPt )
65  {
66  ATH_MSG_FATAL("At most one of Pt and LogPt weighting may be selected");
67  return StatusCode::FAILURE;
68  }
69 
70  ATH_MSG_DEBUG("Initialize successful");
71  return StatusCode::SUCCESS;
72  }
73 
75  {
76  ATH_MSG_DEBUG("Finalize successful");
77  return StatusCode::SUCCESS;
78  }
79 
80 
81  Amg::Vector3D ZScanSeedFinder::findSeed(const std::vector<const Trk::Track*> & VectorTrk,const xAOD::Vertex * constraint) const {
82 
83  //create perigees from track list
84  std::vector<const TrackParameters*> perigeeList;
85 
86  std::vector<const Trk::Track*>::const_iterator begin=VectorTrk.begin();
87  std::vector<const Trk::Track*>::const_iterator end=VectorTrk.end();
88 
89  for (std::vector<const Trk::Track*>::const_iterator iter=begin;
90  iter!=end;++iter)
91  {
92  if (std::isnan((*iter)->perigeeParameters()->parameters()[Trk::d0]))
93  {
94  continue;
95  }
96  perigeeList.push_back((*iter)->perigeeParameters());
97  }
98 
99  //create seed from perigee list
100  return findSeed(perigeeList,constraint);
101 
102  }
103 
104  Amg::Vector3D ZScanSeedFinder::findSeed(const std::vector<const Trk::TrackParameters*> & perigeeList,const xAOD::Vertex * constraint) const {
105 
106 
107  double ZResult=0.;
108  std::vector<Trk::DoubleAndWeight> ZPositions;
109  if (m_cacheWeights) {
110  ZPositions = getPositionsCached (perigeeList, constraint);
111  }
112  else {
113  ZPositions = getPositionsUncached (perigeeList, constraint);
114  }
115 
116  if ( !ZPositions.empty() )
117  {
118  ZResult=m_mode1dfinder->getMode(ZPositions);
119  ATH_MSG_DEBUG("Resulting mean Z position found: " << ZResult);
120  }
121  else
122  {
123  ATH_MSG_DEBUG("No tracks with sufficient weight; return z position = 0");
124  }
125 
126  if (constraint)
127  {
128  return Amg::Vector3D(constraint->position().x(),constraint->position().y(),ZResult);
129  }
130  return Amg::Vector3D(0.,0.,ZResult);
131 
132  }
133 
134 
135  std::vector<Trk::DoubleAndWeight>
136  ZScanSeedFinder::getPositionsUncached (const std::vector<const Trk::TrackParameters*> & perigeeList,
137  const xAOD::Vertex * constraint) const
138  {
139  std::vector<Trk::DoubleAndWeight> ZPositions;
140 
141  for (const Trk::TrackParameters* i : perigeeList)
142  {
143  const Perigee* iTrk = dynamic_cast<const Trk::Perigee*>(i);
144  if (iTrk == nullptr)
145  {
146  ATH_MSG_WARNING("Neutrals not supported for seeding. Rejecting track...");
147  continue;
148  }
149 
150  std::pair<double, double> z0AndWeight =
151  estimateWeight (*iTrk, constraint);
152  ATH_MSG_DEBUG("Found position z: " << z0AndWeight.first <<
153  " with weight " << z0AndWeight.second);
154 
155  if (z0AndWeight.second >= 0.01)
156  {
157  ZPositions.push_back(z0AndWeight);
158  }
159  }
160 
161  return ZPositions;
162  }
163 
164 
165  std::vector<Trk::DoubleAndWeight>
166  ZScanSeedFinder::getPositionsCached (const std::vector<const Trk::TrackParameters*> & perigeeList,
167  const xAOD::Vertex * constraint) const
168  {
169  const EventContext& ctx = Gaudi::Hive::currentContext();
170  Cache& cache = *m_cache;
171 
172  Amg::Vector2D constraintkey;
173  if (constraint) {
174  constraintkey.x() = constraint->position().x();
175  constraintkey.y() = constraint->position().y();
176  }
177  else {
178  constraintkey.x() = std::numeric_limits<double>::max();
179  constraintkey.y() = std::numeric_limits<double>::min();
180  }
181 
182  std::vector<Trk::DoubleAndWeight> ZPositions;
183 
184  std::lock_guard<std::mutex> lock (cache.m_mutex);
185  if (ctx.evt() != cache.m_evt) {
186  cache.m_weightMap.clear();
187  }
188 
189  for (const Trk::TrackParameters* i : perigeeList)
190  {
191  const Perigee* iTrk = dynamic_cast<const Trk::Perigee*>(i);
192  if (iTrk == nullptr)
193  {
194  ATH_MSG_WARNING("Neutrals not supported for seeding. Rejecting track...");
195  continue;
196  }
197 
198  Cache::key_t key (*iTrk, constraintkey);
199  auto [it, flag] = cache.m_weightMap.try_emplace (key, Cache::value_t());
200  if (flag) {
201  it->second = estimateWeight (*iTrk, constraint);
202  ATH_MSG_DEBUG("Found position z: " << it->second.first <<
203  " with weight " << it->second.second);
204  }
205 
206  if (it->second.second >= 0.01)
207  {
208  ZPositions.push_back (it->second);
209  }
210  }
211 
212  return ZPositions;
213  }
214 
215 
219  std::pair<double, double>
221  const xAOD::Vertex* constraint) const
222  {
223  // protect against overflow in exponential function
224  static const double maxExpArg = log(std::numeric_limits<double>::max()/1.1);
225 
226  std::unique_ptr<const Trk::ImpactParametersAndSigma> ipas;
227  if (constraint != nullptr && constraint->covariancePosition()(0,0)!=0) {
228  ipas = std::unique_ptr<const Trk::ImpactParametersAndSigma> (m_IPEstimator->estimate (&iTrk, constraint));
229  }
230 
231  std::pair<double, double> z0AndWeight;
232  if (ipas != nullptr && ipas->sigmad0 > 0)
233  {
234  z0AndWeight.first = ipas->IPz0 + constraint->position().z();
235  double chi2IP = std::pow(ipas->IPd0/ipas->sigmad0, 2);
236  ATH_MSG_VERBOSE("d0 from tool: " << ipas->IPd0 << " error: " << ipas->sigmad0 << " chi2: " << chi2IP);
237  if ( !m_disableAllWeights )
238  {
239  z0AndWeight.second = 1./(1.+exp(std::min((chi2IP-m_constraintcutoff)/m_constrainttemp, maxExpArg)));
240  }
241  else
242  {
243  z0AndWeight.second = 1.;
244  }
245  }
246  else
247  {
248  if (constraint != nullptr) ATH_MSG_WARNING("Unable to compute impact parameter significance; setting IPWeight = 1");
249  z0AndWeight.first = iTrk.position()[Trk::z];
250  z0AndWeight.second = 1.;
251  }
252 
253  // apply pT weighting as/if configured
254  if (!m_disableAllWeights && ( m_usePt || m_useLogPt) )
255  {
256  double Pt = fabs(1./iTrk.parameters()[Trk::qOverP])*sin(iTrk.parameters()[Trk::theta]);
257  if ( m_usePt )
258  {
259  z0AndWeight.second *= std::pow(Pt, m_expPt);
260  }
261  else
262  {
263  z0AndWeight.second *= log(std::max(Pt / m_minPt, 1.001));
264  }
265  }
266 
267  return z0AndWeight;
268  }
269 
270 
271  std::vector<Amg::Vector3D> ZScanSeedFinder::findMultiSeeds(const std::vector<const Trk::Track*>& /* vectorTrk */,const xAOD::Vertex * /* constraint */) const {
272 
273  //implemented to satisfy inheritance but this algorithm only supports one seed at a time
274  ATH_MSG_WARNING("Multi-seeding requested but seed finder not able to operate in that mode, returning no seeds");
275  return std::vector<Amg::Vector3D>(0);
276 
277  }
278 
279 
280  std::vector<Amg::Vector3D> ZScanSeedFinder::findMultiSeeds(const std::vector<const Trk::TrackParameters*>& /* perigeeList */,const xAOD::Vertex * /* constraint */) const {
281 
282  //implemented to satisfy inheritance but this algorithm only supports one seed at a time
283  ATH_MSG_WARNING("Multi-seeding requested but seed finder not able to operate in that mode, returning no seeds");
284  return std::vector<Amg::Vector3D>(0);
285 
286  }
287 
288 
289 }
Trk::ZScanSeedFinder::m_expPt
double m_expPt
Definition: ZScanSeedFinder.h:143
ZScanSeedFinder.h
ATH_MSG_FATAL
#define ATH_MSG_FATAL(x)
Definition: AthMsgStreamMacros.h:34
python.PerfMonSerializer.p
def p
Definition: PerfMonSerializer.py:743
max
#define max(a, b)
Definition: cfImp.cxx:41
Trk::ZScanSeedFinder::m_usePt
bool m_usePt
Definition: ZScanSeedFinder.h:142
TrackParameters.h
Trk::z
@ z
global position (cartesian)
Definition: ParamDefs.h:63
Trk::ZScanSeedFinder::m_IPEstimator
ToolHandle< ITrackToVertexIPEstimator > m_IPEstimator
Definition: ZScanSeedFinder.h:131
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::ZScanSeedFinder::Cache::m_mutex
std::mutex m_mutex
Definition: ZScanSeedFinder.h:190
Amg::Vector2D
Eigen::Matrix< double, 2, 1 > Vector2D
Definition: GeoPrimitives.h:48
Trk::ParametersT
Dummy class used to allow special convertors to be called for surfaces owned by a detector element.
Definition: EMErrorDetail.h:25
Trk::ZScanSeedFinder::findSeed
virtual Amg::Vector3D findSeed(const std::vector< const Trk::Track * > &vectorTrk, const xAOD::Vertex *constraint=0) const override final
Finds a linearization point out of a vector of tracks and returns it as an Amg::Vector3D object.
Definition: ZScanSeedFinder.cxx:83
Trk::ZScanSeedFinder::Cache::value_t
std::pair< double, double > value_t
Definition: ZScanSeedFinder.h:149
conifer::pow
constexpr int pow(int x)
Definition: conifer.h:20
Trk::ZScanSeedFinder::m_cacheWeights
bool m_cacheWeights
Definition: ZScanSeedFinder.h:144
Trk::ZScanSeedFinder::finalize
virtual StatusCode finalize() override
Definition: ZScanSeedFinder.cxx:76
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
skel.it
it
Definition: skel.GENtoEVGEN.py:423
Trk::ZScanSeedFinder::m_eventInfoKey
SG::ReadHandleKey< xAOD::EventInfo > m_eventInfoKey
Definition: ZScanSeedFinder.h:120
Trk::ImpactParametersAndSigma::IPd0
double IPd0
Definition: ITrackToVertexIPEstimator.h:34
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
ATH_MSG_VERBOSE
#define ATH_MSG_VERBOSE(x)
Definition: AthMsgStreamMacros.h:28
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
Trk::ZScanSeedFinder::m_minPt
double m_minPt
Definition: ZScanSeedFinder.h:141
ParamDefs.h
Trk::ZScanSeedFinder::findMultiSeeds
virtual std::vector< Amg::Vector3D > findMultiSeeds(const std::vector< const Trk::Track * > &vectorTrk, const xAOD::Vertex *constraint=0) const override final
Finds full vector of linearization points from a vector of tracks and returns it as an Amg::Vector3D ...
Definition: ZScanSeedFinder.cxx:273
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
Trk::ZScanSeedFinder::m_mode1dfinder
ToolHandle< IMode1dFinder > m_mode1dfinder
Definition: ZScanSeedFinder.h:127
Track.h
Trk::ZScanSeedFinder::Cache
Definition: ZScanSeedFinder.h:147
Trk::ZScanSeedFinder::estimateWeight
std::pair< double, double > estimateWeight(const Trk::Perigee &iTrk, const xAOD::Vertex *constraint) const
Estimate z-position and weight for one track.
Definition: ZScanSeedFinder.cxx:222
GeoPrimitives.h
Trk::ZScanSeedFinder::Cache::m_evt
EventContext::ContextEvt_t m_evt
Definition: ZScanSeedFinder.h:189
lumiFormat.i
int i
Definition: lumiFormat.py:92
beamspotman.n
n
Definition: beamspotman.py:731
Trk::theta
@ theta
Definition: ParamDefs.h:72
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
master.flag
bool flag
Definition: master.py:29
Trk::ImpactParametersAndSigma::IPz0
double IPz0
Definition: ITrackToVertexIPEstimator.h:35
Trk::ZScanSeedFinder::getPositionsUncached
std::vector< Trk::DoubleAndWeight > getPositionsUncached(const std::vector< const Trk::TrackParameters * > &perigeeList, const xAOD::Vertex *constraint) const
Definition: ZScanSeedFinder.cxx:138
Trk::ZScanSeedFinder::m_constraintcutoff
float m_constraintcutoff
Definition: ZScanSeedFinder.h:138
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::ZScanSeedFinder::initialize
virtual StatusCode initialize() override
Definition: ZScanSeedFinder.cxx:54
Trk::ZScanSeedFinder::ZScanSeedFinder
ZScanSeedFinder(const std::string &t, const std::string &n, const IInterface *p)
Definition: ZScanSeedFinder.cxx:28
Trk::ParametersBase
Definition: ParametersBase.h:55
Trk::ImpactParametersAndSigma::sigmad0
double sigmad0
Definition: ITrackToVertexIPEstimator.h:37
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::ZScanSeedFinder::m_constrainttemp
float m_constrainttemp
Definition: ZScanSeedFinder.h:139
Trk::ZScanSeedFinder::Cache::m_weightMap
WeightMap_t m_weightMap
Definition: ZScanSeedFinder.h:187
min
#define min(a, b)
Definition: cfImp.cxx:40
Trk::ZScanSeedFinder::m_disableAllWeights
bool m_disableAllWeights
Definition: ZScanSeedFinder.h:137
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::ZScanSeedFinder::getPositionsCached
std::vector< Trk::DoubleAndWeight > getPositionsCached(const std::vector< const Trk::TrackParameters * > &perigeeList, const xAOD::Vertex *constraint) const
Definition: ZScanSeedFinder.cxx:168
Trk::d0
@ d0
Definition: ParamDefs.h:69
SeedFinderParamDefs.h
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
Prompt::Def::Pt
@ Pt
Definition: VarHolder.h:76
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
Trk::ZScanSeedFinder::~ZScanSeedFinder
virtual ~ZScanSeedFinder()
Trk::qOverP
@ qOverP
perigee
Definition: ParamDefs.h:73
python.CaloCondTools.log
log
Definition: CaloCondTools.py:20
Trk::ZScanSeedFinder::m_useLogPt
bool m_useLogPt
Definition: ZScanSeedFinder.h:140
declareProperty
#define declareProperty(n, p, h)
Definition: BaseFakeBkgTool.cxx:15
Trk::ZScanSeedFinder::Cache::key_t
std::pair< Trk::Perigee, Amg::Vector2D > key_t
Definition: ZScanSeedFinder.h:148
drawFromPickle.sin
sin
Definition: drawFromPickle.py:36
mapkey::key
key
Definition: TElectronEfficiencyCorrectionTool.cxx:37