37std::unique_ptr<RooAbsArg> createSimultaneousNLL(
RooSimultaneous const &simPdf,
RooArgSet &observables,
bool isExtended,
38 std::string
const &rangeName,
bool doOffset)
43 for (
auto const &catItem : simPdf.indexCat()) {
44 auto const &catName = catItem.
first;
47 auto nll = std::make_unique<RooNLLVarNew>(
name.c_str(),
name.c_str(), *pdf, observables, isExtended, rangeName,
50 newObservables.
add(nll->prefixObservableAndWeightNames(std::string(
"_") + catName +
"_"));
56 observables.
add(newObservables);
59 auto nll = std::make_unique<RooAddition>(
"mynll",
"mynll", nllTerms);
60 nll->addOwnedComponents(std::move(nllTerms));
64class RooAbsRealWrapper final :
public RooAbsReal {
66 RooAbsRealWrapper(std::unique_ptr<RooFitDriver> driver, std::string
const &rangeName,
67 RooAbsCategory const *indexCatForSplitting,
bool takeGlobalObservablesFromData)
68 :
RooAbsReal{
"RooFitDriverWrapper",
"RooFitDriverWrapper"}, _driver{std::move(driver)},
69 _topNode(
"topNode",
"top node", this, _driver->topNode()), _rangeName{rangeName},
70 _indexCatForSplitting{indexCatForSplitting}, _takeGlobalObservablesFromData{takeGlobalObservablesFromData}
74 RooAbsRealWrapper(
const RooAbsRealWrapper &other,
const char *
name =
nullptr)
76 _topNode(
"topNode", this, other._topNode), _data{other._data}, _parameters{other._parameters},
77 _rangeName{other._rangeName}, _indexCatForSplitting{other._indexCatForSplitting},
78 _takeGlobalObservablesFromData{other._takeGlobalObservablesFromData}
82 TObject *clone(
const char *newname)
const override {
return new RooAbsRealWrapper(*
this, newname); }
84 double defaultErrorLevel()
const override {
return _driver->topNode().defaultErrorLevel(); }
86 bool getParameters(
const RooArgSet *observables,
RooArgSet &outputSet,
bool )
const override
88 outputSet.
add(_parameters);
90 outputSet.
remove(*observables);
96 if (_takeGlobalObservablesFromData && _data->getGlobalObservables()) {
97 outputSet.
replace(*_data->getGlobalObservables());
102 bool setData(
RooAbsData &data,
bool )
override
105 _driver->topNode().getParameters(_data->get(), _parameters,
true);
106 _driver->setData(*_data, _rangeName, _indexCatForSplitting,
true,
107 _takeGlobalObservablesFromData);
113 void applyWeightSquared(
bool flag)
override
115 const_cast<RooAbsReal &
>(_driver->topNode()).applyWeightSquared(flag);
119 double evaluate()
const override {
return _driver ? _driver->getVal() : 0.0; }
122 std::shared_ptr<RooFitDriver> _driver;
126 std::string _rangeName;
128 const bool _takeGlobalObservablesFromData;
133std::unique_ptr<RooAbsReal>
135 std::string
const &rangeName, std::string
const &addCoefRangeName,
136 RooArgSet const &projDeps,
bool isExtended,
double integrateOverBinsPrecision,
138 bool takeGlobalObservablesFromData)
145 pdfClone->recursiveRedirectServers(origParams);
150 pdfClone->getObservables(data.
get(), obsClones);
152 observables.
remove(projDeps,
true,
true);
153 obsClones.
remove(projDeps,
true,
true);
156 <<
") fixing normalization set for coefficient determination to observables in data"
158 pdfClone->fixAddCoefNormalization(obsClones,
false);
160 if (!addCoefRangeName.empty()) {
162 <<
") fixing interpretation of coefficients of any component to range "
163 << addCoefRangeName <<
"\n";
164 pdfClone->fixAddCoefRange(addCoefRangeName.c_str(),
false);
171 RooAbsPdf &finalPdf = wrappedPdf ? *wrappedPdf : *pdfClone;
173 binSamplingPdfs.
addOwned(std::move(wrappedPdf));
181 indexCatForSplitting = &simPdf->
indexCat();
184 nllTerms.
addOwned(createSimultaneousNLL(*simPdf, obsClones, isExtended, rangeName, doOffset));
186 nllTerms.
addOwned(std::make_unique<RooNLLVarNew>(
"RooNLLVarNew",
"RooNLLVarNew", finalPdf, obsClones, isExtended,
187 rangeName, doOffset));
190 nllTerms.
addOwned(std::move(constraints));
193 std::string nllName = std::string(
"nll_") + pdfClone->GetName() +
"_" + data.
GetName();
194 auto nll = std::make_unique<RooAddition>(nllName.c_str(), nllName.c_str(), nllTerms);
195 nll->addOwnedComponents(std::move(binSamplingPdfs));
196 nll->addOwnedComponents(std::move(nllTerms));
198 auto driver = std::make_unique<RooFitDriver>(*nll, obsClones, batchMode);
201 if (!rangeName.empty()) {
203 std::string fitrangeValue;
205 for (
auto const &subrange : subranges) {
206 if (subrange.empty())
208 std::string fitrangeValueSubrange = std::string(
"fit_") + nll->GetName();
209 if (subranges.size() > 1) {
210 fitrangeValueSubrange +=
"_" + subrange;
212 fitrangeValue += fitrangeValueSubrange +
",";
213 for (
auto *observable : static_range_cast<RooRealVar *>(obsClones)) {
214 observable->setRange(fitrangeValueSubrange.c_str(), observable->getMin(subrange.c_str()),
215 observable->getMax(subrange.c_str()));
218 pdf.
setStringAttribute(
"fitrange", fitrangeValue.substr(0, fitrangeValue.size() - 1).c_str());
221 auto driverWrapper = std::make_unique<RooAbsRealWrapper>(std::move(driver), rangeName, indexCatForSplitting,
222 takeGlobalObservablesFromData);
223 driverWrapper->setData(data,
false);
224 driverWrapper->addOwnedComponents(std::move(nll));
225 driverWrapper->addOwnedComponents(std::move(pdfClone));
227 return driverWrapper;
242 static std::pair<RooFit::BatchModeOption, bool> lastBatchMode;
243 if (lastBatchMode.second && lastBatchMode.first == batchMode)
245 lastBatchMode = {batchMode,
true};
248 auto log = [](std::string_view message) {
253 throw std::runtime_error(std::string(
"In: ") + __func__ +
"(), " + __FILE__ +
":" + __LINE__ +
254 ": Cuda implementation of the computing library is not available\n");
257 log(
"using generic CPU library compiled with no vectorizations");
259 log(std::string(
"using CPU computation library compiled with -m") +
263 log(
"using CUDA computation library");
RooAbsArg is the common abstract base class for objects that represent a value and a "shape" in RooFi...
virtual RooAbsArg * cloneTree(const char *newname=0) const
Clone tree expression of objects.
RooArgSet * getObservables(const RooArgSet &set, Bool_t valueOnly=kTRUE) const
Given a set of possible observables, return the observables that this PDF depends on.
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
RooArgSet * getParameters(const RooAbsData *data, bool stripDisconnected=true) const
Create a list of leaf nodes in the arg tree starting with ourself as top node that don't match any of...
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
virtual Bool_t replace(const RooAbsArg &var1, const RooAbsArg &var2)
Replace var1 with var2 and return kTRUE for success.
virtual Bool_t add(const RooAbsArg &var, Bool_t silent=kFALSE)
Add the specified argument to list.
virtual Bool_t addOwned(RooAbsArg &var, Bool_t silent=kFALSE)
Add an argument and transfer the ownership to the collection.
RooAbsArg * first() const
void clear()
Clear contents. If the collection is owning, it will also delete the contents.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsData is the common abstract base class for binned and unbinned datasets.
virtual const RooArgSet * get() const
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
virtual void fixAddCoefRange(const char *rangeName=0, Bool_t force=kTRUE)
Fix the interpretation of the coefficient of any RooAddPdf component in the expression tree headed by...
virtual void fixAddCoefNormalization(const RooArgSet &addNormSet=RooArgSet(), Bool_t force=kTRUE)
Fix the interpretation of the coefficient of any RooAddPdf component in the expression tree headed by...
RooArgList is a container object that can hold multiple RooAbsArg objects.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
static std::unique_ptr< RooAbsPdf > create(RooAbsPdf &pdf, RooAbsData const &data, double precision)
Creates a wrapping RooBinSamplingPdf if appropriate.
static RooMsgService & instance()
Return reference to singleton instance.
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
void wrapPdfsInBinSamplingPdfs(RooAbsData const &data, double precision)
Wraps the components of this RooSimultaneous in RooBinSamplingPdfs.
const RooAbsCategoryLValue & indexCat() const
RooAbsPdf * getPdf(const char *catName) const
Return the p.d.f associated with the given index category name.
virtual const char * GetName() const
Returns name of object.
Mother of all ROOT objects.
std::vector< std::string > Split(std::string_view str, std::string_view delims, bool skipEmpty=false)
Splits a string at each character in delims.
R__EXTERN RooBatchComputeInterface * dispatchCUDA
R__EXTERN RooBatchComputeInterface * dispatchCPU
This dispatch pointer points to an implementation of the compute library, provided one has been loade...
std::unique_ptr< RooAbsReal > createNLL(RooAbsPdf &pdf, RooAbsData &data, std::unique_ptr< RooAbsReal > &&constraints, std::string const &rangeName, std::string const &addCoefRangeName, RooArgSet const &projDeps, bool isExtended, double integrateOverBinsPrecision, RooFit::BatchModeOption batchMode, bool doOffset, bool takeGlobalObservablesFromData)
void logArchitectureInfo(RooFit::BatchModeOption batchMode)
BatchModeOption
For setting the batch mode flag with the BatchMode() command argument to RooAbsPdf::fitTo();.
void evaluate(typename Architecture_t::Tensor_t &A, EActivationFunction f)
Apply the given activation function to each value in the given tensor A.