]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStrLine.cxx
8eea1aaaaa1c688dda1e041436545050e81f72f6
[u/mrichter/AliRoot.git] / STEER / AliStrLine.cxx
1 /**************************************************************************
2  * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
3  *                                                                        *
4  * Author: The ALICE Off-line Project.                                    *
5  * Contributors are mentioned in the code where appropriate.              *
6  *                                                                        *
7  * Permission to use, copy, modify and distribute this software and its   *
8  * documentation strictly for non-commercial purposes is hereby granted   *
9  * without fee, provided that the above copyright notice appears in all   *
10  * copies and that both the copyright notice and this permission notice   *
11  * appear in the supporting documentation. The authors make no claims     *
12  * about the suitability of this software for any purpose. It is          *
13  * provided "as is" without express or implied warranty.                  *
14  **************************************************************************/
15 ///////////////////////////////////////////////////////////////////
16 //                                                               //
17 // A straight line is coded as a point (3 Double_t) and           //
18 // 3 direction cosines                                           //
19 //                                                               //
20 ///////////////////////////////////////////////////////////////////
21 #include <Riostream.h>
22 #include <TTree.h>
23 #include "AliStrLine.h"
24
25 ClassImp(AliStrLine)
26
27 //________________________________________________________
28 AliStrLine::AliStrLine() :
29   TObject(),
30   fTpar(0),
31   fDebug(0)
32  {
33   // Default constructor
34   for(Int_t i=0;i<3;i++) {
35     fP0[i] = 0.;
36     fCd[i] = 0.;
37   }
38 }
39
40 //________________________________________________________
41 AliStrLine::AliStrLine(Double_t *point, Double_t *cd,Bool_t twopoints) :
42   TObject(),
43   fTpar(0),
44   fDebug(0)
45 {
46   // Standard constructor
47   // if twopoints is true:  point and cd are the 3D coordinates of
48   //                        two points defininig the straight line
49   // if twopoint is false: point represents the 3D coordinates of a point
50   //                       belonging to the straight line and cd is the
51   //                       direction in space
52   if(twopoints){
53     InitTwoPoints(point,cd);
54   }
55   else {
56     InitDirection(point,cd);
57   }
58 }
59
60 //________________________________________________________
61 AliStrLine::AliStrLine(Float_t *pointf, Float_t *cdf,Bool_t twopoints) :
62   TObject(),
63   fTpar(0),
64   fDebug(0)
65 {
66   // Standard constructor - with float arguments
67   // if twopoints is true:  point and cd are the 3D coordinates of
68   //                        two points defininig the straight line
69   // if twopoint is false: point represents the 3D coordinates of a point
70   //                       belonging to the straight line and cd is the
71   //                       direction in space
72   Double_t point[3];
73   Double_t cd[3];
74   for(Int_t i=0;i<3;i++){
75     point[i] = pointf[i];
76     cd[i] = cdf[i];
77   }
78   if(twopoints){
79     InitTwoPoints(point,cd);
80   }
81   else {
82     InitDirection(point,cd);
83   }
84 }
85
86 //________________________________________________________
87 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
88   // Initialization from a point and a direction
89   Double_t norm = 0.;
90   for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
91   if(norm) {
92     norm = TMath::Sqrt(norm);
93     for(Int_t i=0;i<3;i++) cd[i]/=norm;
94   }
95   else {
96     Error("AliStrLine","Null direction cosines!!!");
97   }
98   SetP0(point);
99   SetCd(cd);
100   fTpar = 0.;
101   SetDebug();
102 }
103
104 //________________________________________________________
105 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
106   // Initialization from the coordinates of two
107   // points in the space
108   Double_t cd[3];
109   for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
110   InitDirection(pA,cd);
111 }
112
113 //________________________________________________________
114 AliStrLine::~AliStrLine() {
115   // destructor
116 }
117
118 //________________________________________________________
119 void AliStrLine::PrintStatus() const {
120   // Print current status
121   cout <<"=======================================================\n";
122   cout <<"Direction cosines: ";
123   for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
124   cout <<endl;
125   cout <<"Known point: ";
126   for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
127   cout <<endl;
128   cout <<"Current value for the parameter: "<<fTpar<<endl;
129   cout <<" Debug flag: "<<fDebug<<endl;
130 }
131
132 //________________________________________________________
133 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
134   // returns 1 if lines are parallel, 0 if not paralel
135   Double_t cd2[3];
136   line->GetCd(cd2);
137   Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
138   if(vecpx!=0) return 0;
139   Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
140   if(vecpy!=0) return 0;
141   Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
142   if(vecpz!=0) return 0;
143   return 1;
144 }
145 //________________________________________________________
146 Int_t AliStrLine::Crossrphi(AliStrLine *line){
147   // Cross 2 lines in the X-Y plane
148   Double_t p2[3];
149   Double_t cd2[3];
150   line->GetP0(p2);
151   line->GetCd(cd2);
152   Double_t a=fCd[0];
153   Double_t b=-cd2[0];
154   Double_t c=p2[0]-fP0[0];
155   Double_t d=fCd[1];
156   Double_t e=-cd2[1];
157   Double_t f=p2[1]-fP0[1];
158   Double_t deno = a*e-b*d;
159   Int_t retcode = 0;
160   if(deno != 0.) {
161     fTpar = (c*e-b*f)/deno;
162   }
163   else {
164     retcode = -1;
165   }
166   return retcode;
167 }
168
169 //________________________________________________________
170 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
171   // Looks for the crossing point estimated starting from the
172   // DCA segment
173   Double_t p2[3];
174   Double_t cd2[3];
175   line->GetP0(p2);
176   line->GetCd(cd2);
177   Int_t i;
178   Double_t k1 = 0;
179   for(i=0;i<3;i++)k1+=(fP0[i]-p2[i])*fCd[i];
180   Double_t k2 = 0;
181   for(i=0;i<3;i++)k2+=(fP0[i]-p2[i])*cd2[i];
182   Double_t a11 = 0;
183   for(i=0;i<3;i++)a11+=fCd[i]*cd2[i];
184   Double_t a22 = -a11;
185   Double_t a21 = 0;
186   for(i=0;i<3;i++)a21+=cd2[i]*cd2[i];
187   Double_t a12 = 0;
188   for(i=0;i<3;i++)a12-=fCd[i]*fCd[i];
189   Double_t deno = a11*a22-a21*a12;
190   if(deno == 0.) return -1;
191   fTpar = (a11*k2-a21*k1) / deno;
192   Double_t par2 = (k1*a22-k2*a12) / deno;
193   line->SetPar(par2);
194   GetCurrentPoint(point1);
195   line->GetCurrentPoint(point2);
196   return 0;
197 }
198 //________________________________________________________________
199 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
200
201   //Finds intersection between lines
202   Double_t point1[3];
203   Double_t point2[3];
204   Int_t retcod=CrossPoints(line,point1,point2);
205   if(retcod==0){
206     for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
207     return 0;
208   }else{
209     return -1;
210   }
211 }
212
213 //___________________________________________________________
214 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
215   //Returns the distance of closest approach between two lines
216   Double_t p2[3];
217   Double_t cd2[3];
218   line->GetP0(p2);
219   line->GetCd(cd2);
220   Int_t i;
221   Int_t ispar=IsParallelTo(line);
222   if(ispar){
223     Double_t dist1q=0,dist2=0,mod=0;
224     for(i=0;i<3;i++){
225       dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
226       dist2+=(fP0[i]-p2[i])*fCd[i];
227       mod+=fCd[i]*fCd[i];
228     }
229     if(mod!=0){
230       dist2/=mod;
231       return TMath::Sqrt(dist1q-dist2*dist2);
232     }else{return -1;}
233   }else{
234      Double_t perp[3];
235      perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
236      perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
237      perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
238      Double_t mod=0,dist=0;
239      for(i=0;i<3;i++){
240        mod+=perp[i]*perp[i];
241        dist+=(fP0[i]-p2[i])*perp[i];
242      }
243      mod=sqrt(mod);
244      if(mod!=0){
245        dist/=mod;
246        return TMath::Abs(dist);
247      }else{return -1;}
248   }
249 }
250 //________________________________________________________
251 void AliStrLine::GetCurrentPoint(Double_t *point) const {
252   // Fills the array point with the current value on the line
253   for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
254 }
255
256 //________________________________________________________
257 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
258   // computes distance from point 
259   AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
260   return this->GetDCA(&tmp);
261 }