]>
Commit | Line | Data |
---|---|---|
d88f97cc | 1 | #include "Ali3Vector.h" |
2 | ||
3 | ClassImp(Ali3Vector) // Class implementation to enable ROOT I/O | |
4 | ||
5 | Ali3Vector::Ali3Vector() | |
6 | { | |
7 | // Creation of an Ali3Vector object and initialisation of parameters | |
8 | fV=0; | |
9 | fTheta=0; | |
10 | fPhi=0; | |
11 | } | |
12 | /////////////////////////////////////////////////////////////////////////// | |
13 | Ali3Vector::~Ali3Vector() | |
14 | { | |
15 | // Destructor to delete dynamically allocated memory | |
16 | } | |
17 | /////////////////////////////////////////////////////////////////////////// | |
18 | void Ali3Vector::SetVector(Double_t* v,TString f) | |
19 | { | |
20 | // Store vector according to reference frame f | |
21 | Double_t pi=acos(-1.); | |
22 | Int_t frame=0; | |
23 | if (f == "car") frame=1; | |
24 | if (f == "sph") frame=2; | |
25 | if (f == "cyl") frame=3; | |
26 | ||
27 | Double_t x,y,z,rho,phi; | |
28 | ||
29 | switch (frame) | |
30 | { | |
31 | case 1: // Cartesian coordinates | |
32 | x=v[0]; | |
33 | y=v[1]; | |
34 | z=v[2]; | |
35 | fV=sqrt(x*x+y*y+z*z); | |
36 | fTheta=0; | |
37 | if (fV && fabs(z/fV)<=1.) | |
38 | { | |
39 | fTheta=acos(z/fV); | |
40 | } | |
41 | else | |
42 | { | |
43 | if (z<0.) fTheta=pi; | |
44 | } | |
45 | if (fTheta<0.) fTheta+=2.*pi; | |
46 | fPhi=0; | |
47 | if (x || y) fPhi=atan2(y,x); | |
48 | if (fPhi<0.) fPhi+=2.*pi; | |
49 | break; | |
50 | ||
51 | case 2: // Spherical coordinates | |
52 | fV=v[0]; | |
53 | fTheta=v[1]; | |
54 | fPhi=v[2]; | |
55 | break; | |
56 | ||
57 | case 3: // Cylindrical coordinates | |
58 | rho=v[0]; | |
59 | phi=v[1]; | |
60 | z=v[2]; | |
61 | fV=sqrt(rho*rho+z*z); | |
62 | fPhi=phi; | |
63 | if (fPhi<0.) fPhi+=2.*pi; | |
64 | fTheta=0; | |
65 | if (fV && fabs(z/fV)<=1.) | |
66 | { | |
67 | fTheta=acos(z/fV); | |
68 | } | |
69 | else | |
70 | { | |
71 | if (z<0.) fTheta=pi; | |
72 | } | |
73 | if (fTheta<0.) fTheta+=2.*pi; | |
74 | break; | |
75 | ||
76 | default: // Unsupported reference frame | |
77 | cout << "*Ali3Vector::SetVector* Unsupported frame : " << f << endl | |
78 | << " Possible frames are 'car', 'sph' and 'cyl'." << endl; | |
79 | fV=0; | |
80 | fTheta=0; | |
81 | fPhi=0; | |
82 | break; | |
83 | } | |
84 | } | |
85 | /////////////////////////////////////////////////////////////////////////// | |
86 | void Ali3Vector::GetVector(Double_t* v,TString f) | |
87 | { | |
88 | // Provide vector according to reference frame f | |
89 | Int_t frame=0; | |
90 | if (f == "car") frame=1; | |
91 | if (f == "sph") frame=2; | |
92 | if (f == "cyl") frame=3; | |
93 | ||
94 | switch (frame) | |
95 | { | |
96 | case 1: // Cartesian coordinates | |
97 | v[0]=fV*sin(fTheta)*cos(fPhi); | |
98 | v[1]=fV*sin(fTheta)*sin(fPhi); | |
99 | v[2]=fV*cos(fTheta); | |
100 | break; | |
101 | ||
102 | case 2: // Spherical coordinates | |
103 | v[0]=fV; | |
104 | v[1]=fTheta; | |
105 | v[2]=fPhi; | |
106 | break; | |
107 | ||
108 | case 3: // Cylindrical coordinates | |
109 | v[0]=fV*sin(fTheta); | |
110 | v[1]=fPhi; | |
111 | v[2]=fV*cos(fTheta); | |
112 | break; | |
113 | ||
114 | default: // Unsupported reference frame | |
115 | cout << "*Ali3Vector::GetVector* Unsupported frame : " << f << endl | |
116 | << " Possible frames are 'car', 'sph' and 'cyl'." << endl; | |
117 | for (Int_t i=0; i<3; i++) | |
118 | { | |
119 | v[i]=0; | |
120 | } | |
121 | break; | |
122 | } | |
123 | } | |
124 | /////////////////////////////////////////////////////////////////////////// | |
125 | void Ali3Vector::SetVector(Float_t* v,TString f) | |
126 | { | |
127 | // Store vector according to reference frame f | |
128 | Double_t vec[3]; | |
129 | for (Int_t i=0; i<3; i++) | |
130 | { | |
131 | vec[i]=v[i]; | |
132 | } | |
133 | SetVector(vec,f); | |
134 | } | |
135 | /////////////////////////////////////////////////////////////////////////// | |
136 | void Ali3Vector::GetVector(Float_t* v,TString f) | |
137 | { | |
138 | // Provide vector according to reference frame f | |
139 | Double_t vec[3]; | |
140 | GetVector(vec,f); | |
141 | for (Int_t i=0; i<3; i++) | |
142 | { | |
143 | v[i]=vec[i]; | |
144 | } | |
145 | } | |
146 | /////////////////////////////////////////////////////////////////////////// | |
147 | void Ali3Vector::Info(TString f) | |
148 | { | |
149 | // Print vector components according to reference frame f | |
150 | if (f=="car" || f=="sph" || f=="cyl") | |
151 | { | |
152 | Double_t vec[3]; | |
153 | GetVector(vec,f); | |
154 | cout << " Vector in " << f << " coordinates : " | |
155 | << vec[0] << " " << vec[1] << " " << vec[2] << endl; | |
156 | } | |
157 | else | |
158 | { | |
159 | cout << " *Ali3Vector::Info* Unsupported frame : " << f << endl | |
160 | << " Possible frames are 'car', 'sph' and 'cyl'." << endl; | |
161 | } | |
162 | } | |
163 | /////////////////////////////////////////////////////////////////////////// | |
164 | Double_t Ali3Vector::GetNorm() | |
165 | { | |
166 | return fV; | |
167 | } | |
168 | /////////////////////////////////////////////////////////////////////////// | |
169 | Double_t Ali3Vector::Dot(Ali3Vector& q) | |
170 | { | |
171 | // Provide the dot product of the current vector with vector q | |
172 | Double_t a[3],b[3]; | |
173 | Double_t dotpro=0; | |
174 | ||
175 | GetVector(a,"car"); | |
176 | q.GetVector(b,"car"); | |
177 | for (Int_t i=0; i<3; i++) | |
178 | { | |
179 | dotpro+=a[i]*b[i]; | |
180 | } | |
181 | ||
182 | return dotpro; | |
183 | } | |
184 | /////////////////////////////////////////////////////////////////////////// | |
185 | Ali3Vector Ali3Vector::Cross(Ali3Vector& q) | |
186 | { | |
187 | // Provide the cross product of the current vector with vector q | |
188 | Double_t a[3],b[3],c[3]; | |
189 | ||
190 | GetVector(a,"car"); | |
191 | q.GetVector(b,"car"); | |
192 | ||
193 | c[0]=a[1]*b[2]-a[2]*b[1]; | |
194 | c[1]=a[2]*b[0]-a[0]*b[2]; | |
195 | c[2]=a[0]*b[1]-a[1]*b[0]; | |
196 | ||
197 | Ali3Vector v; | |
198 | v.SetVector(c,"car"); | |
199 | ||
200 | return v; | |
201 | } | |
202 | /////////////////////////////////////////////////////////////////////////// | |
203 | Ali3Vector Ali3Vector::operator+(Ali3Vector& q) | |
204 | { | |
205 | // Add vector q to the current vector | |
206 | Double_t a[3],b[3]; | |
207 | ||
208 | GetVector(a,"car"); | |
209 | q.GetVector(b,"car"); | |
210 | ||
211 | for (Int_t i=0; i<3; i++) | |
212 | { | |
213 | a[i]+=b[i]; | |
214 | } | |
215 | ||
216 | Ali3Vector v; | |
217 | v.SetVector(a,"car"); | |
218 | ||
219 | return v; | |
220 | } | |
221 | /////////////////////////////////////////////////////////////////////////// | |
222 | Ali3Vector Ali3Vector::operator-(Ali3Vector& q) | |
223 | { | |
224 | // Subtract vector q from the current vector | |
225 | Double_t a[3],b[3]; | |
226 | ||
227 | GetVector(a,"car"); | |
228 | q.GetVector(b,"car"); | |
229 | ||
230 | for (Int_t i=0; i<3; i++) | |
231 | { | |
232 | a[i]-=b[i]; | |
233 | } | |
234 | ||
235 | Ali3Vector v; | |
236 | v.SetVector(a,"car"); | |
237 | ||
238 | return v; | |
239 | } | |
240 | /////////////////////////////////////////////////////////////////////////// | |
241 | Ali3Vector Ali3Vector::operator*(Double_t s) | |
242 | { | |
243 | // Multiply the current vector with a scalar s | |
244 | Double_t a[3]; | |
245 | ||
246 | GetVector(a,"car"); | |
247 | ||
248 | for (Int_t i=0; i<3; i++) | |
249 | { | |
250 | a[i]*=s; | |
251 | } | |
252 | ||
253 | Ali3Vector v; | |
254 | v.SetVector(a,"car"); | |
255 | ||
256 | return v; | |
257 | } | |
258 | /////////////////////////////////////////////////////////////////////////// | |
259 | Ali3Vector Ali3Vector::operator/(Double_t s) | |
260 | { | |
261 | // Divide the current vector by a scalar s | |
262 | ||
263 | if (fabs(s)<1.e-20) // Protect against division by 0 | |
264 | { | |
265 | cout << " *Ali3Vector::/* Division by 0 detected. No action taken." << endl; | |
266 | return *this; | |
267 | } | |
268 | else | |
269 | { | |
270 | Double_t a[3]; | |
271 | ||
272 | GetVector(a,"car"); | |
273 | ||
274 | for (Int_t i=0; i<3; i++) | |
275 | { | |
276 | a[i]/=s; | |
277 | } | |
278 | ||
279 | Ali3Vector v; | |
280 | v.SetVector(a,"car"); | |
281 | ||
282 | return v; | |
283 | } | |
284 | } | |
285 | /////////////////////////////////////////////////////////////////////////// | |
286 | Ali3Vector& Ali3Vector::operator+=(Ali3Vector& q) | |
287 | { | |
288 | // Add vector q to the current vector | |
289 | Double_t a[3],b[3]; | |
290 | ||
291 | GetVector(a,"car"); | |
292 | q.GetVector(b,"car"); | |
293 | ||
294 | for (Int_t i=0; i<3; i++) | |
295 | { | |
296 | a[i]+=b[i]; | |
297 | } | |
298 | ||
299 | SetVector(a,"car"); | |
300 | ||
301 | return *this; | |
302 | } | |
303 | /////////////////////////////////////////////////////////////////////////// | |
304 | Ali3Vector& Ali3Vector::operator-=(Ali3Vector& q) | |
305 | { | |
306 | // Subtract vector q from the current vector | |
307 | Double_t a[3],b[3]; | |
308 | ||
309 | GetVector(a,"car"); | |
310 | q.GetVector(b,"car"); | |
311 | ||
312 | for (Int_t i=0; i<3; i++) | |
313 | { | |
314 | a[i]-=b[i]; | |
315 | } | |
316 | ||
317 | SetVector(a,"car"); | |
318 | ||
319 | return *this; | |
320 | } | |
321 | /////////////////////////////////////////////////////////////////////////// | |
322 | Ali3Vector& Ali3Vector::operator*=(Double_t s) | |
323 | { | |
324 | // Multiply the current vector with a scalar s | |
325 | Double_t a[3]; | |
326 | ||
327 | GetVector(a,"car"); | |
328 | ||
329 | for (Int_t i=0; i<3; i++) | |
330 | { | |
331 | a[i]*=s; | |
332 | } | |
333 | ||
334 | SetVector(a,"car"); | |
335 | ||
336 | return *this; | |
337 | } | |
338 | /////////////////////////////////////////////////////////////////////////// | |
339 | Ali3Vector& Ali3Vector::operator/=(Double_t s) | |
340 | { | |
341 | // Divide the current vector by a scalar s | |
342 | ||
343 | if (fabs(s)<1.e-20) // Protect against division by 0 | |
344 | { | |
345 | cout << " *Ali3Vector::/=* Division by 0 detected. No action taken." << endl; | |
346 | return *this; | |
347 | } | |
348 | else | |
349 | { | |
350 | Double_t a[3]; | |
351 | ||
352 | GetVector(a,"car"); | |
353 | ||
354 | for (Int_t i=0; i<3; i++) | |
355 | { | |
356 | a[i]/=s; | |
357 | } | |
358 | ||
359 | SetVector(a,"car"); | |
360 | ||
361 | return *this; | |
362 | } | |
363 | } | |
364 | /////////////////////////////////////////////////////////////////////////// |