mupp 1.1.0
Loading...
Searching...
No Matches
PVarHandler Class Reference

The PVarHandler class handles variable parsing, evaluation, and data management. More...

#include <PVarHandler.h>

Collaboration diagram for PVarHandler:

Public Member Functions

 PVarHandler ()
 Default constructor.
 
 PVarHandler (PmuppCollection *coll, std::string parse_str, std::string var_name="", const std::vector< PmuppCollection * > &allColl=std::vector< PmuppCollection * >())
 Constructor that parses and evaluates a variable expression.
 
bool isValid ()
 Checks if the parsing and evaluation were successful.
 
QString getCollName ()
 Gets the collection name.
 
QString getVarName ()
 Gets the variable name.
 
std::vector< double > getValues ()
 Gets the computed values for the variable.
 
std::vector< double > getErrors ()
 Gets the computed errors for the variable.
 

Private Member Functions

void injectPredefVariables ()
 Injects predefined variables from the collection into the AST.
 
void evaluatePython ()
 Evaluates the variable using an embedded Python3 interpreter (TPython).
 
std::string getVarName (int idx)
 Gets the variable name at a specific parameter index.
 
std::vector< double > getData (int idx)
 Gets the data values for a specific parameter across all runs.
 
std::vector< double > getDataErr (int idx)
 Gets the error values for a specific parameter across all runs.
 

Private Attributes

PmuppCollectionfColl
 pointer to collection containing run data needed for parsing and evaluation
 
std::vector< PmuppCollection * > fAllColl
 all loaded collections (handler-index order); Python path only, exposed as coll[]/collErr[]
 
std::string fParseStr
 the variable input string to be parsed
 
std::string fVarName
 name of the variable to extract from evaluation results
 
mupp::prog::PVarHandler fVar
 variable handler storing the computed values and errors
 
bool fIsValid
 flag indicating whether parsing and evaluation succeeded
 
mupp::ast::statement_list fAst
 Abstract Syntax Tree generated from parsing.
 

Detailed Description

The PVarHandler class handles variable parsing, evaluation, and data management.

This class provides the main interface for parsing variable definition strings, evaluating expressions using the Boost.Spirit parser framework, and managing the resulting computed values and errors. It integrates with PmuppCollection to access run data and inject predefined variables for use in expressions.

The variable syntax supports:

  • Variable declarations: var <name> = <expression>
  • Identifiers (prefixed with '$'): $varName
  • Mathematical functions: sin, cos, tan, exp, log, sqrt, pow, etc.
  • Arithmetic operations: +, -, *, /
  • Error variables (suffixed with 'Err'): must be defined for each variable

Example usage:

PmuppCollection *coll = ...;
std::string expr = "var sigma = pow(abs(pow($T1,2.0)-pow(0.11,2.0)),0.5)";
PVarHandler handler(coll, expr, "sigma");
if (handler.isValid()) {
std::vector<double> values = handler.getValues();
std::vector<double> errors = handler.getErrors();
}
PVarHandler()
Default constructor.
Represents a collection of related experimental runs.
Definition Pmupp.h:234

Definition at line 69 of file PVarHandler.h.

Constructor & Destructor Documentation

◆ PVarHandler() [1/2]

PVarHandler::PVarHandler ( )

Default constructor.

Default constructor creating an invalid handler.

Creates an invalid PVarHandler instance with null pointers and empty strings.

Initializes all members to default/null values, resulting in an invalid handler that will return false from isValid().

Definition at line 286 of file PVarHandler.cpp.

◆ PVarHandler() [2/2]

PVarHandler::PVarHandler ( PmuppCollection * coll,
std::string parse_str,
std::string var_name = "",
const std::vector< PmuppCollection * > & allColl = std::vector<PmuppCollection*>() )

Constructor that parses and evaluates a variable expression.

Parses the provided expression string, performs semantic analysis, injects predefined variables from the collection, evaluates the expression, and stores the results.

Parameters
collpointer to the PmuppCollection containing run data and parameters
parse_strthe variable definition string to parse (e.g., "var x = $T1 + 1.0")
var_nameoptional variable name to extract from the evaluation results; if empty, only parsing/checking is performed
allColloptional list of all loaded collections (in handler-index order). Only used by the Python path, where it is exposed inside the <python> block as coll[]/collErr[] (see evaluatePython()). If empty, only the bound collection 'coll' is injected (bare names and par[]/parErr[]).

Performs the complete variable processing pipeline:

  1. Injects predefined variables from the collection into the AST
  2. Parses the input string using the PStatement grammar
  3. Performs semantic analysis using PProgram
  4. Injects actual data values from the collection
  5. Evaluates the expression using PProgEval
  6. Extracts and stores the requested variable results

If parsing, semantic analysis, or evaluation fails, fIsValid is set to false and error messages are logged to ~/.musrfit/mupp/mupp_err.log.

Parameters
collpointer to the collection containing run data
parse_strthe variable definition string to parse
var_nameoptional variable name to extract; if empty, only validation is performed
allColloptional list of all loaded collections (Python path only)

Definition at line 312 of file PVarHandler.cpp.

Member Function Documentation

◆ evaluatePython()

void PVarHandler::evaluatePython ( )
private

Evaluates the variable using an embedded Python3 interpreter (TPython).

Evaluates a <python> ... </python> variable block via ROOT's TPython.

Used when the input string contains a <python> ... </python> block. The bound collection's parameters are injected into the interpreter as bare-name lists (one value per run) plus par[]/parErr[] dictionaries as a fallback for names that are not valid Python identifiers (e.g. the keyword 'lambda'). In addition, every loaded collection passed via fAllColl is exposed through the coll[]/collErr[] dictionaries, addressable both by integer index (matching the script 'col <idx>') and by collection name; index and name keys reference the same per-parameter dict. This lets a single <python> block compute (and combine) variables for several collections. The user script must assign the requested variable and its error counterpart (<name>Err). Both value and error arrays are read back and stored in fVar.

If mupp was built without TPython support, the handler is marked invalid.

See the declaration in PVarHandler.h for the data contract. On any failure a message is written to ~/.musrfit/mupp/mupp_err.log and fIsValid stays false.

Definition at line 368 of file PVarHandler.cpp.

◆ getCollName()

QString PVarHandler::getCollName ( )
inline

Gets the collection name.

Returns
QString containing the name of the associated collection

Definition at line 107 of file PVarHandler.h.

◆ getData()

std::vector< double > PVarHandler::getData ( int idx)
private

Gets the data values for a specific parameter across all runs.

Parameters
idxthe parameter index in the collection
Returns
vector of data values from all runs for the specified parameter

Collects the value of the specified parameter from every run in the collection, returning them as a vector.

Parameters
idxthe parameter index (0-based)
Returns
vector of parameter values from all runs, or empty vector if index is out of range

Definition at line 607 of file PVarHandler.cpp.

◆ getDataErr()

std::vector< double > PVarHandler::getDataErr ( int idx)
private

Gets the error values for a specific parameter across all runs.

Computes the geometric mean of positive and negative errors for each run.

Parameters
idxthe parameter index in the collection
Returns
vector of error values (geometric mean of pos/neg errors) from all runs

Collects the error of the specified parameter from every run in the collection. The error is computed as the geometric mean of the positive and negative errors: sqrt(abs(posErr * negErr)).

Parameters
idxthe parameter index (0-based)
Returns
vector of parameter errors from all runs, or empty vector if index is out of range

Definition at line 635 of file PVarHandler.cpp.

◆ getErrors()

std::vector< double > PVarHandler::getErrors ( )

Gets the computed errors for the variable.

Gets the computed error values for the variable.

Returns
vector of double error values computed from the expression evaluation

Returns the vector of error values computed during expression evaluation. Each element corresponds to a different run in the collection.

Returns
vector of computed error values, or empty vector if not valid

Definition at line 536 of file PVarHandler.cpp.

◆ getValues()

std::vector< double > PVarHandler::getValues ( )

Gets the computed values for the variable.

Returns
vector of double values computed from the expression evaluation

Returns the vector of values computed during expression evaluation. Each element corresponds to a different run in the collection.

Returns
vector of computed values, or empty vector if not valid

Definition at line 518 of file PVarHandler.cpp.

◆ getVarName() [1/2]

QString PVarHandler::getVarName ( )
inline

Gets the variable name.

Returns
QString containing the variable name

Definition at line 113 of file PVarHandler.h.

◆ getVarName() [2/2]

std::string PVarHandler::getVarName ( int idx)
private

Gets the variable name at a specific parameter index.

Parameters
idxthe parameter index in the collection
Returns
variable name as a string, or "??" if index is out of range

Retrieves the parameter name from the collection's first run at the specified index.

Parameters
idxthe parameter index (0-based)
Returns
the parameter name, or "??" if index is out of range

Definition at line 586 of file PVarHandler.cpp.

◆ injectPredefVariables()

void PVarHandler::injectPredefVariables ( )
private

Injects predefined variables from the collection into the AST.

Extracts all parameter names from the collection's first run and creates variable declarations for both the parameter names and their corresponding error variables (suffixed with 'Err'). These declarations are prepended to the AST before evaluation.

For each parameter in the collection's first run, creates variable declarations for both the parameter name and its corresponding error variable (with 'Err' suffix). These declarations are prepended to the AST so they are processed before user-defined variables, making all collection parameters available for use in expressions.

Definition at line 555 of file PVarHandler.cpp.

◆ isValid()

bool PVarHandler::isValid ( )
inline

Checks if the parsing and evaluation were successful.

Returns
true if the expression was parsed and evaluated successfully, false otherwise

Definition at line 101 of file PVarHandler.h.

Member Data Documentation

◆ fAllColl

std::vector<PmuppCollection*> PVarHandler::fAllColl
private

all loaded collections (handler-index order); Python path only, exposed as coll[]/collErr[]

Definition at line 129 of file PVarHandler.h.

◆ fAst

mupp::ast::statement_list PVarHandler::fAst
private

Abstract Syntax Tree generated from parsing.

Definition at line 135 of file PVarHandler.h.

◆ fColl

PmuppCollection* PVarHandler::fColl
private

pointer to collection containing run data needed for parsing and evaluation

Definition at line 128 of file PVarHandler.h.

◆ fIsValid

bool PVarHandler::fIsValid
private

flag indicating whether parsing and evaluation succeeded

Definition at line 134 of file PVarHandler.h.

◆ fParseStr

std::string PVarHandler::fParseStr
private

the variable input string to be parsed

Definition at line 130 of file PVarHandler.h.

◆ fVar

mupp::prog::PVarHandler PVarHandler::fVar
private

variable handler storing the computed values and errors

Definition at line 132 of file PVarHandler.h.

◆ fVarName

std::string PVarHandler::fVarName
private

name of the variable to extract from evaluation results

Definition at line 131 of file PVarHandler.h.


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