CMS 3D CMS Logo

ALIUtils.cc
Go to the documentation of this file.
1 // COCOA class implementation file
2 //Id: ALIUtils.cc
3 //CAT: ALIUtils
4 //
5 // History: v1.0
6 // Pedro Arce
7 
10 
11 #include <cmath>
12 #include <cstdlib>
13 #include <iomanip>
14 
25 time_t ALIUtils::_time_now;
26 ALIdouble ALIUtils::deg = 0.017453293;
29 
30 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
31 //@@ CHECKS THAT EVERY CHARACTER IN A STRING IS NUMBER, ELSE GIVES AN ERROR
32 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
34  int isnum = 1;
35  int numE = 0;
36  for (ALIuint ii = 0; ii < str.length(); ii++) {
37  if (!isdigit(str[ii]) && str[ii] != '.' && str[ii] != '-' && str[ii] != '+') {
38  //--- check for E(xponential)
39  if (str[ii] == 'E' || str[ii] == 'e') {
40  if (numE != 0 || ii == str.length() - 1) {
41  isnum = 0;
42  break;
43  }
44  numE++;
45  } else {
46  isnum = 0;
47  break;
48  }
49  }
50  }
51 
52  return isnum;
53 }
54 
55 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
56 //@@ Dump a Hep3DVector with the chosen precision
57 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
58 void ALIUtils::dump3v(const CLHEP::Hep3Vector& vec, const std::string& msg) {
59  // double phicyl = atan( vec.y()/vec.x() );
60  std::cout << msg << std::setprecision(8) << vec;
61  std::cout << std::endl;
62  // std::cout << " " << vec.theta()/deg << " " << vec.phi()/deg << " " << vec.perp() << " " << phicyl/deg << std::endl;
63  // setw(10);
64  // std::cout << msg << " x=" << std::setprecision(8) << vec.x() << " y=" << setprecision(8) <<vec.y() << " z=" << std::setprecision(8) << vec.z() << std::endl;
65  // std::setprecision(8);
66 }
67 
68 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
69 //@@
70 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
71 void ALIUtils::dumprm(const CLHEP::HepRotation& rm, const std::string& msg, std::ostream& out) {
72  out << msg << " xx=" << rm.xx() << " xy=" << rm.xy() << " xz=" << rm.xz() << std::endl;
73  out << msg << " yx=" << rm.yx() << " yy=" << rm.yy() << " yz=" << rm.yz() << std::endl;
74  out << msg << " zx=" << rm.zx() << " zy=" << rm.zy() << " zz=" << rm.zz() << std::endl;
75 }
76 
77 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
78 //@@ Set the dimension factor to convert input length values and errors to
79 //@@ the dimension of errors
80 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
82  //---------------------------------------- if it doesn exist, GlobalOptions is 0
83  //---------- Calculate factors to convert to meters
85  ALIint ad = ALIint(gomgr->getGlobalOption("length_value_dimension"));
86 
88  ad = ALIint(gomgr->GlobalOptions()[ALIstring("length_error_dimension")]);
90 
91  //---------- Change factor to convert to error dimensions
92  // _LengthValueDimensionFactor /= _LengthSigmaDimensionFactor;
93  //_LengthSigmaDimensionFactor = 1;
94 
95  if (ALIUtils::debug >= 6)
96  std::cout << _LengthValueDimensionFactor << " Set Length DimensionFactors " << _LengthSigmaDimensionFactor
97  << std::endl;
98 }
99 
100 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
101 //@@ Set the dimension factor to convert input angle values and errors to
102 //@@ the dimension of errors
103 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
105  //--------------------- if it doesn exist, GlobalOptions is 0
106  //---------- Calculate factors to convert to radians
108  ALIint ad = ALIint(gomgr->GlobalOptions()[ALIstring("angle_value_dimension")]);
110 
111  ad = ALIint(gomgr->GlobalOptions()[ALIstring("angle_error_dimension")]);
113 
114  //---------- Change factor to convert to error dimensions
115  // _AngleValueDimensionFactor /= _AngleSigmaDimensionFactor;
116  //_AngleSigmaDimensionFactor = 1;
117 
118  if (ALIUtils::debug >= 6)
119  std::cout << _AngleValueDimensionFactor << "Set Angle DimensionFactors" << _AngleSigmaDimensionFactor << std::endl;
120 }
121 
122 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
123 //@@ Set the dimension factor to convert input length values and errors to
124 //@@ the dimension of errors
125 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
127  //---------------------------------------- if it doesn exist, GlobalOptions is 0
128  //---------- Calculate factors to convert to meters
130  ALIint ad = ALIint(gomgr->GlobalOptions()[ALIstring("output_length_value_dimension")]);
131  if (ad == 0)
132  ad = ALIint(gomgr->GlobalOptions()[ALIstring("length_value_dimension")]);
134 
135  ad = ALIint(gomgr->GlobalOptions()[ALIstring("output_length_error_dimension")]);
136  if (ad == 0)
137  ad = ALIint(gomgr->GlobalOptions()[ALIstring("length_error_dimension")]);
139 
140  //---------- Change factor to convert to error dimensions
141  // _LengthValueDimensionFactor /= _LengthSigmaDimensionFactor;
142  //_LengthSigmaDimensionFactor = 1;
143 
144  if (ALIUtils::debug >= 6)
145  std::cout << _OutputLengthValueDimensionFactor << "Output Length Dimension Factors"
146  << _OutputLengthSigmaDimensionFactor << std::endl;
147 }
148 
149 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
150 //@@ Set the dimension factor to convert input angle values and errors to
151 //@@ the dimension of errors
152 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
154  //--------------------- if it doesn exist, GlobalOptions is 0
155  //---------- Calculate factors to convert to radians
157  ALIint ad = ALIint(gomgr->GlobalOptions()[ALIstring("output_angle_value_dimension")]);
158  if (ad == 0)
159  ad = ALIint(gomgr->GlobalOptions()[ALIstring("angle_value_dimension")]);
161 
162  ad = ALIint(gomgr->GlobalOptions()[ALIstring("output_angle_error_dimension")]);
163  if (ad == 0)
164  ad = ALIint(gomgr->GlobalOptions()[ALIstring("angle_error_dimension")]);
166 
167  //---------- Change factor to convert to error dimensions
168  // _AngleValueDimensionFactor /= _AngleSigmaDimensionFactor;
169  //_AngleSigmaDimensionFactor = 1;
170 
171  if (ALIUtils::debug >= 9)
172  std::cout << _OutputAngleValueDimensionFactor << "Output Angle Dimension Factors"
173  << _OutputAngleSigmaDimensionFactor << std::endl;
174 }
175 
176 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
177 //@@ Calculate dimension factor to convert any length values and errors to meters
178 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
180  ALIdouble valsig = 1.;
181  ALIstring internalDim = "m";
182  if (internalDim == "m") {
183  if (dimstr == "m") {
184  valsig = 1.;
185  } else if (dimstr == "mm") {
186  valsig = 1.E-3;
187  } else if (dimstr == "mum") {
188  valsig = 1.E-6;
189  } else if (dimstr == "cm") {
190  valsig = 1.E-2;
191  } else {
192  std::cerr << "!!! UNKNOWN DIMENSION SCALING " << dimstr << std::endl
193  << "VALUE MUST BE BETWEEN 0 AND 3 " << std::endl;
194  exit(1);
195  }
196  } else if (internalDim == "mm") {
197  if (dimstr == "m") {
198  valsig = 1.E3;
199  } else if (dimstr == "mm") {
200  valsig = 1.;
201  } else if (dimstr == "mum") {
202  valsig = 1.E-3;
203  } else if (dimstr == "cm") {
204  valsig = 1.E+1;
205  } else {
206  std::cerr << "!!! UNKNOWN DIMENSION SCALING: " << dimstr << std::endl
207  << "VALUE MUST BE A LENGTH DIMENSION " << std::endl;
208  exit(1);
209  }
210  }
211 
212  return valsig;
213 }
214 
215 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
216 //@@ Calculate dimension factor to convert any angle values and errors to radians
217 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
219  ALIdouble valsig;
220  if (dimstr == "rad") {
221  valsig = 1.;
222  } else if (dimstr == "mrad") {
223  valsig = 1.E-3;
224  } else if (dimstr == "murad") {
225  valsig = 1.E-6;
226  } else if (dimstr == "deg") {
227  valsig = M_PI / 180.;
228  } else if (dimstr == "grad") {
229  valsig = M_PI / 200.;
230  } else {
231  std::cerr << "!!! UNKNOWN DIMENSION SCALING: " << dimstr << std::endl
232  << "VALUE MUST BE AN ANGLE DIMENSION " << std::endl;
233  exit(1);
234  }
235 
236  return valsig;
237 }
238 
239 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
240 //@@ Calculate dimension factor to convert any length values and errors to meters
241 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
243  ALIdouble valsig;
244  switch (ad) {
245  case 0: //----- metres
247  break;
248  case 1: //----- milimetres
250  break;
251  case 2: //----- micrometres
253  break;
254  case 3: //----- centimetres
256  break;
257  default:
258  std::cerr << "!!! UNKNOWN DIMENSION SCALING " << ad << std::endl << "VALUE MUST BE BETWEEN 0 AND 3 " << std::endl;
259  exit(1);
260  }
261 
262  // use microradinas instead of radians
263  //- valsig *= 1000000.;
264 
265  return valsig;
266 }
267 
268 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
269 //@@ Calculate dimension factor to convert any angle values and errors to radians
270 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
272  ALIdouble valsig;
273  switch (ad) {
274  case 0: //----- radians
276  break;
277  case 1: //----- miliradians
279  break;
280  case 2: //----- microradians
281  valsig = CalculateAngleDimensionFactorFromString("murad");
282  break;
283  case 3: //----- degrees
285  break;
286  case 4: //----- grads
288  break;
289  default:
290  std::cerr << "!!! UNKNOWN DIMENSION SCALING " << ad << std::endl << "VALUE MUST BE BETWEEN 0 AND 3 " << std::endl;
291  exit(1);
292  }
293 
294  // use microradinas instead of radians
295  //- valsig *= 1000000.;
296 
297  return valsig;
298 }
299 
300 /*template<class T>
301  ALIuint FindItemInVector( const T* item, const std::vector<T*> std::vector )
302 {
303 
304 }
305 */
306 
307 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 void ALIUtils::dumpDimensions(std::ofstream& fout) {
309  fout << "DIMENSIONS: lengths = ";
310  ALIstring internalDim = "m";
312  fout << "m";
313  } else if (_OutputLengthValueDimensionFactor == 1.E-3) {
314  fout << "mm";
315  } else if (_OutputLengthValueDimensionFactor == 1.E-6) {
316  fout << "mum";
317  } else if (_OutputLengthValueDimensionFactor == 1.E-2) {
318  fout << "cm";
319  } else {
320  std::cerr << " !! unknown OutputLengthValueDimensionFactor " << _OutputLengthValueDimensionFactor << std::endl;
321  exit(1);
322  }
323 
324  fout << " +- ";
326  fout << "m";
327  } else if (_OutputLengthSigmaDimensionFactor == 1.E-3) {
328  fout << "mm";
329  } else if (_OutputLengthSigmaDimensionFactor == 1.E-6) {
330  fout << "mum";
331  } else if (_OutputLengthSigmaDimensionFactor == 1.E-2) {
332  fout << "cm";
333  } else {
334  std::cerr << " !! unknown OutputLengthSigmaDimensionFactor " << _OutputLengthSigmaDimensionFactor << std::endl;
335  exit(1);
336  }
337 
338  fout << " angles = ";
340  fout << "rad";
341  } else if (_OutputAngleValueDimensionFactor == 1.E-3) {
342  fout << "mrad";
343  } else if (_OutputAngleValueDimensionFactor == 1.E-6) {
344  fout << "murad";
345  } else if (_OutputAngleValueDimensionFactor == M_PI / 180.) {
346  fout << "deg";
347  } else if (_OutputAngleValueDimensionFactor == M_PI / 200.) {
348  fout << "grad";
349  } else {
350  std::cerr << " !! unknown OutputAngleValueDimensionFactor " << _OutputAngleValueDimensionFactor << std::endl;
351  exit(1);
352  }
353 
354  fout << " +- ";
356  fout << "rad";
357  } else if (_OutputAngleSigmaDimensionFactor == 1.E-3) {
358  fout << "mrad";
359  } else if (_OutputAngleSigmaDimensionFactor == 1.E-6) {
360  fout << "murad";
361  } else if (_OutputAngleSigmaDimensionFactor == M_PI / 180.) {
362  fout << "deg";
363  } else if (_OutputAngleSigmaDimensionFactor == M_PI / 200.) {
364  fout << "grad";
365  } else {
366  std::cerr << " !! unknown OutputAngleSigmaDimensionFactor " << _OutputAngleSigmaDimensionFactor << std::endl;
367  exit(1);
368  }
369  fout << std::endl;
370 }
371 
372 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374  //----------- first check that it is a number
375  if (!IsNumber(str)) {
376  std::cerr << "!!!! EXITING: trying to get the float from a string that is not a number " << str << std::endl;
377  exit(1);
378  }
379 
380  return atof(str.c_str());
381 }
382 
383 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
385  //----------- first check that it is an integer
386  if (!IsNumber(str)) {
387  //----- Check that it is a number
388  std::cerr << "!!!! EXITING: trying to get the integer from a string that is not a number " << str << std::endl;
389  exit(1);
390  } else {
391  //----- Check that it is not a float, no decimal or E-n
392  bool isFloat = false;
393  int ch = str.find('.');
394  ALIuint ii = 0;
395  if (ch != -1) {
396  for (ii = ch + 1; ii < str.size(); ii++) {
397  if (str[ii] != '0')
398  isFloat = true;
399  }
400  }
401 
402  ch = str.find('E');
403  if (ch != -1)
404  ch = str.find('e');
405  if (ch != -1) {
406  if (str[ch + 1] == '-')
407  isFloat = true;
408  }
409 
410  if (isFloat) {
411  std::cerr << "!!!! EXITING: trying to get the integer from a string that is a float: " << str << std::endl;
412  std::cerr << ii << " ii " << ch << std::endl;
413  exit(1);
414  }
415  }
416  return int(atof(str.c_str()));
417 }
418 
419 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421  bool val;
422 
423  //t str = upper( str );
424  //----------- first check that it is a not number
425  if (str == "ON" || str == "TRUE") {
426  val = true;
427  } else if (str == "OFF" || str == "FALSE") {
428  val = false;
429  } else {
430  std::cerr << "!!!! EXITING: trying to get the float from a string that is not 'ON'/'OFF'/'TRUE'/'FALSE' " << str
431  << std::endl;
432  exit(1);
433  }
434 
435  return val;
436 }
437 
438 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440  //---------- Take out leading and trailing '"'
441  if (str.find('"') != 0 || str.rfind('"') != str.length() - 1) {
442  std::cerr << "!!!EXITING trying to substract quotes from a word that has no quotes " << str << std::endl;
443  exit(1);
444  }
445 
446  // str = str.strip(ALIstring::both, '\"');
447  //---------- Take out leading and trallling '"'
448  ALIstring strt = str.substr(1, str.size() - 2);
449 
450  //- std::cout << " subquotes " << str << std::endl;
451  //---------- Look for leading spaces
452  while (strt[0] == ' ') {
453  strt = strt.substr(1, strt.size() - 1);
454  }
455 
456  //---------- Look for trailing spaces
457  while (strt[strt.size() - 1] == ' ') {
458  strt = strt.substr(0, strt.size() - 1);
459  }
460 
461  return strt;
462 }
463 
464 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465 void ALIUtils::dumpVS(const std::vector<ALIstring>& wl, const std::string& msg, std::ostream& outs) {
466  outs << msg << std::endl;
467  ALIuint siz = wl.size();
468  for (ALIuint ii = 0; ii < siz; ii++) {
469  outs << wl[ii] << " ";
470  /* ostream_iterator<ALIstring> os(outs," ");
471  copy(wl.begin(), wl.end(), os);*/
472  }
473  outs << std::endl;
474 }
475 
476 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
479  if (dimType == "Length") {
480  if (dim == "mm") {
481  value = 1.E-3;
482  } else if (dim == "cm") {
483  value = 1.E-2;
484  } else if (dim == "m") {
485  value = 1.;
486  } else if (dim == "mum") {
487  value = 1.E-6;
488  } else if (dim == "dm") {
489  value = 1.E-1;
490  } else if (dim == "nm") {
491  value = 1.E-9;
492  } else {
493  std::cerr << "!!!!FATAL ERROR: ALIUtils::GetDimensionValue. " << dim
494  << " is a dimension not supported for dimension type " << dimType << std::endl;
495  abort();
496  }
497  } else if (dimType == "Angle") {
498  if (dim == "rad") {
499  value = 1.;
500  } else if (dim == "mrad") {
501  value = 1.E-3;
502  } else if (dim == "murad") {
503  value = 1.E-6;
504  } else if (dim == "deg") {
505  value = M_PI / 180.;
506  } else if (dim == "grad") {
507  value = M_PI / 200.;
508  } else {
509  std::cerr << "!!!!FATAL ERROR: ALIUtils::GetDimensionValue. " << dim
510  << " is a dimension not supported for dimension type " << dimType << std::endl;
511  abort();
512  }
513  } else {
514  std::cerr << "!!!!FATAL ERROR: ALIUtils::GetDimensionValue. " << dimType << " is a dimension type not supported "
515  << std::endl;
516  abort();
517  }
518 
519  return value;
520 }
521 
522 /*
523 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
524 std::ostream& operator << (std::ostream& os, const CLHEP::HepRotation& rm)
525 {
526 
527  return os << " xx=" << rm.xx() << " xy=" << rm.xy() << " xz=" << rm.xz() << std::endl
528  << " yx=" << rm.yx() << " yy=" << rm.yy() << " yz=" << rm.yz() << std::endl
529  << " zx=" << rm.zx() << " zy=" << rm.zy() << " zz=" << rm.zz() << std::endl;
530 
531 }
532 */
533 
534 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
535 std::string ALIUtils::changeName(const std::string& oldName, const std::string& subsstr1, const std::string& subsstr2) {
537  int il = oldName.find(subsstr1);
538  // std::cout << " il " << il << " oldname " << oldName << " " << subsstr1 << std::endl;
539  while (il >= 0) {
540  newName = newName.substr(0, il) + subsstr2 + newName.substr(il + subsstr1.length(), newName.length());
541  // std::cout << " dnewName " << newName << " " << newName.substr( 0, il ) << " " << subsstr2 << " " << newName.substr( il+subsstr1.length(), newName.length() ) << std::endl;
542  il = oldName.find(subsstr1, il + 1);
543  }
544 
545  return newName;
546 }
547 
548 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
549 std::vector<double> ALIUtils::getRotationAnglesFromMatrix(const CLHEP::HepRotation& rmLocal,
550  double origAngleX,
551  double origAngleY,
552  double origAngleZ) {
553  double pii = acos(0.) * 2;
554  std::vector<double> newang(3);
555  double angleX = origAngleX;
556  double angleY = origAngleY;
557  double angleZ = origAngleZ;
558 
559  if (ALIUtils::debug >= 4) {
560  std::cout << " angles as value entries: X= " << angleX << " Y= " << angleY << " Z " << angleZ << std::endl;
561  }
562 
563  //- std::cout << name () << " vdbf " << angleX << " " << angleY << " " << angleZ << std::endl;
564  double rotzx = approxTo0(rmLocal.zx());
565  double rotzy = approxTo0(rmLocal.zy());
566  double rotzz = approxTo0(rmLocal.zz());
567  double rotyx = approxTo0(rmLocal.yx());
568  double rotxx = approxTo0(rmLocal.xx());
569  if (rotzy == 0. && rotzz == 0.) {
570  //check that entry is z angle
571  newang[0] = angleX;
572  //beware of aa <==> pii - aa
573  if (eq2ang(rmLocal.zx(), -1.)) {
574  double aa = asin(rmLocal.xy());
575  if (diff2pi(angleZ, -aa + newang[0]) < diff2pi(angleZ, -pii + aa + newang[0])) {
576  newang[2] = -aa + newang[0];
577  if (ALIUtils::debug >= 5)
578  std::cout << " newang[0] = -aa + newang[0] " << std::endl;
579  } else {
580  newang[2] = -pii + aa + newang[0];
581  if (ALIUtils::debug >= 5)
582  std::cout << " newang[0] = -pii + aa + newang[0] " << newang[0] << " " << aa << " " << newang[2] << std::endl;
583  }
584  } else {
585  double aa = asin(-rmLocal.xy());
586  if (diff2pi(angleZ, aa - newang[0]) < diff2pi(angleZ, pii - aa - newang[0])) {
587  newang[2] = aa - newang[0];
588  if (ALIUtils::debug >= 5)
589  std::cout << " newang[0] = aa - newang[2] " << std::endl;
590  } else {
591  newang[2] = pii - aa - newang[0];
592  if (ALIUtils::debug >= 5)
593  std::cout << " newang[0] = pii - aa - newang[2] " << newang[0] << " " << aa << " " << newang[2] << std::endl;
594  }
595  }
596  } else {
597  newang[0] = atan(rotzy / rotzz);
598  newang[2] = atan(rotyx / rotxx);
599  }
600  if (rotzx < -1.) {
601  //- std::cerr << " rotzx too small " << rotzx << " = " << rmLocal.zx() << " " << rotzx-rmLocal.zx() << std::endl;
602  rotzx = -1.;
603  } else if (rotzx > 1.) {
604  //- std::cerr << " rotzx too big " << rotzx << " = " << rmLocal.zx() << " " << rotzx-rmLocal.zx() << std::endl;
605  rotzx = 1.;
606  }
607  newang[1] = -asin(rotzx);
608  if (ALIUtils::debug >= 5)
609  std::cout << "First calculation of angles: " << std::endl
610  << " newang[0] " << newang[0] << " " << rotzy << " " << rotzz << std::endl
611  << " newang[1] " << newang[1] << " " << rotzx << std::endl
612  << " newang[2] " << newang[2] << " " << rotyx << " " << rotxx << std::endl;
613 
614  // newang[2] = acos( rmLocal.xx() / cos( newang[1] ) );
615  //----- CHECK if the angles are OK (there are several symmetries)
616  //--- Check if the predictions with the angles obtained match the values of the rotation matrix (they may differ for exampole by a sign or more in complicated formulas)
617  double rotnewxx = cos(newang[1]) * cos(newang[2]);
618  double rotnewzz = cos(newang[0]) * cos(newang[1]);
619  double rotnewxy = sin(newang[0]) * sin(newang[1]) * cos(newang[2]) - cos(newang[0]) * sin(newang[2]);
620  double rotnewxz = cos(newang[0]) * sin(newang[1]) * cos(newang[2]) + sin(newang[0]) * sin(newang[2]);
621  double rotnewyy = sin(newang[0]) * sin(newang[1]) * sin(newang[2]) + cos(newang[0]) * cos(newang[2]);
622  double rotnewyz = cos(newang[0]) * sin(newang[1]) * sin(newang[2]) - sin(newang[0]) * cos(newang[2]);
623 
624  bool eqxx = eq2ang(rotnewxx, rmLocal.xx());
625  bool eqzz = eq2ang(rotnewzz, rmLocal.zz());
626  bool eqxy = eq2ang(rotnewxy, rmLocal.xy());
627  bool eqxz = eq2ang(rotnewxz, rmLocal.xz());
628  bool eqyy = eq2ang(rotnewyy, rmLocal.yy());
629  bool eqyz = eq2ang(rotnewyz, rmLocal.yz());
630 
631  //--- Check if one of the tree angles should be changed
632  if (ALIUtils::debug >= 5) {
633  std::cout << " pred rm.xx " << rotnewxx << " =? " << rmLocal.xx() << " pred rm.zz " << rotnewzz << " =? "
634  << rmLocal.zz() << std::endl;
635  std::cout << " eqxx " << eqxx << " eqzz " << eqzz << std::endl;
636  //- std::cout << " rotnewxx " << rotnewxx << " = " << rmLocal.xx() << " " << fabs( rotnewxx - rmLocal.xx() ) << " " <<(fabs( rotnewxx - rmLocal.xx() ) < 0.0001) << std::endl;
637  }
638 
639  if (eqxx & !eqzz) {
640  newang[0] = pii + newang[0];
641  if (ALIUtils::debug >= 5)
642  std::cout << " change newang[0] " << newang[0] << std::endl;
643  } else if (!eqxx & !eqzz) {
644  newang[1] = pii - newang[1];
645  if (ALIUtils::debug >= 5)
646  std::cout << " change newang[1] " << newang[1] << std::endl;
647  } else if (!eqxx & eqzz) {
648  newang[2] = pii + newang[2];
649  if (ALIUtils::debug >= 5)
650  std::cout << " change newang[2] " << newang[2] << std::endl;
651  }
652 
653  //--- Check if the 3 angles should be changed (previous check is invariant to the 3 changing)
654  if (ALIUtils::debug >= 5) {
655  std::cout << " pred rm.xy " << rotnewxy << " =? " << rmLocal.xy() << " pred rm.xz " << rotnewxz << " =? "
656  << rmLocal.xz() << " pred rm.yy " << rotnewyy << " =? " << rmLocal.yy() << " pred rm.yz " << rotnewyz
657  << " =? " << rmLocal.yz() << std::endl;
658  std::cout << " eqxy " << eqxy << " eqxz " << eqxz << " eqyy " << eqyy << " eqyz " << eqyz << std::endl;
659  }
660 
661  if (!eqxy || !eqxz || !eqyy || !eqyz) {
662  // check also cases where one of the above 'eq' is OK because it is = 0
663  if (ALIUtils::debug >= 5)
664  std::cout << " change the 3 newang " << std::endl;
665  newang[0] = addPii(newang[0]);
666  newang[1] = pii - newang[1];
667  newang[2] = addPii(newang[2]);
668  double rotnewxy = -sin(newang[0]) * sin(newang[1]) * cos(newang[2]) - cos(newang[0]) * sin(newang[2]);
669  double rotnewxz = -cos(newang[0]) * sin(newang[1]) * cos(newang[2]) - sin(newang[0]) * sin(newang[2]);
670  if (ALIUtils::debug >= 5)
671  std::cout << " rotnewxy " << rotnewxy << " = " << rmLocal.xy() << " rotnewxz " << rotnewxz << " = "
672  << rmLocal.xz() << std::endl;
673  }
674  if (diff2pi(angleX, newang[0]) + diff2pi(angleY, newang[1]) + diff2pi(angleZ, newang[2]) >
675  diff2pi(angleX, pii + newang[0]) + diff2pi(angleY, pii - newang[1]) + diff2pi(angleZ, pii + newang[2])) {
676  // check also cases where one of the above 'eq' is OK because it is = 0
677  if (ALIUtils::debug >= 5)
678  std::cout << " change the 3 newang " << std::endl;
679  newang[0] = addPii(newang[0]);
680  newang[1] = pii - newang[1];
681  newang[2] = addPii(newang[2]);
682  double rotnewxy = -sin(newang[0]) * sin(newang[1]) * cos(newang[2]) - cos(newang[0]) * sin(newang[2]);
683  double rotnewxz = -cos(newang[0]) * sin(newang[1]) * cos(newang[2]) - sin(newang[0]) * sin(newang[2]);
684  if (ALIUtils::debug >= 5)
685  std::cout << " rotnewxy " << rotnewxy << " = " << rmLocal.xy() << " rotnewxz " << rotnewxz << " = "
686  << rmLocal.xz() << std::endl;
687  }
688 
689  for (int ii = 0; ii < 3; ii++) {
690  newang[ii] = approxTo0(newang[ii]);
691  }
692  // double rotnewyx = cos( newang[1] ) * sin( newang[2] );
693 
694  if (checkMatrixEquations(newang[0], newang[1], newang[2], rmLocal) != 0) {
695  std::cerr << " wrong rotation matrix " << newang[0] << " " << newang[1] << " " << newang[2] << std::endl;
696  ALIUtils::dumprm(rmLocal, " matrix is ");
697  }
698  if (ALIUtils::debug >= 5) {
699  std::cout << "Final angles: newang[0] " << newang[0] << " newang[1] " << newang[1] << " newang[2] " << newang[2]
700  << std::endl;
701  CLHEP::HepRotation rot;
702  rot.rotateX(newang[0]);
703  ALIUtils::dumprm(rot, " new rot after X ");
704  rot.rotateY(newang[1]);
705  ALIUtils::dumprm(rot, " new rot after Y ");
706  rot.rotateZ(newang[2]);
707  ALIUtils::dumprm(rot, " new rot ");
708  ALIUtils::dumprm(rmLocal, " rmLocal ");
709  //- ALIUtils::dumprm( theRmGlobOriginal, " theRmGlobOriginal " );
710  }
711 
712  //- std::cout << " before return newang[0] " << newang[0] << " newang[1] " << newang[1] << " newang[2] " << newang[2] << std::endl;
713  return newang;
714 }
715 
716 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
717 double ALIUtils::diff2pi(double ang1, double ang2) {
718  double pii = acos(0.) * 2;
719  double diff = fabs(ang1 - ang2);
720  diff = diff - int(diff / 2. / pii) * 2 * pii;
721  return diff;
722 }
723 
724 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
725 bool ALIUtils::eq2ang(double ang1, double ang2) {
726  bool beq = true;
727 
728  double pii = acos(0.) * 2;
729  double diff = diff2pi(ang1, ang2);
730  if (diff > 0.00001) {
731  if (fabs(diff - 2 * pii) > 0.00001) {
732  //- std::cout << " diff " << diff << " " << ang1 << " " << ang2 << std::endl;
733  beq = false;
734  }
735  } else {
736  beq = true;
737  }
738 
739  return beq;
740 }
741 
742 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
743 double ALIUtils::approxTo0(double val) {
744  double precision = 1.e-9;
745  if (fabs(val) < precision)
746  val = 0;
747  return val;
748 }
749 
750 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
751 double ALIUtils::addPii(double val) {
752  if (val < M_PI) {
753  val += M_PI;
754  } else {
755  val -= M_PI;
756  }
757 
758  return val;
759 }
760 
761 //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
762 int ALIUtils::checkMatrixEquations(double angleX, double angleY, double angleZ, const CLHEP::HepRotation& rot) {
763  double sx = sin(angleX);
764  double cx = cos(angleX);
765  double sy = sin(angleY);
766  double cy = cos(angleY);
767  double sz = sin(angleZ);
768  double cz = cos(angleZ);
769 
770  double rotxx = cy * cz;
771  double rotxy = sx * sy * cz - cx * sz;
772  double rotxz = cx * sy * cz + sx * sz;
773  double rotyx = cy * sz;
774  double rotyy = sx * sy * sz + cx * cz;
775  double rotyz = cx * sy * sz - sx * cz;
776  double rotzx = -sy;
777  double rotzy = sx * cy;
778  double rotzz = cx * cy;
779 
780  int matrixElemBad = 0;
781  if (!eq2ang(rot.xx(), rotxx) || !eq2ang(rot.xy(), rotxy) || !eq2ang(rot.xz(), rotxz) || !eq2ang(rot.yx(), rotyx) ||
782  !eq2ang(rot.yy(), rotyy) || !eq2ang(rot.yz(), rotyz) || !eq2ang(rot.zx(), rotzx) || !eq2ang(rot.zy(), rotzy) ||
783  !eq2ang(rot.zz(), rotzz)) {
784  matrixElemBad++;
785  }
786 
787  return matrixElemBad;
788 }
def rm(path, rec=False)
Definition: eostools.py:363
static time_t _time_now
Definition: ALIUtils.h:111
long double ALIdouble
Definition: CocoaGlobals.h:11
static ALIdouble maximum_deviation_derivative
Definition: ALIUtils.h:115
static ALIdouble _OutputLengthValueDimensionFactor
Definition: ALIUtils.h:107
static double approxTo0(double val)
Definition: ALIUtils.cc:743
static double diff2pi(double ang1, double ang2)
Definition: ALIUtils.cc:717
static bool eq2ang(double ang1, double ang2)
Definition: ALIUtils.cc:725
static std::vector< double > getRotationAnglesFromMatrix(const CLHEP::HepRotation &rmLocal, double origAngleX, double origAngleY, double origAngleZ)
Definition: ALIUtils.cc:549
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
static void dumprm(const CLHEP::HepRotation &rm, const std::string &msg, std::ostream &out=std::cout)
Definition: ALIUtils.cc:71
static void SetOutputLengthDimensionFactors()
Definition: ALIUtils.cc:126
int ALIint
Definition: CocoaGlobals.h:15
static ALIdouble _AngleValueDimensionFactor
Definition: ALIUtils.h:105
static ALIint debug
Definition: ALIUtils.h:34
string newName
Definition: mps_merge.py:86
static double addPii(double val)
Definition: ALIUtils.cc:751
static GlobalOptionMgr * getInstance()
static ALIbool firstTime
Definition: ALIUtils.h:113
static double getFloat(const ALIstring &str)
Convert a string to an float, checking that it is really a number.
Definition: ALIUtils.cc:373
bool ALIbool
Definition: CocoaGlobals.h:19
static bool getBool(const ALIstring &str)
Convert a bool to an integer, checking that it is really a bool.
Definition: ALIUtils.cc:420
static ALIdouble _OutputAngleSigmaDimensionFactor
Definition: ALIUtils.h:110
static ALIdouble _AngleSigmaDimensionFactor
Definition: ALIUtils.h:106
static ALIdouble CalculateAngleDimensionFactorFromString(ALIstring dimstr)
Definition: ALIUtils.cc:218
static ALIdouble CalculateLengthDimensionFactorFromInt(ALIint ad)
Definition: ALIUtils.cc:242
static int IsNumber(const ALIstring &str)
Definition: ALIUtils.cc:33
Cos< T >::type cos(const T &t)
Definition: Cos.h:22
static int checkMatrixEquations(double angleX, double angleY, double angleZ, const CLHEP::HepRotation &rot)
Definition: ALIUtils.cc:762
Definition: value.py:1
static ALIdouble CalculateLengthDimensionFactorFromString(ALIstring dimstr)
Definition: ALIUtils.cc:179
static void SetAngleDimensionFactors()
Definition: ALIUtils.cc:104
static void SetOutputAngleDimensionFactors()
Definition: ALIUtils.cc:153
ii
Definition: cuy.py:589
#define M_PI
static ALIdouble deg
Definition: ALIUtils.h:35
static void dump3v(const CLHEP::Hep3Vector &vec, const std::string &msg)
Definition: ALIUtils.cc:58
static void SetLengthDimensionFactors()
Definition: ALIUtils.cc:81
static ALIint report
Definition: ALIUtils.h:33
static std::string changeName(const std::string &oldName, const std::string &subsstr1, const std::string &subsstr2)
Definition: ALIUtils.cc:535
static ALIdouble _LengthSigmaDimensionFactor
Definition: ALIUtils.h:104
tuple msg
Definition: mps_check.py:285
static void dumpVS(const std::vector< ALIstring > &wl, const std::string &msg, std::ostream &outs=std::cout)
dumps a vector of strings with a message to outs
Definition: ALIUtils.cc:465
static ALIdouble getDimensionValue(const ALIstring &dim, const ALIstring &dimType)
Definition: ALIUtils.cc:477
std::map< ALIstring, ALIdouble, std::less< ALIstring > > & GlobalOptions()
std::string ALIstring
Definition: CocoaGlobals.h:9
static ALIdouble _OutputAngleValueDimensionFactor
Definition: ALIUtils.h:109
static int getInt(const ALIstring &str)
Convert a string to an integer, checking that it is really an integer.
Definition: ALIUtils.cc:384
ALIdouble getGlobalOption(const ALIstring &sstr)
#define str(s)
static ALIdouble _OutputLengthSigmaDimensionFactor
Definition: ALIUtils.h:108
static void dumpDimensions(std::ofstream &fout)
Definition: ALIUtils.cc:308
static ALIstring subQuotes(const ALIstring &str)
Definition: ALIUtils.cc:439
static ALIdouble _LengthValueDimensionFactor
Definition: ALIUtils.h:103
unsigned int ALIuint
Definition: CocoaGlobals.h:17
def exit(msg="")
static ALIdouble CalculateAngleDimensionFactorFromInt(ALIint ad)
Definition: ALIUtils.cc:271