CMS 3D CMS Logo

/data/git/CMSSW_5_3_11_patch5/src/Alignment/CocoaModel/src/ParameterMgr.cc

Go to the documentation of this file.
00001 //   COCOA class implementation file
00002 //Id:  ParameterMgr.cc
00003 //CAT: Model
00004 //
00005 //   History: v1.0  10/11/01   Pedro Arce
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 #include <cstdlib>
00013 //----------------------------------------------------------------------------
00014 
00015 
00016 ParameterMgr* ParameterMgr::theInstance = 0;
00017 
00018 //----------------------------------------------------------------------------
00019 ParameterMgr* ParameterMgr::getInstance()
00020 {
00021   if( !theInstance ) {
00022     theInstance = new ParameterMgr;
00023   }
00024 
00025   return theInstance;
00026 
00027 }
00028 
00029 
00030 //----------------------------------------------------------------------------
00031 ALIdouble ParameterMgr::getVal( const ALIstring& str, const ALIdouble dimensionFactor )
00032 {
00033   //If there is a '*', the characters after '*' are the unit
00034   ALIint iast =  str.find('*');
00035   //  ALIdouble vl;
00036   if( iast != -1 ) {
00037     ALIstring valstr = str.substr( 0, iast );
00038     ALIstring unitstr = str.substr(iast+1, str.length() );
00039 
00040     //-    std::cout << iast << "parametermgr " << str << " " << valstr << " " << unitstr << std::endl;
00041     if( !ALIUtils::IsNumber( valstr ) ) {
00042       std::cerr << " ParameterMgr::getVal of an ALIstring that is not a number: " << valstr << std::endl;
00043       abort();
00044     }
00045  
00046     //-    std::cout << " getVal " <<  atof( valstr.c_str() ) << " * " << ALIUnitDefinition::GetValueOf(unitstr) << std::endl;
00047     return atof( valstr.c_str() ) * ALIUnitDefinition::GetValueOf(unitstr);
00048   } else {
00049   //If there is not a '*', use the dimensionFactor 
00050     if( !ALIUtils::IsNumber( str ) ) {
00051       //--- Check if it is referring to a previous parameter.
00052       ALIdouble val;
00053       if( getParameterValue( str, val ) ) {
00054         return val;
00055       } else {
00056         std::cerr << " ParameterMgr::getVal of an string that is not a number nor a previous parameter: " << str << std::endl;
00057         abort();
00058       }
00059     }
00060 
00061 
00062     //-    std::cout << "ParameterMgr::getVal " << atof( str.c_str() ) << " * " << dimensionFactor << std::endl;
00063     return atof( str.c_str() ) * dimensionFactor;
00064   }
00065 }
00066 
00067 
00068 //----------------------------------------------------------------------------
00069 void ParameterMgr::addParameter( const ALIstring& name, const ALIstring& valstr )
00070 {
00071   if( theParameters.find( name ) != theParameters.end() ) {
00072     if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00073   } else {
00074     theParameters[name] = getVal( valstr );
00075   }
00076 
00077 }
00078 
00079 
00080 void ParameterMgr::setRandomSeed( const long seed )
00081 {
00082   CLHEP::HepRandom::setTheSeed( seed );
00083 }
00084 
00085 
00086 //----------------------------------------------------------------------------
00087 void ParameterMgr::addRandomGaussParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valStdDev ) 
00088 {
00089   if( theParameters.find( name ) != theParameters.end() ) {
00090     if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00091   } else {
00092     ALIdouble mean = getVal( valMean );
00093     ALIdouble stddev = getVal( valStdDev );
00094     ALIdouble val = CLHEP::RandGauss::shoot( mean, stddev );
00095     theParameters[name] = val;
00096     if( ALIUtils::debug >= -2 ) std::cout << " addRandomGaussParameter "  << name << " " << valMean << " " << valStdDev << " = " << val << std::endl;
00097   }
00098 
00099 }
00100 
00101 
00102 //----------------------------------------------------------------------------
00103 void ParameterMgr::addRandomFlatParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valInterval ) 
00104 {
00105   if( theParameters.find( name ) != theParameters.end() ) {
00106     if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
00107   } else {
00108     ALIdouble mean = getVal( valMean );
00109     ALIdouble interval = getVal( valInterval );
00110     ALIdouble val = CLHEP::HepRandom::getTheEngine()->flat();
00111     // flat between ]mean-interval, mean+interval[
00112     val = val * 2*interval + mean-interval;
00113     theParameters[name] = val;
00114     if( ALIUtils::debug >= 2 )std::cout << " addRandomFlatParameter " << name << " " << valMean << " " << valInterval << " = " << val << std::endl;
00115   }
00116 
00117 }
00118 
00119 
00120 //----------------------------------------------------------------------------
00121 // get the parameter value if parameter name exists and return 1, else return 0
00122 ALIint ParameterMgr::getParameterValue( const ALIstring& name, ALIdouble& val )
00123 {
00124   //-  std::cout << " ParameterMgr::getParameterValu " << name << " " << std::endl;
00125   //---------- Convert negative parameters
00126   ALIstring namet = name;
00127   ALIint negpar = 1;
00128   if( namet[0] == '-' ) {
00129     negpar = -1;
00130     namet = namet.substr(1, namet.length() );
00131   }
00132 
00133   //---------- Find Parameter by name
00134   msd::iterator ite = theParameters.find( namet );
00135   if( ite == theParameters.end() ) {
00136     /*    msd::iterator ite2 = theParameters.find( name );
00137     for( ite2 = theParameters.begin(); ite2 != theParameters.end(); ite2++ ) {
00138       std::cout << "PARAMETER: " << (*ite2).first << " = " << (*ite2).second << std::endl;
00139     }
00140     */
00141     return 0;
00142   } else {
00143     val = (*ite).second * negpar;
00144     //-    std::cout << "PARAMETER: " << val << " name " << name << std::endl; 
00145     return 1;
00146   }
00147 
00148 }
00149 
00150