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<Filter*> 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  void add_filter(GenValue* filter){
92  filters.push_back(dynamic_cast<Filter*>(filter));
93  }
94 
95  void fill(){
96  for (auto filter : filters){
97  if (!filter->get_value()) return;
98  }
99  _fill();
100  }
101 
102  void set_description(const std::string& description){
103  desc = description;
104  }
105  const std::string& get_name(){
106  return name;
107  }
108  virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
109  virtual void save(const SaveOption& option=SaveOption::PNG) {
110  save_as(get_name(), option);
111  }
112 };
113 
121 template <typename H>
122 class Container : public GenContainer{
123  protected:
124  H* container;
125  public:
126  Container(const std::string& name, H* container)
127  :GenContainer(name),
128  container(container){ }
129  virtual H* get_container(){
130  return container;
131  }
132 
133 };
134 
143 template <typename T>
144 class ContainerMean : public Container<T>{
145  private:
146  Value<T>* value;
147  int count;
148  T sum;
149 
150  void _fill(){
151  count++;
152  sum += value->get_value();
153  }
154  public:
155  ContainerMean(const std::string& name, Value<T>* value)
156  :Container<std::vector<T> >(name, nullptr),
157  value(value){
158  this->container = new T();
159  }
160 
161  T* get_container(){
162  *(this->container) = sum/count;
163  return (this->container);
164  }
165  void save_as(const std::string& fname) {
166  WARNING("Saving of ContainerMean objects not supported");
167  }
168 };
169 
170 }
171 #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:144
The namespace containing all filval classes and functions.
Definition: api.hpp:6
A generic value.
Definition: value.hpp:339
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:122