TTTT Analysis  0.1
container.hpp
Go to the documentation of this file.
1 
31 #ifndef container_hpp
32 #define container_hpp
33 #include <typeindex>
34 #include <vector>
35 #include <map>
36 
37 #include "value.hpp"
38 #include "filter.hpp"
39 
40 template class std::vector<std::vector<float> >;
41 template class std::vector<std::vector<int> >;
42 
43 namespace fv::util{
44 std::string get_type_name(const std::type_index& index){
45  std::map<std::type_index, std::string> _map;
46  // Add to this list as needed :)
47  _map[typeid(int)]="int";
48  _map[typeid(unsigned int)]="unsigned int";
49  _map[typeid(float)]="float";
50  _map[typeid(double)]="double";
51  _map[typeid(std::vector<int>)]="std::vector<int>";
52  _map[typeid(std::vector<float>)]="std::vector<float>";
53 
54  if (_map[index] == ""){
55  CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
56  }
57  return _map[index];
58 }
59 }
60 
61 namespace fv{
62 
68  PNG = 0,
69  PDF = 1,
70  ROOT = 2
71 };
72 
78  private:
79  std::string name;
80  std::string desc;
81  std::vector<ObsFilter*> filters;
82  protected:
83  virtual void _fill() = 0;
84  public:
85  GenContainer(const std::string name, const std::string& desc)
86  :name(name),desc(desc) { }
87 
88  GenContainer(const std::string name)
89  :GenContainer(name,"N/A"){ }
90 
91  GenContainer* add_filter(GenValue* filter){
92  filters.push_back(dynamic_cast<ObsFilter*>(filter));
93  return this;
94  }
95 
96  void fill(){
97  for (auto filter : filters){
98  if (!filter->get_value()) return;
99  }
100  _fill();
101  }
102 
103  void set_description(const std::string& description){
104  desc = description;
105  }
106  const std::string& get_name(){
107  return name;
108  }
109  virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
110  virtual void save(const SaveOption& option=SaveOption::PNG) {
111  save_as(get_name(), option);
112  }
113 };
114 
122 template <typename H>
123 class Container : public GenContainer{
124  protected:
125  H* container;
126  public:
127  Container(const std::string& name, H* container)
128  :GenContainer(name),
129  container(container){ }
130  virtual H* get_container(){
131  return container;
132  }
133 
134 };
135 
144 template <typename T>
145 class ContainerMean : public Container<T>{
146  private:
147  Value<T>* value;
148  int count;
149  T sum;
150 
151  void _fill(){
152  count++;
153  sum += value->get_value();
154  }
155  public:
156  ContainerMean(const std::string& name, Value<T>* value)
157  :Container<std::vector<T> >(name, nullptr),
158  value(value){
159  this->container = new T();
160  }
161 
162  T* get_container(){
163  *(this->container) = sum/count;
164  return (this->container);
165  }
166  void save_as(const std::string& fname) {
167  WARNING("Saving of ContainerMean objects not supported");
168  }
169 };
170 
171 }
172 #endif // container_hpp
Definition: argparse.hpp:39
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:67
Calculate the Mean of a Value over a series of observations.
Definition: container.hpp:145
The namespace containing all filval classes and functions.
Definition: api.hpp:6
A generic value.
Definition: value.hpp:352
Generic, untyped parent class of Container.
Definition: container.hpp:77
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
A class that is used to "hold" values.
Definition: container.hpp:123