CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
RKPropagatorInS.cc
Go to the documentation of this file.
2 #include "RKCartesianDistance.h"
4 #include "RKLocalFieldProvider.h"
7 #include "RKAdaptiveSolver.h"
8 #include "RKOne4OrderStep.h"
9 #include "RKOneCashKarpStep.h"
10 #include "PathToPlane2Order.h"
11 #include "CartesianStateAdaptor.h"
16 #include "FrameChanger.h"
21 
24 
25 
26 std::pair<TrajectoryStateOnSurface,double>
27 RKPropagatorInS::propagateWithPath(const FreeTrajectoryState& fts,
28  const Plane& plane) const
29 {
30  GlobalParametersWithPath gp = propagateParametersOnPlane( fts, plane);
31  if unlikely(!gp) return TsosWP(TrajectoryStateOnSurface(),0.);
32 
33  SurfaceSideDefinition::SurfaceSide side = PropagationDirectionFromPath()(gp.s(),propagationDirection())==alongMomentum
34  ? SurfaceSideDefinition::beforeSurface : SurfaceSideDefinition::afterSurface;
35  return analyticalErrorPropagation( fts, plane, side, gp.parameters(),gp.s());
36 }
37 
38 std::pair< TrajectoryStateOnSurface, double>
39 RKPropagatorInS::propagateWithPath (const FreeTrajectoryState& fts, const Cylinder& cyl) const
40 {
41  GlobalParametersWithPath gp = propagateParametersOnCylinder( fts, cyl);
42  if unlikely(!gp) return TsosWP(TrajectoryStateOnSurface(),0.);
43 
44  SurfaceSideDefinition::SurfaceSide side = PropagationDirectionFromPath()(gp.s(),propagationDirection())==alongMomentum
45  ? SurfaceSideDefinition::beforeSurface : SurfaceSideDefinition::afterSurface;
46  return analyticalErrorPropagation( fts, cyl, side, gp.parameters(),gp.s());
47 
48 }
49 
51 RKPropagatorInS::propagateParametersOnPlane( const FreeTrajectoryState& ts,
52  const Plane& plane) const
53 {
54 
55  GlobalPoint gpos( ts.position());
56  GlobalVector gmom( ts.momentum());
57  double startZ = plane.localZ(gpos);
58  // (transverse) curvature
59  double rho = ts.transverseCurvature();
60  //
61  // Straight line approximation? |rho|<1.e-10 equivalent to ~ 1um
62  // difference in transversal position at 10m.
63  //
64  if unlikely( fabs(rho)<1.e-10 ) {
65  //
66  // Instantiate auxiliary object for finding intersection.
67  // Frame-independant point and vector are created explicitely to
68  // avoid confusing gcc (refuses to compile with temporary objects
69  // in the constructor).
70  //
71  LogDebug("RKPropagatorInS")<<" startZ = "<<startZ;
72 
73  if unlikely(fabs(startZ) < 1e-5){
74  LogDebug("RKPropagatorInS")<< "Propagation is not performed: state is already on final surface.";
75  GlobalTrajectoryParameters res( gpos, gmom, ts.charge(),
76  theVolume);
77  return GlobalParametersWithPath( res, 0.0);
78  }
79 
82  StraightLinePlaneCrossing planeCrossing(pos,dir, propagationDirection());
83  //
84  // get solution
85  //
86  std::pair<bool,double> propResult = planeCrossing.pathLength(plane);
87  if likely( propResult.first && theVolume !=0) {
88  double s = propResult.second;
89  // point (reconverted to GlobalPoint)
90  GlobalPoint x (planeCrossing.position(s));
91  GlobalTrajectoryParameters res( x, gmom, ts.charge(), theVolume);
92  return GlobalParametersWithPath( res, s);
93  }
94  //do someting
95  LogDebug("RKPropagatorInS")<< "Straight line propgation to plane failed !!";
96  return GlobalParametersWithPath( );
97 
98  }
99 
100 
101 #ifdef EDM_LM_DEBUG
102  if (theVolume != 0) {
103  LogDebug("RKPropagatorInS") << "RKPropagatorInS: starting prop to plane in volume with pos " << theVolume->position()
104  << " Z axis " << theVolume->toGlobal( LocalVector(0,0,1)) ;
105 
106  LogDebug("RKPropagatorInS") << "The starting position is " << ts.position() << " (global) "
107  << theVolume->toLocal(ts.position()) << " (local) " ;
108 
109  FrameChanger changer;
110  FrameChanger::PlanePtr localPlane = changer.transformPlane( plane, *theVolume);
111  LogDebug("RKPropagatorInS") << "The plane position is " << plane.position() << " (global) "
112  << localPlane->position() << " (local) " ;
113 
114  LogDebug("RKPropagatorInS") << "The initial distance to plane is " << plane.localZ( ts.position()) ;
115 
116  StraightLinePlaneCrossing cross( ts.position().basicVector(), ts.momentum().basicVector());
117  std::pair<bool,double> res3 = cross.pathLength(plane);
118  LogDebug("RKPropagatorInS") << "straight line distance " << res3.first << " " << res3.second ;
119  }
120 #endif
121 
122  typedef RKAdaptiveSolver<double,RKOneCashKarpStep, 6> Solver;
123  typedef Solver::Vector RKVector;
124 
125  RKLocalFieldProvider field( fieldProvider());
126  PathToPlane2Order pathLength( field, &field.frame());
127  CartesianLorentzForce deriv(field, ts.charge());
128 
129  RKCartesianDistance dist;
130  double eps = theTolerance;
131  Solver solver;
132  double stot = 0;
133  PropagationDirection currentDirection = propagationDirection();
134 
135  // in magVolume frame
136  RKVector start( CartesianStateAdaptor::rkstate( rkPosition(gpos), rkMomentum(gmom)));
137  int safeGuard = 0;
138  while (safeGuard++<100) {
139  CartesianStateAdaptor startState(start);
140 
141  std::pair<bool,double> path = pathLength( plane, startState.position(),
142  startState.momentum(),
143  (double) ts.charge(), currentDirection);
144  if unlikely(!path.first) {
145  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Path length calculation to plane failed!"
146  << "...distance to plane " << plane.localZ( globalPosition(startState.position()))
147  << "...Local starting position in volume " << startState.position()
148  << "...Magnetic field " << field.inTesla( startState.position()) ;
149 
150 
151  return GlobalParametersWithPath();
152  }
153 
154  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Path lenght to plane is " << path.second ;
155 
156 
157  double sstep = path.second;
158  if unlikely( std::abs(sstep) < eps) {
159  LogDebug("RKPropagatorInS") << "On-surface accuracy not reached, but pathLength calculation says we are there! "
160  << "path " << path.second << " distance to plane is " << startZ ;
161  GlobalTrajectoryParameters res( gtpFromVolumeLocal( startState, ts.charge()));
162  return GlobalParametersWithPath( res, stot);
163  }
164 
165  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Solving for " << sstep
166  << " current distance to plane is " << startZ ;
167 
168  RKVector rkresult = solver( 0, start, sstep, deriv, dist, eps);
169  stot += sstep;
170  CartesianStateAdaptor cur( rkresult);
171  double remainingZ = plane.localZ( globalPosition(cur.position()));
172 
173  if ( fabs(remainingZ) < eps) {
174  LogDebug("RKPropagatorInS") << "On-surface accuracy reached! " << remainingZ ;
175  GlobalTrajectoryParameters res( gtpFromVolumeLocal( cur, ts.charge()));
176  return GlobalParametersWithPath( res, stot);
177  }
178 
179  start = rkresult;
180 
181  if (remainingZ * startZ > 0) {
182  LogDebug("RKPropagatorInS") << "Accuracy not reached yet, trying in same direction again "
183  << remainingZ ;
184  }
185  else {
186  LogDebug("RKPropagatorInS") << "Accuracy not reached yet, trying in opposite direction "
187  << remainingZ ;
188  currentDirection = invertDirection( currentDirection);
189  }
190  startZ = remainingZ;
191  }
192 
193  edm::LogError("FailedPropagation") << " too many iterations trying to reach plane ";
194  return GlobalParametersWithPath();
195 }
196 
198 RKPropagatorInS::propagateParametersOnCylinder( const FreeTrajectoryState& ts,
199  const Cylinder& cyl) const {
200 
201  typedef RKAdaptiveSolver<double,RKOneCashKarpStep, 6> Solver;
202  typedef Solver::Vector RKVector;
203 
204 
205  GlobalPoint sp = cyl.position();
206  if unlikely(sp.x()!=0. || sp.y()!=0.) {
207  throw PropagationException("Cannot propagate to an arbitrary cylinder");
208  }
209 
210  GlobalPoint gpos( ts.position());
211  GlobalVector gmom( ts.momentum());
212  LocalPoint pos(cyl.toLocal(gpos));
213  LocalVector mom(cyl.toLocal(gmom));
214  double startR = cyl.radius() - pos.perp();
215 
216  // LogDebug("RKPropagatorInS") << "RKPropagatorInS: starting from FTS " << ts ;
217 
218 
219  // (transverse) curvature
220  double rho = ts.transverseCurvature();
221  //
222  // Straight line approximation? |rho|<1.e-10 equivalent to ~ 1um
223  // difference in transversal position at 10m.
224  //
225  if unlikely( fabs(rho)<1.e-10 ) {
226  //
227  // Instantiate auxiliary object for finding intersection.
228  // Frame-independant point and vector are created explicitely to
229  // avoid confusing gcc (refuses to compile with temporary objects
230  // in the constructor).
231  //
232 
233  StraightLineBarrelCylinderCrossing cylCrossing(gpos,gmom,propagationDirection());
234 
235  //
236  // get solution
237  //
238  std::pair<bool,double> propResult = cylCrossing.pathLength(cyl);
239  if likely( propResult.first && theVolume !=0) {
240  double s = propResult.second;
241  // point (reconverted to GlobalPoint)
242  GlobalPoint x (cylCrossing.position(s));
243  GlobalTrajectoryParameters res( x, gmom, ts.charge(), theVolume);
244  LogDebug("RKPropagatorInS") << "Straight line propagation to cylinder succeeded !!";
245  return GlobalParametersWithPath( res, s);
246  }
247 
248  //do someting
249  edm::LogError("RKPropagatorInS") <<"Straight line propagation to cylinder failed !!";
250  return GlobalParametersWithPath( );
251 
252  }
253 
254 
255  RKLocalFieldProvider field( fieldProvider(cyl));
256  // StraightLineCylinderCrossing pathLength( pos, mom, propagationDirection());
257  CartesianLorentzForce deriv(field, ts.charge());
258 
259  RKCartesianDistance dist;
260  double eps = theTolerance;
261  Solver solver;
262  double stot = 0;
263  PropagationDirection currentDirection = propagationDirection();
264 
265  RKVector start( CartesianStateAdaptor::rkstate( pos.basicVector(), mom.basicVector()));
266  int safeGuard = 0;
267  while (safeGuard++<100) {
268  CartesianStateAdaptor startState(start);
269  StraightLineCylinderCrossing pathLength( LocalPoint(startState.position()),
270  LocalVector(startState.momentum()),
271  currentDirection, eps);
272 
273  std::pair<bool,double> path = pathLength.pathLength( cyl);
274  if unlikely(!path.first) {
275  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Path length calculation to cylinder failed!"
276  << "Radius " << cyl.radius() << " pos.perp() " << LocalPoint(startState.position()).perp() ;
277  return GlobalParametersWithPath();
278  }
279 
280  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Path lenght to cylinder is " << path.second
281  << " from point (R,z) " << startState.position().perp()
282  << ", " << startState.position().z()
283  << " to R " << cyl.radius()
284  ;
285 
286 
287  double sstep = path.second;
288  if unlikely( std::abs(sstep) < eps) {
289  LogDebug("RKPropagatorInS") << "accuracy not reached, but pathLength calculation says we are there! "
290  << path.second ;
291 
292  GlobalTrajectoryParameters res( gtpFromLocal( startState.position(),
293  startState.momentum(),
294  ts.charge(), cyl));
295  return GlobalParametersWithPath( res, stot);
296  }
297 
298  LogDebug("RKPropagatorInS") << "RKPropagatorInS: Solving for " << sstep
299  << " current distance to cylinder is " << startR ;
300 
301  RKVector rkresult = solver( 0, start, sstep, deriv, dist, eps);
302  stot += sstep;
303  CartesianStateAdaptor cur( rkresult);
304  double remainingR = cyl.radius() - cur.position().perp();
305 
306  if ( fabs(remainingR) < eps) {
307  LogDebug("RKPropagatorInS") << "Accuracy reached! " << remainingR ;
308  GlobalTrajectoryParameters res( gtpFromLocal( cur.position(),
309  cur.momentum(),
310  ts.charge(), cyl));
311  return GlobalParametersWithPath( res, stot);
312  }
313 
314  start = rkresult;
315  if (remainingR * startR > 0) {
316  LogDebug("RKPropagatorInS") << "Accuracy not reached yet, trying in same direction again "
317  << remainingR ;
318  }
319  else {
320  LogDebug("RKPropagatorInS") << "Accuracy not reached yet, trying in opposite direction "
321  << remainingR ;
322  currentDirection = invertDirection( currentDirection);
323  }
324  startR = remainingR;
325  }
326 
327  edm::LogError("FailedPropagation") << " too many iterations trying to reach cylinder ";
328  return GlobalParametersWithPath();
329 }
330 
331 
333 {
334  return new RKPropagatorInS(*this);
335 }
336 
337 GlobalTrajectoryParameters RKPropagatorInS::gtpFromLocal( const Basic3DVector<double>& lpos,
338  const Basic3DVector<double>& lmom,
339  TrackCharge ch, const Surface& surf) const
340 {
341  return GlobalTrajectoryParameters( surf.toGlobal( LocalPoint( lpos)),
342  surf.toGlobal( LocalVector( lmom)), ch, theVolume);
343 }
344 
345 RKLocalFieldProvider RKPropagatorInS::fieldProvider() const
346 {
347  return RKLocalFieldProvider( *theVolume);
348 }
349 
350 RKLocalFieldProvider RKPropagatorInS::fieldProvider( const Cylinder& cyl) const
351 {
352  return RKLocalFieldProvider( *theVolume, cyl);
353 }
354 
355 PropagationDirection RKPropagatorInS::invertDirection( PropagationDirection dir) const
356 {
357  if (dir == anyDirection) return dir;
358  return ( dir == alongMomentum ? oppositeToMomentum : alongMomentum);
359 }
360 
361 Basic3DVector<double> RKPropagatorInS::rkPosition( const GlobalPoint& pos) const
362 {
363  if (theVolume != 0) return theVolume->toLocal( pos).basicVector();
364  else return pos.basicVector();
365 }
366 
367 Basic3DVector<double> RKPropagatorInS::rkMomentum( const GlobalVector& mom) const
368 {
369  if (theVolume != 0) return theVolume->toLocal( mom).basicVector();
370  else return mom.basicVector();
371 }
372 
373 GlobalPoint RKPropagatorInS::globalPosition( const Basic3DVector<double>& pos) const
374 {
375  if (theVolume != 0) return theVolume->toGlobal( LocalPoint(pos));
376  else return GlobalPoint(pos);
377 }
378 
379 GlobalVector RKPropagatorInS::globalMomentum( const Basic3DVector<double>& mom) const
380 
381 {
382  if (theVolume != 0) return theVolume->toGlobal( LocalVector(mom));
383  else return GlobalVector(mom);
384 }
385 
387 RKPropagatorInS::gtpFromVolumeLocal( const CartesianStateAdaptor& state,
388  TrackCharge charge) const
389 {
390  return GlobalTrajectoryParameters( globalPosition(state.position()),
391  globalMomentum(state.momentum()),
392  charge, theVolume);
393 }
#define LogDebug(id)
GlobalPoint toGlobal(const Point2DBase< Scalar, LocalTag > lp) const
Definition: Surface.h:114
dictionary parameters
Definition: Parameters.py:2
tuple start
Check for commandline option errors.
Definition: dqm_diff.py:58
Local3DVector LocalVector
Definition: LocalVector.h:12
ROOT::Math::Plane3D::Vector Vector
Definition: EcalHitMaker.cc:29
Definition: DDAxes.h:10
Global3DPoint GlobalPoint
Definition: GlobalPoint.h:10
T y() const
Definition: PV3DBase.h:63
PropagationDirection
TrackCharge charge() const
double charge(const std::vector< uint8_t > &Ampls)
Definition: Plane.h:17
#define unlikely(x)
Definition: Likely.h:21
int TrackCharge
Definition: TrackCharge.h:4
tuple path
else: Piece not in the list, fine.
static RKSmallVector< double, 6 > rkstate(const Vector3D &pos, const Vector3D &mom)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const Vector3D & momentum() const
GlobalVector momentum() const
GlobalPoint position() const
static Plane transformPlane(const Plane &plane, const GloballyPositioned< T > &frame)
Definition: FrameChanger.h:15
string const
Definition: compareJSON.py:14
TEveGeoShape * clone(const TEveElement *element, TEveElement *parent)
Definition: eve_macros.cc:135
double transverseCurvature() const
return(e1-e2)*(e1-e2)+dp *dp
std::pair< TrajectoryStateOnSurface, double > analyticalErrorPropagation(const FreeTrajectoryState &startingState, const Surface &surface, SurfaceSideDefinition::SurfaceSide side, const GlobalTrajectoryParameters &destParameters, const double &s)
T perp() const
Magnitude of transverse component.
#define likely(x)
Definition: Likely.h:20
Local3DPoint LocalPoint
Definition: LocalPoint.h:11
dbl *** dir
Definition: mlp_gen.cc:35
Definition: DDAxes.h:10
const Vector3D & position() const
T x() const
Definition: PV3DBase.h:62
const BasicVectorType & basicVector() const
Definition: PV3DBase.h:56
Global3DVector GlobalVector
Definition: GlobalVector.h:10
Basic3DVector cross(const Basic3DVector &v) const
Vector product, or &quot;cross&quot; product, with a vector of same type.