ConfigParameter.cc
Go to the documentation of this file.
1 #include <utl/ConfigParameter.h>
2 
3 #include <utl/VRandomSampler.h>
4 #include <utl/RandomSamplerFromPDF.h>
5 #include <utl/RandomSamplerFromCDF.h>
6 #include <utl/Branch.h>
7 #include <utl/ConsecutiveEnumFactory.h>
8 #include <utl/RandomEngine.h>
9 #include <utl/ErrorLogger.h>
10 #include <utl/TabulatedFunction.h>
11 //
12 #include <string>
13 #include <list>
14 #include <vector>
15 #include <algorithm>
16 #include <iterator>
17 
18 
19 namespace {
20 
22  class FixedSampler: public utl::VRandomSampler {
23  public:
24  FixedSampler(const utl::ConfigParameter::Type& v);
25  protected:
26  virtual double MapRandom(double rand) const;
27  private:
29  };
30 
31 
32  FixedSampler::FixedSampler(const utl::ConfigParameter::Type& v) :
33  fValue(v)
34  { }
35 
36 
37  double
38  FixedSampler::MapRandom(double /*rand*/)
39  const
40  {
41  // Here we have a "cast" from the utl::ConfigParameter::Type
42  // to double, which are the same by now.
43  return fValue;
44  }
45 
46 
48  class ListSampler : public utl::VRandomSampler {
49  public:
51  typedef std::list<utl::ConfigParameter::Type> List;
53  template<class C>
54  ListSampler(const C& values);
56  ListSampler(std::unique_ptr<List>& source);
57  protected:
58  virtual double MapRandom(const double rand) const;
59  private:
60  void Init();
61  std::unique_ptr<List> fValues;
62  mutable List::const_iterator fIt;
63  List::const_iterator fEnd;
64  };
65 
66 
67  template<class C>
68  ListSampler::ListSampler(const C& values) :
69  fValues(new List(values.begin(), values.end()))
70  {
71  Init();
72  }
73 
74 
75  ListSampler::ListSampler(std::unique_ptr<List>& source) :
76  fValues(std::move(source))
77  {
78  Init();
79  }
80 
81 
82  void
84  {
85  // Init iterators:
86  fIt = fValues->begin();
87  fEnd = fValues->end(); // once and for all.
88  }
89 
90 
91  double
92  ListSampler::MapRandom(double /*rand*/)
93  const
94  {
95  if (fIt == fEnd)
96  fIt = fValues->begin(); // circularly.
97  // see FixedSampler regarding the cast.
98  return *(fIt++); // note post-increment.
99  }
100 
101 }
102 
103 
104 namespace utl {
105 
106  const char* const ConfigParameter::kFunctionTypeTags[] = { "Fixed", "PDF", "CDF", "ValueList" };
107 
108  const char* const ConfigParameter::kParametrizationTags[] =
109  { "Formula", "FormulaWithVariable", "Map", "TabulatedFunction", "Vector", "VectorPair" };
110 
111  const char* const ConfigParameter::kInterpolationTags[] = { "Linear", "Discrete", "Step" };
112 
113 
114  ConfigParameter::ConfigParameter(utl::RandomEngine& eng, const utl::Branch& configData) :
115  fEngine(eng),
116  fLastValue(0)
117  {
118  std::string tagFunc;
119  configData.GetChild("functionType").GetData(tagFunc);
121  switch (type) {
122  case eFixed:
123  {
124  const double v = configData.GetChild("value").Get<double>();
125  fSampler.reset(new FixedSampler(v));
126  }
127  break;
128  case eValueList:
129  {
130  // Heap allocated and then tranfer ownership.
131  std::unique_ptr<ListSampler::List> vs(new ListSampler::List());
132  configData.GetChild("valueList").GetData(*vs);
133  fSampler.reset(new ListSampler(vs));
134  }
135  break;
136  case ePDF:
137  case eCDF:
138  {
139  const std::string tagPar = configData.GetChild("parametrization").Get<std::string>();
141  // All kind of posible parameters.
142  std::string function;
143  double min;
144  double max;
145  int bins;
146  std::string iVarName;
147  std::map<double, double> dfMap;
149  std::vector<double> dfVect;
150  std::vector<double> xVect;
151  std::vector<double> yVect;
152  std::string interpol; // watch out, the police!
153  // Alias for the factory: used inside the ePDF case.
156  // Cheap: do it despite may not be needed (so to have a little cleaner code)...
157  utl::Branch interpolChild = configData.GetChild("interpolationType");
158  if (interpolChild) // ... but do a check in this case.
159  interpolChild.GetData(interpol);
160  switch (par) {
162  configData.GetChild("independentVariableName").GetData(iVarName);
163  // fall through
164  case eFormula:
165  configData.GetChild("function").GetData(function);
166  configData.GetChild("min").GetData(min);
167  configData.GetChild("max").GetData(max);
168  configData.GetChild("bins").GetData(bins);
169  if (par == eFormula) {
170  if (type == ePDF)
171  fSampler.reset(new utl::RandomSamplerFromPDF(function, min, max, bins));
172  else
173  fSampler.reset(new utl::RandomSamplerFromCDF(function, min, max, bins));
174  } else {
175  if (type == ePDF)
176  fSampler.reset(new utl::RandomSamplerFromPDF(function, iVarName, min, max, bins));
177  else
178  fSampler.reset(new utl::RandomSamplerFromCDF(function, iVarName, min, max, bins));
179  }
180  break;
181  case eMap:
182  /*configData.GetChild("map").GetData(dfMap);
183  if (type == ePDF)
184  fSampler.reset(new utl::RandomSamplerFromPDF(dfMap, InterpolFactory::Create(interpol)));
185  else
186  fSampler.reset(new utl::RandomSamplerFromCDF(dfMap));*/
187  ERROR("Branch doesn't provide a GetData(std::map<>) method.");
188  break;
189  case eTabulatedFunction:
190  configData.GetChild("tabulatedFunction").GetData(dfTab);
191  if (type == ePDF)
192  fSampler.reset(new utl::RandomSamplerFromPDF(dfTab, InterpolFactory::Create(interpol)));
193  else
194  fSampler.reset(new utl::RandomSamplerFromCDF(dfTab));
195  break;
196  case eVector:
197  configData.GetChild("vector").GetData(dfVect);
198  if (type == ePDF)
199  fSampler.reset(new utl::RandomSamplerFromPDF(dfTab, InterpolFactory::Create(interpol)));
200  else
201  fSampler.reset(new utl::RandomSamplerFromCDF(dfTab));
202  break;
203  case eVectorPair:
204  configData.GetChild("vectorX").GetData(xVect);
205  configData.GetChild("vectorY").GetData(yVect);
206  if (type == ePDF)
207  fSampler.reset(new utl::RandomSamplerFromPDF(xVect, yVect, InterpolFactory::Create(interpol)));
208  else
209  fSampler.reset(new utl::RandomSamplerFromCDF(xVect, yVect));
210  break;
211  }
212  }
213  break;
214  }
215  configData.GetChild("delta").GetData(fDelta);
216  if (fDelta) {
217  // Load an initial value for the sequence obtained
218  // with the delta strategy. If not present, zero remains.
219  utl::Branch b = configData.GetChild("initialOffset");
220  if (b)
221  b.GetData(fLastValue);
222  }
223  }
224 
225 
227  {
228  // Nothing to do, but here the type pointed to by the
229  // auto_ptr has been defined.
230  }
231 
232 
233  double
235  const
236  {
237  const double v = fSampler->shoot(fEngine.GetEngine());
238  // If delta we take the shot value to be an increment wrt the previous.
239  return fDelta ? (fLastValue += v) : (fLastValue = v);
240  }
241 
242 }
Simple factory to create an enumerator for a given enumeration.
FunctionType
Type of function to be used.
static EnumType Create(const int k)
int version of the overloaded creation method.
utl::RandomEngine & fEngine
RandomEngineType & GetEngine()
Definition: RandomEngine.h:32
std::unique_ptr< utl::VRandomSampler > fSampler
Class to hold collection (x,y) points and provide interpolation between them.
void Init()
Initialise the registry.
Branch GetChild(const std::string &childName) const
Get child of this Branch by child name.
Definition: Branch.cc:211
Type operator()() const
T Get() const
Definition: Branch.h:271
#define max(a, b)
Class representing a document branch.
Definition: Branch.h:107
Wraps the random number engine used to generate distributions.
Definition: RandomEngine.h:27
~ConfigParameter()
Explicit declaratio+defintion so as to have, when defined, also fully defined the pointed-to type of ...
void GetData(bool &b) const
Overloads of the GetData member template function.
Definition: Branch.cc:644
virtual double MapRandom(const double rand) const =0
#define ERROR(message)
Macro for logging error messages.
Definition: ErrorLogger.h:165
Class to shoot random numbers given by a user-defined distribution function.

, generated on Tue Sep 26 2023.