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