File indexing completed on 2024-04-06 11:56:04
0001
0002
0003
0004
0005
0006
0007 #include "Alignment/CocoaModel/interface/ParameterMgr.h"
0008 #include "Alignment/CocoaUtilities/interface/ALIUtils.h"
0009 #include "Alignment/CocoaModel/interface/ALIUnitsTable.h"
0010 #include <CLHEP/Random/RandGauss.h>
0011 #include <CLHEP/Random/Random.h>
0012 #include <cstdlib>
0013
0014
0015 ParameterMgr* ParameterMgr::theInstance = nullptr;
0016
0017
0018 ParameterMgr* ParameterMgr::getInstance() {
0019 if (!theInstance) {
0020 theInstance = new ParameterMgr;
0021 }
0022
0023 return theInstance;
0024 }
0025
0026
0027 ALIdouble ParameterMgr::getVal(const ALIstring& str, const ALIdouble dimensionFactor) {
0028
0029 ALIint iast = str.find('*');
0030
0031 if (iast != -1) {
0032 ALIstring valstr = str.substr(0, iast);
0033 ALIstring unitstr = str.substr(iast + 1, str.length());
0034
0035
0036 if (!ALIUtils::IsNumber(valstr)) {
0037 std::cerr << " ParameterMgr::getVal of an ALIstring that is not a number: " << valstr << std::endl;
0038 abort();
0039 }
0040
0041
0042 return atof(valstr.c_str()) * ALIUnitDefinition::GetValueOf(unitstr);
0043 } else {
0044
0045 if (!ALIUtils::IsNumber(str)) {
0046
0047 ALIdouble val;
0048 if (getParameterValue(str, val)) {
0049 return val;
0050 } else {
0051 std::cerr << " ParameterMgr::getVal of an string that is not a number nor a previous parameter: " << str
0052 << std::endl;
0053 abort();
0054 }
0055 }
0056
0057
0058 return atof(str.c_str()) * dimensionFactor;
0059 }
0060 }
0061
0062
0063 void ParameterMgr::addParameter(const ALIstring& name, const ALIstring& valstr) {
0064 if (theParameters.find(name) != theParameters.end()) {
0065 if (ALIUtils::debug >= 1)
0066 std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
0067 } else {
0068 theParameters[name] = getVal(valstr);
0069 }
0070 }
0071
0072 void ParameterMgr::setRandomSeed(const long seed) { CLHEP::HepRandom::setTheSeed(seed); }
0073
0074
0075 void ParameterMgr::addRandomGaussParameter(const ALIstring& name,
0076 const ALIstring& valMean,
0077 const ALIstring& valStdDev) {
0078 if (theParameters.find(name) != theParameters.end()) {
0079 if (ALIUtils::debug >= 1)
0080 std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
0081 } else {
0082 ALIdouble mean = getVal(valMean);
0083 ALIdouble stddev = getVal(valStdDev);
0084 ALIdouble val = CLHEP::RandGauss::shoot(mean, stddev);
0085 theParameters[name] = val;
0086 if (ALIUtils::debug >= -2)
0087 std::cout << " addRandomGaussParameter " << name << " " << valMean << " " << valStdDev << " = " << val
0088 << std::endl;
0089 }
0090 }
0091
0092
0093 void ParameterMgr::addRandomFlatParameter(const ALIstring& name,
0094 const ALIstring& valMean,
0095 const ALIstring& valInterval) {
0096 if (theParameters.find(name) != theParameters.end()) {
0097 if (ALIUtils::debug >= 1)
0098 std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
0099 } else {
0100 ALIdouble mean = getVal(valMean);
0101 ALIdouble interval = getVal(valInterval);
0102 ALIdouble val = CLHEP::HepRandom::getTheEngine()->flat();
0103
0104 val = val * 2 * interval + mean - interval;
0105 theParameters[name] = val;
0106 if (ALIUtils::debug >= 2)
0107 std::cout << " addRandomFlatParameter " << name << " " << valMean << " " << valInterval << " = " << val
0108 << std::endl;
0109 }
0110 }
0111
0112
0113
0114 ALIint ParameterMgr::getParameterValue(const ALIstring& name, ALIdouble& val) {
0115
0116
0117 ALIstring namet = name;
0118 ALIint negpar = 1;
0119 if (namet[0] == '-') {
0120 negpar = -1;
0121 namet = namet.substr(1, namet.length());
0122 }
0123
0124
0125 msd::iterator ite = theParameters.find(namet);
0126 if (ite == theParameters.end()) {
0127
0128
0129
0130
0131
0132 return 0;
0133 } else {
0134 val = (*ite).second * negpar;
0135
0136 return 1;
0137 }
0138 }