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