CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 
16 
17 //----------------------------------------------------------------------------
19  if (!theInstance) {
21  }
22 
23  return theInstance;
24 }
25 
26 //----------------------------------------------------------------------------
27 ALIdouble ParameterMgr::getVal(const ALIstring& str, const ALIdouble dimensionFactor) {
28  //If there is a '*', the characters after '*' are the unit
29  ALIint iast = str.find('*');
30  // ALIdouble vl;
31  if (iast != -1) {
32  ALIstring valstr = str.substr(0, iast);
33  ALIstring unitstr = str.substr(iast + 1, str.length());
34 
35  //- std::cout << iast << "parametermgr " << str << " " << valstr << " " << unitstr << std::endl;
36  if (!ALIUtils::IsNumber(valstr)) {
37  std::cerr << " ParameterMgr::getVal of an ALIstring that is not a number: " << valstr << std::endl;
38  abort();
39  }
40 
41  //- std::cout << " getVal " << atof( valstr.c_str() ) << " * " << ALIUnitDefinition::GetValueOf(unitstr) << std::endl;
42  return atof(valstr.c_str()) * ALIUnitDefinition::GetValueOf(unitstr);
43  } else {
44  //If there is not a '*', use the dimensionFactor
45  if (!ALIUtils::IsNumber(str)) {
46  //--- Check if it is referring to a previous parameter.
47  ALIdouble val;
48  if (getParameterValue(str, val)) {
49  return val;
50  } else {
51  std::cerr << " ParameterMgr::getVal of an string that is not a number nor a previous parameter: " << str
52  << std::endl;
53  abort();
54  }
55  }
56 
57  //- std::cout << "ParameterMgr::getVal " << atof( str.c_str() ) << " * " << dimensionFactor << std::endl;
58  return atof(str.c_str()) * dimensionFactor;
59  }
60 }
61 
62 //----------------------------------------------------------------------------
63 void ParameterMgr::addParameter(const ALIstring& name, const ALIstring& valstr) {
64  if (theParameters.find(name) != theParameters.end()) {
65  if (ALIUtils::debug >= 1)
66  std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
67  } else {
68  theParameters[name] = getVal(valstr);
69  }
70 }
71 
72 void ParameterMgr::setRandomSeed(const long seed) { CLHEP::HepRandom::setTheSeed(seed); }
73 
74 //----------------------------------------------------------------------------
76  const ALIstring& valMean,
77  const ALIstring& valStdDev) {
78  if (theParameters.find(name) != theParameters.end()) {
79  if (ALIUtils::debug >= 1)
80  std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
81  } else {
82  ALIdouble mean = getVal(valMean);
83  ALIdouble stddev = getVal(valStdDev);
84  ALIdouble val = CLHEP::RandGauss::shoot(mean, stddev);
86  if (ALIUtils::debug >= -2)
87  std::cout << " addRandomGaussParameter " << name << " " << valMean << " " << valStdDev << " = " << val
88  << std::endl;
89  }
90 }
91 
92 //----------------------------------------------------------------------------
94  const ALIstring& valMean,
95  const ALIstring& valInterval) {
96  if (theParameters.find(name) != theParameters.end()) {
97  if (ALIUtils::debug >= 1)
98  std::cerr << "!! WARNING: PARAMETER " << name << " appears twice, it will take first value " << std::endl;
99  } else {
100  ALIdouble mean = getVal(valMean);
101  ALIdouble interval = getVal(valInterval);
102  ALIdouble val = CLHEP::HepRandom::getTheEngine()->flat();
103  // flat between ]mean-interval, mean+interval[
104  val = val * 2 * interval + mean - interval;
106  if (ALIUtils::debug >= 2)
107  std::cout << " addRandomFlatParameter " << name << " " << valMean << " " << valInterval << " = " << val
108  << std::endl;
109  }
110 }
111 
112 //----------------------------------------------------------------------------
113 // get the parameter value if parameter name exists and return 1, else return 0
115  //- std::cout << " ParameterMgr::getParameterValu " << name << " " << std::endl;
116  //---------- Convert negative parameters
117  ALIstring namet = name;
118  ALIint negpar = 1;
119  if (namet[0] == '-') {
120  negpar = -1;
121  namet = namet.substr(1, namet.length());
122  }
123 
124  //---------- Find Parameter by name
125  msd::iterator ite = theParameters.find(namet);
126  if (ite == theParameters.end()) {
127  /* msd::iterator ite2 = theParameters.find( name );
128  for( ite2 = theParameters.begin(); ite2 != theParameters.end(); ite2++ ) {
129  std::cout << "PARAMETER: " << (*ite2).first << " = " << (*ite2).second << std::endl;
130  }
131  */
132  return 0;
133  } else {
134  val = (*ite).second * negpar;
135  //- std::cout << "PARAMETER: " << val << " name " << name << std::endl;
136  return 1;
137  }
138 }
long double ALIdouble
Definition: CocoaGlobals.h:11
tuple interval
Definition: MergeJob_cfg.py:20
static ParameterMgr * getInstance()
Definition: ParameterMgr.cc:18
int ALIint
Definition: CocoaGlobals.h:15
static ALIint debug
Definition: ALIUtils.h:34
ALIint getParameterValue(const ALIstring &name, ALIdouble &val)
void addRandomGaussParameter(const ALIstring &name, const ALIstring &valMean, const ALIstring &valStdDev)
Definition: ParameterMgr.cc:75
void setRandomSeed(const long seed)
Definition: ParameterMgr.cc:72
static int IsNumber(const ALIstring &str)
Definition: ALIUtils.cc:33
static ParameterMgr * theInstance
Definition: ParameterMgr.h:32
void addRandomFlatParameter(const ALIstring &name, const ALIstring &valMean, const ALIstring &valInterval)
Definition: ParameterMgr.cc:93
static ALIdouble GetValueOf(ALIstring)
std::string ALIstring
Definition: CocoaGlobals.h:9
tuple cout
Definition: gather_cfg.py:144
ALIdouble getVal(const ALIstring &str, const ALIdouble dimensionFactor=1.)
Definition: ParameterMgr.cc:27
void addParameter(const ALIstring &name, const ALIstring &valstr)
Definition: ParameterMgr.cc:63
#define str(s)