improve the doxygen docu of PRunSingleHistoRRF.*

This commit is contained in:
2025-11-23 17:25:45 +01:00
parent db4b6395ef
commit 633f63b104
2 changed files with 1116 additions and 118 deletions

View File

@@ -53,7 +53,22 @@
// Constructor
//--------------------------------------------------------------------------
/**
* <p>Constructor
* \brief Default constructor for RRF single histogram fitting class.
*
* Initializes all member variables to safe default values:
* - fNoOfFitBins = 0 (no bins to fit)
* - fBackground = 0 (will be estimated or set from MSR file)
* - fBkgErr = 1.0 (default error estimate)
* - fRRFPacking = -1 (invalid until set from GLOBAL block)
* - fTheoAsData = false (high-resolution theory grid)
* - fGoodBins[0,1] = -1 (calculated from data range)
* - fN0EstimateEndTime = 1.0 μs (default N₀ estimation window)
*
* \warning This constructor creates an invalid object that cannot be used
* until properly initialized with MSR file data. Use the full
* constructor for normal operation.
*
* \see PRunSingleHistoRRF(PMsrHandler*, PRunDataHandler*, UInt_t, EPMusrHandleTag, Bool_t)
*/
PRunSingleHistoRRF::PRunSingleHistoRRF() : PRunBase()
{
@@ -75,12 +90,44 @@ PRunSingleHistoRRF::PRunSingleHistoRRF() : PRunBase()
// Constructor
//--------------------------------------------------------------------------
/**
* <p>Constructor
* \brief Main constructor for RRF single histogram fitting and viewing.
*
* \param msrInfo pointer to the msr-file handler
* \param rawData raw run data
* \param runNo number of the run within the msr-file
* \param tag tag showing what shall be done: kFit == fitting, kView == viewing
* Constructs a fully initialized RRF single histogram run object by:
* -# Validating GLOBAL block presence (mandatory for RRF analysis)
* -# Validating RRF frequency specification (rrf_freq in GLOBAL block)
* -# Validating RRF packing specification (rrf_packing in GLOBAL block)
* -# Calling PrepareData() to load histogram and perform RRF transformation
*
* <b>GLOBAL Block Requirements:</b>
* The RRF fit type requires the following entries in the GLOBAL block:
* - \c rrf_freq: Rotation frequency with unit (e.g., "13.554 T", "183.7 MHz")
* - \c rrf_packing: Number of bins to average (integer)
* - \c rrf_phase: (optional) Initial phase in degrees
*
* <b>Error Handling:</b>
* If any validation fails, the constructor:
* - Outputs detailed error message to stderr
* - Sets fValid = false
* - Returns immediately (PrepareData() is not called)
*
* \param msrInfo Pointer to MSR file handler (NOT owned, must outlive this object)
* \param rawData Pointer to raw run data handler (NOT owned, must outlive this object)
* \param runNo Zero-based index of the RUN block in the MSR file
* \param tag Operation mode: kFit (fitting) or kView (viewing/plotting)
* \param theoAsData If true, theory calculated only at data points (for viewing);
* if false, theory uses finer time grid (8× data resolution)
*
* \warning GLOBAL block with RRF parameters is MANDATORY for this fit type.
* Always check IsValid() after construction.
*
* \note After construction, check IsValid() to ensure initialization succeeded.
* Common failure modes:
* - Missing GLOBAL block
* - Missing rrf_freq specification
* - Missing rrf_packing specification
* - Data file not found or histogram missing
*
* \see PrepareData(), PrepareFitData(), PrepareViewData()
*/
PRunSingleHistoRRF::PRunSingleHistoRRF(PMsrHandler *msrInfo, PRunDataHandler *rawData, UInt_t runNo, EPMusrHandleTag tag, Bool_t theoAsData) :
PRunBase(msrInfo, rawData, runNo, tag), fTheoAsData(theoAsData)
@@ -131,7 +178,17 @@ PRunSingleHistoRRF::PRunSingleHistoRRF(PMsrHandler *msrInfo, PRunDataHandler *ra
// Destructor
//--------------------------------------------------------------------------
/**
* <p>Destructor
* \brief Destructor for RRF single histogram fitting class.
*
* Cleans up dynamically allocated memory:
* - Clears the forward histogram data vector (fForward)
* - Other vectors (fM, fMerr, fW, fAerr) are local to PrepareFitData
* and cleared automatically
*
* Base class destructor (PRunBase) handles cleanup of:
* - Theory objects
* - Function value arrays
* - Other shared resources
*/
PRunSingleHistoRRF::~PRunSingleHistoRRF()
{
@@ -142,12 +199,42 @@ PRunSingleHistoRRF::~PRunSingleHistoRRF()
// CalcChiSquare (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate chi-square.
* \brief Calculates χ² between RRF-transformed data and theory (least-squares fit metric).
*
* <b>return:</b>
* - chisq value
* Computes the standard chi-square goodness-of-fit statistic for RRF asymmetry:
* \f[
* \chi^2 = \sum_{i=t_{\rm start}}^{t_{\rm end}} \frac{[A_{\rm RRF}^{\rm data}(t_i) - A_{\rm RRF}^{\rm theory}(t_i)]^2}{\sigma_i^2}
* \f]
*
* \param par parameter vector iterated by minuit2
* Unlike standard single histogram fitting, no explicit N₀ or exponential decay
* factors appear since the RRF transformation already produces dimensionless
* asymmetry with properly propagated errors.
*
* <b>Algorithm:</b>
* -# Evaluate all user-defined functions from FUNCTIONS block
* -# Pre-evaluate theory at t=1.0 to initialize any stateful functions
* (e.g., LF relaxation, user functions with internal state)
* -# Loop over fit range bins [fStartTimeBin, fEndTimeBin)
* -# For each bin: calculate time, evaluate theory, accumulate χ²
*
* <b>OpenMP Parallelization:</b>
* When compiled with OpenMP (HAVE_GOMP defined):
* - Dynamic scheduling with chunk size = max(10, N_bins / N_processors)
* - Private variables per thread: i, time, diff
* - Reduction performed on chisq accumulator
* - Thread-safe due to pre-evaluation of theory at t=1.0
*
* \param par Parameter vector from MINUIT minimizer, containing current
* estimates of all fit parameters
*
* \return χ² value (sum over all bins in fit range). Minimize this value
* during fitting to find optimal parameters.
*
* \note The theory function is evaluated in the RRF frame. The THEORY block
* should describe the low-frequency RRF signal, not the laboratory frame
* precession.
*
* \see CalcChiSquareExpected(), CalcMaxLikelihood()
*/
Double_t PRunSingleHistoRRF::CalcChiSquare(const std::vector<Double_t>& par)
{
@@ -189,12 +276,32 @@ Double_t PRunSingleHistoRRF::CalcChiSquare(const std::vector<Double_t>& par)
// CalcChiSquareExpected (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate expected chi-square.
* \brief Calculates expected χ² using theory variance instead of data variance.
*
* <b>return:</b>
* - chisq value
* Computes the expected chi-square where the error estimate in the denominator
* comes from the theory prediction rather than the data:
* \f[
* \chi^2_{\rm exp} = \sum_{i=t_{\rm start}}^{t_{\rm end}} \frac{[A_{\rm RRF}^{\rm data}(t_i) - A_{\rm RRF}^{\rm theory}(t_i)]^2}{A_{\rm RRF}^{\rm theory}(t_i)}
* \f]
*
* \param par parameter vector iterated by minuit2
* This metric is useful for:
* - Diagnostic purposes to assess fit quality
* - Detecting systematic deviations from the model
* - Comparing with standard χ² to identify error estimation issues
*
* <b>Algorithm:</b>
* Same as CalcChiSquare() but uses theory value as variance estimate instead
* of measured error bars. OpenMP parallelization is applied when available.
*
* \param par Parameter vector from MINUIT minimizer
*
* \return Expected χ² value. For a good fit, this should be approximately
* equal to the number of degrees of freedom (N_bins - N_params).
*
* \warning Theory values must be positive for valid variance estimate.
* Negative theory values can lead to incorrect χ² calculation.
*
* \see CalcChiSquare()
*/
Double_t PRunSingleHistoRRF::CalcChiSquareExpected(const std::vector<Double_t>& par)
{
@@ -238,12 +345,35 @@ Double_t PRunSingleHistoRRF::CalcChiSquareExpected(const std::vector<Double_t>&
// CalcMaxLikelihood (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate log maximum-likelihood. See http://pdg.lbl.gov/index.html
* \brief Calculates maximum likelihood for RRF data (NOT YET IMPLEMENTED).
*
* <b>return:</b>
* - log maximum-likelihood value
* Maximum likelihood estimation for RRF single histogram data is more complex
* than for raw histograms due to the non-linear transformation from
* Poisson-distributed counts to RRF asymmetry.
*
* \param par parameter vector iterated by minuit2
* <b>Theoretical Background:</b>
* For raw histogram data, the likelihood is:
* \f[
* \mathcal{L} = \prod_i \frac{\mu_i^{n_i} e^{-\mu_i}}{n_i!}
* \f]
* where \f$\mu_i\f$ is the expected count and \f$n_i\f$ is the observed count.
*
* For RRF-transformed data, the error propagation through the transformation
* must be properly accounted for in the likelihood function.
*
* <b>Current Implementation:</b>
* Returns 0.0 (not implemented). Use χ² minimization (CalcChiSquare) instead.
*
* \param par Parameter vector from MINUIT minimizer (unused)
*
* \return 0.0 (not implemented)
*
* \todo Implement proper maximum likelihood for RRF data by:
* -# Deriving the likelihood function for transformed asymmetry
* -# Accounting for error propagation through RRF transformation
* -# Including correlations introduced by packing
*
* \see CalcChiSquare() for currently supported fit metric
*/
Double_t PRunSingleHistoRRF::CalcMaxLikelihood(const std::vector<Double_t>& par)
{
@@ -256,7 +386,39 @@ Double_t PRunSingleHistoRRF::CalcMaxLikelihood(const std::vector<Double_t>& par)
// CalcTheory (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate theory for a given set of fit-parameters.
* \brief Evaluates theory function at all data points for viewing/plotting.
*
* Calculates the theoretical RRF asymmetry using the current MSR parameter
* values and stores results in fData for display. This method is called
* after fitting to generate the theory curve overlay.
*
* <b>Algorithm:</b>
* -# Extract parameter values from MSR parameter list
* -# Evaluate all user-defined functions from FUNCTIONS block
* -# Loop over data points (size matches RRF-packed data)
* -# Calculate time: t = dataTimeStart + i × dataTimeStep
* -# Evaluate theory: P(t) = Func(t, par, funcValues)
* -# Store results via fData.AppendTheoryValue()
*
* <b>Theory Function:</b>
* The theory is evaluated directly in the RRF frame. The THEORY block should
* specify the low-frequency RRF signal (after transformation), not the
* laboratory-frame high-frequency precession.
*
* Example THEORY block for RRF analysis:
* \code
* THEORY
* asymmetry 1 (amplitude)
* simpleGss 2 (Gaussian relaxation)
* TFieldCos map1 fun1 (frequency shift, phase)
* \endcode
*
* where the frequency in TFieldCos is the difference frequency (ω - ω_RRF).
*
* \note Unlike CalcChiSquare(), this method does not return a value.
* Results are stored internally in fData.fTheory vector.
*
* \see CalcChiSquare(), PrepareViewData()
*/
void PRunSingleHistoRRF::CalcTheory()
{
@@ -289,9 +451,23 @@ void PRunSingleHistoRRF::CalcTheory()
// GetNoOfFitBins (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate the number of fitted bins for the current fit range.
* \brief Returns the number of bins included in the current fit range.
*
* <b>return:</b> number of fitted bins.
* Triggers CalcNoOfFitBins() to ensure the bin count is current based on
* the latest fit range settings, then returns the cached value.
*
* The number of fit bins is needed for:
* - Calculating degrees of freedom: ν = N_bins - N_params
* - Reduced χ²: χ²_red = χ² / ν
* - Statistical diagnostics and fit quality assessment
*
* \return Number of RRF-packed bins within [fFitStartTime, fFitEndTime].
* This accounts for RRF packing: fewer bins than raw data.
*
* \note The fit range may be modified during fitting by COMMANDS block
* instructions. Always call this method to get the current count.
*
* \see CalcNoOfFitBins(), SetFitRangeBin()
*/
UInt_t PRunSingleHistoRRF::GetNoOfFitBins()
{
@@ -304,15 +480,51 @@ UInt_t PRunSingleHistoRRF::GetNoOfFitBins()
// SetFitRangeBin (public)
//--------------------------------------------------------------------------
/**
* <p>Allows to change the fit range on the fly. Used in the COMMAND block.
* The syntax of the string is: FIT_RANGE fgb[+n00] lgb[-n01] [fgb[+n10] lgb[-n11] ... fgb[+nN0] lgb[-nN1]].
* If only one pair of fgb/lgb is given, it is used for all runs in the RUN block section.
* If multiple fgb/lgb's are given, the number N has to be the number of RUN blocks in
* the msr-file.
* \brief Sets fit range using bin-offset syntax from COMMANDS block.
*
* <p>nXY are offsets which can be used to shift, limit the fit range.
* Allows dynamic modification of the fit range during fitting, as specified
* in the COMMANDS block. This is used for systematic studies where the fit
* range needs to be varied.
*
* \param fitRange string containing the necessary information.
* <b>Syntax:</b>
* \code
* FIT_RANGE fgb[+n0] lgb[-n1] # single range for all runs
* FIT_RANGE fgb+n00 lgb-n01 fgb+n10 lgb-n11 ... # per-run ranges
* \endcode
*
* where:
* - \c fgb = first good bin (symbolic, replaced by actual value)
* - \c lgb = last good bin (symbolic, replaced by actual value)
* - \c +n0 = positive offset added to fgb
* - \c -n1 = positive offset subtracted from lgb
*
* <b>Conversion to Time:</b>
* \f[
* t_{\rm start} = ({\rm fgb} + n_0 - t_0) \times \Delta t
* \f]
* \f[
* t_{\rm end} = ({\rm lgb} - n_1 - t_0) \times \Delta t
* \f]
*
* where Δt is the raw time resolution (before RRF packing).
*
* <b>Multiple Run Handling:</b>
* - Single pair: Applied to all runs
* - Multiple pairs: Must match number of RUN blocks; each run uses its own range
* - Run selection: Uses (2 × runNo + 1) to index into token array
*
* <b>Example:</b>
* \code
* COMMANDS
* FIT_RANGE fgb+100 lgb-500 # start 100 bins after fgb, end 500 bins before lgb
* \endcode
*
* \param fitRange String containing FIT_RANGE specification from COMMANDS block
*
* \warning Invalid syntax produces error message but does not throw exception.
* The previous fit range values are retained on error.
*
* \see GetProperFitRange(), CalcNoOfFitBins()
*/
void PRunSingleHistoRRF::SetFitRangeBin(const TString fitRange)
{
@@ -394,7 +606,37 @@ void PRunSingleHistoRRF::SetFitRangeBin(const TString fitRange)
// CalcNoOfFitBins (public)
//--------------------------------------------------------------------------
/**
* <p>Calculate the number of fitted bins for the current fit range.
* \brief Calculates start/end bin indices from fit time range.
*
* Converts the fit range from time (μs) to RRF-packed bin indices.
* This method is called whenever the fit range may have changed.
*
* <b>Conversion Formulas:</b>
* \f[
* i_{\rm start} = \lceil \frac{t_{\rm start} - t_{\rm data,0}}{\Delta t_{\rm data}} \rceil
* \f]
* \f[
* i_{\rm end} = \lfloor \frac{t_{\rm end} - t_{\rm data,0}}{\Delta t_{\rm data}} \rfloor + 1
* \f]
*
* where:
* - \f$t_{\rm data,0}\f$ = fData.GetDataTimeStart() (first RRF-packed bin center)
* - \f$\Delta t_{\rm data}\f$ = fData.GetDataTimeStep() (RRF-packed bin width)
*
* <b>Bounds Checking:</b>
* - fStartTimeBin clamped to [0, data size)
* - fEndTimeBin clamped to [0, data size]
* - fNoOfFitBins = 0 if fEndTimeBin <= fStartTimeBin
*
* <b>Side Effects:</b>
* Updates member variables:
* - fStartTimeBin: First bin index in fit range (inclusive)
* - fEndTimeBin: Last bin index in fit range (exclusive)
* - fNoOfFitBins: Number of bins = fEndTimeBin - fStartTimeBin
*
* \note Time step includes RRF packing: dataTimeStep = rawTimeRes × fRRFPacking
*
* \see GetNoOfFitBins(), SetFitRangeBin()
*/
void PRunSingleHistoRRF::CalcNoOfFitBins()
{
@@ -416,17 +658,50 @@ void PRunSingleHistoRRF::CalcNoOfFitBins()
// PrepareData (protected)
//--------------------------------------------------------------------------
/**
* <p>Prepare data for fitting or viewing. What is already processed at this stage:
* -# get proper raw run data
* -# get all needed forward histograms
* -# get time resolution
* -# get t0's and perform necessary cross checks (e.g. if t0 of msr-file (if present) are consistent with t0 of the data files, etc.)
* -# add runs (if addruns are present)
* -# group histograms (if grouping is present)
* \brief Main data preparation orchestrator for RRF single histogram analysis.
*
* <b>return:</b>
* - true if everthing went smooth
* - false, otherwise.
* Coordinates the loading and preprocessing of histogram data before RRF
* transformation. This method handles all operations common to both fitting
* and viewing modes.
*
* <b>Processing Steps:</b>
* -# Validate object state (return false if already marked invalid)
* -# Get GLOBAL block reference for settings
* -# Load raw run data from data handler using run name from MSR file
* -# Extract metadata from data file:
* - Magnetic field (fMetaData.fField)
* - Beam energy (fMetaData.fEnergy)
* - Sample temperature(s) (fMetaData.fTemp)
* -# Collect histogram numbers from RUN block forward specification
* -# Validate all specified histograms exist in data file
* -# Determine time resolution: ns → μs conversion
* -# Determine t0 values via GetProperT0()
* -# Initialize forward histogram from first group
* -# Handle addrun (co-add multiple runs with t0 alignment)
* -# Handle grouping (combine multiple detectors with t0 alignment)
* -# Determine data range via GetProperDataRange()
* -# Determine fit range via GetProperFitRange()
* -# Dispatch to PrepareFitData() or PrepareViewData() based on tag
*
* <b>Run Addition (addrun):</b>
* When multiple runs are specified in the RUN block, histograms are co-added
* with t0 alignment:
* \code
* fForward[i] += addRunData[i + addT0 - mainT0]
* \endcode
*
* <b>Detector Grouping:</b>
* When multiple forward histograms are specified, they are summed with
* t0 alignment to form a single combined histogram.
*
* \return true if data preparation succeeds, false on any error:
* - Run data not found
* - Histogram not present in data file
* - Invalid t0 determination
* - Data range validation failure
* - Fit/view data preparation failure
*
* \see PrepareFitData(), PrepareViewData(), GetProperT0(), GetProperDataRange()
*/
Bool_t PRunSingleHistoRRF::PrepareData()
{
@@ -560,21 +835,73 @@ Bool_t PRunSingleHistoRRF::PrepareData()
// PrepareFitData (protected)
//--------------------------------------------------------------------------
/**
* <p>Take the pre-processed data (i.e. grouping and addrun are preformed) and form the RRF histogram for fitting.
* The following steps are preformed:
* -# get fit start/stop time
* -# check that 'first good data bin', 'last good data bin', and 't0' make any sense
* -# check how the background shall be handled, i.e. fitted, subtracted from background estimate data range, or subtacted from a given fixed background.
* -# estimate N0
* -# RRF transformation
* -# packing (i.e rebinning)
* \brief Performs full RRF transformation on histogram data for fitting.
*
* <b>return:</b>
* - true, if everything went smooth
* - false, otherwise
* Takes the pre-processed histogram (grouping and addrun already applied) and
* transforms it into RRF asymmetry suitable for fitting. This is the core
* method implementing the RRF analysis technique.
*
* \param runData raw run data handler
* \param histoNo forward histogram number
* <b>Processing Steps:</b>
*
* <b>1. Frequency Analysis:</b>
* - Calls GetMainFrequency() on raw data to find dominant precession frequency
* - Used to determine optimal N₀ estimation window (integer oscillation cycles)
* - Prints "optimal packing" suggestion for user reference
*
* <b>2. Background Handling:</b>
* - If fixed background specified: use directly from RUN block
* - If background range given: estimate via EstimateBkg()
* - If neither: estimate range as [0.1×t0, 0.6×t0] with warning
* - Subtract background: N(t) → N(t) - B
*
* <b>3. Lifetime Correction:</b>
* \f[
* M(t) = [N(t) - B] \cdot e^{+t/\tau_\mu}
* \f]
* - Removes exponential decay from signal
* - Error: \f$\sigma_M = e^{+t/\tau_\mu} \sqrt{N(t) + \sigma_B^2}\f$
* - Weights: \f$w = 1/\sigma_M^2\f$
*
* <b>4. N₀ Estimation:</b>
* - Call EstimateN0() with frequency information
* - Uses average of M(t) over integer number of oscillation cycles
* - Returns N₀ and its error σ_N₀
*
* <b>5. Asymmetry Extraction:</b>
* \f[
* A(t) = \frac{M(t)}{N_0} - 1
* \f]
* Error propagation:
* \f[
* \sigma_A(t) = \frac{e^{+t/\tau_\mu}}{N_0} \sqrt{N(t) + \left(\frac{N(t)-B}{N_0}\right)^2 \sigma_{N_0}^2}
* \f]
*
* <b>6. RRF Rotation:</b>
* \f[
* A_{\rm RRF}(t) = 2 \cdot A(t) \cdot \cos(\omega_{\rm RRF} t + \phi_{\rm RRF})
* \f]
* - ω_RRF from GLOBAL block (rrf_freq converted to angular frequency)
* - φ_RRF from GLOBAL block (rrf_phase in radians)
* - Factor 2 compensates for discarded counter-rotating component
*
* <b>7. RRF Packing:</b>
* - Average over fRRFPacking consecutive bins
* - Data: \f$A_{\rm packed} = \frac{1}{n}\sum_{i=1}^{n} A_{\rm RRF}(t_i)\f$
* - Error: \f$\sigma_{\rm packed} = \frac{\sqrt{2}}{n}\sqrt{\sum_{i=1}^{n} \sigma_A^2(t_i)}\f$
* - √2 factor accounts for doubling in RRF rotation
*
* <b>8. Time Grid Setup:</b>
* - Data time start: accounts for packing offset (center of first packed bin)
* - Data time step: rawTimeResolution × fRRFPacking
*
* \param runData Raw run data handler (for background estimation)
* \param histoNo Forward histogram number (0-based, for background error messages)
*
* \return true on success, false on error:
* - Background estimation failure
* - Invalid bin ranges
*
* \see PrepareViewData(), GetMainFrequency(), EstimateN0(), EstimateBkg()
*/
Bool_t PRunSingleHistoRRF::PrepareFitData(PRawRunData* runData, const UInt_t histoNo)
{
@@ -712,20 +1039,55 @@ Bool_t PRunSingleHistoRRF::PrepareFitData(PRawRunData* runData, const UInt_t his
// PrepareViewData (protected)
//--------------------------------------------------------------------------
/**
* <p>Take the pre-processed data (i.e. grouping and addrun are preformed) and form the histogram for viewing
* with life time correction, i.e. the exponential decay is removed.
* <p>The following steps are preformed:
* -# check if view packing is whished.
* -# check that 'first good data bin', 'last good data bin', and 't0' makes any sense
* -# transform data sets (see below).
* -# calculate theory
* \brief Prepares RRF-transformed data for viewing/plotting.
*
* <b>return:</b>
* - true, if everything went smooth
* - false, otherwise
* Takes the pre-processed histogram and prepares both data and theory curves
* for display. This method is used when the operation mode is kView rather
* than kFit.
*
* \param runData raw run data handler
* \param histoNo forward histogram number
* <b>Processing Steps:</b>
*
* <b>1. Data Preparation:</b>
* - Calls PrepareFitData() to perform full RRF transformation
* - Data is ready for display after this step
*
* <b>2. View Packing Check:</b>
* - Checks if additional view packing is specified in PLOT block
* - If viewPacking > fRRFPacking: additional packing would be applied (TODO)
* - If viewPacking < fRRFPacking: warning issued and view packing ignored
* (cannot unpack already-packed RRF data)
*
* <b>3. Theory Grid Setup:</b>
* Determines theory evaluation resolution:
* - If fTheoAsData = true: theory calculated only at data points
* - If fTheoAsData = false: theory calculated on 8× finer grid for smooth curves
*
* Time grid:
* - Theory time start = data time start
* - Theory time step = data time step / factor (where factor = 1 or 8)
*
* <b>4. Theory Evaluation:</b>
* - Extract parameter values from MSR parameter list
* - Evaluate all user-defined functions from FUNCTIONS block
* - Loop over theory grid points
* - Evaluate theory: P(t) = Func(t, par, funcValues)
* - Apply sanity check: |P(t)| > 10 → set to 0 (dirty hack for edge cases)
* - Store results via fData.AppendTheoryValue()
*
* <b>Theory Function:</b>
* The theory is evaluated directly in the RRF frame. The THEORY block should
* specify the low-frequency signal after RRF transformation, not the
* laboratory-frame high-frequency precession.
*
* \param runData Raw run data handler (passed to PrepareFitData)
* \param histoNo Forward histogram number (passed to PrepareFitData)
*
* \return true on success, false on error (typically from PrepareFitData)
*
* \note The 8× theory resolution provides smoother curves for visualization
* and better Fourier transforms without affecting fit performance.
*
* \see PrepareFitData(), CalcTheory()
*/
Bool_t PRunSingleHistoRRF::PrepareViewData(PRawRunData* runData, const UInt_t histoNo)
{
@@ -793,21 +1155,52 @@ Bool_t PRunSingleHistoRRF::PrepareViewData(PRawRunData* runData, const UInt_t hi
// GetProperT0 (private)
//--------------------------------------------------------------------------
/**
* <p>Get the proper t0 for the single histogram run.
* -# the t0 vector size = number of detectors (grouping) for forward.
* -# initialize t0's with -1
* -# fill t0's from RUN block
* -# if t0's are missing (i.e. t0 == -1), try to fill from the GLOBAL block.
* -# if t0's are missing, try t0's from the data file
* -# if t0's are missing, try to estimate them
* \brief Determines and validates t0 values for all histograms in the run.
*
* \param runData pointer to the current RUN block entry from the msr-file
* \param globalBlock pointer to the GLOBLA block entry from the msr-file
* \param histoNo histogram number vector of forward; histoNo = msr-file forward + redGreen_offset - 1
* Searches for time-zero (t0) values through a priority cascade and performs
* validation. t0 marks the muon arrival time and is critical for proper
* timing alignment.
*
* <b>return:</b>
* - true if everthing went smooth
* - false, otherwise.
* <b>t0 Search Priority:</b>
* -# RUN block: t0 values specified in the MSR file RUN block
* -# GLOBAL block: fallback t0 values from GLOBAL block
* -# Data file: t0 values stored in the raw data file header
* -# Automatic estimation: estimated from histogram shape (with warning)
*
* <b>Algorithm:</b>
* -# Initialize fT0s vector with size = number of forward histograms (grouping)
* -# Set all t0 values to -1.0 (unset marker)
* -# Fill from RUN block if present
* -# Fill remaining -1.0 values from GLOBAL block
* -# Fill remaining -1.0 values from data file
* -# Fill remaining -1.0 values from automatic estimation (with warning)
* -# Validate all t0 values are within histogram bounds
*
* <b>Addrun Handling:</b>
* When multiple runs are co-added (addrun), each additional run needs its own
* t0 value for proper time alignment:
* -# Initialize fAddT0s[runIdx] for each addrun
* -# Apply same priority cascade for each addrun
* -# Validate addrun t0 values
*
* <b>Validation:</b>
* - t0 must be ≥ 0
* - t0 must be < histogram length
* - Error message and return false on validation failure
*
* \param runData Pointer to raw run data for histogram access and data file t0
* \param globalBlock Pointer to GLOBAL block for fallback t0 values
* \param histoNo Vector of histogram indices (0-based) for forward grouping
*
* \return true if valid t0 found for all histograms, false on:
* - t0 out of histogram bounds
* - addrun data not accessible
* - addrun t0 validation failure
*
* \warning Automatic t0 estimation is unreliable for LEM and other specialized
* setups. Always specify t0 explicitly for best results.
*
* \see GetProperDataRange(), PrepareData()
*/
Bool_t PRunSingleHistoRRF::GetProperT0(PRawRunData* runData, PMsrGlobalBlock *globalBlock, PUIntVector &histoNo)
{
@@ -931,14 +1324,40 @@ Bool_t PRunSingleHistoRRF::GetProperT0(PRawRunData* runData, PMsrGlobalBlock *gl
// GetProperDataRange (private)
//--------------------------------------------------------------------------
/**
* <p>Get the proper data range, i.e. first/last good bin (fgb/lgb).
* -# get fgb/lgb from the RUN block
* -# if fgb/lgb still undefined, try to get it from the GLOBAL block
* -# if fgb/lgb still undefined, try to estimate them.
* \brief Determines valid data range (first/last good bins) for analysis.
*
* <b>return:</b>
* - true if everthing went smooth
* - false, otherwise.
* Establishes the boundaries of usable histogram data through a priority
* cascade. The data range defines which bins contain valid signal (after t0,
* before noise/overflow).
*
* <b>Data Range Search Priority:</b>
* -# RUN block: data range specified in MSR file RUN block
* -# GLOBAL block: fallback data range from GLOBAL block
* -# Automatic estimation: estimate from t0 (with warning)
* - Start: t0 + 10 ns offset
* - End: histogram length
*
* <b>Validation:</b>
* -# Check start < end (swap if reversed)
* -# Check start ≥ 0 and start < histogram size
* -# Check end ≥ 0 (adjust if > histogram size with warning)
*
* <b>Result:</b>
* Sets fGoodBins[0] (first good bin) and fGoodBins[1] (last good bin).
* These are used for:
* - RRF transformation range
* - Fit range specification in bins (fgb+n0 lgb-n1)
* - Data validity checking
*
* \return true if valid data range determined, false on:
* - Start bin out of bounds
* - End bin negative
* - Other range validation failures
*
* \note The data range is typically wider than the fit range. Data range
* defines where valid data exists; fit range defines what is fitted.
*
* \see GetProperFitRange(), GetProperT0()
*/
Bool_t PRunSingleHistoRRF::GetProperDataRange()
{
@@ -1019,16 +1438,45 @@ Bool_t PRunSingleHistoRRF::GetProperDataRange()
// GetProperFitRange (private)
//--------------------------------------------------------------------------
/**
* <p>Get the proper fit range. There are two possible fit range commands:
* fit <start> <end> given in (usec), or
* fit fgb+offset_0 lgb-offset_1 given in (bins), therefore it works the following way:
* -# get fit range assuming given in time from RUN block
* -# if fit range in RUN block is given in bins, replace start/end
* -# if fit range is NOT given yet, try fit range assuming given in time from GLOBAL block
* -# if fit range in GLOBAL block is given in bins, replace start/end
* -# if still no fit range is given, use fgb/lgb.
* \brief Determines fit time range from MSR file settings.
*
* \param globalBlock pointer to the GLOBAL block information form the msr-file.
* Extracts the fit range (time window for parameter extraction) through a
* priority cascade. Supports both time-based and bin-based specifications.
*
* <b>Fit Range Formats:</b>
* - Time-based: \c fit \c 0.1 \c 8.0 (in microseconds relative to t0)
* - Bin-based: \c fit \c fgb+100 \c lgb-500 (bins relative to good bins)
*
* <b>Search Priority:</b>
* -# RUN block time-based fit range
* -# RUN block bin-based fit range (converted to time)
* -# GLOBAL block time-based fit range
* -# GLOBAL block bin-based fit range (converted to time)
* -# Default to data range (fgb to lgb) with warning
*
* <b>Bin-to-Time Conversion:</b>
* \f[
* t_{\rm start} = ({\rm fgb} + n_0 - t_0) \times \Delta t
* \f]
* \f[
* t_{\rm end} = ({\rm lgb} - n_1 - t_0) \times \Delta t
* \f]
*
* where Δt = fTimeResolution (raw, before RRF packing).
*
* <b>Result:</b>
* Sets fFitStartTime and fFitEndTime in microseconds relative to t0.
* These define the range used in χ² calculation.
*
* \param globalBlock Pointer to GLOBAL block for fallback fit range settings
*
* \note The converted time values are written back to the MSR data structures
* for log file output consistency.
*
* \warning If no fit range is specified, the full data range is used with
* a warning message. This may not be appropriate for all analyses.
*
* \see GetProperDataRange(), SetFitRangeBin(), CalcNoOfFitBins()
*/
void PRunSingleHistoRRF::GetProperFitRange(PMsrGlobalBlock *globalBlock)
{
@@ -1067,9 +1515,41 @@ void PRunSingleHistoRRF::GetProperFitRange(PMsrGlobalBlock *globalBlock)
// GetMainFrequency (private)
//--------------------------------------------------------------------------
/**
* <p>gets the maximum frequency of the given data.
* \brief Finds the dominant precession frequency in raw histogram data.
*
* \param raw data set.
* Performs Fourier analysis on the raw histogram to identify the main muon
* spin precession frequency. This is essential for:
* - Determining optimal N₀ estimation window (integer oscillation cycles)
* - Calculating suggested "optimal packing" for user information
* - Validating that the RRF frequency is appropriate
*
* <b>Algorithm:</b>
* -# Create ROOT TH1F histogram from data in good bin range [fGoodBins[0], fGoodBins[1]]
* -# Set histogram binning to match time structure
* -# Apply Fourier transform using PFourier class
* -# Use strong apodization (windowing) to reduce spectral leakage
* -# Search power spectrum for maximum above 10 MHz (ignores DC component)
* -# Return frequency at maximum power
*
* <b>Frequency Search Constraints:</b>
* - Ignores frequencies below 10 MHz (DC and low-frequency noise)
* - Searches for local maximum (rising then falling power)
* - Returns 0 if no clear maximum found
*
* <b>Output Information:</b>
* The method prints diagnostic information:
* - Detected maximum frequency (MHz)
* - Suggested optimal packing for ~5-8 points per cycle
*
* \param data Raw histogram data vector (counts per bin)
*
* \return Maximum frequency in MHz, or 0 if no maximum found above 10 MHz.
*
* \note The frequency is used to determine the N₀ estimation window:
* window = ceil(fN0EstimateEndTime × freqMax / 2π) × (2π / freqMax)
* This ensures an integer number of complete oscillation cycles.
*
* \see EstimateN0(), PrepareFitData()
*/
Double_t PRunSingleHistoRRF::GetMainFrequency(PDoubleVector &data)
{
@@ -1117,9 +1597,50 @@ Double_t PRunSingleHistoRRF::GetMainFrequency(PDoubleVector &data)
// EstimateN0 (private)
//--------------------------------------------------------------------------
/**
* <p>Estimate the N0 for the given run.
* \brief Estimates initial normalization N from lifetime-corrected histogram data.
*
* \param errN0
* Calculates N₀ by averaging the lifetime-corrected signal M(t) over an initial
* time window. The window is chosen to span an integer number of complete
* precession cycles to minimize bias from oscillations.
*
* <b>N₀ Estimation Formula:</b>
* \f[
* N_0 = \frac{1}{n} \sum_{i=0}^{n-1} M(t_i)
* \f]
*
* where M(t) = [N(t) - B] × exp(+t/τ_μ) is the lifetime-corrected histogram
* stored in fM vector.
*
* <b>Window Determination:</b>
* The end bin is calculated to include an integer number of oscillation cycles:
* \f[
* n_{\rm end} = {\rm round}\left( \left\lceil \frac{T \cdot f_{\rm max}}{2\pi} \right\rceil \cdot \frac{2\pi}{f_{\rm max} \cdot \Delta t} \right)
* \f]
*
* where:
* - T = fN0EstimateEndTime (default 1.0 μs)
* - f_max = main precession frequency from GetMainFrequency()
* - Δt = fTimeResolution
*
* <b>Error Estimation:</b>
* \f[
* \sigma_{N_0} = \frac{\sqrt{\sum_{i=0}^{n-1} w_i^2 \sigma_{M_i}^2}}{\sum_{i=0}^{n-1} w_i}
* \f]
*
* where w_i = 1/σ_M_i² are the weights stored in fW vector.
*
* <b>Output Information:</b>
* Prints diagnostic message: "N₀ = value (error)"
*
* \param errN0 [out] Reference to store the estimated error on N₀
* \param freqMax Main precession frequency (MHz) from GetMainFrequency()
*
* \return Estimated N₀ value (counts × exp(+t/τ))
*
* \note The simple average (rather than weighted average) is used for N₀ itself,
* while the error uses the full weight information.
*
* \see GetMainFrequency(), PrepareFitData()
*/
Double_t PRunSingleHistoRRF::EstimateN0(Double_t &errN0, Double_t freqMax)
{
@@ -1152,13 +1673,47 @@ Double_t PRunSingleHistoRRF::EstimateN0(Double_t &errN0, Double_t freqMax)
// EstimatBkg (private)
//--------------------------------------------------------------------------
/**
* <p>Estimate the background for a given interval.
* \brief Estimates background level and error from pre-t0 histogram bins.
*
* <b>return:</b>
* - true, if everything went smooth
* - false, otherwise
* Calculates the background (random coincidences, accidentals) from a
* specified range of histogram bins, typically before t0. The background
* range is adjusted to span an integer number of accelerator RF cycles
* for proper averaging.
*
* \param histoNo forward histogram number of the run
* <b>Accelerator RF Periods:</b>
* - PSI: ACCEL_PERIOD_PSI ns
* - RAL: ACCEL_PERIOD_RAL ns
* - TRIUMF: ACCEL_PERIOD_TRIUMF ns
* - Unknown: no RF adjustment
*
* <b>Algorithm:</b>
* -# Get background range [start, end] from RUN block
* -# Swap start/end if in wrong order (with message)
* -# Determine accelerator RF period from institute name
* -# Adjust end to span integer number of RF cycles
* -# Validate range is within histogram bounds
* -# Calculate mean background:
* \f$ B = \frac{1}{n}\sum_{i={\rm start}}^{{\rm end}} N(i) \f$
* -# Calculate background error (standard deviation):
* \f$ \sigma_B = \sqrt{\frac{1}{n-1}\sum_{i={\rm start}}^{{\rm end}} (N(i) - B)^2} \f$
* -# Store results in fBackground and fBkgErr
* -# Update RUN block with estimated background value
*
* <b>Output Information:</b>
* Prints diagnostic messages:
* - Adjusted background range (if RF adjustment applied)
* - Background value and error: "fBackground = value (error)"
*
* \param histoNo Forward histogram number (for error message context)
*
* \return true on success, false on error:
* - Background start out of histogram bounds
* - Background end out of histogram bounds
*
* \note The RF cycle adjustment ensures proper averaging over the
* accelerator bunch structure, reducing systematic bias.
*
* \see PrepareFitData()
*/
Bool_t PRunSingleHistoRRF::EstimateBkg(UInt_t histoNo)
{