container.hpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /**
  2. * @file
  3. * @author Caleb Fangmeier <caleb@fangmeier.tech>
  4. * @version 0.1
  5. *
  6. * @section LICENSE
  7. *
  8. *
  9. * MIT License
  10. *
  11. * Copyright (c) 2017 Caleb Fangmeier
  12. *
  13. * Permission is hereby granted, free of charge, to any person obtaining a copy
  14. * of this software and associated documentation files (the "Software"), to deal
  15. * in the Software without restriction, including without limitation the rights
  16. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  17. * copies of the Software, and to permit persons to whom the Software is
  18. * furnished to do so, subject to the following conditions:
  19. *
  20. * The above copyright notice and this permission notice shall be included in all
  21. * copies or substantial portions of the Software.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  24. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  26. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  28. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  29. * SOFTWARE.
  30. */
  31. #ifndef container_hpp
  32. #define container_hpp
  33. #include <typeindex>
  34. #include <vector>
  35. #include <map>
  36. #include "value.hpp"
  37. #include "filter.hpp"
  38. template class std::vector<std::vector<float> >;
  39. template class std::vector<std::vector<int> >;
  40. namespace fv::util{
  41. std::string get_type_name(const std::type_index& index){
  42. std::map<std::type_index, std::string> _map;
  43. // Add to this list as needed :)
  44. _map[typeid(int)]="int";
  45. _map[typeid(unsigned int)]="unsigned int";
  46. _map[typeid(float)]="float";
  47. _map[typeid(double)]="double";
  48. _map[typeid(std::vector<int>)]="std::vector<int>";
  49. _map[typeid(std::vector<float>)]="std::vector<float>";
  50. _map[typeid(std::pair<std::vector<float>,std::vector<float>>)]="std::pair<std::vector<float>,std::vector<float>>";
  51. _map[typeid(std::vector<std::pair<float,int>>)]="std::vector<std::pair<float,int>>";
  52. _map[typeid(std::map<std::string,std::string>)] = "std::map<std::string,std::string>";
  53. if (_map[index] == ""){
  54. CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
  55. }
  56. return _map[index];
  57. }
  58. }
  59. namespace fv{
  60. /**
  61. * Enumeration of different options that can be used to save Containers. Not
  62. * all options are allowed for all Containers.
  63. */
  64. enum SaveOption{
  65. PNG = 0,
  66. PDF = 1,
  67. ROOT = 2
  68. };
  69. /**
  70. * Generic, untyped parent class of Container. Used to allow for placing
  71. * Containers of disparate types in common data structures.
  72. */
  73. class GenContainer{
  74. private:
  75. std::string name;
  76. std::string desc;
  77. std::vector<ObsFilter*> filters;
  78. protected:
  79. virtual void _fill() = 0;
  80. public:
  81. GenContainer(const std::string name, const std::string& desc)
  82. :name(name),desc(desc) { }
  83. GenContainer(const std::string name)
  84. :GenContainer(name,"N/A"){ }
  85. GenContainer* add_filter(GenValue* filter){
  86. filters.push_back(static_cast<ObsFilter*>(filter));
  87. return this;
  88. }
  89. void fill(){
  90. for (auto filter : filters){
  91. if (!filter->get_value()) return;
  92. }
  93. _fill();
  94. }
  95. void set_description(const std::string& description){
  96. desc = description;
  97. }
  98. const std::string& get_name(){
  99. return name;
  100. }
  101. virtual const std::string get_value_name(){
  102. return "N/A";
  103. }
  104. virtual GenContainer* clone_as(const std::string& new_name) = 0;
  105. virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
  106. virtual void save(const SaveOption& option=SaveOption::PNG) {
  107. save_as(get_name(), option);
  108. }
  109. };
  110. /**
  111. * A class that is used to "hold" values. When an event is loaded, the
  112. * associated filters on this container are checked. If they all pass, the
  113. * \c _fill() method is called and the Container can access the stored Value
  114. * object to process it. For example, if the Container is a ROOT Histogram
  115. * object, it may call <tt>container->Fill(value->get_value())</tt>.
  116. */
  117. template <typename H, typename V>
  118. class Container : public GenContainer{
  119. protected:
  120. H* container;
  121. Value<V> *value;
  122. public:
  123. Container(const std::string& name, Value<V>* value)
  124. :GenContainer(name),
  125. container(nullptr),
  126. value(value){ }
  127. virtual ~Container(){
  128. if (container != nullptr) delete container;
  129. }
  130. virtual H* get_container(){
  131. return container;
  132. }
  133. virtual const std::string get_value_name(){
  134. return value->get_name();
  135. }
  136. };
  137. /**
  138. * Calculate the Mean of a Value over a series of observations. This class is
  139. * given a value of some type that supports addition and division(eg. a \c
  140. * float) and yields the mean value. Note that this implementation does \i not
  141. * support serialization so it is not incredibly useful. See the ROOT
  142. * Containers for Containers that support serialization using ROOT's
  143. * facilities.
  144. */
  145. template <typename T>
  146. class ContainerMean : public Container<T,T>{
  147. private:
  148. int count;
  149. T sum;
  150. void _fill(){
  151. count++;
  152. sum += this->value->get_value();
  153. }
  154. public:
  155. ContainerMean(const std::string& name, Value<T>* value)
  156. :Container<std::vector<T>,T>(name, value){
  157. this->container = new T();
  158. }
  159. T* get_container(){
  160. *(this->container) = sum/count;
  161. return (this->container);
  162. }
  163. GenContainer* clone_as(const std::string& new_name){
  164. return new ContainerMean(new_name, this->value);
  165. }
  166. void save_as(const std::string&) {
  167. WARNING("Saving of ContainerMean objects not supported");
  168. }
  169. };
  170. template<typename T>
  171. std::ostream& operator<< (std::ostream & out, const std::vector<T>& data) {
  172. size_t len = data.size();
  173. if (len == 0) out << "[]";
  174. else {
  175. out << "[" << data[0];
  176. for (size_t i=1; i<len; i++) out << ", " << data[i];
  177. out << "]";
  178. }
  179. return out ;
  180. }
  181. /**
  182. * Prints out the value registered to it
  183. */
  184. template <typename T>
  185. class Logger : public Container<std::ostream, T>{
  186. private:
  187. using to_str_t = std::function<void(T, std::ostream&)>;
  188. to_str_t value_to_string;
  189. static void default_to_string(T& t, std::ostream& os) {
  190. os << t;
  191. }
  192. void _fill(){
  193. auto& os = (*this->container);
  194. std::stringstream vs;
  195. os << this->get_name() << std::endl;
  196. if (value_to_string != nullptr) value_to_string(this->value->get_value(), vs);
  197. else default_to_string(this->value->get_value(), vs);
  198. for (const char& c : vs.str()) {
  199. if (c == '\n') os << std::endl << " ";
  200. else os << c;
  201. }
  202. os << std::endl;
  203. }
  204. public:
  205. Logger(Value<T>* value, std::ostream* out, to_str_t value_to_string = nullptr)
  206. :Container<std::ostream,T>("Logger:" + value->get_name(), value),
  207. value_to_string(value_to_string) {
  208. this->container = out;
  209. }
  210. std::ostream* get_container(){
  211. return this->container;
  212. }
  213. GenContainer* clone_as(const std::string& new_name){
  214. return new Logger(this->value, this->container, value_to_string);
  215. }
  216. void save_as(const std::string&, const SaveOption&) { }
  217. };
  218. /**
  219. * Prints out the value registered to it
  220. */
  221. class FuncLogger : public GenContainer {
  222. private:
  223. std::ostream* os;
  224. using to_str_t = std::function<void(std::ostream&)>;
  225. to_str_t value_to_string;
  226. void _fill(){
  227. auto& os_ref = (*this->os);
  228. std::stringstream vs;
  229. os_ref << this->get_name() << std::endl << " ";
  230. value_to_string(vs);
  231. for (const char& c : vs.str()) {
  232. if (c == '\n') os_ref << std::endl << " ";
  233. else os_ref << c;
  234. }
  235. os_ref << std::endl;
  236. }
  237. public:
  238. FuncLogger(const std::string& name, std::ostream* os, to_str_t value_to_string)
  239. :GenContainer("Logger:" + name),
  240. value_to_string(value_to_string),
  241. os(os) { }
  242. std::ostream* get_container(){
  243. return os;
  244. }
  245. GenContainer* clone_as(const std::string& new_name){
  246. return new FuncLogger(new_name, this->os, value_to_string);
  247. }
  248. void save_as(const std::string&, const SaveOption&) { }
  249. };
  250. }
  251. #endif // container_hpp