dataset.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "log.hpp"
  37. namespace fv{
  38. typedef std::map<std::string, GenContainer*> ContainerSet;
  39. /*
  40. * A DataSet is a generic source of data that is used to populate
  41. * ObservedValues. For each ObservedValue, it is recommened that the DataSet
  42. * have a field whose value is updated when the load_next() method is called. A
  43. * pointer to this value is then passed during the creation of the
  44. * ObservedValue. It is important, therefore, that the location in memory of
  45. * the data not change from event to event.
  46. */
  47. class DataSet{
  48. private:
  49. void summary(){
  50. INFO(GenValue::summary());
  51. INFO(GenFunction::summary());
  52. }
  53. protected:
  54. ContainerSet containers;
  55. virtual bool load_next() = 0;
  56. virtual int get_events() = 0;
  57. virtual int get_current_event() = 0;
  58. public:
  59. void process(bool silent=false){
  60. int events, current_event;
  61. summary();
  62. events = get_events();
  63. if (!silent) std::cout << std::endl;
  64. while( load_next() ){
  65. current_event = get_current_event();
  66. if (!silent) std::cout << "\rprocessing event: " << current_event+1 << "/" << events << std::flush;
  67. GenValue::reset();
  68. for(auto con : containers){
  69. con.second->fill();
  70. }
  71. }
  72. if (!silent) std::cout << " Finished!" << std::endl;
  73. }
  74. virtual void save_all(){
  75. for(auto container : containers)
  76. container.second->save();
  77. }
  78. GenContainer* register_container(GenContainer *container){
  79. if (containers[container->get_name()] != nullptr){
  80. CRITICAL("Container with name \""+container->get_name()+"\" already exists.", -1);
  81. }
  82. containers[container->get_name()] = container;
  83. return container;
  84. }
  85. GenContainer* get_container(std::string container_name){
  86. GenContainer* c = containers[container_name];
  87. if (c == nullptr){
  88. CRITICAL("Request for container \"" << container_name << "\" failed. Doesn't exist.", -1);
  89. }
  90. return c;
  91. }
  92. };
  93. }
  94. #endif // dataset_hpp