Belle II Software development
GeneralCut.h
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#pragma once
10
11#include <boost/python.hpp>
12#include <framework/utilities/CutNodes.h>
13#include <framework/utilities/NodeFactory.h>
14
15#include <string>
16#include <memory>
17
18#include <sstream>
19
20#include <iostream>
21#include <stdexcept>
22
23#include <variant>
24
25namespace py = boost::python;
26typedef const py::tuple& Nodetuple;
27
28namespace Belle2 {
70 template <class AVariableManager>
71 class GeneralCut {
73 typedef typename AVariableManager::Object Object;
75 typedef typename AVariableManager::Var Var;
76
77 public:
84 static std::unique_ptr<GeneralCut> compile(const std::string& cut)
85 {
86 // Here we parse
87 Py_Initialize();
88 try {
89 py::object b2parser_namespace = py::import("b2parser");
90 py::tuple tuple = py::extract<py::tuple>(b2parser_namespace.attr("parse")(cut));
91 return std::unique_ptr<GeneralCut>(new GeneralCut(tuple));
92 } catch (py::error_already_set&) {
93 PyErr_Print();
94 B2FATAL("Parsing error on cutstring:\n" + cut);
95 }
96 }
102 bool check(const Object* p) const
103 {
104 if (m_root != nullptr) return m_root->check(p);
105 throw std::runtime_error("GeneralCut m_root is not initialized.");
106 }
107
111 void print() const
112 {
113 m_root->print();
114 }
115
119 std::string decompile() const
120 {
121 std::stringstream stringstream;
122 stringstream << m_root->decompile();
123 return stringstream.str();
124 }
125
126
127 private:
132 explicit GeneralCut(Nodetuple tuple) : m_root{NodeFactory::compile_boolean_node<AVariableManager>(tuple)} {}
133
137 GeneralCut(const GeneralCut&) = delete;
138
142 GeneralCut& operator=(const GeneralCut&) = delete;
143
144 std::unique_ptr<const AbstractBooleanNode<AVariableManager>> m_root;
145 };
147}
This class implements a common way to implement cut/selection functionality for arbitrary objects.
Definition: GeneralCut.h:71
std::unique_ptr< const AbstractBooleanNode< AVariableManager > > m_root
cut root node
Definition: GeneralCut.h:144
AVariableManager::Var Var
Variable returned by the variable manager.
Definition: GeneralCut.h:75
static std::unique_ptr< GeneralCut > compile(const std::string &cut)
Creates an instance of a cut and returns a unique_ptr to it, if you need a copy-able object instead y...
Definition: GeneralCut.h:84
GeneralCut(const GeneralCut &)=delete
Delete Copy constructor.
GeneralCut(Nodetuple tuple)
Constructor of the cut.
Definition: GeneralCut.h:132
void print() const
Print cut tree.
Definition: GeneralCut.h:111
AVariableManager::Object Object
Object, that can be checked. This depends on the VariableManager, as the returned variables from the ...
Definition: GeneralCut.h:73
std::string decompile() const
Do the compilation from a string in return.
Definition: GeneralCut.h:119
GeneralCut & operator=(const GeneralCut &)=delete
Delete assign operator.
bool check(const Object *p) const
Check if the current cuts are passed by the given object.
Definition: GeneralCut.h:102
Wrapper class for static node compile functions.
Definition: NodeFactory.h:64
Abstract base class for different kinds of events.