ATLAS Offline Software
CrossDistancesSeedFinder.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 /*********************************************************************
6  CrossDistancesSeedFinder.cxx - Description in header file
7 *********************************************************************/
8 
9 //#define CROSSDISTANCESSEEDFINDER_DEBUG
10 
12 
14 #include "TrkTrack/Track.h"
15 
19 
22 #include <cmath>
23 
24 
25 // Would be nice to use something like Amg::distance instead.
26 // But that rounds slightly differently.
27 // Do it like this so that results are identical with the pre-MT version.
28 namespace {
29  inline double square(const double tosquare) {
30  return std::pow(tosquare,2);
31  }
32  double dist(const std::pair<Amg::Vector3D,Amg::Vector3D>& pairofpos) {
33  Amg::Vector3D diff(pairofpos.first-pairofpos.second);
34  return std::sqrt(square(diff.x())+square(diff.y())+square(diff.z()));
35  }
36 }
37 
38 
39 namespace Trk
40 {
41 
42  CrossDistancesSeedFinder::CrossDistancesSeedFinder(const std::string& t, const std::string& n, const IInterface* p) :
43  base_class(t,n,p),
44  m_useweights(true),
45  m_trackdistcutoff(0.020),
46  m_trackdistexppower(2),
47  m_constraintcutoff(9.),
48  m_constrainttemp(9.),
49  m_maximumTracksNoCut(20),
50  m_maximumDistanceCut(3.)
51  {
52  declareProperty("useweights",m_useweights);
53  declareProperty("trackdistcutoff",m_trackdistcutoff);
54  declareProperty("trackdistexppower",m_trackdistexppower);
55  declareProperty("constrainttemp",m_constrainttemp);
56  declareProperty("constraintcutoff",m_constraintcutoff);
57  declareProperty("maximumTracksNoCut",m_maximumTracksNoCut);
58  declareProperty("maximumDistanceCut",m_maximumDistanceCut);
59  }
60 
61 
63  = default;
64 
65 
67  {
68  ATH_CHECK( m_mode3dfinder.retrieve() );
69  ATH_CHECK( m_distancefinder.retrieve() );
70  return StatusCode::SUCCESS;
71  }
72 
73  Amg::Vector3D CrossDistancesSeedFinder::findSeed(const std::vector<const Trk::Track*> & VectorTrk,const xAOD::Vertex * constraint) const {
74 
75 
76  //create perigees from track list
77  std::vector<const TrackParameters*> perigeeList;
78  for (const auto *iter : VectorTrk) {
79  if (std::isnan(iter->perigeeParameters()->parameters()[Trk::d0])) {
80  continue;
81  }
82  perigeeList.push_back(iter->perigeeParameters());
83  }
84 
85  if (perigeeList.size()<2)
86  {
87  return Amg::Vector3D(0.,0.,0.);
88  }
89 
90  //create seed from perigee list
91  return findSeed(perigeeList,constraint);
92 
93  }
94 
95  Amg::Vector3D CrossDistancesSeedFinder::findSeed(const std::vector<const Trk::TrackParameters*> & perigeeList,const xAOD::Vertex * constraint) const {
96 
97  bool useCutOnDistance=false;
98  if (perigeeList.size()>m_maximumTracksNoCut)
99  {
100  useCutOnDistance=true;
101  }
102 
103  //now implement the code you already had in the standalone code...
104 
105 
106  //Calculate and cache the covariance matrix for the constraint
107  AmgSymMatrix(3) weightMatrixPositionConstraint;
108  weightMatrixPositionConstraint.setIdentity(); //very arbitrary
109  if (constraint != nullptr) {
110  weightMatrixPositionConstraint = constraint->covariancePosition().inverse();
111  }
112 
113  //Prepare the vector of points, on which the 3d mode has later to be calculated
114  std::vector<PositionAndWeight> CrossingPointsAndWeights;
115  std::vector<Amg::Vector3D> CrossingPoints;
116 
117  //Implement here SeedPointFinder algorithm, acting with the track vector....
118  const std::vector<const Trk::TrackParameters*>::const_iterator begin=perigeeList.begin();
119  const std::vector<const Trk::TrackParameters*>::const_iterator end=perigeeList.end();
120 
121  for (std::vector<const Trk::TrackParameters*>::const_iterator i=begin;i!=end-1;++i) {
122 
123  const Trk::Perigee* MyI=dynamic_cast<const Trk::Perigee*>(*i);
124  if (MyI==nullptr) {
125  ATH_MSG_WARNING( "Neutrals not supported for seeding. Rejecting this track..." );
126  continue;
127  }
128 
129  for (std::vector<const Trk::TrackParameters*>::const_iterator j=i+1;j!=end;++j) {
130 
131  const Trk::Perigee* MyJ=dynamic_cast<const Trk::Perigee*>(*j);
132  if (MyJ==nullptr) {
133  ATH_MSG_WARNING( "Neutrals not supported for seeding. Rejecting this track..." );
134  continue;
135  }
136 
137 #ifdef CROSSDISTANCESSEEDFINDER_DEBUG
138 
139  ATH_MSG_DEBUG( "Track 1 d0: " << MyI->parameters()[Trk::d0] << " Track2 d0: " << MyJ->parameters()[Trk::d0] );
140  ATH_MSG_DEBUG( "Track 1 z0: " << MyI->parameters()[Trk::z0] << "Track2 z0: " << MyJ->parameters()[Trk::z0] );
141 #endif
142 
143  try {
144 
145  std::optional<ITrkDistanceFinder::TwoPoints> result
146  = m_distancefinder->CalculateMinimumDistance(*MyI,*MyJ);
147  if (!result) { ATH_MSG_DEBUG( "Problem with distance finder: THIS POINT WILL BE SKIPPED!" );
148  }
149  else
150  {
151  //Get the points which connect the minimum distance between the two tracks
152  double distance = dist (result.value());
153 #ifdef CROSSDISTANCESSEEDFINDER_DEBUG
154  ATH_MSG_DEBUG( "Point 1 x: " << result->first.x() <<
155  " y: " << result->first.y() <<
156  " z: " << result->first.z() );
157  ATH_MSG_DEBUG( "Point 2 x: " << result->second.x() <<
158  " y: " << result->second.y() <<
159  " z: " << result->second.z() );
160  ATH_MSG_DEBUG( "distance is: " << distance );
161 #endif
162 
163  Amg::Vector3D thepoint((result->first+result->second)/2.);
164 
165  if (m_useweights)
166  {
167  PositionAndWeight thispoint(thepoint,
169 
170  ATH_MSG_DEBUG( "distance weight: " << 1./pow(m_trackdistcutoff+distance,m_trackdistexppower) );
171 
172  if (constraint!=nullptr) {
173 
174  Amg::Vector3D DeltaP(thepoint-constraint->position());
175  Amg::Vector3D DeltaPConv;
176  ATH_MSG_DEBUG( "position x: " << DeltaP.x() << "position y: " << DeltaP.y() << "position z: " << DeltaP.z() );
177  DeltaPConv[0]=DeltaP.x();
178  DeltaPConv[1]=DeltaP.y();
179  DeltaPConv[2]=DeltaP.z();
180 
181 
182  double chi2=DeltaPConv.transpose()*weightMatrixPositionConstraint*DeltaPConv;
183 
184  ATH_MSG_DEBUG( " chi: " << chi2
185  << " beam weight " << 1./(1.+exp((chi2-m_constraintcutoff)/m_constrainttemp)) );
186 
187  thispoint.second=thispoint.second*1./(1.+exp((chi2-m_constraintcutoff)/m_constrainttemp));
188 
189  }
190  if ((!useCutOnDistance || distance<m_maximumDistanceCut) && thispoint.second > 1e-10)
191  {
192  CrossingPointsAndWeights.push_back(thispoint);
193  }
194  }
195  else
196  {
197  const Amg::Vector3D& thispoint(thepoint);
198  if (!useCutOnDistance || distance<m_maximumDistanceCut)
199  {
200  CrossingPoints.push_back(thispoint);
201  }
202  }
203  }
204  } catch (...) {
205  ATH_MSG_ERROR( "Something wrong in distance calculation: please report..." );
206  }
207  }
208  //to be understood...
209  }
210 
211  //Now all points have been collected (N*(N-1)/2) and
212  //the mode has to be calculated
213 
214  if (CrossingPoints.empty() && CrossingPointsAndWeights.empty())
215  {
216  return Amg::Vector3D(0,0,0);
217  }
218 
219  Amg::Vector3D myresult;
220 
221  if (m_useweights)
222  {
223  myresult=m_mode3dfinder->getMode(0, 0, CrossingPointsAndWeights);
224  }
225  else
226  {
227  myresult=m_mode3dfinder->getMode(0, 0, CrossingPoints);
228  }
229 
230 #ifdef CROSSDISTANCESSEEDFINDER_DEBUG
231  ATH_MSG_INFO( "Resulting mean POINT FOUND: x: " << myresult.x() <<
232  " y: " << myresult.y() <<
233  " z: " << myresult.z() );
234 #endif
235 
236 
237  return myresult;
238 
239 
240 
241 
242  }
243 
244  std::vector<Amg::Vector3D> CrossDistancesSeedFinder::findMultiSeeds(const std::vector<const Trk::Track*>& /* vectorTrk */,const xAOD::Vertex * /* constraint */) const {
245 
246  //implemented to satisfy inheritance but this algorithm only supports one seed at a time
247  ATH_MSG_WARNING( "Multi-seeding requested but seed finder not able to operate in that mode, returning no seeds" );
248  return std::vector<Amg::Vector3D>(0);
249 
250  }
251 
252  std::vector<Amg::Vector3D> CrossDistancesSeedFinder::findMultiSeeds(const std::vector<const Trk::TrackParameters*>& /* perigeeList */,const xAOD::Vertex * /* constraint */) const {
253 
254  //implemented to satisfy inheritance but this algorithm only supports one seed at a time
255  ATH_MSG_WARNING( "Multi-seeding requested but seed finder not able to operate in that mode, returning no seeds" );
256  return std::vector<Amg::Vector3D>(0);
257 
258  }
259 
260 
261 }
Trk::CrossDistancesSeedFinder::m_trackdistcutoff
float m_trackdistcutoff
Definition: CrossDistancesSeedFinder.h:102
Trk::CrossDistancesSeedFinder::m_distancefinder
ToolHandle< ITrkDistanceFinder > m_distancefinder
Definition: CrossDistancesSeedFinder.h:112
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
get_generator_info.result
result
Definition: get_generator_info.py:21
TrackParameters.h
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
Trk::CrossDistancesSeedFinder::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: CrossDistancesSeedFinder.cxx:73
Trk::CrossDistancesSeedFinder::m_maximumDistanceCut
double m_maximumDistanceCut
Definition: CrossDistancesSeedFinder.h:107
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
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
mc.diff
diff
Definition: mc.SFGenPy8_MuMu_DD.py:14
Trk::z0
@ z0
Definition: ParamDefs.h:64
xAOD::Vertex_v1::position
const Amg::Vector3D & position() const
Returns the 3-pos.
read_hist_ntuple.t
t
Definition: read_hist_ntuple.py:5
drawFromPickle.exp
exp
Definition: drawFromPickle.py:36
Trk::CrossDistancesSeedFinder::~CrossDistancesSeedFinder
virtual ~CrossDistancesSeedFinder()
ParamDefs.h
Trk::CrossDistancesSeedFinder::initialize
virtual StatusCode initialize() override
Definition: CrossDistancesSeedFinder.cxx:66
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
Track.h
Trk::AmgSymMatrix
AmgSymMatrix(5) &GXFTrackState
Definition: GXFTrackState.h:156
Trk::PositionAndWeight
std::pair< Amg::Vector3D, double > PositionAndWeight
Definition: SeedFinderParamDefs.h:17
GeoPrimitives.h
python.utils.AtlRunQueryDQUtils.p
p
Definition: AtlRunQueryDQUtils.py:210
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
lumiFormat.i
int i
Definition: lumiFormat.py:85
beamspotman.n
n
Definition: beamspotman.py:731
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
chi2
double chi2(TH1 *h0, TH1 *h1)
Definition: comparitor.cxx:522
Trk::CrossDistancesSeedFinder::m_trackdistexppower
int m_trackdistexppower
Definition: CrossDistancesSeedFinder.h:103
ATH_CHECK
#define ATH_CHECK
Definition: AthCheckMacros.h:40
Trk::CrossDistancesSeedFinder::m_constrainttemp
float m_constrainttemp
Definition: CrossDistancesSeedFinder.h:105
Trk::CrossDistancesSeedFinder::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: CrossDistancesSeedFinder.cxx:244
Trk::CrossDistancesSeedFinder::m_useweights
bool m_useweights
Definition: CrossDistancesSeedFinder.h:101
EventPrimitives.h
Trk
Ensure that the ATLAS eigen extensions are properly loaded.
Definition: FakeTrackBuilder.h:9
Trk::d0
@ d0
Definition: ParamDefs.h:63
SeedFinderParamDefs.h
Trk::CrossDistancesSeedFinder::CrossDistancesSeedFinder
CrossDistancesSeedFinder(const std::string &t, const std::string &n, const IInterface *p)
Definition: CrossDistancesSeedFinder.cxx:42
Amg::Vector3D
Eigen::Matrix< double, 3, 1 > Vector3D
Definition: GeoPrimitives.h:47
xAOD::Vertex_v1
Class describing a Vertex.
Definition: Vertex_v1.h:42
ATH_MSG_WARNING
#define ATH_MSG_WARNING(x)
Definition: AthMsgStreamMacros.h:32
GeoPrimitivesHelpers.h
Trk::CrossDistancesSeedFinder::m_maximumTracksNoCut
unsigned int m_maximumTracksNoCut
Definition: CrossDistancesSeedFinder.h:106
CrossDistancesSeedFinder.h
Trk::CrossDistancesSeedFinder::m_mode3dfinder
ToolHandle< IMode3dFinder > m_mode3dfinder
Definition: CrossDistancesSeedFinder.h:109
Amg::distance
float distance(const Amg::Vector3D &p1, const Amg::Vector3D &p2)
calculates the distance between two point in 3D space
Definition: GeoPrimitivesHelpers.h:54
Trk::CrossDistancesSeedFinder::m_constraintcutoff
float m_constraintcutoff
Definition: CrossDistancesSeedFinder.h:104