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 if(source.fWMatrix)fWMatrix = new Double_t [6];
208 for(Int_t i=0;i<3;i++){
209 fP0[i]=source.fP0[i];
210 fSigma2P0[i]=source.fSigma2P0[i];
211 fCd[i]=source.fCd[i];
213 for(Int_t i=0;i<6;i++)fWMatrix[i]=source.fWMatrix[i];
216 //________________________________________________________
217 AliStrLine& AliStrLine::operator=(const AliStrLine& source){
218 // Assignment operator
221 new(this)AliStrLine(source);
226 //________________________________________________________
227 void AliStrLine::GetWMatrix(Double_t *wmat)const {
228 // Getter for weighting matrix, as a [9] dim. array
231 for(Int_t i=0;i<3;i++){
232 for(Int_t j=0;j<3;j++){
234 wmat[3*i+j]=fWMatrix[k++];
237 wmat[3*i+j]=wmat[3*j+i];
243 //________________________________________________________
244 void AliStrLine::SetWMatrix(const Double_t *wmat) {
245 // Setter for weighting matrix, strating from a [9] dim. array
246 if(fWMatrix)delete [] fWMatrix;
247 fWMatrix = new Double_t [6];
249 for(Int_t i=0;i<3;i++){
250 for(Int_t j=0;j<3;j++)if(j>=i)fWMatrix[k++]=wmat[3*i+j];
254 //________________________________________________________
255 void AliStrLine::InitDirection(Double_t *point, Double_t *cd){
256 // Initialization from a point and a direction
258 for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
260 norm = TMath::Sqrt(norm);
261 for(Int_t i=0;i<3;i++) cd[i]/=norm;
264 Error("AliStrLine","Null direction cosines!!!");
271 //________________________________________________________
272 void AliStrLine::InitTwoPoints(Double_t *pA, Double_t *pB){
273 // Initialization from the coordinates of two
274 // points in the space
276 for(Int_t i=0;i<3;i++)cd[i] = pB[i]-pA[i];
277 InitDirection(pA,cd);
280 //________________________________________________________
281 AliStrLine::~AliStrLine() {
283 if(fWMatrix)delete [] fWMatrix;
286 //________________________________________________________
287 void AliStrLine::PrintStatus() const {
288 // Print current status
289 cout <<"=======================================================\n";
290 cout <<"Direction cosines: ";
291 for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
293 cout <<"Known point: ";
294 for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
296 cout <<"Error on known point: ";
297 for(Int_t i=0;i<3;i++)cout <<TMath::Sqrt(fSigma2P0[i])<<"; ";
299 cout <<"Current value for the parameter: "<<fTpar<<endl;
302 //________________________________________________________
303 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
304 // returns 1 if lines are parallel, 0 if not paralel
307 Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
308 if(vecpx!=0) return 0;
309 Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
310 if(vecpy!=0) return 0;
311 Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
312 if(vecpz!=0) return 0;
315 //________________________________________________________
316 Int_t AliStrLine::Crossrphi(AliStrLine *line){
317 // Cross 2 lines in the X-Y plane
324 Double_t c=p2[0]-fP0[0];
327 Double_t f=p2[1]-fP0[1];
328 Double_t deno = a*e-b*d;
331 fTpar = (c*e-b*f)/deno;
339 //________________________________________________________
340 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
341 // Looks for the crossing point estimated starting from the
352 k1+=(fP0[i]-p2[i])*fCd[i];
353 k2+=(fP0[i]-p2[i])*cd2[i];
363 Double_t deno = a11*a22-a21*a12;
364 if(deno == 0.) return -1;
365 fTpar = (a11*k2-a21*k1) / deno;
366 Double_t par2 = (k1*a22-k2*a12) / deno;
368 GetCurrentPoint(point1);
369 line->GetCurrentPoint(point2);
372 //________________________________________________________________
373 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
375 //Finds intersection between lines
378 Int_t retcod=CrossPoints(line,point1,point2);
380 for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
387 //___________________________________________________________
388 Double_t AliStrLine::GetDCA(AliStrLine *line) const{
389 //Returns the distance of closest approach between two lines
395 Int_t ispar=IsParallelTo(line);
397 Double_t dist1q=0,dist2=0,mod=0;
399 dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
400 dist2+=(fP0[i]-p2[i])*fCd[i];
405 return TMath::Sqrt(dist1q-dist2*dist2);
409 perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
410 perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
411 perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
412 Double_t mod=0,dist=0;
414 mod+=perp[i]*perp[i];
415 dist+=(fP0[i]-p2[i])*perp[i];
420 return TMath::Abs(dist);
424 //________________________________________________________
425 void AliStrLine::GetCurrentPoint(Double_t *point) const {
426 // Fills the array point with the current value on the line
427 for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
430 //________________________________________________________
431 Double_t AliStrLine::GetDistFromPoint(Double_t *point) const {
432 // computes distance from point
433 AliStrLine tmp(point,(Double_t *)fCd,kFALSE);
434 return this->GetDCA(&tmp);