Warning, file /include/root/RooDataHist.h was not indexed
or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #ifndef ROO_DATA_HIST
0017 #define ROO_DATA_HIST
0018
0019 #include <RooAbsData.h>
0020 #include <RooArgSet.h>
0021 #include <RooDirItem.h>
0022 #include <RooGlobalFunc.h>
0023
0024 #include <Rtypes.h>
0025
0026 #include <functional>
0027 #include <map>
0028 #include <memory>
0029 #include <string>
0030 #include <string_view>
0031 #include <unordered_map>
0032 #include <vector>
0033
0034 class TAxis ;
0035 class RooAbsArg;
0036 class RooCategory ;
0037 class RooPlot;
0038 class RooAbsLValue ;
0039
0040 class RooDataHist : public RooAbsData, public RooDirItem {
0041 public:
0042
0043
0044 RooDataHist() ;
0045 RooDataHist(RooStringView name, RooStringView title, const RooArgSet& vars, const char* binningName=nullptr) ;
0046 RooDataHist(RooStringView name, RooStringView title, const RooArgSet& vars, const RooAbsData& data, double initWgt=1.0) ;
0047 RooDataHist(RooStringView name, RooStringView title, const RooArgList& vars, const TH1* hist, double initWgt=1.0) ;
0048 RooDataHist(RooStringView name, RooStringView title, const RooArgList& vars, RooCategory& indexCat, std::map<std::string,TH1*> histMap, double initWgt=1.0) ;
0049 RooDataHist(RooStringView name, RooStringView title, const RooArgList& vars, RooCategory& indexCat, std::map<std::string,RooDataHist*> dhistMap, double wgt=1.0) ;
0050 RooDataHist(RooStringView name, RooStringView title, const RooArgList& vars, const RooCmdArg& arg1, const RooCmdArg& arg2={}, const RooCmdArg& arg3={},
0051 const RooCmdArg& arg4={},const RooCmdArg& arg5={},const RooCmdArg& arg6={},const RooCmdArg& arg7={},const RooCmdArg& arg8={}) ;
0052
0053
0054 template<class Val_t>
0055 inline RooDataHist(RooStringView name, RooStringView title, const RooArgList& vars, RooCategory& indexCat, RooFit::Detail::FlatMap<std::string,Val_t> const& histMap, double initWgt=1.0)
0056 : RooDataHist(name, title, vars, indexCat, RooFit::Detail::flatMapToStdMap(histMap), initWgt) {}
0057
0058 RooDataHist& operator=(const RooDataHist&) = delete;
0059
0060 RooDataHist(const RooDataHist& other, const char* newname = nullptr) ;
0061 TObject* Clone(const char* newname="") const override {
0062 return new RooDataHist(*this, newname && newname[0] != '\0' ? newname : GetName());
0063 }
0064 ~RooDataHist() override ;
0065
0066
0067 RooFit::OwningPtr<RooAbsData> emptyClone(const char* newName=nullptr, const char* newTitle=nullptr, const RooArgSet*vars=nullptr, const char* =nullptr) const override {
0068 return RooFit::makeOwningPtr(std::make_unique<RooDataHist>(newName?newName:GetName(),newTitle?newTitle:GetTitle(),vars?*vars:*get()));
0069 }
0070
0071
0072 void add(const RooArgSet& row, double wgt=1.0) override { add(row,wgt,-1.); }
0073 void add(const RooArgSet& row, double weight, double sumw2);
0074 void set(std::size_t binNumber, double weight, double wgtErr);
0075 void set(const RooArgSet& row, double weight, double wgtErr=-1.) ;
0076 void set(const RooArgSet& row, double weight, double wgtErrLo, double wgtErrHi) ;
0077
0078 void add(const RooAbsData& dset, const RooFormulaVar* cutVar=nullptr, double weight=1.0 ) ;
0079 void add(const RooAbsData& dset, const char* cut, double weight=1.0 ) ;
0080
0081
0082 const RooArgSet* get() const override { return &_vars; }
0083 const RooArgSet* get(Int_t binNumber) const override;
0084 virtual const RooArgSet* get(const RooArgSet& coord) const;
0085 double sumEntries() const override;
0086 double sumEntries(const char* cutSpec, const char* cutRange=nullptr) const override;
0087
0088
0089 bool isWeighted() const override { return true; }
0090 bool isNonPoissonWeighted() const override ;
0091
0092 std::span<const double> getWeightBatch(std::size_t first, std::size_t len, bool sumW2=false) const override;
0093
0094
0095 std::span<const double> binVolumes(std::size_t first, std::size_t len) const {
0096 return {_binv + first, len};
0097 }
0098
0099 double sum(bool correctForBinSize, bool inverseCorr=false) const ;
0100 double sum(const RooArgSet& sumSet, const RooArgSet& sliceSet, bool correctForBinSize, bool inverseCorr=false) ;
0101 double sum(const RooArgSet& sumSet,
0102 const RooArgSet& sliceSet,
0103 bool correctForBinSize,
0104 bool inverseCorr,
0105 const std::map<const RooAbsArg*, std::pair<double, double> >& ranges,
0106 std::function<double(int)> getBinScale = [](int){ return 1.0; } );
0107
0108 void weights(double* output, std::span<double const> xVals, int intOrder, bool correctForBinSize, bool cdfBoundaries);
0109
0110 double weight(std::size_t i) const { return _wgt[i]; }
0111 double weightFast(const RooArgSet& bin, int intOrder, bool correctForBinSize, bool cdfBoundaries);
0112 double weight(const RooArgSet& bin, Int_t intOrder=1, bool correctForBinSize=false, bool cdfBoundaries=false, bool oneSafe=false);
0113
0114
0115
0116 double weightSquared(std::size_t i) const { return _sumw2 ? _sumw2[i] : _wgt[i]; }
0117
0118 double binVolume(std::size_t i) const { return _binv[i]; }
0119 double binVolume(const RooArgSet& bin) const;
0120
0121 TIterator* sliceIterator(RooAbsArg& sliceArg, const RooArgSet& otherArgs) ;
0122
0123 void weightError(double& lo, double& hi, ErrorType etype=Poisson) const override;
0124
0125 double weightError(ErrorType etype=Poisson) const override {
0126
0127 double lo;
0128 double hi;
0129 weightError(lo,hi,etype) ;
0130 return (lo+hi)/2 ;
0131 }
0132
0133 using RooAbsData::plotOn ;
0134 RooPlot *plotOn(RooPlot *frame, PlotOpt o) const override;
0135
0136 void reset() override;
0137
0138 void printMultiline(std::ostream& os, Int_t content, bool verbose=false, TString indent="") const override;
0139 void printArgs(std::ostream& os) const override;
0140 void printValue(std::ostream& os) const override;
0141 void printDataHistogram(std::ostream& os, RooRealVar* obs) const;
0142
0143 void SetName(const char *name) override;
0144 void SetNameTitle(const char *name, const char* title) override;
0145
0146 Int_t getIndex(const RooAbsCollection& coord, bool fast = false) const;
0147
0148
0149
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159
0160 Int_t getIndex(const RooArgSet& coord, bool fast = false) const {
0161 return getIndex(static_cast<const RooAbsCollection&>(coord), fast);
0162 }
0163
0164 void removeSelfFromDir() { removeFromDir(this) ; }
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184
0185 double weight() const override
0186 R__SUGGEST_ALTERNATIVE("Use the safer weight(std::size_t) const.")
0187 { return _wgt[_curIndex]; }
0188
0189
0190 double weightSquared() const override
0191 R__SUGGEST_ALTERNATIVE("Use the safer weightSquared(std::size_t) const.")
0192 { return weightSquared(_curIndex); }
0193
0194 double binVolume() const
0195 R__SUGGEST_ALTERNATIVE("Use binVolume(std::size_t) const.")
0196 { return _binv[_curIndex]; }
0197
0198 void set(double wgt, double wgtErr=-1)
0199 R__SUGGEST_ALTERNATIVE("Use set(std::size_t,double,double).");
0200
0201
0202
0203
0204
0205
0206 struct VarInfo {
0207 size_t nRealVars = 0;
0208 size_t realVarIdx1 = 0;
0209 size_t realVarIdx2 = 0;
0210 bool initialized = false;
0211 };
0212
0213 std::vector<std::unique_ptr<const RooAbsBinning>> const& getBinnings() const { return _lvbins; }
0214
0215 int arraySize() const { return _arrSize; }
0216 double const* weightArray() const { return _wgt; }
0217 double const* wgtErrLoArray() const { return _errLo; }
0218 double const* wgtErrHiArray() const { return _errHi; }
0219 double const* sumW2Array() const { return _sumw2; }
0220
0221 std::string calculateTreeIndexForCodeSquash(RooAbsArg const *klass, RooFit::Detail::CodeSquashContext &ctx,
0222 const RooAbsCollection &coords, bool reverse = false) const;
0223 std::string declWeightArrayForCodeSquash(RooFit::Detail::CodeSquashContext &ctx,
0224 bool correctForBinSize) const;
0225
0226 protected:
0227 friend class RooDataHistSliceIter ;
0228
0229 std::size_t calcTreeIndex(const RooAbsCollection& coords, bool fast) const;
0230
0231
0232 Int_t calcTreeIndex() const { return static_cast<Int_t>(calcTreeIndex(_vars, true)); }
0233
0234 void initialize(const char* binningName=nullptr,bool fillTree=true) ;
0235 std::unique_ptr<RooAbsData> reduceEng(const RooArgSet& varSubset, const RooFormulaVar* cutVar, const char* cutRange=nullptr,
0236 std::size_t nStart=0, std::size_t nStop=std::numeric_limits<std::size_t>::max()) override;
0237 double interpolateDim(int iDim, double xval, size_t centralIdx, int intOrder, bool correctForBinSize, bool cdfBoundaries) ;
0238 const std::vector<double>& calculatePartialBinVolume(const RooArgSet& dimSet) const ;
0239 void checkBinBounds() const;
0240
0241 void adjustBinning(const RooArgList& vars, const TH1& href, Int_t* offset=nullptr) ;
0242 void importTH1(const RooArgList& vars, const TH1& histo, double initWgt, bool doDensityCorrection) ;
0243 void importTH1Set(const RooArgList& vars, RooCategory& indexCat, std::map<std::string,TH1*> hmap, double initWgt, bool doDensityCorrection) ;
0244 void importDHistSet(const RooArgList& vars, RooCategory& indexCat, std::map<std::string,RooDataHist*> dmap, double initWgt) ;
0245
0246 Int_t _arrSize{0};
0247 std::vector<Int_t> _idxMult ;
0248
0249 double* _wgt {nullptr};
0250 mutable double* _errLo{nullptr};
0251 mutable double* _errHi{nullptr};
0252 mutable double* _sumw2{nullptr};
0253 double* _binv {nullptr};
0254
0255 mutable ULong64_t _curIndex{std::numeric_limits<ULong64_t>::max()};
0256
0257 mutable std::unordered_map<int,std::vector<double>> _pbinvCache ;
0258 std::vector<RooAbsLValue*> _lvvars ;
0259 std::vector<std::unique_ptr<const RooAbsBinning>> _lvbins ;
0260 mutable std::vector<std::vector<double> > _binbounds;
0261
0262 enum CacheSumState_t{kInvalid = 0, kNoBinCorrection = 1, kCorrectForBinSize = 2, kInverseBinCorr = 3};
0263 mutable Int_t _cache_sum_valid{kInvalid};
0264 mutable double _cache_sum{0.};
0265
0266 private:
0267 void interpolateQuadratic(double* output, std::span<const double> xVals, bool correctForBinSize, bool cdfBoundaries);
0268 void interpolateLinear(double* output, std::span<const double> xVals, bool correctForBinSize, bool cdfBoundaries);
0269 double weightInterpolated(const RooArgSet& bin, int intOrder, bool correctForBinSize, bool cdfBoundaries);
0270
0271 void _adjustBinning(RooRealVar &theirVar, const TAxis &axis, RooRealVar *ourVar, Int_t *offset);
0272 void registerWeightArraysToDataStore() const;
0273 void initializeAsymErrArrays() const;
0274 VarInfo const& getVarInfo();
0275
0276 static std::unique_ptr<RooAbsDataStore>
0277 makeDefaultDataStore(RooStringView name, RooStringView title, RooArgSet const &vars);
0278
0279 VarInfo _varInfo;
0280 std::vector<double> _interpolationBuffer;
0281
0282 ClassDefOverride(RooDataHist, 8)
0283 };
0284
0285 #endif