CMS 3D CMS Logo

ParameterMgr.cc
Go to the documentation of this file.
1 // COCOA class implementation file
2 //Id: ParameterMgr.cc
3 //CAT: Model
4 //
5 // History: v1.0 10/11/01 Pedro Arce
6 
10 #include <CLHEP/Random/RandGauss.h>
11 #include <CLHEP/Random/Random.h>
12 #include <cstdlib>
13 //----------------------------------------------------------------------------
14 
15 
17 
18 //----------------------------------------------------------------------------
20 {
21  if( !theInstance ) {
23  }
24 
25  return theInstance;
26 
27 }
28 
29 
30 //----------------------------------------------------------------------------
31 ALIdouble ParameterMgr::getVal( const ALIstring& str, const ALIdouble dimensionFactor )
32 {
33  //If there is a '*', the characters after '*' are the unit
34  ALIint iast = str.find('*');
35  // ALIdouble vl;
36  if( iast != -1 ) {
37  ALIstring valstr = str.substr( 0, iast );
38  ALIstring unitstr = str.substr(iast+1, str.length() );
39 
40  //- std::cout << iast << "parametermgr " << str << " " << valstr << " " << unitstr << std::endl;
41  if( !ALIUtils::IsNumber( valstr ) ) {
42  std::cerr << " ParameterMgr::getVal of an ALIstring that is not a number: " << valstr << std::endl;
43  abort();
44  }
45 
46  //- std::cout << " getVal " << atof( valstr.c_str() ) << " * " << ALIUnitDefinition::GetValueOf(unitstr) << std::endl;
47  return atof( valstr.c_str() ) * ALIUnitDefinition::GetValueOf(unitstr);
48  } else {
49  //If there is not a '*', use the dimensionFactor
50  if( !ALIUtils::IsNumber( str ) ) {
51  //--- Check if it is referring to a previous parameter.
52  ALIdouble val;
53  if( getParameterValue( str, val ) ) {
54  return val;
55  } else {
56  std::cerr << " ParameterMgr::getVal of an string that is not a number nor a previous parameter: " << str << std::endl;
57  abort();
58  }
59  }
60 
61 
62  //- std::cout << "ParameterMgr::getVal " << atof( str.c_str() ) << " * " << dimensionFactor << std::endl;
63  return atof( str.c_str() ) * dimensionFactor;
64  }
65 }
66 
67 
68 //----------------------------------------------------------------------------
69 void ParameterMgr::addParameter( const ALIstring& name, const ALIstring& valstr )
70 {
71  if( theParameters.find( name ) != theParameters.end() ) {
72  if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
73  } else {
74  theParameters[name] = getVal( valstr );
75  }
76 
77 }
78 
79 
81 {
82  CLHEP::HepRandom::setTheSeed( seed );
83 }
84 
85 
86 //----------------------------------------------------------------------------
87 void ParameterMgr::addRandomGaussParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valStdDev )
88 {
89  if( theParameters.find( name ) != theParameters.end() ) {
90  if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
91  } else {
92  ALIdouble mean = getVal( valMean );
93  ALIdouble stddev = getVal( valStdDev );
94  ALIdouble val = CLHEP::RandGauss::shoot( mean, stddev );
96  if( ALIUtils::debug >= -2 ) std::cout << " addRandomGaussParameter " << name << " " << valMean << " " << valStdDev << " = " << val << std::endl;
97  }
98 
99 }
100 
101 
102 //----------------------------------------------------------------------------
103 void ParameterMgr::addRandomFlatParameter( const ALIstring& name, const ALIstring& valMean, const ALIstring& valInterval )
104 {
105  if( theParameters.find( name ) != theParameters.end() ) {
106  if( ALIUtils::debug >= 1) std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
107  } else {
108  ALIdouble mean = getVal( valMean );
109  ALIdouble interval = getVal( valInterval );
110  ALIdouble val = CLHEP::HepRandom::getTheEngine()->flat();
111  // flat between ]mean-interval, mean+interval[
112  val = val * 2*interval + mean-interval;
114  if( ALIUtils::debug >= 2 )std::cout << " addRandomFlatParameter " << name << " " << valMean << " " << valInterval << " = " << val << std::endl;
115  }
116 
117 }
118 
119 
120 //----------------------------------------------------------------------------
121 // get the parameter value if parameter name exists and return 1, else return 0
123 {
124  //- std::cout << " ParameterMgr::getParameterValu " << name << " " << std::endl;
125  //---------- Convert negative parameters
126  ALIstring namet = name;
127  ALIint negpar = 1;
128  if( namet[0] == '-' ) {
129  negpar = -1;
130  namet = namet.substr(1, namet.length() );
131  }
132 
133  //---------- Find Parameter by name
134  msd::iterator ite = theParameters.find( namet );
135  if( ite == theParameters.end() ) {
136  /* msd::iterator ite2 = theParameters.find( name );
137  for( ite2 = theParameters.begin(); ite2 != theParameters.end(); ite2++ ) {
138  std::cout << "PARAMETER: " << (*ite2).first << " = " << (*ite2).second << std::endl;
139  }
140  */
141  return 0;
142  } else {
143  val = (*ite).second * negpar;
144  //- std::cout << "PARAMETER: " << val << " name " << name << std::endl;
145  return 1;
146  }
147 
148 }
149 
150 
long double ALIdouble
Definition: CocoaGlobals.h:11
static ParameterMgr * getInstance()
Definition: ParameterMgr.cc:19
int ALIint
Definition: CocoaGlobals.h:15
static ALIint debug
Definition: ALIUtils.h:36
ALIint getParameterValue(const ALIstring &name, ALIdouble &val)
void addRandomGaussParameter(const ALIstring &name, const ALIstring &valMean, const ALIstring &valStdDev)
Definition: ParameterMgr.cc:87
void setRandomSeed(const long seed)
Definition: ParameterMgr.cc:80
static int IsNumber(const ALIstring &str)
Definition: ALIUtils.cc:34
static ParameterMgr * theInstance
Definition: ParameterMgr.h:34
void addRandomFlatParameter(const ALIstring &name, const ALIstring &valMean, const ALIstring &valInterval)
static ALIdouble GetValueOf(ALIstring)
std::string ALIstring
Definition: CocoaGlobals.h:9
ALIdouble getVal(const ALIstring &str, const ALIdouble dimensionFactor=1.)
Definition: ParameterMgr.cc:31
void addParameter(const ALIstring &name, const ALIstring &valstr)
Definition: ParameterMgr.cc:69
#define str(s)