CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
KalmanAlignmentTrackRefitter.cc
Go to the documentation of this file.
1 
3 
5 
7 
10 
13 
15 
17 
18 #include "CLHEP/GenericFunctions/CumulativeChiSquare.hh"
19 
20 #include <iostream>
21 
22 using namespace std;
23 using namespace reco;
24 using namespace Genfun;
25 
26 
28  theRefitterAlgo( config ),
29  theNavigator( navigator ),
30  theDebugFlag( config.getUntrackedParameter<bool>( "debug", true ) )
31 {
33  // --- GPetrucc: I can't understand where anything is read from the event, and who's the consumer.
34  // If there is one anywhere, it should do the consumes<T> calls and pass that to the setSrc
35  //TrackProducerBase< reco::Track >::setSrc( consumes<TrackCandidateCollection>(iConfig.getParameter<edm::InputTag>( "src" )),
36  // consumes<reco::BeamSpot>(iConfig.getParameter<edm::InputTag>( "beamSpot" )));
37 }
38 
39 
41 
42 
45  const AlignmentSetupCollection& algoSetups,
47  const reco::BeamSpot* beamSpot )
48 {
49  // Retrieve what we need from the EventSetup
51  edm::ESHandle< MagneticField > aMagneticField;
52  edm::ESHandle< TrajectoryFitter > aTrajectoryFitter;
53  edm::ESHandle< Propagator > aPropagator;
56 
57  getFromES( setup, aGeometry, aMagneticField, aTrajectoryFitter, aPropagator, theMeasTk, aRecHitBuilder );
58 
60  TrackCollection fullTracks;
61 
62  ConstTrajTrackPairCollection refittedFullTracks;
63  ConstTrajTrackPairCollection::const_iterator itTrack;
64 
65  for( itTrack = tracks.begin(); itTrack != tracks.end(); ++itTrack )
66  {
67  TransientTrack fullTrack( *(*itTrack).second, aMagneticField.product() );
68 
69  AlignmentSetupCollection::const_iterator itSetup;
70  for ( itSetup = algoSetups.begin(); itSetup != algoSetups.end(); ++itSetup )
71  {
72  RecHitContainer trackingRecHits;
73  RecHitContainer externalTrackingRecHits;
74 
76  RecHitContainer zPlusRecHits;
77  RecHitContainer zMinusRecHits;
78 
79  // Extract collection with TrackingRecHits
80  Trajectory::ConstRecHitContainer hits = (*itTrack).first->recHits();
81  Trajectory::ConstRecHitContainer::iterator itHits;
82 
83  for ( itHits = hits.begin(); itHits != hits.end(); ++itHits )
84  {
85  if ( !(*itHits)->isValid() ) continue;
86 
87  try
88  {
89  //if ( !theNavigator->alignableFromDetId( (*itHits)->geographicalId() )->alignmentParameters() ) continue;
90  theNavigator->alignableFromDetId( (*itHits)->geographicalId() );
91  } catch(...) { continue; }
92 
93  if ( (*itSetup)->useForTracking( *itHits ) )
94  {
95  trackingRecHits.push_back( (*itHits)->hit()->clone() );
96 
97  ( (*itHits)->det()->position().z() > 0. ) ?
98  zPlusRecHits.push_back( (*itHits)->hit()->clone() ) :
99  zMinusRecHits.push_back( (*itHits)->hit()->clone() );
100 
103  }
104  else if ( (*itSetup)->useForExternalTracking( *itHits ) )
105  {
106  externalTrackingRecHits.push_back( (*itHits)->hit()->clone() );
107  }
108  }
109 
110  //edm::LogInfo( "KalmanAlignmentTrackRefitter" ) << "Hits for tracking/external: " << trackingRecHits.size() << "/" << externalTrackingRecHits.size();
111 
112  //if ( !zPlusRecHits.size() || !zMinusRecHits.size() ) continue;
114 
115  if ( trackingRecHits.empty() ) continue;
116 
117  if ( externalTrackingRecHits.empty() )
118  {
119  if ( ( (*itSetup)->getExternalTrackingSubDetIds().size() == 0 ) && // O.K., no external hits expected,
120  ( trackingRecHits.size() >= (*itSetup)->minTrackingHits() ) )
121  {
122  TrajTrackPairCollection refitted = refitSingleTracklet( aGeometry.product(), aMagneticField.product(),
123  (*itSetup)->fitter(), (*itSetup)->propagator(),
124  aRecHitBuilder.product(), fullTrack,
125  trackingRecHits, beamSpot,
126  (*itSetup)->sortingDirection(), false, true );
127 
128  // The refitting did not work ... Try next!
129  if ( refitted.empty() ) continue;
130  if ( rejectTrack( refitted.front().second ) ) continue;
131 
132  if ( theDebugFlag )
133  {
134  debugTrackData( (*itSetup)->id(), refitted.front().first, refitted.front().second, beamSpot );
135  debugTrackData( "OrigFullTrack", (*itTrack).first, (*itTrack).second, beamSpot );
136  }
137 
138 
139  TrackletPtr trackletPtr( new KalmanAlignmentTracklet( refitted.front(), *itSetup ) );
140  result.push_back( trackletPtr );
141  }
142  else { continue; } // Expected external hits but found none or not enough hits.
143  }
144  else if ( ( trackingRecHits.size() >= (*itSetup)->minTrackingHits() ) &&
145  ( externalTrackingRecHits.size() >= (*itSetup)->minExternalHits() ) )
146  {
147  // Create an instance of KalmanAlignmentTracklet with an external prediction.
148 
149  TrajTrackPairCollection external = refitSingleTracklet( aGeometry.product(), aMagneticField.product(),
150  (*itSetup)->externalFitter(), (*itSetup)->externalPropagator(),
151  aRecHitBuilder.product(), fullTrack,
152  externalTrackingRecHits, beamSpot,
153  (*itSetup)->externalSortingDirection(),
154  false, true );
155  //if ( external.empty() || rejectTrack( external.front().second ) ) { continue; }
156  if ( external.empty() ) { continue; }
157 
158  TransientTrack externalTrack( *external.front().second, aMagneticField.product() );
159 
160  TrajTrackPairCollection refitted = refitSingleTracklet( aGeometry.product(), aMagneticField.product(),
161  (*itSetup)->fitter(), (*itSetup)->propagator(),
162  aRecHitBuilder.product(), externalTrack,
163  trackingRecHits, beamSpot,
164  (*itSetup)->sortingDirection(),
165  false, true, (*itSetup)->id() );
166 
167  if ( refitted.empty() ) { continue; }
168  if ( rejectTrack( refitted.front().second ) ) continue;
169 
170  //const Surface& surface = refitted.front().first->firstMeasurement().updatedState().surface();
171  const Surface& surface = refitted.front().first->lastMeasurement().updatedState().surface();
172  TrajectoryStateOnSurface externalTsos = externalTrack.impactPointState();
173  AnalyticalPropagator externalPredictionPropagator( aMagneticField.product(), anyDirection );
174  TrajectoryStateOnSurface externalPrediction = externalPredictionPropagator.propagate( externalTsos, surface );
175  if ( !externalPrediction.isValid() ) continue;
176 
177  if ( theDebugFlag )
178  {
179  debugTrackData( string("External") + (*itSetup)->id(), external.front().first, external.front().second, beamSpot );
180  debugTrackData( (*itSetup)->id(), refitted.front().first, refitted.front().second, beamSpot );
181  debugTrackData( "OrigFullTrack", (*itTrack).first, (*itTrack).second, beamSpot );
182  }
183 
184  TrackletPtr trackletPtr( new KalmanAlignmentTracklet( refitted.front(), externalPrediction, *itSetup ) );
185  result.push_back( trackletPtr );
186 
187  delete external.front().first;
188  delete external.front().second;
189  }
190  }
191  }
192 
193  return result;
194 }
195 
196 
199  const MagneticField* magneticField,
200  const TrajectoryFitter* fitter,
201  const Propagator* propagator,
202  const TransientTrackingRecHitBuilder* recHitBuilder,
203  const TransientTrack& fullTrack,
204  RecHitContainer& recHits,
205  const reco::BeamSpot* beamSpot,
206  const SortingDirection& sortingDir,
207  bool useExternalEstimate,
208  bool reuseMomentumEstimate,
209  const string identifier )
210 {
211 
213 
214  if ( recHits.size() < 2 ) return result;
215 
216  sortRecHits( recHits, recHitBuilder, sortingDir );
217 
219  RecHitContainer::iterator itRecHit;
220  for ( itRecHit = recHits.begin(); itRecHit != recHits.end(); ++itRecHit )
221  hits.push_back( recHitBuilder->build( &(*itRecHit) ) );
222 
223  TransientTrackingRecHit::ConstRecHitPointer firstHit = hits.front();
224 
225  AnalyticalPropagator firstStatePropagator( magneticField, anyDirection );
226  TrajectoryStateOnSurface firstState = firstStatePropagator.propagate( fullTrack.impactPointState(), firstHit->det()->surface() );
227 
228  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_IPPt"),
229  1e-2*fullTrack.impactPointState().globalParameters().momentum().perp() );
230 
231  if ( !firstState.isValid() ) return result;
232 
233 // LocalTrajectoryError startError;
234 
235 // const double startErrorValue = 100;
236 // const unsigned int nTrajParam = 5;
237 
238 // if ( useExternalEstimate ) {
239 // startError = firstState.localError();
240 // } else {
241 // if ( reuseMomentumEstimate )
242 // {
243 // AlgebraicSymMatrix firstStateError( asHepMatrix( firstState.localError().matrix() ) );
244 // AlgebraicSymMatrix startErrorMatrix( nTrajParam, 0 );
245 // startErrorMatrix[0][0] = 1e-10;
246 // //startErrorMatrix[0][0] = firstStateError[0][0];
247 // startErrorMatrix[1][1] = startErrorValue;//firstStateError[1][1];
248 // startErrorMatrix[2][2] = startErrorValue;//firstStateError[2][2];
249 // startErrorMatrix[3][3] = startErrorValue;
250 // startErrorMatrix[4][4] = startErrorValue;
251 // startError = LocalTrajectoryError( startErrorMatrix );
252 // } else {
253 // AlgebraicSymMatrix startErrorMatrix( startErrorValue*AlgebraicSymMatrix( nTrajParam, 1 ) );
254 // startError = LocalTrajectoryError( startErrorMatrix );
255 // }
256 
257 // }
258 
259 // // MOMENTUM ESTIMATE FOR COSMICS. P = 1.5 GeV
260 // LocalTrajectoryParameters firstStateParameters = firstState.localParameters();
261 // AlgebraicVector firstStateParamVec = asHepVector( firstStateParameters.mixedFormatVector() );
262 // firstStateParamVec[0] = 1./1.5;
263 // LocalTrajectoryParameters cosmicsStateParameters( firstStateParamVec, firstStateParameters.pzSign(), true );
264 // TrajectoryStateOnSurface tsos( cosmicsStateParameters, startError, firstState.surface(), magneticField );
265 
266  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_FSPt"),
267  1e-2*firstState.globalParameters().momentum().perp() );
268 
269  firstState.rescaleError( 100 );
270  TrajectoryStateOnSurface tsos( firstState.localParameters(), firstState.localError(),
271  firstState.surface(), magneticField );
272 
273  // Generate a trajectory seed.
274  TrajectorySeed seed( PTrajectoryStateOnDet(), recHits, propagator->propagationDirection() );
275 
276  // Generate track candidate.
277 
278  PTrajectoryStateOnDet state = trajectoryStateTransform::persistentState( tsos, firstHit->det()->geographicalId().rawId() );
279  TrackCandidate candidate( recHits, seed, state );
280 
281  AlgoProductCollection algoResult;
282 
283  int charge = static_cast<int>( tsos.charge() );
284  double momentum = firstState.localParameters().momentum().mag();
286  TRecHit1DMomConstraint::build( charge, momentum, 1e-10, &tsos.surface() );
287 
288  //no insert in OwnVector...
290  tmpHits.push_back(testhit);
291  for (TransientTrackingRecHit::RecHitContainer::const_iterator i=hits.begin(); i!=hits.end(); i++){
292  tmpHits.push_back(*i);
293  }
294  hits.swap(tmpHits);
295 
296  theRefitterAlgo.buildTrack( fitter, propagator, algoResult, hits, tsos, seed, 0, *beamSpot, candidate.seedRef());
297 
298  for ( AlgoProductCollection::iterator it = algoResult.begin(); it != algoResult.end(); ++it )
299  result.push_back( make_pair( (*it).first, (*it).second.first ) );
300 
301  return result;
302 }
303 
304 
306  const TransientTrackingRecHitBuilder* builder,
307  const SortingDirection& sortingDir ) const
308 {
309  // Don't start sorting if there is only 1 or even 0 elements.
310  if ( hits.size() < 2 ) return;
311 
312  TransientTrackingRecHit::RecHitPointer firstHit = builder->build( &hits.front() );
313  double firstRadius = firstHit->det()->surface().toGlobal( firstHit->localPosition() ).mag();
314  double firstY = firstHit->det()->surface().toGlobal( firstHit->localPosition() ).y();
315 
316  TransientTrackingRecHit::RecHitPointer lastHit = builder->build( &hits.back() );
317  double lastRadius = lastHit->det()->surface().toGlobal( lastHit->localPosition() ).mag();
318  double lastY = lastHit->det()->surface().toGlobal( lastHit->localPosition() ).y();
319 
320  bool insideOut = firstRadius < lastRadius;
321  bool upsideDown = lastY < firstY;
322 
323  if ( ( insideOut && ( sortingDir == KalmanAlignmentSetup::sortInsideOut ) ) ||
324  ( !insideOut && ( sortingDir == KalmanAlignmentSetup::sortOutsideIn ) ) ||
325  ( upsideDown && ( sortingDir == KalmanAlignmentSetup::sortUpsideDown ) ) ||
326  ( !upsideDown && ( sortingDir == KalmanAlignmentSetup::sortDownsideUp ) ) ) return;
327 
328  // Fill temporary container with reversed hits.
330  RecHitContainer::iterator itHit = hits.end();
331  do { --itHit; tmp.push_back( ( *itHit ).clone() ); } while ( itHit != hits.begin() );
332 
333  // Swap the content of the temporary and the input container.
334  hits.swap( tmp );
335 
336  return;
337 }
338 
339 
341 {
342  double trackChi2 = track->chi2();
343  unsigned int ndof = static_cast<unsigned int>( track->ndof() );
344  if ( trackChi2 <= 0. || ndof <= 0 ) return false;
345 
346  //FIXME: should be configurable (via KalmanAlignmentSetup)
347  double minChi2Prob = 0;//1e-6;
348  double maxChi2Prob = 1.0;
349 
350  GENFUNCTION cumulativeChi2 = Genfun::CumulativeChiSquare( ndof );
351  double chi2Prob = 1. - cumulativeChi2( trackChi2 );
352  return ( chi2Prob < minChi2Prob ) || ( chi2Prob > maxChi2Prob );
353 }
354 
355 
356 void KalmanAlignmentTrackRefitter::debugTrackData( const string identifier,
357  const Trajectory* traj,
358  const Track* track,
359  const reco::BeamSpot* bs )
360 {
361  unsigned int ndof = static_cast<unsigned int>( track->ndof() );
362  double trackChi2 = track->chi2();
363  if ( ( trackChi2 > 0. ) && ( ndof > 0 ) )
364  {
365  GENFUNCTION cumulativeChi2 = Genfun::CumulativeChiSquare( ndof );
366  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_CumChi2"), 1. - cumulativeChi2( trackChi2 ) );
367  } else if ( ndof == 0 ) {
368  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_CumChi2"), -1. );
369  } else {
370  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_CumChi2"), -2. );
371  }
372 
373  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_NHits"), traj->foundHits() );
374  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_Pt"), 1e-2*track->pt() );
375  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_Eta"), track->eta() );
376  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_Phi"), track->phi() );
377  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_NormChi2"), track->normalizedChi2() );
378  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_DZ"), track->dz() );
379 
380  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_DXY_BS"), fabs( track->dxy( bs->position() ) ) );
381  KalmanAlignmentDataCollector::fillHistogram( identifier + string("_DXY"), fabs( track->dxy() ) );
382  //KalmanAlignmentDataCollector::fillHistogram( identifier + string("_D0"), fabs( track->d0() ) );
383 }
int foundHits() const
Definition: Trajectory.h:236
int i
Definition: DBlmapReader.cc:9
TrackletCollection refitTracks(const edm::EventSetup &eventSetup, const AlignmentSetupCollection &algoSetups, const ConstTrajTrackPairCollection &tracks, const reco::BeamSpot *beamSpot)
reference back()
Definition: OwnVector.h:320
T perp() const
Definition: PV3DBase.h:72
static void fillHistogram(std::string histo_name, float data)
bool rejectTrack(const reco::Track *track) const
const LocalTrajectoryParameters & localParameters() const
double normalizedChi2() const
chi-squared divided by n.d.o.f. (or chi-squared * 1e6 if n.d.o.f. is zero)
Definition: TrackBase.h:109
std::vector< reco::Track > TrackCollection
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
size_type size() const
Definition: OwnVector.h:247
PTrajectoryStateOnDet persistentState(const TrajectoryStateOnSurface &ts, unsigned int detid)
double phi() const
azimuthal angle of momentum vector
Definition: TrackBase.h:137
void swap(OwnVector< T, P > &other) noexcept
Definition: OwnVector.h:400
double charge(const std::vector< uint8_t > &Ampls)
std::vector< ConstRecHitPointer > RecHitContainer
std::vector< KalmanAlignmentSetup * > AlignmentSetupCollection
void setConf(const edm::ParameterSet &conf)
Set parameter set.
virtual PropagationDirection propagationDirection() const GCC11_FINAL
Definition: Propagator.h:145
bool buildTrack(const TrajectoryFitter *, const Propagator *, AlgoProductCollection &, TransientTrackingRecHit::RecHitContainer &, TrajectoryStateOnSurface &, const TrajectorySeed &, float, const reco::BeamSpot &, SeedRef seedRef=SeedRef(), int qualityMask=0, signed char nLoops=0)
Construct Tracks to be put in the event.
TransientTrackingRecHit::ConstRecHitContainer ConstRecHitContainer
Definition: Trajectory.h:43
iterator begin()
Definition: OwnVector.h:227
KalmanAlignmentTracklet::TrajTrackPairCollection TrajTrackPairCollection
void push_back(D *&d)
Definition: OwnVector.h:273
const SurfaceType & surface() const
T mag() const
Definition: PV3DBase.h:67
double eta() const
pseudorapidity of momentum vector
Definition: TrackBase.h:139
double chi2() const
chi-squared of the fit
Definition: TrackBase.h:105
double ndof() const
number of degrees of freedom of the fit
Definition: TrackBase.h:107
virtual RecHitPointer build(const TrackingRecHit *p) const =0
build a tracking rechit from an existing rechit
bool empty() const
Definition: OwnVector.h:252
double pt() const
track transverse momentum
Definition: TrackBase.h:129
tuple result
Definition: query.py:137
LocalVector momentum() const
Momentum vector in the local frame.
const LocalTrajectoryError & localError() const
std::vector< AlgoProduct > AlgoProductCollection
void sortRecHits(RecHitContainer &hits, const TransientTrackingRecHitBuilder *builder, const SortingDirection &sortingDir) const
TrajTrackPairCollection refitSingleTracklet(const TrackingGeometry *geometry, const MagneticField *magneticField, const TrajectoryFitter *fitter, const Propagator *propagator, const TransientTrackingRecHitBuilder *recHitBuilder, const reco::TransientTrack &originalTrack, RecHitContainer &recHits, const reco::BeamSpot *beamSpot, const SortingDirection &sortingDir, bool useExternalEstimate, bool reuseMomentumEstimate, const std::string identifier=std::string("RefitSingle_"))
double dz() const
dz parameter (= dsz/cos(lambda)). This is the track z0 w.r.t (0,0,0) only if the refPoint is close to...
Definition: TrackBase.h:125
void debugTrackData(const std::string identifier, const Trajectory *traj, const reco::Track *track, const reco::BeamSpot *bs)
iterator end()
Definition: OwnVector.h:232
const GlobalTrajectoryParameters & globalParameters() const
tuple tracks
Definition: testEve_cfg.py:39
AlignmentAlgorithmBase::ConstTrajTrackPairCollection ConstTrajTrackPairCollection
T const * product() const
Definition: ESHandle.h:62
std::vector< TrackletPtr > TrackletCollection
std::vector< std::vector< double > > tmp
Definition: MVATrainer.cc:100
ESHandle< TrackerGeometry > geometry
const Point & position() const
position
Definition: BeamSpot.h:62
TrajectoryStateOnSurface impactPointState() const
double dxy() const
dxy parameter. (This is the transverse impact parameter w.r.t. to (0,0,0) ONLY if refPoint is close t...
Definition: TrackBase.h:119
AlignableDetOrUnitPtr alignableFromDetId(const DetId &detid)
Returns AlignableDetOrUnitPtr corresponding to given DetId.
reference front()
Definition: OwnVector.h:348
void setup(std::vector< TH2F > &depth, std::string name, std::string units="")
KalmanAlignmentTrackRefitter(const edm::ParameterSet &config, AlignableNavigator *navigator)
Constructor.
TrackProducerAlgorithm< reco::Track > theRefitterAlgo
virtual void getFromES(const edm::EventSetup &, edm::ESHandle< TrackerGeometry > &, edm::ESHandle< MagneticField > &, edm::ESHandle< TrajectoryFitter > &, edm::ESHandle< Propagator > &, edm::ESHandle< MeasurementTracker > &, edm::ESHandle< TransientTrackingRecHitBuilder > &)
Get needed services from the Event Setup.