Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooCustomizer.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * WV, Wouter Verkerke, UC Santa Barbara, verkerke@slac.stanford.edu *
7 * DK, David Kirkby, UC Irvine, dkirkby@uci.edu *
8 * *
9 * Copyright (c) 2000-2005, Regents of the University of California *
10 * and Stanford University. All rights reserved. *
11 * *
12 * Redistribution and use in source and binary forms, *
13 * with or without modification, are permitted according to the terms *
14 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
15 *****************************************************************************/
16
17/**
18 * \class RooCustomizer
19 *
20 * RooCustomizer is a factory class to produce clones
21 * of a prototype composite PDF object with the same structure but
22 * different leaf servers (parameters or dependents).
23 *
24 * RooCustomizer supports two kinds of modifications:
25 *
26 * - replaceArg(leaf_arg, repl_arg):
27 * Replaces each occurrence of leaf_arg with repl_arg in the composite pdf.
28 *
29 * - splitArg(split_arg):
30 * Build multiple clones of the same prototype. Each
31 * occurrence of split_arg is replaced with a clone of split_arg
32 * named split_arg_[MCstate], where [MCstate] is the name of the
33 * 'master category state' that indexes the clones to be built.
34 *
35 *
36 * ### Example: Change the decay constant of an exponential for each run
37 *
38 * Splitting is particularly useful when building simultaneous fits to
39 * subsets of the data sample with different background properties.
40 * In such a case, the user builds a single prototype PDF representing
41 * the structure of the signal and background and splits the dataset
42 * into categories with different background properties. Using
43 * RooCustomizer a PDF for each subfit can be constructed from the
44 * prototype that has same structure and signal parameters, but
45 * different instances of the background parameters: e.g.
46 * ```
47 * ...
48 * RooExponential bg("bg","background",x,alpha) ;
49 * RooGaussian sig("sig","signal",x,mean,sigma) ;
50 * RooAddPdf pdf("pdf","pdf",sig,bg,sigfrac) ;
51 *
52 * RooDataSet data("data","dataset",RooArgSet(x,runblock),...)
53 *
54 * RooCategory runblock("runblock","run block") ;
55 * runblock.defineType("run1") ;
56 * runblock.defineType("run2") ;
57 *
58 * RooArgSet splitLeaves;
59 * RooCustomizer cust(pdf,runblock,splitLeaves);
60 * cust.splitArg(alpha,runblock);
61 *
62 * RooAbsPdf* pdf_run1 = cust.build("run1") ;
63 * RooAbsPdf* pdf_run2 = cust.build("run2") ;
64 *
65 * RooSimultaneous simpdf("simpdf","simpdf",RooArgSet(*pdf_run1,*pdf_run2))
66 * ```
67 * If the master category state is a super category, leaves may be split
68 * by any subset of that master category. E.g. if the master category
69 * is 'A x B', leaves may be split by A, B or AxB.
70 *
71 * In addition to replacing leaf nodes, RooCustomizer clones all branch
72 * nodes that depend directly or indirectly on modified leaf nodes, so
73 * that the input pdf is untouched by each build operation.
74 *
75 * The customizer owns all the branch nodes including the returned top
76 * level node, so the customizer should live as longs as the cloned
77 * composites are needed.
78 *
79 * Any leaf nodes that are created by the customizer will be put into
80 * the leaf list that is passed into the customizers constructor (splitLeaves in
81 * the above example. The list owner is responsible for deleting these leaf
82 * nodes after the customizer is deleted.
83 *
84 *
85 * ## Advanced techniques
86 *
87 * ### Reuse nodes to customise a different PDF
88 * By default, the customizer clones the prototype leaf node when splitting a leaf,
89 * but the user can feed pre-defined split leaves in leaf list. These leaves
90 * must have the name `<split_leaf>_<splitcat_label>` to be picked up. The list
91 * of pre-supplied leaves may be partial, any missing split leaves will be auto
92 * generated.
93 *
94 * Another common construction is to have two prototype PDFs, each to be customized
95 * by a separate customizer instance, that share parameters. To ensure that
96 * the customized clones also share their respective split leaves, i.e.
97 * ```
98 * PDF1(x,y, A) and PDF2(z, A) ---> PDF1_run1(x,y, A_run1) and PDF2_run1(x,y, A_run1)
99 * PDF1_run2(x,y, A_run2) and PDF2_run2(x,y, A_run2)
100 * ```
101 * feed the same split leaf list into both customizers. In that case, the second customizer
102 * will pick up the split leaves instantiated by the first customizer and the link between
103 * the two PDFs is retained.
104 *
105 * ### Customising with pre-defined leaves
106 * If leaf nodes are provided in the sets, the customiser will use them. This is a complete
107 * example that customises the `yield` parameter, and splits (automatically clones) the
108 * mean of the Gaussian. This is a short version of the tutorial rf514_RooCustomizer.C.
109 * ```
110 * RooRealVar E("Energy","Energy",0,3000);
111 *
112 * RooRealVar meanG("meanG","meanG", peak[1]);
113 * RooRealVar fwhm("fwhm", "fwhm", 5/(2*Sqrt(2*Log(2))));
114 * RooGaussian gauss("gauss", "gauss", E, meanG, fwhm);
115 *
116 * RooPolynomial linear("linear","linear",E,RooArgList());
117 *
118 * RooRealVar yieldSig("yieldSig", "yieldSig", 1, 0, 1.E4);
119 * RooRealVar yieldBkg("yieldBkg", "yieldBkg", 1, 0, 1.E4);
120 *
121 * RooAddPdf model("model","model",
122 * RooArgList(gauss,linear),
123 * RooArgList(yieldSig, yieldBkg));
124 *
125 * RooCategory sample("sample","sample");
126 * sample.defineType("BBG1m2T");
127 * sample.defineType("BBG2m2T");
128 *
129 *
130 * RooArgSet customisedLeaves;
131 * RooArgSet allLeaves;
132 *
133 * RooRealVar mass("M", "M", 1, 0, 12000);
134 * RooFormulaVar yield1("yieldSig_BBG1m2T","sigy1","M/3.360779",mass);
135 * RooFormulaVar yield2("yieldSig_BBG2m2T","sigy2","M/2",mass);
136 * allLeaves.add(yield1);
137 * allLeaves.add(yield2);
138 *
139 *
140 * RooCustomizer cust(model, sample, customisedLeaves, &allLeaves);
141 * cust.splitArg(yieldSig, sample);
142 * cust.splitArg(meanG, sample);
143 *
144 * auto pdf1 = cust.build("BBG1m2T");
145 * auto pdf2 = cust.build("BBG2m2T");
146 * ```
147*/
148
149
150#include "RooCustomizer.h"
151
152#include "RooFactoryWSTool.h"
153#include "RooAbsCategoryLValue.h"
154#include "RooAbsCategory.h"
155#include "RooAbsArg.h"
156#include "RooAbsPdf.h"
157#include "RooMsgService.h"
158#include "RooHelpers.h"
159
160#include <iostream>
161#include "strtok.h"
162#include "strlcpy.h"
163
164#include "RooWorkspace.h"
165#include "RooGlobalFunc.h"
166#include "RooConstVar.h"
167#include "RooRealConstant.h"
168
169
170#ifndef _WIN32
171#include <strings.h>
172#endif
173
174
175using std::endl, std::ostream, std::string;
176
177
178namespace {
179
180/// Factory interface
181class CustIFace : public RooFactoryWSTool::IFace {
182public:
183 std::string create(RooFactoryWSTool& ft, const char* typeName, const char* instanceName, std::vector<std::string> args) override ;
184} ;
185
186
187static Int_t init();
188
189Int_t dummy = init() ;
190
191Int_t init()
192{
193 RooFactoryWSTool::IFace* iface = new CustIFace ;
195 (void) dummy;
196 return 0 ;
197}
198
199}
200
201////////////////////////////////////////////////////////////////////////////////
202/// Constructor with a prototype and masterCat index category.
203/// Customizers created by this constructor offer both the
204/// replaceArg() and splitArg() functionality.
205/// \param[in] pdf Proto PDF to be customised.
206/// \param[in] masterCat Category to be used for splitting.
207/// \param[in,out] splitLeaves All nodes created in
208/// the customisation process are added to this set.
209/// The user can provide nodes that are *taken*
210/// from the set if they have a name that matches `<parameterNameToBeReplaced>_<category>`.
211/// \note The set needs to own its contents if they are user-provided.
212/// Use *e.g.*
213/// ```
214/// RooArgSet customisedLeaves;
215/// auto yield1 = new RooFormulaVar("yieldSig_BBG1m2T","sigy1","M/3.360779",mass);
216/// customisedLeaves.addOwned(*yield1);
217/// ```
218/// \param[in,out] splitLeavesAll All leaves that are used when customising are collected here.
219/// If this set already contains leaves, they will be used for customising if the names match
220/// as above.
221///
222
223RooCustomizer::RooCustomizer(const RooAbsArg &pdf, const RooAbsCategoryLValue &masterCat, RooArgSet &splitLeaves,
224 RooArgSet *splitLeavesAll)
225 : _sterile(false),
226 _owning(true),
227 _masterPdf(const_cast<RooAbsArg *>(&pdf)),
228 _masterCat(const_cast<RooAbsCategoryLValue *>(&masterCat)),
229 _masterBranchList("masterBranchList"),
230 _masterLeafList("masterLeafList"),
231 _internalCloneBranchList("cloneBranchList"),
232 _cloneBranchList(&_internalCloneBranchList),
233 _cloneNodeListAll(splitLeavesAll),
234 _cloneNodeListOwned(&splitLeaves)
235{
236
237 initialize() ;
238}
239
240
241
242////////////////////////////////////////////////////////////////////////////////
243/// Sterile Constructor. Customizers created by this constructor
244/// offer only the replace() method. The supplied 'name' is used as
245/// suffix for any cloned branch nodes
246
248 : _sterile(true),
249 _owning(false),
250 _name(name),
251 _masterPdf(const_cast<RooAbsArg *>(&pdf)),
252 _masterBranchList("masterBranchList"),
253 _masterLeafList("masterLeafList"),
254 _internalCloneBranchList("cloneBranchList"),
255 _cloneBranchList(&_internalCloneBranchList)
256{
257
258 initialize() ;
259}
260
261
262
263
264////////////////////////////////////////////////////////////////////////////////
265/// Initialize the customizer
266
268{
271}
272
273////////////////////////////////////////////////////////////////////////////////
274/// Split all arguments in 'set' into individualized clones for each
275/// defined state of 'splitCat'. The 'splitCats' category must be
276/// subset of or equal to the master category supplied in the
277/// customizer constructor.
278///
279/// Splitting is only available on customizers created with a master index category
280
281void RooCustomizer::splitArgs(const RooArgSet& set, const RooAbsCategory& splitCat)
282{
283 if (_sterile) {
284 oocoutE(nullptr, InputArguments) << "RooCustomizer::splitArgs(" << _name
285 << ") ERROR cannot set spitting rules on this sterile customizer" << endl ;
286 return ;
287 }
288
289 for (auto arg : set) {
290 splitArg(*arg,splitCat) ;
291 }
292}
293
294
295
296////////////////////////////////////////////////////////////////////////////////
297/// Split all argument 'arg' into individualized clones for each
298/// defined state of 'splitCat'. The 'splitCats' category must be
299/// subset of or equal to the master category supplied in the
300/// customizer constructor.
301///
302/// Splitting is only available on customizers created with a master index category
303
304void RooCustomizer::splitArg(const RooAbsArg& arg, const RooAbsCategory& splitCat)
305{
306 if (_splitArgList.find(arg.GetName())) {
307 oocoutE(nullptr, InputArguments) << "RooCustomizer(" << _masterPdf->GetName() << ") ERROR: multiple splitting rules defined for "
308 << arg.GetName() << " only using first rule" << endl ;
309 return ;
310 }
311
312 if (_sterile) {
313 oocoutE(nullptr, InputArguments) << "RooCustomizer::splitArg(" << _name
314 << ") ERROR cannot set spitting rules on this sterile customizer" << endl ;
315 return ;
316 }
317
318 _splitArgList.add(arg);
319 _splitCatList.add(splitCat);
320}
321
322
323
324////////////////////////////////////////////////////////////////////////////////
325/// Replace any occurrence of arg 'orig' with arg 'subst'
326
327void RooCustomizer::replaceArg(const RooAbsArg& orig, const RooAbsArg& subst)
328{
329 if (_replaceArgList.find(orig.GetName())) {
330 oocoutE(nullptr, InputArguments) << "RooCustomizer(" << _masterPdf->GetName() << ") ERROR: multiple replacement rules defined for "
331 << orig.GetName() << " only using first rule" << endl ;
332 return ;
333 }
334
335 _replaceArgList.add(orig);
336 _replaceSubList.add(subst);
337}
338
339
340
341////////////////////////////////////////////////////////////////////////////////
342/// Build a clone of the prototype executing all registered 'replace' rules.
343/// If verbose is set, a message is printed for each leaf or branch node
344/// modification. The returned head node owns all cloned branch nodes
345/// that were created in the cloning process.
346
348{
349 // Execute build
350 RooAbsArg* ret = doBuild(_name.Length()>0?_name.Data():nullptr,verbose) ;
351
352 // Make root object own all cloned nodes
353
354 // First make list of all objects that were created
355 RooArgSet allOwned ;
357 allOwned.add(*_cloneNodeListOwned) ;
358 }
359 allOwned.add(*_cloneBranchList) ;
360
361 // Remove head node from list
362 allOwned.remove(*ret) ;
363
364 // If list with owned objects is not empty, assign
365 // head node as owner
366 if (!allOwned.empty()) {
367 ret->addOwnedComponents(allOwned) ;
368 }
369
370 return ret ;
371}
372
373
374
375////////////////////////////////////////////////////////////////////////////////
376/// Build a clone of the prototype executing all registered 'replace'
377/// rules and 'split' rules for the masterCat state named
378/// 'masterCatState'. If verbose is set a message is printed for
379/// each leaf or branch node modification. The returned composite arg
380/// is owned by the customizer. This function cannot be called on
381/// customizer build with the sterile constructor.
382
383RooAbsArg* RooCustomizer::build(const char* masterCatState, bool verbose)
384{
385 if (_sterile) {
386 oocoutE(nullptr, InputArguments) << "RooCustomizer::build(" << _name
387 << ") ERROR cannot use leaf spitting build() on this sterile customizer" << endl ;
388 return nullptr ;
389 }
390
391 // Set masterCat to given state
392 if (_masterCat->setLabel(masterCatState)) {
393 oocoutE(nullptr, InputArguments) << "RooCustomizer::build(" << _masterPdf->GetName() << "): ERROR label '" << masterCatState
394 << "' not defined for master splitting category " << _masterCat->GetName() << endl ;
395 return nullptr ;
396 }
397
398 return doBuild(masterCatState,verbose) ;
399}
400
401
402
403////////////////////////////////////////////////////////////////////////////////
404/// Back-end implementation of the p.d.f building functionality
405
406RooAbsArg* RooCustomizer::doBuild(const char* masterCatState, bool verbose)
407{
408 // Find nodes that must be split according to provided description, Clone nodes, change their names
409 RooArgSet masterNodesToBeSplit("masterNodesToBeSplit") ;
410 RooArgSet masterNodesToBeReplaced("masterNodesToBeReplaced") ;
411 RooArgSet masterReplacementNodes("masterReplacementNodes") ;
412 RooArgSet clonedMasterNodes("clonedMasterNodes") ;
413
414
415 RooArgSet nodeList(_masterLeafList) ;
416 nodeList.add(_masterBranchList) ;
417
418 // cout << "loop over " << nodeList.size() << " nodes" << endl ;
419 for (auto node : nodeList) {
420 RooAbsArg* theSplitArg = !_sterile?static_cast<RooAbsArg*>(_splitArgList.find(node->GetName())):nullptr ;
421 if (theSplitArg) {
422 RooAbsCategory* splitCat = static_cast<RooAbsCategory*>(_splitCatList.at(_splitArgList.index(theSplitArg))) ;
423 if (verbose) {
424 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
425 << "): tree node " << node->GetName() << " is split by category " << splitCat->GetName() << endl ;
426 }
427
428 TString newName(node->GetName()) ;
429 if (masterCatState) {
430 newName.Append("_") ;
431 newName.Append(splitCat->getCurrentLabel()) ;
432 }
433
434 // Check if this node instance already exists
435 RooAbsArg* specNode = _cloneNodeListAll ? _cloneNodeListAll->find(newName) : _cloneNodeListOwned->find(newName) ;
436 if (specNode) {
437
438 // Copy instance to one-time use list for this build
439 clonedMasterNodes.add(*specNode) ;
440 if (verbose) {
441 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
442 << ") Adding existing node specialization " << newName << " to clonedMasterNodes" << endl ;
443 }
444
445 // Affix attribute with old name to clone to support name changing server redirect
446 TString nameAttrib("ORIGNAME:") ;
447 nameAttrib.Append(node->GetName()) ;
448 specNode->setAttribute(nameAttrib) ;
449
450 if (!specNode->getStringAttribute("origName")) {
451 specNode->setStringAttribute("origName",node->GetName()) ;
452 }
453
454
455
456 } else {
457
458 if (node->isDerived()) {
459 oocoutW(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
460 << "): WARNING: branch node " << node->GetName() << " is split but has no pre-defined specializations" << endl ;
461 }
462
463 TString newTitle(node->GetTitle()) ;
464 newTitle.Append(" (") ;
465 newTitle.Append(splitCat->getCurrentLabel()) ;
466 newTitle.Append(")") ;
467
468 // Create a new clone
469 RooAbsArg* clone = static_cast<RooAbsArg*>(node->Clone(newName.Data())) ;
470 clone->removeStringAttribute("factory_tag") ;
471 clone->SetTitle(newTitle) ;
472
473 // Affix attribute with old name to clone to support name changing server redirect
474 TString nameAttrib("ORIGNAME:") ;
475 nameAttrib.Append(node->GetName()) ;
476 clone->setAttribute(nameAttrib) ;
477
478 if (!clone->getStringAttribute("origName")) {
479 clone->setStringAttribute("origName",node->GetName()) ;
480 }
481
482 // Add to one-time use list and life-time use list
483 clonedMasterNodes.add(*clone) ;
484 if (_owning) {
485 _cloneNodeListOwned->addOwned(std::unique_ptr<RooAbsArg>{clone});
486 } else {
488 }
489 if (_cloneNodeListAll) {
491 }
492 }
493 masterNodesToBeSplit.add(*node) ;
494 }
495
496 RooAbsArg* ReplaceArg = static_cast<RooAbsArg*>(_replaceArgList.find(node->GetName())) ;
497 if (ReplaceArg) {
498 RooAbsArg* substArg = static_cast<RooAbsArg*>(_replaceSubList.at(_replaceArgList.index(ReplaceArg))) ;
499 if (verbose) {
500 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
501 << "): tree node " << node->GetName() << " will be replaced by " << substArg->GetName() << endl ;
502 }
503
504 // Affix attribute with old name to support name changing server redirect
505 TString nameAttrib("ORIGNAME:") ;
506 nameAttrib.Append(node->GetName()) ;
507 substArg->setAttribute(nameAttrib) ;
508
509 // Add to list
510 masterNodesToBeReplaced.add(*node) ;
511 masterReplacementNodes.add(*substArg) ;
512 }
513 }
514
515 // Find branches that are affected by splitting and must be cloned
516 RooArgSet masterBranchesToBeCloned("masterBranchesToBeCloned") ;
517 for (auto branch : _masterBranchList) {
518
519 // If branch is split itself, don't handle here
520 if (masterNodesToBeSplit.find(branch->GetName())) {
521 if (verbose) {
522 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node " << branch->GetName() << " is already split" << endl ;
523 }
524 continue ;
525 }
526 if (masterNodesToBeReplaced.find(branch->GetName())) {
527 if (verbose) {
528 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node " << branch->GetName() << " is already replaced" << endl ;
529 }
530 continue ;
531 }
532
533 if (branch->dependsOn(masterNodesToBeSplit)) {
534 if (verbose) {
535 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node "
536 << branch->ClassName() << "::" << branch->GetName() << " cloned: depends on a split parameter" << endl ;
537 }
538 masterBranchesToBeCloned.add(*branch) ;
539 } else if (branch->dependsOn(masterNodesToBeReplaced)) {
540 if (verbose) {
541 oocoutI(nullptr, ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node "
542 << branch->ClassName() << "::" << branch->GetName() << " cloned: depends on a replaced parameter" << endl ;
543 }
544 masterBranchesToBeCloned.add(*branch) ;
545 }
546 }
547
548 // Clone branches, changes their names
549 RooAbsArg* cloneTopPdf = nullptr;
550 RooArgSet clonedMasterBranches("clonedMasterBranches") ;
551
552 for (auto branch : masterBranchesToBeCloned) {
553 TString newName(branch->GetName()) ;
554 if (masterCatState) {
555 newName.Append("_") ;
556 newName.Append(masterCatState) ;
557 }
558
559 // Affix attribute with old name to clone to support name changing server redirect
560 RooAbsArg* clone = static_cast<RooAbsArg*>(branch->Clone(newName.Data())) ;
561 clone->removeStringAttribute("factory_tag") ;
562 TString nameAttrib("ORIGNAME:") ;
563 nameAttrib.Append(branch->GetName()) ;
564 clone->setAttribute(nameAttrib) ;
565
566 if (!clone->getStringAttribute("origName")) {
567 clone->setStringAttribute("origName",branch->GetName()) ;
568 }
569
570 clonedMasterBranches.add(*clone) ;
571
572 // Save pointer to clone of top-level pdf
573 if (branch==_masterPdf) cloneTopPdf=(RooAbsArg*)clone ;
574 }
575
576 if (_owning) {
577 _cloneBranchList->addOwned(clonedMasterBranches) ;
578 } else {
579 _cloneBranchList->add(clonedMasterBranches) ;
580 }
581
582 // Reconnect cloned branches to each other and to cloned nodes
583 for (auto branch : clonedMasterBranches) {
584 branch->redirectServers(clonedMasterBranches,false,true) ;
585 branch->redirectServers(clonedMasterNodes,false,true) ;
586 branch->redirectServers(masterReplacementNodes,false,true) ;
587 }
588
589 return cloneTopPdf ? cloneTopPdf : _masterPdf ;
590}
591
592
593////////////////////////////////////////////////////////////////////////////////
594/// Print arguments of customizer, i.e. input p.d.f and input master category (if any)
595
596void RooCustomizer::printArgs(ostream& os) const
597{
598 os << "[ masterPdf=" << _masterPdf->GetName() ;
599 if (_masterCat) {
600 os << " masterCat=" << _masterCat->GetName() ;
601 }
602 os << " ]" ;
603}
604
605
606
607////////////////////////////////////////////////////////////////////////////////
608/// Print customizer configuration details
609
610void RooCustomizer::printMultiline(ostream& os, Int_t /*content*/, bool /*verbose*/, TString indent) const
611{
612 os << indent << "RooCustomizer for " << _masterPdf->GetName() << (_sterile?" (sterile)":"") << endl ;
613
614 Int_t i;
615 Int_t nsplit = _splitArgList.size();
616 if (nsplit>0) {
617 os << indent << " Splitting rules:" << endl ;
618 for (i=0 ; i<nsplit ; i++) {
619 os << indent << " " << _splitArgList.at(i)->GetName() << " is split by " << _splitCatList.at(i)->GetName() << endl ;
620 }
621 }
622
623 Int_t nrepl = _replaceArgList.size() ;
624 if (nrepl>0) {
625 os << indent << " Replacement rules:" << endl ;
626 for (i=0 ; i<nrepl ; i++) {
627 os << indent << " " << _replaceSubList.at(i)->GetName() << " replaces " << _replaceArgList.at(i)->GetName() << endl ;
628 }
629 }
630
631 return ;
632}
633
634
635
636////////////////////////////////////////////////////////////////////////////////
637/// Install the input RooArgSet as container in which all cloned branches
638/// will be stored
639
641{
642 _cloneBranchList = &cloneBranchSet ;
644}
645
646
648 return static_cast<RooAbsPdf&>(*_masterPdf);
649}
650
651
652namespace {
653
654////////////////////////////////////////////////////////////////////////////////
655
656std::string CustIFace::create(RooFactoryWSTool& ft, const char* typeName, const char* instanceName, std::vector<std::string> args)
657{
658 // Check number of arguments
659 if (args.size()<2) {
660 throw string(Form("RooCustomizer::CustIFace::create() ERROR: expect at least 2 arguments for EDIT: the input object and at least one $Replace() rule")) ;
661 }
662
663 if (string(typeName)!="EDIT") {
664 throw string(Form("RooCustomizer::CustIFace::create() ERROR: unknown type requested: %s",typeName)) ;
665 }
666
667 // Check that first arg exists as RooAbsArg
668 RooAbsArg* arg = ft.ws().arg(args[0]) ;
669 if (!arg) {
670 throw string(Form("RooCustomizer::CustIFace::create() ERROR: input RooAbsArg %s does not exist",args[0].c_str())) ;
671 }
672
673 // If name of new object is same as original, execute in sterile mode (i.e no suffixes attached), and rename original nodes in workspace upon import
674 if (args[0]==instanceName) {
675 instanceName=nullptr ;
676 }
677
678 // Create a customizer
679 RooCustomizer cust(*arg,instanceName) ;
680
681 for (unsigned int i=1 ; i<args.size() ; i++) {
682 char buf[1024] ;
683 strlcpy(buf,args[i].c_str(),1024) ;
684 char* sep = strchr(buf,'=') ;
685 if (!sep) {
686 throw string(Form("RooCustomizer::CustIFace::create() ERROR: unknown argument: %s, expect form orig=subst",args[i].c_str())) ;
687 }
688 *sep = 0 ;
689 RooAbsArg* orig = ft.ws().arg(buf) ;
690 RooAbsArg* subst(nullptr) ;
691 if (string(sep+1).find("$REMOVE")==0) {
692
693 // Create a removal dummy ;
695
696 // If removal instructed was annotated with target node, encode these in removal dummy
697 char* sep2 = strchr(sep+1,'(') ;
698 if (sep2) {
699 char buf2[1024] ;
700 strlcpy(buf2,sep2+1,1024) ;
701 char* saveptr ;
702 char* tok = R__STRTOK_R(buf2,",)",&saveptr) ;
703 while(tok) {
704 //cout << "$REMOVE is restricted to " << tok << endl ;
705 subst->setAttribute(Form("REMOVE_FROM_%s",tok)) ;
706 tok = R__STRTOK_R(nullptr,",)",&saveptr) ;
707 }
708 } else {
709 // Otherwise mark as universal removal node
710 subst->setAttribute("REMOVE_ALL") ;
711 }
712
713 } else {
714 subst = ft.ws().arg(sep+1) ;
715 }
716// if (!orig) {
717// throw string(Form("RooCustomizer::CustIFace::create() ERROR: $Replace() input RooAbsArg %s does not exist",buf)) ;
718// }
719// if (!subst) {
720// throw string(Form("RooCustomizer::CustIFace::create() ERROR: $Replace() replacement RooAbsArg %s does not exist",sep+1)) ;
721// }
722 if (orig && subst) {
723 cust.replaceArg(*orig,*subst) ;
724 } else {
725 oocoutW(nullptr,ObjectHandling) << "RooCustomizer::CustIFace::create() WARNING: input or replacement of a replacement operation not found, operation ignored"<< endl ;
726 }
727 }
728
729 // Build the desired edited object
730 RooAbsArg* targ = cust.build(false) ;
731 if (!targ) {
732 throw string(Form("RooCustomizer::CustIFace::create() ERROR in customizer build, object %snot created",instanceName)) ;
733 }
734
735 // Import the object into the workspace
736 if (instanceName) {
737 // Set the desired name of the top level node
738 targ->SetName(instanceName) ;
739 // Now import everything. What we didn't touch gets recycled, everything else was cloned here:
740 ft.ws().import(cust.cloneBranchList(), RooFit::Silence(true), RooFit::RecycleConflictNodes(true), RooFit::NoRecursion(false));
741 } else {
742 ft.ws().import(cust.cloneBranchList(), RooFit::Silence(true), RooFit::RenameConflictNodes("orig",true), RooFit::NoRecursion(true));
743 }
744
745 return string(instanceName?instanceName:targ->GetName()) ;
746}
747
748} // namespace
TObject * clone(const char *newname) const override
Definition RooChi2Var.h:9
#define oocoutW(o, a)
#define oocoutE(o, a)
#define oocoutI(o, a)
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2489
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
void SetName(const char *name) override
Set the name of the TNamed.
bool addOwnedComponents(const RooAbsCollection &comps)
Take ownership of the contents of 'comps'.
const Text_t * getStringAttribute(const Text_t *key) const
Get string attribute mapped under key 'key'.
void setAttribute(const Text_t *name, bool value=true)
Set (default) or clear a named boolean attribute of this object.
void branchNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool recurseNonDerived=false) const
Fill supplied list with all branch nodes of the arg tree starting with ourself as top node.
void leafNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=nullptr, bool recurseNonDerived=false) const
Fill supplied list with all leaf nodes of the arg tree, starting with ourself as top node.
Abstract base class for objects that represent a discrete value that can be set from the outside,...
virtual bool setLabel(const char *label, bool printError=true)=0
Change category state by specifying a state name.
A space to attach TBranches.
virtual const char * getCurrentLabel() const
Return label string of current state.
virtual bool add(const RooAbsArg &var, bool silent=false)
Add the specified argument to list.
Int_t index(const RooAbsArg *arg) const
Returns index of given arg, or -1 if arg is not in the collection.
Storage_t::size_type size() const
virtual bool addOwned(RooAbsArg &var, bool silent=false)
Add an argument and transfer the ownership to the collection.
void useHashMapForFind(bool flag) const
RooAbsArg * find(const char *name) const
Find object with given name in list.
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
RooAbsArg * at(Int_t idx) const
Return object at given index, or nullptr if index is out of range.
Definition RooArgList.h:110
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
TString _name
Name of this object.
RooArgList _replaceSubList
List of replacement RooAbsArgs.
void splitArg(const RooAbsArg &arg, const RooAbsCategory &splitCat)
Split all argument 'arg' into individualized clones for each defined state of 'splitCat'.
void setCloneBranchSet(RooArgSet &cloneBranchSet)
Releases ownership of list of cloned branch nodes.
RooArgSet * _cloneNodeListAll
List of all cloned nodes.
void replaceArg(const RooAbsArg &orig, const RooAbsArg &subst)
Replace any occurrence of arg 'orig' with arg 'subst'.
RooAbsArg * _masterPdf
Pointer to input p.d.f.
bool _owning
If true we own all created components.
RooAbsArg * build(const char *masterCatState, bool verbose=false)
Build a clone of the prototype executing all registered 'replace' rules and 'split' rules for the mas...
RooArgList _splitArgList
List of RooAbsArgs to be split.
RooArgList _splitCatList
List of categories to be used for above splits.
RooArgSet * _cloneBranchList
Pointer to list of cloned branches used.
RooCustomizer(const RooAbsArg &pdf, const RooAbsCategoryLValue &masterCat, RooArgSet &splitLeafListOwned, RooArgSet *splitLeafListAll=nullptr)
Constructor with a prototype and masterCat index category.
RooArgSet _masterBranchList
List of branch nodes.
RooArgSet _masterLeafList
List of leaf nodes.
void initialize()
Initialize the customizer.
RooAbsCategoryLValue * _masterCat
Pointer to input master category.
RooArgList _replaceArgList
List of RooAbsArgs to be replaced.
void splitArgs(const RooArgSet &argSet, const RooAbsCategory &splitCat)
Split all arguments in 'set' into individualized clones for each defined state of 'splitCat'.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const
Print customizer configuration details.
RooAbsArg * doBuild(const char *masterCatState, bool verbose)
Back-end implementation of the p.d.f building functionality.
bool _sterile
If true we do not have as associated master category.
RooAbsPdf const & pdf() const
RooArgSet * _cloneNodeListOwned
List of owned cloned nodes.
void printArgs(std::ostream &os) const
Print arguments of customizer, i.e. input p.d.f and input master category (if any)
virtual std::string create(RooFactoryWSTool &ft, const char *typeName, const char *instanceName, std::vector< std::string > args)=0
Implementation detail of the RooWorkspace.
RooWorkspace & ws()
static void registerSpecial(const char *typeName, RooFactoryWSTool::IFace *iface)
Register foreign special objects in factory.
static RooConstVar & removalDummy()
Create a dummy node used in node-removal operations.
RooAbsArg * arg(RooStringView name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
bool import(const RooAbsArg &arg, const RooCmdArg &arg1={}, const RooCmdArg &arg2={}, const RooCmdArg &arg3={}, const RooCmdArg &arg4={}, const RooCmdArg &arg5={}, const RooCmdArg &arg6={}, const RooCmdArg &arg7={}, const RooCmdArg &arg8={}, const RooCmdArg &arg9={})
Import a RooAbsArg object, e.g.
const char * GetName() const override
Returns name of object.
Definition TNamed.h:47
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
const char * Data() const
Definition TString.h:376
TString & Append(const char *cs)
Definition TString.h:572
RooCmdArg RecycleConflictNodes(bool flag=true)
RooCmdArg Silence(bool flag=true)
RooCmdArg NoRecursion(bool flag=true)
RooCmdArg RenameConflictNodes(const char *suffix, bool renameOrigNodes=false)
void(off) SmallVectorTemplateBase< T