All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups
MathOperator.cc
Go to the documentation of this file.
1 #include "MathOperator.hh"
2 using std::vector;
3 namespace TTbarAnalysis
4 {
5  bool MathOperator::approximatelyEqual(const double * start1, const double * start2, double precision)
6  {
7  float distance = getDistance(start1, start2);
8  return distance < precision;
9  }
10  bool MathOperator::approximatelyEqual(const double * start1, const float * start2, double precision)
11  {
12  //float distanceCut = 5.0;
13  double end[3];
14  for (int i = 0; i < 3; i++)
15  {
16  end[i] = start2[i];
17  }
18  float distance = getDistance(start1, end);
19  return distance < precision;
20  }
21 
22  float MathOperator::getDistance(const double * start, const double * end)
23  {
24  vector<float> vec;
25  for (int i = 0; i < 3; i++)
26  {
27  vec.push_back(start[i] - end[i]);
28  }
29  float sqr = 0.0;
30  for (int i = 0; i < 3; i++)
31  {
32  sqr += vec[i]*vec[i];
33  }
34  return sqrt(sqr);
35  }
36  float MathOperator::getDistance(const float * start, const float * end)
37  {
38  vector<float> vec;
39  for (int i = 0; i < 3; i++)
40  {
41  vec.push_back(start[i] - end[i]);
42  }
43  float sqr = 0.0;
44  for (int i = 0; i < 3; i++)
45  {
46  sqr += vec[i]*vec[i];
47  }
48  return sqrt(sqr);
49  }
50 
51 
52  float MathOperator::getModule(const vector< int > & v)
53  {
54  float module = 0.0;
55  for (unsigned int i = 0; i < v.size(); i++)
56  {
57  module += v[i]*v[i];
58  }
59  module = sqrt(module);
60  return module;
61  }
62 
63  float MathOperator::getModule(const vector< float > & v)
64  {
65  float module = 0.0;
66  for (unsigned int i = 0; i < v.size(); i++)
67  {
68  module += v[i]*v[i];
69  }
70  module = sqrt(module);
71  return module;
72  }
73  float MathOperator::getModule(const double * vector1)
74  {
75  float module = 0.0;
76  for (int i = 0; i < 3; i++)
77  {
78  module += vector1[i]*vector1[i];
79  }
80  return sqrt(module);
81  }
82  float MathOperator::getAngle(const double * vector1, const double * vector2)
83  {
84  float module1 = getModule(vector1);
85  float module2 = getModule(vector2);
86  if (module1 < 0.00000000001 || module2 < 0.000000000001)
87  {
88  return 10e10;
89  }
90  double product = 0.0;
91  for (int i = 0; i < 3; i++)
92  {
93  product += vector1[i]*vector2[i];
94  }
95  return acos((float)product/module1/module2);
96  }
97  vector< float > MathOperator::getAngles(vector< float > & direction)
98  {
99  vector< float > result;
100  float epsilon = 0.00001;
101  float semi = 1.5708;
102  float pi = 2*semi;
103  float phi = 0.0;
104  if (direction[0] > 0.0 && direction[1] > 0.0 - epsilon)
105  {
106  phi = atan(direction[1] / direction[0]); //(direction[0] < epsilon && direction[0] > 0.0 - epsilon)?
107  }
108  if (direction[0] < 0.0 && direction[1] > 0.0)
109  {
110  phi = semi - atan(direction[1] / direction[0]) ;
111  }
112  if (direction[0] < 0.0 && direction[1] < 0.0 + epsilon)
113  {
114  phi = atan(direction[1] / direction[0]) + pi;
115  }
116  if (direction[0] > 0.0 && direction[1] < 0.0 - epsilon)
117  {
118  phi = semi - atan(direction[1] / direction[0]) + pi;
119  }
120  if (direction[1] > 0.0 && direction[0] < 0.0 + epsilon && direction[0] > 0.0 - epsilon)
121  {
122  phi = semi;
123  }
124  if (direction[1] < 0.0 && direction[0] < 0.0 + epsilon && direction[0] > 0.0 - epsilon)
125  {
126  phi = pi + semi;
127  }
128  float teta = acos(direction[2]);
129  result.push_back(phi);
130  result.push_back(teta);
131  return result;
132  }
133  vector< float > MathOperator::getDirection(vector< int > & vectorPoint1, vector< int > & vectorPoint2)
134  {
135  /*double * arr1 = MathOperator::castIntToDouble(&vectorPoint1[0]);
136  double * arr2 = MathOperator::castIntToDouble(&vectorPoint2[0]);
137  vector< float > result = MathOperator::getDirection(arr1,arr2);
138  delete [] arr1;
139  delete [] arr2;
140  return result;*/
141  vector< float > vector1;
142  for (int i = 0; i < 3; i++)
143  {
144  vector1.push_back((float)(vectorPoint1[i] - vectorPoint2[i]));
145  }
146  float module = getModule(vector1);
147  for (int i = 0; i < 3; i++)
148  {
149  vector1[i] = vector1[i]/module;
150  }
151  return vector1;
152  }
153 
154  vector< float > MathOperator::getDirection(const double * vectorPoint1, const double * vectorPoint2)
155  {
156  vector< float > vector1;
157  for (int i = 0; i < 3; i++)
158  {
159  vector1.push_back(vectorPoint1[i] - vectorPoint2[i]);
160  }
161  float module = getModule(vector1);
162  for (int i = 0; i < 3; i++)
163  {
164  vector1[i] = vector1[i]/module;
165  }
166  return vector1;
167  }
168  vector< float > MathOperator::getDirection(const double * vectorPoint)
169  {
170  float module = getModule(vectorPoint);
171  vector< float > vector1;
172  for (int i = 0; i < 3; i++)
173  {
174  vector1.push_back(vectorPoint[i] / module);
175  }
176  return vector1;
177  }
178 
179  vector< float > * MathOperator::vectorProduct(const vector< float > & v1, const vector< float > & v2)
180  {
181  vector<float> * result = new vector<float>();
182  result->push_back(v1[1]*v2[2]-v1[2]*v2[1]);
183  result->push_back(v1[2]*v2[0]-v1[0]*v2[2]);
184  result->push_back(v1[0]*v2[1]-v1[1]*v2[0]);
185  return result;
186  }
187 
188  float MathOperator::getDistanceTo( const vector< int > & vectorPoint1, const vector< float > & vector1,const vector< int > * point )
189  {
190  float result = 0.0;
191  //vector< float > vector1 = getDirection(vectorPoint1,vectorPoint2);
192  vector< float > vector2;
193  for (int i = 0; i < 3; i++)
194  {
195  vector2.push_back((float)(vectorPoint1[i] - point->at(i)));
196  }
197  vector< float > * product = vectorProduct(vector1,vector2);
198  result = getModule(*product)/getModule(vector1);
199  delete product;
200  return result;
201  }
202 
203  float MathOperator::getDistanceTo(const double * vectorPoint1, vector< float > & vector1,const double * point )
204  {
205  float result = 0.0;
206  vector< float > vector2;
207  for (int i = 0; i < 3; i++)
208  {
209  vector2.push_back(vectorPoint1[i] - point[i]);
210  }
211  vector< float > * product = vectorProduct(vector1,vector2);
212  result = getModule(*product)/getModule(vector1);
213  delete product;
214  return result;
215  }
216 
217 
218  double * MathOperator::castIntToDouble(int * array)
219  {
220  int size = (sizeof(array)/sizeof(*array));
221  if (size < 1)
222  {
223  return NULL;
224  }
225  double * arrPoint1 = new double[size];
226  for (int i = 0; i < size; i++)
227  {
228  arrPoint1[i] = array[i];
229  }
230  return arrPoint1;
231  }
232  vector< vector< int > * > * MathOperator::GetMagicNumbers()
233  {
234  vector< vector< int > * > * result = new vector< vector< int > * >();
235  for (int x = -2; x < 3; x++)
236  {
237  for (int y = -2; y < 3; y++)
238  {
239  int z = 2;
240  result->push_back(getPoint(x,y,z));
241  }
242  for (int y = -2; y < 3; y+=4)
243  {
244  int z = 1;
245  result->push_back(getPoint(x,y,z));
246  }
247  int y = -2;
248  int z = 0;
249  result->push_back(getPoint(x,y,z));
250 
251  }
252  for (int x = -2; x < 3; x+=4)
253  {
254  for (int y = -1; y < 2; y++)
255  {
256  int z = 1;
257  result->push_back(getPoint(x,y,z));
258 
259  }
260  int y = -1;
261  int z = 0;
262  result->push_back(getPoint(x,y,z));
263  }
264  result->push_back(getPoint(2,0,0));
265  /*vector<int> additional = *getPoint(3,4,6);
266  for (int i = 0; i < 3; i++)
267  {
268  for (int j = -1; j < 2; j+=2)
269  {
270  result->push_back(getPoint(additional[i],j,0));
271  result->push_back(getPoint(additional[i],0,j));
272  result->push_back(getPoint(0,additional[i],j));
273  result->push_back(getPoint(j,additional[i],0));
274  result->push_back(getPoint(j,0,additional[i]));
275  result->push_back(getPoint(0,j,additional[i]));
276  }
277  }*/
278  int z = 3;
279  for (int y = -2; y < 3; y++)
280  {
281  for (int x = -2; x < 3; x++)
282  {
283  if (x == 0 && y == 0)
284  {
285  continue;
286  }
287  result->push_back(getPoint(x,y,z));
288  }
289  }
290  z = 4;
291  for (int y = -1; y < 2; y++)
292  {
293  for (int x = -1; x < 2; x++)
294  {
295  if (x == 0 && y == 0)
296  {
297  continue;
298  }
299  result->push_back(getPoint(x,y,z));
300  }
301  }
302  return result;
303  }
304  vector< int > * MathOperator::getPoint(int x, int y, int z)
305  {
306  vector< int > * point = new vector< int >();
307  point->push_back(x);
308  point->push_back(y);
309  point->push_back(z);
310  return point;
311  }
312  float MathOperator::getPt(const double * momentum)
313  {
314  return sqrt(momentum[0] * momentum[0] + momentum[1] * momentum[1]);
315  }
316  float MathOperator::getRapidity(const double * momentum)
317  {
318  float p = getModule(momentum);
319  float pL = momentum[2];
320  float result = -1.0;
321  if (p > pL)
322  {
323  result = 0.5 * log((p+pL)/(p-pL));
324  }
325  return result;
326  }
327  double * MathOperator::getPtOnVector(const double * momentum, const float * target)
328  {
329  double * converted = toDoubleArray(target, 3);
330  /*for (int i = 0; i < 3; i++)
331  {
332  std::cout << i << ": " << converted[i];
333  }
334  std::cout << '\n';*/
335  vector< float > direction = getDirection(converted);
336  double * pt = new double[3];
337  double product = 0.0;
338  for (int i = 0; i < 3; i++)
339  {
340  product += momentum[i] * direction[i];
341  }
342  for (int i = 0; i < 3; i++)
343  {
344  pt[i] = momentum[i] - direction[i] * product;
345  }
346  //std::cout << "Pl: " << product << " |Pt|: " << getModule(pt) << '\n';
347  return pt;
348  }
349  double MathOperator::getMissingPt(vector< const double * > & vectors, const float * target)
350  {
351  double sum[3];
352  vector< double * > pts;
353  for (unsigned int i = 0; i < vectors.size(); i++)
354  {
355  pts.push_back(getPtOnVector(vectors[i], target));
356  }
357  for (int i = 0; i < 3; i++)
358  {
359  for (unsigned int j = 0; j < vectors.size(); j++)
360  {
361  sum[i] += pts[j][i];
362  }
363  }
364  return getModule(sum);
365  }
366  double * MathOperator::toDoubleArray(const float * target, int size)
367  {
368  double * array = new double[3]();
369  for (int i = 0; i < size; i++)
370  {
371  array[i] = target[i];
372  }
373  return array;
374  }
375 }
static double * castIntToDouble(int *array)
const int precision
static double * getPtOnVector(const double *momentum, const float *target)
static std::vector< float > getDirection(std::vector< int > &vectorPoint1, std::vector< int > &vectorPoint2)
const float pi
Definition: G2CD.cc:34
static std::vector< float > * vectorProduct(const std::vector< float > &v1, const std::vector< float > &v2)
static float getModule(const std::vector< int > &v)
Definition: MathOperator.cc:52
static bool approximatelyEqual(const double *start1, const double *end, double p)
Definition: MathOperator.cc:5
static float getPt(const double *momentum)
static std::vector< std::vector< int > * > * GetMagicNumbers()
static std::vector< float > getAngles(std::vector< float > &direction)
Definition: MathOperator.cc:97
static double * toDoubleArray(const float *target, int size)
static double getMissingPt(std::vector< const double * > &vectors, const float *target)
static std::vector< int > * getPoint(int x, int y, int z)
static float getDistance(const double *start, const double *end)
Definition: MathOperator.cc:22
static float getDistanceTo(const std::vector< int > &vectorPoint1, const std::vector< float > &vector1, const std::vector< int > *pointOfLine)
static float getAngle(const double *vector1, const double *vector2)
Definition: MathOperator.cc:82
static float getRapidity(const double *momentum)