AmpGen 2.1
Loading...
Searching...
No Matches
ExpressionParser.h
Go to the documentation of this file.
1#ifndef AMPGEN_EXPRESSIONPARSER_H
2#define AMPGEN_EXPRESSIONPARSER_H 1
3
4#include <complex>
5#include <functional>
6#include <map>
7#include <string>
8#include <utility>
9#include <vector>
10
11#include "AmpGen/Expression.h"
12#include "AmpGen/MetaUtils.h"
13#include "AmpGen/Types.h"
14#include "AmpGen/enum.h"
15#include "AmpGen/MsgService.h"
16
17namespace AmpGen
18{
19 class ASTResolver;
20 class MinuitParameter;
22
23 declare_enum(coordinateType, cartesian, polar)
24 declare_enum(angType, deg, rad)
25
26 template <typename arg_type> void set_tuple_from_expression( arg_type& arg, const Expression& expr )
27 {
28 if constexpr( std::is_same<arg_type, Expression>::value ){ arg = expr ; }
29 else if constexpr( std::is_same<arg_type, std::string>::value ){ arg = is<Parameter>(expr) ? cast<Parameter>(expr).name() : "ERROR"; }
30 else if constexpr( std::is_same<arg_type, int>::value ){ arg = int(std::real(expr())); }
31 else if constexpr( std::is_same<arg_type, double>::value ){ arg = double( std::real(expr())); }
32 else if constexpr( std::is_same<arg_type, std::complex<double>>::value ){ arg = expr(); }
33 else FATAL( "Unrecognised type: " << type_string<arg_type> () );
34 }
35
36 template <typename... types> std::tuple< types... > unpack( const Expression& expression )
37 {
38 std::tuple<types...> rt;
39 unsigned int counter = 0;
40 if( ! is<ExpressionPack>( expression ) ) FATAL("Not a packed expression" );
41 auto as_pack = cast<ExpressionPack>(expression).expressions();
42 if( as_pack.size() != std::tuple_size<std::tuple<types...>>::value ) FATAL("Wrong number of expressions");
43 for_each( rt, [&as_pack, &counter](auto& element)
44 {
45 set_tuple_from_expression(element, as_pack[counter] );
46 counter++;
47 } );
48 return rt;
49 }
50
51 class ExpressionParser
52 {
53 public:
54 typedef std::function<Expression( const Expression& )> unaryFCN;
55 typedef std::function<Expression( const Expression&, const Expression& )> binaryFCN;
56
57 void add_unary( const std::string& name, const unaryFCN& op ){ m_unaryFunctions[name] = op; }
58 template <typename OP> void add_unary( const std::string& name )
59 {
60 add_unary(name, [](const auto& expression){ return OP(expression) ; } );
61 }
62 template<typename... args> void add_multi_arg( const std::string& name, const std::function<Expression(args...)>& fcn )
63 {
64 add_unary(name, [fcn](const auto& expression){ return std::apply(fcn, unpack<args...>(expression) ) ;} );
65 }
66
67 void add_binary( const std::string& name, binaryFCN op ) { m_binaryFunctions.emplace_back( name, op ); }
68
69 static Expression parse( const std::string& str, const MinuitParameterSet* mps=nullptr );
70 static Expression parse(std::vector<std::string>::const_iterator begin,
71 std::vector<std::string>::const_iterator end ,
72 const MinuitParameterSet* mps = nullptr );
73 static ExpressionParser* gExpressionParser;
74 static ExpressionParser* getMe()
75 {
76 if ( !gExpressionParser ) gExpressionParser = new ExpressionParser();
77 return gExpressionParser;
78 }
79 private:
81
82 Expression processEndPoint( const std::string& name, const MinuitParameterSet* mps = nullptr );
83 Expression parseTokens( std::vector<std::string>::const_iterator begin, std::vector<std::string>::const_iterator end , const MinuitParameterSet* mps = nullptr );
84
85 void processBinaryOperators( std::vector<std::string>& opCodes, std::vector<Expression>& expressions );
86 void processUnaryOperators( std::vector<std::string>& opCodes, std::vector<Expression>& expressions );
87
88 std::map<std::string, unaryFCN> m_unaryFunctions;
89 std::vector<std::pair<std::string, binaryFCN>> m_binaryFunctions;
90 bool m_isCartesian = {true};
91 double m_sf = {1};
92
93 };
94} // namespace AmpGen
95
96#endif
(Internal) class to aide in the resolution of the dependencies of expression trees.
Definition ASTResolver.h:29
Wrapper class for shared_ptrs to virtual expressions for use in conjunction with operators to build e...
Definition Expression.h:135
void add_unary(const std::string &name, const unaryFCN &op)
void add_unary(const std::string &name)
static ExpressionParser * getMe()
void add_multi_arg(const std::string &name, const std::function< Expression(args...)> &fcn)
static Expression parse(std::vector< std::string >::const_iterator begin, std::vector< std::string >::const_iterator end, const MinuitParameterSet *mps=nullptr)
static Expression parse(const std::string &str, const MinuitParameterSet *mps=nullptr)
std::function< Expression(const Expression &)> unaryFCN
std::function< Expression(const Expression &, const Expression &)> binaryFCN
void add_binary(const std::string &name, binaryFCN op)
static ExpressionParser * gExpressionParser
#define declare_enum(name,...)
Definition enum.h:7
#define FATAL(X)
Used for printing fatal errors messages, and will always be printed and will terminate the process af...
Definition MsgService.h:87
bool is(const Expression &expression)
Definition Expression.h:494
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:18
rad void set_tuple_from_expression(arg_type &arg, const Expression &expr)
T cast(const Expression &expression)
Definition Expression.h:497
std::tuple< types... > unpack(const Expression &expression)
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:39
declare_enum(coordinateType, cartesian, polar) declare_enum(angType