dataset.hpp
Go to the documentation of this file.
#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;
/*
* A DataSet is a generic source of data that is used to populate
* ObservedValues. For each ObservedValue, it is recommened that the DataSet
* have a field whose value is updated when the load_next() method is called. A
* pointer to this value is then passed during the creation of the
* ObservedValue. It is important, therefore, that the location in memory of
* the data not change from event to event.
*/
class DataSet{
private:
void summary(){
INFO(GenValue::summary());
INFO(GenFunction::summary());
}
Value<int>* current_event_number;
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:
void process(bool silent=false){
int events, current_event;
summary();
events = get_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 (!silent) std::cout << " Finished!" << std::endl;
}
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;
}
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;
}
DataSet(){
auto& event_check = GenFunction::register_function<int()>("event_number",
FUNC(([ds=this](){
return ds->get_current_event();
})));
current_event_number = new BoundValue<int>(event_check);
}
Value<int>* get_current_event_number(){
return current_event_number;
}
};
}
#endif // dataset_hpp
fv
The namespace containing all filval classes and functions.
Definition: api.hpp:46
fv::GenFunction::format_code
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
log.hpp
fv::GenFunction::function_registry
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