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