container.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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::map<std::string,std::string>)] = "std::map<std::string,std::string>";
  51. if (_map[index] == ""){
  52. CRITICAL("Cannot lookup type name of \"" << index.name() << "\"",-1);
  53. }
  54. return _map[index];
  55. }
  56. }
  57. namespace fv{
  58. /**
  59. * Enumeration of different options that can be used to save Containers. Not
  60. * all options are allowed for all Containers.
  61. */
  62. enum SaveOption{
  63. PNG = 0,
  64. PDF = 1,
  65. ROOT = 2
  66. };
  67. /**
  68. * Generic, untyped parent class of Container. Used to allow for placing
  69. * Containers of disparate types in common data structures.
  70. */
  71. class GenContainer{
  72. private:
  73. std::string name;
  74. std::string desc;
  75. std::vector<ObsFilter*> filters;
  76. protected:
  77. virtual void _fill() = 0;
  78. public:
  79. GenContainer(const std::string name, const std::string& desc)
  80. :name(name),desc(desc) { }
  81. GenContainer(const std::string name)
  82. :GenContainer(name,"N/A"){ }
  83. GenContainer* add_filter(GenValue* filter){
  84. filters.push_back(dynamic_cast<ObsFilter*>(filter));
  85. return this;
  86. }
  87. void fill(){
  88. for (auto filter : filters){
  89. if (!filter->get_value()) return;
  90. }
  91. _fill();
  92. }
  93. void set_description(const std::string& description){
  94. desc = description;
  95. }
  96. const std::string& get_name(){
  97. return name;
  98. }
  99. virtual const std::string get_value_name(){
  100. return "N/A";
  101. }
  102. virtual GenContainer* clone_as(const std::string& new_name) = 0;
  103. virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
  104. virtual void save(const SaveOption& option=SaveOption::PNG) {
  105. save_as(get_name(), option);
  106. }
  107. };
  108. /**
  109. * A class that is used to "hold" values. When an event is loaded, the
  110. * associated filters on this container are checked. If they all pass, the
  111. * \c _fill() method is called and the Container can access the stored Value
  112. * object to process it. For example, if the Container is a ROOT Histogram
  113. * object, it may call <tt>container->Fill(value->get_value())</tt>.
  114. */
  115. template <typename H, typename V>
  116. class Container : public GenContainer{
  117. protected:
  118. H* container;
  119. Value<V> *value;
  120. public:
  121. Container(const std::string& name, Value<V>* value)
  122. :GenContainer(name),
  123. container(nullptr),
  124. value(value){ }
  125. virtual H* get_container(){
  126. return container;
  127. }
  128. virtual const std::string get_value_name(){
  129. return value->get_name();
  130. }
  131. };
  132. /**
  133. * Calculate the Mean of a Value over a series of observations. This class is
  134. * given a value of some type that supports addition and division(eg. a \c
  135. * float) and yields the mean value. Note that this implementation does \i not
  136. * support serialization so it is not incredibly useful. See the ROOT
  137. * Containers for Containers that support serialization using ROOT's
  138. * facilities.
  139. */
  140. template <typename T>
  141. class ContainerMean : public Container<T,T>{
  142. private:
  143. int count;
  144. T sum;
  145. void _fill(){
  146. count++;
  147. sum += this->value->get_value();
  148. }
  149. public:
  150. ContainerMean(const std::string& name, Value<T>* value)
  151. :Container<std::vector<T>,T>(name, value){
  152. this->container = new T();
  153. }
  154. T* get_container(){
  155. *(this->container) = sum/count;
  156. return (this->container);
  157. }
  158. GenContainer* clone_as(const std::string& new_name){
  159. return new ContainerMean(new_name, this->value);
  160. }
  161. void save_as(const std::string&) {
  162. WARNING("Saving of ContainerMean objects not supported");
  163. }
  164. };
  165. }
  166. #endif // container_hpp