123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #ifndef dataset_hpp
- #define dataset_hpp
- #include <iostream>
- #include "value.hpp"
- #include "container.hpp"
- #include "log.hpp"
- namespace fv{
- typedef std::map<std::string, GenContainer*> ContainerSet;
- class DataSet{
- private:
- void summary(){
- INFO(GenValue::summary());
- INFO(GenFunction::summary());
- }
- Value<int>* current_event_number;
- int max_events;
- protected:
- ContainerSet containers;
- virtual bool load_next() = 0;
- virtual int get_events() = 0;
- virtual int get_current_event() = 0;
- std::map<std::string,std::string> get_container_name_value_map(){
- std::map<std::string, std::string> value_map;
- for(auto container : containers)
- value_map[container.first] = container.second->get_value_name();
- return value_map;
- }
- std::map<std::string,std::string> get_function_name_impl_map(){
- std::map<std::string, std::string> impl_map;
- for(auto fn : GenFunction::function_registry)
- if (fn.second != nullptr){
- impl_map[fn.first] = GenFunction::format_code(fn.second->get_impl());
- }
- return impl_map;
- }
- public:
- DataSet():max_events(0){
- auto& event_check = GenFunction::reg_func<int()>("event_number",
- FUNC(([ds=this](){
- return ds->get_current_event();
- })));
- current_event_number = new BoundValue<int>(event_check);
- }
- void process(bool silent=false){
- int events, current_event;
- summary();
- events = get_events();
- if (max_events > 0) events = max_events;
- if (!silent) std::cout << std::endl;
- while( load_next() ){
- current_event = get_current_event();
- if (!silent) std::cout << "\rprocessing event: " << current_event+1 << "/" << events << std::flush;
- GenValue::reset();
- for(auto con : containers){
- con.second->fill();
- }
- if(max_events && current_event+1 >= max_events) break;
- }
- if (!silent) std::cout << " Finished!" << std::endl;
- }
- void set_max_events(const int& max_events){
- this->max_events = max_events;
- }
- int get_max_events(){
- return this->max_events;
- }
- virtual void save_all(){
- for(auto container : containers)
- container.second->save();
- }
- template<typename C, typename... ArgTypes>
- C* register_container(ArgTypes... args){
- C* container = new C(args...);
- if (containers[container->get_name()] != nullptr){
- CRITICAL("Container with name \""+container->get_name()+"\" already exists.", -1);
- }
- containers[container->get_name()] = container;
- return container;
- }
- void cut_set(GenContainer* base_container, std::vector<std::pair<Value<bool>*, std::string>> filters){
- for(auto p : filters){
- Value<bool>* filter;
- std::string new_name;
- std::tie(filter, new_name) = p;
- if (containers[new_name] != nullptr){
- CRITICAL("Container with name \""<<new_name<<"\" already exists.", -1);
- }
- auto new_container = base_container->clone_as(new_name);
- new_container->add_filter(filter);
- containers[new_container->get_name()] = new_container;
- }
- }
- GenContainer* get_container(std::string container_name){
- GenContainer* c = containers[container_name];
- if (c == nullptr){
- CRITICAL("Request for container \"" << container_name << "\" failed. Doesn't exist.", -1);
- }
- return c;
- }
- Value<int>* get_current_event_number(){
- return current_event_number;
- }
- };
- }
- #endif
|