Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
BatchModeHelpers.cxx
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * Jonas Rembser, CERN 2021
5 *
6 * Copyright (c) 2021, CERN
7 *
8 * Redistribution and use in source and binary forms,
9 * with or without modification, are permitted according to the terms
10 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
11 */
12
14#include <RooAbsData.h>
15#include <RooAbsPdf.h>
16#include <RooAddition.h>
17#include <RooBatchCompute.h>
18#include <RooBinSamplingPdf.h>
19#include <RooCategory.h>
20#include <RooConstraintSum.h>
21#include <RooDataSet.h>
22#include "RooNLLVarNew.h"
23#include <RooRealVar.h>
24#include <RooSimultaneous.h>
25
26#include <string>
27
29
30namespace {
31
32std::unique_ptr<RooAbsArg> createSimultaneousNLL(RooSimultaneous const &simPdf, bool isExtended,
33 std::string const &rangeName, RooFit::OffsetMode offset)
34{
35 RooAbsCategoryLValue const &simCat = simPdf.indexCat();
36
37 // Prepare the NLL terms for each component
38 RooArgList nllTerms;
39 for (auto const &catState : simCat) {
40 std::string const &catName = catState.first;
41 RooAbsCategory::value_type catIndex = catState.second;
42
43 // If the channel is not in the selected range of the category variable, we
44 // won't create an NLL this channel.
45 if (!rangeName.empty()) {
46 // Only the RooCategory supports ranges, not the other
47 // RooAbsCategoryLValue-derived classes.
48 auto simCatAsRooCategory = dynamic_cast<RooCategory const *>(&simCat);
49 if (simCatAsRooCategory && !simCatAsRooCategory->isStateInRange(rangeName.c_str(), catIndex)) {
50 continue;
51 }
52 }
53
54 if (RooAbsPdf *pdf = simPdf.getPdf(catName.c_str())) {
55 auto name = std::string("nll_") + pdf->GetName();
56 std::unique_ptr<RooArgSet> observables(
57 static_cast<RooArgSet *>(std::unique_ptr<RooArgSet>(pdf->getVariables())->selectByAttrib("__obs__", true)));
58 auto nll = std::make_unique<RooNLLVarNew>(name.c_str(), name.c_str(), *pdf, *observables, isExtended, offset);
59 // Rename the special variables
60 nll->setPrefix(std::string("_") + catName + "_");
61 nllTerms.addOwned(std::move(nll));
62 }
63 }
64
65 for (auto *nll : static_range_cast<RooNLLVarNew *>(nllTerms)) {
66 nll->setSimCount(nllTerms.size());
67 }
68
69 // Time to sum the NLLs
70 auto nll = std::make_unique<RooAddition>("mynll", "mynll", nllTerms);
71 nll->addOwnedComponents(std::move(nllTerms));
72 return nll;
73}
74
75} // namespace
76
77std::unique_ptr<RooAbsReal>
78RooFit::BatchModeHelpers::createNLL(RooAbsPdf &pdf, RooAbsData &data, std::unique_ptr<RooAbsReal> &&constraints,
79 std::string const &rangeName, RooArgSet const &projDeps, bool isExtended,
80 double integrateOverBinsPrecision, RooFit::OffsetMode offset)
81{
82 if (constraints) {
83 // Redirect the global observables to the ones from the dataset if applicable.
84 constraints->setData(data, false);
85
86 // The computation graph for the constraints is very small, no need to do
87 // the tracking of clean and dirty nodes here.
88 constraints->setOperMode(RooAbsArg::ADirty);
89 }
90
91 RooArgSet observables;
92 pdf.getObservables(data.get(), observables);
93 observables.remove(projDeps, true, true);
94
95 oocxcoutI(&pdf, Fitting) << "RooAbsPdf::fitTo(" << pdf.GetName()
96 << ") fixing normalization set for coefficient determination to observables in data"
97 << "\n";
98 pdf.fixAddCoefNormalization(observables, false);
99
100 // Deal with the IntegrateBins argument
101 RooArgList binSamplingPdfs;
102 std::unique_ptr<RooAbsPdf> wrappedPdf = RooBinSamplingPdf::create(pdf, data, integrateOverBinsPrecision);
103 RooAbsPdf &finalPdf = wrappedPdf ? *wrappedPdf : pdf;
104 if (wrappedPdf) {
105 binSamplingPdfs.addOwned(std::move(wrappedPdf));
106 }
107 // Done dealing with the IntegrateBins option
108
109 RooArgList nllTerms;
110
111 auto simPdf = dynamic_cast<RooSimultaneous *>(&finalPdf);
112 if (simPdf) {
113 simPdf->wrapPdfsInBinSamplingPdfs(data, integrateOverBinsPrecision);
114 nllTerms.addOwned(createSimultaneousNLL(*simPdf, isExtended, rangeName, offset));
115 } else {
116 nllTerms.addOwned(
117 std::make_unique<RooNLLVarNew>("RooNLLVarNew", "RooNLLVarNew", finalPdf, observables, isExtended, offset));
118 }
119 if (constraints) {
120 nllTerms.addOwned(std::move(constraints));
121 }
122
123 std::string nllName = std::string("nll_") + pdf.GetName() + "_" + data.GetName();
124 auto nll = std::make_unique<RooAddition>(nllName.c_str(), nllName.c_str(), nllTerms);
125 nll->addOwnedComponents(std::move(binSamplingPdfs));
126 nll->addOwnedComponents(std::move(nllTerms));
127
128 return nll;
129}
ROOT::RRangeCast< T, false, Range_t > static_range_cast(Range_t &&coll)
#define oocxcoutI(o, a)
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void data
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
char name[80]
Definition TGX11.cxx:110
RooFit::OwningPtr< RooArgSet > getObservables(const RooArgSet &set, bool valueOnly=true) const
Given a set of possible observables, return the observables that this PDF depends on.
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
virtual bool remove(const RooAbsArg &var, bool silent=false, bool matchByNameOnly=false)
Remove the specified argument from our list.
Storage_t::size_type size() const
RooAbsArg * first() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
RooAbsData is the common abstract base class for binned and unbinned datasets.
Definition RooAbsData.h:59
virtual void fixAddCoefNormalization(const RooArgSet &addNormSet=RooArgSet(), bool force=true)
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.
Definition RooArgList.h:22
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
static std::unique_ptr< RooAbsPdf > create(RooAbsPdf &pdf, RooAbsData const &data, double precision)
Creates a wrapping RooBinSamplingPdf if appropriate.
RooCategory is an object to represent discrete states.
Definition RooCategory.h:28
RooSimultaneous facilitates simultaneous fitting of multiple PDFs to subsets of a given dataset.
RooAbsPdf * getPdf(RooStringView catName) const
Return the p.d.f associated with the given index category name.
void wrapPdfsInBinSamplingPdfs(RooAbsData const &data, double precision)
Wraps the components of this RooSimultaneous in RooBinSamplingPdfs.
const RooAbsCategoryLValue & indexCat() const
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
std::unique_ptr< RooAbsReal > createNLL(RooAbsPdf &pdf, RooAbsData &data, std::unique_ptr< RooAbsReal > &&constraints, std::string const &rangeName, RooArgSet const &projDeps, bool isExtended, double integrateOverBinsPrecision, RooFit::OffsetMode offset)
OffsetMode
For setting the offset mode with the Offset() command argument to RooAbsPdf::fitTo()