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++) {
47 //________________________________________________________
48 AliStrLine::AliStrLine(Double_t *point, Double_t *cd,Bool_t twopoints) :
53 // Standard constructor
54 // if twopoints is true: point and cd are the 3D coordinates of
55 // two points defininig the straight line
56 // if twopoint is false: point represents the 3D coordinates of a point
57 // belonging to the straight line and cd is the
59 for(Int_t i=0;i<3;i++){
63 InitTwoPoints(point,cd);
66 InitDirection(point,cd);
70 //________________________________________________________
71 AliStrLine::AliStrLine(Float_t *pointf, Float_t *cdf,Bool_t twopoints) :
76 // Standard constructor - with float arguments
77 // if twopoints is true: point and cd are the 3D coordinates of
78 // two points defininig the straight line
79 // if twopoint is false: point represents the 3D coordinates of a point
80 // belonging to the straight line and cd is the
84 for(Int_t i=0;i<3;i++){
90 InitTwoPoints(point,cd);
93 InitDirection(point,cd);
97 //________________________________________________________
98 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *cd,Bool_t twopoints) :
103 // Standard constructor
104 // if twopoints is true: point and cd are the 3D coordinates of
105 // two points defininig the straight line
106 // if twopoint is false: point represents the 3D coordinates of a point
107 // belonging to the straight line and cd is the
108 // direction in space
109 for(Int_t i=0;i<3;i++){
110 fSigma2P0[i] = sig2point[i];
113 InitTwoPoints(point,cd);
116 InitDirection(point,cd);
120 //________________________________________________________
121 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *cdf,Bool_t twopoints) :
126 // Standard constructor - with float arguments
127 // if twopoints is true: point and cd are the 3D coordinates of
128 // two points defininig the straight line
129 // if twopoint is false: point represents the 3D coordinates of a point
130 // belonging to the straight line and cd is the
131 // direction in space
134 for(Int_t i=0;i<3;i++){
135 point[i] = pointf[i];
137 fSigma2P0[i] = sig2point[i];
140 InitTwoPoints(point,cd);
143 InitDirection(point,cd);
146 //________________________________________________________
147 AliStrLine::AliStrLine(Double_t *point, Double_t *sig2point, Double_t *wmat, Double_t *cd,Bool_t twopoints) :
152 // Standard constructor
153 // if twopoints is true: point and cd are the 3D coordinates of
154 // two points defininig the straight line
155 // if twopoint is false: point represents the 3D coordinates of a point
156 // belonging to the straight line and cd is the
157 // direction in space
159 fWMatrix = new Double_t [6];
160 for(Int_t i=0;i<3;i++){
161 fSigma2P0[i] = sig2point[i];
162 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
165 InitTwoPoints(point,cd);
168 InitDirection(point,cd);
172 //________________________________________________________
173 AliStrLine::AliStrLine(Float_t *pointf, Float_t *sig2point, Float_t *wmat, Float_t *cdf,Bool_t twopoints) :
178 // Standard constructor - with float arguments
179 // if twopoints is true: point and cd are the 3D coordinates of
180 // two points defininig the straight line
181 // if twopoint is false: point represents the 3D coordinates of a point
182 // belonging to the straight line and cd is the
183 // direction in space
186 fWMatrix = new Double_t [6];
188 for(Int_t i=0;i<3;i++){
189 point[i] = pointf[i];
191 fSigma2P0[i] = sig2point[i];
192 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
195 InitTwoPoints(point,cd);
198 InitDirection(point,cd);
202 //________________________________________________________
203 AliStrLine::AliStrLine(const AliStrLine &source):TObject(source),
207 for(Int_t i=0;i<3;i++){
208 fP0[i]=source.fP0[i];
209 fSigma2P0[i]=source.fSigma2P0[i];
210 fCd[i]=source.fCd[i];
213 fWMatrix = new Double_t [6];
214 for(Int_t i=0;i<6;i++)fWMatrix[i]=source.fWMatrix[i];
218 //________________________________________________________
219 AliStrLine& AliStrLine::operator=(const AliStrLine& source){
220 // Assignment operator
223 new(this)AliStrLine(source);
228 //________________________________________________________
229 void AliStrLine::GetWMatrix(Double_t *wmat)const {
230 // Getter for weighting matrix, as a [9] dim. array
233 for(Int_t i=0;i<3;i++){
234 for(Int_t j=0;j<3;j++){
236 wmat[3*i+j]=fWMatrix[k++];
239 wmat[3*i+j]=wmat[3*j+i];
245 //________________________________________________________
246 void AliStrLine::SetWMatrix(const Double_t *wmat) {
247 // Setter for weighting matrix, strating from a [9] dim. array
248 if(fWMatrix)delete [] fWMatrix;
249 fWMatrix = new Double_t [6];
251 for(Int_t i=0;i<3;i++){
252 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
256 //________________________________________________________
257 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
258 // Initialization from a point and a direction
260 for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
262 norm = TMath::Sqrt(norm);
263 for(Int_t i=0;i<3;i++) cd[i]/=norm;
266 Error("AliStrLine","Null direction cosines!!!");
273 //________________________________________________________
274 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
275 // Initialization from the coordinates of two
276 // points in the space
278 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
279 InitDirection(pA,cd);
282 //________________________________________________________
283 AliStrLine::~AliStrLine() {
285 if(fWMatrix)delete [] fWMatrix;
288 //________________________________________________________
289 void AliStrLine::PrintStatus() const {
290 // Print current status
291 cout <<"=======================================================\n";
292 cout <<"Direction cosines: ";
293 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
295 cout <<"Known point: ";
296 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
298 cout <<"Error on known point: ";
299 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
301 cout <<"Current value for the parameter: "<<fTpar<<endl;
304 //________________________________________________________
305 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
306 // returns 1 if lines are parallel, 0 if not paralel
309 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
310 if(vecpx!=0) return 0;
311 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
312 if(vecpy!=0) return 0;
313 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
314 if(vecpz!=0) return 0;
317 //________________________________________________________
318 Int_t AliStrLine::Crossrphi(AliStrLine *line){
319 // Cross 2 lines in the X-Y plane
326 Double_t c=p2[0]-fP0[0];
329 Double_t f=p2[1]-fP0[1];
330 Double_t deno = a*e-b*d;
333 fTpar = (c*e-b*f)/deno;
341 //________________________________________________________
342 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
343 // Looks for the crossing point estimated starting from the
354 k1+=(fP0[i]-p2[i])*fCd[i];
355 k2+=(fP0[i]-p2[i])*cd2[i];
365 Double_t deno = a11*a22-a21*a12;
366 if(deno == 0.) return -1;
367 fTpar = (a11*k2-a21*k1) / deno;
368 Double_t par2 = (k1*a22-k2*a12) / deno;
370 GetCurrentPoint(point1);
371 line->GetCurrentPoint(point2);
374 //________________________________________________________________
375 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
377 //Finds intersection between lines
380 Int_t retcod=CrossPoints(line,point1,point2);
382 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
389 //___________________________________________________________
390 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
391 //Returns the distance of closest approach between two lines
397 Int_t ispar=IsParallelTo(line);
399 Double_t dist1q=0,dist2=0,mod=0;
401 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
402 dist2+=(fP0[i]-p2[i])*fCd[i];
407 return TMath::Sqrt(dist1q-dist2*dist2);
411 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
412 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
413 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
414 Double_t mod=0,dist=0;
416 mod+=perp[i]*perp[i];
417 dist+=(fP0[i]-p2[i])*perp[i];
422 return TMath::Abs(dist);
426 //________________________________________________________
427 void AliStrLine::GetCurrentPoint(Double_t *point) const {
428 // Fills the array point with the current value on the line
429 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
432 //________________________________________________________
433 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
434 // computes distance from point
435 AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
436 return this->GetDCA(&tmp);