musrfit 1.10.0
PFunction.h
Go to the documentation of this file.
1/***************************************************************************
2
3 PFunction.h
4
5 Author: Andreas Suter
6 e-mail: andreas.suter@psi.ch
7
8***************************************************************************/
9
10/***************************************************************************
11 * Copyright (C) 2007-2026 by Andreas Suter *
12 * andreas.suter@psi.ch *
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 * This program is distributed in the hope that it will be useful, *
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
22 * GNU General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU General Public License *
25 * along with this program; if not, write to the *
26 * Free Software Foundation, Inc., *
27 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
28 ***************************************************************************/
29
30#ifndef _PFUNCTION_H_
31#define _PFUNCTION_H_
32
33#include <vector>
34
35#include <boost/version.hpp>
36#include <boost/variant/static_visitor.hpp>
37#include <boost/variant/apply_visitor.hpp>
38
39#include <TString.h>
40
41#include "PMusr.h"
42#include "PFunctionAst.h"
43#include "PFunctionGrammar.h"
44
45//----------------------------------------------------------------------------
69class PFunction {
70 public:
71 //------------------------------------------------------------------------
77 PFunction(const musrfit::ast::assignment& assignment);
78
79 //------------------------------------------------------------------------
83 virtual ~PFunction();
84
85 //------------------------------------------------------------------------
91 virtual Bool_t IsValid() { return fValid; }
92
93 //------------------------------------------------------------------------
99 virtual Int_t GetFuncNo() { return fFuncNo; }
100
101 //------------------------------------------------------------------------
109 virtual Bool_t CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize);
110
111 //------------------------------------------------------------------------
119 virtual Double_t Eval(std::vector<Double_t> param, PMetaData metaData);
120
121 //------------------------------------------------------------------------
130 virtual void SetMap(std::vector<Int_t> map) { fMap = map; }
131
132 //------------------------------------------------------------------------
138 virtual TString* GetFuncString() { return &fFuncString; }
139
140 protected:
141 //------------------------------------------------------------------------
150 virtual Bool_t FindAndCheckMapAndParamRange(const musrfit::ast::operand& operand,
151 UInt_t mapSize, UInt_t paramSize);
152
153 //------------------------------------------------------------------------
160 virtual TString GenerateString(const musrfit::ast::expression& expr);
161
162 //------------------------------------------------------------------------
169 virtual TString GenerateStringOperand(const musrfit::ast::operand& operand);
170
171 private:
179 class EvalVisitor : public boost::static_visitor<Double_t>
180 {
181 public:
182 EvalVisitor(const std::vector<Int_t>& map,
183 const std::vector<Double_t>& param,
184 const PMetaData& metaData)
185 : fMap(map), fParam(param), fMetaData(metaData) {}
186
187 Double_t operator()(const musrfit::ast::leer&) const;
188 Double_t operator()(double val) const;
189 Double_t operator()(const musrfit::ast::constant& c) const;
190 Double_t operator()(const musrfit::ast::parameter& p) const;
191 Double_t operator()(const musrfit::ast::map_ref& m) const;
192 Double_t operator()(const musrfit::ast::function_call& f) const;
193 Double_t operator()(const musrfit::ast::power_call& p) const;
194 Double_t operator()(const musrfit::ast::expression& expr) const;
195
196 private:
197 const std::vector<Int_t>& fMap;
198 const std::vector<Double_t>& fParam;
200 };
201
202 musrfit::ast::expression fAst;
203 std::vector<Double_t> fParam;
204 std::vector<Int_t> fMap;
205
206 Bool_t fValid;
207 Int_t fFuncNo;
208 TString fFuncString;
210};
211
212#endif // _PFUNCTION_H_
const PMetaData & fMetaData
Definition PFunction.h:199
Double_t operator()(const musrfit::ast::leer &) const
Evaluates a leer (empty) AST node.
const std::vector< Double_t > & fParam
Definition PFunction.h:198
const std::vector< Int_t > & fMap
Definition PFunction.h:197
EvalVisitor(const std::vector< Int_t > &map, const std::vector< Double_t > &param, const PMetaData &metaData)
Definition PFunction.h:182
virtual TString * GetFuncString()
Returns the human-readable string representation of the function.
Definition PFunction.h:138
PMetaData fMetaData
Metadata from experimental data (field, energy, temperature, etc.)
Definition PFunction.h:209
virtual Bool_t IsValid()
Checks if the function was successfully parsed and initialized.
Definition PFunction.h:91
Int_t fFuncNo
Function number extracted from label (x in FUNx)
Definition PFunction.h:207
virtual Int_t GetFuncNo()
Returns the function number extracted from the function label.
Definition PFunction.h:99
Bool_t fValid
Validity flag: true if function parsed and initialized successfully.
Definition PFunction.h:206
musrfit::ast::expression fAst
Abstract syntax tree for the expression.
Definition PFunction.h:202
virtual Bool_t CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize)
Validates that all parameter and map references are within valid ranges.
Definition PFunction.cpp:91
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.
TString fFuncString
Formatted, human-readable function representation.
Definition PFunction.h:208
virtual void SetMap(std::vector< Int_t > map)
Sets the map vector for parameter indirection.
Definition PFunction.h:130
PFunction(const musrfit::ast::assignment &assignment)
Constructor that parses and prepares a function for evaluation.
Definition PFunction.cpp:55
virtual ~PFunction()
Destructor that cleans up resources.
Definition PFunction.cpp:74
std::vector< Double_t > fParam
Current fit parameter values for evaluation.
Definition PFunction.h:203
virtual Double_t Eval(std::vector< Double_t > param, PMetaData metaData)
Evaluates the function with given parameters and metadata.
std::vector< Int_t > fMap
Map vector for indirect parameter references.
Definition PFunction.h:204