00001
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef __BCINTEGRATE__H
00023 #define __BCINTEGRATE__H
00024
00025
00026
00027 #include <iostream>
00028 #include <vector>
00029
00030 #include <math.h>
00031
00032 #include <TROOT.h>
00033 #include <TRandom3.h>
00034 #include <TH1D.h>
00035 #include <TH2D.h>
00036 #include <TMinuit.h>
00037 #include <TTree.h>
00038
00039 #include "BCMath.h"
00040 #include "BCParameter.h"
00041 #include "BCDataPoint.h"
00042 #include "BCEngineMCMC.h"
00043
00044 #define DEBUG 0
00045
00046
00047
00048 class BCIntegrate : public BCEngineMCMC
00049 {
00050
00051 public:
00052
00054
00055
00059 enum BCIntegrationMethod { kIntMonteCarlo, kIntImportance, kIntMetropolis, kIntCuba };
00060
00064 enum BCMarginalizationMethod { kMargMonteCarlo, kMargMetropolis };
00065
00069 enum BCOptimizationMethod { kOptSimulatedAnnealing, kOptMetropolis, kOptMinuit };
00070
00071
00072
00074
00075
00079 BCIntegrate();
00080
00084 BCIntegrate(BCParameterSet * par);
00085
00089 virtual ~BCIntegrate();
00090
00091
00092
00094
00095
00099 BCIntegrate::BCIntegrationMethod GetIntegrationMethod()
00100 { return fIntegrationMethod; };
00101
00105 BCIntegrate::BCMarginalizationMethod GetMarginalizationMethod()
00106 { return fMarginalizationMethod; };
00107
00111 BCIntegrate::BCOptimizationMethod GetOptimizationMethod()
00112 { return fOptimizationMethod; };
00113
00118 void GetRandomVector(std::vector <double> &x);
00119
00120 virtual void GetRandomVectorMetro(std::vector <double> &x);
00121
00128 double GetRandomPoint(std::vector <double> &x);
00129
00136 double GetRandomPointImportance(std::vector <double> &x);
00137
00143 void GetRandomPointMetro(std::vector <double> &x);
00144
00150 void GetRandomPointSamplingMetro(std::vector <double> &x);
00151
00155 int GetNiterationsPerDimension()
00156 { return fNiterPerDimension; };
00157
00161 int GetNSamplesPer2DBin()
00162 { return fNSamplesPer2DBin; };
00163
00167 int GetNvar()
00168 { return fNvar; };
00169
00173 int GetNIterationsMax()
00174 { return fNIterationsMax; };
00175
00179 int GetNIterations()
00180 { return fNIterations; };
00181
00185 double GetRelativePrecision()
00186 { return fRelativePrecision; };
00187
00191 double GetError()
00192 { return fError; };
00193
00197 int GetNbins()
00198 { return fNbins; };
00199
00203 TMinuit * GetMinuit();
00204
00205 int GetMinuitErrorFlag()
00206 { return fMinuitErrorFlag; };
00207
00211 TTree * GetMarkovChainTree()
00212 { return fMarkovChainTree; };
00213
00222 void GetRandomPointSA(std::vector <double> &x, double T, double step);
00223
00227 std::vector<double> * GetMarkovChainPoint()
00228 { return &fXmetro1; };
00229
00233 int * GetMCMCIteration()
00234 { return &fMCMCIteration; };
00235
00239 double * GetMarkovChainValue()
00240 { return &fMarkovChainValue; };
00241
00242
00243
00245
00246
00247 void SetMinuitArlist(double * arglist)
00248 { fMinuitArglist[0] = arglist[0];
00249 fMinuitArglist[1] = arglist[1]; };
00250
00255 void SetParameters(BCParameterSet * par);
00256
00260 void SetVarList(int * varlist);
00261
00265 void SetVar(int index){fVarlist[index]=1;};
00266
00270 void SetIntegrationMethod(BCIntegrate::BCIntegrationMethod method)
00271 { fIntegrationMethod = method; };
00272
00276 void SetMarginalizationMethod(BCIntegrate::BCMarginalizationMethod method)
00277 { fMarginalizationMethod = method; };
00278
00282 void SetOptimizationMethod(BCIntegrate::BCOptimizationMethod method)
00283 { fOptimizationMethod = method; };
00284
00288 void SetNiterationsPerDimension(int niterations)
00289 { fNiterPerDimension = niterations; };
00290
00295 void SetNSamplesPer2DBin(int n)
00296 { fNSamplesPer2DBin = n; };
00297
00301 void SetNIterationsMax(int niterations)
00302 { fNIterationsMax = niterations; };
00303
00308 void SetRelativePrecision(double relprecision)
00309 { fRelativePrecision = relprecision; };
00310
00315 void SetNbins(int n);
00316 void SetNbinsX(int n);
00317 void SetNbinsY(int n);
00318
00323 void SetFitFunctionIndexX(int index)
00324 { fFitFunctionIndexX = index; };
00325
00330 void SetFitFunctionIndexY(int index)
00331 { fFitFunctionIndexY = index; };
00332
00333 void SetFitFunctionIndices(int indexx, int indexy)
00334 { this -> SetFitFunctionIndexX(indexx);
00335 this -> SetFitFunctionIndexY(indexy); };
00336
00341 void SetDataPointLowerBoundaries(BCDataPoint* datasetlowerboundaries)
00342 { fDataPointLowerBoundaries = datasetlowerboundaries; };
00343
00348 void SetDataPointUpperBoundaries(BCDataPoint* datasetupperboundaries)
00349 { fDataPointUpperBoundaries = datasetupperboundaries; };
00350
00355 void SetDataPointLowerBoundary(int index, double lowerboundary)
00356 { fDataPointLowerBoundaries -> SetValue(index, lowerboundary); };
00357
00362 void SetDataPointUpperBoundary(int index, double upperboundary)
00363 { fDataPointUpperBoundaries -> SetValue(index, upperboundary); };
00364
00368 void WriteMarkovChain(bool flag)
00369 { fFlagWriteMarkovChain = flag; fMCMCFlagWriteChainToFile = flag;};
00370
00374 void SetMarkovChainTree(TTree * tree)
00375 { fMarkovChainTree = tree; };
00376
00380 void SetMarkovChainInitialPosition(std::vector<double> position);
00381
00385 void SetMarkovChainStepSize(double stepsize)
00386 { fMarkovChainStepSize = stepsize; };
00387
00391 void SetMarkovChainNIterations(int niterations)
00392 { fMarkovChainNIterations = niterations;
00393 fMarkovChainAutoN = false; }
00394
00398 void SetMarkovChainAutoN(bool flag)
00399 { fMarkovChainAutoN = flag; };
00400
00404 void SetMode(std::vector <double> mode);
00405
00409 void SetErrorBandHisto(TH2D * h) { fErrorBandXY = h; };
00410
00411
00412
00414
00415
00419 void DeleteVarList();
00420
00425 void ResetVarlist(int v);
00426
00431 void UnsetVar(int index)
00432 { fVarlist[index] = 0; };
00433
00440 virtual double Eval(std::vector <double> x);
00441
00448 virtual double LogEval(std::vector <double> x);
00449
00456 virtual double EvalSampling(std::vector <double> x);
00457
00464 double LogEvalSampling(std::vector <double> x);
00465
00473 double EvalPrint(std::vector <double> x);
00474
00481 virtual double FitFunction(std::vector <double> x, std::vector <double> parameters)
00482 { return 0.0; };
00483
00488 double Integrate();
00489
00496 double IntegralMC(std::vector <double> x, int * varlist);
00497
00498 double IntegralMC(std::vector <double> x);
00499
00505 double IntegralMetro(std::vector <double> x);
00506
00512 double IntegralImportance(std::vector <double> x);
00513
00521 double CubaIntegrate(int method, std::vector<double> parameters_double, std::vector<double> parameters_int);
00522
00523 double CubaIntegrate();
00524
00533 static void CubaIntegrand(const int * ndim, const double xx[],
00534 const int * ncomp, double ff[]);
00535
00541 TH1D * Marginalize(BCParameter * parameter);
00542
00549 TH2D * Marginalize(BCParameter * parameter1, BCParameter * parameter2);
00550
00557 TH1D * MarginalizeByIntegrate(BCParameter * parameter);
00558
00566 TH2D * MarginalizeByIntegrate(BCParameter * parameter1, BCParameter * parameter2);
00567
00574 TH1D * MarginalizeByMetro(BCParameter * parameter);
00575
00582 TH2D * MarginalizeByMetro(BCParameter * parameter1, BCParameter * parameter2);
00583
00590 int MarginalizeAllByMetro(const char * name);
00591
00596 TH1D * GetH1D(int parIndex);
00597
00604 int GetH2DIndex(int parIndex1, int parIndex2);
00605
00611 TH2D * GetH2D(int parIndex1, int parIndex2);
00612
00616 void InitMetro();
00617
00621
00622
00626 void FindModeSA();
00627
00631 void FindModeMinuit();
00632
00636 void FindModeMCMC();
00637
00638
00639 static void FCNLikelihood(int &npar, double * grad, double &fval, double * par, int flag);
00640
00641
00642
00643 private:
00644
00648 BCParameterSet * fx;
00649
00653 double * fMin;
00654
00658 double * fMax;
00659
00663 int * fVarlist;
00664
00668 int fNiterPerDimension;
00669
00673 BCIntegrate::BCIntegrationMethod fIntegrationMethod;
00674
00678 BCIntegrate::BCMarginalizationMethod fMarginalizationMethod;
00679
00683 BCIntegrate::BCOptimizationMethod fOptimizationMethod;
00684
00688 int fNIterationsMax;
00689
00693 int fNIterations;
00694
00698 double fRelativePrecision;
00699
00703 double fError;
00704
00708 int fNmetro;
00709 int fNacceptedMCMC;
00710
00714 std::vector <double> fXmetro0;
00715
00719 std::vector <double> fXmetro1;
00720
00724 double fMarkovChainValue;
00725
00726
00727
00728
00729 void MCMCUpdateFunctionFitting();
00730
00731 protected:
00732
00736 int fNvar;
00737
00741 int fNbins;
00742
00747 int fNSamplesPer2DBin;
00748
00752 double fMarkovChainStepSize;
00753
00754 int fMarkovChainNIterations;
00755
00756 bool fMarkovChainAutoN;
00757
00761 BCDataPoint * fDataPointLowerBoundaries;
00762
00766 BCDataPoint * fDataPointUpperBoundaries;
00767
00768 std::vector <bool> fDataFixedValues;
00769
00773 TRandom3 * fRandom;
00774
00778 std::vector <double> fBestFitParameters;
00779
00783 std::vector <double> fBestFitParametersMarginalized;
00784
00788 std::vector <TH1D *> fHProb1D;
00789
00793 std::vector <TH2D *> fHProb2D;
00794
00798 int fFitFunctionIndexX;
00799 int fFitFunctionIndexY;
00800
00804 bool fErrorBandContinuous;
00805 std::vector <double> fErrorBandX;
00806
00810 TH2D * fErrorBandXY;
00811 int fErrorBandNbinsX;
00812 int fErrorBandNbinsY;
00813
00817 TMinuit * fMinuit;
00818
00819 double fMinuitArglist[2];
00820 int fMinuitErrorFlag;
00821
00825 bool fFlagWriteMarkovChain;
00826
00830 TTree * fMarkovChainTree;
00831
00835 int fMCMCIteration;
00836
00837 };
00838
00839
00840
00841 #endif