CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CosmicMuonSmoother.cc
Go to the documentation of this file.
1 
16 
25 
27 
28 using namespace edm;
29 using namespace std;
30 
31 //
32 // constructor
33 //
34 CosmicMuonSmoother::CosmicMuonSmoother(const ParameterSet& par, const MuonServiceProxy *service) : theService(service) {
35 
36  theUpdator = new KFUpdator;
39  thePropagatorAlongName = par.getParameter<string>("PropagatorAlong");
40  thePropagatorOppositeName = par.getParameter<string>("PropagatorOpposite");
41  theErrorRescaling = par.getParameter<double>("RescalingFactor");
42 
43  category_ = "Muon|RecoMuon|CosmicMuon|CosmicMuonSmoother";
44 }
45 
46 //
47 // destructor
48 //
50 
51  if ( theUpdator ) delete theUpdator;
52  if ( theUtilities ) delete theUtilities;
53  if ( theEstimator ) delete theEstimator;
54 
55 }
56 
57 
58 //
59 // fit and smooth trajectory
60 //
61 vector<Trajectory> CosmicMuonSmoother::trajectories(const Trajectory& t) const {
62  vector<Trajectory> fitted = fit(t);
63  return smooth(fitted);
64 
65 }
66 
67 //
68 // fit and smooth trajectory
69 //
70 vector<Trajectory> CosmicMuonSmoother::trajectories(const TrajectorySeed& seed,
71  const ConstRecHitContainer& hits,
72  const TrajectoryStateOnSurface& firstPredTsos) const {
73 
74  if ( hits.empty() ||!firstPredTsos.isValid() ) return vector<Trajectory>();
75 
76  LogTrace(category_)<< "trajectory begin (seed hits tsos)";
77 
78  TrajectoryStateOnSurface firstTsos = firstPredTsos;
80 
81  LogTrace(category_)<< "first TSOS: "<<firstTsos;
82 
83  vector<Trajectory> fitted = fit(seed, hits, firstTsos);
84  LogTrace(category_)<< "fitted: "<<fitted.size();
85  vector<Trajectory> smoothed = smooth(fitted);
86  LogTrace(category_)<< "smoothed: "<<smoothed.size();
87 
88  return smoothed;
89 
90 }
91 
92 //
93 // fit trajectory
94 //
95 vector<Trajectory> CosmicMuonSmoother::fit(const Trajectory& t) const {
96 
97  if ( t.empty() ) return vector<Trajectory>();
98 
99  LogTrace(category_)<< "fit begin (trajectory) ";
100 
101  TrajectoryStateOnSurface firstTsos = initialState(t);
102  if ( !firstTsos.isValid() ) {
103  LogTrace(category_)<< "Error: firstTsos invalid. ";
104  return vector<Trajectory>();
105  }
106 
107  LogTrace(category_)<< "firstTsos: "<<firstTsos;
108 
109  ConstRecHitContainer hits = t.recHits();
110  LogTrace(category_)<< "hits: "<<hits.size();
111  LogTrace(category_)<<"hit front" <<hits.front()->globalPosition()<< " hit back"
112  <<hits.back()->globalPosition();
113 
114  sortHitsAlongMom(hits, firstTsos);
115 
116  LogTrace(category_)<<"after sorting hit front" <<hits.front()->globalPosition()<< " hit back"
117  <<hits.back()->globalPosition();
118 
119  return fit(t.seed(), hits, firstTsos);
120 
121 }
122 
123 
124 //
125 // fit trajectory
126 //
127 vector<Trajectory> CosmicMuonSmoother::fit(const TrajectorySeed& seed,
128  const ConstRecHitContainer& hits,
129  const TrajectoryStateOnSurface& firstPredTsos) const {
130 
131  LogTrace(category_)<< "fit begin (seed, hit, tsos).";
132 
133  if ( hits.empty() ) {
134  LogTrace(category_)<< "Error: empty hits container.";
135  return vector<Trajectory>();
136  }
137 
138  Trajectory myTraj(seed, alongMomentum);
139 
140  TrajectoryStateOnSurface predTsos(firstPredTsos);
141  LogTrace(category_)<< "first pred TSOS: "<<predTsos;
142 
143  if ( !predTsos.isValid() ) {
144  LogTrace(category_)<< "Error: firstTsos invalid.";
145  return vector<Trajectory>();
146  }
147  TrajectoryStateOnSurface currTsos;
148 
149  if ( hits.front()->isValid() ) {
150 
151  TransientTrackingRecHit::RecHitPointer preciseHit = hits.front()->clone(predTsos);
152  LogTrace(category_)<<"first hit is at det "<< hits.front()->det()->surface().position();
153 
154  currTsos = theUpdator->update(predTsos, *preciseHit);
155  if (!currTsos.isValid()){
157  <<"an updated state is not valid. killing the trajectory.";
158  return vector<Trajectory>();
159  }
160  myTraj.push(TrajectoryMeasurement(predTsos, currTsos, hits.front(),
161  theEstimator->estimate(predTsos, *hits.front()).second));
162  if ( currTsos.isValid() ) LogTrace(category_)<< "first curr TSOS: "<<currTsos;
163 
164  } else {
165 
166  currTsos = predTsos;
167  myTraj.push(TrajectoryMeasurement(predTsos, hits.front()));
168  }
169  //const TransientTrackingRecHit& firsthit = *hits.front();
170 
171  for ( ConstRecHitContainer::const_iterator ihit = hits.begin() + 1;
172  ihit != hits.end(); ++ihit ) {
173 
174  if ( !(**ihit).isValid() ) {
175  LogTrace(category_)<< "Error: invalid hit.";
176  continue;
177  }
178  if (currTsos.isValid()) {
179  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
180  <<"mom "<<currTsos.globalMomentum();
181  } else {
182  LogTrace(category_)<<"current state invalid";
183  }
184 
185  predTsos = propagatorAlong()->propagate(currTsos, (**ihit).det()->surface());
186  LogTrace(category_)<<"predicted state propagate directly "<<predTsos.isValid();
187 
188  if ( !predTsos.isValid() ) {
189  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
190  predTsos = theUtilities->stepPropagate(currTsos, (*ihit), *propagatorAlong());
191  }
192  if ( !predTsos.isValid() && (fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) && (theService->propagator("StraightLinePropagator").isValid() ) ) {
193  LogTrace(category_)<<"straight-line propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
194  predTsos = theService->propagator("StraightLinePropagator")->propagate(currTsos, (**ihit).det()->surface());
195  }
196  if (predTsos.isValid()) {
197  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
198  <<"mom "<<predTsos.globalMomentum();
199  } else {
200  LogTrace(category_)<<"predicted state invalid";
201  }
202  if ( !predTsos.isValid() ) {
203  LogTrace(category_)<< "Error: predTsos is still invalid forward fit.";
204 // return vector<Trajectory>();
205  continue;
206  } else if ( (**ihit).isValid() ) {
207  // update
208  TransientTrackingRecHit::RecHitPointer preciseHit = (**ihit).clone(predTsos);
209 
210  if ( !preciseHit->isValid() ) {
211  currTsos = predTsos;
212  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
213  } else {
214  currTsos = theUpdator->update(predTsos, *preciseHit);
215  if (!currTsos.isValid()){
217  <<"an updated state is not valid. killing the trajectory.";
218  return vector<Trajectory>();
219  }
220  myTraj.push(TrajectoryMeasurement(predTsos, currTsos, preciseHit,
221  theEstimator->estimate(predTsos, *preciseHit).second));
222  }
223  } else {
224  currTsos = predTsos;
225  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
226  }
227 
228  }
229 
230  std::vector<TrajectoryMeasurement> mytms = myTraj.measurements();
231  LogTrace(category_)<<"fit result "<<mytms.size();
232  for (std::vector<TrajectoryMeasurement>::const_iterator itm = mytms.begin();
233  itm != mytms.end(); ++itm ) {
234  LogTrace(category_)<<"updated pos "<<itm->updatedState().globalPosition()
235  <<"mom "<<itm->updatedState().globalMomentum();
236  }
237 
238 
239  return vector<Trajectory>(1, myTraj);
240 
241 }
242 
243 
244 //
245 // smooth trajectory
246 //
247 vector<Trajectory> CosmicMuonSmoother::smooth(const vector<Trajectory>& tc) const {
248 
249  vector<Trajectory> result;
250 
251  for ( vector<Trajectory>::const_iterator it = tc.begin(); it != tc.end(); ++it ) {
252  vector<Trajectory> smoothed = smooth(*it);
253  result.insert(result.end(), smoothed.begin(), smoothed.end());
254  }
255 
256  return result;
257 
258 }
259 
260 
261 //
262 // smooth trajectory
263 //
264 vector<Trajectory> CosmicMuonSmoother::smooth(const Trajectory& t) const {
265 
266  if ( t.empty() ) {
267  LogTrace(category_)<< "Error: smooth: empty trajectory.";
268  return vector<Trajectory>();
269  }
270 
271  Trajectory myTraj(t.seed(), oppositeToMomentum);
272 
273  vector<TrajectoryMeasurement> avtm = t.measurements();
274 
275  if ( avtm.size() < 2 ) {
276  LogTrace(category_)<< "Error: smooth: too little TM. ";
277  return vector<Trajectory>();
278  }
279 
280  TrajectoryStateOnSurface predTsos = avtm.back().forwardPredictedState();
282 
283  if ( !predTsos.isValid() ) {
284  LogTrace(category_)<< "Error: smooth: first TSOS from back invalid. ";
285  return vector<Trajectory>();
286  }
287 
288  TrajectoryStateOnSurface currTsos;
289 
290  // first smoothed TrajectoryMeasurement is last fitted
291  if ( avtm.back().recHit()->isValid() ) {
292  currTsos = theUpdator->update(predTsos, (*avtm.back().recHit()));
293  if (!currTsos.isValid()){
295  <<"an updated state is not valid. killing the trajectory.";
296  return vector<Trajectory>();
297  }
298  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
299  predTsos,
300  avtm.back().updatedState(),
301  avtm.back().recHit(),
302  avtm.back().estimate()//,
303  /*avtm.back().layer()*/),
304  avtm.back().estimate());
305 
306  } else {
307  currTsos = predTsos;
308  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
309  avtm.back().recHit()//,
310  /*avtm.back().layer()*/));
311 
312  }
313 
315 
316 
317  for ( vector<TrajectoryMeasurement>::reverse_iterator itm = avtm.rbegin() + 1;
318  itm != avtm.rend() - 1; ++itm ) {
319 
320  if (currTsos.isValid()) {
321  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
322  <<"mom "<<currTsos.globalMomentum();
323  } else {
324  LogTrace(category_)<<"current state invalid";
325  }
326 
327  predTsos = propagatorOpposite()->propagate(currTsos,(*itm).recHit()->det()->surface());
328 
329  if ( !predTsos.isValid() ) {
330  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*itm).recHit()->globalPosition();
331  predTsos = theUtilities->stepPropagate(currTsos, (*itm).recHit(), *propagatorOpposite());
332  }
333  if (predTsos.isValid()) {
334  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
335  <<"mom "<<predTsos.globalMomentum();
336  } else {
337  LogTrace(category_)<<"predicted state invalid";
338  }
339 
340  if ( !predTsos.isValid() ) {
341  LogTrace(category_)<< "Error: predTsos is still invalid backward smooth.";
342  return vector<Trajectory>();
343  // continue;
344  } else if ( (*itm).recHit()->isValid() ) {
345  //update
346  currTsos = theUpdator->update(predTsos, (*(*itm).recHit()));
347  if (!currTsos.isValid()){
349  <<"an updated state is not valid. killing the trajectory.";
350  return vector<Trajectory>();
351  }
352  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
353  if ( !combTsos.isValid() ) {
354  LogTrace(category_)<< "Error: smooth: combining pred TSOS failed. ";
355  return vector<Trajectory>();
356  }
357 
358  TrajectoryStateOnSurface smooTsos = combiner((*itm).updatedState(), predTsos);
359 
360  if ( !smooTsos.isValid() ) {
361  LogTrace(category_)<< "Error: smooth: combining smooth TSOS failed. ";
362  return vector<Trajectory>();
363  }
364 
365  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
366  predTsos,
367  smooTsos,
368  (*itm).recHit(),
369  theEstimator->estimate(combTsos, (*(*itm).recHit())).second//,
370  /*(*itm).layer()*/),
371  (*itm).estimate());
372  } else {
373  currTsos = predTsos;
374  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
375 
376  if ( !combTsos.isValid() ) {
377  LogTrace(category_)<< "Error: smooth: combining TSOS failed. ";
378  return vector<Trajectory>();
379  }
380 
381  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
382  predTsos,
383  combTsos,
384  (*itm).recHit()//,
385  /*(*itm).layer()*/));
386  }
387  }
388 
389  // last smoothed TrajectoryMeasurement is last filtered
390  predTsos = propagatorOpposite()->propagate(currTsos, avtm.front().recHit()->det()->surface());
391 
392  if ( !predTsos.isValid() ){
393  LogTrace(category_)<< "Error: last predict TSOS failed, use original one. ";
394  // return vector<Trajectory>();
395  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
396  avtm.front().recHit()));
397  } else {
398  if ( avtm.front().recHit()->isValid() ) {
399  //update
400  currTsos = theUpdator->update(predTsos, (*avtm.front().recHit()));
401  if (currTsos.isValid())
402  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
403  predTsos,
404  currTsos,
405  avtm.front().recHit(),
406  theEstimator->estimate(predTsos, (*avtm.front().recHit())).second//,
407  /*avtm.front().layer()*/),
408  avtm.front().estimate());
409  }
410  }
411  LogTrace(category_)<< "myTraj foundHits. "<<myTraj.foundHits();
412 
413  if (myTraj.foundHits() >= 3)
414  return vector<Trajectory>(1, myTraj);
415  else {
416  LogTrace(category_)<< "Error: smooth: No enough hits in trajctory. ";
417  return vector<Trajectory>();
418  }
419 
420 }
421 
423  if ( t.empty() ) return TrajectoryStateOnSurface();
424 
426 
428 
429  bool beamhaloFlag = ( t.firstMeasurement().updatedState().globalMomentum().eta() > 4.0 || t.lastMeasurement().updatedState().globalMomentum().eta() > 4.0 );
430 
431  if ( !beamhaloFlag ) { //initialState is the top one
433  result = t.firstMeasurement().updatedState();
434  } else {
435  result = t.lastMeasurement().updatedState();
436  }
437  if (result.globalMomentum().y()> 1.0 ) //top tsos should pointing down
438  theUtilities->reverseDirection(result,&*theService->magneticField());
439  } else {
440  if ( t.firstMeasurement().updatedState().globalPosition().z() * t.lastMeasurement().updatedState().globalPosition().z() >0 ) { //same side
441  if ( fabs(t.firstMeasurement().updatedState().globalPosition().z()) > fabs(t.lastMeasurement().updatedState().globalPosition().z()) ) {
442  result = t.firstMeasurement().updatedState();
443  } else {
444  result = t.lastMeasurement().updatedState();
445  }
446  } else { //different sides
447 
449  result = t.firstMeasurement().updatedState();
450  } else {
451  result = t.lastMeasurement().updatedState();
452  }
453  }
454 
455  }
456 
457  return result;
458 
459 }
460 
462 
463  if (hits.size() < 2) return;
464  float dis1 = (hits.front()->globalPosition() - tsos.globalPosition()).mag();
465  float dis2 = (hits.back()->globalPosition() - tsos.globalPosition()).mag();
466 
467  if ( dis1 > dis2 )
468  std::reverse(hits.begin(),hits.end());
469 
470  return;
471 }
472 
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:234
T getParameter(std::string const &) const
void sortHitsAlongMom(ConstRecHitContainer &hits, const TrajectoryStateOnSurface &) const
const MuonServiceProxy * theService
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:265
T mag() const
The vector magnitude. Equivalent to sqrt(vec.mag2())
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:62
GlobalPoint globalPosition() const
const Propagator * propagatorOpposite() const
const Propagator * propagatorAlong() const
ConstRecHitContainer recHits(bool splitting=false) const
Definition: Trajectory.cc:67
U second(std::pair< T, U > const &p)
DataContainer const & measurements() const
Definition: Trajectory.h:203
std::vector< Trajectory > smooth(const std::vector< Trajectory > &) const
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
T z() const
Definition: PV3DBase.h:63
tuple result
Definition: query.py:137
TrajectoryStateOnSurface updatedState() const
std::string thePropagatorAlongName
TrajectoryStateOnSurface stepPropagate(const TrajectoryStateOnSurface &, const ConstRecHitPointer &, const Propagator &) const
#define LogTrace(id)
virtual std::pair< bool, double > estimate(const TrajectoryStateOnSurface &, const TransientTrackingRecHit &) const
CosmicMuonUtilities * theUtilities
virtual TrajectoryStateOnSurface propagate(const FreeTrajectoryState &, const Surface &) const
Definition: Propagator.cc:12
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:194
TrajectoryStateOnSurface initialState(const Trajectory &) const
Chi2MeasurementEstimator * theEstimator
std::vector< Trajectory > fit(const Trajectory &) const
T eta() const
Definition: PV3DBase.h:75
GlobalVector globalMomentum() const
TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TransientTrackingRecHit &) const
Definition: KFUpdator.cc:10
const Surface & surface() const
CosmicMuonSmoother(const edm::ParameterSet &, const MuonServiceProxy *service)
TransientTrackingRecHit::ConstRecHitContainer ConstRecHitContainer
virtual std::vector< Trajectory > trajectories(const Trajectory &) const
void reverseDirection(TrajectoryStateOnSurface &, const MagneticField *) const
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:35
std::string thePropagatorOppositeName