dataset.hpp 6.2 KB

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