CMS 3D CMS Logo

GsfTrackProducerBase.cc
Go to the documentation of this file.
2 // system include files
3 #include <memory>
4 // user include files
20 
22 
24 
26 
29 
30 void
32  const Propagator* prop,
33  const MeasurementTracker* measTk,
34  std::unique_ptr<TrackingRecHitCollection>& selHits,
35  std::unique_ptr<reco::GsfTrackCollection>& selTracks,
36  std::unique_ptr<reco::TrackExtraCollection>& selTrackExtras,
37  std::unique_ptr<reco::GsfTrackExtraCollection>& selGsfTrackExtras,
38  std::unique_ptr<std::vector<Trajectory> >& selTrajectories,
39  AlgoProductCollection& algoResults,
40  TransientTrackingRecHitBuilder const * hitBuilder,
41  const reco::BeamSpot& bs, const TrackerTopology *ttopo)
42 {
43 
48 
52  edm::Ref< std::vector<Trajectory> >::key_type iTjRef = 0;
53  std::map<unsigned int, unsigned int> tjTkMap;
54 
55  TSCBLBuilderNoMaterial tscblBuilder;
56 
57  for(auto & i : algoResults){
58  Trajectory * theTraj = i.trajectory;
59  if(trajectoryInEvent_) {
60  selTrajectories->push_back(*theTraj);
61  iTjRef++;
62  }
63 
64  reco::GsfTrack * theTrack = i.track;
65  PropagationDirection seedDir = i.pDir;
66 
67  LogDebug("TrackProducer") << "In GsfTrackProducerBase::putInEvt - seedDir=" << seedDir;
68 
69  reco::GsfTrack t = * theTrack;
70  selTracks->push_back( t );
71  iTkRef++;
72 
73  // Store indices in local map (starts at 0)
74  if(trajectoryInEvent_) tjTkMap[iTjRef-1] = iTkRef-1;
75 
76  //sets the outermost and innermost TSOSs
77  TrajectoryStateOnSurface outertsos;
78  TrajectoryStateOnSurface innertsos;
79  unsigned int innerId, outerId;
80 
81  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
82  // This is consistent with innermost and outermost labels only for tracks from LHC collision
83  if (theTraj->direction() == alongMomentum) {
84  outertsos = theTraj->lastMeasurement().updatedState();
85  innertsos = theTraj->firstMeasurement().updatedState();
86  outerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
87  innerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
88  } else {
89  outertsos = theTraj->firstMeasurement().updatedState();
90  innertsos = theTraj->lastMeasurement().updatedState();
91  outerId = theTraj->firstMeasurement().recHit()->geographicalId().rawId();
92  innerId = theTraj->lastMeasurement().recHit()->geographicalId().rawId();
93  }
94  //build the TrackExtra
95  GlobalPoint v = outertsos.globalParameters().position();
96  GlobalVector p = outertsos.globalParameters().momentum();
97  math::XYZVector outmom( p.x(), p.y(), p.z() );
98  math::XYZPoint outpos( v.x(), v.y(), v.z() );
99  v = innertsos.globalParameters().position();
100  p = innertsos.globalParameters().momentum();
101  math::XYZVector inmom( p.x(), p.y(), p.z() );
102  math::XYZPoint inpos( v.x(), v.y(), v.z() );
103 
104  reco::TrackExtraRef teref= reco::TrackExtraRef ( rTrackExtras, idx ++ );
105  reco::GsfTrack & track = selTracks->back();
106  track.setExtra( teref );
107 
108  //======= I want to set the second hitPattern here =============
109  if (theSchool.isValid())
110  {
112  evt.getByToken(mteSrc_, mte);
113  // NavigationSetter setter( *theSchool );
114  setSecondHitPattern(theTraj,track,prop,&*mte, ttopo);
115  }
116  //==============================================================
117 
118  selTrackExtras->push_back( reco::TrackExtra (outpos, outmom, true, inpos, inmom, true,
119  outertsos.curvilinearError(), outerId,
120  innertsos.curvilinearError(), innerId,
121  seedDir, theTraj->seedRef()));
122 
123 
124  reco::TrackExtra & tx = selTrackExtras->back();
125 
126  // --- NOTA BENE: the convention is to sort hits and measurements "along the momentum".
127  // This is consistent with innermost and outermost labels only for tracks from LHC collisions
128  reco::TrackExtra::TrajParams trajParams;
130  Traj2TrackHits t2t;
131  auto ih = selHits->size();
132  t2t(*theTraj,*selHits,trajParams,chi2s);
133  auto ie = selHits->size();
134  tx.setHits(rHits,ih,ie-ih);
135  tx.setTrajParams(std::move(trajParams),std::move(chi2s));
136  for (;ih<ie; ++ih) {
137  auto const & hit = (*selHits)[ih];
138  track.appendHitPattern(hit, *ttopo);
139  }
140 
141  // ----
142 
143  std::vector<reco::GsfTangent> tangents;
144  const Trajectory::DataContainer& measurements = theTraj->measurements();
145  if ( measurements.size()>2 ) {
146  tangents.reserve(measurements.size()-2);
147  Trajectory::DataContainer::const_iterator ibegin,iend;
148  int increment(0);
149  if (theTraj->direction() == alongMomentum) {
150  ibegin = measurements.begin() + 1;
151  iend = measurements.end() - 1;
152  increment = 1;
153  }
154  else {
155  ibegin = measurements.end() - 2;
156  iend = measurements.begin();
157  increment = -1;
158  }
160  math::XYZVector momentum;
161  Measurement1D deltaP;
162  // only measurements on "mono" detectors
163  for ( Trajectory::DataContainer::const_iterator i=ibegin;
164  i!=iend; i+=increment ) {
165  if ( i->recHit().get() ) {
166  DetId detId(i->recHit()->geographicalId());
167  if ( detId.det()==DetId::Tracker ) {
168  int subdetId = detId.subdetId();
169  if ( subdetId==SiStripDetId::TIB || subdetId==SiStripDetId::TID ||
170  subdetId==SiStripDetId::TOB || subdetId==SiStripDetId::TEC ) {
171  if ( SiStripDetId(detId).stereo() ) continue;
172  }
173  }
174  }
175  bool valid = computeModeAtTM(*i,position,momentum,deltaP);
176  if ( valid ) {
177  tangents.push_back(reco::GsfTangent(position,momentum,deltaP));
178  }
179  }
180  }
181 
182 
183  //build the GsfTrackExtra
184  std::vector<reco::GsfComponent5D> outerStates;
185  fillStates(outertsos,outerStates);
186  std::vector<reco::GsfComponent5D> innerStates;
187  fillStates(innertsos,innerStates);
188 
189 
190  reco::GsfTrackExtraRef terefGsf = reco::GsfTrackExtraRef ( rGsfTrackExtras, idxGsf ++ );
191  track.setGsfExtra( terefGsf );
192  selGsfTrackExtras->push_back( reco::GsfTrackExtra (outerStates, outertsos.localParameters().pzSign(),
193  innerStates, innertsos.localParameters().pzSign(),
194  tangents));
195 
196  if ( innertsos.isValid() ) {
198  TransverseImpactPointExtrapolator tipExtrapolator(gsfProp);
199  fillMode(track,innertsos,gsfProp,tipExtrapolator,tscblBuilder,bs);
200  }
201 
202  delete theTrack;
203  delete theTraj;
204  }
205 
206  LogTrace("TrackingRegressionTest") << "========== TrackProducer Info ===================";
207  LogTrace("TrackingRegressionTest") << "number of finalGsfTracks: " << selTracks->size();
208  for (reco::GsfTrackCollection::const_iterator it = selTracks->begin(); it != selTracks->end(); it++) {
209  LogTrace("TrackingRegressionTest") << "track's n valid and invalid hit, chi2, pt : "
210  << it->found() << " , "
211  << it->lost() <<" , "
212  << it->normalizedChi2() << " , "
213  << it->pt() << " , "
214  << it->eta() ;
215  }
216  LogTrace("TrackingRegressionTest") << "=================================================";
217 
218 
219  rTracks_ = evt.put(std::move(selTracks) );
220  evt.put(std::move(selTrackExtras) );
221  evt.put(std::move(selGsfTrackExtras) );
222  evt.put(std::move(selHits) );
223 
224  if(trajectoryInEvent_) {
225  edm::OrphanHandle<std::vector<Trajectory> > rTrajs = evt.put(std::move(selTrajectories));
226 
227  // Now Create traj<->tracks association map
228  std::unique_ptr<TrajGsfTrackAssociationCollection> trajTrackMap( new TrajGsfTrackAssociationCollection(rTrajs, rTracks_) );
229  for ( std::map<unsigned int, unsigned int>::iterator i = tjTkMap.begin();
230  i != tjTkMap.end(); i++ ) {
231  edm::Ref<std::vector<Trajectory> > trajRef( rTrajs, (*i).first );
232  edm::Ref<reco::GsfTrackCollection> tkRef( rTracks_, (*i).second );
233  trajTrackMap->insert( edm::Ref<std::vector<Trajectory> >( rTrajs, (*i).first ),
235  }
236  evt.put( std::move(trajTrackMap) );
237  }
238 }
239 
240 void
242  std::vector<reco::GsfComponent5D>& states) const
243 {
246  GetComponents comps(tsos);
247  auto const & components = comps();
248  states.reserve(components.size());
249  for (auto const & st : components)
250  states.emplace_back(st.weight(),st.localParameters().vector(),st.localError().matrix());
251 }
252 
253 void
255  const Propagator& gsfProp,
256  const TransverseImpactPointExtrapolator& tipExtrapolator,
258  const reco::BeamSpot& bs) const
259 {
260  // Get transverse impact parameter plane (from mean). This is a first approximation;
261  // the mode is then extrapolated to the
262  // final position closest to the beamline.
263  GlobalPoint bsPos(bs.position().x()+(track.vz()-bs.position().z())*bs.dxdz(),
264  bs.position().y()+(track.vz()-bs.position().z())*bs.dydz(),
265  track.vz());
266  TrajectoryStateOnSurface vtxTsos = tipExtrapolator.extrapolate(innertsos,bsPos);
267  if ( !vtxTsos.isValid() ) vtxTsos = innertsos;
268  // extrapolate mixture
269  vtxTsos = gsfProp.propagate(innertsos,vtxTsos.surface());
270  if ( !vtxTsos.isValid() ) return; // failed (GsfTrack keeps mode = mean)
271  // extract mode
272  // build perigee parameters (for covariance to be stored)
273  AlgebraicVector5 modeParameters;
274  AlgebraicSymMatrix55 modeCovariance;
275  // set parameters and variances for "mode" state (local parameters)
276  for ( unsigned int iv=0; iv<5; ++iv ) {
278  GaussianSumUtilities1D utils(state1D);
279  modeParameters(iv) = utils.mode().mean();
280  modeCovariance(iv,iv) = utils.mode().variance();
281  if ( !utils.modeIsValid() ) {
282  // if mode calculation fails: use mean
283  modeParameters(iv) = utils.mean();
284  modeCovariance(iv,iv) = utils.variance();
285  }
286  }
287  // complete covariance matrix
288  // approximation: use correlations from mean
289  const AlgebraicSymMatrix55& meanCovariance(vtxTsos.localError().matrix());
290  for ( unsigned int iv1=0; iv1<5; ++iv1 ) {
291  for ( unsigned int iv2=0; iv2<iv1; ++iv2 ) {
292  double cov12 = meanCovariance(iv1,iv2) *
293  sqrt(modeCovariance(iv1,iv1)/meanCovariance(iv1,iv1)*
294  modeCovariance(iv2,iv2)/meanCovariance(iv2,iv2));
295  modeCovariance(iv1,iv2) = modeCovariance(iv2,iv1) = cov12;
296  }
297  }
298  TrajectoryStateOnSurface modeTsos(LocalTrajectoryParameters(modeParameters,
299  vtxTsos.localParameters().pzSign()),
300  LocalTrajectoryError(modeCovariance),
301  vtxTsos.surface(),
302  vtxTsos.magneticField(),
303  vtxTsos.surfaceSide());
304  TrajectoryStateClosestToBeamLine tscbl = tscblBuilder(*modeTsos.freeState(),bs);
305  if ( !tscbl.isValid() ) return; // failed (GsfTrack keeps mode = mean)
306  //
307  // extract state at PCA and create momentum vector and covariance matrix
308  //
309  const FreeTrajectoryState& fts = tscbl.trackStateAtPCA();
310  GlobalVector tscblMom = fts.momentum();
311  reco::GsfTrack::Vector mom(tscblMom.x(),tscblMom.y(),tscblMom.z());
313  const AlgebraicSymMatrix55& tscblCov = fts.curvilinearError().matrix();
314  for ( unsigned int iv1=0; iv1<reco::GsfTrack::dimensionMode; ++iv1 ) {
315  for ( unsigned int iv2=0; iv2<reco::GsfTrack::dimensionMode; ++iv2 ) {
316  cov(iv1,iv2) = tscblCov(iv1,iv2);
317  }
318  }
319  track.setMode(fts.charge(),mom,cov);
320 }
321 
322 void
325  AlgebraicSymMatrix55& covariance) const
326 {
327  //
328  // parameters and errors from combined state
329  //
330  parameters = tsos.localParameters().vector();
331  covariance = tsos.localError().matrix();
332  //
333  // mode for parameter 0 (q/p)
334  //
336  GaussianSumUtilities1D qpGS(qpState);
337  if ( !qpGS.modeIsValid() ) return;
338  double qp = qpGS.mode().mean();
339  double varQp = qpGS.mode().variance();
340  //
341  // replace q/p value and variance, rescale correlation terms
342  // (heuristic procedure - alternative would be mode in 5D ...)
343  //
344  double VarQpRatio = sqrt(varQp/covariance(0,0));
345  parameters(0) = qp;
346  covariance(0,0) = varQp;
347  for ( int i=1; i<5; ++i ) covariance(i,0) *= VarQpRatio;
348 }
349 
350 bool
353  reco::GsfTrackExtra::Vector& momentum,
354  Measurement1D& deltaP) const
355 {
356  //
357  // states
358  //
359  TrajectoryStateOnSurface const& fwdState = tm.forwardPredictedState();
360  TrajectoryStateOnSurface const& bwdState = tm.backwardPredictedState();
361  TrajectoryStateOnSurface const& upState = tm.updatedState();
362 
363 
364  if ( !fwdState.isValid() || !bwdState.isValid() || !upState.isValid() ) {
365  return false;
366  }
367  //
368  // position from mean, momentum from mode (in cartesian coordinates)
369  // following PF code
370  //
371  GlobalPoint pos = upState.globalPosition();
372  position = reco::GsfTrackExtra::Point(pos.x(),pos.y(),pos.z());
373  GlobalVector mom;
375  if ( !result ) {
376 // std::cout << "momentumFromModeCartesian failed" << std::endl;
377  return false;
378  }
379  momentum = reco::GsfTrackExtra::Vector(mom.x(),mom.y(),mom.z());
380  //
381  // calculation from deltaP from fit to forward & backward predictions
382  // (momentum from mode) and hit
383  //
384  // prepare input parameter vectors and covariance matrices
385  AlgebraicVector5 fwdPars = fwdState.localParameters().vector();
386  AlgebraicSymMatrix55 fwdCov = fwdState.localError().matrix();
387  localParametersFromQpMode(fwdState,fwdPars,fwdCov);
388  AlgebraicVector5 bwdPars = bwdState.localParameters().vector();
389  AlgebraicSymMatrix55 bwdCov = bwdState.localError().matrix();
390  localParametersFromQpMode(bwdState,bwdPars,bwdCov);
391  LocalPoint hitPos(0.,0.,0.);
392  LocalError hitErr(-1.,-1.,-1.);
393  if ( tm.recHit()->isValid() ) {
394  hitPos = tm.recHit()->localPosition();
395  hitErr = tm.recHit()->localPositionError();
396  }
397  CollinearFitAtTM2 collinearFit(fwdPars,fwdCov,bwdPars,bwdCov,hitPos,hitErr);
398  deltaP = collinearFit.deltaP();
399 
400  return true;
401 }
#define LogDebug(id)
math::Error< dimension >::type CovarianceMatrix
std::remove_cv< typename std::remove_reference< argument_type >::type >::type key_type
Definition: Ref.h:167
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
math::Error< dimensionMode >::type CovarianceMatrixMode
3 parameter covariance matrix (momentum part) from mode
Definition: GsfTrack.h:21
ConstRecHitPointer const & recHit() const
std::vector< unsigned char > Chi2sFive
const LocalTrajectoryParameters & localParameters() const
void setGsfExtra(const GsfTrackExtraRef &ref)
set reference to GSF "extra" object
Definition: GsfTrack.h:30
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
void setHits(TrackingRecHitRefProd const &prod, unsigned firstH, unsigned int nH)
const CurvilinearTrajectoryError & curvilinearError() const
T y() const
Definition: PV3DBase.h:63
void setSecondHitPattern(Trajectory *traj, reco::GsfTrack &track, const Propagator *prop, const MeasurementTrackerEvent *measTk, const TrackerTopology *ttopo)
std::vector< GsfTrackExtra > GsfTrackExtraCollection
collection of GsfTrackExtra objects
GlobalPoint globalPosition() const
edm::Ref< TrackExtraCollection > TrackExtraRef
persistent reference to a TrackExtra
Definition: TrackExtraFwd.h:17
ROOT::Math::SMatrix< double, 5, 5, ROOT::Math::MatRepSym< double, 5 > > AlgebraicSymMatrix55
PropagationDirection
TrackCharge charge() const
void fillMode(reco::GsfTrack &track, const TrajectoryStateOnSurface innertsos, const Propagator &gsfProp, const TransverseImpactPointExtrapolator &tipExtrapolator, TrajectoryStateClosestToBeamLineBuilder &tscblBuilder, const reco::BeamSpot &bs) const
const MagneticField * magneticField() const
const CurvilinearTrajectoryError & curvilinearError() const
void fillStates(TrajectoryStateOnSurface tsos, std::vector< reco::GsfComponent5D > &states) const
PropagationDirection const & direction() const
Definition: Trajectory.cc:140
double dydz() const
dydz slope
Definition: BeamSpot.h:84
DataContainer const & measurements() const
Definition: Trajectory.h:196
AlgebraicVector5 vector() const
void setMode(int chargeMode, const Vector &momentumMode, const CovarianceMatrixMode &covarianceMode)
set mode parameters
Definition: GsfTrack.cc:27
std::vector< LocalTrajectoryParameters > TrajParams
const SurfaceType & surface() const
std::vector< TrajectoryMeasurement > DataContainer
Definition: Trajectory.h:44
MultiGaussianState1D multiState1D(const std::vector< MultiGaussianState< N >::Vector > &, const std::vector< MultiGaussianState< N >::Matrix > &, const std::vector< double > &, unsigned int)
double mean() const
parameter vector
T sqrt(T t)
Definition: SSEVec.h:18
Measurement1D deltaP() const
estimated deltaP (out-in) from fit parameters
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:174
T z() const
Definition: PV3DBase.h:64
std::vector< GsfTrack > GsfTrackCollection
collection of GsfTracks
Definition: GsfTrackFwd.h:9
double variance() const
variance
const SingleGaussianState1D & mode() const
constexpr int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:41
const AlgebraicSymMatrix55 & matrix() const
const LocalTrajectoryError & localError() const
virtual void putInEvt(edm::Event &, const Propagator *prop, const MeasurementTracker *measTk, std::unique_ptr< TrackingRecHitCollection > &, std::unique_ptr< reco::GsfTrackCollection > &, std::unique_ptr< reco::TrackExtraCollection > &, std::unique_ptr< reco::GsfTrackExtraCollection > &, std::unique_ptr< std::vector< Trajectory > > &, AlgoProductCollection &, TransientTrackingRecHitBuilder const *, const reco::BeamSpot &, const TrackerTopology *ttopo)
Put produced collections in the event.
edm::RefToBase< TrajectorySeed > seedRef(void) const
Definition: Trajectory.h:321
void localParametersFromQpMode(const TrajectoryStateOnSurface tsos, AlgebraicVector5 &parameters, AlgebraicSymMatrix55 &covariance) const
local parameters rescaled with q/p from mode
GlobalVector momentum() const
#define LogTrace(id)
double dxdz() const
dxdz slope
Definition: BeamSpot.h:82
RefProd< PROD > getRefBeforePut()
Definition: Event.h:150
TrajectoryStateOnSurface const & forwardPredictedState() const
Access to forward predicted state (from fitter or builder)
edm::Ref< GsfTrackExtraCollection > GsfTrackExtraRef
persistent reference to a GsfTrackExtra
edm::AssociationMap< edm::OneToOne< std::vector< Trajectory >, reco::GsfTrackCollection, unsigned short > > TrajGsfTrackAssociationCollection
edm::EDGetTokenT< MeasurementTrackerEvent > mteSrc_
Detector identifier class for the strip tracker.
Definition: SiStripDetId.h:17
double vz() const
z coordinate of the reference point on track
Definition: TrackBase.h:708
bool computeModeAtTM(const TrajectoryMeasurement &tm, reco::GsfTrackExtra::Point &position, reco::GsfTrackExtra::Vector &momentum, Measurement1D &deltaP) const
position, momentum and estimated deltaP at an intermediate measurement (true if successful) ...
Definition: DetId.h:18
std::vector< TrackExtra > TrackExtraCollection
collection of TrackExtra objects
Definition: TrackExtraFwd.h:11
void setExtra(const TrackExtraRef &ref)
set reference to "extra" object
Definition: Track.h:189
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:187
ROOT::Math::SVector< double, 5 > AlgebraicVector5
const GlobalTrajectoryParameters & globalParameters() const
double mean(unsigned int i) const
mean value of a component
XYZVectorD XYZVector
spatial vector with cartesian internal representation
Definition: Vector3D.h:30
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
math::XYZPoint Point
point in the space
Definition: GsfTrackExtra.h:25
TrajectoryStateOnSurface propagate(STA const &state, SUR const &surface) const
Definition: Propagator.h:53
bool appendHitPattern(const TrackingRecHit &hit, const TrackerTopology &ttopo)
append a single hit to the HitPattern
Definition: TrackBase.h:495
double variance(unsigned int i) const
variance of a component
std::vector< AlgoProduct > AlgoProductCollection
TrajectoryStateOnSurface extrapolate(const FreeTrajectoryState &fts, const GlobalPoint &vtx) const
extrapolation with default (=geometrical) propagator
Definition: utils.py:1
const AlgebraicSymMatrix55 & matrix() const
static int position[264][3]
Definition: ReadPGInfo.cc:509
edm::ESHandle< NavigationSchool > theSchool
const Point & position() const
position
Definition: BeamSpot.h:62
TrajectoryStateOnSurface const & updatedState() const
math::XYZVector Vector
spatial vector
Definition: GsfTrackExtra.h:27
bool momentumFromModeCartesian(TrajectoryStateOnSurface const &tsos, GlobalVector &momentum)
float pzSign() const
Sign of the z-component of the momentum in the local frame.
bool isValid() const
Definition: ESHandle.h:44
edm::OrphanHandle< TrackCollection > rTracks_
T x() const
Definition: PV3DBase.h:62
math::XYZVector Vector
spatial vector
Definition: TrackBase.h:80
def move(src, dest)
Definition: eostools.py:511
void setTrajParams(TrajParams tmps, Chi2sFive chi2s)
bool modeIsValid() const
mode status
TrajectoryStateOnSurface const & backwardPredictedState() const
Access to backward predicted state (from smoother)
math::Vector< dimension >::type ParameterVector