TUnfold.h

Go to the documentation of this file.
00001 // @(#)root/hist:$Id: TUnfold.h 37440 2010-12-09 15:13:46Z moneta $
00002 // Author: Stefan Schmitt
00003 // DESY, 13/10/08
00004 
00005 //  Version 16, some cleanup, more getter functions, query version number
00006 //
00007 //  History:
00008 //    Version 15, simplified L-curve scan, new tau definition, new eror calc.
00009 //    Version 14, with changes in TUnfoldSys.cxx
00010 //    Version 13, new methods for derived classes
00011 //    Version 12, with support for preconditioned matrix inversion
00012 //    Version 11, regularisation methods have return values
00013 //    Version 10, with bug-fix in TUnfold.cxx
00014 //    Version 9, implements method for optimized inversion of sparse matrix
00015 //    Version 8, replace all TMatrixSparse matrix operations by private code
00016 //    Version 7, fix problem with TMatrixDSparse,TMatrixD multiplication
00017 //    Version 6, completely remove definition of class XY
00018 //    Version 5, move definition of class XY from TUnfold.C to this file
00019 //    Version 4, with bug-fix in TUnfold.C
00020 //    Version 3, with bug-fix in TUnfold.C
00021 //    Version 2, with changed ScanLcurve() arguments
00022 //    Version 1, added ScanLcurve() method
00023 //    Version 0, stable version of basic unfolding algorithm
00024 
00025 
00026 #ifndef ROOT_TUnfold
00027 #define ROOT_TUnfold
00028 
00029 //////////////////////////////////////////////////////////////////////////
00030 //                                                                      //
00031 //                                                                      //
00032 //  TUnfold solves the inverse problem                                  //
00033 //                                                                      //
00034 //                   T   -1            2          T                     //
00035 //    chi**2 = (y-Ax) Vyy  (y-Ax) + tau  (L(x-x0)) L(x-x0)              //
00036 //                                                                      //
00037 //  Monte Carlo input                                                   //
00038 //    y: vector of measured quantities  (dimension ny)                  //
00039 //    Vyy: covariance matrix for y (dimension ny x ny)                  //
00040 //    A: migration matrix               (dimension ny x nx)             //
00041 //    x: unknown underlying distribution (dimension nx)                 //
00042 //  Regularisation                                                      //
00043 //    tau: parameter, defining the regularisation strength              //
00044 //    L: matrix of regularisation conditions (dimension nl x nx)        //
00045 //    x0: underlying distribution bias                                  //
00046 //                                                                      //
00047 //  where chi**2 is minimized as a function of x                        //
00048 //                                                                      //
00049 //  The algorithm is based on "standard" matrix inversion, with the     //
00050 //  known limitations in numerical accuracy and computing cost for      //
00051 //  matrices with large dimensions.                                     //
00052 //                                                                      //
00053 //  Thus the algorithm should not used for large dimensions of x and y  //
00054 //    dim(x) should not exceed O(100)                                   //    
00055 //    dim(y) should not exceed O(500)                                   //
00056 //                                                                      //
00057 //////////////////////////////////////////////////////////////////////////
00058 
00059 
00060 #include <TH1D.h>
00061 #include <TH2D.h>
00062 #include <TObject.h>
00063 #include <TArrayI.h>
00064 #include <TSpline.h>
00065 #include <TMatrixDSparse.h>
00066 #include <TMatrixD.h>
00067 #include <TObjArray.h>
00068 
00069 #define TUnfold_VERSION "V16.0"
00070 
00071 class TUnfold : public TObject {
00072  private:
00073    void InitTUnfold(void);     // initialize all data members
00074  public:
00075    enum EConstraint {
00076       kEConstraintNone =0, // use no extra constraint
00077       kEConstraintArea =1  // enforce preservation of the area
00078    };
00079    enum ERegMode {              // regularisation scheme
00080       kRegModeNone = 0,         // no regularisation
00081       kRegModeSize = 1,         // regularise the size of the output
00082       kRegModeDerivative = 2,   // regularize the 1st derivative of the output
00083       kRegModeCurvature = 3,    // regularize the 2nd derivative of the output
00084       kRegModeMixed = 4         // mixed regularisation pattern
00085    };
00086  protected:
00087    TMatrixDSparse * fA;         // Input: matrix
00088    TMatrixDSparse *fLsquared;   // Input: regularisation conditions squared
00089    TMatrixDSparse *fVyy;        // Input: covariance matrix for y
00090    TMatrixD *fY;                // Input: y
00091    TMatrixD *fX0;               // Input: x0
00092    Double_t fTauSquared;        // Input: regularisation parameter
00093    Double_t fBiasScale;         // Input: scale factor for the bias
00094    TArrayI fXToHist;            // Input: matrix indices -> histogram bins
00095    TArrayI fHistToX;            // Input: histogram bins -> matrix indices
00096    TArrayD fSumOverY;           // Input: sum of all columns
00097    EConstraint fConstraint;     // Input: type of constraint to use
00098    ERegMode fRegMode;           // Input: type of regularisation
00099  private:
00100    TMatrixD *fX;                // Result: x
00101    TMatrixDSparse *fVxx;        // Result: covariance matrix on x
00102    TMatrixDSparse *fVxxInv;     // Result: inverse of covariance matrix on x
00103    TMatrixDSparse *fAx;         // Result: Ax
00104    Double_t fChi2A;             // Result: chi**2 contribution from (y-Ax)V(y-Ax)
00105    Double_t fLXsquared;         // Result: chi**2 contribution from (x-s*x0)Lsquared(x-s*x0)
00106    Double_t fRhoMax;            // Result: maximum global correlation
00107    Double_t fRhoAvg;            // Result: average global correlation
00108    Int_t fNdf;                  // Result: number of degrees of freedom
00109    TMatrixDSparse *fDXDAM[2];   // Result: part of derivative dx_k/dA_ij
00110    TMatrixDSparse *fDXDAZ[2];   // Result: part of derivative dx_k/dA_ij
00111    TMatrixDSparse *fDXDtauSquared;     // Result: derivative dx/dtau
00112    TMatrixDSparse *fDXDY;       // Result: derivative dx/dy
00113    TMatrixDSparse *fEinv;       // Result: matrix E^(-1)
00114    TMatrixDSparse *fE;          // Result: matrix E
00115  protected:
00116    TUnfold(void);              // for derived classes
00117    virtual Double_t DoUnfold(void);     // the unfolding algorithm
00118    virtual void ClearResults(void);     // clear all results
00119 
00120    TMatrixDSparse *MultiplyMSparseM(const TMatrixDSparse *a,const TMatrixD *b) const; // multiply sparse and non-sparse matrix
00121    TMatrixDSparse *MultiplyMSparseMSparse(const TMatrixDSparse *a,const TMatrixDSparse *b) const; // multiply sparse and sparse matrix
00122    TMatrixDSparse *MultiplyMSparseTranspMSparse(const TMatrixDSparse *a,const TMatrixDSparse *b) const; // multiply transposed sparse and sparse matrix
00123    TMatrixDSparse *MultiplyMSparseMSparseTranspVector
00124       (const TMatrixDSparse *m1,const TMatrixDSparse *m2,
00125        const TMatrixTBase<Double_t> *v) const; // calculate M_ij = sum_k [m1_ik*m2_jk*v[k] ]. the pointer v may be zero (means no scaling).
00126    TMatrixD *InvertMSparse(const TMatrixDSparse *A) const; // invert sparse matrix
00127    static Bool_t InvertMConditioned(TMatrixD *A); // invert matrix including preconditioning
00128    void AddMSparse(TMatrixDSparse *dest,Double_t f,const TMatrixDSparse *src); // replacement for dest += f*src
00129    TMatrixDSparse *CreateSparseMatrix(Int_t nrow,Int_t ncol,Int_t nele,Int_t *row,Int_t *col,Double_t *data) const; // create a TMatrixDSparse from an array
00130    inline Int_t GetNx(void) const {
00131       return fA->GetNcols();
00132    } // number of non-zero output bins
00133    inline Int_t GetNy(void) const {
00134       return fA->GetNrows();
00135    } // number of input bins
00136    void ErrorMatrixToHist(TH2 *ematrix,const TMatrixDSparse *emat,const Int_t *binMap,
00137                           Bool_t doClear) const; // return an error matrix as histogram
00138    inline const TMatrixDSparse *GetDXDY(void) const { return fDXDY; } // access derivative dx/dy
00139    inline const TMatrixDSparse *GetDXDAM(int i) const { return fDXDAM[i]; } // access matrix parts of the derivative dx/dA
00140    inline const TMatrixDSparse *GetDXDAZ(int i) const { return fDXDAZ[i]; } // access vector parts of the derivative dx/dA
00141    inline const TMatrixDSparse *GetDXDtauSquared(void) const { return fDXDtauSquared; } // get derivative dx/dtauSquared
00142    inline const TMatrixDSparse *GetAx(void) const { return fAx; } // get vector Ax
00143    inline const TMatrixDSparse *GetEinv(void) const { return fEinv; } // get matrix E^-1
00144    inline const TMatrixDSparse *GetE(void) const { return fE; } // get matrix E
00145    inline const TMatrixDSparse *GetVxx(void) const { return fVxx; } // get covariance matrix of x
00146    inline const TMatrixDSparse *GetVxxInv(void) const { return fVxxInv; } // get inverse of covariance matrix of x
00147    inline const TMatrixD *GetX(void) const { return fX; } // get result vector x
00148    static void DeleteMatrix(TMatrixD **m); // delete and invalidate pointer
00149    static void DeleteMatrix(TMatrixDSparse **m); // delete and invalidate pointer
00150 public:
00151    enum EHistMap {              // mapping between unfolding matrix and TH2 axes
00152       kHistMapOutputHoriz = 0,  // map unfolding output to x-axis of TH2 matrix
00153       kHistMapOutputVert = 1    // map unfolding output to y-axis of TH2 matrix
00154    };
00155 
00156    TUnfold(const TH2 *hist_A, EHistMap histmap,
00157            ERegMode regmode = kRegModeSize,
00158            EConstraint constraint=kEConstraintArea);      // constructor
00159    virtual ~ TUnfold(void);    // delete data members
00160    static const char*GetTUnfoldVersion(void);
00161    void SetBias(const TH1 *bias);       // set alternative bias
00162    void SetConstraint(EConstraint constraint); // set type of constraint for the next unfolding
00163    Int_t RegularizeSize(int bin, Double_t scale = 1.0);   // regularise the size of one output bin
00164    Int_t RegularizeDerivative(int left_bin, int right_bin, Double_t scale = 1.0); // regularize difference of two output bins (1st derivative)
00165    Int_t RegularizeCurvature(int left_bin, int center_bin, int right_bin, Double_t scale_left = 1.0, Double_t scale_right = 1.0);  // regularize curvature of three output bins (2nd derivative)
00166    Int_t RegularizeBins(int start, int step, int nbin, ERegMode regmode);        // regularize a 1-dimensional curve
00167    Int_t RegularizeBins2D(int start_bin, int step1, int nbin1, int step2, int nbin2, ERegMode regmode);  // regularize a 2-dimensional grid
00168    Double_t DoUnfold(Double_t tau,
00169                      const TH1 *hist_y, Double_t scaleBias=0.0);  // do the unfolding
00170    virtual Int_t SetInput(const TH1 *hist_y, Double_t scaleBias=0.0,Double_t oneOverZeroError=0.0); // define input distribution for ScanLCurve
00171    virtual Double_t DoUnfold(Double_t tau); // Unfold with given choice of tau
00172    virtual Int_t ScanLcurve(Int_t nPoint,Double_t tauMin,
00173                             Double_t tauMax,TGraph **lCurve,
00174                             TSpline **logTauX=0,TSpline **logTauY=0); // scan the L curve using successive calls to DoUnfold(Double_t)
00175    TH1D *GetOutput(const char *name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;    // get unfolding result
00176    TH1D *GetBias(const char *name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;      // get bias
00177    TH1D *GetFoldedOutput(const char *name,const char *title, Double_t y0 = 0.0, Double_t y1 = 0.0) const; // get folded unfolding result
00178    TH1D *GetInput(const char *name,const char *title, Double_t y0 = 0.0, Double_t y1 = 0.0) const;     // get unfolding input
00179    TH2D *GetRhoIJ(const char *name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;     // get correlation coefficients
00180    TH2D *GetEmatrix(const char*name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;   // get error matrix
00181    TH1D *GetRhoI(const char*name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;      // get global correlation coefficients
00182    TH2D *GetLsquared(const char*name,const char *title, Double_t x0 = 0.0, Double_t x1 = 0.0) const;  // get regularisation conditions squared
00183 
00184    void GetOutput(TH1 *output,const Int_t *binMap=0) const; // get output distribution, averaged over bins
00185    void GetEmatrix(TH2 *ematrix,const Int_t *binMap=0) const; // get error matrix, averaged over bins
00186    Double_t GetRhoI(TH1 *rhoi,TH2 *ematrixinv=0,const Int_t *binMap=0) const; // get global correlation coefficients and inverse of error matrix, averaged over bins
00187    void GetRhoIJ(TH2 *rhoij,const Int_t *binMap=0) const; // get correlation coefficients, averaged over bins
00188    Double_t GetTau(void) const;  // regularisation parameter
00189    inline Double_t GetRhoMax(void) const { return fRhoMax; } // maximum global correlation
00190    inline Double_t GetRhoAvg(void) const { return fRhoAvg; }  // average global correlation
00191    inline Double_t GetChi2A(void) const { return fChi2A; }  // chi**2 contribution from A
00192    Double_t GetChi2L(void) const;        // chi**2 contribution from L
00193    virtual Double_t GetLcurveX(void) const;        // x axis of L curve
00194    virtual Double_t GetLcurveY(void) const;        // y axis of L curve
00195    inline Int_t GetNdf(void) const { return fNdf; }   // number of degrees of freedom
00196    Int_t GetNpar(void) const;  // number of parameters
00197 
00198    ClassDef(TUnfold, 0) //Unfolding with support for L-curve analysis
00199 };
00200 
00201 #endif

Generated on Tue Jul 5 14:22:59 2011 for ROOT_528-00b_version by  doxygen 1.5.1