Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class ParamHistFunc : public RooAbsReal {

RooDataHist const& dataHist() const { return _dataSet; }

double binVolume() const { return _dataSet.binVolume(); }
double binVolume(std::size_t i) const { return _dataSet.binVolume(i); }

bool forceAnalyticalInt(const RooAbsArg&) const override { return true ; }

Expand Down
2 changes: 1 addition & 1 deletion roofit/histfactory/src/RooBarlowBeestonLL.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ void RooStats::HistFactory::RooBarlowBeestonLL::initializeBarlowCache() {
cache.bin_center = bin_center;
cache.observables = obsSet;

cache.binVolume = param_func->binVolume();
cache.binVolume = param_func->binVolume(bin_index);

// Delete this part, simply a comment
RooArgList obs_list( *(cache.bin_center) );
Expand Down
6 changes: 3 additions & 3 deletions roofit/roofit/src/RooHistConstraint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ RooHistConstraint::RooHistConstraint(const char *name, const char *title,

// Now populate nominal with parameters
for (int i=0 ; i<phf->_dh.numEntries() ; i++) {
phf->_dh.get(i) ;
if (phf->_dh.weight()<threshold && phf->_dh.weight() != 0.) {
const double wi = phf->_dh.weight(i) ;
if (wi<threshold && wi != 0.) {
const char* vname = Form("%s_nominal_bin_%i",GetName(),i) ;
auto var = std::make_unique<RooRealVar>(vname,vname,0,1.E30);
var->setVal(phf->_dh.weight()) ;
var->setVal(wi) ;
var->setConstant(true);

auto gamma = static_cast<RooRealVar*>(phf->_p.at(i));
Expand Down
14 changes: 8 additions & 6 deletions roofit/roofit/src/RooIntegralMorph.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ void RooIntegralMorph::MorphCacheElem::calculate(TIterator* dIter)
// Zero output histogram below lowest calculable X value
for (int i=0; i<_yatXmin ; i++) {
dIter->Next() ;
//_hist->get(i) ;
hist()->set(0) ;
const std::size_t binIdx = hist()->getIndex(*hist()->get(), /*fast=*/true);
hist()->set(binIdx, 0, -1) ;
}

double x1 = _x->getMin("cache");
Expand All @@ -420,14 +420,16 @@ void RooIntegralMorph::MorphCacheElem::calculate(TIterator* dIter)
double fbarX = f1x1*f2x2 / ( _alpha->getVal()*f2x2 + (1-_alpha->getVal())*f1x1 ) ;

dIter->Next() ;
//_hist->get(i) ;
hist()->set(fbarX) ;
{
const std::size_t binIdx = hist()->getIndex(*hist()->get(), /*fast=*/true);
hist()->set(binIdx, fbarX, -1) ;
}
}
// Zero output histogram above highest calculable X value
for (int i=_yatXmax+1 ; i<nbins ; i++) {
dIter->Next() ;
//_hist->get(i) ;
hist()->set(0) ;
const std::size_t binIdx = hist()->getIndex(*hist()->get(), /*fast=*/true);
hist()->set(binIdx, 0, -1) ;
}

pdf()->setUnitNorm(true) ;
Expand Down
11 changes: 5 additions & 6 deletions roofit/roofit/src/RooLagrangianMorphFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2572,12 +2572,12 @@ TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name, bool correlateEr
continue;
}
const RooDataHist &dhist = hf->dataHist();
dhist.get(i);
RooAbsReal *formula = dynamic_cast<RooAbsReal *>(prod->components().find(Form("w_%s", prod->GetName())));
double weight = formula->getVal();
unc2 += dhist.weightSquared() * weight * weight;
unc += sqrt(dhist.weightSquared()) * weight;
val += dhist.weight() * weight;
const double w2 = dhist.weightSquared(i);
unc2 += w2 * weight * weight;
unc += sqrt(w2) * weight;
val += dhist.weight(i) * weight;
}
hist->SetBinContent(i + 1, val);
hist->SetBinError(i + 1, correlateErrors ? unc : sqrt(unc2));
Expand Down Expand Up @@ -2827,8 +2827,7 @@ double RooLagrangianMorphFunc::expectedUncertainty() const
if (hf) {
const RooDataHist &hist = hf->dataHist();
for (Int_t j = 0; j < observable->getBins(); ++j) {
hist.get(j);
newunc2 += hist.weightSquared();
newunc2 += hist.weightSquared(j);
}
} else if (rv) {
newunc2 = pow(rv->getError(), 2);
Expand Down
9 changes: 4 additions & 5 deletions roofit/roofit/src/RooParamHistFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ RooParamHistFunc::RooParamHistFunc(const char *name, const char *title, RooDataH
// Now populate p with parameters
RooArgSet allVars;
for (Int_t i = 0; i < _dh.numEntries(); i++) {
_dh.get(i);
const double wi = _dh.weight(i);
const char *vname = Form("%s_gamma_bin_%i", GetName(), i);
RooRealVar *var = new RooRealVar(vname, vname, 0, 1000);
var->setVal(_relParam ? 1 : _dh.weight());
var->setError(_relParam ? 1 / sqrt(_dh.weight()) : sqrt(_dh.weight()));
var->setVal(_relParam ? 1 : wi);
var->setError(_relParam ? 1 / sqrt(wi) : sqrt(wi));
var->setConstant(true);
allVars.add(*var);
_p.add(*var);
Expand Down Expand Up @@ -94,8 +94,7 @@ void RooParamHistFunc::setActual(Int_t ibin, double newVal)

double RooParamHistFunc::getNominal(Int_t ibin) const
{
_dh.get(ibin) ;
return _dh.weight() ;
return _dh.weight(ibin) ;
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 4 additions & 1 deletion roofit/roofitcore/inc/RooEvaluatorWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
#include <RooRealProxy.h>
#include <RooSetProxy.h>

#include <memory>
#include <stack>

class ChangeOperModeRAII;
class RooAbsArg;
class RooAbsCategory;
class RooAbsPdf;
Expand Down Expand Up @@ -71,9 +73,10 @@ class RooEvaluatorWrapper final : public RooAbsReal {
void generateHessian();

void setUseGeneratedFunctionCode(bool);

void writeDebugMacro(std::string const &) const;

std::unique_ptr<ChangeOperModeRAII> setOperModes(RooAbsArg::OperMode opMode);

protected:
double evaluate() const override;

Expand Down
5 changes: 3 additions & 2 deletions roofit/roofitcore/inc/RooFit/Evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
#include <RConfig.h>

#include <memory>
#include <stack>

class ChangeOperModeRAII;
class RooAbsArg;
Expand All @@ -45,6 +44,8 @@ class Evaluator {

void setOffsetMode(RooFit::EvalContext::OffsetMode);

std::unique_ptr<ChangeOperModeRAII> setOperModes(RooAbsArg::OperMode opMode);

private:
void processVariable(NodeInfo &nodeInfo);
void processCategory(NodeInfo &nodeInfo);
Expand All @@ -66,7 +67,7 @@ class Evaluator {
RooFit::EvalContext _evalContextCUDA;
std::vector<NodeInfo> _nodes; // the ordered computation graph
std::unordered_map<TNamed const *, NodeInfo *> _nodesMap; // for quick lookup of nodes
std::stack<std::unique_ptr<ChangeOperModeRAII>> _changeOperModeRAIIs;
std::unique_ptr<ChangeOperModeRAII> _operModeChanges;
};

} // end namespace RooFit
Expand Down
1 change: 1 addition & 0 deletions roofit/roofitcore/inc/RooMinimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class RooMinimizer : public TObject {
void fillCorrMatrix(RooFitResult &fitRes);
void updateErrors();

RooAbsReal &_function;
ROOT::Fit::FitConfig _config; ///< fitter configuration (options and parameter settings)
std::unique_ptr<FitResult> _result; ///<! pointer to the object containing the result of the fit
std::unique_ptr<ROOT::Math::Minimizer> _minimizer; ///<! pointer to used minimizer
Expand Down
48 changes: 37 additions & 11 deletions roofit/roofitcore/res/RooFitImplHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,51 @@ class DisableCachingRAII {
bool _oldState;
};

/// Struct to temporarily change the operation mode of a RooAbsArg until it
/// goes out of scope.
/// Scope guard that temporarily changes the operation mode of one or more
/// RooAbsArg instances. Each call to change() records the arg's current
/// operMode before flipping it to the requested mode (non-recursively, i.e.
/// value clients are not touched). Destruction (or an explicit clear())
/// restores every recorded mode in LIFO order.
///
/// The class is movable but not copyable, so it can be returned from
/// functions that build up a batch of changes to hand to the caller.
class ChangeOperModeRAII {
public:
ChangeOperModeRAII(RooAbsArg *arg, RooAbsArg::OperMode opMode) : _arg{arg}, _oldOpMode(arg->operMode())
ChangeOperModeRAII() = default;

/// Convenience ctor: behaves like a scope guard for a single arg.
ChangeOperModeRAII(RooAbsArg *arg, RooAbsArg::OperMode opMode) { change(arg, opMode); }

~ChangeOperModeRAII() { clear(); }

ChangeOperModeRAII(ChangeOperModeRAII &&) = default;
ChangeOperModeRAII &operator=(ChangeOperModeRAII &&) = default;
ChangeOperModeRAII(ChangeOperModeRAII const &) = delete;
ChangeOperModeRAII &operator=(ChangeOperModeRAII const &) = delete;

/// Record arg's current operMode and flip it to opMode. If the current
/// mode already equals opMode, this is a no-op (nothing to restore).
void change(RooAbsArg *arg, RooAbsArg::OperMode opMode)
{
arg->setOperMode(opMode, /*recurse=*/false);
if (opMode == arg->operMode())
return;
_entries.emplace_back(arg, arg->operMode());
arg->setOperMode(opMode, /*recurseADirty=*/false);
}

ChangeOperModeRAII(ChangeOperModeRAII const &other) = delete;
ChangeOperModeRAII &operator=(ChangeOperModeRAII const &other) = delete;
ChangeOperModeRAII(ChangeOperModeRAII &&other) = delete;
ChangeOperModeRAII &operator=(ChangeOperModeRAII &&other) = delete;
/// Restore every recorded change right away, emptying this guard.
void clear()
{
for (auto it = _entries.rbegin(); it != _entries.rend(); ++it) {
it->first->setOperMode(it->second, /*recurseADirty=*/false);
}
_entries.clear();
}

~ChangeOperModeRAII() { _arg->setOperMode(_oldOpMode, /*recurse=*/false); }
bool empty() const { return _entries.empty(); }

private:
RooAbsArg *_arg = nullptr;
RooAbsArg::OperMode _oldOpMode;
std::vector<std::pair<RooAbsArg *, RooAbsArg::OperMode>> _entries;
};

namespace RooHelpers {
Expand Down
22 changes: 11 additions & 11 deletions roofit/roofitcore/src/RooAbsPdf.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1703,26 +1703,27 @@ RooFit::OwningPtr<RooDataHist> RooAbsPdf::generateBinned(const RooArgSet &whatVa
Int_t histOutSum(0) ;
for (int i=0 ; i<hist->numEntries() ; i++) {
hist->get(i) ;
const double wi = hist->weight(i) ;
if (expectedData) {

// Expected data, multiply p.d.f by nEvents
double w=hist->weight()*nEvents ;
double w=wi*nEvents ;
hist->set(i, w, sqrt(w));

} else if (extended) {

// Extended mode, set contents to Poisson(pdf*nEvents)
double w = RooRandom::randomGenerator()->Poisson(hist->weight()*nEvents) ;
hist->set(w,sqrt(w)) ;
double w = RooRandom::randomGenerator()->Poisson(wi*nEvents) ;
hist->set(i, w, sqrt(w)) ;

} else {

// Regular mode, fill array of weights with Poisson(pdf*nEvents), but to not fill
// histogram yet.
if (hist->weight()>histMax) {
histMax = hist->weight() ;
if (wi>histMax) {
histMax = wi ;
}
histOut[i] = RooRandom::randomGenerator()->Poisson(hist->weight()*nEvents) ;
histOut[i] = RooRandom::randomGenerator()->Poisson(wi*nEvents) ;
histOutSum += histOut[i] ;
}
}
Expand All @@ -1745,7 +1746,7 @@ RooFit::OwningPtr<RooDataHist> RooAbsPdf::generateBinned(const RooArgSet &whatVa
hist->get(ibinRand) ;
double ranY = RooRandom::randomGenerator()->Uniform(histMax) ;

if (ranY<hist->weight()) {
if (ranY<hist->weight(ibinRand)) {
if (wgt==1) {
histOut[ibinRand]++ ;
} else {
Expand All @@ -1768,8 +1769,7 @@ RooFit::OwningPtr<RooDataHist> RooAbsPdf::generateBinned(const RooArgSet &whatVa

// Transfer working array to histogram
for (int i=0 ; i<hist->numEntries() ; i++) {
hist->get(i) ;
hist->set(histOut[i],sqrt(1.0*histOut[i])) ;
hist->set(i, histOut[i], sqrt(1.0*histOut[i])) ;
}

} else if (expectedData) {
Expand All @@ -1779,8 +1779,8 @@ RooFit::OwningPtr<RooDataHist> RooAbsPdf::generateBinned(const RooArgSet &whatVa
// bin average and bin integral in sampling bins
double corr = nEvents/hist->sumEntries() ;
for (int i=0 ; i<hist->numEntries() ; i++) {
hist->get(i) ;
hist->set(hist->weight()*corr,sqrt(hist->weight()*corr)) ;
const double wnew = hist->weight(i)*corr ;
hist->set(i, wnew, sqrt(wnew)) ;
}

}
Expand Down
2 changes: 1 addition & 1 deletion roofit/roofitcore/src/RooAbsReal.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ RooDataHist* RooAbsReal::fillDataHist(RooDataHist *hist, const RooArgSet* normSe
const RooArgSet* obs = hist->get(i) ;
double binVal = theClone->getVal(normSet?normSet:obs)*scaleFactor ;
if (correctForBinSize) {
binVal*= hist->binVolume() ;
binVal*= hist->binVolume(i) ;
}
hist->set(i, binVal, 0.);
}
Expand Down
19 changes: 10 additions & 9 deletions roofit/roofitcore/src/RooBinnedGenContext.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,28 @@ RooDataSet *RooBinnedGenContext::generate(double nEvt, bool /*skipInit*/, bool e
double histMax(-1) ;
Int_t histOutSum(0) ;
for (int i=0 ; i<_hist->numEntries() ; i++) {
_hist->get(i) ;
const RooArgSet* coords = _hist->get(i) ;
const double wi = _hist->weight(i) ;
if (_expectedData) {

// Expected data, multiply p.d.f by nEvents
double w=_hist->weight()*nEvents ;
wudata->add(*_hist->get(),w) ;
double w=wi*nEvents ;
wudata->add(*coords,w) ;

} else if (extended) {

// Extended mode, set contents to Poisson(pdf*nEvents)
double w = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
wudata->add(*_hist->get(),w) ;
double w = RooRandom::randomGenerator()->Poisson(wi*nEvents) ;
wudata->add(*coords,w) ;

} else {

// Regular mode, fill array of weights with Poisson(pdf*nEvents), but to not fill
// histogram yet.
if (_hist->weight()>histMax) {
histMax = _hist->weight() ;
if (wi>histMax) {
histMax = wi ;
}
histOut[i] = RooRandom::randomGenerator()->Poisson(_hist->weight()*nEvents) ;
histOut[i] = RooRandom::randomGenerator()->Poisson(wi*nEvents) ;
histOutSum += histOut[i] ;
}
}
Expand All @@ -182,7 +183,7 @@ RooDataSet *RooBinnedGenContext::generate(double nEvt, bool /*skipInit*/, bool e
_hist->get(ibinRand) ;
double ranY = RooRandom::randomGenerator()->Uniform(histMax) ;

if (ranY<_hist->weight()) {
if (ranY<_hist->weight(ibinRand)) {
if (wgt==1) {
histOut[ibinRand]++ ;
} else {
Expand Down
4 changes: 2 additions & 2 deletions roofit/roofitcore/src/RooChi2Var.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,9 @@ double RooChi2Var::evaluatePartition(std::size_t firstEvent, std::size_t lastEve
}
if (!doSelect) continue ;

const double nData = hdata->weight() ;
const double nData = hdata->weight(i) ;

const double nPdf = _funcClone->getVal(_normSet) * normFactor * hdata->binVolume() ;
const double nPdf = _funcClone->getVal(_normSet) * normFactor * hdata->binVolume(i) ;

const double eExt = nPdf-nData ;

Expand Down
Loading
Loading