CMS 3D CMS Logo

TkRadialStripTopology.cc
Go to the documentation of this file.
3 
4 #include <iostream>
5 #include <cmath>
6 #include <algorithm>
7 #include <cassert>
8 
9 #include <vdt/vdtMath.h>
10 
11 #ifdef MATH_STS
12 #include<iostream>
13 #endif
14 namespace {
15 
16 #ifdef MATH_STS
17  struct Stat {
18  Stat(const char * in) : name(in){};
19  ~Stat() {
20  std::cout << name << ": atan0 calls tot/large/over1: " << natan << "/" << nlarge <<"/" << over1 << std::endl;
21  }
22 
23  void add(float t) {
24  auto at = std::abs(t);
25  ++natan;
26  if (at>0.40f) ++nlarge;
27  if (at>1.0) ++over1;
28  }
29  const char * name;
30  long long natan=0;
31  long long nlarge=0;
32  long long over1=0;
33  };
34 
35  Stat statM("mpos");
36  Stat statS("span");
37 #endif
38 
39 
40  // valid for |x| < 0.15 (better then 10^-9
41  template<typename T>
42  inline T tan15(T x) {
43  return x * (T(1) + (x*x) * (T(0.33331906795501708984375) + (x*x) * T(0.135160386562347412109375)));
44  }
45 
46  // valid for z < pi/8
47  // x * (1 + x*x * (-0.33322894573211669921875 + x*x * (0.1967026889324188232421875 + x*x * (-0.11053790152072906494140625)))) // .1e-7 by Sollya
48  inline
49  float atan0(float t) {
50  auto z=t;
51  // if( t > 0.4142135623730950f ) // * tan pi/8
52  // z = (t-1.0f)/(t+1.0f);
53  float z2 = z * z;
54  float ret=
55  ((( 8.05374449538e-2f * z2
56  - 1.38776856032E-1f) * z2
57  + 1.99777106478E-1f) * z2
58  - 3.33329491539E-1f) * z2 * z
59  + z;
60  // if( t > 0.4142135623730950f ) ret +=0.7853981633974483096f;
61  return ret;
62  }
63 
64  inline
65  float atanClip(float t) {
66  constexpr float tanPi8 = 0.4142135623730950;
67  constexpr float pio8 = 3.141592653589793238/8;
68  float at = std::abs(t);
69  return std::copysign( (at< tanPi8 ) ? atan0(at) : pio8, t );
70  }
71 
72 }
73 
74 TkRadialStripTopology::TkRadialStripTopology(int ns, float aw, float dh, float r, int yAx, float yMid) :
75  theNumberOfStrips(ns), theAngularWidth(aw), theAWidthInverse(1.f/aw),theTanAW(std::tan(aw)),
76  theDetHeight(dh), theCentreToIntersection(r),
77  theYAxisOrientation(yAx), yCentre( yMid),
78  theRadialSigma(std::pow(dh, 2.f) * (1.f/12.f)) {
79  // Angular offset of extreme edge of detector, so that angle is
80  // zero for a strip lying along local y axis = long symmetry axis of plane of strips
81  thePhiOfOneEdge = -(0.5*theNumberOfStrips) * theAngularWidth; // always negative!
83  assert(std::abs(thePhiOfOneEdge)<0.15); //
84 
85  LogTrace("TkRadialStripTopology") << "TkRadialStripTopology: constructed with"
86  << " strips = " << ns
87  << " width = " << aw << " rad "
88  << " det_height = " << dh
89  << " ctoi = " << r
90  << " phi_edge = " << thePhiOfOneEdge << " rad "
91  << " y_ax_ori = " << theYAxisOrientation
92  << " y_det_centre = " << yCentre
93  << "\n";
94 }
95 
96 int TkRadialStripTopology::channel(const LocalPoint& lp) const { return std::min( int( strip(lp) ), theNumberOfStrips-1 ) ;}
97 
98 int TkRadialStripTopology::nearestStrip(const LocalPoint & lp) const { return std::min( nstrips(), static_cast<int>( std::max(float(0), strip(lp)) ) + 1);}
99 
101 
103  return detHeight() * std::sqrt(1.f + std::pow( lp.x()/yDistanceToIntersection(lp.y()), 2.f) );
104 }
105 
106 float TkRadialStripTopology::xOfStrip(int strip, float y) const {
107  return
108  yAxisOrientation() * yDistanceToIntersection( y ) * std::tan( stripAngle(static_cast<float>(strip) - 0.5f ) );
109 }
110 
111 float TkRadialStripTopology::strip(const LocalPoint& lp) const {
112  // phi is measured from y axis --> sign of angle is sign of x * yAxisOrientation --> use atan2(x,y), not atan2(y,x)
113  const float phi = atanClip(lp.x()/yDistanceToIntersection(lp.y()));
114  const float aStrip = ( phi - phiOfOneEdge() )*theAWidthInverse;
115  return std::max(float(0), std::min( (float)nstrips(), aStrip ));
116 }
117 
118 float TkRadialStripTopology::coveredStrips(const LocalPoint& lp1, const LocalPoint& lp2) const {
119  // http://en.wikipedia.org/wiki/List_of_trigonometric_identities#Angle_sum_and_difference_identities
120  // atan(a)-atan(b) = atan( (a-b)/(1+a*b) )
121  // avoid divisions
122  // float t1 = lp1.x()/yDistanceToIntersection( lp1.y() );
123  // float t2 = lp2.x()/yDistanceToIntersection( lp2.y() );
124  // float t = (t1-t2)/(1.+t1*t2);
125  auto y1 = yDistanceToIntersection( lp1.y() );
126  auto y2 = yDistanceToIntersection( lp2.y() );
127  auto x1 = lp1.x();
128  auto x2 = lp2.x();
129 
130  auto t = (y2*x1 -y1*x2)/(y1*y2 + x1*x2);
131 
132 
133 #ifdef MATH_STS
134  statS.add(t);
135 #endif
136 // std::cout << "atans " << atanClip(t)
137 // <<" "<< std::atan2(lp1.x(),yDistanceToIntersection(lp1.y()) )
138 // -std::atan2(lp2.x(),yDistanceToIntersection(lp2.y()) ) << std::endl;
139  // clip???
140  return atanClip(t)*theAWidthInverse;
141  // return (measurementPosition(lp1)-measurementPosition(lp2)).x();
142 }
143 
145  return LocalPoint( yAxisOrientation() * originToIntersection() * tan15( stripAngle(strip) ), 0 );
146 }
147 
149  const float // y = (L/cos(phi))*mp.y()*cos(phi)
150  y( mp.y()*detHeight() + yCentreOfStripPlane() ),
151  x( yAxisOrientation() * yDistanceToIntersection( y ) * tan15( stripAngle( mp.x() ) ) );
152  return LocalPoint( x, y );
153 }
154 
156  // phi is [pi/2 - conventional local phi], use atan2(x,y) rather than atan2(y,x)
157  // clip ( at pi/8 or detedge+tollerance?)
158  float t = lp.x()/yDistanceToIntersection(lp.y());
159 #ifdef MATH_STS
160  statM.add(t);
161 #endif
162  const float phi = atanClip(t);
164  ( lp.y() - yCentreOfStripPlane() ) / detHeight() );
165 }
166 
167 
168 LocalError TkRadialStripTopology::localError(float strip, float stripErr2) const {
169  double phi = stripAngle(strip);
170 
171  const double
172  t1(tan15(phi)), // std::tan(phif)), // (vdt::fast_tanf(phif)),
173  t2(t1*t1),
174  // s1(std::sin(phi)), c1(std::cos(phi)),
175  // cs(s1*c1), s2(s1*s1), c2(1-s2), // rotation matrix
176 
177  tt( stripErr2 * std::pow( centreToIntersection()*angularWidth() ,2.f) ), // tangential sigma^2 *c2
178  rr( theRadialSigma), // radial sigma^2( uniform prob density along strip) *c2
179 
180  xx( tt + t2*rr ),
181  yy( t2*tt + rr ),
182  xy( t1*( rr - tt ) );
183 
184  return LocalError( xx, xy, yy );
185 }
186 
188  const double
189  phi(stripAngle(mp.x())), s1(std::sin(phi)), c1(std::cos(phi)),
190  cs(s1*c1), s2(s1*s1), c2(1-s2), // rotation matrix
191 
192  T( angularWidth() * ( centreToIntersection() + yAxisOrientation()*mp.y()*detHeight()) / c1 ), // tangential measurement unit (local pitch)
193  R( detHeight()/ c1 ), // radial measurement unit (strip length)
194  tt( me.uu() * T*T ), // tangential sigma^2
195  rr( me.vv() * R*R ), // radial sigma^2
196  tr( me.uv() * T*R ),
197 
198  xx( c2*tt + 2*cs*tr + s2*rr ),
199  yy( s2*tt - 2*cs*tr + c2*rr ),
200  xy( cs*( rr - tt ) + tr*( c2 - s2 ) );
201 
202  return LocalError( xx, xy, yy );
203 }
204 
206  const double
207  yHitToInter(yDistanceToIntersection(p.y())),
208  t(yAxisOrientation() * p.x() / yHitToInter), // tan(strip angle)
209  cs(t/(1+t*t)), s2(t*cs), c2(1-s2), // rotation matrix
210 
211  T2( 1./(std::pow(angularWidth(),2.f) * ( std::pow(p.x(),2.f) + std::pow(yHitToInter,2)) )), // 1./tangential measurement unit (local pitch) ^2
212  R2( c2/std::pow(detHeight(),2.f) ), // 1./ radial measurement unit (strip length) ^2
213 
214  uu( ( c2*e.xx() - 2*cs*e.xy() + s2*e.yy() ) * T2 ),
215  vv( ( s2*e.xx() + 2*cs*e.xy() + c2*e.yy() ) * R2 ),
216  uv( ( cs*( e.xx() - e.yy() ) + e.xy()*( c2 - s2 ) ) * std::sqrt (T2*R2) );
217 
218  return MeasurementError(uu, uv, vv);
219 }
220 
221 
222  // The local pitch is the local x width of the strip at the local (x,y)
224  // this should be ~ y*(tan(phi+aw)-tan(phi)) = -x + y*(tan(aw)+tan(phi))/(1.f-tan(aw)*tan(phi)) tan(phi)=x/y
225  float y = yDistanceToIntersection( lp.y() );
226  float x = std::abs(lp.x());
227  return y*(y*theTanAW+x)/(y-theTanAW*x)-x;
228 }
229 
230 /* old version
231 float TkRadialStripTopology::localPitch(const LocalPoint& lp) const {
232  // this should be ~ y*(tan(phi+aw)-tan(phi)) = -tan(phi) + (tan(aw)+tan(phi))/(1.f-tan(aw)*tan(phi))
233  const int istrip = std::min(nstrips(), static_cast<int>(strip(lp)) + 1); // which strip number
234  float fangle = stripAngle(static_cast<float>(istrip) - 0.5); // angle of strip centre
235  float p =
236  yDistanceToIntersection( lp.y() ) * std::sin(angularWidth()) /
237  std::pow( std::cos(fangle-0.5f*angularWidth()), 2.f);
238 
239  float theTanAW = std::tan(theAngularWidth);
240  float y = yDistanceToIntersection( lp.y() );
241  float x = std::abs(lp.x());
242  float myP = y*(y*theTanAW+x)/(y-theTanAW*x)-x; // (y*theTanAW+x)/(1.f-theTanAW*x/y)-x;
243  std::cout << "localPitch " << p << " " << myP << std::endl;
244 
245  return p;
246 
247 }
248 */
float xx() const
Definition: LocalError.h:24
float vv() const
auto_ptr< ClusterSequence > cs
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:32
T y() const
Definition: PV2DBase.h:46
int nstrips() const override
float yAxisOrientation() const override
float strip(const LocalPoint &) const override
float yDistanceToIntersection(float y) const override
TkRadialStripTopology(int ns, float aw, float dh, float r, int yAx=1, float yMid=0.)
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
float localStripLength(const LocalPoint &) const override
T y() const
Definition: PV3DBase.h:63
float localPitch(const LocalPoint &) const override
float stripAngle(float strip) const override
float angularWidth() const override
float coveredStrips(const LocalPoint &lp1, const LocalPoint &lp2) const override
#define constexpr
LocalPoint localPosition(float strip) const override
float xy() const
Definition: LocalError.h:25
Measurement2DPoint MeasurementPoint
Measurement points are two-dimensional by default.
int nearestStrip(const LocalPoint &) const override
float yy() const
Definition: LocalError.h:26
auto const T2 &decltype(t1.eta()) t2
Definition: deltaR.h:16
LocalError localError(float strip, float stripErr2) const override
T sqrt(T t)
Definition: SSEVec.h:18
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
float centreToIntersection() const override
float uu() const
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
float yCentreOfStripPlane() const override
T min(T a, T b)
Definition: MathUtil.h:58
#define LogTrace(id)
MeasurementPoint measurementPosition(const LocalPoint &) const override
float phiOfOneEdge() const override
float originToIntersection() const override
void add(std::map< std::string, TH1 * > &h, TH1 *hist)
float xOfStrip(int strip, float y) const override
MeasurementError measurementError(const LocalPoint &, const LocalError &) const override
float detHeight() const override
int channel(const LocalPoint &) const override
dh
Definition: cuy.py:353
T x() const
Definition: PV2DBase.h:45
long double T
T x() const
Definition: PV3DBase.h:62
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
float uv() const