Skip to content

Commit 34361dc

Browse files
committed
[RF] Upstream from CMS Combine functions relevant for parameter freezing
Upstream from CMS Combine functions relevant for parameter freezing when using RooMultiPdfs.
1 parent 7f76766 commit 34361dc

File tree

5 files changed

+77
-10
lines changed

5 files changed

+77
-10
lines changed

roofit/roofitcore/inc/RooAbsCollection.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,8 @@ class RooAbsCollection : public TObject, public RooPrintable {
362362

363363
void useHashMapForFind(bool flag) const;
364364

365+
void removeConstantParameters();
366+
365367
// For use in the RooArgList/Set(std::vector<RooAbsArgPtrOrDouble> const&) constructor.
366368
// Can be replaced with std::variant when C++17 is the minimum supported standard.
367369
struct RooAbsArgPtrOrDouble {

roofit/roofitcore/inc/RooHelpers.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_lis
8888
double max = std::numeric_limits<double>::max(), bool limitsInAllowedRange = false,
8989
std::string const &extraMessage = "");
9090

91+
/// set all RooRealVars to constants. return true if at least one changed status
92+
bool setAllConstant(const RooAbsCollection &coll, bool constant = true);
93+
94+
bool freezeAllDisassociatedRooMultiPdfParameters(const RooArgSet &multiPdfs, const RooArgSet &allRooMultiPdfParams,
95+
bool freeze = true, bool freezeDisassParams_verb = false);
9196

9297
} // namespace RooHelpers
9398

roofit/roofitcore/src/RooAbsCollection.cxx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,3 +1646,13 @@ void RooAbsCollection::throwAddTypedException(TClass *klass, RooAbsArg *arg)
16461646
oocoutE(nullptr, InputArguments) << msg.str() << std::endl;
16471647
throw std::invalid_argument(msg.str());
16481648
}
1649+
1650+
void RooAbsCollection::removeConstantParameters()
1651+
{
1652+
RooArgSet constSet;
1653+
for (auto const *myarg : static_range_cast<RooRealVar *>(*this)) {
1654+
if (myarg->isConstant())
1655+
constSet.add(*myarg);
1656+
}
1657+
this->remove(constSet);
1658+
}

roofit/roofitcore/src/RooHelpers.cxx

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
#include <RooAbsPdf.h>
1818
#include <RooAbsRealLValue.h>
1919
#include <RooArgList.h>
20+
#include <RooCategory.h>
2021
#include <RooDataHist.h>
2122
#include <RooDataSet.h>
23+
#include <RooMultiPdf.h>
2224
#include <RooProdPdf.h>
2325
#include <RooRealSumPdf.h>
26+
#include <RooRealVar.h>
2427
#include <RooSimultaneous.h>
2528

2629
#include <ROOT/StringUtils.hxx>
@@ -148,4 +151,59 @@ void checkRangeOfParameters(const RooAbsReal *callingClass, std::initializer_lis
148151
}
149152
}
150153

154+
bool setAllConstant(const RooAbsCollection &coll, bool constant)
155+
{
156+
bool changed = false;
157+
for (RooAbsArg *a : coll) {
158+
RooRealVar *v = dynamic_cast<RooRealVar *>(a);
159+
RooCategory *cv = dynamic_cast<RooCategory *>(a);
160+
if (v && (v->isConstant() != constant)) {
161+
changed = true;
162+
v->setConstant(constant);
163+
} else if (cv && (cv->isConstant() != constant)) {
164+
changed = true;
165+
cv->setConstant(constant);
166+
}
167+
}
168+
return changed;
169+
}
170+
171+
bool freezeAllDisassociatedRooMultiPdfParameters(const RooArgSet &multiPdfs, const RooArgSet &allRooMultiPdfParams,
172+
bool freeze, bool freezeDisassParams_verb)
173+
{
174+
RooArgSet multiPdfParams(allRooMultiPdfParams);
175+
176+
// For each multiPdf, get the active pdf and remove its parameters
177+
// from this list of params and then freeze the remaining ones
178+
179+
for (RooAbsArg *P : multiPdfs) {
180+
RooMultiPdf *mpdf = dynamic_cast<RooMultiPdf *>(P);
181+
RooAbsPdf *pdf = (RooAbsPdf *)mpdf->getCurrentPdf();
182+
if (freezeDisassParams_verb) {
183+
std::cout << " Current active PDF - " << pdf->GetName() << std::endl;
184+
}
185+
std::unique_ptr<RooArgSet> pdfPars(pdf->getParameters((const RooArgSet *)0));
186+
pdfPars->removeConstantParameters(); // make sure still to ignore user set constants
187+
multiPdfParams.remove(*pdfPars);
188+
}
189+
190+
if (multiPdfParams.getSize() > 0) {
191+
if (freezeDisassParams_verb) {
192+
std::cout << " Going to " << (freeze ? " freeze " : " float ") << " the following (disassociated) parameters"
193+
<< std::endl;
194+
multiPdfParams.Print("V");
195+
}
196+
setAllConstant(multiPdfParams, freeze);
197+
198+
// std::cout << " Current state of all MultiPdfParams -> " << std::endl;
199+
// std::unique_ptr<TIterator> iter_par(allRooMultiPdfParams.createIterator());
200+
// for (RooAbsArg *P = (RooAbsArg *) iter_par->Next(); P != 0; P = (RooAbsArg *) iter_par->Next()){
201+
// std::cout << P->GetName() << ", constant=" << P->isConstant() << std::endl;
202+
// }
203+
return true;
204+
}
205+
206+
return false;
207+
}
208+
151209
} // namespace RooHelpers

roofit/roostats/inc/RooStats/RooStatsUtils.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,11 @@ namespace RooStats {
6565
}
6666

6767
inline void RemoveConstantParameters(RooArgSet* set){
68-
RooArgSet constSet;
69-
for (auto const *myarg : static_range_cast<RooRealVar *>(*set)) {
70-
if(myarg->isConstant()) constSet.add(*myarg);
71-
}
72-
set->remove(constSet);
68+
set->removeConstantParameters();
7369
}
7470

7571
inline void RemoveConstantParameters(RooArgList& set){
76-
RooArgSet constSet;
77-
for (auto const *myarg : static_range_cast<RooRealVar *>(set)) {
78-
if(myarg->isConstant()) constSet.add(*myarg);
79-
}
80-
set.remove(constSet);
72+
set.removeConstantParameters();
8173
}
8274

8375
/// utility function to set all variable constant in a collection

0 commit comments

Comments
 (0)