1 /**************************************************************************
2 * Copyright(c) 1998-2003, ALICE Experiment at CERN, All rights reserved. *
4 * Author: The ALICE Off-line Project. *
5 * Contributors are mentioned in the code where appropriate. *
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 **************************************************************************/
18 ///////////////////////////////////////////////////////////////////
20 // A straight line is coded as a point (3 Double_t) and //
21 // 3 direction cosines //
23 ///////////////////////////////////////////////////////////////////
25 #include <Riostream.h>
29 #include "AliStrLine.h"
33 //________________________________________________________
34 AliStrLine::AliStrLine() :
39 // Default constructor
40 for(Int_t i=0;i<3;i++) {
45 for(Int_t i=0;i<9;i++) fWMatrix[i] = 0.;
48 //________________________________________________________
49 AliStrLine::AliStrLine(Double_t *point, Double_t *cd,Bool_t twopoints) :
54 // Standard constructor
55 // if twopoints is true: point and cd are the 3D coordinates of
56 // two points defininig the straight line
57 // if twopoint is false: point represents the 3D coordinates of a point
58 // belonging to the straight line and cd is the
60 for(Int_t i=0;i<3;i++){
63 for(Int_t i=0;i<9;i++) fWMatrix[i] = 0.;
65 InitTwoPoints(point,cd);
68 InitDirection(point,cd);
72 //________________________________________________________
73 AliStrLine::AliStrLine(Float_t *pointf, Float_t *cdf,Bool_t twopoints) :
78 // Standard constructor - with float arguments
79 // if twopoints is true: point and cd are the 3D coordinates of
80 // two points defininig the straight line
81 // if twopoint is false: point represents the 3D coordinates of a point
82 // belonging to the straight line and cd is the
86 for(Int_t i=0;i<3;i++){
91 for(Int_t i=0;i<9;i++) fWMatrix[i] = 0.;
93 InitTwoPoints(point,cd);
96 InitDirection(point,cd);
100 //________________________________________________________
101 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *cd,Bool_t twopoints) :
106 // Standard constructor
107 // if twopoints is true: point and cd are the 3D coordinates of
108 // two points defininig the straight line
109 // if twopoint is false: point represents the 3D coordinates of a point
110 // belonging to the straight line and cd is the
111 // direction in space
112 for(Int_t i=0;i<3;i++){
113 fSigma2P0[i] = sig2point[i];
115 for(Int_t i=0;i<9;i++) fWMatrix[i] = 0.;
117 InitTwoPoints(point,cd);
120 InitDirection(point,cd);
124 //________________________________________________________
125 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *cdf,Bool_t twopoints) :
130 // Standard constructor - with float arguments
131 // if twopoints is true: point and cd are the 3D coordinates of
132 // two points defininig the straight line
133 // if twopoint is false: point represents the 3D coordinates of a point
134 // belonging to the straight line and cd is the
135 // direction in space
138 for(Int_t i=0;i<3;i++){
139 point[i] = pointf[i];
141 fSigma2P0[i] = sig2point[i];
143 for(Int_t i=0;i<9;i++) fWMatrix[i] = 0.;
145 InitTwoPoints(point,cd);
148 InitDirection(point,cd);
151 //________________________________________________________
152 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *wmat, Double_t *cd,Bool_t twopoints) :
157 // Standard constructor
158 // if twopoints is true: point and cd are the 3D coordinates of
159 // two points defininig the straight line
160 // if twopoint is false: point represents the 3D coordinates of a point
161 // belonging to the straight line and cd is the
162 // direction in space
163 for(Int_t i=0;i<3;i++){
164 fSigma2P0[i] = sig2point[i];
166 for(Int_t i=0;i<9;i++) fWMatrix[i] = wmat[i];
168 InitTwoPoints(point,cd);
171 InitDirection(point,cd);
175 //________________________________________________________
176 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *wmat, Float_t *cdf,Bool_t twopoints) :
181 // Standard constructor - with float arguments
182 // if twopoints is true: point and cd are the 3D coordinates of
183 // two points defininig the straight line
184 // if twopoint is false: point represents the 3D coordinates of a point
185 // belonging to the straight line and cd is the
186 // direction in space
189 for(Int_t i=0;i<3;i++){
190 point[i] = pointf[i];
192 fSigma2P0[i] = sig2point[i];
194 for(Int_t i=0;i<9;i++) fWMatrix[i] = wmat[i];
196 InitTwoPoints(point,cd);
199 InitDirection(point,cd);
202 //________________________________________________________
203 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
204 // Initialization from a point and a direction
206 for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
208 norm = TMath::Sqrt(norm);
209 for(Int_t i=0;i<3;i++) cd[i]/=norm;
212 Error("AliStrLine","Null direction cosines!!!");
220 //________________________________________________________
221 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
222 // Initialization from the coordinates of two
223 // points in the space
225 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
226 InitDirection(pA,cd);
229 //________________________________________________________
230 AliStrLine::~AliStrLine() {
234 //________________________________________________________
235 void AliStrLine::PrintStatus() const {
236 // Print current status
237 cout <<"=======================================================\n";
238 cout <<"Direction cosines: ";
239 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
241 cout <<"Known point: ";
242 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
244 cout <<"Error on known point: ";
245 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
247 cout <<"Current value for the parameter: "<<fTpar<<endl;
248 cout <<" Debug flag: "<<fDebug<<endl;
251 //________________________________________________________
252 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
253 // returns 1 if lines are parallel, 0 if not paralel
256 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
257 if(vecpx!=0) return 0;
258 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
259 if(vecpy!=0) return 0;
260 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
261 if(vecpz!=0) return 0;
264 //________________________________________________________
265 Int_t AliStrLine::Crossrphi(AliStrLine *line){
266 // Cross 2 lines in the X-Y plane
273 Double_t c=p2[0]-fP0[0];
276 Double_t f=p2[1]-fP0[1];
277 Double_t deno = a*e-b*d;
280 fTpar = (c*e-b*f)/deno;
288 //________________________________________________________
289 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
290 // Looks for the crossing point estimated starting from the
298 for(i=0;i<3;i++)k1+=(fP0[i]-p2[i])*fCd[i];
300 for(i=0;i<3;i++)k2+=(fP0[i]-p2[i])*cd2[i];
302 for(i=0;i<3;i++)a11+=fCd[i]*cd2[i];
305 for(i=0;i<3;i++)a21+=cd2[i]*cd2[i];
307 for(i=0;i<3;i++)a12-=fCd[i]*fCd[i];
308 Double_t deno = a11*a22-a21*a12;
309 if(deno == 0.) return -1;
310 fTpar = (a11*k2-a21*k1) / deno;
311 Double_t par2 = (k1*a22-k2*a12) / deno;
313 GetCurrentPoint(point1);
314 line->GetCurrentPoint(point2);
317 //________________________________________________________________
318 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
320 //Finds intersection between lines
323 Int_t retcod=CrossPoints(line,point1,point2);
325 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
332 //___________________________________________________________
333 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
334 //Returns the distance of closest approach between two lines
340 Int_t ispar=IsParallelTo(line);
342 Double_t dist1q=0,dist2=0,mod=0;
344 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
345 dist2+=(fP0[i]-p2[i])*fCd[i];
350 return TMath::Sqrt(dist1q-dist2*dist2);
354 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
355 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
356 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
357 Double_t mod=0,dist=0;
359 mod+=perp[i]*perp[i];
360 dist+=(fP0[i]-p2[i])*perp[i];
365 return TMath::Abs(dist);
369 //________________________________________________________
370 void AliStrLine::GetCurrentPoint(Double_t *point) const {
371 // Fills the array point with the current value on the line
372 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
375 //________________________________________________________
376 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
377 // computes distance from point
378 AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
379 return this->GetDCA(&tmp);