00001
00002
00003
00004
00005
00006
00007 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
00008 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
00009 #include "Alignment/CocoaModel/interface/ALIUnitsTable.h"
00010 #include <CLHEP/Random/RandGauss.h>
00011 #include <CLHEP/Random/Random.h>
00012
00013
00014
00015 ParameterMgr* ParameterMgr::theInstance = 0;
00016
00017
00018 ParameterMgr* ParameterMgr::getInstance()
00019 {
00020 if( !theInstance ) {
00021 theInstance = new ParameterMgr;
00022 }
00023
00024 return theInstance;
00025
00026 }
00027
00028
00029
00030 ALIdouble ParameterMgr::getVal( const ALIstring& str, const ALIdouble dimensionFactor )
00031 {
00032
00033 ALIint iast = str.find('*');
00034
00035 if( iast != -1 ) {
00036 ALIstring valstr = str.substr( 0, iast );
00037 ALIstring unitstr = str.substr(iast+1, str.length() );
00038
00039
00040 if( !ALIUtils::IsNumber( valstr ) ) {
00041 std::cerr << " ParameterMgr::getVal of an ALIstring that is not a number: " << valstr << std::endl;
00042 abort();
00043 }
00044
00045
00046 return atof( valstr.c_str() ) * ALIUnitDefinition::GetValueOf(unitstr);
00047 } else {
00048
00049 if( !ALIUtils::IsNumber( str ) ) {
00050
00051 ALIdouble val;
00052 if( getParameterValue( str, val ) ) {
00053 return val;
00054 } else {
00055 std::cerr << " ParameterMgr::getVal of an string that is not a number nor a previous parameter: " << str << std::endl;
00056 abort();
00057 }
00058 }
00059
00060
00061
00062 return atof( str.c_str() ) * dimensionFactor;
00063 }
00064 }
00065
00066
00067
00068 void ParameterMgr::addParameter( const ALIstring& name, const ALIstring& valstr )
00069 {
00070 if( theParameters.find( name ) != theParameters.end() ) {
00071 if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00072 } else {
00073 theParameters[name] = getVal( valstr );
00074 }
00075
00076 }
00077
00078
00079 void ParameterMgr::setRandomSeed( const long seed )
00080 {
00081 HepRandom::setTheSeed( seed );
00082 }
00083
00084
00085
00086 void ParameterMgr::addRandomGaussParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valStdDev )
00087 {
00088 if( theParameters.find( name ) != theParameters.end() ) {
00089 if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00090 } else {
00091 ALIdouble mean = getVal( valMean );
00092 ALIdouble stddev = getVal( valStdDev );
00093 ALIdouble val = RandGauss::shoot( mean, stddev );
00094 theParameters[name] = val;
00095 if( ALIUtils::debug >= -2 ) std::cout << " addRandomGaussParameter " << name << " " << valMean << " " << valStdDev << " = " << val << std::endl;
00096 }
00097
00098 }
00099
00100
00101
00102 void ParameterMgr::addRandomFlatParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valInterval )
00103 {
00104 if( theParameters.find( name ) != theParameters.end() ) {
00105 if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00106 } else {
00107 ALIdouble mean = getVal( valMean );
00108 ALIdouble interval = getVal( valInterval );
00109 ALIdouble val = HepRandom::getTheEngine()->flat();
00110
00111 val = val * 2*interval + mean-interval;
00112 theParameters[name] = val;
00113 if( ALIUtils::debug >= 2 )std::cout << " addRandomFlatParameter " << name << " " << valMean << " " << valInterval << " = " << val << std::endl;
00114 }
00115
00116 }
00117
00118
00119
00120
00121 ALIint ParameterMgr::getParameterValue( const ALIstring& name, ALIdouble& val )
00122 {
00123
00124
00125 ALIstring namet = name;
00126 ALIint negpar = 1;
00127 if( namet[0] == '-' ) {
00128 negpar = -1;
00129 namet = namet.substr(1, namet.length() );
00130 }
00131
00132
00133 msd::iterator ite = theParameters.find( namet );
00134 if( ite == theParameters.end() ) {
00135
00136
00137
00138
00139
00140 return 0;
00141 } else {
00142 val = (*ite).second * negpar;
00143
00144 return 1;
00145 }
00146
00147 }
00148
00149