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 //
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  // FIXME FIXME CLONE !!!
151  // TrackingRecHit::RecHitPointer preciseHit = hits.front()->clone(predTsos);
152  auto preciseHit = hits.front();
153 
154  LogTrace(category_)<<"first hit is at det "<< hits.front()->det()->surface().position();
155 
156  currTsos = theUpdator->update(predTsos, *preciseHit);
157  if (!currTsos.isValid()){
159  <<"an updated state is not valid. killing the trajectory.";
160  return vector<Trajectory>();
161  }
162  myTraj.push(TrajectoryMeasurement(predTsos, currTsos, hits.front(),
163  theEstimator->estimate(predTsos, *hits.front()).second));
164  if ( currTsos.isValid() ) LogTrace(category_)<< "first curr TSOS: "<<currTsos;
165 
166  } else {
167 
168  currTsos = predTsos;
169  myTraj.push(TrajectoryMeasurement(predTsos, hits.front()));
170  }
171  //const TransientTrackingRecHit& firsthit = *hits.front();
172 
173  for ( ConstRecHitContainer::const_iterator ihit = hits.begin() + 1;
174  ihit != hits.end(); ++ihit ) {
175 
176  if ( !(**ihit).isValid() ) {
177  LogTrace(category_)<< "Error: invalid hit.";
178  continue;
179  }
180  if (currTsos.isValid()) {
181  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
182  <<"mom "<<currTsos.globalMomentum();
183  } else {
184  LogTrace(category_)<<"current state invalid";
185  }
186 
187  predTsos = propagatorAlong()->propagate(currTsos, (**ihit).det()->surface());
188  LogTrace(category_)<<"predicted state propagate directly "<<predTsos.isValid();
189 
190  if ( !predTsos.isValid() ) {
191  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
192  predTsos = theUtilities->stepPropagate(currTsos, (*ihit), *propagatorAlong());
193  }
194  if ( !predTsos.isValid() && (fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) && (theService->propagator("StraightLinePropagator").isValid() ) ) {
195  LogTrace(category_)<<"straight-line propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
196  predTsos = theService->propagator("StraightLinePropagator")->propagate(currTsos, (**ihit).det()->surface());
197  }
198  if (predTsos.isValid()) {
199  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
200  <<"mom "<<predTsos.globalMomentum();
201  } else {
202  LogTrace(category_)<<"predicted state invalid";
203  }
204  if ( !predTsos.isValid() ) {
205  LogTrace(category_)<< "Error: predTsos is still invalid forward fit.";
206 // return vector<Trajectory>();
207  continue;
208  } else if ( (**ihit).isValid() ) {
209  // FIXME FIXME CLONE !!!
210  // update (FIXME!)
211  // TransientTrackingRecHit::RecHitPointer preciseHit = (**ihit).clone(predTsos);
212  auto preciseHit = *ihit;
213 
214  if ( !preciseHit->isValid() ) {
215  currTsos = predTsos;
216  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
217  } else {
218  currTsos = theUpdator->update(predTsos, *preciseHit);
219  if (!currTsos.isValid()){
221  <<"an updated state is not valid. killing the trajectory.";
222  return vector<Trajectory>();
223  }
224  myTraj.push(TrajectoryMeasurement(predTsos, currTsos, preciseHit,
225  theEstimator->estimate(predTsos, *preciseHit).second));
226  }
227  } else {
228  currTsos = predTsos;
229  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
230  }
231 
232  }
233 
234  std::vector<TrajectoryMeasurement> mytms = myTraj.measurements();
235  LogTrace(category_)<<"fit result "<<mytms.size();
236  for (std::vector<TrajectoryMeasurement>::const_iterator itm = mytms.begin();
237  itm != mytms.end(); ++itm ) {
238  LogTrace(category_)<<"updated pos "<<itm->updatedState().globalPosition()
239  <<"mom "<<itm->updatedState().globalMomentum();
240  }
241 
242 
243  return vector<Trajectory>(1, myTraj);
244 
245 }
246 
247 
248 //
249 // smooth trajectory
250 //
251 vector<Trajectory> CosmicMuonSmoother::smooth(const vector<Trajectory>& tc) const {
252 
253  vector<Trajectory> result;
254 
255  for ( vector<Trajectory>::const_iterator it = tc.begin(); it != tc.end(); ++it ) {
256  vector<Trajectory> smoothed = smooth(*it);
257  result.insert(result.end(), smoothed.begin(), smoothed.end());
258  }
259 
260  return result;
261 
262 }
263 
264 
265 //
266 // smooth trajectory
267 //
268 vector<Trajectory> CosmicMuonSmoother::smooth(const Trajectory& t) const {
269 
270  if ( t.empty() ) {
271  LogTrace(category_)<< "Error: smooth: empty trajectory.";
272  return vector<Trajectory>();
273  }
274 
275  Trajectory myTraj(t.seed(), oppositeToMomentum);
276 
277  vector<TrajectoryMeasurement> avtm = t.measurements();
278 
279  if ( avtm.size() < 2 ) {
280  LogTrace(category_)<< "Error: smooth: too little TM. ";
281  return vector<Trajectory>();
282  }
283 
284  TrajectoryStateOnSurface predTsos = avtm.back().forwardPredictedState();
286 
287  if ( !predTsos.isValid() ) {
288  LogTrace(category_)<< "Error: smooth: first TSOS from back invalid. ";
289  return vector<Trajectory>();
290  }
291 
292  TrajectoryStateOnSurface currTsos;
293 
294  // first smoothed TrajectoryMeasurement is last fitted
295  if ( avtm.back().recHit()->isValid() ) {
296  currTsos = theUpdator->update(predTsos, (*avtm.back().recHit()));
297  if (!currTsos.isValid()){
299  <<"an updated state is not valid. killing the trajectory.";
300  return vector<Trajectory>();
301  }
302  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
303  predTsos,
304  avtm.back().updatedState(),
305  avtm.back().recHit(),
306  avtm.back().estimate()//,
307  /*avtm.back().layer()*/),
308  avtm.back().estimate());
309 
310  } else {
311  currTsos = predTsos;
312  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
313  avtm.back().recHit()//,
314  /*avtm.back().layer()*/));
315 
316  }
317 
319 
320 
321  for ( vector<TrajectoryMeasurement>::reverse_iterator itm = avtm.rbegin() + 1;
322  itm != avtm.rend() - 1; ++itm ) {
323 
324  if (currTsos.isValid()) {
325  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
326  <<"mom "<<currTsos.globalMomentum();
327  } else {
328  LogTrace(category_)<<"current state invalid";
329  }
330 
331  predTsos = propagatorOpposite()->propagate(currTsos,(*itm).recHit()->det()->surface());
332 
333  if ( !predTsos.isValid() ) {
334  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*itm).recHit()->globalPosition();
335  predTsos = theUtilities->stepPropagate(currTsos, (*itm).recHit(), *propagatorOpposite());
336  }
337  if (predTsos.isValid()) {
338  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
339  <<"mom "<<predTsos.globalMomentum();
340  } else {
341  LogTrace(category_)<<"predicted state invalid";
342  }
343 
344  if ( !predTsos.isValid() ) {
345  LogTrace(category_)<< "Error: predTsos is still invalid backward smooth.";
346  return vector<Trajectory>();
347  // continue;
348  } else if ( (*itm).recHit()->isValid() ) {
349  //update
350  currTsos = theUpdator->update(predTsos, (*(*itm).recHit()));
351  if (!currTsos.isValid()){
353  <<"an updated state is not valid. killing the trajectory.";
354  return vector<Trajectory>();
355  }
356  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
357  if ( !combTsos.isValid() ) {
358  LogTrace(category_)<< "Error: smooth: combining pred TSOS failed. ";
359  return vector<Trajectory>();
360  }
361 
362  TrajectoryStateOnSurface smooTsos = combiner((*itm).updatedState(), predTsos);
363 
364  if ( !smooTsos.isValid() ) {
365  LogTrace(category_)<< "Error: smooth: combining smooth TSOS failed. ";
366  return vector<Trajectory>();
367  }
368 
369  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
370  predTsos,
371  smooTsos,
372  (*itm).recHit(),
373  theEstimator->estimate(combTsos, (*(*itm).recHit())).second//,
374  /*(*itm).layer()*/),
375  (*itm).estimate());
376  } else {
377  currTsos = predTsos;
378  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
379 
380  if ( !combTsos.isValid() ) {
381  LogTrace(category_)<< "Error: smooth: combining TSOS failed. ";
382  return vector<Trajectory>();
383  }
384 
385  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
386  predTsos,
387  combTsos,
388  (*itm).recHit()//,
389  /*(*itm).layer()*/));
390  }
391  }
392 
393  // last smoothed TrajectoryMeasurement is last filtered
394  predTsos = propagatorOpposite()->propagate(currTsos, avtm.front().recHit()->det()->surface());
395 
396  if ( !predTsos.isValid() ){
397  LogTrace(category_)<< "Error: last predict TSOS failed, use original one. ";
398  // return vector<Trajectory>();
399  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
400  avtm.front().recHit()));
401  } else {
402  if ( avtm.front().recHit()->isValid() ) {
403  //update
404  currTsos = theUpdator->update(predTsos, (*avtm.front().recHit()));
405  if (currTsos.isValid())
406  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
407  predTsos,
408  currTsos,
409  avtm.front().recHit(),
410  theEstimator->estimate(predTsos, (*avtm.front().recHit())).second//,
411  /*avtm.front().layer()*/),
412  avtm.front().estimate());
413  }
414  }
415  LogTrace(category_)<< "myTraj foundHits. "<<myTraj.foundHits();
416 
417  if (myTraj.foundHits() >= 3)
418  return vector<Trajectory>(1, myTraj);
419  else {
420  LogTrace(category_)<< "Error: smooth: No enough hits in trajctory. ";
421  return vector<Trajectory>();
422  }
423 
424 }
425 
427  if ( t.empty() ) return TrajectoryStateOnSurface();
428 
430 
432 
433  bool beamhaloFlag = ( t.firstMeasurement().updatedState().globalMomentum().eta() > 4.0 || t.lastMeasurement().updatedState().globalMomentum().eta() > 4.0 );
434 
435  if ( !beamhaloFlag ) { //initialState is the top one
437  result = t.firstMeasurement().updatedState();
438  } else {
439  result = t.lastMeasurement().updatedState();
440  }
441  if (result.globalMomentum().y()> 1.0 ) //top tsos should pointing down
442  theUtilities->reverseDirection(result,&*theService->magneticField());
443  } else {
444  if ( t.firstMeasurement().updatedState().globalPosition().z() * t.lastMeasurement().updatedState().globalPosition().z() >0 ) { //same side
445  if ( fabs(t.firstMeasurement().updatedState().globalPosition().z()) > fabs(t.lastMeasurement().updatedState().globalPosition().z()) ) {
446  result = t.firstMeasurement().updatedState();
447  } else {
448  result = t.lastMeasurement().updatedState();
449  }
450  } else { //different sides
451 
453  result = t.firstMeasurement().updatedState();
454  } else {
455  result = t.lastMeasurement().updatedState();
456  }
457  }
458 
459  }
460 
461  return result;
462 
463 }
464 
466 
467  if (hits.size() < 2) return;
468  float dis1 = (hits.front()->globalPosition() - tsos.globalPosition()).mag();
469  float dis2 = (hits.back()->globalPosition() - tsos.globalPosition()).mag();
470 
471  if ( dis1 > dis2 )
472  std::reverse(hits.begin(),hits.end());
473 
474  return;
475 }
476 
bool empty() const
True if trajectory has no measurements.
Definition: Trajectory.h:244
T getParameter(std::string const &) const
void sortHitsAlongMom(ConstRecHitContainer &hits, const TrajectoryStateOnSurface &) const
virtual FreeTrajectoryState propagate(const FreeTrajectoryState &ftsStart, const GlobalPoint &pDest) const final
Definition: Propagator.h:119
virtual TrajectoryContainer trajectories(const Trajectory &traj) const override
const MuonServiceProxy * theService
TrajectorySeed const & seed() const
Access to the seed used to reconstruct the Trajectory.
Definition: Trajectory.h:275
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
Trajectory trajectory(const Trajectory &) const override
const Propagator * propagatorOpposite() const
const Propagator * propagatorAlong() const
const Chi2MeasurementEstimator * theEstimator
U second(std::pair< T, U > const &p)
DataContainer const & measurements() const
Definition: Trajectory.h:203
const SurfaceType & surface() const
TrajectoryStateOnSurface update(const TrajectoryStateOnSurface &, const TrackingRecHit &) const
Definition: KFUpdator.cc:10
const CosmicMuonUtilities * theUtilities
std::vector< Trajectory > smooth(const std::vector< Trajectory > &) const
TrajectoryMeasurement const & lastMeasurement() const
Definition: Trajectory.h:181
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)
const KFUpdator * theUpdator
TrajectoryMeasurement const & firstMeasurement() const
Definition: Trajectory.h:194
TrajectoryStateOnSurface initialState(const Trajectory &) const
std::vector< Trajectory > fit(const Trajectory &) const
T eta() const
Definition: PV3DBase.h:76
GlobalVector globalMomentum() const
virtual std::pair< bool, double > estimate(const TrajectoryStateOnSurface &, const TrackingRecHit &) const
CosmicMuonSmoother(const edm::ParameterSet &, const MuonServiceProxy *service)
TransientTrackingRecHit::ConstRecHitContainer ConstRecHitContainer
TrajectoryStateOnSurface const & updatedState() const
void reverseDirection(TrajectoryStateOnSurface &, const MagneticField *) const
void push(const TrajectoryMeasurement &tm)
Definition: Trajectory.cc:30
std::string thePropagatorOppositeName