Skip to content
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

Adding getProcessNorms() to CMSHistErrorPropagator #994

Merged
merged 3 commits into from
Jul 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions interface/CMSHistErrorPropagator.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class CMSHistErrorPropagator : public RooAbsReal {
RooArgList wrapperList() const;
RooArgList const& coefList() const { return coeffs_; }
RooArgList const& funcList() const { return funcs_; }

std::map<std::string, Double_t> getProcessNorms() const;

friend class CMSHistV<CMSHistErrorPropagator>;

Expand Down
37 changes: 37 additions & 0 deletions src/CMSHistErrorPropagator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -549,5 +549,42 @@ RooArgList CMSHistErrorPropagator::wrapperList() const {
return result;
}

std::map<std::string, Double_t> CMSHistErrorPropagator::getProcessNorms() const {

std::map<std::string, Double_t> vals_;
RooArgList clist(coefList());
RooArgList plist(funcList());
/*if (plist.getSize() == 1) {
CMSHistErrorPropagator *err = dynamic_cast<CMSHistErrorPropagator*>(plist.at(0));
if (err) {
clist.removeAll();
plist.removeAll();
clist.add(err->coefList());
plist.add(err->wrapperList());
}
}
*/
for (int i = 0, n = clist.getSize(); i < n; ++i) {
RooAbsReal *coeff = (RooAbsReal *) clist.at(i);
std::string coeffName = coeff->GetName();
RooAbsReal* shape = (RooAbsReal*)plist.at(i);
std::unique_ptr<RooArgSet> myobs(shape->getObservables(*x_));
TString normProdName = TString::Format("%s", coeff->GetName());
RooAbsReal * normProd = nullptr;
if (coeff->ownedComponents()) {
normProd = dynamic_cast<RooAbsReal*>(coeff->ownedComponents()->find(normProdName));
}
if (!normProd) {
RooAbsReal* integral = shape->createIntegral(*myobs);
RooArgList normProdInputs;
normProdInputs.add(*integral);
normProdInputs.add(*coeff);
normProd = new RooProduct(normProdName, "", normProdInputs);
normProd->addOwnedComponents(normProdInputs);
}
vals_[normProdName.Data()] = normProd->getVal();
}
return vals_;
}
#undef HFVERBOSE

41 changes: 24 additions & 17 deletions test/printWorkspaceNormalisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,14 @@
type="float",
help="Only print values if yield is less than this threshold.",
)
parser.add_option(
"",
"--use-cms-histsum",
dest="use_cms_histsum",
default=False,
action="store_true",
help="Set to true if workspace built with --use-cms-histsum option.",
)
# parser.add_option(
# "",
# "--use-cms-histsum",
# dest="use_cms_histsum",
# default=False,
# action="store_true",
# help="Set to true if workspace built with --use-cms-histsum option.",
# )
parser.add_option(
"",
"--procFilter",
Expand Down Expand Up @@ -169,17 +169,24 @@ def find_chan_proc(name):
else:
chan_procs[chan] = [[proc, norm, 0, 0]]

use_cms_histsum = False

all_props = ws.allFunctions().selectByName("prop_bin*")
if all_props.getSize() > 0:
use_cms_histsum = True

# Look for cases where chan stored as CMSHistSum, set flag
if options.use_cms_histsum:
if use_cms_histsum:
chan_CMSHistSum_norms = {}
all_props = ws.allFunctions().selectByName("prop_bin*")
# all_props = ws.allFunctions().selectByName("prop_bin*")
for chan in chan_procs.keys():
prop_it = all_props.createIterator()
for i in range(all_props.getSize()):
prop = prop_it.Next()
prop_name = prop.GetName()
if chan == prop_name.split("_bin")[-1]:
if type(prop) == ROOT.CMSHistSum:
types = [ROOT.CMSHistSum, ROOT.CMSHistErrorPropagator]
if type(prop) in types:
chan_CMSHistSum_norms[chan] = dict(prop.getProcessNorms())


Expand Down Expand Up @@ -243,7 +250,7 @@ def printEndExpand():
continue
print("<hr>")
print("Top-level normalization for process {proc0} -> {proc1_name}<br>".format(proc0=proc[0], proc1_name=proc[1].GetName()))
if options.use_cms_histsum:
if use_cms_histsum:
if chan in chan_CMSHistSum_norms:
default_val = chan_CMSHistSum_norms[chan][proc[1].GetName()]
else:
Expand All @@ -253,7 +260,7 @@ def printEndExpand():
default_norms[chan][proc[1].GetName()] = default_val

if options.printValueOnly:
print("default value = {default_val}<br>".format(default_val=default_val))
print("default value = {default_val:.5f}<br>".format(default_val=default_val))
else:
if proc[2]:
proc_norm_var = ws.function("n_exp_bin%s_proc_%s" % (chan, proc[0]))
Expand Down Expand Up @@ -289,7 +296,7 @@ def printEndExpand():
print(" ... is a constant (RooRealVar)")
printEndExpand()

print(" default value = ", default_val, "<br>")
print(" default value = %.5f " % default_val, "<br>")
print("</tr>")

print(
Expand Down Expand Up @@ -320,7 +327,7 @@ def printEndExpand():
print("---------------------------------------------------------------------------")
print(" Top-level normalization for process %s -> %s" % (proc[0], proc[1].GetName()))
print("---------------------------------------------------------------------------")
if options.use_cms_histsum:
if use_cms_histsum:
if chan in chan_CMSHistSum_norms:
default_val = chan_CMSHistSum_norms[chan][proc[1].GetName()]
else:
Expand All @@ -330,7 +337,7 @@ def printEndExpand():
default_norms[chan][proc[1].GetName()] = default_val

if options.printValueOnly:
print(" default value = ", default_val)
print(" default value = %.5f " % default_val)
# if options.printValueOnly: print " --xcp %s:%s "%(chan,proc[0]),
else:
if proc[2]:
Expand All @@ -354,7 +361,7 @@ def printEndExpand():
proc[1].Print()
print(" ... is a constant (RooRealVar)")
print(" -------------------------------------------------------------------------")
print(" default value = ", default_val)
print(" default value = %.5f " % default_val)

# Save norms to json file
if options.output_json != "":
Expand Down
Loading