File indexing completed on 2025-12-16 10:29:45
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #ifndef RooStats_MCMCInterval
0013 #define RooStats_MCMCInterval
0014
0015 #include "Rtypes.h"
0016
0017 #include "RooStats/ConfInterval.h"
0018 #include "RooArgSet.h"
0019 #include "RooArgList.h"
0020 #include "RooMsgService.h"
0021 #include "RooStats/MarkovChain.h"
0022
0023 #include <vector>
0024
0025 class RooNDKeysPdf;
0026 class RooProduct;
0027
0028
0029 namespace RooStats {
0030
0031 class Heaviside;
0032
0033 class MCMCInterval : public ConfInterval {
0034
0035
0036 public:
0037
0038
0039 explicit MCMCInterval(const char *name = nullptr);
0040
0041
0042 MCMCInterval(const char* name, const RooArgSet& parameters,
0043 MarkovChain& chain);
0044
0045 enum {DEFAULT_NUM_BINS = 50};
0046 enum IntervalType {kShortest, kTailFraction};
0047
0048 ~MCMCInterval() override;
0049
0050
0051 bool IsInInterval(const RooArgSet& point) const override;
0052
0053
0054
0055
0056
0057
0058
0059 void SetConfidenceLevel(double cl) override;
0060
0061
0062 double ConfidenceLevel() const override {return fConfidenceLevel;}
0063
0064
0065
0066 RooArgSet* GetParameters() const override;
0067
0068
0069
0070 virtual double GetHistCutoff();
0071
0072
0073
0074 virtual double GetKeysPdfCutoff();
0075
0076
0077
0078 virtual double GetActualConfidenceLevel();
0079
0080
0081
0082 virtual void SetHistStrict(bool isHistStrict)
0083 { fIsHistStrict = isHistStrict; }
0084
0085
0086 bool CheckParameters(const RooArgSet& point) const override;
0087
0088
0089
0090 virtual void SetParameters(const RooArgSet& parameters);
0091
0092
0093
0094 virtual void SetChain(MarkovChain& chain) { fChain.reset(&chain); }
0095
0096
0097
0098
0099 virtual void SetAxes(RooArgList& axes);
0100
0101
0102
0103 virtual RooArgList* GetAxes()
0104 {
0105 RooArgList* axes = new RooArgList();
0106 for (Int_t i = 0; i < fDimension; i++)
0107 axes->addClone(*fAxes[i]);
0108 return axes;
0109 }
0110
0111
0112 virtual double LowerLimit(RooRealVar& param);
0113
0114
0115 virtual double LowerLimitTailFraction(RooRealVar& param);
0116
0117
0118
0119
0120 virtual double LowerLimitShortest(RooRealVar& param);
0121
0122
0123 virtual double LowerLimitByKeys(RooRealVar& param);
0124
0125
0126 virtual double LowerLimitByHist(RooRealVar& param);
0127
0128
0129 virtual double LowerLimitBySparseHist(RooRealVar& param);
0130
0131
0132 virtual double LowerLimitByDataHist(RooRealVar& param);
0133
0134
0135 virtual double UpperLimit(RooRealVar& param);
0136
0137
0138 virtual double UpperLimitTailFraction(RooRealVar& param);
0139
0140
0141
0142
0143 virtual double UpperLimitShortest(RooRealVar& param);
0144
0145
0146 virtual double UpperLimitByKeys(RooRealVar& param);
0147
0148
0149 virtual double UpperLimitByHist(RooRealVar& param);
0150
0151
0152 virtual double UpperLimitBySparseHist(RooRealVar& param);
0153
0154
0155 virtual double UpperLimitByDataHist(RooRealVar& param);
0156
0157
0158 double GetKeysMax();
0159
0160
0161
0162 virtual void SetNumBurnInSteps(Int_t numBurnInSteps)
0163 { fNumBurnInSteps = numBurnInSteps; }
0164
0165
0166 virtual void SetUseKeys(bool useKeys) { fUseKeys = useKeys; }
0167
0168
0169
0170 virtual void SetUseSparseHist(bool useSparseHist)
0171 { fUseSparseHist = useSparseHist; }
0172
0173
0174 virtual bool GetUseKeys() { return fUseKeys; }
0175
0176
0177
0178
0179
0180 virtual Int_t GetNumBurnInSteps() { return fNumBurnInSteps; }
0181
0182
0183
0184
0185
0186 virtual TH1* GetPosteriorHist();
0187
0188
0189 virtual RooNDKeysPdf* GetPosteriorKeysPdf();
0190
0191
0192 virtual RooProduct* GetPosteriorKeysProduct();
0193
0194
0195 virtual Int_t GetDimension() const { return fDimension; }
0196
0197
0198
0199 virtual const MarkovChain* GetChain() { return fChain.get(); }
0200
0201
0202
0203 virtual RooFit::OwningPtr<RooDataSet> GetChainAsDataSet(RooArgSet* whichVars = nullptr)
0204 { return fChain->GetAsDataSet(whichVars); }
0205
0206
0207
0208 virtual const RooDataSet* GetChainAsConstDataSet()
0209 { return fChain->GetAsConstDataSet(); }
0210
0211
0212
0213 virtual RooFit::OwningPtr<RooDataHist> GetChainAsDataHist(RooArgSet* whichVars = nullptr)
0214 { return fChain->GetAsDataHist(whichVars); }
0215
0216
0217
0218 virtual THnSparse* GetChainAsSparseHist(RooArgSet* whichVars = nullptr)
0219 { return fChain->GetAsSparseHist(whichVars); }
0220
0221
0222 virtual RooRealVar* GetNLLVar() const
0223 { return fChain->GetNLLVar(); }
0224
0225
0226 virtual RooRealVar* GetWeightVar() const
0227 { return fChain->GetWeightVar(); }
0228
0229
0230 virtual void SetEpsilon(double epsilon)
0231 {
0232 if (epsilon < 0) {
0233 coutE(InputArguments) << "MCMCInterval::SetEpsilon will not allow "
0234 << "negative epsilon value" << std::endl;
0235 } else {
0236 fEpsilon = epsilon;
0237 }
0238 }
0239
0240
0241
0242
0243
0244 virtual void SetIntervalType(enum IntervalType intervalType)
0245 { fIntervalType = intervalType; }
0246 virtual void SetShortestInterval() { SetIntervalType(kShortest); }
0247
0248
0249 virtual enum IntervalType GetIntervalType() { return fIntervalType; }
0250
0251
0252 virtual void SetLeftSideTailFraction(double a) {
0253 fIntervalType = kTailFraction;
0254 fLeftSideTF = a;
0255 }
0256
0257
0258
0259
0260
0261
0262
0263
0264
0265 virtual void SetDelta(double delta)
0266 {
0267 if (delta < 0.) {
0268 coutE(InputArguments) << "MCMCInterval::SetDelta will not allow "
0269 << "negative delta value" << std::endl;
0270 } else {
0271 fDelta = delta;
0272 }
0273 }
0274
0275 private:
0276 inline bool AcceptableConfLevel(double confLevel);
0277 inline bool WithinDeltaFraction(double a, double b);
0278
0279 constexpr static const double DEFAULT_EPSILON = 0.01;
0280 constexpr static const double DEFAULT_DELTA = 10e-6;
0281
0282 protected:
0283 RooArgSet fParameters;
0284 std::unique_ptr<MarkovChain> fChain;
0285 double fConfidenceLevel = 0.0;
0286
0287 std::unique_ptr<RooDataHist> fDataHist;
0288 std::unique_ptr<THnSparse> fSparseHist;
0289 double fHistConfLevel = 0.0;
0290 double fHistCutoff = -1;
0291
0292 std::unique_ptr<RooNDKeysPdf> fKeysPdf;
0293 std::unique_ptr<RooProduct> fProduct;
0294 std::unique_ptr<Heaviside> fHeaviside;
0295 std::unique_ptr<RooDataHist> fKeysDataHist;
0296 std::unique_ptr<RooRealVar> fCutoffVar;
0297 double fKeysConfLevel = 0.0;
0298 double fKeysCutoff = -1;
0299 double fFull = 0.0;
0300
0301 double fLeftSideTF = -1;
0302 double fTFConfLevel = 0.0;
0303 std::vector<Int_t> fVector;
0304 double fVecWeight = 0;
0305 double fTFLower;
0306 double fTFUpper;
0307
0308 std::unique_ptr<TH1> fHist;
0309
0310 bool fUseKeys = false;
0311 bool fUseSparseHist = false;
0312 bool fIsHistStrict = true;
0313
0314
0315
0316 Int_t fDimension = 1;
0317 Int_t fNumBurnInSteps = 0;
0318
0319 std::vector<RooRealVar*> fAxes;
0320
0321
0322
0323 double fEpsilon = DEFAULT_EPSILON;
0324
0325 double fDelta = DEFAULT_DELTA;
0326
0327
0328
0329
0330 enum IntervalType fIntervalType = kShortest;
0331
0332
0333 virtual void DetermineInterval();
0334 virtual void DetermineShortestInterval();
0335 virtual void DetermineTailFractionInterval();
0336 virtual void DetermineByHist();
0337 virtual void DetermineBySparseHist();
0338 virtual void DetermineByDataHist();
0339 virtual void DetermineByKeys();
0340 virtual void CreateHist();
0341 virtual void CreateSparseHist();
0342 virtual void CreateDataHist();
0343 virtual void CreateKeysPdf();
0344 virtual void CreateKeysDataHist();
0345 virtual void CreateVector(RooRealVar* param);
0346 inline virtual double CalcConfLevel(double cutoff, double full);
0347
0348 ClassDefOverride(MCMCInterval,2)
0349
0350 };
0351 }
0352
0353 #endif