Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 11:56:04

0001 //   COCOA class implementation file

0002 //Id:  ParameterMgr.cc

0003 //CAT: Model

0004 //

0005 //   History: v1.0  10/11/01   Pedro Arce

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   //If there is a '*', the characters after '*' are the unit

0029   ALIint iast = str.find('*');
0030   //  ALIdouble vl;

0031   if (iast != -1) {
0032     ALIstring valstr = str.substr(0, iast);
0033     ALIstring unitstr = str.substr(iast + 1, str.length());
0034 
0035     //-    std::cout << iast << "parametermgr " << str << " " << valstr << " " << unitstr << std::endl;

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     //-    std::cout << " getVal " <<  atof( valstr.c_str() ) << " * " << ALIUnitDefinition::GetValueOf(unitstr) << std::endl;

0042     return atof(valstr.c_str()) * ALIUnitDefinition::GetValueOf(unitstr);
0043   } else {
0044     //If there is not a '*', use the dimensionFactor

0045     if (!ALIUtils::IsNumber(str)) {
0046       //--- Check if it is referring to a previous parameter.

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     //-    std::cout << "ParameterMgr::getVal " << atof( str.c_str() ) << " * " << dimensionFactor << std::endl;

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     // flat between ]mean-interval, mean+interval[

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 // get the parameter value if parameter name exists and return 1, else return 0

0114 ALIint ParameterMgr::getParameterValue(const ALIstring& name, ALIdouble& val) {
0115   //-  std::cout << " ParameterMgr::getParameterValu " << name << " " << std::endl;

0116   //---------- Convert negative parameters

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   //---------- Find Parameter by name

0125   msd::iterator ite = theParameters.find(namet);
0126   if (ite == theParameters.end()) {
0127     /*    msd::iterator ite2 = theParameters.find( name );

0128     for( ite2 = theParameters.begin(); ite2 != theParameters.end(); ite2++ ) {

0129       std::cout << "PARAMETER: " << (*ite2).first << " = " << (*ite2).second << std::endl;

0130     }

0131     */
0132     return 0;
0133   } else {
0134     val = (*ite).second * negpar;
0135     //-    std::cout << "PARAMETER: " << val << " name " << name << std::endl;

0136     return 1;
0137   }
0138 }