// @(#)root/minuit2:$Id$
// Authors: M. Winkler, F. James, L. Moneta, A. Zsenei   2003-2005

/**********************************************************************
 *                                                                    *
 * Copyright (c) 2005 LCG ROOT Math team,  CERN/PH-SFT                *
 *                                                                    *
 **********************************************************************/

#ifndef ROOT_Minuit2_MnUserParameterState
#define ROOT_Minuit2_MnUserParameterState

#include "Minuit2/MnUserParameters.h"
#include "Minuit2/MnUserCovariance.h"
#include "Minuit2/MnGlobalCorrelationCoeff.h"

namespace ROOT {

   namespace Minuit2 {


class MinimumState;

//_____________________________________________________________________________
/**
    class which holds the external user and/or internal Minuit representation
    of the parameters and errors;
    transformation internal <-> external on demand;
 */

class MnUserParameterState {

public:

   /// default constructor (invalid state)
   MnUserParameterState() : fValid(false), fCovarianceValid(false), fGCCValid(false), fCovStatus(-1), fFVal(0), fEDM(0), fNFcn(0),
                            fParameters(MnUserParameters()), fCovariance(MnUserCovariance()),
                            fIntParameters(std::vector<double>()), fIntCovariance(MnUserCovariance()) {}

   /// construct from user parameters (before minimization)
   MnUserParameterState(const std::vector<double>&, const std::vector<double>&);

   MnUserParameterState(const MnUserParameters&);

   /// construct from user parameters + covariance (before minimization)
   MnUserParameterState(const std::vector<double>&, const std::vector<double>&, unsigned int);

   MnUserParameterState(const std::vector<double>&, const MnUserCovariance&);

   MnUserParameterState(const MnUserParameters&, const MnUserCovariance&);

   /// construct from internal parameters (after minimization)
   MnUserParameterState(const MinimumState&, double, const MnUserTransformation&);

   ~MnUserParameterState() {}

   MnUserParameterState(const MnUserParameterState& state) : fValid(state.fValid),
                                                             fCovarianceValid(state.fCovarianceValid), fGCCValid(state.fGCCValid), fCovStatus(state.fCovStatus),
                                                             fFVal(state.fFVal), fEDM(state.fEDM), fNFcn(state.fNFcn),
                                                             fParameters(state.fParameters),
                                                             fCovariance(state.fCovariance),
                                                             fGlobalCC(state.fGlobalCC), fIntParameters(state.fIntParameters), fIntCovariance(state.fIntCovariance) {}

   MnUserParameterState& operator=(const MnUserParameterState& state) {
      if(this != &state) {
         fValid = state.fValid;
         fCovarianceValid = state.fCovarianceValid;
         fGCCValid = state.fGCCValid;
         fCovStatus = state.fCovStatus;
         fFVal = state.fFVal;
         fEDM = state.fEDM;
         fNFcn = state.fNFcn;
         fParameters = state.fParameters;
         fCovariance = state.fCovariance;
         fGlobalCC = state.fGlobalCC;
         fIntParameters = state.fIntParameters;
         fIntCovariance = state.fIntCovariance;
      }
      return *this;
   }

   //user external representation
   const MnUserParameters& Parameters() const {return fParameters;}
   const MnUserCovariance& Covariance() const {return fCovariance;}
   const MnGlobalCorrelationCoeff& GlobalCC() const {return fGlobalCC;}

   // hessian (inverse of covariance matrix)
   MnUserCovariance Hessian() const;

   //Minuit internal representation
   const std::vector<double>& IntParameters() const {return fIntParameters;}
   const MnUserCovariance& IntCovariance() const {return fIntCovariance;}

   // covariance matrix status (0 = not valid, 1 approximate, 2, full but made pos def, 3 accurate and not pos def
   int CovarianceStatus() const { return fCovStatus; }

   //transformation internal <-> external
   const MnUserTransformation& Trafo() const {return fParameters.Trafo();}

   bool IsValid() const {return fValid;}
   bool HasCovariance() const {return fCovarianceValid;}
   bool HasGlobalCC() const {return fGCCValid;}

   double Fval() const {return fFVal;}
   double Edm() const {return fEDM;}
   unsigned int NFcn() const {return fNFcn;}


public:

   /** facade: forward interface of MnUserParameters and MnUserTransformation */

   //access to parameters (row-wise)
   const std::vector<ROOT::Minuit2::MinuitParameter>& MinuitParameters() const;
   //access to parameters and errors in column-wise representation
   std::vector<double> Params() const;
   std::vector<double> Errors() const;

   //access to single Parameter
   const MinuitParameter& Parameter(unsigned int i) const;

   //add free Parameter
   void Add(const std::string & name, double val, double err);
   //add limited Parameter
   void Add(const std::string & name, double val, double err, double , double);
   //add const Parameter
   void Add(const std::string &, double);

   //interaction via external number of Parameter
   void Fix(unsigned int);
   void Release(unsigned int);
   void RemoveLimits(unsigned int);
   void SetValue(unsigned int, double);
   void SetError(unsigned int, double);
   void SetLimits(unsigned int, double, double);
   void SetUpperLimit(unsigned int, double);
   void SetLowerLimit(unsigned int, double);
   void SetName(unsigned int iext, const std::string &name) { fParameters.SetName(iext,name); }

   double Value(unsigned int) const;
   double Error(unsigned int) const;

   //interaction via Name of Parameter
   void Fix(const std::string &);
   void Release(const std::string &);
   void SetValue(const std::string &, double);
   void SetError(const std::string &, double);
   void SetLimits(const std::string &, double, double);
   void SetUpperLimit(const std::string &, double);
   void SetLowerLimit(const std::string &, double);
   void RemoveLimits(const std::string &);

   double Value(const std::string &) const;
   double Error(const std::string &) const;

   //convert Name into external number of Parameter
   unsigned int Index(const std::string &) const;
   //convert external number into Name of Parameter
   const std::string & GetName(unsigned int) const;
   // mantain interface with const char * for backward compatibility
   const char* Name(unsigned int) const;

   // transformation internal <-> external
   double Int2ext(unsigned int, double) const;
   double Ext2int(unsigned int, double) const;
   unsigned int IntOfExt(unsigned int) const;
   unsigned int ExtOfInt(unsigned int) const;
   unsigned int VariableParameters() const;
   const MnMachinePrecision& Precision() const;
   void SetPrecision(double eps);


private:

   bool fValid;
   bool fCovarianceValid;
   bool fGCCValid;
   int  fCovStatus; // covariance matrix status
   double fFVal;
   double fEDM;
   unsigned int fNFcn;

   MnUserParameters fParameters;
   MnUserCovariance fCovariance;
   MnGlobalCorrelationCoeff fGlobalCC;

   std::vector<double> fIntParameters;
   MnUserCovariance fIntCovariance;

};

  }  // namespace Minuit2

}  // namespace ROOT

#endif  // ROOT_Minuit2_MnUserParameterState
 MnUserParameterState.h:1
 MnUserParameterState.h:2
 MnUserParameterState.h:3
 MnUserParameterState.h:4
 MnUserParameterState.h:5
 MnUserParameterState.h:6
 MnUserParameterState.h:7
 MnUserParameterState.h:8
 MnUserParameterState.h:9
 MnUserParameterState.h:10
 MnUserParameterState.h:11
 MnUserParameterState.h:12
 MnUserParameterState.h:13
 MnUserParameterState.h:14
 MnUserParameterState.h:15
 MnUserParameterState.h:16
 MnUserParameterState.h:17
 MnUserParameterState.h:18
 MnUserParameterState.h:19
 MnUserParameterState.h:20
 MnUserParameterState.h:21
 MnUserParameterState.h:22
 MnUserParameterState.h:23
 MnUserParameterState.h:24
 MnUserParameterState.h:25
 MnUserParameterState.h:26
 MnUserParameterState.h:27
 MnUserParameterState.h:28
 MnUserParameterState.h:29
 MnUserParameterState.h:30
 MnUserParameterState.h:31
 MnUserParameterState.h:32
 MnUserParameterState.h:33
 MnUserParameterState.h:34
 MnUserParameterState.h:35
 MnUserParameterState.h:36
 MnUserParameterState.h:37
 MnUserParameterState.h:38
 MnUserParameterState.h:39
 MnUserParameterState.h:40
 MnUserParameterState.h:41
 MnUserParameterState.h:42
 MnUserParameterState.h:43
 MnUserParameterState.h:44
 MnUserParameterState.h:45
 MnUserParameterState.h:46
 MnUserParameterState.h:47
 MnUserParameterState.h:48
 MnUserParameterState.h:49
 MnUserParameterState.h:50
 MnUserParameterState.h:51
 MnUserParameterState.h:52
 MnUserParameterState.h:53
 MnUserParameterState.h:54
 MnUserParameterState.h:55
 MnUserParameterState.h:56
 MnUserParameterState.h:57
 MnUserParameterState.h:58
 MnUserParameterState.h:59
 MnUserParameterState.h:60
 MnUserParameterState.h:61
 MnUserParameterState.h:62
 MnUserParameterState.h:63
 MnUserParameterState.h:64
 MnUserParameterState.h:65
 MnUserParameterState.h:66
 MnUserParameterState.h:67
 MnUserParameterState.h:68
 MnUserParameterState.h:69
 MnUserParameterState.h:70
 MnUserParameterState.h:71
 MnUserParameterState.h:72
 MnUserParameterState.h:73
 MnUserParameterState.h:74
 MnUserParameterState.h:75
 MnUserParameterState.h:76
 MnUserParameterState.h:77
 MnUserParameterState.h:78
 MnUserParameterState.h:79
 MnUserParameterState.h:80
 MnUserParameterState.h:81
 MnUserParameterState.h:82
 MnUserParameterState.h:83
 MnUserParameterState.h:84
 MnUserParameterState.h:85
 MnUserParameterState.h:86
 MnUserParameterState.h:87
 MnUserParameterState.h:88
 MnUserParameterState.h:89
 MnUserParameterState.h:90
 MnUserParameterState.h:91
 MnUserParameterState.h:92
 MnUserParameterState.h:93
 MnUserParameterState.h:94
 MnUserParameterState.h:95
 MnUserParameterState.h:96
 MnUserParameterState.h:97
 MnUserParameterState.h:98
 MnUserParameterState.h:99
 MnUserParameterState.h:100
 MnUserParameterState.h:101
 MnUserParameterState.h:102
 MnUserParameterState.h:103
 MnUserParameterState.h:104
 MnUserParameterState.h:105
 MnUserParameterState.h:106
 MnUserParameterState.h:107
 MnUserParameterState.h:108
 MnUserParameterState.h:109
 MnUserParameterState.h:110
 MnUserParameterState.h:111
 MnUserParameterState.h:112
 MnUserParameterState.h:113
 MnUserParameterState.h:114
 MnUserParameterState.h:115
 MnUserParameterState.h:116
 MnUserParameterState.h:117
 MnUserParameterState.h:118
 MnUserParameterState.h:119
 MnUserParameterState.h:120
 MnUserParameterState.h:121
 MnUserParameterState.h:122
 MnUserParameterState.h:123
 MnUserParameterState.h:124
 MnUserParameterState.h:125
 MnUserParameterState.h:126
 MnUserParameterState.h:127
 MnUserParameterState.h:128
 MnUserParameterState.h:129
 MnUserParameterState.h:130
 MnUserParameterState.h:131
 MnUserParameterState.h:132
 MnUserParameterState.h:133
 MnUserParameterState.h:134
 MnUserParameterState.h:135
 MnUserParameterState.h:136
 MnUserParameterState.h:137
 MnUserParameterState.h:138
 MnUserParameterState.h:139
 MnUserParameterState.h:140
 MnUserParameterState.h:141
 MnUserParameterState.h:142
 MnUserParameterState.h:143
 MnUserParameterState.h:144
 MnUserParameterState.h:145
 MnUserParameterState.h:146
 MnUserParameterState.h:147
 MnUserParameterState.h:148
 MnUserParameterState.h:149
 MnUserParameterState.h:150
 MnUserParameterState.h:151
 MnUserParameterState.h:152
 MnUserParameterState.h:153
 MnUserParameterState.h:154
 MnUserParameterState.h:155
 MnUserParameterState.h:156
 MnUserParameterState.h:157
 MnUserParameterState.h:158
 MnUserParameterState.h:159
 MnUserParameterState.h:160
 MnUserParameterState.h:161
 MnUserParameterState.h:162
 MnUserParameterState.h:163
 MnUserParameterState.h:164
 MnUserParameterState.h:165
 MnUserParameterState.h:166
 MnUserParameterState.h:167
 MnUserParameterState.h:168
 MnUserParameterState.h:169
 MnUserParameterState.h:170
 MnUserParameterState.h:171
 MnUserParameterState.h:172
 MnUserParameterState.h:173
 MnUserParameterState.h:174
 MnUserParameterState.h:175
 MnUserParameterState.h:176
 MnUserParameterState.h:177
 MnUserParameterState.h:178
 MnUserParameterState.h:179
 MnUserParameterState.h:180
 MnUserParameterState.h:181
 MnUserParameterState.h:182
 MnUserParameterState.h:183
 MnUserParameterState.h:184
 MnUserParameterState.h:185
 MnUserParameterState.h:186
 MnUserParameterState.h:187
 MnUserParameterState.h:188
 MnUserParameterState.h:189
 MnUserParameterState.h:190
 MnUserParameterState.h:191
 MnUserParameterState.h:192
 MnUserParameterState.h:193
 MnUserParameterState.h:194
 MnUserParameterState.h:195
 MnUserParameterState.h:196