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
44 namespace fv::util{
45 std::string get_type_name(const std::type_index& index){
46 std::map<std::type_index, std::string> _map;
47 // Add to this list as needed :)
48 _map[typeid(int)]="int";
49 _map[typeid(unsigned int)]="unsigned int";
50 _map[typeid(float)]="float";
51 _map[typeid(double)]="double";
52 _map[typeid(std::vector<int>)]="std::vector<int>";
53 _map[typeid(std::vector<float>)]="std::vector<float>";
54 _map[typeid(std::map<std::string,std::string>)] = "std::map<std::string,std::string>";
55
56 if (_map[index] == ""){
57 CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
58 }
59 return _map[index];
60 }
61 }
62
63 namespace fv{
64
69 enum SaveOption{
70 PNG = 0,
71 PDF = 1,
72 ROOT = 2
73 };
74
79 class GenContainer{
80 private:
81 std::string name;
82 std::string desc;
83 std::vector<ObsFilter*> filters;
84 protected:
85 virtual void _fill() = 0;
86 public:
87 GenContainer(const std::string name, const std::string& desc)
88 :name(name),desc(desc) { }
89
90 GenContainer(const std::string name)
91 :GenContainer(name,"N/A"){ }
92
93 GenContainer* add_filter(GenValue* filter){
94 filters.push_back(dynamic_cast<ObsFilter*>(filter));
95 return this;
96 }
97
98 void fill(){
99 for (auto filter : filters){
100 if (!filter->get_value()) return;
101 }
102 _fill();
103 }
104
105 void set_description(const std::string& description){
106 desc = description;
107 }
108
109 const std::string& get_name(){
110 return name;
111 }
112
113 virtual const std::string& get_value_name(){
114 return "N/A";
115 }
116
117 virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
118
119 virtual void save(const SaveOption& option=SaveOption::PNG) {
120 save_as(get_name(), option);
121 }
122 };
123
131 template <typename H, typename V>
132 class Container : public GenContainer{
133 protected:
134 H* container;
135 Value<V> *value;
136 public:
137 Container(const std::string& name, Value<V>* value)
138 :GenContainer(name),
139 container(nullptr),
140 value(value){ }
141
142 virtual H* get_container(){
143 return container;
144 }
145
146 virtual const std::string& get_value_name(){
147 return value->get_name();
148 }
149 };
150
159 template <typename T>
160 class ContainerMean : public Container<T,T>{
161 private:
162 int count;
163 T sum;
164
165 void _fill(){
166 count++;
167 sum += this->value->get_value();
168 }
169 public:
170 ContainerMean(const std::string& name, Value<T>* value)
171 :Container<std::vector<T>,T>(name, value){
172 this->container = new T();
173 }
174
175 T* get_container(){
176 *(this->container) = sum/count;
177 return (this->container);
178 }
179
180
181 void save_as(const std::string& fname) {
182 WARNING("Saving of ContainerMean objects not supported");
183 }
184
185 };
186
187 }
188 #endif // container_hpp
fv::util
Definition: argparse.hpp:39
fv::SaveOption
SaveOption
Enumeration of different options that can be used to save Containers.
Definition: container.hpp:69
fv::ContainerMean
Calculate the Mean of a Value over a series of observations.
Definition: container.hpp:160
fv
The namespace containing all filval classes and functions.
Definition: api.hpp:38
fv::Value< V >
fv::GenContainer
Generic, untyped parent class of Container.
Definition: container.hpp:79
fv::Container
A class that is used to "hold" values.
Definition: container.hpp:132