BCVariableSet.h
1 #ifndef __BCVARIABLESET__H
2 #define __BCVARIABLESET__H
3 
12 /*
13  * Copyright (C) 2007-2013, the BAT core developer team
14  * All rights reserved.
15  *
16  * For the licensing terms see doc/COPYING.
17  * For documentation see http://mpp.mpg.de/bat
18  */
19 
20 // ---------------------------------------------------------
21 
22 #include <vector>
23 #include <string>
24 
25 #include "BCLog.h"
26 #include "BCAux.h"
27 
28 #include <TString.h>
29 
30 class TRandom;
31 
32 // ---------------------------------------------------------
33 
34 template<class T>
36 {
37 public:
38 
43  {
44  }
45 
46  /*
47  * Destructor */
48  virtual ~BCVariableSet()
49  {
50  }
51 
56  virtual bool Add(const T& var)
57  {
58  // check if variable with same name or same safe name exists
59  for (unsigned int i = 0; i < fVars.size() ; ++i)
60  if (var.IsNamed(fVars[i].GetName())) {
61  BCLog::OutError("BCVariableSet::Add : Variable with name" + var.GetName() + "exists already.");
62  return false;
63  } else if ( var.IsSafeNamed(fVars[i].GetSafeName()) ) {
64  BCLog::OutError("BCVariableSet::Add : Variable with safe name " + var.GetSafeName() + " exists already.");
65  return false;
66  }
67 
68  // add var to container
69  fVars.push_back(var);
70  fMaxNameLength = std::max(fMaxNameLength, (unsigned)var.GetName().length());
71 
72  return true;
73  }
74 
83  virtual bool Add(const std::string& name, double min, double max, const std::string& latexname = "", const std::string& unitstring = "")
84  {
85  // check if variable with same name or same safe name exists
86  for (unsigned int i = 0; i < fVars.size() ; ++i)
87  if (fVars[i].IsNamed(name)) {
88  BCLog::OutError("BCVariableSet::Add : Variable with name " + name + " exists already.");
89  return false;
90  } else if ( fVars[i].IsSafeNamed(BCAux::SafeName(name)) ) {
91  BCLog::OutError("BCVariableSet::Add : Variable with safe name " + fVars[i].GetSafeName() + "%s exists already.");
92  return false;
93  }
94 
95  // add var to container
96  fVars.push_back(T(name, min, max, latexname, unitstring));
97  fMaxNameLength = std::max(fMaxNameLength, (unsigned)name.length());
98 
99  return true;
100  }
101 
106  virtual T& operator[](unsigned index)
107  { return fVars[index]; }
108 
113  virtual const T& operator[](unsigned index) const
114  { return fVars[index]; }
115 
120  virtual T& At(unsigned index)
121  { return fVars.at(index); }
122 
127  virtual const T& At(unsigned index) const
128  { return fVars.at(index); }
129 
134  virtual T& Get(const std::string& name)
135  { return At(Index(name)); }
136 
141  virtual const T& Get(const std::string& name) const
142  { return At(Index(name)); }
143 
146  virtual T& Back()
147  { return fVars.back(); }
148 
151  virtual const T& Back() const
152  { return fVars.back(); }
153 
157  virtual unsigned Index(const std::string& name) const
158  {
159  for (unsigned int i = 0; i < fVars.size() ; ++i)
160  if ( fVars[i].IsNamed(name) )
161  return i;
162  BCLog::OutWarning("BCVariableSet::Index : no variable named '" + name + "'");
163  return fVars.size();
164  }
165 
168  virtual unsigned Size() const
169  { return fVars.size(); }
170 
173  virtual bool Empty() const
174  { return fVars.empty(); }
175 
179  virtual void SetNBins(unsigned nbins)
180  {
181  for (unsigned i = 0 ; i < fVars.size() ; ++i )
182  fVars[i].SetNbins(nbins);
183  }
184 
188  virtual void SetPrecision(unsigned n)
189  {
190  for (unsigned i = 0 ; i < fVars.size() ; ++i )
191  fVars[i].SetPrecision(n);
192  }
193 
197  virtual void FillHistograms(bool flag)
198  { FillHistograms(flag, flag); }
199 
204  virtual void FillHistograms(bool flag_1d, bool flag_2d)
205  { FillH1(flag_1d); FillH2(flag_2d); }
206 
210  virtual void FillH1(bool flag)
211  {
212  for (unsigned i = 0 ; i < fVars.size() ; ++i )
213  fVars[i].FillH1(flag);
214  }
215 
219  virtual void FillH2(bool flag)
220  {
221  for (unsigned i = 0 ; i < fVars.size() ; ++i )
222  fVars[i].FillH2(flag);
223  }
224 
227  virtual unsigned MaxNameLength() const
228  { return fMaxNameLength; }
229 
232  virtual double Volume() const
233  {
234  double volume = 1;
235  for (unsigned i = 0; i < fVars.size(); ++i)
236  volume *= fVars[i].GetRangeWidth();
237  if (volume < 0)
238  return 0;
239  return volume;
240  }
241 
244  virtual bool IsWithinLimits(const std::vector<double>& x) const
245  {
246  if (x.size() != fVars.size())
247  return false;
248  for (unsigned i = 0; i < fVars.size(); ++i)
249  if (!fVars[i].IsWithinLimits(x[i]))
250  return false;
251  return true;
252  }
253 
260  virtual std::vector<double> PositionInRange(const std::vector<double>& x) const
261  {
262  std::vector<double> p;
263  for (unsigned i = 0; i < fVars.size(); ++i)
264  p.push_back(fVars[i].PositionInRange(x[i]));
265  return p;
266  }
267 
271  virtual void ValueFromPositionInRange(std::vector<double>& p) const
272  {
273  if ( p.size() != fVars.size() )
274  return;
275  for (unsigned i = 0; i < fVars.size(); ++i)
276  p[i] = fVars[i].ValueFromPositionInRange(p[i]);
277  }
278 
281  virtual std::vector<double> GetRangeCenters() const
282  {
283  std::vector<double> p;
284  for (unsigned i = 0; i < fVars.size(); ++i)
285  p.push_back(fVars[i].GetRangeCenter());
286  return p;
287  }
288 
293  virtual std::vector<double> GetUniformRandomValues(TRandom* const R) const
294  {
295  std::vector<double> p;
296  for (unsigned i = 0; i < fVars.size(); ++i)
297  p.push_back(fVars[i].GetUniformRandomValue(R));
298  return p;
299  }
300 
303  virtual void PrintSummary() const
304  {
305  unsigned n = (int)log10(fVars.size()) + 1;
306  for (unsigned i = 0; i < fVars.size(); ++i)
307  BCLog::OutSummary(Form(" %*u) ", n, i) + fVars[i].OneLineSummary(false, fMaxNameLength));
308  }
309 
310 protected:
314  std::vector<T> fVars;
315 
318  unsigned fMaxNameLength;
319 
320 };
321 #endif
virtual std::vector< double > PositionInRange(const std::vector< double > &x) const
return positions in ranges of given values from 0 (at lower limit) to 1 (at upper limit) for each var...
virtual const T & Back() const
Access to last pushed variable.
virtual unsigned Index(const std::string &name) const
Find index of parameter identified by name; return Size() if name not found.
virtual const T & At(unsigned index) const
Safe access, but slightly less efficient access to parameter.
virtual void PrintSummary() const
Print summary of variable set to logs.
virtual unsigned Size() const
Number of variables contained.
virtual bool Add(const std::string &name, double min, double max, const std::string &latexname="", const std::string &unitstring="")
Add a variable.
Definition: BCVariableSet.h:83
unsigned fMaxNameLength
Maximum length (in characters) of variable names.
virtual T & operator[](unsigned index)
Raw and fast access.
BCVariableSet()
Constructor.
Definition: BCVariableSet.h:41
virtual bool Add(const T &var)
Add a variable if no variable of same name exists yet.
Definition: BCVariableSet.h:56
virtual void FillH2(bool flag)
Set fill-histograms flag for all 2D histograms for all parameters.
virtual void FillHistograms(bool flag)
Set fill-histograms flag for 1D and 2D histograms for all parameters.
virtual void FillHistograms(bool flag_1d, bool flag_2d)
Set fill-histograms flag for 1D and 2D histograms for all parameters.
virtual bool IsWithinLimits(const std::vector< double > &x) const
std::string SafeName(const std::string &name)
Convert a name into a safe name for use in ROOT object naming.
Definition: BCAux.cxx:111
virtual void FillH1(bool flag)
Set fill-histograms flag for all 1D histograms for all parameters.
virtual const T & Get(const std::string &name) const
Safe access, but slightly less efficient access to parameter.
virtual std::vector< double > GetUniformRandomValues(TRandom *const R) const
Get vector of uniformly distributed random values.
virtual const T & operator[](unsigned index) const
Raw and fast access.
std::vector< T > fVars
Vector of BCVariables that forms the set.
virtual double Volume() const
virtual void SetNBins(unsigned nbins)
Set number of bins for all parameters.
virtual unsigned MaxNameLength() const
virtual T & Back()
Access to last pushed variable.
virtual void SetPrecision(unsigned n)
Set precision for output of all parameters.
virtual void ValueFromPositionInRange(std::vector< double > &p) const
Translate from unit interval to values in variable ranges.
virtual bool Empty() const
Whether container is empty.
Wrapper to allow access by name into list of BCVariable.
Definition: BCVariableSet.h:35
virtual T & Get(const std::string &name)
Safe access, but slightly less efficient access to parameter.
virtual std::vector< double > GetRangeCenters() const
virtual T & At(unsigned index)
Safe access, but slightly less efficient access to parameter.