-
Notifications
You must be signed in to change notification settings - Fork 1.5k
[RF] Let RooGenericPdf/RooFormulaVar declare a piecewise-flat binning #22708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
guitargeek
wants to merge
1
commit into
root-project:master
Choose a base branch
from
guitargeek:flat_binning
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+493
−7
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,6 +48,10 @@ the names of the arguments are not hard coded. | |
| #include "RooMsgService.h" | ||
| #include "RooArgList.h" | ||
| #include "RooFormula.h" | ||
| #include "RooAbsRealLValue.h" | ||
| #include "RooAbsBinning.h" | ||
| #include "RooCurve.h" | ||
| #include "RooHelpers.h" | ||
|
|
||
| using std::istream, std::ostream, std::endl; | ||
|
|
||
|
|
@@ -107,6 +111,9 @@ RooGenericPdf::RooGenericPdf(const RooGenericPdf& other, const char* name) : | |
| _actualVars("actualVars",this,other._actualVars), | ||
| _formExpr(other._formExpr) | ||
| { | ||
| for (auto const &item : other._binnings) { | ||
| _binnings[item.first] = std::unique_ptr<RooAbsBinning>{item.second->clone()}; | ||
| } | ||
| formula(); | ||
| } | ||
|
|
||
|
|
@@ -122,7 +129,99 @@ RooFormula& RooGenericPdf::formula() const | |
| return *_formula ; | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Declare that this pdf is piecewise constant (flat) within the bins of the | ||
| /// given `binning` of the observable `obs`, which must be one of the formula | ||
| /// variables. The method can be called several times to set a binning for more | ||
| /// than one observable. Use a RooUniformBinning to describe many uniform bins | ||
| /// compactly. | ||
| /// | ||
| /// Once set, integrals over `obs` use the fast bin integrator (which sums the | ||
| /// central value of each bin times the bin width) instead of the generic | ||
| /// numeric integrator, and plotting samples the step shape exactly. | ||
| /// | ||
| /// If `checkFlatness` is true (the default), the function is sampled at several | ||
| /// points inside each bin to verify that it is indeed flat; if it is not, an | ||
| /// error is issued and the binning is not stored. | ||
|
|
||
| void RooGenericPdf::setBinBoundaries(RooAbsRealLValue &obs, const RooAbsBinning &binning, bool checkFlatness) | ||
| { | ||
| // Match the observable to a formula variable by name, so that a same-named | ||
| // stand-in for the actual server is accepted too. | ||
| const int idx = _actualVars.index(obs.GetName()); | ||
| if (idx < 0) { | ||
| coutE(InputArguments) << "RooGenericPdf::setBinBoundaries(" << GetName() << ") the observable " << obs.GetName() | ||
| << " is not one of the formula variables of this pdf, nothing done." << std::endl; | ||
| return; | ||
| } | ||
|
|
||
| if (checkFlatness) { | ||
| // Vary the actual formula variable (the server), which may be a different | ||
| // object than `obs` if `obs` is just a same-named stand-in. | ||
| auto *serverObs = dynamic_cast<RooAbsRealLValue *>(_actualVars.at(idx)); | ||
| RooAbsRealLValue &flatObs = serverObs ? *serverObs : obs; | ||
| std::span<const double> boundaries{binning.array(), static_cast<std::size_t>(binning.numBoundaries())}; | ||
| if (!RooHelpers::isFunctionFlatInBins(*this, flatObs, boundaries)) { | ||
| coutE(InputArguments) << "RooGenericPdf::setBinBoundaries(" << GetName() << ") the expression \"" << _formExpr | ||
| << "\" is not flat within the given bins of " << obs.GetName() | ||
| << ". The binning is not set. Pass checkFlatness=false to override this check." | ||
| << std::endl; | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| // Key the binning by the observable's index in _actualVars (not its name), so | ||
| // that it survives a renaming of the variable or a server redirection. | ||
| _binnings[idx] = std::unique_ptr<RooAbsBinning>{binning.clone()}; | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Return true if a binning was set with setBinBoundaries() for every | ||
| /// observable in the integration set `obs`. | ||
|
|
||
| bool RooGenericPdf::isBinnedDistribution(const RooArgSet &obs) const | ||
| { | ||
| if (obs.empty() || _binnings.empty()) { | ||
| return false; | ||
| } | ||
| for (RooAbsArg *o : obs) { | ||
| if (_binnings.find(_actualVars.index(o->GetName())) == _binnings.end()) { | ||
| return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if obs contains a variable that we do not depend on, will this return false then? If I look at behaviour of e.g. RooProduct's implementation: https://root.cern.ch/doc/master/RooProduct_8cxx_source.html#l00420 wont it return true if it doesn't depend on the variables passed? |
||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Return the boundaries of the binning set with setBinBoundaries() that fall | ||
| /// within [xlo, xhi], or a null pointer if no binning was set for this observable. | ||
|
|
||
| std::list<double> *RooGenericPdf::binBoundaries(RooAbsRealLValue &obs, double xlo, double xhi) const | ||
| { | ||
| auto found = _binnings.find(_actualVars.index(obs.GetName())); | ||
| if (found == _binnings.end()) { | ||
| return nullptr; | ||
| } | ||
| const RooAbsBinning &binning = *found->second; | ||
| return RooHelpers::binBoundariesInRange({binning.array(), static_cast<std::size_t>(binning.numBoundaries())}, xlo, | ||
| xhi); | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Return sampling hints that draw the piecewise-flat shape exactly (a pair of | ||
| /// points just left and right of every bin boundary), or a null pointer if no | ||
| /// binning was set for this observable. | ||
|
|
||
| std::list<double> *RooGenericPdf::plotSamplingHint(RooAbsRealLValue &obs, double xlo, double xhi) const | ||
| { | ||
| auto found = _binnings.find(_actualVars.index(obs.GetName())); | ||
| if (found == _binnings.end()) { | ||
| return nullptr; | ||
| } | ||
| const RooAbsBinning &binning = *found->second; | ||
| return RooCurve::plotSamplingHintForBinBoundaries( | ||
| {binning.array(), static_cast<std::size_t>(binning.numBoundaries())}, xlo, xhi); | ||
| } | ||
|
|
||
| //////////////////////////////////////////////////////////////////////////////// | ||
| /// Calculate current value of this object | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Part of me thinks it would be nice that user can reverse this operation, but this method doesn't allow for that? If don't want to change the signature, then have a r
emoveBinBoundaries(RooAbsRealLValue &obs)method or similar?Also why
RooAbsRealLValue &obsand notconst RooAbsRealLValue &obs?