dataset.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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 <sstream>
  35. #include <functional>
  36. #include <sys/time.h>
  37. #include <signal.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include "value.hpp"
  41. #include "container.hpp"
  42. #include "memtrack.hpp"
  43. #include "config.hpp"
  44. #include "log.hpp"
  45. #include "TFile.h"
  46. namespace fv {
  47. void request_stop_callback(int); // Forward Declaration
  48. class DataSet; // Forward Declaration
  49. DataSet* the_dataset;
  50. /*
  51. * A DataSet is a generic source of data that is used to populate
  52. * ObservedValues. For each ObservedValue, it is recommended that the DataSet
  53. * have a field whose value is updated when the load_next() method is called. A
  54. * pointer to this value is then passed during the creation of the
  55. * ObservedValue. It is important, therefore, that the location in memory of
  56. * the data not change from event to event.
  57. */
  58. class DataSet {
  59. private:
  60. void summary() {
  61. INFO(GenValue::summary());
  62. }
  63. timeval start_time;
  64. bool stop_requested;
  65. bool silent;
  66. bool batch;
  67. void print_status() {
  68. size_t m_used = fv_util::getCurrentRSS() / 1024 / 1024;
  69. timeval curr_time;
  70. gettimeofday(&curr_time, nullptr);
  71. float delta_secs = (curr_time.tv_sec - start_time.tv_sec) + (curr_time.tv_usec - start_time.tv_usec) / 1E6f;
  72. int current_event = get_current_event();
  73. float events_per_second = current_event / delta_secs;
  74. int secs_remaining = int((get_events() - current_event) / events_per_second);
  75. std::stringstream time_remaining;
  76. if (secs_remaining > 3600.) {
  77. int hours = secs_remaining / 3600;
  78. secs_remaining %= 3600;
  79. time_remaining << hours << "H ";
  80. }
  81. if (secs_remaining > 60) {
  82. int minutes = secs_remaining / 60;
  83. secs_remaining %= 60;
  84. time_remaining << minutes << "M ";
  85. }
  86. if (secs_remaining > 0) {
  87. time_remaining << secs_remaining << "S";
  88. }
  89. if (batch) {
  90. std::cout << "\n";
  91. } else {
  92. std::cout << "\r";
  93. }
  94. std::string filename = get_current_file().filename;
  95. if (filename.size() > 80) {
  96. std::stringstream ss;
  97. ss << filename.substr(0,27) << "..." << filename.substr(filename.size() - 50);
  98. filename = ss.str();
  99. }
  100. std::cout << "processing event: " << current_event + 1 << "/" << get_events()
  101. << " of file: " << filename
  102. << ", " << m_used << "MB used "
  103. << ", " << time_remaining.str() << " est. time remaining"
  104. << std::flush;
  105. }
  106. protected:
  107. std::map<std::string, GenContainer *> containers;
  108. long max_events;
  109. virtual int get_events() = 0;
  110. virtual int get_current_event() = 0;
  111. virtual fv_util::DataFileDescriptor &get_current_file() = 0;
  112. std::map<std::string, std::string> get_container_name_value_map() {
  113. std::map<std::string, std::string> value_map;
  114. for (auto container : containers)
  115. value_map[container.first] = container.second->get_value_name();
  116. return value_map;
  117. }
  118. virtual bool load_next() = 0;
  119. virtual void save_config() = 0;
  120. public:
  121. DataSet() : stop_requested(false){
  122. using fv_util::the_config;
  123. max_events = the_config->get_max_events();
  124. the_dataset = this;
  125. silent = the_config->get("silent").as<bool>(false);
  126. batch = the_config->get("batch").as<bool>(false);
  127. signal(SIGINT, request_stop_callback);
  128. signal(SIGTERM, request_stop_callback);
  129. }
  130. bool next(bool verbose=true) {
  131. if (stop_requested) return false;
  132. int current_event = get_current_event();
  133. if (current_event == 0) gettimeofday(&start_time, nullptr);
  134. if (!silent and (((current_event + 1) % 500) == 0 or current_event+1 == get_events())) print_status();
  135. GenValue::reset();
  136. return load_next();
  137. }
  138. void set_max_events(const int &max_events) {
  139. this->max_events = max_events;
  140. }
  141. int get_max_events() {
  142. return this->max_events;
  143. }
  144. void request_stop() {
  145. stop_requested = true;
  146. }
  147. virtual void save_all() {
  148. for (auto container : containers)
  149. container.second->save();
  150. save_config();
  151. }
  152. template<typename C, typename... ArgTypes>
  153. C *register_container(ArgTypes... args) {
  154. C *container = new C(args...);
  155. if (containers[container->get_name()] != nullptr) {
  156. CRITICAL("Container with name \"" + container->get_name() + "\" already exists.");
  157. }
  158. containers[container->get_name()] = container;
  159. return container;
  160. }
  161. GenContainer *get_container(std::string container_name) {
  162. GenContainer *c = containers[container_name];
  163. if (c == nullptr) {
  164. CRITICAL("Request for container \"" << container_name << "\" failed. Doesn't exist.");
  165. }
  166. return c;
  167. }
  168. };
  169. void request_stop_callback(int) {
  170. std::cout << std::endl << "SIGINT/SIGTERM caught, stopping after current event" << std::endl;
  171. the_dataset->request_stop();
  172. }
  173. }
  174. #endif // dataset_hpp