CMS 3D CMS Logo

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 //----------------------------------------------------------------------------
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   //If there is a '*', the characters after '*' are the unit
00033   ALIint iast =  str.find('*');
00034   //  ALIdouble vl;
00035   if( iast != -1 ) {
00036     ALIstring valstr = str.substr( 0, iast );
00037     ALIstring unitstr = str.substr(iast+1, str.length() );
00038 
00039     //-    std::cout << iast << "parametermgr " << str << " " << valstr << " " << unitstr << std::endl;
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     //-    std::cout << " getVal " <<  atof( valstr.c_str() ) << " * " << ALIUnitDefinition::GetValueOf(unitstr) << std::endl;
00046     return atof( valstr.c_str() ) * ALIUnitDefinition::GetValueOf(unitstr);
00047   } else {
00048   //If there is not a '*', use the dimensionFactor 
00049     if( !ALIUtils::IsNumber( str ) ) {
00050       //--- Check if it is referring to a previous parameter.
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     //-    std::cout << "ParameterMgr::getVal " << atof( str.c_str() ) << " * " << dimensionFactor << std::endl;
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     // flat between ]mean-interval, mean+interval[
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 // get the parameter value if parameter name exists and return 1, else return 0
00121 ALIint ParameterMgr::getParameterValue( const ALIstring& name, ALIdouble& val )
00122 {
00123   //-  std::cout << " ParameterMgr::getParameterValu " << name << " " << std::endl;
00124   //---------- Convert negative parameters
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   //---------- Find Parameter by name
00133   msd::iterator ite = theParameters.find( namet );
00134   if( ite == theParameters.end() ) {
00135     /*    msd::iterator ite2 = theParameters.find( name );
00136     for( ite2 = theParameters.begin(); ite2 != theParameters.end(); ite2++ ) {
00137       std::cout << "PARAMETER: " << (*ite2).first << " = " << (*ite2).second << std::endl;
00138     }
00139     */
00140     return 0;
00141   } else {
00142     val = (*ite).second * negpar;
00143     //-    std::cout << "PARAMETER: " << val << " name " << name << std::endl; 
00144     return 1;
00145   }
00146 
00147 }
00148 
00149 

Generated on Tue Jun 9 17:23:40 2009 for CMSSW by  doxygen 1.5.4