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 #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  edm::LogVerbatim("CommonTopologies")
19  << name << ": atan0 calls tot/large/over1: " << natan << "/" << nlarge << "/" << over1;
20  }
21 
22  void add(float t) {
23  auto at = std::abs(t);
24  ++natan;
25  if (at > 0.40f)
26  ++nlarge;
27  if (at > 1.0)
28  ++over1;
29  }
30  const char* name;
31  long long natan = 0;
32  long long nlarge = 0;
33  long long over1 = 0;
34  };
35 
36  Stat statM("mpos");
37  Stat statS("span");
38 #endif
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 float atan0(float t) {
49  auto z = t;
50  // if( t > 0.4142135623730950f ) // * tan pi/8
51  // z = (t-1.0f)/(t+1.0f);
52  float z2 = z * z;
53  float ret =
54  (((8.05374449538e-2f * z2 - 1.38776856032E-1f) * z2 + 1.99777106478E-1f) * z2 - 3.33329491539E-1f) * z2 * z + z;
55  // if( t > 0.4142135623730950f ) ret +=0.7853981633974483096f;
56  return ret;
57  }
58 
59  inline float atanClip(float t) {
60  constexpr float tanPi8 = 0.4142135623730950;
61  constexpr float pio8 = 3.141592653589793238 / 8;
62  float at = std::abs(t);
63  return std::copysign((at < tanPi8) ? atan0(at) : pio8, t);
64  }
65 
66 } // namespace
67 
68 TkRadialStripTopology::TkRadialStripTopology(int ns, float aw, float dh, float r, int yAx, float yMid)
69  : theNumberOfStrips(ns),
70  theAngularWidth(aw),
71  theAWidthInverse(1.f / aw),
72  theTanAW(std::tan(aw)),
73  theDetHeight(dh),
74  theCentreToIntersection(r),
75  theYAxisOrientation(yAx),
76  yCentre(yMid),
77  theRadialSigma(std::pow(dh, 2.f) * (1.f / 12.f)) {
78  // Angular offset of extreme edge of detector, so that angle is
79  // zero for a strip lying along local y axis = long symmetry axis of plane of strips
80  thePhiOfOneEdge = -(0.5 * theNumberOfStrips) * theAngularWidth; // always negative!
82  assert(std::abs(thePhiOfOneEdge) < 0.15); //
83 
84  LogTrace("TkRadialStripTopology") << "TkRadialStripTopology: constructed with"
85  << " strips = " << ns << " width = " << aw << " rad "
86  << " det_height = " << dh << " ctoi = " << r << " phi_edge = " << thePhiOfOneEdge
87  << " rad "
88  << " y_ax_ori = " << theYAxisOrientation << " y_det_centre = " << yCentre << "\n";
89 }
90 
92  return std::min(int(strip(lp)), theNumberOfStrips - 1);
93 }
94 
96  return std::min(nstrips(), static_cast<int>(std::max(float(0), strip(lp))) + 1);
97 }
98 
100  return yAxisOrientation() * y + originToIntersection();
101 }
102 
104  return detHeight() * std::sqrt(1.f + std::pow(lp.x() / yDistanceToIntersection(lp.y()), 2.f));
105 }
106 
107 float TkRadialStripTopology::xOfStrip(int strip, float y) const {
108  return 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 #ifdef MATH_STS
133  statS.add(t);
134 #endif
135  // edm::LogVerbatim("CommonTopologies") << "atans " << atanClip(t) << " " << std::atan2(lp1.x(), yDistanceToIntersection(lp1.y())) - std::atan2(lp2.x(),yDistanceToIntersection(lp2.y()));
136  // clip???
137  return atanClip(t) * theAWidthInverse;
138  // return (measurementPosition(lp1)-measurementPosition(lp2)).x();
139 }
140 
143 }
144 
146  const float // y = (L/cos(phi))*mp.y()*cos(phi)
147  y(mp.y() * detHeight() + yCentreOfStripPlane()),
149  return LocalPoint(x, y);
150 }
151 
153  // phi is [pi/2 - conventional local phi], use atan2(x,y) rather than atan2(y,x)
154  // clip ( at pi/8 or detedge+tollerance?)
155  float t = lp.x() / yDistanceToIntersection(lp.y());
156 #ifdef MATH_STS
157  statM.add(t);
158 #endif
159  const float phi = atanClip(t);
161 }
162 
163 LocalError TkRadialStripTopology::localError(float strip, float stripErr2) const {
164  double phi = stripAngle(strip);
165 
166  const double t1(tan15(phi)), // std::tan(phif)), // (vdt::fast_tanf(phif)),
167  t2(t1 * t1),
168  // s1(std::sin(phi)), c1(std::cos(phi)),
169  // cs(s1*c1), s2(s1*s1), c2(1-s2), // rotation matrix
170 
171  tt(stripErr2 * std::pow(centreToIntersection() * angularWidth(), 2.f)), // tangential sigma^2 *c2
172  rr(theRadialSigma), // radial sigma^2( uniform prob density along strip) *c2
173 
174  xx(tt + t2 * rr), yy(t2 * tt + rr), xy(t1 * (rr - tt));
175 
176  return LocalError(xx, xy, yy);
177 }
178 
180  const double phi(stripAngle(mp.x())), s1(std::sin(phi)), c1(std::cos(phi)), cs(s1 * c1), s2(s1 * s1),
181  c2(1 - s2), // rotation matrix
182 
184  c1), // tangential measurement unit (local pitch)
185  R(detHeight() / c1), // radial measurement unit (strip length)
186  tt(me.uu() * T * T), // tangential sigma^2
187  rr(me.vv() * R * R), // radial sigma^2
188  tr(me.uv() * T * R),
189 
190  xx(c2 * tt + 2 * cs * tr + s2 * rr), yy(s2 * tt - 2 * cs * tr + c2 * rr), xy(cs * (rr - tt) + tr * (c2 - s2));
191 
192  return LocalError(xx, xy, yy);
193 }
194 
196  const double yHitToInter(yDistanceToIntersection(p.y())),
197  t(yAxisOrientation() * p.x() / yHitToInter), // tan(strip angle)
198  cs(t / (1 + t * t)), s2(t * cs), c2(1 - s2), // rotation matrix
199 
200  T2(1. / (std::pow(angularWidth(), 2.f) *
201  (std::pow(p.x(), 2.f) + std::pow(yHitToInter, 2)))), // 1./tangential measurement unit (local pitch) ^2
202  R2(c2 / std::pow(detHeight(), 2.f)), // 1./ radial measurement unit (strip length) ^2
203 
204  uu((c2 * e.xx() - 2 * cs * e.xy() + s2 * e.yy()) * T2), vv((s2 * e.xx() + 2 * cs * e.xy() + c2 * e.yy()) * R2),
205  uv((cs * (e.xx() - e.yy()) + e.xy() * (c2 - s2)) * std::sqrt(T2 * R2));
206 
207  return MeasurementError(uu, uv, vv);
208 }
209 
210 // The local pitch is the local x width of the strip at the local (x,y)
212  // 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
213  float y = yDistanceToIntersection(lp.y());
214  float x = std::abs(lp.x());
215  return y * (y * theTanAW + x) / (y - theTanAW * x) - x;
216 }
217 
218 /* old version
219 float TkRadialStripTopology::localPitch(const LocalPoint& lp) const {
220  // this should be ~ y*(tan(phi+aw)-tan(phi)) = -tan(phi) + (tan(aw)+tan(phi))/(1.f-tan(aw)*tan(phi))
221  const int istrip = std::min(nstrips(), static_cast<int>(strip(lp)) + 1); // which strip number
222  float fangle = stripAngle(static_cast<float>(istrip) - 0.5); // angle of strip centre
223  float p =
224  yDistanceToIntersection( lp.y() ) * std::sin(angularWidth()) /
225  std::pow( std::cos(fangle-0.5f*angularWidth()), 2.f);
226 
227  float theTanAW = std::tan(theAngularWidth);
228  float y = yDistanceToIntersection( lp.y() );
229  float x = std::abs(lp.x());
230  float myP = y*(y*theTanAW+x)/(y-theTanAW*x)-x; // (y*theTanAW+x)/(1.f-theTanAW*x/y)-x;
231  edm::LogVerbatim("CommonTopologies") << "localPitch " << p << " " << myP;
232 
233  return p;
234 
235 }
236 */
Log< level::Info, true > LogVerbatim
int channel(const LocalPoint &) const override
Point3DBase< Scalar, LocalTag > LocalPoint
Definition: Definitions.h:30
float stripAngle(float strip) const override
ret
prodAgent to be discontinued
float xOfStrip(int strip, 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
T x() const
Definition: PV2DBase.h:43
int nstrips() const override
float originToIntersection() const override
assert(be >=bs)
#define LogTrace(id)
T y() const
Definition: PV2DBase.h:44
T x() const
Definition: PV3DBase.h:59
T y() const
Definition: PV3DBase.h:60
Measurement2DPoint MeasurementPoint
Measurement points are two-dimensional by default.
Definition: TTTypes.h:54
float strip(const LocalPoint &) const override
int nearestStrip(const LocalPoint &) const override
T sqrt(T t)
Definition: SSEVec.h:23
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
LocalError localError(float strip, float stripErr2) const override
Tan< T >::type tan(const T &t)
Definition: Tan.h:22
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float localStripLength(const LocalPoint &) const override
float localPitch(const LocalPoint &) const override
double f[11][100]
float angularWidth() const override
float centreToIntersection() const override
float coveredStrips(const LocalPoint &lp1, const LocalPoint &lp2) const override
float yCentreOfStripPlane() const override
MeasurementPoint measurementPosition(const LocalPoint &) const override
MeasurementError measurementError(const LocalPoint &, const LocalError &) const override
void add(std::map< std::string, TH1 *> &h, TH1 *hist)
float detHeight() const override
float yDistanceToIntersection(float y) const override
float x
float yAxisOrientation() const override
dh
Definition: cuy.py:354
long double T
float phiOfOneEdge() const override
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
LocalPoint localPosition(float strip) const override