Skip to content

Commit caf347d

Browse files
authored
Merge pull request #994 from cms-analysis/nckw_add_procnormsreturn_to_CMSHistErrorPropagator
Adding getProcessNorms() to CMSHistErrorPropagator
2 parents 0cb4611 + d8224f8 commit caf347d

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

interface/CMSHistErrorPropagator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class CMSHistErrorPropagator : public RooAbsReal {
7272
RooArgList wrapperList() const;
7373
RooArgList const& coefList() const { return coeffs_; }
7474
RooArgList const& funcList() const { return funcs_; }
75+
76+
std::map<std::string, Double_t> getProcessNorms() const;
7577

7678
friend class CMSHistV<CMSHistErrorPropagator>;
7779

src/CMSHistErrorPropagator.cc

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,5 +549,42 @@ RooArgList CMSHistErrorPropagator::wrapperList() const {
549549
return result;
550550
}
551551

552+
std::map<std::string, Double_t> CMSHistErrorPropagator::getProcessNorms() const {
553+
554+
std::map<std::string, Double_t> vals_;
555+
RooArgList clist(coefList());
556+
RooArgList plist(funcList());
557+
/*if (plist.getSize() == 1) {
558+
CMSHistErrorPropagator *err = dynamic_cast<CMSHistErrorPropagator*>(plist.at(0));
559+
if (err) {
560+
clist.removeAll();
561+
plist.removeAll();
562+
clist.add(err->coefList());
563+
plist.add(err->wrapperList());
564+
}
565+
}
566+
*/
567+
for (int i = 0, n = clist.getSize(); i < n; ++i) {
568+
RooAbsReal *coeff = (RooAbsReal *) clist.at(i);
569+
std::string coeffName = coeff->GetName();
570+
RooAbsReal* shape = (RooAbsReal*)plist.at(i);
571+
std::unique_ptr<RooArgSet> myobs(shape->getObservables(*x_));
572+
TString normProdName = TString::Format("%s", coeff->GetName());
573+
RooAbsReal * normProd = nullptr;
574+
if (coeff->ownedComponents()) {
575+
normProd = dynamic_cast<RooAbsReal*>(coeff->ownedComponents()->find(normProdName));
576+
}
577+
if (!normProd) {
578+
RooAbsReal* integral = shape->createIntegral(*myobs);
579+
RooArgList normProdInputs;
580+
normProdInputs.add(*integral);
581+
normProdInputs.add(*coeff);
582+
normProd = new RooProduct(normProdName, "", normProdInputs);
583+
normProd->addOwnedComponents(normProdInputs);
584+
}
585+
vals_[normProdName.Data()] = normProd->getVal();
586+
}
587+
return vals_;
588+
}
552589
#undef HFVERBOSE
553590

test/printWorkspaceNormalisations.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,14 @@
4949
type="float",
5050
help="Only print values if yield is less than this threshold.",
5151
)
52-
parser.add_option(
53-
"",
54-
"--use-cms-histsum",
55-
dest="use_cms_histsum",
56-
default=False,
57-
action="store_true",
58-
help="Set to true if workspace built with --use-cms-histsum option.",
59-
)
52+
# parser.add_option(
53+
# "",
54+
# "--use-cms-histsum",
55+
# dest="use_cms_histsum",
56+
# default=False,
57+
# action="store_true",
58+
# help="Set to true if workspace built with --use-cms-histsum option.",
59+
# )
6060
parser.add_option(
6161
"",
6262
"--procFilter",
@@ -169,17 +169,24 @@ def find_chan_proc(name):
169169
else:
170170
chan_procs[chan] = [[proc, norm, 0, 0]]
171171

172+
use_cms_histsum = False
173+
174+
all_props = ws.allFunctions().selectByName("prop_bin*")
175+
if all_props.getSize() > 0:
176+
use_cms_histsum = True
177+
172178
# Look for cases where chan stored as CMSHistSum, set flag
173-
if options.use_cms_histsum:
179+
if use_cms_histsum:
174180
chan_CMSHistSum_norms = {}
175-
all_props = ws.allFunctions().selectByName("prop_bin*")
181+
# all_props = ws.allFunctions().selectByName("prop_bin*")
176182
for chan in chan_procs.keys():
177183
prop_it = all_props.createIterator()
178184
for i in range(all_props.getSize()):
179185
prop = prop_it.Next()
180186
prop_name = prop.GetName()
181187
if chan == prop_name.split("_bin")[-1]:
182-
if type(prop) == ROOT.CMSHistSum:
188+
types = [ROOT.CMSHistSum, ROOT.CMSHistErrorPropagator]
189+
if type(prop) in types:
183190
chan_CMSHistSum_norms[chan] = dict(prop.getProcessNorms())
184191

185192

@@ -243,7 +250,7 @@ def printEndExpand():
243250
continue
244251
print("<hr>")
245252
print("Top-level normalization for process {proc0} -> {proc1_name}<br>".format(proc0=proc[0], proc1_name=proc[1].GetName()))
246-
if options.use_cms_histsum:
253+
if use_cms_histsum:
247254
if chan in chan_CMSHistSum_norms:
248255
default_val = chan_CMSHistSum_norms[chan][proc[1].GetName()]
249256
else:
@@ -253,7 +260,7 @@ def printEndExpand():
253260
default_norms[chan][proc[1].GetName()] = default_val
254261

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

292-
print(" default value = ", default_val, "<br>")
299+
print(" default value = %.5f " % default_val, "<br>")
293300
print("</tr>")
294301

295302
print(
@@ -320,7 +327,7 @@ def printEndExpand():
320327
print("---------------------------------------------------------------------------")
321328
print(" Top-level normalization for process %s -> %s" % (proc[0], proc[1].GetName()))
322329
print("---------------------------------------------------------------------------")
323-
if options.use_cms_histsum:
330+
if use_cms_histsum:
324331
if chan in chan_CMSHistSum_norms:
325332
default_val = chan_CMSHistSum_norms[chan][proc[1].GetName()]
326333
else:
@@ -330,7 +337,7 @@ def printEndExpand():
330337
default_norms[chan][proc[1].GetName()] = default_val
331338

332339
if options.printValueOnly:
333-
print(" default value = ", default_val)
340+
print(" default value = %.5f " % default_val)
334341
# if options.printValueOnly: print " --xcp %s:%s "%(chan,proc[0]),
335342
else:
336343
if proc[2]:
@@ -354,7 +361,7 @@ def printEndExpand():
354361
proc[1].Print()
355362
print(" ... is a constant (RooRealVar)")
356363
print(" -------------------------------------------------------------------------")
357-
print(" default value = ", default_val)
364+
print(" default value = %.5f " % default_val)
358365

359366
# Save norms to json file
360367
if options.output_json != "":

0 commit comments

Comments
 (0)