Parcourir la source

adds function to easily create variants of a container with different cuts applied

Caleb Fangmeier il y a 7 ans
Parent
commit
d1ff0d903e
3 fichiers modifiés avec 69 ajouts et 57 suppressions
  1. 8 4
      container.hpp
  2. 22 8
      dataset.hpp
  3. 39 45
      filter.hpp

+ 8 - 4
container.hpp

@@ -110,10 +110,12 @@ class GenContainer{
             return name;
         }
 
-        virtual const std::string& get_value_name(){
+        virtual const std::string get_value_name(){
             return "N/A";
         }
 
+        virtual GenContainer* clone_as(const std::string& new_name) = 0;
+
         virtual void save_as(const std::string& fname, const SaveOption& option) = 0;
 
         virtual void save(const SaveOption& option=SaveOption::PNG) {
@@ -143,7 +145,7 @@ class Container : public GenContainer{
             return container;
         }
 
-        virtual const std::string& get_value_name(){
+        virtual const std::string get_value_name(){
             return value->get_name();
         }
 };
@@ -177,11 +179,13 @@ class ContainerMean : public Container<T,T>{
             return (this->container);
         }
 
+        GenContainer* clone_as(const std::string& new_name){
+            return new ContainerMean(new_name, this->value);
+        }
 
-        void save_as(const std::string& fname) {
+        void save_as(const std::string&) {
             WARNING("Saving of ContainerMean objects not supported");
         }
-
 };
 
 }

+ 22 - 8
dataset.hpp

@@ -78,6 +78,14 @@ class DataSet{
             return impl_map;
         }
     public:
+        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);
+        }
+
         void process(bool silent=false){
             int events, current_event;
             summary();
@@ -109,6 +117,20 @@ class DataSet{
             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){
@@ -117,14 +139,6 @@ class DataSet{
             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;
         }

+ 39 - 45
filter.hpp

@@ -49,59 +49,53 @@ class ObsFilter : public DerivedValue<bool>{
             value = filter_function();
         }
 
-        void verify_integrity(){ };
-
     public:
         ObsFilter(const std::string& name, std::function<bool()> filter_function, const std::string& impl="")
           :DerivedValue<bool>(name),
            filter_function(GenFunction::register_function<bool()>("filter::"+name, filter_function, impl)){ }
+};
 
-        /** Return a new filter that is the conjuction of the two source filters.
-         */
-        static ObsFilter* conj(ObsFilter *f1, ObsFilter *f2){
-            auto new_name = f1->get_name() + "&&" + f2->get_name();
-            return new ObsFilter(new_name, [f1,f2](){return f1->get_value() && f2->get_value();});
-        }
-
-        /** Return a new filter that is the conjuction of the two source filters.
-         */
-        static ObsFilter* disj(ObsFilter *f1, ObsFilter *f2){
-            auto new_name = f1->get_name() + "||" + f2->get_name();
-            return new ObsFilter(new_name, [f1, f2](){return f1->get_value() && f2->get_value();});
-        }
+/** Return a new filter that is the conjuction of a vector of source filters
+ */
+ObsFilter* all(const std::vector<ObsFilter*>&& fs){
+    if(fs.size() == 0){
+        return nullptr;
+    } else{
+        std::stringstream ss;
+        ss << fs[0]->get_name();
+        for(size_t i=1; i<fs.size(); i++) ss << "AND" << fs[i]->get_name();
+        return new ObsFilter(ss.str(), [fs](){
+                return std::all_of(std::begin(fs), std::end(fs), [](ObsFilter* f){return f->get_value();});
+        });
+    }
+}
 
-        /** Return a new filter that is the conjuction of the two source filters.
-         */
-        ObsFilter* operator*(ObsFilter *f){
-            auto new_name = this->get_name() + "&&" + f->get_name();
-            return new ObsFilter(new_name, [this, f](){return this->get_value() && f->get_value();});
-        }
+/** Return a new filter that is the disjunction of a vector of source filters
+ */
+ObsFilter* any(const std::vector<ObsFilter*>&& fs){
+    if(fs.size() == 0){
+        return nullptr;
+    } else{
+        std::stringstream ss;
+        ss << fs[0]->get_name();
+        for(size_t i=1; i<fs.size(); i++) ss << "OR" << fs[i]->get_name();
+        return new ObsFilter(ss.str(), [fs](){
+                return std::any_of(std::begin(fs), std::end(fs), [](ObsFilter* f){return f->get_value();});
+        });
+    }
+}
 
-        /** Return a new filter that is the disjunction of the two source
-         * filters.
-         */
-        ObsFilter* operator+(ObsFilter *f){
-            auto new_name = this->get_name() + "||" + f->get_name();
-            return new ObsFilter(new_name, [this, f](){return this->get_value() || f->get_value();});
-        }
+/** Return a new filter that is the conjuction of the two source filters.
+ */
+ObsFilter* all(ObsFilter *f1, ObsFilter *f2){
+    return all({f1, f2});
+}
 
-        /** Return a new filter that is the negation of the source filter.
-         */
-        ObsFilter* operator!(){
-            auto new_name = std::string("!(") + this->get_name() + std::string(")");
-            return new ObsFilter(new_name, [this](){return !this->get_value();});
-        }
-};
+/** Return a new filter that is the conjuction of the two source filters.
+ */
+ObsFilter* any(ObsFilter *f1, ObsFilter *f2){
+    return any({f1, f2});
+}
 
-template <typename T>
-class RangeObsFilter : public ObsFilter{
-    private:
-    public:
-        RangeObsFilter(const std::string name, Value<T>* test_value, T range_low, T range_high):
-          ObsFilter(name, [test_value, range_low, range_high]{
-                  T val = test_value->get_value();
-                  return (val >= range_low) && (val < range_high);
-                  }) { }
-};
 }
 #endif // filter_h