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