]> git.uio.no Git - u/mrichter/AliRoot.git/blob - STEER/AliStrLine.cxx
5a6ab1d65d4310d4f708d8063b8796ed1c946ad2
[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   // Default constructor
30   for(Int_t i=0;i<3;i++) {
31     fP0[i] = 0.;
32     fCd[i] = 0.;
33   }
34   fTpar = 0.;
35   SetDebug();
36 }
37
38 //________________________________________________________
39 AliStrLine::AliStrLine(Double_t *point, Double_t *cd) {
40   // Standard constructor
41   Double_t norm = 0.;
42   for(Int_t i=0;i<3;i++)norm+=cd[i]*cd[i];
43   if(norm) {
44     norm = TMath::Sqrt(norm);
45     for(Int_t i=0;i<3;i++) cd[i]/=norm;
46   }
47   else {
48     Error("AliStrLine","Null direction cosines!!!");
49   }
50   SetP0(point);
51   SetCd(cd);
52   fTpar = 0.;
53   SetDebug();
54 }
55
56 //________________________________________________________
57 AliStrLine::~AliStrLine() {
58   // destructor
59 }
60
61 //________________________________________________________
62 void AliStrLine::PrintStatus() const {
63   // Print current status
64   cout <<"=======================================================\n";
65   cout <<"Direction cosines: ";
66   for(Int_t i=0;i<3;i++)cout <<fCd[i]<<"; ";
67   cout <<endl;
68   cout <<"Known point: ";
69   for(Int_t i=0;i<3;i++)cout <<fP0[i]<<"; ";
70   cout <<endl;
71   cout <<"Current value for the parameter: "<<fTpar<<endl;
72   cout <<" Debug flag: "<<fDebug<<endl;
73 }
74
75 //________________________________________________________
76 Int_t AliStrLine::IsParallelTo(AliStrLine *line) const {
77   // returns 1 if lines are parallel, 0 if not paralel
78   Double_t cd2[3];
79   line->GetCd(cd2);
80   Double_t vecpx=fCd[1]*cd2[2]-fCd[2]*cd2[1];
81   if(vecpx!=0) return 0;
82   Double_t vecpy=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
83   if(vecpy!=0) return 0;
84   Double_t vecpz=fCd[0]*cd2[1]-fCd[1]*cd2[0];
85   if(vecpz!=0) return 0;
86   return 1;
87 }
88 //________________________________________________________
89 Int_t AliStrLine::Crossrphi(AliStrLine *line){
90   // Cross 2 lines in the X-Y plane
91   Double_t p2[3];
92   Double_t cd2[3];
93   line->GetP0(p2);
94   line->GetCd(cd2);
95   Double_t a=fCd[0];
96   Double_t b=-cd2[0];
97   Double_t c=p2[0]-fP0[0];
98   Double_t d=fCd[1];
99   Double_t e=-cd2[1];
100   Double_t f=p2[1]-fP0[1];
101   Double_t deno = a*e-b*d;
102   Int_t retcode = 0;
103   if(deno != 0.) {
104     fTpar = (c*e-b*f)/deno;
105   }
106   else {
107     retcode = -1;
108   }
109   return retcode;
110 }
111
112 //________________________________________________________
113 Int_t AliStrLine::CrossPoints(AliStrLine *line, Double_t *point1, Double_t *point2){
114   // Looks for the crossing point estimated starting from the
115   // DCA segment
116   Double_t p2[3];
117   Double_t cd2[3];
118   line->GetP0(p2);
119   line->GetCd(cd2);
120   Int_t i;
121   Double_t k1 = 0;
122   for(i=0;i<3;i++)k1+=(fP0[i]-p2[i])*fCd[i];
123   Double_t k2 = 0;
124   for(i=0;i<3;i++)k2+=(fP0[i]-p2[i])*cd2[i];
125   Double_t a11 = 0;
126   for(i=0;i<3;i++)a11+=fCd[i]*cd2[i];
127   Double_t a22 = -a11;
128   Double_t a21 = 0;
129   for(i=0;i<3;i++)a21+=cd2[i]*cd2[i];
130   Double_t a12 = 0;
131   for(i=0;i<3;i++)a12-=fCd[i]*fCd[i];
132   Double_t deno = a11*a22-a21*a12;
133   if(deno == 0.) return -1;
134   fTpar = (a11*k2-a21*k1) / deno;
135   Double_t par2 = (k1*a22-k2*a12) / deno;
136   line->SetPar(par2);
137   GetCurrentPoint(point1);
138   line->GetCurrentPoint(point2);
139   return 0;
140 }
141 //________________________________________________________________
142 Int_t AliStrLine::Cross(AliStrLine *line, Double_t *point){
143
144   //Finds intersection between lines
145   Double_t point1[3];
146   Double_t point2[3];
147   Int_t retcod=CrossPoints(line,point1,point2);
148   if(retcod==0){
149     for(Int_t i=0;i<3;i++)point[i]=(point1[i]+point2[i])/2.;
150     return 0;
151   }else{
152     return -1;
153   }
154 }
155
156 //___________________________________________________________
157 Double_t AliStrLine::GetDCA(AliStrLine *line){
158   //Returns the distance of closest approach between two lines
159   Double_t p2[3];
160   Double_t cd2[3];
161   line->GetP0(p2);
162   line->GetCd(cd2);
163   Int_t i;
164   Int_t ispar=IsParallelTo(line);
165   if(ispar){
166     Double_t dist1q=0,dist2=0,mod=0;
167     for(i=0;i<3;i++){
168       dist1q+=(fP0[i]-p2[i])*(fP0[i]-p2[i]);
169       dist2+=(fP0[i]-p2[i])*fCd[i];
170       mod+=fCd[i]*fCd[i];
171     }
172     if(mod!=0){
173       dist2/=mod;
174       return TMath::Sqrt(dist1q-dist2*dist2);
175     }else{return -1;}
176   }else{
177      Double_t perp[3];
178      perp[0]=fCd[1]*cd2[2]-fCd[2]*cd2[1];
179      perp[1]=-fCd[0]*cd2[2]+fCd[2]*cd2[0];
180      perp[2]=fCd[0]*cd2[1]-fCd[1]*cd2[0];
181      Double_t mod=0,dist=0;
182      for(i=0;i<3;i++){
183        mod+=perp[i]*perp[i];
184        dist+=(fP0[i]-p2[i])*perp[i];
185      }
186      mod=sqrt(mod);
187      if(mod!=0){
188        dist/=mod;
189        return TMath::Abs(dist);
190      }else{return -1;}
191   }
192 }
193 //________________________________________________________
194 void AliStrLine::GetCurrentPoint(Double_t *point) const {
195   // Fills the array point with the current value on the line
196   for(Int_t i=0;i<3;i++)point[i]=fP0[i]+fCd[i]*fTpar;
197 }