BOSS 8.0.0
BESIII Offline Software System
Loading...
Searching...
No Matches
HoughStereo.cxx
Go to the documentation of this file.
1#include "HoughStereo.h"
2#include "HoughRecHit.h"
3
5// HoughStereo::HoughStereo(){
6// }
7// HoughStereo::HoughStereo(double bunchTime):_bunchTime(bunchTime){
8// }
9HoughStereo::HoughStereo( double bunchTime, Hough2D* circle, HoughRecHit* rechit )
10 : _bunchTime( bunchTime ), _circle( circle ), _rechit( rechit ) {
11 _lleft = -99;
12 _zleft = -99;
13 _lright = -99;
14 _zright = -99;
15 _ambig = 0;
16 _charge = circle->getCharge();
17}
18void HoughStereo::setAmb( int i ) {
19 // initial
20 _ambig = i;
21}
22
24 bool ok_ambig[2];
25 bool ok[2][2];
26 ok_ambig[0] = true;
27 ok_ambig[1] = true;
28 ok[0][0] = false;
29 ok[0][1] = false;
30 ok[1][0] = false;
31 ok[1][1] = false;
32 double xeast = _rechit->getEastPoint().x();
33 double xwest = _rechit->getWestPoint().x();
34 double yeast = _rechit->getEastPoint().y();
35 double ywest = _rechit->getWestPoint().y();
36 double zeast = _rechit->getEastPoint().z();
37 double zwest = _rechit->getWestPoint().z();
38 // cout<<"xeast xwest "<<xeast<<" "<<xwest<<endl;
39 double k = ( ywest - yeast ) / ( xwest - xeast );
40 double b = -k * xeast + yeast;
41 // cout<<" k b "<<k<<" "<<b<<endl;
42 double xc = _circle->getCirX();
43 double yc = _circle->getCirY();
44 double rc = _circle->getCirR();
45 // cout<<"xc yc rc "<<xc<<" "<<yc<<" "<<rc<<endl;
46 double drift = _rechit->getDriftDist();
47 // double drift = _rechit->getDriftDistTruth();
48 // cout<<"drift "<<_rechit->getDriftDist()<<endl;
49 // cout<<"driftTruth "<<_rechit->getDriftDistTruth()<<endl;
50 double x1( 999 ), y1( 999 );
51 double x2( 999 ), y2( 999 );
52 double a = k * k + 1;
53 double b1 = -2 * ( xc + k * yc - k * b );
54 double c1 = xc * xc + ( yc - b ) * ( yc - b ) - ( rc + drift ) * ( rc + drift );
55 double c2 = xc * xc + ( yc - b ) * ( yc - b ) - ( rc - drift ) * ( rc - drift );
56 double delta1 = ( b1 * b1 - 4 * a * c1 );
57 double delta2 = ( b1 * b1 - 4 * a * c2 );
58 if ( delta1 < 0 ) ok_ambig[0] = false;
59 if ( delta2 < 0 ) ok_ambig[1] = false;
60 // cout<<"(b1*b1-4*a*c1) "<<(b1*b1-4*a*c1)<<endl;
61 // cout<<"(b1*b1-4*a*c2) "<<(b1*b1-4*a*c2)<<endl;
62 if ( delta1 >= 0 )
63 {
64 double x1_0 = ( -b1 + sqrt( delta1 ) ) / ( 2 * a );
65 double x1_1 = ( -b1 - sqrt( delta1 ) ) / ( 2 * a );
66 // cout<<"x1 0 1 "<<x1_0<<" "<<x1_1<<endl;
67 if ( ( xeast >= x1_0 && xwest <= x1_0 ) or ( xeast <= x1_0 && xwest >= x1_0 ) )
68 ok[0][0] = true;
69 if ( ( xeast >= x1_1 && xwest <= x1_1 ) or ( xeast <= x1_1 && xwest >= x1_1 ) )
70 ok[0][1] = true;
71 if ( ok[0][0] == true && ok[0][1] == false ) x1 = x1_0;
72 if ( ok[0][0] == false && ok[0][1] == true ) x1 = x1_1;
73 if ( ok[0][0] == true && ok[0][1] == true )
74 {
75 x1 = x1_0; //??good
76 // cout<<" error both ok "<<endl;
77 }
78 if ( ok[0][0] == false && ok[0][1] == false ) ok_ambig[0] = false;
79 y1 = k * x1 + b;
80 double theta1 = 0;
81 double l1 = 0;
82 double z1 = 0;
83 if ( ok_ambig[0] == true )
84 {
85 calcu( x1, y1, xc, yc, rc, xeast, yeast, zeast, xwest, ywest, zwest, theta1, l1, z1 );
86 if ( m_debug > 0 ) cout << " theta1 l1 z1 " << theta1 << " " << l1 << " " << z1 << endl;
87 _zleft = z1;
88 _lleft = l1;
89 }
90 }
91
92 if ( delta2 >= 0 )
93 {
94 double x2_0 = ( -b1 + sqrt( delta2 ) ) / ( 2 * a );
95 double x2_1 = ( -b1 - sqrt( delta2 ) ) / ( 2 * a );
96 // cout<<"x2 0 1 "<<x2_0<<" "<<x2_1<<endl;
97 if ( ( xeast >= x2_0 && xwest <= x2_0 ) or ( xeast <= x2_0 && xwest >= x2_0 ) )
98 ok[1][0] = true;
99 if ( ( xeast >= x2_1 && xwest <= x2_1 ) or ( xeast <= x2_1 && xwest >= x2_1 ) )
100 ok[1][1] = true;
101 if ( ok[1][0] == true && ok[1][1] == false ) x2 = x2_0;
102 if ( ok[1][0] == false && ok[1][1] == true ) x2 = x2_1;
103 if ( ok[1][0] == true && ok[1][1] == true )
104 {
105 x2 = x2_0;
106 // cout<<" error both ok "<<endl;
107 }
108 if ( ok[1][0] == false && ok[1][1] == false ) ok_ambig[1] = false;
109 y2 = k * x2 + b;
110 double theta2 = 0;
111 double l2 = 0;
112 double z2 = 0;
113 if ( ok_ambig[1] == true )
114 {
115 calcu( x2, y2, xc, yc, rc, xeast, yeast, zeast, xwest, ywest, zwest, theta2, l2, z2 );
116 if ( m_debug > 0 ) cout << " theta2 l2 z2 " << theta2 << " " << l2 << " " << z2 << endl;
117 _zright = z2;
118 _lright = l2;
119 }
120 }
121 if ( ok_ambig[0] == true && ok_ambig[1] == false ) return -1;
122 if ( ok_ambig[0] == false && ok_ambig[1] == true ) return 1;
123 if ( ok_ambig[0] == true && ok_ambig[1] == true ) return 2;
124 if ( ok_ambig[0] == false && ok_ambig[1] == false ) return 0;
125 // cout<<" ztruth : "<<_rechit->getZTruth()<<endl;
126 std::cerr << __FILE__ << ":" << __LINE__ << ": should not reach here" << std::endl;
127 exit( 1 );
128}
129
130void HoughStereo::calcu( double x1, double y1, double xc, double yc, double rc, double x_east,
131 double y_east, double z_east, double x_west, double y_west,
132 double z_west, double& theta, double& l, double& z ) {
133 // rphi -> sz
134 if ( xc == 0 || x1 - xc == 0 ) { theta = 0; }
135 else
136 {
137 theta = M_PI - atan2( y1 - yc, x1 - xc ) + atan2( yc, xc );
138 if ( theta > 2 * M_PI ) { theta = theta - 2 * M_PI; }
139 if ( theta < 0 ) { theta = theta + 2 * M_PI; }
140 }
141 if ( _charge == 1 ) theta = 2 * M_PI - theta;
142
143 double d1 = sqrt( ( x1 - x_west ) * ( x1 - x_west ) + ( y1 - y_west ) * ( y1 - y_west ) );
144 double d2 = sqrt( ( x_east - x_west ) * ( x_east - x_west ) +
145 ( y_east - y_west ) * ( y_east - y_west ) );
146 // cout<<"d1/d2 "<<d1/d2<<" "<<d1<<" "<<d2<<endl;
147 // cout<<"zw ze "<<z_west<<" "<<z_east<<endl;
148 z = z_west - ( z_west - z_east ) * d1 / d2;
149 l = rc * theta;
150}
151/*
152
153
154int HoughStereo::cald(){
155 HepPoint3D fp =_rechit->getWestPoint();
156 HepPoint3D rp =_rechit->getEastPoint();
157 HepPoint3D X=_rechit->getMidPoint();
158 if(m_debug>0) std::cout<<"fp rp "<<fp<<" "<<rp<<std::endl;//yzhang debug
159 if(m_debug>0) std::cout<<"Xmid "<<X<<std::endl;//yzhang debug
160 HepVector3D xx = HepVector3D(X.x(), X.y(), 0.);
161 HepPoint3D center( _circle->getCirX(),_circle->getCirY(),0 );
162 HepVector3D yy = center - xx;
163 HepVector3D ww = HepVector3D(yy.x(), yy.y(), 0.);
164 double wwmag2 = ww.mag2();
165 double wwmag = sqrt(wwmag2);
166 double driftdist = fabs( _rechit->calDriftDist(_bunchTime,_ambig) );
167 if(m_debug>0) cout<<"Bunch time: "<<_bunchTime<<endl;
168 HepVector3D lr(driftdist/wwmag * ww.x(), driftdist/wwmag * ww.y(), 0.);
169
170 if(m_debug>0){
171 std::cout<<"xc "<< _circle->getCirX()<< " yc "<<_circle->getCirY()<< " drfitdist
172"<<driftdist<<std::endl; std::cout<<"lr "<<lr<<" "<<" ambig "<<_ambig
173 <<"left "<<_rechit->calDriftDist(0,1)
174 <<"right "<<_rechit->calDriftDist(0,-1)<<std::endl;
175 }
176
177 HepPoint3D ORIGIN = HepPoint3D(0., 0., 0.);
178 if (_ambig == 0) lr = ORIGIN;
179 if (_charge> 0){
180 if (_ambig == -1){
181 lr = - lr;
182 }
183 }else{
184 if (_ambig == 1){
185 lr = - lr;
186 }
187 }
188 X += lr;
189
190 HepPoint3D tmp(-9999., -9999., 0.);
191 HepVector3D x = HepVector3D(X.x(), X.y(), 0.);
192 HepVector3D w = x - center;
193 // //modified the next sentence because the direction are different from belle.
194 HepVector3D V = _rechit->wire()->zAxis();
195 HepVector3D v = HepVector3D(V.x(), V.y(), 0.);
196 double vmag2 = v.mag2();
197 //double vmag = sqrt(vmag2);
198 //double r = _helix->curv();
199 double wv = w.dot(v);
200 // wv = abs(wv);
201 if(m_debug>0){
202 std::cout<<"X_fix "<<X<<" center "<<center<<std::endl;
203 std::cout<<"V "<<V<<std::endl;//yzhang debug
204 std::cout<<"w "<<w<<" v "<<v<<std::endl;
205 std::cout<<"wv "<<wv<<endl;
206 }
207 double d2 = wv * wv - vmag2 * (w.mag2() - _circle->getCirR()* _circle->getCirR() );
208 if ( d2<0. ) return -1;
209 double d=sqrt(d2);
210
211 double l[2];
212 l[0] =-1*( (- wv + d) / vmag2 ); //multiply -1 ......?
213 l[1] =-1*( (- wv - d) / vmag2 );
214
215 double z[2];
216 //z[0] = X.z() + l[0] * V.z();
217 z[0] = X.z() - l[0] * V.z(); //l *-1
218 z[1] = X.z() - l[1] * V.z();
219 if(m_debug>0) cout<<"z0 z1: "<<z[0]<<" "<<z[1]<<endl;
220
221
222 bool ok[2];
223 ok[0] = true;
224 ok[1] = true;
225 //...Check z position...
226 if (_ambig == 0) {
227 if (z[0] > rp.z()+20. || z[0] < fp.z()-20.) {
228 ok[0] = false;
229 }
230 if (z[1] > rp.z()+20. || z[1] < fp.z()-20.) {
231 ok[1] = false;
232 }
233 } else {
234 if (fabs(z[0]/rp.z())>1.05 ) { ok[0] = false; }
235 if (fabs(z[1]/rp.z())>1.05 ) { ok[1] = false; }
236 }
237 if ((! ok[0]) && (! ok[1])) {
238 return -1;
239 }
240 if (( ok[0]) && ( ok[1])) {
241 return 2;
242 }
243
244 unsigned best = 0;
245 if (ok[1]) best = 1; //need improve
246 HepVector3D p[2];
247 p[0] = x + l[0] * v;
248 p[1] = x + l[1] * v;
249 double cosdPhi = - center.dot((p[best] - center).unit()) / center.mag();
250 double dPhi;
251 if(fabs(cosdPhi)<=1.0) {
252 dPhi = acos(cosdPhi);
253 } else if (cosdPhi>1.0) {
254 dPhi = 0.0;
255 } else {
256 dPhi = pi;
257 }
258
259 double stemp=_circle->getCirR()*dPhi;
260 if(_ambig==-1){
261 _zright=z[best];
262 _lright=stemp;
263 }
264 if(_ambig==1){
265 _zleft=z[best];
266 _lleft=stemp;
267 }
268 if(m_debug>0){
269 cout<<"in hough stereo "<<endl;
270 cout<<"left: "<<_zleft<<" "<<_lleft<<endl;
271 cout<<"right: "<<_zright<<" "<<_lright<<endl;
272 }
273 return 0;
274}
275*/
276
278 _rechit->setzAmb( 0, _zleft );
279 _rechit->setsAmb( 0, _lleft );
280 _rechit->setzAmb( 1, _zright );
281 _rechit->setsAmb( 1, _lright );
282}
284 cout << "Hit"
285 << "(" << _rechit->getLayerId() << "," << _rechit->getWireId() << ") "
286 << _rechit->getStyle() << endl;
287 cout << " left: " << _lleft << "," << _zleft << endl;
288 cout << " right: " << _lright << "," << _zright << endl;
289}
#define M_PI
Definition TConstant.h:4
int getCharge() const
Definition Hough2D.h:39
HoughStereo(double bunchTime, Hough2D *circle, HoughRecHit *rechit)
static int m_debug
Definition HoughStereo.h:27
void setAmb(int i)
void calcu(double x1, double y1, double xc, double yc, double rc, double x_east, double y_east, double z_east, double x_west, double y_west, double z_west, double &theta, double &l, double &z)