TTTT Analysis  0.1
dataset.hpp
Go to the documentation of this file.
1 
31 #ifndef dataset_hpp
32 #define dataset_hpp
33 #include <iostream>
34 #include "value.hpp"
35 #include "container.hpp"
36 #include "log.hpp"
37 
38 namespace fv{
39 
40 typedef std::map<std::string, GenContainer*> ContainerSet;
41 
42 /*
43  * A DataSet is a generic source of data that is used to populate
44  * ObservedValues. For each ObservedValue, it is recommened 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  INFO(GenFunction::summary());
55  }
56 
57  Value<int>* current_event_number;
58 
59  protected:
60  ContainerSet containers;
61  virtual bool load_next() = 0;
62  virtual int get_events() = 0;
63  virtual int get_current_event() = 0;
64 
65  std::map<std::string,std::string> get_container_name_value_map(){
66  std::map<std::string, std::string> value_map;
67  for(auto container : containers)
68  value_map[container.first] = container.second->get_value_name();
69  return value_map;
70  }
71 
72  std::map<std::string,std::string> get_function_name_impl_map(){
73  std::map<std::string, std::string> impl_map;
74  for(auto fn : GenFunction::function_registry)
75  if (fn.second != nullptr){
76  impl_map[fn.first] = GenFunction::format_code(fn.second->get_impl());
77  }
78  return impl_map;
79  }
80  public:
81  DataSet(){
82  auto& event_check = GenFunction::register_function<int()>("event_number",
83  FUNC(([ds=this](){
84  return ds->get_current_event();
85  })));
86  current_event_number = new BoundValue<int>(event_check);
87  }
88 
89  void process(bool silent=false){
90  int events, current_event;
91  summary();
92  events = get_events();
93  if (!silent) std::cout << std::endl;
94  while( load_next() ){
95  current_event = get_current_event();
96  if (!silent) std::cout << "\rprocessing event: " << current_event+1 << "/" << events << std::flush;
97  GenValue::reset();
98  for(auto con : containers){
99  con.second->fill();
100  }
101  }
102  if (!silent) std::cout << " Finished!" << std::endl;
103  }
104 
105  virtual void save_all(){
106  for(auto container : containers)
107  container.second->save();
108  }
109 
110  template<typename C, typename... ArgTypes>
111  C* register_container(ArgTypes... args){
112  C* container = new C(args...);
113  if (containers[container->get_name()] != nullptr){
114  CRITICAL("Container with name \""+container->get_name()+"\" already exists.", -1);
115  }
116  containers[container->get_name()] = container;
117  return container;
118  }
119 
120  void cut_set(GenContainer* base_container, std::vector<std::pair<Value<bool>*, std::string>> filters){
121  for(auto p : filters){
122  Value<bool>* filter;
123  std::string new_name;
124  std::tie(filter, new_name) = p;
125  if (containers[new_name] != nullptr){
126  CRITICAL("Container with name \""<<new_name<<"\" already exists.", -1);
127  }
128  auto new_container = base_container->clone_as(new_name);
129  new_container->add_filter(filter);
130  containers[new_container->get_name()] = new_container;
131  }
132  }
133 
134  GenContainer* get_container(std::string container_name){
135  GenContainer* c = containers[container_name];
136  if (c == nullptr){
137  CRITICAL("Request for container \"" << container_name << "\" failed. Doesn't exist.", -1);
138  }
139  return c;
140  }
141 
142  Value<int>* get_current_event_number(){
143  return current_event_number;
144  }
145 };
146 }
147 #endif // dataset_hpp
The namespace containing all filval classes and functions.
Definition: api.hpp:46
static std::string format_code(const std::string &code)
Attempt to invoke clang-format for the purpose of printing out nicely formatted functions to the log ...
Definition: value.hpp:172
static std::map< const std::string, GenFunction * > function_registry
Static mapping of functions from their name to the object wrapper of the function.
Definition: value.hpp:151