value.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. * @section DESCRIPTION
  32. * This header defines a set of generic classes that wrap up "values". In
  33. * essence, a Value<T> object is just something that contains a value of type T
  34. * and can provide it when requested. The usefulness stems from composing
  35. * values together with calculations. This enables very clear dependency
  36. * mapping and a way to know clearly how every value was arrived at. This could
  37. * be used to, for example, automatically generate commentary for plots that
  38. * explain the exect calculation used to create it. Or easily making a series
  39. * of plots contrasting different values that have been composed slightly
  40. * differently.
  41. */
  42. #ifndef value_hpp
  43. #define value_hpp
  44. #include <algorithm>
  45. #include <functional>
  46. #include <initializer_list>
  47. #include <iomanip>
  48. #include <iostream>
  49. #include <limits>
  50. #include <map>
  51. #include <sstream>
  52. #include <tuple>
  53. #include <typeindex>
  54. #include <utility>
  55. #include <vector>
  56. #include "log.hpp"
  57. #include "config.hpp"
  58. /**
  59. * The namespace containing all filval classes and functions.
  60. */
  61. namespace fv {
  62. class GenValue; // Forward declaration
  63. template<typename T>
  64. class Value; // Forward declaration
  65. /**
  66. * A static mapping containing all created Value objects.
  67. * Every value object must have a unique name, and this name is used as
  68. * a key in values to that object. This is used to enable more dynamic
  69. * creation of objects as well as avoiding the uneccesary passing of
  70. * pointers.
  71. */
  72. std::map<std::pair<const std::type_index, const std::string>, GenValue *> values;
  73. /**
  74. * A type-agnostic value.
  75. * It is necessary to create a type-agnostic parent class to Value so that
  76. * it is possible to handle collections of them. GenValue also provides the
  77. * rest of the type-independent interface to Value.
  78. */
  79. class GenValue {
  80. private:
  81. /**
  82. * The name of the value.
  83. * This is used to allow for dynamic lookup of
  84. * values based on their name via GenValue::get_value.
  85. */
  86. std::string name;
  87. protected:
  88. /**
  89. * Mark the internal value as invalid. This is needed for DerivedValue
  90. * to force a recalculation of the internal value when a new
  91. * observation is loaded into memory. It is called automatically for
  92. * all GenValue objects when reset is called.
  93. */
  94. bool value_valid;
  95. void _reset() {
  96. this->value_valid = false;
  97. }
  98. public:
  99. GenValue(const std::type_index &&ti, const std::string &name)
  100. : name(name), value_valid(false) {
  101. INFO("Registered value: \"" << name);
  102. values[std::make_pair(ti, name)] = this;
  103. }
  104. const std::string &get_name() {
  105. return name;
  106. }
  107. static void reset() {
  108. for (auto val : values) {
  109. if (val.second != nullptr) {
  110. val.second->_reset();
  111. }
  112. }
  113. }
  114. template<typename T>
  115. static Value<T> *get_value(const std::string &name) {
  116. const std::type_index &ti = typeid(T);
  117. auto lookup_id = std::make_pair(ti, name);
  118. return (Value<T> *) values[lookup_id];
  119. }
  120. static std::string summary() {
  121. std::stringstream ss;
  122. ss << "The following values have been created:" << std::endl;
  123. for (auto item : values) {
  124. auto &key = item.first;
  125. auto &value = item.second;
  126. if (value == nullptr) continue;
  127. ss << "\tVALUE::\"" << key.second << "\" at address " << value << std::endl;
  128. }
  129. return ss.str();
  130. }
  131. friend std::ostream &operator<<(std::ostream &os, const GenValue &gv);
  132. };
  133. std::ostream &operator<<(std::ostream &os, GenValue &gv) {
  134. os << gv.get_name();
  135. return os;
  136. }
  137. /**
  138. * A templated value.
  139. * In order to facilitate run-time creation of analysis routines, it is
  140. * necessary to have some ability to get and store *values*. Values can either
  141. * be directly taken from some original data source (i.e. ObservedValue), or
  142. * they can be a function of some other set of values (i.e. DerivedValue). They
  143. * template class T of Value<T> is the type of thing that is returned upon
  144. * calling get_value().
  145. */
  146. template<typename T>
  147. class Value : public GenValue {
  148. public:
  149. Value(const std::string &name)
  150. : GenValue(typeid(T), name) {}
  151. /** Calculate, if necessary, and return the value held by this object.
  152. */
  153. virtual T &get() = 0;
  154. /** Alias to get
  155. */
  156. virtual T &operator()() = 0;
  157. };
  158. /**
  159. * A value supplied by the dataset, not derived.
  160. * An ObservedValue is the interface to your dataset. Upon creation, an
  161. * ObservedValue is given a pointer to an object of type T. When an observation
  162. * is loaded into memory, the value at the location referenced by that pointer
  163. * must be updated with the associated data from that observation. This is the
  164. * responsibility of whatever DataSet implementation is being used. This object
  165. * then will read that data and return it when requested.
  166. */
  167. template<typename T>
  168. class ObservedValue : public Value<T> {
  169. private:
  170. T *val_ref;
  171. public:
  172. ObservedValue(const std::string &name, T *val_ref)
  173. : Value<T>(name), val_ref(val_ref) {}
  174. static std::string fmt_name(const std::string &name) {
  175. return name;
  176. }
  177. T &get() {
  178. if (!this->value_valid) {
  179. if (fv_util::debug_on) {
  180. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  181. }
  182. this->value_valid = true;
  183. }
  184. return *val_ref;
  185. }
  186. T &operator()() {
  187. return this->get();
  188. }
  189. };
  190. /**
  191. * A Value derived from some other Values, not directly from the dataset.
  192. * A DerivedValue is generally defined as some function of other Value objects.
  193. * For example, a Pair is a function of two other Value objects that makes a
  194. * pair of them. Note that these other Value objects are free to be either
  195. * ObservedValues or other DerivedValues.
  196. *
  197. * It is desireable from a performance standpoint that each DerivedValue be
  198. * calculated no more than once per observation. Therefore, when a get_value is
  199. * called on a DerivedValue, it first checks whether the value that it holds is
  200. * **valid**, meaning it has already been calculated for this observation. If
  201. * so, it simply returns the value. If not, the update_value function is called
  202. * to calculate the value. and then the newly calculated value is marked as
  203. * valid and returned.
  204. */
  205. template<typename T>
  206. class DerivedValue : public Value<T> {
  207. protected:
  208. T value;
  209. /**
  210. * Updates the internal value.
  211. * This function should be overridden by any child class to do the
  212. * actual work of updating value based on whatever rules the class
  213. * chooses. Normally, this consists of geting the values from some
  214. * associated Value objects, doing some calculation on them, and
  215. * storing the result in value.
  216. */
  217. virtual void update_value() = 0;
  218. public:
  219. DerivedValue(const std::string &name)
  220. : Value<T>(name) {}
  221. T &get() {
  222. if (!this->value_valid) {
  223. if (fv_util::debug_on) {
  224. std::cout << "Calculating Value: " << this->get_name() << std::endl;
  225. }
  226. update_value();
  227. this->value_valid = true;
  228. }
  229. return value;
  230. }
  231. T &operator()() {
  232. return this->get();
  233. }
  234. };
  235. /**
  236. * A Value of a pointer. The pointer is constant, however the data the pointer
  237. * points to is variable.
  238. */
  239. template<typename T>
  240. class PointerValue : public DerivedValue<T *> {
  241. protected:
  242. void update_value() {}
  243. public:
  244. PointerValue(const std::string &name, T *ptr)
  245. : DerivedValue<T *>(name) {
  246. this->value = ptr;
  247. }
  248. };
  249. /**
  250. * Used for when the value is an object whose location in memory is changing,
  251. * but one has a pointer to a pointer that points to the always updated
  252. * location. (Mainly when reading objects such as stl containers from a root
  253. * TTree)
  254. */
  255. template<typename T>
  256. class ObjectValue : public DerivedValue<T> {
  257. protected:
  258. T **obj_pointer;
  259. void update_value() {
  260. this->value = **obj_pointer;
  261. }
  262. public:
  263. ObjectValue(const std::string &name, T **ptr)
  264. : DerivedValue<T>(name),
  265. obj_pointer(ptr) {}
  266. };
  267. /**
  268. * A Value which always returns the same value, supplied in the constructor.
  269. */
  270. template<typename T>
  271. class ConstantValue : public DerivedValue<T> {
  272. protected:
  273. void update_value() {}
  274. public:
  275. static std::string fmt_name(const std::string &name) {
  276. return "const::" + name;
  277. }
  278. ConstantValue(const std::string &name, T const_value)
  279. : DerivedValue<T>(fmt_name(name)) {
  280. this->value = const_value;
  281. }
  282. };
  283. }
  284. #endif // value_hpp