dataset.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 dataset_hpp
  32. #define dataset_hpp
  33. #include <iostream>
  34. #include "value.hpp"
  35. #include "container.hpp"
  36. #include "memtrack.hpp"
  37. #include "datafile.hpp"
  38. #include "log.hpp"
  39. namespace fv{
  40. /*
  41. * A DataSet is a generic source of data that is used to populate
  42. * ObservedValues. For each ObservedValue, it is recommened that the DataSet
  43. * have a field whose value is updated when the load_next() method is called. A
  44. * pointer to this value is then passed during the creation of the
  45. * ObservedValue. It is important, therefore, that the location in memory of
  46. * the data not change from event to event.
  47. */
  48. class DataSet{
  49. private:
  50. void summary(){
  51. INFO(GenValue::summary());
  52. INFO(GenFunction::summary());
  53. }
  54. Value<int>* current_event_number;
  55. int max_events;
  56. protected:
  57. std::map<std::string, GenContainer*> containers;
  58. virtual bool load_next() = 0;
  59. virtual int get_events() = 0;
  60. virtual int get_current_event() = 0;
  61. virtual util::DataFileDescriptor& get_current_file() = 0;
  62. std::map<std::string,std::string> get_container_name_value_map(){
  63. std::map<std::string, std::string> value_map;
  64. for(auto container : containers)
  65. value_map[container.first] = container.second->get_value_name();
  66. return value_map;
  67. }
  68. std::map<std::string,std::string> get_function_name_impl_map(){
  69. std::map<std::string, std::string> impl_map;
  70. for(auto fn : GenFunction::function_registry)
  71. if (fn.second != nullptr){
  72. impl_map[fn.first] = GenFunction::format_code(fn.second->get_impl());
  73. }
  74. return impl_map;
  75. }
  76. public:
  77. DataSet():max_events(0){
  78. auto event_check = GenFunction::reg_func<int()>("event_number",
  79. FUNC(([ds=this](){
  80. return ds->get_current_event();
  81. })));
  82. current_event_number = new BoundValue<int>(event_check);
  83. }
  84. void process(bool silent=false){
  85. int events, current_event;
  86. summary();
  87. events = get_events();
  88. if (max_events > 0) events = max_events;
  89. if (!silent) std::cout << std::endl;
  90. while( load_next() ){
  91. current_event = get_current_event();
  92. if (!silent){
  93. size_t m_used = getCurrentRSS() / 1024 / 1024;
  94. size_t m_peak = getPeakRSS() / 1024 / 1024;
  95. std::cout << "\rprocessing event: " << current_event+1 << "/" << events
  96. << ", " << m_used << "/" << m_peak << "MB used/peak"
  97. << " of file: " << get_current_file().filename << std::flush;
  98. }
  99. GenValue::reset();
  100. for(auto con : containers){
  101. DEBUG("Filling container " << con.first << ".");
  102. con.second->fill();
  103. }
  104. if(max_events && current_event+1 >= max_events) break;
  105. }
  106. if (!silent) std::cout << " Finished!" << std::endl;
  107. }
  108. void set_max_events(const int& max_events){
  109. this->max_events = max_events;
  110. }
  111. int get_max_events(){
  112. return this->max_events;
  113. }
  114. virtual void save_all(){
  115. for(auto container : containers)
  116. container.second->save();
  117. }
  118. template<typename C, typename... ArgTypes>
  119. C* register_container(ArgTypes... args){
  120. C* container = new C(args...);
  121. if (containers[container->get_name()] != nullptr){
  122. CRITICAL("Container with name \""+container->get_name()+"\" already exists.", -1);
  123. }
  124. containers[container->get_name()] = container;
  125. return container;
  126. }
  127. void cut_set(GenContainer* base_container, std::vector<std::pair<Value<bool>*, std::string>> filters){
  128. for(auto p : filters){
  129. Value<bool>* filter;
  130. std::string new_name;
  131. std::tie(filter, new_name) = p;
  132. if (containers[new_name] != nullptr){
  133. CRITICAL("Container with name \""<<new_name<<"\" already exists.", -1);
  134. }
  135. auto new_container = base_container->clone_as(new_name);
  136. new_container->add_filter(filter);
  137. containers[new_container->get_name()] = new_container;
  138. }
  139. }
  140. GenContainer* get_container(std::string container_name){
  141. GenContainer* c = containers[container_name];
  142. if (c == nullptr){
  143. CRITICAL("Request for container \"" << container_name << "\" failed. Doesn't exist.", -1);
  144. }
  145. return c;
  146. }
  147. Value<int>* get_current_event_number(){
  148. return current_event_number;
  149. }
  150. };
  151. }
  152. #endif // dataset_hpp