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  // 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() && currTsos.globalMomentum().mag2() > 1e-18f) {
181  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
182  <<"mom "<<currTsos.globalMomentum();
183  } else {
184  LogTrace(category_)<<"current state invalid or momentum is too low";
185  //logically, there's no way out: can't expect a valid result out of an invalid state
187  <<"Input state is not valid. This loop over hits is doomed: breaking out";
188  break;
189  }
190 
191  predTsos = propagatorAlong()->propagate(currTsos, (**ihit).det()->surface());
192  LogTrace(category_)<<"predicted state propagate directly "<<predTsos.isValid();
193 
194  if ( !predTsos.isValid() ) {
195  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
196  predTsos = theUtilities->stepPropagate(currTsos, (*ihit), *propagatorAlong());
197  }
198  if ( !predTsos.isValid() && (fabs(theService->magneticField()->inTesla(GlobalPoint(0,0,0)).z()) < 0.01) && (theService->propagator("StraightLinePropagator").isValid() ) ) {
199  LogTrace(category_)<<"straight-line propagating from "<<currTsos.globalPosition() <<" to position: "<<(*ihit)->globalPosition();
200  predTsos = theService->propagator("StraightLinePropagator")->propagate(currTsos, (**ihit).det()->surface());
201  }
202  if (predTsos.isValid()) {
203  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
204  <<"mom "<<predTsos.globalMomentum();
205  } else {
206  LogTrace(category_)<<"predicted state invalid";
207  }
208  if ( !predTsos.isValid() ) {
209  LogTrace(category_)<< "Error: predTsos is still invalid forward fit.";
210 // return vector<Trajectory>();
211  continue;
212  } else if ( (**ihit).isValid() ) {
213  // FIXME FIXME CLONE !!!
214  // update (FIXME!)
215  // TransientTrackingRecHit::RecHitPointer preciseHit = (**ihit).clone(predTsos);
216  auto preciseHit = *ihit;
217 
218  if ( !preciseHit->isValid() ) {
219  currTsos = predTsos;
220  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
221  } else {
222  currTsos = theUpdator->update(predTsos, *preciseHit);
223  if (!currTsos.isValid()){
225  <<"an updated state is not valid. killing the trajectory.";
226  return vector<Trajectory>();
227  }
228  myTraj.push(TrajectoryMeasurement(predTsos, currTsos, preciseHit,
229  theEstimator->estimate(predTsos, *preciseHit).second));
230  }
231  } else {
232  currTsos = predTsos;
233  myTraj.push(TrajectoryMeasurement(predTsos, *ihit));
234  }
235 
236  }
237 
238  std::vector<TrajectoryMeasurement> mytms = myTraj.measurements();
239  LogTrace(category_)<<"fit result "<<mytms.size();
240  for (std::vector<TrajectoryMeasurement>::const_iterator itm = mytms.begin();
241  itm != mytms.end(); ++itm ) {
242  LogTrace(category_)<<"updated pos "<<itm->updatedState().globalPosition()
243  <<"mom "<<itm->updatedState().globalMomentum();
244  }
245 
246 
247  return vector<Trajectory>(1, myTraj);
248 
249 }
250 
251 
252 //
253 // smooth trajectory
254 //
255 vector<Trajectory> CosmicMuonSmoother::smooth(const vector<Trajectory>& tc) const {
256 
257  vector<Trajectory> result;
258 
259  for ( vector<Trajectory>::const_iterator it = tc.begin(); it != tc.end(); ++it ) {
260  vector<Trajectory> smoothed = smooth(*it);
261  result.insert(result.end(), smoothed.begin(), smoothed.end());
262  }
263 
264  return result;
265 
266 }
267 
268 
269 //
270 // smooth trajectory
271 //
272 vector<Trajectory> CosmicMuonSmoother::smooth(const Trajectory& t) const {
273 
274  if ( t.empty() ) {
275  LogTrace(category_)<< "Error: smooth: empty trajectory.";
276  return vector<Trajectory>();
277  }
278 
279  Trajectory myTraj(t.seed(), oppositeToMomentum);
280 
281  vector<TrajectoryMeasurement> avtm = t.measurements();
282 
283  if ( avtm.size() < 2 ) {
284  LogTrace(category_)<< "Error: smooth: too little TM. ";
285  return vector<Trajectory>();
286  }
287 
288  TrajectoryStateOnSurface predTsos = avtm.back().forwardPredictedState();
290 
291  if ( !predTsos.isValid() ) {
292  LogTrace(category_)<< "Error: smooth: first TSOS from back invalid. ";
293  return vector<Trajectory>();
294  }
295 
296  TrajectoryStateOnSurface currTsos;
297 
298  // first smoothed TrajectoryMeasurement is last fitted
299  if ( avtm.back().recHit()->isValid() ) {
300  currTsos = theUpdator->update(predTsos, (*avtm.back().recHit()));
301  if (!currTsos.isValid()){
303  <<"an updated state is not valid. killing the trajectory.";
304  return vector<Trajectory>();
305  }
306  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
307  predTsos,
308  avtm.back().updatedState(),
309  avtm.back().recHit(),
310  avtm.back().estimate()//,
311  /*avtm.back().layer()*/),
312  avtm.back().estimate());
313 
314  } else {
315  currTsos = predTsos;
316  myTraj.push(TrajectoryMeasurement(avtm.back().forwardPredictedState(),
317  avtm.back().recHit()//,
318  /*avtm.back().layer()*/));
319 
320  }
321 
323 
324 
325  for ( vector<TrajectoryMeasurement>::reverse_iterator itm = avtm.rbegin() + 1;
326  itm != avtm.rend() - 1; ++itm ) {
327 
328  if (currTsos.isValid()) {
329  LogTrace(category_)<<"current pos "<<currTsos.globalPosition()
330  <<"mom "<<currTsos.globalMomentum();
331  } else {
332  LogTrace(category_)<<"current state invalid";
333  }
334 
335  predTsos = propagatorOpposite()->propagate(currTsos,(*itm).recHit()->det()->surface());
336 
337  if ( !predTsos.isValid() ) {
338  LogTrace(category_)<<"step-propagating from "<<currTsos.globalPosition() <<" to position: "<<(*itm).recHit()->globalPosition();
339  predTsos = theUtilities->stepPropagate(currTsos, (*itm).recHit(), *propagatorOpposite());
340  }
341  if (predTsos.isValid()) {
342  LogTrace(category_)<<"predicted pos "<<predTsos.globalPosition()
343  <<"mom "<<predTsos.globalMomentum();
344  } else {
345  LogTrace(category_)<<"predicted state invalid";
346  }
347 
348  if ( !predTsos.isValid() ) {
349  LogTrace(category_)<< "Error: predTsos is still invalid backward smooth.";
350  return vector<Trajectory>();
351  // continue;
352  } else if ( (*itm).recHit()->isValid() ) {
353  //update
354  currTsos = theUpdator->update(predTsos, (*(*itm).recHit()));
355  if (!currTsos.isValid()){
357  <<"an updated state is not valid. killing the trajectory.";
358  return vector<Trajectory>();
359  }
360  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
361  if ( !combTsos.isValid() ) {
362  LogTrace(category_)<< "Error: smooth: combining pred TSOS failed. ";
363  return vector<Trajectory>();
364  }
365 
366  TrajectoryStateOnSurface smooTsos = combiner((*itm).updatedState(), predTsos);
367 
368  if ( !smooTsos.isValid() ) {
369  LogTrace(category_)<< "Error: smooth: combining smooth TSOS failed. ";
370  return vector<Trajectory>();
371  }
372 
373  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
374  predTsos,
375  smooTsos,
376  (*itm).recHit(),
377  theEstimator->estimate(combTsos, (*(*itm).recHit())).second//,
378  /*(*itm).layer()*/),
379  (*itm).estimate());
380  } else {
381  currTsos = predTsos;
382  TrajectoryStateOnSurface combTsos = combiner(predTsos, (*itm).forwardPredictedState());
383 
384  if ( !combTsos.isValid() ) {
385  LogTrace(category_)<< "Error: smooth: combining TSOS failed. ";
386  return vector<Trajectory>();
387  }
388 
389  myTraj.push(TrajectoryMeasurement((*itm).forwardPredictedState(),
390  predTsos,
391  combTsos,
392  (*itm).recHit()//,
393  /*(*itm).layer()*/));
394  }
395  }
396 
397  // last smoothed TrajectoryMeasurement is last filtered
398  predTsos = propagatorOpposite()->propagate(currTsos, avtm.front().recHit()->det()->surface());
399 
400  if ( !predTsos.isValid() ){
401  LogTrace(category_)<< "Error: last predict TSOS failed, use original one. ";
402  // return vector<Trajectory>();
403  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
404  avtm.front().recHit()));
405  } else {
406  if ( avtm.front().recHit()->isValid() ) {
407  //update
408  currTsos = theUpdator->update(predTsos, (*avtm.front().recHit()));
409  if (currTsos.isValid())
410  myTraj.push(TrajectoryMeasurement(avtm.front().forwardPredictedState(),
411  predTsos,
412  currTsos,
413  avtm.front().recHit(),
414  theEstimator->estimate(predTsos, (*avtm.front().recHit())).second//,
415  /*avtm.front().layer()*/),
416  avtm.front().estimate());
417  }
418  }
419  LogTrace(category_)<< "myTraj foundHits. "<<myTraj.foundHits();
420 
421  if (myTraj.foundHits() >= 3)
422  return vector<Trajectory>(1, myTraj);
423  else {
424  LogTrace(category_)<< "Error: smooth: No enough hits in trajctory. ";
425  return vector<Trajectory>();
426  }
427 
428 }
429 
431  if ( t.empty() ) return TrajectoryStateOnSurface();
432 
434 
436 
437  bool beamhaloFlag = ( t.firstMeasurement().updatedState().globalMomentum().eta() > 4.0 || t.lastMeasurement().updatedState().globalMomentum().eta() > 4.0 );
438 
439  if ( !beamhaloFlag ) { //initialState is the top one
441  result = t.firstMeasurement().updatedState();
442  } else {
443  result = t.lastMeasurement().updatedState();
444  }
445  if (result.globalMomentum().y()> 1.0 ) //top tsos should pointing down
446  theUtilities->reverseDirection(result,&*theService->magneticField());
447  } else {
448  if ( t.firstMeasurement().updatedState().globalPosition().z() * t.lastMeasurement().updatedState().globalPosition().z() >0 ) { //same side
449  if ( fabs(t.firstMeasurement().updatedState().globalPosition().z()) > fabs(t.lastMeasurement().updatedState().globalPosition().z()) ) {
450  result = t.firstMeasurement().updatedState();
451  } else {
452  result = t.lastMeasurement().updatedState();
453  }
454  } else { //different sides
455 
457  result = t.firstMeasurement().updatedState();
458  } else {
459  result = t.lastMeasurement().updatedState();
460  }
461  }
462 
463  }
464 
465  return result;
466 
467 }
468 
470 
471  if (hits.size() < 2) return;
472  float dis1 = (hits.front()->globalPosition() - tsos.globalPosition()).mag();
473  float dis2 = (hits.back()->globalPosition() - tsos.globalPosition()).mag();
474 
475  if ( dis1 > dis2 )
476  std::reverse(hits.begin(),hits.end());
477 
478  return;
479 }
480 
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
T mag2() const
Definition: PV3DBase.h:66
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:75
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
double f[11][100]
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