ra4_draw
4bd0201e3d922d42bd545d4b045ed44db33454a4
|
Combines a callable function taking a Baby and returning a scalar or vector with its string representation. More...
#include <named_func.hpp>
Public Types | |
using | ScalarType = double |
using | VectorType = std::vector< ScalarType > |
using | ScalarFunc = ScalarType(const Baby &) |
using | VectorFunc = VectorType(const Baby &) |
Public Member Functions | |
NamedFunc (const std::string &name, const std::function< ScalarFunc > &function) | |
Constructor of a scalar NamedFunc. More... | |
NamedFunc (const std::string &name, const std::function< VectorFunc > &function) | |
Constructor of a vector NamedFunc. More... | |
NamedFunc (const std::string &function) | |
Constructor using FunctionParser to produce a real function from a string. More... | |
NamedFunc (const char *function) | |
Constructor using FunctionParser to produce a real function from a string. More... | |
NamedFunc (const TString &function) | |
Constructor using FunctionParser to produce a real function from a string. More... | |
NamedFunc (ScalarType x) | |
Constructor for NamedFunc returning a constant. More... | |
NamedFunc (const NamedFunc &)=default | |
NamedFunc & | operator= (const NamedFunc &)=default |
NamedFunc (NamedFunc &&)=default | |
NamedFunc & | operator= (NamedFunc &&)=default |
~NamedFunc ()=default | |
const std::string & | Name () const |
Get the string representation of this function. More... | |
NamedFunc & | Name (const std::string &name) |
Set the string representation of this function. More... | |
std::string | PlainName () const |
std::string | PrettyName () const |
NamedFunc & | Function (const std::function< ScalarFunc > &function) |
Set function to given scalar function. More... | |
NamedFunc & | Function (const std::function< VectorFunc > &function) |
Set function to given vector function. More... | |
const std::function< ScalarFunc > & | ScalarFunction () const |
Return the (possibly invalid) scalar function. More... | |
const std::function< VectorFunc > & | VectorFunction () const |
Return the (possibly invalid) vector function. More... | |
bool | IsScalar () const |
Check if scalar function is valid. More... | |
bool | IsVector () const |
Check if vectorr function is valid. More... | |
ScalarType | GetScalar (const Baby &b) const |
Evaluate scalar function with b as argument. More... | |
VectorType | GetVector (const Baby &b) const |
Evaluate vector function with b as argument. More... | |
NamedFunc & | operator+= (const NamedFunc &func) |
Add func to *this. More... | |
NamedFunc & | operator-= (const NamedFunc &func) |
Subtract func from *this. More... | |
NamedFunc & | operator*= (const NamedFunc &func) |
Multiply *this by func. More... | |
NamedFunc & | operator/= (const NamedFunc &func) |
Divide *this by func. More... | |
NamedFunc & | operator%= (const NamedFunc &func) |
Set *this to remainder of *this divided by func. More... | |
NamedFunc | operator[] (const NamedFunc &func) const |
Apply indexing operator and return result as a NamedFunc. More... | |
Private Member Functions | |
NamedFunc ()=delete | |
void | CleanName () |
Strip spaces from name. More... | |
Private Attributes | |
std::string | name_ |
String representation of the function. More... | |
std::function< ScalarFunc > | scalar_func_ |
std::function< VectorFunc > | vector_func_ |
Combines a callable function taking a Baby and returning a scalar or vector with its string representation.
NamedFunc contains both a callabale function which takes a Baby as a parameter and returns either a scalar or a vector result. It also contains a string representation of that function. Typically, this is a C++/"TTree::Draw"-like expression, but can be manually set to any desired string independently of the callable function. Given only a C++ expression, it is able to dynamically generate the appropriate callable and fully compiled function. The string parsing is done just once by FunctionParser, and the resulting callable function stored for fast calling without reinterpreting the string each time.
NamedFuncs can be manipulated in much the same way as C++ arithmetic types. For example, given NamedFuncs x and y, x+y will return a NamedFunc z whose function evaluates the functions of x and y and returns the sum of the results. It is important to note that z's function does not simply return the result it obtains by evaluating x and y on construction. Rather, it remembers the functions from x and y, and reevaluates the component addends and resulting sum each time z is called. This allows construction of arbitrarily complicated functions by applying standard C++ operators to simple NamedFuncs. This ability is used heavily by FunctionParser to build a single NamedFunc from complex expressions. Currently, the operators "+" (unary and binary), "-" (unary and binary), "*", "/", "%", "+=", "-=", "*=", "/=", "%=", "==", "!=", ">", "<", ">=", "<=", "&&", "||", and "!" are supported. Bit-level operators "<<", ">>", "~", "^", "^=", "&=", and "|=" and not supported. The "<<" is used for printing to an output stream.
The current implementation keeps both a scalar and vector function internally, only one of which is valid at any time. To the scalar function is evaluated with NamedFunc::GetScalar(), while the vector function is evaluated with NamedFunc::GetVector(). A possible alternative is to always return a vector (of length 1 in the case of a scalar result), and use a bool to determine if the result should be considered a scalar syntactically. This would allow NamedFunc to act as a true functor with a cleaner interface, but results in extra vectors being constructed (and often copied if care is not taken with results) even when evaluating a simple scalar value.
Definition at line 13 of file named_func.hpp.
using NamedFunc::ScalarFunc = ScalarType(const Baby &) |
Definition at line 17 of file named_func.hpp.
using NamedFunc::ScalarType = double |
Definition at line 15 of file named_func.hpp.
using NamedFunc::VectorFunc = VectorType(const Baby &) |
Definition at line 18 of file named_func.hpp.
using NamedFunc::VectorType = std::vector<ScalarType> |
Definition at line 16 of file named_func.hpp.
NamedFunc::NamedFunc | ( | const std::string & | name, |
const std::function< ScalarFunc > & | function | ||
) |
Constructor of a scalar NamedFunc.
[in] | name | Text representation of function |
[in] | function | Functor taking a Baby and returning a scalar |
Definition at line 310 of file named_func.cpp.
References CleanName().
NamedFunc::NamedFunc | ( | const std::string & | name, |
const std::function< VectorFunc > & | function | ||
) |
Constructor of a vector NamedFunc.
[in] | name | Text representation of function |
[in] | function | Functor taking a Baby and returning a vector |
Definition at line 324 of file named_func.cpp.
References CleanName().
NamedFunc::NamedFunc | ( | const std::string & | function | ) |
Constructor using FunctionParser to produce a real function from a string.
[in] | function | C++/"TTree::Draw"-like expression containing constants, Baby variables, operators, parenthesis, brackets, etc. |
Definition at line 338 of file named_func.cpp.
NamedFunc::NamedFunc | ( | const char * | function | ) |
Constructor using FunctionParser to produce a real function from a string.
[in] | function | C++/"TTree::Draw"-like expression containing constants, Baby variables, operators, parenthesis, brackets, etc. |
Definition at line 348 of file named_func.cpp.
NamedFunc::NamedFunc | ( | const TString & | function | ) |
Constructor using FunctionParser to produce a real function from a string.
[in] | function | C++/"TTree::Draw"-like expression containing constants, Baby variables, operators, parenthesis, brackets, etc. |
Definition at line 358 of file named_func.cpp.
NamedFunc::NamedFunc | ( | ScalarType | x | ) |
Constructor for NamedFunc returning a constant.
[in] | x | The constant to be returned |
Definition at line 366 of file named_func.cpp.
References vector_func_.
|
default |
|
default |
|
default |
|
privatedelete |
Referenced by operator[]().
|
private |
Strip spaces from name.
Definition at line 568 of file named_func.cpp.
References name_, and ReplaceAll().
Referenced by Name(), and NamedFunc().
NamedFunc & NamedFunc::Function | ( | const std::function< ScalarFunc > & | f | ) |
Set function to given scalar function.
This function overwrites the scalar function and invalidates the vector function if set.
[in] | f | Valid function taking a Baby and returning a scalar |
Definition at line 399 of file named_func.cpp.
References scalar_func_, and vector_func_.
Referenced by operator!(), operator!=(), operator&&(), operator-(), operator<(), operator<=(), operator==(), operator>(), operator>=(), and operator||().
NamedFunc & NamedFunc::Function | ( | const std::function< VectorFunc > & | f | ) |
Set function to given vector function.
This function overwrites the vector function and invalidates the scalar function if set.
[in] | f | Valid function taking a Baby and returning a vector |
Definition at line 415 of file named_func.cpp.
References scalar_func_, and vector_func_.
ScalarType NamedFunc::GetScalar | ( | const Baby & | b | ) | const |
Evaluate scalar function with b as argument.
[in] | b | Baby to pass to scalar function |
Definition at line 460 of file named_func.cpp.
References scalar_func_.
Referenced by EventScan::SingleScan::RecordEvent(), Table::TableColumn::RecordEvent(), Hist2D::SingleHist2D::RecordEvent(), and Hist1D::SingleHist1D::RecordEvent().
VectorType NamedFunc::GetVector | ( | const Baby & | b | ) | const |
Evaluate vector function with b as argument.
[in] | b | Baby to pass to vector function |
Definition at line 470 of file named_func.cpp.
References vector_func_.
Referenced by EventScan::SingleScan::RecordEvent(), Table::TableColumn::RecordEvent(), Hist2D::SingleHist2D::RecordEvent(), and Hist1D::SingleHist1D::RecordEvent().
bool NamedFunc::IsScalar | ( | ) | const |
Check if scalar function is valid.
Definition at line 442 of file named_func.cpp.
References scalar_func_.
Referenced by operator[](), EventScan::SingleScan::RecordEvent(), Table::TableColumn::RecordEvent(), Hist2D::SingleHist2D::RecordEvent(), and Hist1D::SingleHist1D::RecordEvent().
bool NamedFunc::IsVector | ( | ) | const |
Check if vectorr function is valid.
Definition at line 450 of file named_func.cpp.
References vector_func_.
Referenced by operator[](), EventScan::SingleScan::RecordEvent(), Hist2D::SingleHist2D::RecordEvent(), and Hist1D::SingleHist1D::RecordEvent().
const string & NamedFunc::Name | ( | ) | const |
Get the string representation of this function.
Definition at line 376 of file named_func.cpp.
References name_.
Referenced by main(), FunctionParser::MergeParentheses(), Hist2D::Name(), Hist1D::Name(), operator!(), operator!=(), operator&&(), operator+(), operator-(), operator<(), operator<=(), operator==(), operator>(), operator>=(), operator[](), operator||(), and Hist1D::Title().
NamedFunc & NamedFunc::Name | ( | const std::string & | name | ) |
Set the string representation of this function.
[in] | name | String representation of the function |
Definition at line 384 of file named_func.cpp.
References CleanName(), and name_.
Set *this to remainder of *this divided by func.
[in] | func | Function with respect to which to take remainder |
Definition at line 544 of file named_func.cpp.
References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.
Multiply *this by func.
[in] | func | Function by which *this is multiplied |
Definition at line 512 of file named_func.cpp.
References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.
Add func to *this.
[in] | func | Function to be added to *this |
Definition at line 480 of file named_func.cpp.
References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.
Subtract func from *this.
[in] | func | Function to be subtracted from *this |
Definition at line 496 of file named_func.cpp.
References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.
Divide *this by func.
[in] | func | Function by which to divide *this |
Definition at line 528 of file named_func.cpp.
References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.
Apply indexing operator and return result as a NamedFunc.
Definition at line 556 of file named_func.cpp.
References ERROR, IsScalar(), IsVector(), Name(), NamedFunc(), ScalarFunction(), and VectorFunction().
std::string NamedFunc::PlainName | ( | ) | const |
Referenced by main().
std::string NamedFunc::PrettyName | ( | ) | const |
const function< ScalarFunc > & NamedFunc::ScalarFunction | ( | ) | const |
Return the (possibly invalid) scalar function.
Definition at line 426 of file named_func.cpp.
References scalar_func_.
Referenced by FunctionParser::ApplySubscripts(), operator!(), operator!=(), operator&&(), operator-(), operator<(), operator<=(), operator==(), operator>(), operator>=(), operator[](), and operator||().
const function< VectorFunc > & NamedFunc::VectorFunction | ( | ) | const |
Return the (possibly invalid) vector function.
Definition at line 434 of file named_func.cpp.
References vector_func_.
Referenced by FunctionParser::ApplySubscripts(), operator!(), operator!=(), operator&&(), operator-(), operator<(), operator<=(), operator==(), operator>(), operator>=(), operator[](), and operator||().
|
private |
String representation of the function.
Definition at line 60 of file named_func.hpp.
Referenced by CleanName(), Name(), operator%=(), operator*=(), operator+=(), operator-=(), and operator/=().
|
private |
Definition at line 61 of file named_func.hpp.
Referenced by Function(), GetScalar(), IsScalar(), operator%=(), operator*=(), operator+=(), operator-=(), operator/=(), and ScalarFunction().
|
private |
Definition at line 62 of file named_func.hpp.
Referenced by Function(), GetVector(), IsVector(), NamedFunc(), operator%=(), operator*=(), operator+=(), operator-=(), operator/=(), and VectorFunction().