Files
musrfit/src/include/PMsrHandler.h

410 lines
17 KiB
C++

/***************************************************************************
PMsrHandler.h
Author: Andreas Suter
e-mail: andreas.suter@psi.ch
***************************************************************************/
/***************************************************************************
* Copyright (C) 2007-2025 by Andreas Suter *
* andreas.suter@psi.ch *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#ifndef _PMSRHANDLER_H_
#define _PMSRHANDLER_H_
#include <string>
#include <memory>
#include <sstream>
#include <string>
#include <TString.h>
#include <TComplex.h>
#include "PMusr.h"
#include "PFunctionHandler.h"
#include "PFunctionGrammar.h"
#include "PFunction.h"
//-------------------------------------------------------------
/**
* <p>MSR file parser and manager for musrfit framework.
*
* <p>This class handles all MSR (Muon Spin Rotation) file operations including:
* - Reading and parsing MSR files
* - Writing MSR files and log files
* - Validating MSR file content (parameter ranges, theory consistency, etc.)
* - Managing fit parameters, theory definitions, run configurations
* - Evaluating user-defined functions
* - Handling parameter mapping between runs
*
* <p>The PMsrHandler performs comprehensive validation during parsing, checking for:
* - Parameter name uniqueness
* - Valid theory-to-parameter mappings
* - Histogram grouping consistency
* - RRF (Rotating Reference Frame) settings
* - Function syntax and parameter usage
*
* <p><b>Usage pattern:</b>
* @code
* PMsrHandler handler("myfile.msr");
* if (handler.ReadMsrFile() == PMUSR_SUCCESS) {
* PMsrParamList *params = handler.GetMsrParamList();
* // Process parameters, run fits, etc.
* handler.WriteMsrFile("output.msr");
* }
* @endcode
*/
class PMsrHandler
{
public:
/**
* <p>Constructor for PMsrHandler.
*
* @param fileName Path to MSR file to read/write
* @param startupOptions Optional startup configuration (from musrfit_startup.xml)
* @param fourierOnly If true, only parse Fourier-related blocks (for musrFT)
*/
PMsrHandler(const Char_t *fileName, PStartupOptions *startupOptions=0, const Bool_t fourierOnly=false);
virtual ~PMsrHandler();
/**
* <p>Reads and parses the MSR file.
*
* <p>Performs comprehensive parsing of all MSR file blocks including
* TITLE, FITPARAMETER, THEORY, FUNCTIONS, GLOBAL, RUN, COMMANDS,
* FOURIER, PLOT, and STATISTIC blocks. Validates consistency and
* reports detailed error messages on failure.
*
* @return PMUSR_SUCCESS on success, negative error code on failure
*
* @see PMUSR_MSR_FILE_NOT_FOUND
* @see PMUSR_MSR_SYNTAX_ERROR
*/
virtual Int_t ReadMsrFile();
/**
* <p>Writes a log file with MSR file content and parsing information.
*
* <p>Creates a .mlog file containing the parsed MSR structure,
* useful for debugging and verifying parameter interpretation.
*
* @param messages If true, includes informational messages in log
* @return PMUSR_SUCCESS on success, negative error code on failure
*/
virtual Int_t WriteMsrLogFile(const Bool_t messages = true);
/**
* <p>Writes MSR file with updated parameters and results.
*
* <p>Writes a complete MSR file, optionally preserving user comments
* from specific blocks. Typically called after fitting to save
* fitted parameters and statistics.
*
* @param filename Output MSR file path
* @param commentsPAR Optional comments for FITPARAMETER block (line number → comment)
* @param commentsTHE Optional comments for THEORY block
* @param commentsFUN Optional comments for FUNCTIONS block
* @param commentsRUN Optional comments for RUN block
* @return PMUSR_SUCCESS on success, negative error code on failure
*/
virtual Int_t WriteMsrFile(const Char_t *filename, std::map<UInt_t, TString> *commentsPAR = 0, std::map<UInt_t, TString> *commentsTHE = 0, \
std::map<UInt_t, TString> *commentsFUN = 0, std::map<UInt_t, TString> *commentsRUN = 0);
/// Returns pointer to MSR file title string
virtual TString* GetMsrTitle() { return &fTitle; }
/// Returns pointer to fit parameter list
virtual PMsrParamList* GetMsrParamList() { return &fParam; }
/// Returns pointer to THEORY block lines
virtual PMsrLines* GetMsrTheory() { return &fTheory; }
/// Returns pointer to FUNCTIONS block lines
virtual PMsrLines* GetMsrFunctions() { return &fFunctions; }
/// Returns pointer to GLOBAL block settings
virtual PMsrGlobalBlock* GetMsrGlobal() { return &fGlobal; }
/// Returns pointer to list of RUN blocks
virtual PMsrRunList* GetMsrRunList() { return &fRuns; }
/// Returns pointer to COMMANDS block lines
virtual PMsrLines* GetMsrCommands() { return &fCommands; }
/// Returns pointer to FOURIER block settings
virtual PMsrFourierStructure* GetMsrFourierList() { return &fFourier; }
/// Returns pointer to list of PLOT blocks
virtual PMsrPlotList* GetMsrPlotList() { return &fPlots; }
/// Returns pointer to STATISTIC block
virtual PMsrStatisticStructure* GetMsrStatistic() { return &fStatistic; }
/// Returns pointer to MSR file directory path
virtual TString* GetMsrFileDirectoryPath() { return &fMsrFileDirectoryPath; }
/// Returns the number of RUN blocks in MSR file
virtual UInt_t GetNoOfRuns() { return fRuns.size(); }
/// Returns the number of fit parameters in FITPARAMETER block
virtual UInt_t GetNoOfParams() { return fParam.size(); }
/// Returns the MSR file name
virtual const TString& GetFileName() const { return fFileName; }
/// Sets the MSR file title
/// @param title New title string
virtual void SetMsrTitle(const TString &title) { fTitle = title; }
/**
* <p>Sets the value of a fit parameter.
*
* @param i Parameter index (0-based)
* @param value New parameter value
* @return true on success, false if index out of range
*/
virtual Bool_t SetMsrParamValue(UInt_t i, Double_t value);
/**
* <p>Sets the step size (or error) of a fit parameter.
*
* @param i Parameter index (0-based)
* @param value New step/error value
* @return true on success, false if index out of range
*/
virtual Bool_t SetMsrParamStep(UInt_t i, Double_t value);
/**
* <p>Sets whether positive error is present for a parameter.
*
* @param i Parameter index (0-based)
* @param value True if positive error is defined
* @return true on success, false if index out of range
*/
virtual Bool_t SetMsrParamPosErrorPresent(UInt_t i, Bool_t value);
/**
* <p>Sets the positive error value for a parameter (asymmetric errors).
*
* @param i Parameter index (0-based)
* @param value Positive error value
* @return true on success, false if index out of range
*/
virtual Bool_t SetMsrParamPosError(UInt_t i, Double_t value);
/**
* <p>Sets a time-zero bin entry for a specific run.
*
* @param runNo Run block number (0-based)
* @param idx Histogram index within t0 list
* @param bin Time-zero bin value
*/
virtual void SetMsrT0Entry(UInt_t runNo, UInt_t idx, Double_t bin);
/**
* <p>Sets a time-zero bin for an addrun histogram.
*
* @param runNo Run block number (0-based)
* @param addRunIdx Index of addrun entry
* @param histoIdx Histogram index within addrun
* @param bin Time-zero bin value
*/
virtual void SetMsrAddT0Entry(UInt_t runNo, UInt_t addRunIdx, UInt_t histoIdx, Double_t bin);
/**
* <p>Sets a data range bin entry for a specific run.
*
* @param runNo Run block number (0-based)
* @param idx Data range index (0=start, 1=end, etc.)
* @param bin Data range bin value
*/
virtual void SetMsrDataRangeEntry(UInt_t runNo, UInt_t idx, Int_t bin);
/**
* <p>Sets a background range bin entry for a specific run.
*
* @param runNo Run block number (0-based)
* @param idx Background range index (0=start, 1=end, etc.)
* @param bin Background range bin value
*/
virtual void SetMsrBkgRangeEntry(UInt_t runNo, UInt_t idx, Int_t bin);
/// Flags that STATISTIC block should be copied as-is (for musrt0)
virtual void CopyMsrStatisticBlock() { fCopyStatisticsBlock = true; }
/// Sets whether fit converged in STATISTIC block
/// @param converged True if fit converged successfully
virtual void SetMsrStatisticConverged(Bool_t converged) { fStatistic.fValid = converged; }
/// Sets the minimum χ² (or max likelihood) in STATISTIC block
/// @param min Minimum value
virtual void SetMsrStatisticMin(Double_t min) { fStatistic.fMin = min; }
/// Sets the number of degrees of freedom in STATISTIC block
/// @param ndf Degrees of freedom
virtual void SetMsrStatisticNdf(UInt_t ndf) { fStatistic.fNdf = ndf; }
/// Returns the number of user-defined functions in FUNCTIONS block
virtual Int_t GetNoOfFuncs() { return fFuncHandler->GetNoOfFuncs(); }
/**
* <p>Gets function number by index.
*
* @param idx Function index (0-based)
* @return Function number as defined in FUNCTIONS block
*/
virtual UInt_t GetFuncNo(Int_t idx) { return fFuncHandler->GetFuncNo(idx); }
/**
* <p>Gets function index from function number.
*
* @param funNo Function number
* @return Function index (0-based)
*/
virtual UInt_t GetFuncIndex(Int_t funNo) { return fFuncHandler->GetFuncIndex(funNo); }
/**
* <p>Checks if map and parameter ranges are valid for functions.
*
* @param mapSize Size of map vector
* @param paramSize Number of available parameters
* @return true if ranges are valid
*/
virtual Bool_t CheckMapAndParamRange(UInt_t mapSize, UInt_t paramSize)
{ return fFuncHandler->CheckMapAndParamRange(mapSize, paramSize); }
/**
* <p>Evaluates a user-defined function.
*
* @param i Function index
* @param map Parameter mapping vector
* @param param Parameter value vector
* @param metaData Experimental metadata (field, temperature, etc.)
* @return Evaluated function value
*/
virtual Double_t EvalFunc(UInt_t i, std::vector<Int_t> map, std::vector<Double_t> param, PMetaData metaData)
{ return fFuncHandler->Eval(i, map, param, metaData); }
/**
* <p>Gets the number of fit parameters used in a specific theory line.
*
* @param idx Theory line index
* @return Number of parameters used
*/
virtual UInt_t GetNoOfFitParameters(UInt_t idx);
/**
* <p>Checks if a parameter is used in theory or functions.
*
* @param paramNo Parameter number (1-based as in MSR file)
* @return 1 if used, 0 if unused, -1 on error
*/
virtual Int_t ParameterInUse(UInt_t paramNo);
/**
* <p>Generates a grouping string for histogram display.
*
* @param runNo Run block number
* @param detector Detector identifier ("forward" or "backward")
* @param groupingStr Output grouping string
*/
virtual void GetGroupingString(Int_t runNo, TString detector, TString &groupingStr);
/**
* <p>Estimates N0 parameter for single histogram fits.
*
* <p>Uses data amplitude at t=0 to provide initial N0 estimate,
* improving fit convergence for single histogram fits.
*
* @return true on success
*/
virtual Bool_t EstimateN0();
/// Returns the last error message as a string
/// @return Error message string
virtual std::string GetLastErrorMsg() { return fLastErrorMsg.str(); }
virtual std::string GetDKSTheoryString();
virtual UInt_t GetDKSTag();
virtual Int_t GetNoOfMaps() { return fNoOfMaps; }
private:
Bool_t fFourierOnly; ///< flag indicating if Fourier transform only is wished. If yes, some part of the msr-file blocks are not needed.
PStartupOptions *fStartupOptions; ///< contains information about startup options from the musrfit_startup.xml
TString fFileName; ///< file name of the msr-file
TString fMsrFileDirectoryPath; ///< msr-file directory path
TString fTitle; ///< holds the title string of the msr-file
PMsrParamList fParam; ///< holds a list of the fit parameters
PMsrLines fTheory; ///< holds the theory definition
PMsrLines fFunctions; ///< holds the user defined functions
PMsrGlobalBlock fGlobal; ///< holds the information of the global section
PMsrRunList fRuns; ///< holds a list of run information
PMsrLines fCommands; ///< holds a list of the minuit commands
PMsrFourierStructure fFourier; ///< holds the parameters used for the Fourier transform
PMsrPlotList fPlots; ///< holds a list of the plot input parameters
PMsrStatisticStructure fStatistic; ///< holds the statistic info
Int_t fMsrBlockCounter; ///< used to select the proper msr-block
std::unique_ptr<PFunctionHandler> fFuncHandler; ///< needed to parse functions
PIntVector fParamInUse; ///< array holding the information if a particular parameter is used at all, i.e. if the theory is using it (perhaps via maps or functions)
Bool_t fCopyStatisticsBlock; ///< flag, if true: just copy to old statistics block (musrt0), otherwise write a new one (musrfit)
Int_t fNoOfMaps;
std::stringstream fLastErrorMsg;
virtual Bool_t HandleFitParameterEntry(PMsrLines &line);
virtual Bool_t HandleTheoryEntry(PMsrLines &line);
virtual Bool_t HandleFunctionsEntry(PMsrLines &line);
virtual Bool_t HandleGlobalEntry(PMsrLines &line);
virtual Bool_t HandleRunEntry(PMsrLines &line);
virtual Bool_t HandleCommandsEntry(PMsrLines &line);
virtual Bool_t HandleFourierEntry(PMsrLines &line);
virtual Bool_t HandlePlotEntry(PMsrLines &line);
virtual Bool_t HandleStatisticEntry(PMsrLines &line);
virtual void FillParameterInUse(PMsrLines &theory, PMsrLines &funcs, PMsrLines &run);
virtual void InitFourierParameterStructure(PMsrFourierStructure &fourier);
virtual void RemoveComment(const TString &str, TString &truncStr);
virtual Bool_t ParseFourierPhaseValueVector(PMsrFourierStructure &fourier, const TString &str, Bool_t &error);
virtual Bool_t ParseFourierPhaseParVector(PMsrFourierStructure &fourier, const TString &str, Bool_t &error);
virtual Bool_t ParseFourierPhaseParIterVector(PMsrFourierStructure &fourier, const TString &str, Bool_t &error);
virtual Bool_t FilterNumber(TString str, const Char_t *filter, Int_t offset, Int_t &no);
virtual UInt_t NeededPrecision(Double_t dval, UInt_t precLimit=13);
virtual UInt_t LastSignificant(Double_t dval, UInt_t precLimit=6);
virtual void MakeDetectorGroupingString(TString str, PIntVector &group, TString &result, Bool_t includeDetector = true);
virtual TString BeautifyFourierPhaseParameterString();
virtual void CheckLegacyLifetimecorrection();
virtual Bool_t CheckRunBlockIntegrity();
virtual Bool_t CheckUniquenessOfParamNames(UInt_t &parX, UInt_t &parY);
virtual Bool_t CheckMaps();
virtual Bool_t CheckFuncs();
virtual Bool_t CheckHistoGrouping();
virtual Bool_t CheckAddRunParameters();
virtual Bool_t CheckRRFSettings();
virtual Bool_t CheckRealFFT();
virtual void CheckMaxLikelihood();
virtual void HandleTheoryArguments(const TString theo, PStringVector &args);
};
#endif // _PMSRHANDLER_H_