GFDetPlane.cxx
Go to the documentation of this file.
1 /* Copyright 2008-2010, Technische Universitaet Muenchen,
2  Authors: Christian Hoeppner & Sebastian Neubert
3 
4  This file is part of GENFIT.
5 
6  GENFIT is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  GENFIT is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with GENFIT. If not, see <http://www.gnu.org/licenses/>.
18 */
22 
23 #include <iostream>
24 #include <cmath>
25 
26 #include "Rtypes.h"
27 #include "TPolyLine3D.h"
28 #include "TPolyMarker3D.h"
29 #include "TMath.h"
30 #include "TRandom3.h"
31 
32 //ClassImp(GFDetPlane)
33 
34 
35 
36 genf::GFDetPlane::GFDetPlane(const TVector3& o,
37  const TVector3& u,
38  const TVector3& v,
39  GFAbsFinitePlane* finite)
40 :fO(o),fU(u),fV(v),fFinitePlane(finite)
41 {
42  sane();
43 }
45  :fFinitePlane(finite)
46 {
47  static TRandom3 r(0);
48  fO.SetXYZ(0.,0.,0.);
49  fU.SetXYZ(r.Uniform(),r.Uniform(),0.);
50  fV.SetXYZ(r.Uniform(),r.Uniform(),0.);
51  sane();
52 }
53 
54 genf::GFDetPlane::GFDetPlane(const TVector3& o,
55  const TVector3& n,
56  GFAbsFinitePlane* finite)
57  :fO(o),fFinitePlane(finite){
58  setNormal(n);
59 }
60 
62  if(fFinitePlane!=NULL) delete fFinitePlane;
63 }
64 
66  if(rhs.fFinitePlane != NULL) fFinitePlane = rhs.fFinitePlane->clone();
67  else fFinitePlane = NULL;
68  fO = rhs.fO;
69  fU = rhs.fU;
70  fV = rhs.fV;
71 }
73  if (this == &rhs) return *this;
74  if(fFinitePlane!=NULL) {
75  delete fFinitePlane;
76  }
77  if(rhs.fFinitePlane != NULL){
79  }
80  else{
81  fFinitePlane = NULL;
82  }
83  fO = rhs.fO;
84  fU = rhs.fU;
85  fV = rhs.fV;
86  return *this;
87 }
88 
89 void
90 genf::GFDetPlane::set(const TVector3& o,
91  const TVector3& u,
92  const TVector3& v)
93 {
94  fO=o;fU=u;fV=v;
95  sane();
96 }
97 
98 void
99 genf::GFDetPlane::setO(const TVector3& o)
100 {
101  fO=o;
102  sane();
103 }
104 void
105 genf::GFDetPlane::setO(double X,double Y,double Z)
106 {
107  fO.SetXYZ(X,Y,Z);
108  sane();
109 }
110 
111 void
112 genf::GFDetPlane::setU(const TVector3& u)
113 {
114  fU=u;
115  sane();
116 }
117 void
118 genf::GFDetPlane::setU(double X,double Y,double Z)
119 {
120  fU.SetXYZ(X,Y,Z);
121  sane();
122 }
123 
124 void
125 genf::GFDetPlane::setV(const TVector3& v)
126 {
127  fV=v;
128  sane();
129 }
130 void
131 genf::GFDetPlane::setV(double X,double Y,double Z)
132 {
133  fV.SetXYZ(X,Y,Z);
134  sane();
135 }
136 void
137 genf::GFDetPlane::setUV(const TVector3& u,const TVector3& v)
138 {
139  fU=u;
140  fV=v;
141  sane();
142 }
143 
144 TVector3
146 {
147  TVector3 result=fU.Cross(fV);
148  result.SetMag(1.);
149  return result;
150 }
151 
152 void genf::GFDetPlane::setON(const TVector3& o,const TVector3& n){
153  fO = o;
154  setNormal(n);
155 }
156 
157 void
158 genf::GFDetPlane::setNormal(double X,double Y,double Z){
159  TVector3 N(X,Y,Z);
160  setNormal(N);
161 }
162 void
164  n.SetMag(1.);
165  if( fabs(n.X()) > 0.1 ){
166  fU.SetXYZ(1./n.X()*(-1.*n.Y()-1.*n.Z()),1.,1.);
167  fU.SetMag(1.);
168  }
169  else {
170  if(fabs(n.Y()) > 0.1){
171  fU.SetXYZ(1.,1./n.Y()*(-1.*n.X()-1.*n.Z()),1.);
172  fU.SetMag(1.);
173  }
174  else{
175  fU.SetXYZ(1.,1.,1./n.Z()*(-1.*n.X()-1.*n.Y()));
176  fU.SetMag(1.);
177  }
178  }
179  fV=n.Cross(fU);
180 }
181 
182 void genf::GFDetPlane::setNormal(const double& theta, const double& phi){
183  TVector3 n(TMath::Sin(theta)*TMath::Cos(phi),TMath::Sin(theta)*TMath::Sin(phi),TMath::Cos(theta));
184  setNormal(n);
185 }
186 
187 TVector2
188 genf::GFDetPlane::project(const TVector3& x)const
189 {
190  Double_t xfU=fU*x;
191  Double_t xfV=fV*x;
192  return TVector2(xfU,xfV);
193 }
194 
195 TVector2
196 genf::GFDetPlane::LabToPlane(const TVector3& x)const
197 {
198  TVector3 d=x-fO;
199  return project(d);
200 }
201 
202 TVector3
203 genf::GFDetPlane::toLab(const TVector2& x)const
204 {
205  TVector3 d(fO);
206  d+=x.X()*fU;
207  d+=x.Y()*fV;
208  return d;
209 }
210 
211 
212 
213 TVector3
214 genf::GFDetPlane::dist(const TVector3& x)const
215 {
216  TVector2 p=LabToPlane(x);
217  TVector3 xplane=toLab(p);
218  return xplane-x;
219 }
220 
221 
222 
223 void
225  if (fU==fV)
226  throw GFException("genf::GFDetPlane::sane() sanity check failed", __LINE__, __FILE__).setFatal();
227  // ensure unit vectors
228  fU.SetMag(1.);
229  fV.SetMag(1.);
230  // ensure orthogonal system
231  // fV should be reached by
232  // rotating fU around _n in a counterclockwise direction
233 
234  TVector3 n=getNormal();
235  fV=n.Cross(fU);
236 
237  TVector3 v=fU;
238  v.Rotate(TMath::Pi()*0.5,n);
239  TVector3 null=v-fV;
240  if (null.Mag() >= 1E-6)
241  throw GFException("genf::GFDetPlane::sane(): non orthogonal!", __LINE__, __FILE__).setFatal();
242 
243 }
244 
245 
246 void
247 genf::GFDetPlane::Print(std::ostream& out /* = std::cout */) const
248 {
249  out<<"GFDetPlane: "
250  <<"O("<<fO.X()<<","<<fO.Y()<<","<<fO.Z()<<") "
251  <<"u("<<fU.X()<<","<<fU.Y()<<","<<fU.Z()<<") "
252  <<"v("<<fV.X()<<","<<fV.Y()<<","<<fV.Z()<<") "
253  <<"n("<<getNormal().X()<<","<<getNormal().Y()<<","<<getNormal().Z()<<") "
254  <<std::endl;
255  out << fFinitePlane << std::endl;
256  if(fFinitePlane!=NULL) fFinitePlane->Print(out);
257 
258 }
259 
260 
261 /*
262  I could write pages of comments about correct equality checking for
263  floating point numbers, but: When two planes are as close as 10E-5 cm
264  in all nine numbers that define the plane, this will be enough for all
265  practical purposes
266  */
267 
268 // == and != are friends, not members, hence not "genf::GFDetPlane::operater==" below.
269 #define DETPLANE_EPSILON 1.E-5
270 bool genf::operator== (const GFDetPlane& lhs, const GFDetPlane& rhs){
271  if(
272  fabs( (lhs.fO.X()-rhs.fO.X()) ) > DETPLANE_EPSILON ||
273  fabs( (lhs.fO.Y()-rhs.fO.Y()) ) > DETPLANE_EPSILON ||
274  fabs( (lhs.fO.Z()-rhs.fO.Z()) ) > DETPLANE_EPSILON
275  ) return false;
276  else if(
277  fabs( (lhs.fU.X()-rhs.fU.X()) ) > DETPLANE_EPSILON ||
278  fabs( (lhs.fU.Y()-rhs.fU.Y()) ) > DETPLANE_EPSILON ||
279  fabs( (lhs.fU.Z()-rhs.fU.Z()) ) > DETPLANE_EPSILON
280  ) return false;
281  else if(
282  fabs( (lhs.fV.X()-rhs.fV.X()) ) > DETPLANE_EPSILON ||
283  fabs( (lhs.fV.Y()-rhs.fV.Y()) ) > DETPLANE_EPSILON ||
284  fabs( (lhs.fV.Z()-rhs.fV.Z()) ) > DETPLANE_EPSILON
285  ) return false;
286  return true;
287 }
288 
289 bool genf::operator!= (const GFDetPlane& lhs, const GFDetPlane& rhs){
290  return !(lhs == rhs);
291 }
292 
293 
294 void genf::GFDetPlane::getGraphics(double mesh, double length, TPolyMarker3D **pl,TPolyLine3D** plLine, TPolyLine3D **u, TPolyLine3D **v, TPolyLine3D**n){
295  *pl = new TPolyMarker3D(21*21,24);
296  (*pl)->SetMarkerSize(0.1);
297  (*pl)->SetMarkerColor(kBlue);
298  int nI=10;
299  int nJ=10;
300  *plLine = new TPolyLine3D(5);
301 
302  {
303  TVector3 linevec;
304  int i,j;
305  i=-1*nI;j=-1*nJ;
306  linevec=(fO+(mesh*i)*fU+(mesh*j)*fV);
307  (*plLine)->SetPoint(0,linevec.X(),linevec.Y(),linevec.Z());
308  i=-1*nI;j=1*nJ;
309  linevec=(fO+(mesh*i)*fU+(mesh*j)*fV);
310  (*plLine)->SetPoint(0,linevec.X(),linevec.Y(),linevec.Z());
311  i=1*nI;j=-1*nJ;
312  linevec=(fO+(mesh*i)*fU+(mesh*j)*fV);
313  (*plLine)->SetPoint(2,linevec.X(),linevec.Y(),linevec.Z());
314  i=1*nI;j=1*nJ;
315  linevec=(fO+(mesh*i)*fU+(mesh*j)*fV);
316  (*plLine)->SetPoint(1,linevec.X(),linevec.Y(),linevec.Z());
317  i=-1*nI;j=-1*nJ;
318  linevec=(fO+(mesh*i)*fU+(mesh*j)*fV);
319  (*plLine)->SetPoint(4,linevec.X(),linevec.Y(),linevec.Z());
320 
321  }
322  for (int i=-1*nI;i<=nI;++i){
323  for (int j=-1*nJ;j<=nJ;++j){
324  TVector3 vec(fO+(mesh*i)*fU+(mesh*j)*fV);
325  int id=(i+10)*21+j+10;
326  (*pl)->SetPoint(id,vec.X(),vec.Y(),vec.Z());
327  }
328  }
329 
330 
331  *u = new TPolyLine3D(2);
332  (*u)->SetPoint(0,fO.X(),fO.Y(),fO.Z());
333  (*u)->SetPoint(1,fO.X()+length*fU.X(),fO.Y()+length*fU.Y(),fO.Z()+length*fU.Z());
334  (*u)->SetLineWidth(2);
335  (*u)->SetLineColor(kGreen);
336 
337 
338  *v = new TPolyLine3D(2);
339  (*v)->SetPoint(0,fO.X(),fO.Y(),fO.Z());
340  (*v)->SetPoint(1,fO.X()+length*fV.X(),fO.Y()+length*fV.Y(),fO.Z()+length*fV.Z());
341  (*v)->SetLineWidth(2);
342  (*v)->SetLineColor(kRed);
343 
344  if(n!=NULL){
345  *n = new TPolyLine3D(2);
346  TVector3 _n=getNormal();
347  (*n)->SetPoint(0,fO.X(),fO.Y(),fO.Z());
348  (*n)->SetPoint(1,fO.X()+length*_n.X(),fO.Y()+length*_n.Y(),fO.Z()+length*_n.Z());
349  (*n)->SetLineWidth(2);
350  (*n)->SetLineColor(kBlue);
351  }
352 }
353 
354 double genf::GFDetPlane::distance(TVector3& v) const{
355  double s = (v - fO)*fU;
356  double t = (v - fO)*fV;
357  TVector3 distanceVector = v - fO - (s*fU) - (t*fV);
358  return distanceVector.Mag();
359 }
360 double genf::GFDetPlane::distance(double x,double y,double z) const{
361  TVector3 v(x,y,z);
362  double s = (v - fO)*fU;
363  double t = (v - fO)*fV;
364  TVector3 distanceVector = v - fO - (s*fU) - (t*fV);
365  return distanceVector.Mag();
366 }
367 
368 TVector2 genf::GFDetPlane::straightLineToPlane (const TVector3& point,const TVector3& dir) const{
369  TVector3 dirNorm(dir);
370  dirNorm.SetMag(1.);
371  TVector3 normal = getNormal();
372  double dirTimesN = dirNorm*normal;
373  if(fabs(dirTimesN)<1.E-6){//straight line is parallel to plane, so return infinity
374  //doesnt parallel mean that they cross in infinity ;-)
375  return TVector2(1.E100,1.E100);
376  }
377  double t = 1/dirTimesN * ((fO-point)*normal);
378  return project(point - fO + t * dirNorm);
379 }
void set(const TVector3 &o, const TVector3 &u, const TVector3 &v)
Definition: GFDetPlane.cxx:90
void setON(const TVector3 &o, const TVector3 &n)
Definition: GFDetPlane.cxx:152
void setU(const TVector3 &u)
Definition: GFDetPlane.cxx:112
GFDetPlane & operator=(const genf::GFDetPlane &)
Definition: GFDetPlane.cxx:72
bool operator!=(const genf::GFDetPlane &, const genf::GFDetPlane &)
Definition: GFDetPlane.cxx:289
static QCString result
void getGraphics(double mesh, double length, TPolyMarker3D **pl, TPolyLine3D **plLine, TPolyLine3D **u, TPolyLine3D **v, TPolyLine3D **n=NULL)
for poor attempts of making an event display. There is a lot of room for improvements.
Definition: GFDetPlane.cxx:294
TVector3 dist(const TVector3 &point) const
Definition: GFDetPlane.cxx:214
void setO(const TVector3 &o)
Definition: GFDetPlane.cxx:99
void setUV(const TVector3 &u, const TVector3 &v)
Definition: GFDetPlane.cxx:137
void Print(std::ostream &out=std::cout) const
Definition: GFDetPlane.cxx:247
string dir
#define DETPLANE_EPSILON
Definition: GFDetPlane.cxx:269
TVector2 project(const TVector3 &x) const
projecting a direction onto the plane:
Definition: GFDetPlane.cxx:188
virtual void Print(std::ostream &out=std::cout) const =0
TVector3 getNormal() const
Definition: GFDetPlane.cxx:145
void setV(const TVector3 &v)
Definition: GFDetPlane.cxx:125
double distance(TVector3 &) const
Definition: GFDetPlane.cxx:354
bool operator==(const genf::GFDetPlane &, const genf::GFDetPlane &)
Definition: GFDetPlane.cxx:270
std::void_t< T > n
genf::GFAbsFinitePlane * fFinitePlane
Definition: GFDetPlane.h:162
TVector3 toLab(const TVector2 &x) const
transform from plane coordinates to lab system
Definition: GFDetPlane.cxx:203
TVector2 LabToPlane(const TVector3 &x) const
transform from Lab system into plane
Definition: GFDetPlane.cxx:196
p
Definition: test.py:223
virtual ~GFDetPlane()
Definition: GFDetPlane.cxx:61
null
Definition: demo.py:50
TVector2 straightLineToPlane(const TVector3 &point, const TVector3 &dir) const
gives u,v coordinates of the intersection point of a straight line with plane
Definition: GFDetPlane.cxx:368
Exception class for error handling in GENFIT (provides storage for diagnostic information) ...
Definition: GFException.h:48
void setNormal(TVector3 n)
Definition: GFDetPlane.cxx:163
GFException & setFatal(bool b=true)
set fatal flag. if this is true, the fit stops for this current track repr.
Definition: GFException.h:78
list x
Definition: train.py:276
virtual GFAbsFinitePlane * clone() const =0
Deep copy ctor for polymorphic class.
static QCString * s
Definition: config.cpp:1042
QTextStream & endl(QTextStream &s)
GFDetPlane(genf::GFAbsFinitePlane *finite=NULL)
Definition: GFDetPlane.cxx:44