musrfit 1.10.0
PUserFcnBase.cpp File Reference
#include <vector>
#include <fstream>
#include "PUserFcnBase.h"
Include dependency graph for PUserFcnBase.cpp:

Go to the source code of this file.

Functions

 ClassImp (PUserFcnBase) Int_t parseXmlFile(TSAXParser *saxParser
 Parses an XML file using buffer-based parsing for better compatibility.
 
xmlFile open (startup_path_name, std::ios::in|std::ios::ate)
 
 if (xmlFile.is_open())
 
 if (!xmlBuffer)
 

Variables

const char * startup_path_name
 
std::fstream xmlFile
 
UInt_t xmlSize = 0
 
Char_t * xmlBuffer = nullptr
 
 else
 
return status
 
std::vector< void * > gGlobalUserFcn
 Global storage for user function objects requiring persistent state.
 

Function Documentation

◆ ClassImp()

ClassImp ( PUserFcnBase )

Parses an XML file using buffer-based parsing for better compatibility.

This function provides a replacement for TSAXParser::ParseFile() that works reliably across different environments. Some systems have issues with direct file parsing, but buffer-based parsing (ParseBuffer) works consistently.

Usage in User Functions

User functions that need to read XML configuration files should use this function instead of TSAXParser::ParseFile():

class TMyConfigurableFcn : public PUserFcnBase {
private:
MyConfigHandler fHandler; // Derived from TSAXParser callbacks
public:
Bool_t LoadConfig(const char* configFile) {
TSAXParser parser;
parser.ConnectToHandler("MyConfigHandler", &fHandler);
Int_t status = parseXmlFile(&parser, configFile);
if (status != 0) {
std::cerr << "Failed to parse config: " << configFile << std::endl;
return false;
}
return true;
}
};
return status
Int_t parseXmlFile(TSAXParser *, const Char_t *)
Abstract base class for user-defined theory functions in musrfit.

Algorithm

  1. Opens the file in binary mode, seeking to end
  2. Determines file size from stream position
  3. Allocates buffer and reads entire file
  4. Passes buffer to TSAXParser::ParseBuffer()
  5. Cleans up buffer memory
Parameters
saxParserPointer to a configured TSAXParser object. The parser should have its handler connected before calling this function.
startup_path_nameFull filesystem path to the XML file to parse.
Returns
Status code:
  • 0: Success - file parsed without errors
  • 1: File error - could not open or read the file
  • >1: XML parse error from TSAXParser::ParseBuffer()
See also
PStartupHandler for an example of XML parsing in musrfit
TSAXParser for ROOT's SAX parser documentation

◆ if() [1/2]

if ( ! xmlBuffer)

Definition at line 105 of file PUserFcnBase.cpp.

References if(), status, and xmlBuffer.

◆ if() [2/2]

if ( xmlFile. is_open())

Definition at line 98 of file PUserFcnBase.cpp.

References xmlFile.

◆ open()

xmlFile open ( startup_path_name ,
std::ios::in|std::ios::ate  )

References startup_path_name, and xmlFile.

Variable Documentation

◆ else

else
Initial value:
{
status = saxParser->ParseBuffer(xmlBuffer, xmlSize)
char * xmlBuffer
return status
unsigned int xmlSize

Definition at line 107 of file PUserFcnBase.cpp.

◆ gGlobalUserFcn

std::vector<void *> gGlobalUserFcn

Global storage for user function objects requiring persistent state.

This vector provides a global container for user functions that need to maintain state across multiple evaluations or share data between runs. It is primarily used by user functions implementing the "global part" interface (NeedGlobalPart(), SetGlobalPart(), GlobalPartIsValid()).

Usage Pattern

User functions with expensive initialization (lookup tables, precomputed grids, loaded data files) store their global objects here:

// In user function's SetGlobalPart implementation:
void TMyFcn::SetGlobalPart(std::vector<void*> &globalPart, UInt_t idx) {
if (idx < globalPart.size() && globalPart[idx] != nullptr) {
fGlobal = static_cast<MyGlobalData*>(globalPart[idx]);
} else {
fGlobal = new MyGlobalData();
fGlobal->Initialize(); // Expensive one-time computation
if (idx < globalPart.size())
globalPart[idx] = fGlobal;
else
globalPart.push_back(fGlobal);
}
}
Note
The vector stores void pointers, so user functions must cast appropriately and manage memory for their specific data types.
Warning
User functions are responsible for proper cleanup of their global objects to avoid memory leaks.
See also
PUserFcnBase::SetGlobalPart() for the interface to populate this vector
PTheory for how global parts are initialized during theory setup

Definition at line 155 of file PUserFcnBase.cpp.

Referenced by PTheory::PTheory(), and PTheory::~PTheory().

◆ startup_path_name

const char* startup_path_name
Initial value:
{
Int_t status

Definition at line 90 of file PUserFcnBase.cpp.

◆ status

return status

Definition at line 113 of file PUserFcnBase.cpp.

◆ xmlBuffer

xmlBuffer = nullptr

Definition at line 95 of file PUserFcnBase.cpp.

◆ xmlFile

std::fstream xmlFile

Definition at line 93 of file PUserFcnBase.cpp.

◆ xmlSize

UInt_t xmlSize = 0

Definition at line 94 of file PUserFcnBase.cpp.