musrfit 1.10.0
PFunction Class Reference

Class for parsing and evaluating mathematical functions from msr-file FUNCTIONS blocks. More...

#include <PFunction.h>

Collaboration diagram for PFunction:

Classes

class  EvalVisitor
 Visitor class for evaluating the AST. More...
 

Public Member Functions

 PFunction (const musrfit::ast::assignment &assignment)
 Constructor that parses and prepares a function for evaluation.
 
virtual ~PFunction ()
 Destructor that cleans up resources.
 
virtual Bool_t IsValid ()
 Checks if the function was successfully parsed and initialized.
 
virtual Int_t GetFuncNo ()
 Returns the function number extracted from the function label.
 
virtual Bool_t CheckMapAndParamRange (UInt_t mapSize, UInt_t paramSize)
 Validates that all parameter and map references are within valid ranges.
 
virtual Double_t Eval (std::vector< Double_t > param, PMetaData metaData)
 Evaluates the function with given parameters and metadata.
 
virtual void SetMap (std::vector< Int_t > map)
 Sets the map vector for parameter indirection.
 
virtual TString * GetFuncString ()
 Returns the human-readable string representation of the function.
 

Protected Member Functions

virtual Bool_t FindAndCheckMapAndParamRange (const musrfit::ast::operand &operand, UInt_t mapSize, UInt_t paramSize)
 Recursively validates parameter and map references in the AST.
 
virtual TString GenerateString (const musrfit::ast::expression &expr)
 Generates a human-readable string representation of the AST.
 
virtual TString GenerateStringOperand (const musrfit::ast::operand &operand)
 Generates a string representation of an operand.
 

Private Attributes

musrfit::ast::expression fAst
 Abstract syntax tree for the expression.
 
std::vector< Double_t > fParam
 Current fit parameter values for evaluation.
 
std::vector< Int_t > fMap
 Map vector for indirect parameter references.
 
Bool_t fValid
 Validity flag: true if function parsed and initialized successfully.
 
Int_t fFuncNo
 Function number extracted from label (x in FUNx)
 
TString fFuncString
 Formatted, human-readable function representation.
 
PMetaData fMetaData
 Metadata from experimental data (field, energy, temperature, etc.)
 

Detailed Description

Class for parsing and evaluating mathematical functions from msr-file FUNCTIONS blocks.

This class handles the complete lifecycle of a function definition:

  1. Parses the function string using PFunctionGrammar into an AST
  2. Validates parameter and map references
  3. Evaluates the function with given parameters and metadata using the visitor pattern

Functions can reference:

  • Fit parameters (PAR1, PAR2, ...)
  • Map values (MAP1, MAP2, ...)
  • Constants (PI, GAMMA_MU)
  • Metadata (magnetic field B, energy EN, temperature T)
  • Other functions (FUN1, FUN2, ...)

Example function definition:

FUN1 = PAR1 * COS(2.0 * PI * GAMMA_MU * B * PAR2)
#define PI
Definition PFourier.cpp:45
See also
PFunctionGrammar for the grammar definition
musrfit::ast for the AST structure

Definition at line 69 of file PFunction.h.

Constructor & Destructor Documentation

◆ PFunction()

PFunction::PFunction ( const musrfit::ast::assignment & assignment)

Constructor that parses and prepares a function for evaluation.

Constructor that initializes the function from a parsed AST.

Parameters
assignmentAbstract syntax tree (AST) from parsing a function expression

This constructor stores the AST generated by the Boost.Spirit X3 parser, extracts the function number, and prepares the function for evaluation. The X3 AST is directly suitable for evaluation using the visitor pattern, eliminating the need for conversion to an intermediate tree structure.

Parameters
assignmentAST from parsing a function expression (FUN# = expression)

Definition at line 55 of file PFunction.cpp.

References fAst, fFuncNo, fFuncString, fValid, and GenerateString().

Referenced by FindAndCheckMapAndParamRange(), and GenerateStringOperand().

◆ ~PFunction()

PFunction::~PFunction ( )
virtual

Destructor that cleans up resources.

Destructor that releases resources.

Cleans up parameter and map vectors.

Definition at line 74 of file PFunction.cpp.

References fMap, and fParam.

Member Function Documentation

◆ CheckMapAndParamRange()

Bool_t PFunction::CheckMapAndParamRange ( UInt_t mapSize,
UInt_t paramSize )
virtual

Validates that all parameter and map references are within valid ranges.

Parameters
mapSizeNumber of available map entries
paramSizeNumber of available fit parameters
Returns
true if all references are valid, false otherwise

Recursively traverses the AST to find all parameter (PAR#) and map (MAP#) references and checks that they are within the specified bounds.

Parameters
mapSizeNumber of available map entries
paramSizeNumber of available fit parameters
Returns
true if all references are valid, false otherwise

Definition at line 91 of file PFunction.cpp.

References fAst, and FindAndCheckMapAndParamRange().

◆ Eval()

Double_t PFunction::Eval ( std::vector< Double_t > param,
PMetaData metaData )
virtual

Evaluates the function with given parameters and metadata.

Parameters
paramVector of fit parameter values
metaDataMetadata containing field, energy, temperature, etc.
Returns
Computed function value

Uses the visitor pattern to traverse the AST and compute the numeric result. The evaluation applies operators with proper precedence as encoded in the AST.

Parameters
paramVector of fit parameter values
metaDataMetadata containing field, energy, temperature, etc.
Returns
Computed function value, or 0.0 if invalid

Definition at line 193 of file PFunction.cpp.

References fAst, fMap, fMetaData, fParam, and fValid.

◆ FindAndCheckMapAndParamRange()

Bool_t PFunction::FindAndCheckMapAndParamRange ( const musrfit::ast::operand & operand,
UInt_t mapSize,
UInt_t paramSize )
protectedvirtual

Recursively validates parameter and map references in the AST.

Recursively validates parameter and map references in an operand.

Parameters
operandCurrent operand being checked
mapSizeNumber of available map entries
paramSizeNumber of available fit parameters
Returns
true if all references are valid, false otherwise

Helper visitor for CheckMapAndParamRange that handles different operand types.

Parameters
operandCurrent operand being checked
mapSizeNumber of available map entries
paramSizeNumber of available fit parameters
Returns
true if all references are valid, false otherwise

Definition at line 121 of file PFunction.cpp.

References FindAndCheckMapAndParamRange(), and PFunction().

Referenced by CheckMapAndParamRange(), and FindAndCheckMapAndParamRange().

◆ GenerateString()

TString PFunction::GenerateString ( const musrfit::ast::expression & expr)
protectedvirtual

Generates a human-readable string representation of the AST.

Generates a human-readable string representation of an expression.

Parameters
exprExpression AST to convert to string
Returns
Formatted string representation
Parameters
exprExpression AST to convert
Returns
Formatted string

Definition at line 238 of file PFunction.cpp.

References GenerateString(), and GenerateStringOperand().

Referenced by GenerateString(), GenerateStringOperand(), and PFunction().

◆ GenerateStringOperand()

TString PFunction::GenerateStringOperand ( const musrfit::ast::operand & operand)
protectedvirtual

Generates a string representation of an operand.

Parameters
operandOperand AST to convert to string
Returns
Formatted string representation
Parameters
operandOperand AST to convert
Returns
Formatted string

Definition at line 318 of file PFunction.cpp.

References GenerateString(), GenerateStringOperand(), and PFunction().

Referenced by GenerateString(), and GenerateStringOperand().

◆ GetFuncNo()

virtual Int_t PFunction::GetFuncNo ( )
inlinevirtual

Returns the function number extracted from the function label.

Returns
Function number (x from FUNx), or -1 if invalid

Definition at line 99 of file PFunction.h.

References fFuncNo.

◆ GetFuncString()

virtual TString * PFunction::GetFuncString ( )
inlinevirtual

Returns the human-readable string representation of the function.

Returns
Pointer to formatted function string

Definition at line 138 of file PFunction.h.

References fFuncString.

◆ IsValid()

virtual Bool_t PFunction::IsValid ( )
inlinevirtual

Checks if the function was successfully parsed and initialized.

Returns
true if the function is valid, false otherwise

Definition at line 91 of file PFunction.h.

References fValid.

◆ SetMap()

virtual void PFunction::SetMap ( std::vector< Int_t > map)
inlinevirtual

Sets the map vector for parameter indirection.

Maps allow indirect parameter references. MAP# references fMap[#-1], which then indexes into the parameter vector.

Parameters
mapVector of parameter indices (1-based)

Definition at line 130 of file PFunction.h.

References fMap.

Member Data Documentation

◆ fAst

musrfit::ast::expression PFunction::fAst
private

Abstract syntax tree for the expression.

Definition at line 202 of file PFunction.h.

Referenced by CheckMapAndParamRange(), Eval(), and PFunction().

◆ fFuncNo

Int_t PFunction::fFuncNo
private

Function number extracted from label (x in FUNx)

Definition at line 207 of file PFunction.h.

Referenced by GetFuncNo(), and PFunction().

◆ fFuncString

TString PFunction::fFuncString
private

Formatted, human-readable function representation.

Definition at line 208 of file PFunction.h.

Referenced by GetFuncString(), and PFunction().

◆ fMap

std::vector<Int_t> PFunction::fMap
private

Map vector for indirect parameter references.

Definition at line 204 of file PFunction.h.

Referenced by Eval(), SetMap(), and ~PFunction().

◆ fMetaData

PMetaData PFunction::fMetaData
private

Metadata from experimental data (field, energy, temperature, etc.)

Definition at line 209 of file PFunction.h.

Referenced by Eval().

◆ fParam

std::vector<Double_t> PFunction::fParam
private

Current fit parameter values for evaluation.

Definition at line 203 of file PFunction.h.

Referenced by Eval(), and ~PFunction().

◆ fValid

Bool_t PFunction::fValid
private

Validity flag: true if function parsed and initialized successfully.

Definition at line 206 of file PFunction.h.

Referenced by Eval(), IsValid(), and PFunction().


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