Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-16 09:08:15

0001 // @(#)root/minuit2:$Id$
0002 // Authors: M. Winkler, F. James, L. Moneta, A. Zsenei   2003-2005
0003 
0004 /**********************************************************************
0005  *                                                                    *
0006  * Copyright (c) 2005 LCG ROOT Math team,  CERN/PH-SFT                *
0007  *                                                                    *
0008  **********************************************************************/
0009 
0010 #ifndef ROOT_Minuit2_FumiliStandardChi2FCN
0011 #define ROOT_Minuit2_FumiliStandardChi2FCN
0012 
0013 #include "Minuit2/FumiliChi2FCN.h"
0014 #include "Minuit2/ParametricFunction.h"
0015 
0016 #include <ROOT/RSpan.hxx>
0017 
0018 #include <cassert>
0019 #include <vector>
0020 #include <cmath>
0021 
0022 namespace ROOT {
0023 
0024 namespace Minuit2 {
0025 
0026 /**
0027 
0028 Class implementing the standard chi square function, which
0029 is the sum of the squares of the figures-of-merit calculated for each measurement
0030 point, the individual figures-of-merit being: (the Value predicted by the
0031 model-measured Value)/standard deviation.
0032 
0033 @author Andras Zsenei and Lorenzo Moneta, Creation date: 31 Aug 2004
0034 
0035 @see FumiliChi2FCN
0036 
0037 @ingroup Minuit
0038 
0039 \todo nice formula for the documentation...
0040 
0041 */
0042 
0043 class FumiliStandardChi2FCN : public FumiliChi2FCN {
0044 
0045 public:
0046    /**
0047 
0048    Constructor which initializes chi square function for one-dimensional model function
0049 
0050    @param modelFCN the model function used for describing the data.
0051 
0052    @param meas vector containing the measured values.
0053 
0054    @param pos vector containing the x values corresponding to the
0055    measurements
0056 
0057    @param mvar vector containing the variances corresponding to each
0058    measurement (where the variance equals the standard deviation squared).
0059    If the variances are zero, a Value of 1 is used (as it is done in ROOT/PAW)
0060 
0061    */
0062 
0063    FumiliStandardChi2FCN(const ParametricFunction &modelFCN, std::span<const double> meas,
0064                          std::span<const double> pos, std::span<const double> mvar)
0065    { // this->fModelFCN = &modelFunction;
0066       this->SetModelFunction(modelFCN);
0067 
0068       assert(meas.size() == pos.size());
0069       assert(meas.size() == mvar.size());
0070       fMeasurements.assign(meas.begin(), meas.end());
0071       std::vector<double> x(1);
0072       unsigned int n = mvar.size();
0073       fPositions.reserve(n);
0074       // correct for variance == 0
0075       fInvErrors.resize(n);
0076       for (unsigned int i = 0; i < n; ++i) {
0077          x[0] = pos[i];
0078          fPositions.push_back(x);
0079          // PAW/ROOT hack : use 1 for 0 entries bins
0080          if (mvar[i] == 0)
0081             fInvErrors[i] = 1;
0082          else
0083             fInvErrors[i] = 1.0 / std::sqrt(mvar[i]);
0084       }
0085    }
0086 
0087    /**
0088 
0089    Constructor which initializes the multi-dimensional model function.
0090 
0091    @param modelFCN the model function used for describing the data.
0092 
0093    @param meas vector containing the measured values.
0094 
0095    @param pos vector containing the x values corresponding to the
0096    measurements
0097 
0098    @param mvar vector containing the variances corresponding to each
0099    measurement (where the variance equals the standard deviation squared).
0100    If the variances are zero, a Value of 1 is used (as it is done in ROOT/PAW)
0101 
0102    */
0103 
0104    FumiliStandardChi2FCN(const ParametricFunction &modelFCN, std::span<const double> meas,
0105                          std::span<const std::vector<double>> pos, std::span<const double> mvar)
0106    { // this->fModelFCN = &modelFunction;
0107       this->SetModelFunction(modelFCN);
0108 
0109       assert(meas.size() == pos.size());
0110       assert(meas.size() == mvar.size());
0111       fMeasurements.assign(meas.begin(), meas.end());
0112       fPositions.assign(pos.begin(), pos.end());
0113       // correct for variance == 0
0114       unsigned int n = mvar.size();
0115       fInvErrors.resize(n);
0116       for (unsigned int i = 0; i < n; ++i) {
0117          // PAW/ROOT hack : use 1 for 0 entries bins
0118          if (mvar[i] == 0)
0119             fInvErrors[i] = 1;
0120          else
0121             fInvErrors[i] = 1.0 / std::sqrt(mvar[i]);
0122       }
0123    }
0124 
0125    /**
0126 
0127    Evaluates the model function for the different measurement points and
0128    the Parameter values supplied, calculates a figure-of-merit for each
0129    measurement and returns a vector containing the result of this
0130    evaluation. The figure-of-merit is (Value predicted by the model
0131    function-measured Value)/standard deviation.
0132 
0133    @param par vector of Parameter values to feed to the model function.
0134 
0135    @return A vector containing the figures-of-merit for the model function evaluated
0136    for each set of measurements.
0137 
0138    \todo What to do when the variances are 0???!! (right now just pushes back 0...)
0139 
0140    */
0141 
0142    std::vector<double> Elements(std::vector<double> const &par) const override;
0143 
0144    /**
0145 
0146    Accessor to the position of the measurement (x coordinate).
0147 
0148    @param Index Index of the measuerement the position of which to return.
0149 
0150    @return the position of the measurement.
0151 
0152    */
0153 
0154    const std::vector<double> &GetMeasurement(int Index) const override;
0155 
0156    /**
0157 
0158    Accessor to the number of measurements used for calculating
0159    the chi-square.
0160 
0161    @return the number of measurements.
0162 
0163    */
0164 
0165    int GetNumberOfMeasurements() const override;
0166 
0167    /**
0168 
0169    Evaluate function Value, Gradient and Hessian using Fumili approximation, for values of parameters p
0170    The result is cached inside and is return from the FumiliFCNBase::Value ,  FumiliFCNBase::Gradient and
0171    FumiliFCNBase::Hessian methods
0172 
0173    @param par vector of parameters
0174 
0175    **/
0176 
0177    void EvaluateAll(std::vector<double> const &par) override;
0178 
0179 private:
0180    std::vector<double> fMeasurements;
0181    // support multi dim coordinates
0182    std::vector<std::vector<double>> fPositions;
0183    std::vector<double> fInvErrors;
0184 };
0185 
0186 } // namespace Minuit2
0187 
0188 } // namespace ROOT
0189 
0190 #endif // ROOT_Minuit2_FumiliStandardChi2FCN