TTTT Analysis  0.1
container.hpp
1 #ifndef container_hpp
2 #define container_hpp
3 #include <typeindex>
4 #include <vector>
5 #include <map>
6 
7 #include "value.hpp"
8 #include "filter.hpp"
9 
10 template class std::vector<std::vector<float> >;
11 template class std::vector<std::vector<int> >;
12 
13 namespace fv::util{
14 std::string get_type_name(const std::type_index& index){
15  std::map<std::type_index, std::string> _map;
16  // Add to this list as needed :)
17  _map[typeid(int)]="int";
18  _map[typeid(unsigned int)]="unsigned int";
19  _map[typeid(float)]="float";
20  _map[typeid(double)]="double";
21  _map[typeid(std::vector<int>)]="std::vector<int>";
22  _map[typeid(std::vector<float>)]="std::vector<float>";
23 
24  if (_map[index] == ""){
25  CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
26  }
27  return _map[index];
28 }
29 }
30 
31 namespace fv{
32 
33 enum SaveOption{
34  PNG = 0,
35  PDF = 1,
36  ROOT = 2
37 };
38 
40  private:
41  std::string name;
42  std::string desc;
43  std::vector<Filter*> filters;
44  protected:
45  virtual void _fill() = 0;
46  public:
47  GenContainer(const std::string name, const std::string& desc)
48  :name(name),desc(desc) { }
49 
50  GenContainer(const std::string name)
51  :GenContainer(name,"N/A"){ }
52 
53  void add_filter(GenValue* filter){
54  filters.push_back(dynamic_cast<Filter*>(filter));
55  }
56 
57  void fill(){
58  for (auto filter : filters){
59  if (!filter->get_value()) return;
60  }
61  _fill();
62  }
63 
64  void set_description(const std::string& description){
65  desc = description;
66  }
67  const std::string& get_name(){
68  return name;
69  }
70  virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
71  virtual void save(const SaveOption& option=SaveOption::PNG) {
72  save_as(get_name(), option);
73  }
74 };
75 typedef std::map<std::string, GenContainer*> ContainerSet;
76 
77 template <typename H>
78 class Container : public GenContainer{
79  protected:
80  H* container;
81  public:
82  Container(const std::string& name, H* container)
83  :GenContainer(name),
84  container(container){ }
85  virtual H* get_container(){
86  return container;
87  }
88 
89 };
90 
91 template <typename T>
92 class ContainerMean : public Container<T>{
93  private:
94  Value<T>* value;
95  int count;
96  T sum;
97 
98  void _fill(){
99  count++;
100  sum += value->get_value();
101  }
102  public:
103  ContainerMean(const std::string& name, Value<T>* value)
104  :Container<std::vector<T> >(name, nullptr),
105  value(value){
106  this->container = new T();
107  }
108 
109  T* get_container(){
110  *(this->container) = sum/count;
111  return (this->container);
112  }
113  void save_as(const std::string& fname) {
114  WARNING("Saving of ContainerMean objects not supported");
115  }
116 };
117 
118 }
119 #endif // container_hpp
Definition: value.hpp:213
Definition: argparse.hpp:39
Definition: container.hpp:92
The namespace containing all filval classes and functions.
Definition: api.hpp:6
A generic value.
Definition: value.hpp:338
Definition: container.hpp:39
virtual T & get_value()=0
Calculate, if necessary, and return the value held by this object.
Definition: container.hpp:78