ra4_draw  4bd0201e3d922d42bd545d4b045ed44db33454a4
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
NamedFunc Class Reference

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
 
NamedFuncoperator= (const NamedFunc &)=default
 
 NamedFunc (NamedFunc &&)=default
 
NamedFuncoperator= (NamedFunc &&)=default
 
 ~NamedFunc ()=default
 
const std::string & Name () const
 Get the string representation of this function. More...
 
NamedFuncName (const std::string &name)
 Set the string representation of this function. More...
 
std::string PlainName () const
 
std::string PrettyName () const
 
NamedFuncFunction (const std::function< ScalarFunc > &function)
 Set function to given scalar function. More...
 
NamedFuncFunction (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...
 
NamedFuncoperator+= (const NamedFunc &func)
 Add func to *this. More...
 
NamedFuncoperator-= (const NamedFunc &func)
 Subtract func from *this. More...
 
NamedFuncoperator*= (const NamedFunc &func)
 Multiply *this by func. More...
 
NamedFuncoperator/= (const NamedFunc &func)
 Divide *this by func. More...
 
NamedFuncoperator%= (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< ScalarFuncscalar_func_
 
std::function< VectorFuncvector_func_
 

Detailed Description

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.

See also
FunctionParser for allowed expression syntax for constructing a NamedFunc.

Definition at line 13 of file named_func.hpp.

Member Typedef Documentation

Definition at line 17 of file named_func.hpp.

using NamedFunc::ScalarType = double

Definition at line 15 of file named_func.hpp.

Definition at line 18 of file named_func.hpp.

using NamedFunc::VectorType = std::vector<ScalarType>

Definition at line 16 of file named_func.hpp.

Constructor & Destructor Documentation

NamedFunc::NamedFunc ( const std::string &  name,
const std::function< ScalarFunc > &  function 
)

Constructor of a scalar NamedFunc.

Parameters
[in]nameText representation of function
[in]functionFunctor 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.

Parameters
[in]nameText representation of function
[in]functionFunctor 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.

Parameters
[in]functionC++/"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.

Parameters
[in]functionC++/"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.

Parameters
[in]functionC++/"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.

Parameters
[in]xThe constant to be returned

Definition at line 366 of file named_func.cpp.

References vector_func_.

NamedFunc::NamedFunc ( const NamedFunc )
default
NamedFunc::NamedFunc ( NamedFunc &&  )
default
NamedFunc::~NamedFunc ( )
default
NamedFunc::NamedFunc ( )
privatedelete

Referenced by operator[]().

Member Function Documentation

void NamedFunc::CleanName ( )
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.

Parameters
[in]fValid function taking a Baby and returning a scalar
Returns
Reference to *this

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.

Parameters
[in]fValid function taking a Baby and returning a vector
Returns
Reference to *this

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.

Parameters
[in]bBaby to pass to scalar function
Returns
Result of applying scalar function to b

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.

Parameters
[in]bBaby to pass to vector function
Returns
Result of applying vector function to b

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.

Returns
True if scalar function is valid; false otherwise.

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.

Returns
True if vector function is valid; false otherwise.

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.

Returns
The standard 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.

Parameters
[in]nameString representation of the function

Definition at line 384 of file named_func.cpp.

References CleanName(), and name_.

NamedFunc & NamedFunc::operator%= ( const NamedFunc func)

Set *this to remainder of *this divided by func.

Parameters
[in]funcFunction with respect to which to take remainder
Returns
Reference to *this

Definition at line 544 of file named_func.cpp.

References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.

NamedFunc & NamedFunc::operator*= ( const NamedFunc func)

Multiply *this by func.

Parameters
[in]funcFunction by which *this is multiplied
Returns
Reference to *this

Definition at line 512 of file named_func.cpp.

References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.

NamedFunc & NamedFunc::operator+= ( const NamedFunc func)

Add func to *this.

Parameters
[in]funcFunction to be added to *this
Returns
Reference to *this

Definition at line 480 of file named_func.cpp.

References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.

NamedFunc & NamedFunc::operator-= ( const NamedFunc func)

Subtract func from *this.

Parameters
[in]funcFunction to be subtracted from *this
Returns
Reference to *this

Definition at line 496 of file named_func.cpp.

References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.

NamedFunc & NamedFunc::operator/= ( const NamedFunc func)

Divide *this by func.

Parameters
[in]funcFunction by which to divide *this
Returns
Reference to *this

Definition at line 528 of file named_func.cpp.

References anonymous_namespace{named_func.cpp}::ApplyOp(), name_, scalar_func_, and vector_func_.

NamedFunc& NamedFunc::operator= ( const NamedFunc )
default
NamedFunc& NamedFunc::operator= ( NamedFunc &&  )
default
NamedFunc NamedFunc::operator[] ( const NamedFunc func) const

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.

Returns
The (possibly invalid) scalar function associated to *this

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.

Returns
The (possibly invalid) vector function associated to *this

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||().

Member Data Documentation

std::string NamedFunc::name_
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/=().

std::function<ScalarFunc> NamedFunc::scalar_func_
private
std::function<VectorFunc> NamedFunc::vector_func_
private

The documentation for this class was generated from the following files: