YODA is hosted by Hepforge, IPPP Durham
YODA - Yet more Objects for Data Analysis  1.3.1
AnalysisObject.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // This file is part of YODA -- Yet more Objects for Data Analysis
4 // Copyright (C) 2008-2015 The YODA collaboration (see AUTHORS for details)
5 //
6 #ifndef YODA_AnalysisObject_h
7 #define YODA_AnalysisObject_h
8 
9 #include "YODA/Exceptions.h"
11 #include <string>
12 #include <map>
13 
14 namespace YODA {
15 
16 
19 
20  public:
21 
23  typedef std::map<std::string, std::string> Annotations;
24 
25 
27 
28 
31 
33  AnalysisObject(const std::string& type, const std::string& path, const std::string& title="") {
34  setAnnotation("Type", type);
35  setPath(path);
36  setTitle(title);
37  }
38 
40  AnalysisObject(const std::string& type, const std::string& path,
41  const AnalysisObject& ao, const std::string& title="") {
42  BOOST_FOREACH (const std::string& a, ao.annotations())
43  setAnnotation(a, ao.annotation(a));
44  setAnnotation("Type", type); // might override the copied ones
45  setPath(path);
46  setTitle(title);
47  }
48 
49  // /// Default copy constructor
50  // AnalysisObject(const AnalysisObject& ao) {
51  // if (ao.path().length() > 0) setPath(ao.path());
52  // if (ao.title().length() > 0) setTitle(ao.title());
53  // }
54 
56  virtual ~AnalysisObject() { }
57 
60  if (ao.path().length() > 0) setPath(ao.path());
61  if (ao.title().length() > 0) setTitle(ao.title());
62  return *this;
63  }
64 
66  virtual AnalysisObject* newclone() const = 0;
67 
69 
70 
71 
73 
74 
76  virtual void reset() = 0;
77 
79 
80 
81 
83 
84 
86  const std::vector<std::string> annotations() const {
87  std::vector<std::string> rtn;
88  rtn.reserve(_annotations.size());
89  BOOST_FOREACH (const Annotations::value_type& kv, _annotations) rtn.push_back(kv.first);
90  return rtn;
91  }
92 
93 
95  bool hasAnnotation(const std::string& name) const {
96  return _annotations.find(name) != _annotations.end();
97  }
98 
99 
101  const std::string& annotation(const std::string& name) const {
102  Annotations::const_iterator v = _annotations.find(name);
103  // If not found... written this way round on purpose
104  if (v == _annotations.end()) {
105  std::string missing = "YODA::AnalysisObject: No annotation named " + name;
106  throw AnnotationError(missing);
107  }
108  return v->second;
109  }
110 
111 
113  const std::string& annotation(const std::string& name, const std::string& defaultreturn) const {
114  Annotations::const_iterator v = _annotations.find(name);
115  if (v != _annotations.end()) return v->second;
116  return defaultreturn;
117  }
118 
119 
123  template <typename T>
124  const T annotation(const std::string& name) const {
125  std::string s = annotation(name);
126  return boost::lexical_cast<T>(s);
127  }
128 
129 
133  template <typename T>
134  const T annotation(const std::string& name, const T& defaultreturn) const {
135  try {
136  std::string s = annotation(name);
137  return boost::lexical_cast<T>(s);
138  } catch (const AnnotationError& ae) {
139  return defaultreturn;
140  }
141  }
142 
143 
147  template <typename T>
148  void setAnnotation(const std::string& name, const T& value) {
149  _annotations[name] = boost::lexical_cast<std::string>(value);
151  // std::stringstream ss;
152  // ss << setprecison(std::numeric_limits<double>::max_digits10) << scientific << output_value;
153  }
154 
155 
157  void setAnnotations(const Annotations& anns) {
158  _annotations = anns;
159  }
160 
161 
165  template <typename T>
166  void addAnnotation(const std::string& name, const T& value) {
167  setAnnotation(name, value);
168  }
169 
170 
172  void rmAnnotation(const std::string& name) {
173  _annotations.erase(name);
174  }
175 
176 
179  _annotations.clear();
180  }
181 
183 
184 
186 
187 
190  const std::string title() const {
191  try {
192  return annotation("Title");
193  } catch (AnnotationError& ae) {
194  return "";
195  }
196  }
197 
199  void setTitle(const std::string& title) {
200  setAnnotation("Title", title);
201  }
202 
205  const std::string path() const {
206  try {
207  return annotation("Path");
208  } catch (AnnotationError& ae) {
209  return "";
210  }
211  }
212 
214  void setPath(const std::string& path) {
215  if (path.length() > 0 && path.find("/") != 0) {
216  throw AnnotationError("Histo paths must start with a slash (/) character.");
217  }
218  setAnnotation("Path", path);
219  }
220 
222 
223 
224  public:
225 
227 
228 
230  virtual std::string type() const {
231  return annotation("Type");
232  }
233 
235 
236 
237  private:
238 
240  std::map<std::string,std::string> _annotations;
241 
242  };
243 
244 
245 }
246 
247 #endif // YODA_AnalysisObject_h
void rmAnnotation(const std::string &name)
Delete an annotation by name.
virtual void reset()=0
Reset this analysis object.
virtual AnalysisObject * newclone() const =0
Make a copy on the heap, via 'new'.
AnalysisObject()
Default constructor.
void setAnnotation(const std::string &name, const T &value)
Add or set an annotation by name.
void setPath(const std::string &path)
Set the AO path.
const std::vector< std::string > annotations() const
Get all the annotation names.
bool hasAnnotation(const std::string &name) const
Check if an annotation is defined.
std::map< std::string, std::string > Annotations
Collection type for annotations, as a string-string map.
const std::string & annotation(const std::string &name) const
Get an annotation by name (as a string)
const T annotation(const std::string &name, const T &defaultreturn) const
Get an annotation by name (copied to another type) with a default in case the annotation is not found...
void clearAnnotations()
Delete an annotation by name.
void addAnnotation(const std::string &name, const T &value)
Add or set an annotation by name.
virtual std::string type() const
Get name of the analysis object type, for persistency.
const std::string & annotation(const std::string &name, const std::string &defaultreturn) const
Get an annotation by name (as a string) with a default in case the annotation is not found...
Error for unfound or broken AnalysisObject annotations.
Definition: Exceptions.h:79
AnalysisObject(const std::string &type, const std::string &path, const std::string &title="")
Constructor giving a type, a path and an optional title.
const T annotation(const std::string &name) const
Get an annotation by name (copied to another type)
virtual ~AnalysisObject()
Default destructor.
AnalysisObject is the base class for histograms and scatters.
void setTitle(const std::string &title)
Set the AO title.
const std::string path() const
AnalysisObject(const std::string &type, const std::string &path, const AnalysisObject &ao, const std::string &title="")
Constructor giving a type, a path, another AO to copy annotation from, and an optional title...
const std::string title() const
void setAnnotations(const Annotations &anns)
Set all annotations at once.
virtual AnalysisObject & operator=(const AnalysisObject &ao)
Default copy assignment operator.