dataset.hpp 5.6 KB

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