CMS 3D CMS Logo

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