StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
StFormulary.cc
1 // $Id: StFormulary.cc,v 1.5 2003/09/16 15:27:01 jcs Exp $
2 // $Log: StFormulary.cc,v $
3 // Revision 1.5 2003/09/16 15:27:01 jcs
4 // removed inline as it would leave a few undefined reference
5 //
6 // Revision 1.4 2002/10/03 10:33:56 oldi
7 // Usage of gufld removed.
8 // Magnetic field is read by StMagUtilities, now.
9 //
10 // Revision 1.3 2002/04/09 16:10:09 oldi
11 // Method to get the magentic field factor moved to StFormulary. It works for
12 // simulation as well, now.
13 //
14 // Revision 1.2 2000/07/18 21:22:15 oldi
15 // Changes due to be able to find laser tracks.
16 // Cleanup: - new functions in StFtpcConfMapper, StFtpcTrack, and StFtpcPoint
17 // to bundle often called functions
18 // - short functions inlined
19 // - formulas of StFormulary made static
20 // - avoid streaming of objects of unknown size
21 // (removes the bunch of CINT warnings during compile time)
22 // - two or three minor bugs cured
23 //
24 // Revision 1.1 2000/05/10 13:39:03 oldi
25 // Initial version of StFtpcTrackMaker
26 //
27 
28 //----------Author: Markus D. Oldenburg
29 //----------Last Modified: 18.07.2000
30 //----------Copyright: &copy MDO Production 1999
31 
32 #include "StFormulary.hh"
33 #include "TMath.h"
34 
36 // StFormulary //
37 // //
38 // The StFormulary class improves the TMath class by adding some often used //
39 // functions and calulations. //
41 
42 ClassImp(StFormulary)
43 
44 void StFormulary::Pq(const Double_t *p, const Double_t *q, Double_t x[2])
45 {
46  // Calculates the x(y=0)-values of a parabola of the form
47  // x^2 + p * x + q = 0.
48 
49  Int_t i;
50 
51  Double_t minus_p_halbe = -(*p)/2.;
52  Double_t wurzel = TMath::Sqrt(-(*q) + TMath::Power((*p)/2., 2));
53 
54  for (i = 0; i < 2; i++) {
55  x[i] = minus_p_halbe + TMath::Power(-1., i) * wurzel;
56  }
57 
58  return;
59 }
60 
61 
62 Double_t StFormulary::Angle(const Double_t *x1, const Double_t *x2, Int_t dim)
63 {
64  // Returns the angle between to vectors (in dim dimensions).
65 
66  Double_t value = ScalarProd(x1, x2, dim)/(Abs(x1, dim) * Abs(x2, dim));
67 
68  // This ugly if statement was necessary because if the angle was exactly 0. TMath::ACos() was confused sometimes.
69  if (TMath::Abs(value) < 1.) {
70  return TMath::ACos(value);
71  }
72 
73  else if (value >= 1. && value < 1.00001) {
74  return 0.;
75  }
76 
77  else {
78  return TMath::ACos(value);
79  }
80 }
81 
82 
83 Double_t StFormulary::Dist_squared(const Double_t *p1, const Double_t *p2, Int_t dim)
84 {
85  // Calculates the distance squared of two points in euclidian geometry.
86 
87  Double_t *minus = new Double_t[dim];
88 
89  Diff(p1, p2, minus, dim);
90  Double_t dist_sq = Square(minus, dim);
91  delete[] minus;
92 
93  return dist_sq;
94 }
95 
96 
97 Double_t StFormulary::CheckASinArg(Double_t asin_arg)
98 {
99  // Returns +1. if asin_arg>1. or -1. if asin_arg<-1.
100 
101  if (TMath::Abs(asin_arg) > 1.) {
102  asin_arg = (asin_arg >= 0) ? +1. : -1.;
103  }
104 
105  return asin_arg;
106 }
107 
108 
109 Double_t StFormulary::RelDiff(const Double_t p1, const Double_t p2)
110 {
111  // Returns the relative diffenrence of two points.
112 
113  return (p1 - p2) / (p1 + p2);
114 }
115 
116 
117 Double_t StFormulary::Dist(const Double_t *p1, const Double_t *p2, Int_t dim)
118 {
119  // Calculates the distance of two points in euclidian geometry.
120 
121  return TMath::Sqrt(Dist_squared(p1, p2, dim));
122 }
123 
124 
125 void StFormulary::VectorProd(const Double_t *a, const Double_t *b, Double_t *c)
126 {
127  // Calculates the (euclidian) vector product of two vectors in three dimensions.
128 
129  c[1] = a[2]*b[3] - a[3]*b[2];
130  c[2] = a[3]*b[1] - a[1]*b[3];
131  c[3] = a[1]*b[2] - a[2]*b[1];
132 
133  return;
134 }
135 
136 
137 Double_t StFormulary::ScalarProd(const Double_t *a, const Double_t *b, Int_t dim)
138 {
139  // Calculates the (euclidian) scalar product of two vectors
140 
141  Double_t result = 0.;
142 
143  for (Int_t i=0; i<dim; result += a[i] * b[i], i++);
144 
145  return result;
146 }
147 
148 
149 Double_t StFormulary::Square(const Double_t *p, Int_t dim)
150 {
151  // Calculates the scalar product of the given vector with itself.
152 
153  return ScalarProd(p, p, dim);
154 }
155 
156 
157 void StFormulary::Sum(const Double_t *p1, const Double_t *p2, Double_t *P, Int_t n)
158 {
159  // Calculates the sum of the two n-dimensional vectors and stores the
160  // result in the vector P.
161 
162  for (Int_t i=0; i<n; P[i] = p1[i] + p2[i], i++);
163 
164  return;
165 }
166 
167 
168 void StFormulary::Diff(const Double_t *p1, const Double_t *p2, Double_t *P, Int_t n)
169 {
170  // Calculates the difference of the two n-dimensional vectors and stores the
171  // result in the vector P.
172 
173  for (Int_t i=0; i<n; P[i] = p1[i] - p2[i], i++);
174 
175  return;
176 }
177 
178 
179 Double_t StFormulary::Abs(const Double_t *p, Int_t dim)
180 {
181  // Calculates the length of a vector in ordinary euclidian geometry.
182 
183  return TMath::Sqrt(Square(p, dim));
184 }