Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
[RF] Cleanup RooLagrangianMorphFunc
  • Loading branch information
rahulgrit authored and guitargeek committed Jul 26, 2022
commit bcc3b3df378999612ad14eadcd38f9db676fb5ed
45 changes: 22 additions & 23 deletions roofit/roofit/inc/RooLagrangianMorphFunc.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class RooLagrangianMorphFunc : public RooAbsReal {
RooArgList prodCouplings;
RooArgList folders;
std::vector<RooArgList *> vertices;
std::vector<RooArgList *> nonInterfering;
std::vector<std::vector<const char *>> nonInterfering;
bool allowNegativeYields = true;
};

Expand All @@ -108,24 +108,21 @@ class RooLagrangianMorphFunc : public RooAbsReal {

virtual ~RooLagrangianMorphFunc();

virtual std::list<Double_t> *
binBoundaries(RooAbsRealLValue & /*obs*/, Double_t /*xlo*/, Double_t /*xhi*/) const override;
virtual std::list<Double_t> *
plotSamplingHint(RooAbsRealLValue & /*obs*/, Double_t /*xlo*/, Double_t /*xhi*/) const override;
virtual Bool_t isBinnedDistribution(const RooArgSet &obs) const override;
virtual Double_t evaluate() const override;
virtual TObject *clone(const char *newname) const override;
virtual Double_t getValV(const RooArgSet *set = 0) const override;

virtual Bool_t checkObservables(const RooArgSet *nset) const override;
virtual Bool_t forceAnalyticalInt(const RooAbsArg &arg) const override;
virtual Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &numVars, const RooArgSet *normSet,
const char *rangeName = 0) const override;
virtual Double_t
analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName = 0) const override;
virtual void printMetaArgs(std::ostream &os) const override;
virtual RooAbsArg::CacheMode canNodeBeCached() const override;
virtual void setCacheAndTrackHints(RooArgSet &) override;
std::list<double> *binBoundaries(RooAbsRealLValue & /*obs*/, double /*xlo*/, double /*xhi*/) const override;
std::list<double> *plotSamplingHint(RooAbsRealLValue & /*obs*/, double /*xlo*/, double /*xhi*/) const override;
bool isBinnedDistribution(const RooArgSet &obs) const override;
double evaluate() const override;
TObject *clone(const char *newname) const override;
double getValV(const RooArgSet *set = 0) const override;

bool checkObservables(const RooArgSet *nset) const override;
bool forceAnalyticalInt(const RooAbsArg &arg) const override;
Int_t getAnalyticalIntegralWN(RooArgSet &allVars, RooArgSet &numVars, const RooArgSet *normSet,
const char *rangeName = 0) const override;
double analyticalIntegralWN(Int_t code, const RooArgSet *normSet, const char *rangeName = 0) const override;
void printMetaArgs(std::ostream &os) const override;
RooAbsArg::CacheMode canNodeBeCached() const override;
void setCacheAndTrackHints(RooArgSet &) override;

void insert(RooWorkspace *ws);

Expand Down Expand Up @@ -178,11 +175,13 @@ class RooLagrangianMorphFunc : public RooAbsReal {
TH1 *createTH1(const std::string &name);
TH1 *createTH1(const std::string &name, bool correlateErrors);

protected:
private:
class CacheElem;
void init();
void setup(bool ownParams = true);
bool _ownParameters = false;
void disableInterference(const std::vector<const char *> &nonInterfering);
void disableInterferences(const std::vector<std::vector<const char *>> &nonInterfering);

mutable RooObjCacheManager _cacheMgr; //! The cache manager

Expand All @@ -206,7 +205,6 @@ class RooLagrangianMorphFunc : public RooAbsReal {
int countSamples(std::vector<RooArgList *> &vertices);
int countSamples(int nprod, int ndec, int nboth);

TPair *makeCrosssectionContainer(double xs, double unc);
std::map<std::string, std::string>
createWeightStrings(const ParamMap &inputs, const std::vector<std::vector<std::string>> &vertices);
std::map<std::string, std::string>
Expand Down Expand Up @@ -252,7 +250,7 @@ class RooLagrangianMorphFunc : public RooAbsReal {

static std::unique_ptr<RooRatio> makeRatio(const char *name, const char *title, RooArgList &nr, RooArgList &dr);

protected:
private:
double _scale = 1;
std::map<std::string, int> _sampleMap;
RooListProxy _physics;
Expand All @@ -262,7 +260,8 @@ class RooLagrangianMorphFunc : public RooAbsReal {
RooListProxy _flags;
Config _config;
std::vector<std::vector<RooListProxy *>> _diagrams;
mutable const RooArgSet *_curNormSet; //!
mutable const RooArgSet *_curNormSet = nullptr; //!

std::vector<RooListProxy *> _nonInterfering;

ClassDefOverride(RooLagrangianMorphFunc, 1)
Expand Down
144 changes: 43 additions & 101 deletions roofit/roofit/src/RooLagrangianMorphFunc.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -638,43 +638,6 @@ inline void extractCouplings(const T1 &inCouplings, T2 &outCouplings)
}
}

///////////////////////////////////////////////////////////////////////////////
/// find and, if necessary, create a parameter from a list

template <class T>
inline RooAbsArg &get(T &operators, const char *name, double defaultval = 0)
{
RooAbsArg *kappa = operators.find(name);
if (kappa)
return *kappa;
RooRealVar *newKappa = new RooRealVar(name, name, defaultval);
newKappa->setConstant(false);
operators.add(*newKappa);
return *newKappa;
}

///////////////////////////////////////////////////////////////////////////////
/// find and, if necessary, create a parameter from a list

template <class T>
inline RooAbsArg &get(T &operators, const std::string &name, double defaultval = 0)
{
return get(operators, name.c_str(), defaultval);
}

////////////////////////////////////////////////////////////////////////////////
/// create a new coupling and add it to the set

template <class T>
inline void addCoupling(T &set, const TString &name, const TString &formula, const RooArgList &components, bool isNP)
{
if (!set.find(name)) {
RooFormulaVar *c = new RooFormulaVar(name, formula, components);
c->setAttribute("NewPhysics", isNP);
set.add(*c);
}
}

////////////////////////////////////////////////////////////////////////////////
/// set parameter values first set all values to defaultVal (if value not
/// present in param_card then it should be 0)
Expand Down Expand Up @@ -1396,9 +1359,8 @@ class RooLagrangianMorphFunc::CacheElem : public RooAbsCacheElement {
extractCouplings(*vertex, this->_couplings);
}
}
extractOperators(this->_couplings, operators);
this->_formulas =
::createFormulas(funcname, inputParameters, inputFlags, diagrams, this->_couplings, flags, nonInterfering);
extractOperators(_couplings, operators);
_formulas = ::createFormulas(funcname, inputParameters, inputFlags, diagrams, _couplings, flags, nonInterfering);
}

//////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -1771,18 +1733,15 @@ void RooLagrangianMorphFunc::collectInputs(TDirectory *file)

RooRealVar *observable = this->setupObservable(obsName.c_str(), mode, obj);
if (classname.find("TH1") != std::string::npos) {
collectHistograms(this->GetName(), file, this->_sampleMap, this->_physics, *observable, obsName,
this->_config.paramCards);
collectHistograms(this->GetName(), file, _sampleMap, _physics, *observable, obsName, _config.paramCards);
} else if (classname.find("RooHistFunc") != std::string::npos ||
classname.find("RooParamHistFunc") != std::string::npos ||
classname.find("PiecewiseInterpolation") != std::string::npos) {
collectRooAbsReal(this->GetName(), file, this->_sampleMap, this->_physics, obsName, this->_config.paramCards);
} else if (classname.find("TParameter<double>") != std::string::npos) {
collectCrosssections<double>(this->GetName(), file, this->_sampleMap, this->_physics, obsName,
this->_config.paramCards);
collectCrosssections<double>(this->GetName(), file, _sampleMap, _physics, obsName, _config.paramCards);
} else if (classname.find("TParameter<float>") != std::string::npos) {
collectCrosssections<float>(this->GetName(), file, this->_sampleMap, this->_physics, obsName,
this->_config.paramCards);
collectCrosssections<float>(this->GetName(), file, _sampleMap, _physics, obsName, _config.paramCards);
} else if (classname.find("TPair") != std::string::npos) {
collectCrosssectionsTPair(this->GetName(), file, this->_sampleMap, this->_physics, obsName, folderNames[0],
this->_config.paramCards);
Expand Down Expand Up @@ -1868,6 +1827,7 @@ RooLagrangianMorphFunc::RooLagrangianMorphFunc(const char *name, const char *tit
_binWidths("binWidths", "set of binWidth objects", this, kTRUE, kFALSE), _config(config), _curNormSet(0)
{
this->init();
this->disableInterferences(_config.nonInterfering);
this->setup(false);

TRACE_CREATE
Expand Down Expand Up @@ -1899,42 +1859,9 @@ RooLagrangianMorphFunc::RooLagrangianMorphFunc(const char *name, const char *tit

void RooLagrangianMorphFunc::setup(bool own)
{
this->_ownParameters = own;
auto diagrams = this->_diagrams;

if (diagrams.size() > 0) {
RooArgList operators;
for (auto const &v : diagrams) {
for (const RooArgList *t : v) {
extractOperators(*t, operators);
}
}

if (own) {
this->_operators.addOwned(operators);
} else {
this->_operators.add(operators);
}

for (size_t j = 0; j < diagrams.size(); ++j) {
std::vector<RooListProxy *> diagram;
for (size_t i = 0; i < diagrams[j].size(); ++i) {
std::stringstream name;
name << "!vertex" << i;
std::stringstream title;
title << "set of couplings in the vertex " << i;
diagram.push_back(new RooListProxy(name.str().c_str(), title.str().c_str(), this, kTRUE, kFALSE));
if (own) {
diagram[i]->addOwned(*diagrams[j][i]);
} else {
diagram[i]->add(*diagrams[j][i]);
}
}
this->_diagrams.push_back(diagram);
}
}
_ownParameters = own;

else if (this->_config.couplings.size() > 0) {
if (_config.couplings.size() > 0) {
RooArgList operators;
std::vector<RooListProxy *> vertices;
extractOperators(this->_config.couplings, operators);
Expand Down Expand Up @@ -1973,7 +1900,36 @@ void RooLagrangianMorphFunc::setup(bool own)
}

////////////////////////////////////////////////////////////////////////////////
/// (-?-)
/// disable interference between terms

void RooLagrangianMorphFunc::disableInterference(const std::vector<const char *> &nonInterfering)
{
// disable interference between the listed operators
std::stringstream name;
name << "noInteference";
for (auto c : nonInterfering) {
name << c;
}
RooListProxy *p = new RooListProxy(name.str().c_str(), name.str().c_str(), this, kTRUE, kFALSE);
this->_nonInterfering.push_back(p);
for (auto c : nonInterfering) {
p->addOwned(*(new RooStringVar(c, c, c)));
}
}

////////////////////////////////////////////////////////////////////////////////
/// disable interference between terms

void RooLagrangianMorphFunc::disableInterferences(const std::vector<std::vector<const char *>> &nonInterfering)
{
// disable interferences between the listed groups of operators
for (size_t i = 0; i < nonInterfering.size(); ++i) {
this->disableInterference(nonInterfering[i]);
}
}

////////////////////////////////////////////////////////////////////////////////
/// initialise inputs required for the morphing function

void RooLagrangianMorphFunc::init()
{
Expand Down Expand Up @@ -2104,9 +2060,6 @@ int RooLagrangianMorphFunc::countSamples(int nprod, int ndec, int nboth)

////////////////////////////////////////////////////////////////////////////////
/// calculate the number of samples needed to morph a certain physics process
/// usage:
/// countSamples ( { RooLagrangianMorphFunc::makeHCggfCouplings(),
/// RooLagrangianMorphFunc::makeHCHZZCouplings() } )

int RooLagrangianMorphFunc::countSamples(std::vector<RooArgList *> &vertices)
{
Expand All @@ -2122,15 +2075,6 @@ int RooLagrangianMorphFunc::countSamples(std::vector<RooArgList *> &vertices)
return morphfuncpattern.size();
}

////////////////////////////////////////////////////////////////////////////////
/// create TPair containers of the type expected by the RooLagrangianMorph

TPair *RooLagrangianMorphFunc::makeCrosssectionContainer(double xs, double unc)
{
TPair *v = new TPair(new TParameter<double>("xsection", xs), new TParameter<double>("uncertainty", unc));
return v;
}

////////////////////////////////////////////////////////////////////////////////
/// create only the weight formulas. static function for external usage.

Expand Down Expand Up @@ -2618,24 +2562,22 @@ RooRealVar *RooLagrangianMorphFunc::getBinWidth() const
////////////////////////////////////////////////////////////////////////////////
/// retrieve a histogram output of the current morphing settings

TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name
/*RooFitResult *r*/)
TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name)
{
return this->createTH1(name, false /*r*/);
return this->createTH1(name, false);
}

////////////////////////////////////////////////////////////////////////////////
/// retrieve a histogram output of the current morphing settings

TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name, bool correlateErrors
/* RooFitResult *r*/)
TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name, bool correlateErrors)
{
auto mf = std::make_unique<RooRealSumFunc>(*(this->getFunc()));
RooRealVar *observable = this->getObservable();

const int nbins = observable->getBins();

TH1 *hist = new TH1F(name.c_str(), name.c_str(), nbins, observable->getBinning().array());
auto hist = std::make_unique<TH1F>(name.c_str(), name.c_str(), nbins, observable->getBinning().array());

RooArgSet *args = mf->getComponents();
for (int i = 0; i < nbins; ++i) {
Expand Down Expand Up @@ -2663,7 +2605,7 @@ TH1 *RooLagrangianMorphFunc::createTH1(const std::string &name, bool correlateEr
hist->SetBinContent(i + 1, val);
hist->SetBinError(i + 1, correlateErrors ? unc : sqrt(unc2));
}
return hist;
return hist.release();
}

////////////////////////////////////////////////////////////////////////////////
Expand Down