Logo ROOT  
Reference Guide
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 occurence 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 splitLeafs;
59 * RooCustomizer cust(pdf,runblock,splitLeafs);
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, leafs may be split
68 * by any subset of that master category. E.g. if the master category
69 * is 'A x B', leafs 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 (splitLeafs 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 leafs in leaf list. These leafs
90 * must have the name <split_leaf>_<splitcat_label> to be picked up. The list
91 * of pre-supplied leafs may be partial, any missing split leafs 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 leafs, 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 leafs instantiated by the first customizer and the link between
103 * the two PDFs is retained.
104 *
105 * ### Customising with pre-defined leafs
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 customisedLeafs;
131 * RooArgSet allLeafs;
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 * allLeafs.add(yield1);
137 * allLeafs.add(yield2);
138 *
139 *
140 * RooCustomizer cust(model, sample, customisedLeafs, &allLeafs);
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 "RooAbsCategoryLValue.h"
153#include "RooAbsCategory.h"
154#include "RooAbsArg.h"
155#include "RooAbsPdf.h"
156#include "RooArgSet.h"
157#include "RooArgList.h"
158#include "RooMsgService.h"
159#include "RooHelpers.h"
160
161#include "Riostream.h"
162#include "RooWorkspace.h"
163#include "RooGlobalFunc.h"
164#include "RooConstVar.h"
165#include "RooRealConstant.h"
166
167
168#ifndef _WIN32
169#include <strings.h>
170#endif
171
172
173using namespace std;
174
176;
177
178
179static Int_t init();
180
181static Int_t dummy = init() ;
182
183static Int_t init()
184{
187 (void) dummy;
188 return 0 ;
189}
190
191
192////////////////////////////////////////////////////////////////////////////////
193/// Constructor with a prototype and masterCat index category.
194/// Customizers created by this constructor offer both the
195/// replaceArg() and splitArg() functionality.
196/// \param[in] pdf Proto PDF to be customised.
197/// \param[in] masterCat Category to be used for splitting.
198/// \param[in/out] splitLeafs All nodes created in
199/// the customisation process are added to this set.
200/// The user can provide nodes that are *taken*
201/// from the set if they have a name that matches `<parameterNameToBeReplaced>_<category>`.
202/// \note The set needs to own its contents if they are user-provided.
203/// Use *e.g.*
204/// ```
205/// RooArgSet customisedLeafs;
206/// auto yield1 = new RooFormulaVar("yieldSig_BBG1m2T","sigy1","M/3.360779",mass);
207/// customisedLeafs.addOwned(*yield1);
208/// ```
209/// \param[in/out] splitLeafsAll All leafs that are used when customising are collected here.
210/// If this set already contains leaves, they will be used for customising if the names match
211/// as above.
212///
213
215 RooArgSet& splitLeafs, RooArgSet* splitLeafsAll) :
216 TNamed(pdf.GetName(),pdf.GetTitle()),
217 _sterile(kFALSE),
218 _owning(kTRUE),
219 _masterPdf((RooAbsArg*)&pdf),
220 _masterCat((RooAbsCategoryLValue*)&masterCat),
221 _masterBranchList("masterBranchList"),
222 _masterLeafList("masterLeafList"),
223 _internalCloneBranchList("cloneBranchList"),
224 _cloneNodeListAll(splitLeafsAll),
225 _cloneNodeListOwned(&splitLeafs)
226{
228
229 initialize() ;
230}
231
232
233
234////////////////////////////////////////////////////////////////////////////////
235/// Sterile Constructor. Customizers created by this constructor
236/// offer only the replace() method. The supplied 'name' is used as
237/// suffix for any cloned branch nodes
238
240 TNamed(pdf.GetName(),pdf.GetTitle()),
241 _sterile(kTRUE),
242 _owning(kFALSE),
243 _name(name),
244 _masterPdf((RooAbsArg*)&pdf),
245 _masterCat(0),
246 _masterBranchList("masterBranchList"),
247 _masterLeafList("masterLeafList"),
248 _internalCloneBranchList("cloneBranchList"),
249 _cloneNodeListAll(0),
250 _cloneNodeListOwned(0)
251{
253
254 initialize() ;
255}
256
257
258
259
260////////////////////////////////////////////////////////////////////////////////
261/// Initialize the customizer
262
264{
267}
268
269
270
271////////////////////////////////////////////////////////////////////////////////
272/// Destructor
273
275{
276
277}
278
279
280
281
282////////////////////////////////////////////////////////////////////////////////
283/// Split all arguments in 'set' into individualized clones for each
284/// defined state of 'splitCat'. The 'splitCats' category must be
285/// subset of or equal to the master category supplied in the
286/// customizer constructor.
287///
288/// Splitting is only available on customizers created with a master index category
289
290void RooCustomizer::splitArgs(const RooArgSet& set, const RooAbsCategory& splitCat)
291{
292 if (_sterile) {
293 coutE(InputArguments) << "RooCustomizer::splitArgs(" << _name
294 << ") ERROR cannot set spitting rules on this sterile customizer" << endl ;
295 return ;
296 }
297
298 for (auto arg : set) {
299 splitArg(*arg,splitCat) ;
300 }
301}
302
303
304
305////////////////////////////////////////////////////////////////////////////////
306/// Split all argument 'arg' into individualized clones for each
307/// defined state of 'splitCat'. The 'splitCats' category must be
308/// subset of or equal to the master category supplied in the
309/// customizer constructor.
310///
311/// Splitting is only available on customizers created with a master index category
312
313void RooCustomizer::splitArg(const RooAbsArg& arg, const RooAbsCategory& splitCat)
314{
315 if (_splitArgList.FindObject(arg.GetName())) {
316 coutE(InputArguments) << "RooCustomizer(" << GetName() << ") ERROR: multiple splitting rules defined for "
317 << arg.GetName() << " only using first rule" << endl ;
318 return ;
319 }
320
321 if (_sterile) {
322 coutE(InputArguments) << "RooCustomizer::splitArg(" << _name
323 << ") ERROR cannot set spitting rules on this sterile customizer" << endl ;
324 return ;
325 }
326
327 _splitArgList.Add((RooAbsArg*)&arg) ;
328 _splitCatList.Add((RooAbsCategory*)&splitCat) ;
329}
330
331
332
333////////////////////////////////////////////////////////////////////////////////
334/// Replace any occurence of arg 'orig' with arg 'subst'
335
336void RooCustomizer::replaceArg(const RooAbsArg& orig, const RooAbsArg& subst)
337{
338 if (_replaceArgList.FindObject(orig.GetName())) {
339 coutE(InputArguments) << "RooCustomizer(" << GetName() << ") ERROR: multiple replacement rules defined for "
340 << orig.GetName() << " only using first rule" << endl ;
341 return ;
342 }
343
344 _replaceArgList.Add((RooAbsArg*)&orig) ;
345 _replaceSubList.Add((RooAbsArg*)&subst) ;
346}
347
348
349
350////////////////////////////////////////////////////////////////////////////////
351/// Build a clone of the prototype executing all registered 'replace' rules.
352/// If verbose is set, a message is printed for each leaf or branch node
353/// modification. The returned head node owns all cloned branch nodes
354/// that were created in the cloning process.
355
357{
358 // Execute build
359 RooAbsArg* ret = doBuild(_name.Length()>0?_name.Data():0,verbose) ;
360
361 // Make root object own all cloned nodes
362
363 // First make list of all objects that were created
364 RooArgSet allOwned ;
366 allOwned.add(*_cloneNodeListOwned) ;
367 }
368 allOwned.add(*_cloneBranchList) ;
369
370 // Remove head node from list
371 allOwned.remove(*ret) ;
372
373 // If list with owned objects is not empty, assign
374 // head node as owner
375 if (allOwned.getSize()>0) {
376 ret->addOwnedComponents(allOwned) ;
377 }
378
379 return ret ;
380}
381
382
383
384////////////////////////////////////////////////////////////////////////////////
385/// Build a clone of the prototype executing all registered 'replace'
386/// rules and 'split' rules for the masterCat state named
387/// 'masterCatState'. If verbose is set a message is printed for
388/// each leaf or branch node modification. The returned composite arg
389/// is owned by the customizer. This function cannot be called on
390/// customizer build with the sterile constructor.
391
392RooAbsArg* RooCustomizer::build(const char* masterCatState, Bool_t verbose)
393{
394 if (_sterile) {
395 coutE(InputArguments) << "RooCustomizer::build(" << _name
396 << ") ERROR cannot use leaf spitting build() on this sterile customizer" << endl ;
397 return 0 ;
398 }
399
400 // Set masterCat to given state
401 if (_masterCat->setLabel(masterCatState)) {
402 coutE(InputArguments) << "RooCustomizer::build(" << _masterPdf->GetName() << "): ERROR label '" << masterCatState
403 << "' not defined for master splitting category " << _masterCat->GetName() << endl ;
404 return 0 ;
405 }
406
407 return doBuild(masterCatState,verbose) ;
408}
409
410
411
412////////////////////////////////////////////////////////////////////////////////
413/// Back-end implementation of the p.d.f building functionality
414
415RooAbsArg* RooCustomizer::doBuild(const char* masterCatState, Bool_t verbose)
416{
417 // Find nodes that must be split according to provided description, Clone nodes, change their names
418 RooArgSet masterNodesToBeSplit("masterNodesToBeSplit") ;
419 RooArgSet masterNodesToBeReplaced("masterNodesToBeReplaced") ;
420 RooArgSet masterReplacementNodes("masterReplacementNodes") ;
421 RooArgSet clonedMasterNodes("clonedMasterNodes") ;
422
423
424 RooArgSet nodeList(_masterLeafList) ;
425 nodeList.add(_masterBranchList) ;
426
427 // cout << "loop over " << nodeList.getSize() << " nodes" << endl ;
428 for (auto node : nodeList) {
429 RooAbsArg* theSplitArg = !_sterile?(RooAbsArg*) _splitArgList.FindObject(node->GetName()):0 ;
430 if (theSplitArg) {
432 if (verbose) {
433 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
434 << "): tree node " << node->GetName() << " is split by category " << splitCat->GetName() << endl ;
435 }
436
437 TString newName(node->GetName()) ;
438 if (masterCatState) {
439 newName.Append("_") ;
440 newName.Append(splitCat->getCurrentLabel()) ;
441 }
442
443 // Check if this node instance already exists
444 RooAbsArg* specNode = _cloneNodeListAll ? _cloneNodeListAll->find(newName) : _cloneNodeListOwned->find(newName) ;
445 if (specNode) {
446
447 // Copy instance to one-time use list for this build
448 clonedMasterNodes.add(*specNode) ;
449 if (verbose) {
450 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
451 << ") Adding existing node specialization " << newName << " to clonedMasterNodes" << endl ;
452 }
453
454 // Affix attribute with old name to clone to support name changing server redirect
455 TString nameAttrib("ORIGNAME:") ;
456 nameAttrib.Append(node->GetName()) ;
457 specNode->setAttribute(nameAttrib) ;
458
459 if (!specNode->getStringAttribute("origName")) {
460 specNode->setStringAttribute("origName",node->GetName()) ;
461 }
462
463
464
465 } else {
466
467 if (node->isDerived()) {
468 coutW(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
469 << "): WARNING: branch node " << node->GetName() << " is split but has no pre-defined specializations" << endl ;
470 }
471
472 TString newTitle(node->GetTitle()) ;
473 newTitle.Append(" (") ;
474 newTitle.Append(splitCat->getCurrentLabel()) ;
475 newTitle.Append(")") ;
476
477 // Create a new clone
478 RooAbsArg* clone = (RooAbsArg*) node->Clone(newName.Data()) ;
479 clone->setStringAttribute("factory_tag",0) ;
480 clone->SetTitle(newTitle) ;
481
482 // Affix attribute with old name to clone to support name changing server redirect
483 TString nameAttrib("ORIGNAME:") ;
484 nameAttrib.Append(node->GetName()) ;
485 clone->setAttribute(nameAttrib) ;
486
487 if (!clone->getStringAttribute("origName")) {
488 clone->setStringAttribute("origName",node->GetName()) ;
489 }
490
491 // Add to one-time use list and life-time use list
492 clonedMasterNodes.add(*clone) ;
493 if (_owning) {
494 _cloneNodeListOwned->addOwned(*clone) ;
495 } else {
496 _cloneNodeListOwned->add(*clone) ;
497 }
498 if (_cloneNodeListAll) {
499 _cloneNodeListAll->add(*clone) ;
500 }
501 }
502 masterNodesToBeSplit.add(*node) ;
503 }
504
505 RooAbsArg* ReplaceArg = (RooAbsArg*) _replaceArgList.FindObject(node->GetName()) ;
506 if (ReplaceArg) {
507 RooAbsArg* substArg = (RooAbsArg*) _replaceSubList.At(_replaceArgList.IndexOf(ReplaceArg)) ;
508 if (verbose) {
509 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName()
510 << "): tree node " << node->GetName() << " will be replaced by " << substArg->GetName() << endl ;
511 }
512
513 // Affix attribute with old name to support name changing server redirect
514 TString nameAttrib("ORIGNAME:") ;
515 nameAttrib.Append(node->GetName()) ;
516 substArg->setAttribute(nameAttrib) ;
517
518 // Add to list
519 masterNodesToBeReplaced.add(*node) ;
520 masterReplacementNodes.add(*substArg) ;
521 }
522 }
523
524 // Find branches that are affected by splitting and must be cloned
525 RooArgSet masterBranchesToBeCloned("masterBranchesToBeCloned") ;
526 for (auto branch : _masterBranchList) {
527
528 // If branch is split itself, don't handle here
529 if (masterNodesToBeSplit.find(branch->GetName())) {
530 if (verbose) {
531 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node " << branch->GetName() << " is already split" << endl ;
532 }
533 continue ;
534 }
535 if (masterNodesToBeReplaced.find(branch->GetName())) {
536 if (verbose) {
537 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node " << branch->GetName() << " is already replaced" << endl ;
538 }
539 continue ;
540 }
541
542 if (branch->dependsOn(masterNodesToBeSplit)) {
543 if (verbose) {
544 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node "
545 << branch->IsA()->GetName() << "::" << branch->GetName() << " cloned: depends on a split parameter" << endl ;
546 }
547 masterBranchesToBeCloned.add(*branch) ;
548 } else if (branch->dependsOn(masterNodesToBeReplaced)) {
549 if (verbose) {
550 coutI(ObjectHandling) << "RooCustomizer::build(" << _masterPdf->GetName() << ") Branch node "
551 << branch->IsA()->GetName() << "::" << branch->GetName() << " cloned: depends on a replaced parameter" << endl ;
552 }
553 masterBranchesToBeCloned.add(*branch) ;
554 }
555 }
556
557 // Clone branches, changes their names
558 RooAbsArg* cloneTopPdf = 0;
559 RooArgSet clonedMasterBranches("clonedMasterBranches") ;
560
561 for (auto branch : masterBranchesToBeCloned) {
562 TString newName(branch->GetName()) ;
563 if (masterCatState) {
564 newName.Append("_") ;
565 newName.Append(masterCatState) ;
566 }
567
568 // Affix attribute with old name to clone to support name changing server redirect
569 RooAbsArg* clone = (RooAbsArg*) branch->Clone(newName.Data()) ;
570 clone->setStringAttribute("factory_tag",0) ;
571 TString nameAttrib("ORIGNAME:") ;
572 nameAttrib.Append(branch->GetName()) ;
573 clone->setAttribute(nameAttrib) ;
574
575 if (!clone->getStringAttribute("origName")) {
576 clone->setStringAttribute("origName",branch->GetName()) ;
577 }
578
579 clonedMasterBranches.add(*clone) ;
580
581 // Save pointer to clone of top-level pdf
582 if (branch==_masterPdf) cloneTopPdf=(RooAbsArg*)clone ;
583 }
584
585 if (_owning) {
586 _cloneBranchList->addOwned(clonedMasterBranches) ;
587 } else {
588 _cloneBranchList->add(clonedMasterBranches) ;
589 }
590
591 // Reconnect cloned branches to each other and to cloned nodess
592 for (auto branch : clonedMasterBranches) {
593 branch->redirectServers(clonedMasterBranches,kFALSE,kTRUE) ;
594 branch->redirectServers(clonedMasterNodes,kFALSE,kTRUE) ;
595 branch->redirectServers(masterReplacementNodes,kFALSE,kTRUE) ;
596 }
597
598 return cloneTopPdf ? cloneTopPdf : _masterPdf ;
599}
600
601
602////////////////////////////////////////////////////////////////////////////////
603/// Print name of customizer
604
605void RooCustomizer::printName(ostream& os) const
606{
607 os << GetName() ;
608}
609
610
611////////////////////////////////////////////////////////////////////////////////
612/// Print title of customizer
613
614void RooCustomizer::printTitle(ostream& os) const
615{
616 os << GetTitle() ;
617}
618
619
620////////////////////////////////////////////////////////////////////////////////
621/// Print class name of customizer
622
623void RooCustomizer::printClassName(ostream& os) const
624{
625 os << IsA()->GetName() ;
626}
627
628
629////////////////////////////////////////////////////////////////////////////////
630/// Print arguments of customizer, i.e. input p.d.f and input master category (if any)
631
632void RooCustomizer::printArgs(ostream& os) const
633{
634 os << "[ masterPdf=" << _masterPdf->GetName() ;
635 if (_masterCat) {
636 os << " masterCat=" << _masterCat->GetName() ;
637 }
638 os << " ]" ;
639}
640
641
642
643////////////////////////////////////////////////////////////////////////////////
644/// Print customizer configuration details
645
646void RooCustomizer::printMultiline(ostream& os, Int_t /*content*/, Bool_t /*verbose*/, TString indent) const
647{
648 os << indent << "RooCustomizer for " << _masterPdf->GetName() << (_sterile?" (sterile)":"") << endl ;
649
650 Int_t i, nsplit = _splitArgList.GetSize() ;
651 if (nsplit>0) {
652 os << indent << " Splitting rules:" << endl ;
653 for (i=0 ; i<nsplit ; i++) {
654 os << indent << " " << _splitArgList.At(i)->GetName() << " is split by " << _splitCatList.At(i)->GetName() << endl ;
655 }
656 }
657
658 Int_t nrepl = _replaceArgList.GetSize() ;
659 if (nrepl>0) {
660 os << indent << " Replacement rules:" << endl ;
661 for (i=0 ; i<nrepl ; i++) {
662 os << indent << " " << _replaceSubList.At(i)->GetName() << " replaces " << _replaceArgList.At(i)->GetName() << endl ;
663 }
664 }
665
666 return ;
667}
668
669
670
671////////////////////////////////////////////////////////////////////////////////
672/// Install the input RooArgSet as container in which all cloned branches
673/// will be stored
674
676{
677 _cloneBranchList = &cloneBranchSet ;
679}
680
681
682
683
684////////////////////////////////////////////////////////////////////////////////
685
686std::string RooCustomizer::CustIFace::create(RooFactoryWSTool& ft, const char* typeName, const char* instanceName, std::vector<std::string> args)
687{
688 // Check number of arguments
689 if (args.size()<2) {
690 throw string(Form("RooCustomizer::CustIFace::create() ERROR: expect at least 2 arguments for EDIT: the input object and at least one $Replace() rule")) ;
691 }
692
693 if (string(typeName)!="EDIT") {
694 throw string(Form("RooCustomizer::CustIFace::create() ERROR: unknown type requested: %s",typeName)) ;
695 }
696
697 // Check that first arg exists as RooAbsArg
698 RooAbsArg* arg = ft.ws().arg(args[0].c_str()) ;
699 if (!arg) {
700 throw string(Form("RooCustomizer::CustIFace::create() ERROR: input RooAbsArg %s does not exist",args[0].c_str())) ;
701 }
702
703 // 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
704 if (args[0]==instanceName) {
705 instanceName=0 ;
706 }
707
708 // Create a customizer
709 RooCustomizer cust(*arg,instanceName) ;
710
711 for (unsigned int i=1 ; i<args.size() ; i++) {
712 char buf[1024] ;
713 strlcpy(buf,args[i].c_str(),1024) ;
714 char* sep = strchr(buf,'=') ;
715 if (!sep) {
716 throw string(Form("RooCustomizer::CustIFace::create() ERROR: unknown argument: %s, expect form orig=subst",args[i].c_str())) ;
717 }
718 *sep = 0 ;
719 RooAbsArg* orig = ft.ws().arg(buf) ;
720 RooAbsArg* subst(0) ;
721 if (string(sep+1).find("$REMOVE")==0) {
722
723 // Create a removal dummy ;
725
726 // If removal instructed was annotated with target node, encode these in removal dummy
727 char* sep2 = strchr(sep+1,'(') ;
728 if (sep2) {
729 char buf2[1024] ;
730 strlcpy(buf2,sep2+1,1024) ;
731 char* saveptr ;
732 char* tok = R__STRTOK_R(buf2,",)",&saveptr) ;
733 while(tok) {
734 //cout << "$REMOVE is restricted to " << tok << endl ;
735 subst->setAttribute(Form("REMOVE_FROM_%s",tok)) ;
736 tok = R__STRTOK_R(0,",)",&saveptr) ;
737 }
738 } else {
739 // Otherwise mark as universal removal node
740 subst->setAttribute("REMOVE_ALL") ;
741 }
742
743 } else {
744 subst = ft.ws().arg(sep+1) ;
745 }
746// if (!orig) {
747// throw string(Form("RooCustomizer::CustIFace::create() ERROR: $Replace() input RooAbsArg %s does not exist",buf)) ;
748// }
749// if (!subst) {
750// throw string(Form("RooCustomizer::CustIFace::create() ERROR: $Replace() replacement RooAbsArg %s does not exist",sep+1)) ;
751// }
752 if (orig && subst) {
753 cust.replaceArg(*orig,*subst) ;
754 } else {
755 oocoutW((TObject*)0,ObjectHandling) << "RooCustomizer::CustIFace::create() WARNING: input or replacement of a replacement operation not found, operation ignored"<< endl ;
756 }
757 }
758
759 // Build the desired edited object
760 RooAbsArg* targ = cust.build(kFALSE) ;
761 if (!targ) {
762 throw string(Form("RooCustomizer::CustIFace::create() ERROR in customizer build, object %snot created",instanceName)) ;
763 }
764
765 // Import the object into the workspace
766 if (instanceName) {
767 // Set the desired name of the top level node
768 targ->SetName(instanceName) ;
769 // Now import everything. What we didn't touch gets recycled, everything else was cloned here:
771 } else {
773 }
774
775 return string(instanceName?instanceName:targ->GetName()) ;
776}
static Int_t dummy
static Int_t init()
#define coutI(a)
Definition: RooMsgService.h:30
#define oocoutW(o, a)
Definition: RooMsgService.h:47
#define coutW(a)
Definition: RooMsgService.h:32
#define coutE(a)
Definition: RooMsgService.h:33
int Int_t
Definition: RtypesCore.h:43
const Bool_t kFALSE
Definition: RtypesCore.h:90
const Bool_t kTRUE
Definition: RtypesCore.h:89
#define ClassImp(name)
Definition: Rtypes.h:361
char * R__STRTOK_R(char *str, const char *delim, char **saveptr)
Definition: Rtypes.h:482
static void indent(ostringstream &buf, int indent_level)
char name[80]
Definition: TGX11.cxx:109
char * Form(const char *fmt,...)
typedef void((*Func_t)())
RooAbsArg is the common abstract base class for objects that represent a value (of arbitrary type) an...
Definition: RooAbsArg.h:73
void setStringAttribute(const Text_t *key, const Text_t *value)
Associate string 'value' to this object under key 'key'.
Definition: RooAbsArg.cxx:289
void leafNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=0, Bool_t recurseNonDerived=kFALSE) const
Fill supplied list with all leaf nodes of the arg tree, starting with ourself as top node.
Definition: RooAbsArg.cxx:475
virtual TObject * Clone(const char *newname=0) const
Make a clone of an object using the Streamer facility.
Definition: RooAbsArg.h:85
const Text_t * getStringAttribute(const Text_t *key) const
Get string attribute mapped under key 'key'.
Definition: RooAbsArg.cxx:302
void setAttribute(const Text_t *name, Bool_t value=kTRUE)
Set (default) or clear a named boolean attribute of this object.
Definition: RooAbsArg.cxx:257
Bool_t addOwnedComponents(const RooArgSet &comps)
Take ownership of the contents of 'comps'.
Definition: RooAbsArg.cxx:2107
void branchNodeServerList(RooAbsCollection *list, const RooAbsArg *arg=0, Bool_t recurseNonDerived=kFALSE) const
Fill supplied list with all branch nodes of the arg tree starting with ourself as top node.
Definition: RooAbsArg.cxx:486
void SetName(const char *name)
Set the name of the TNamed.
Definition: RooAbsArg.cxx:2216
RooAbsCategoryLValue is the common abstract base class for objects that represent a discrete value th...
virtual bool setLabel(const char *label, Bool_t printError=kTRUE)=0
Change category state by specifying a state name.
RooAbsCategory is the base class for objects that represent a discrete value with a finite number of ...
virtual const char * getCurrentLabel() const
Return label string of current state.
Int_t getSize() const
void useHashMapForFind(bool flag) const
Install an internal hash map for fast finding of elements by name.
virtual Bool_t remove(const RooAbsArg &var, Bool_t silent=kFALSE, Bool_t matchByNameOnly=kFALSE)
Remove the specified argument from our list.
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:28
virtual Bool_t addOwned(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling addOwned() for each element in the source...
Definition: RooArgSet.h:92
virtual Bool_t add(const RooAbsCollection &col, Bool_t silent=kFALSE)
Add a collection of arguments to this collection by calling add() for each element in the source coll...
Definition: RooArgSet.h:88
std::string create(RooFactoryWSTool &ft, const char *typeName, const char *instanceName, std::vector< std::string > args)
RooCustomizer is a factory class to produce clones of a prototype composite PDF object with the same ...
Definition: RooCustomizer.h:32
TList _splitArgList
Definition: RooCustomizer.h:94
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)
Install the input RooArgSet as container in which all cloned branches will be stored.
RooArgSet * _cloneNodeListAll
void replaceArg(const RooAbsArg &orig, const RooAbsArg &subst)
Replace any occurence of arg 'orig' with arg 'subst'.
RooAbsArg * _masterPdf
TList _replaceSubList
Definition: RooCustomizer.h:98
const RooArgSet & cloneBranchList() const
Definition: RooCustomizer.h:52
RooArgSet * _cloneBranchList
virtual void printName(std::ostream &os) const
Print name of customizer.
virtual void printTitle(std::ostream &os) const
Print title of customizer.
RooCustomizer(const RooAbsArg &pdf, const RooAbsCategoryLValue &masterCat, RooArgSet &splitLeafListOwned, RooArgSet *splitLeafListAll=0)
Constructor with a prototype and masterCat index category.
virtual void printMultiline(std::ostream &os, Int_t content, Bool_t verbose=kFALSE, TString indent="") const
Print customizer configuration details.
RooArgSet _masterBranchList
RooAbsArg * doBuild(const char *masterCatState, Bool_t verbose)
Back-end implementation of the p.d.f building functionality.
RooArgSet _masterLeafList
virtual void printClassName(std::ostream &os) const
Print class name of customizer.
virtual ~RooCustomizer()
Destructor.
RooArgSet _internalCloneBranchList
void initialize()
Initialize the customizer.
RooAbsCategoryLValue * _masterCat
void splitArgs(const RooArgSet &argSet, const RooAbsCategory &splitCat)
Split all arguments in 'set' into individualized clones for each defined state of 'splitCat'.
RooAbsArg * build(const char *masterCatState, Bool_t verbose=kFALSE)
Build a clone of the prototype executing all registered 'replace' rules and 'split' rules for the mas...
TList _replaceArgList
Definition: RooCustomizer.h:97
TList _splitCatList
Definition: RooCustomizer.h:95
RooArgSet * _cloneNodeListOwned
virtual void printArgs(std::ostream &os) const
Print arguments of customizer, i.e. input p.d.f and input master category (if any)
RooFactoryWSTool is a class similar to TTree::MakeClass() that generates skeleton code for RooAbsPdf ...
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(const char *name) const
Return RooAbsArg with given name. A null pointer is returned if none is found.
Bool_t import(const RooAbsArg &arg, const RooCmdArg &arg1=RooCmdArg(), const RooCmdArg &arg2=RooCmdArg(), const RooCmdArg &arg3=RooCmdArg(), const RooCmdArg &arg4=RooCmdArg(), const RooCmdArg &arg5=RooCmdArg(), const RooCmdArg &arg6=RooCmdArg(), const RooCmdArg &arg7=RooCmdArg(), const RooCmdArg &arg8=RooCmdArg(), const RooCmdArg &arg9=RooCmdArg())
Import a RooAbsArg object, e.g.
virtual Int_t GetSize() const
Return the capacity of the collection, i.e.
Definition: TCollection.h:182
virtual void Add(TObject *obj)
Definition: TList.h:87
virtual TObject * FindObject(const char *name) const
Find an object in this list using its name.
Definition: TList.cxx:577
virtual TObject * At(Int_t idx) const
Returns the object at position idx. Returns 0 if idx is out of range.
Definition: TList.cxx:356
The TNamed class is the base class for all named ROOT classes.
Definition: TNamed.h:29
virtual void SetTitle(const char *title="")
Set the title of the TNamed.
Definition: TNamed.cxx:164
virtual const char * GetTitle() const
Returns title of object.
Definition: TNamed.h:48
virtual const char * GetName() const
Returns name of object.
Definition: TNamed.h:47
Mother of all ROOT objects.
Definition: TObject.h:37
virtual const char * GetName() const
Returns name of object.
Definition: TObject.cxx:357
virtual Int_t IndexOf(const TObject *obj) const
Return index of object in collection.
Basic string class.
Definition: TString.h:131
Ssiz_t Length() const
Definition: TString.h:405
const char * Data() const
Definition: TString.h:364
TString & Append(const char *cs)
Definition: TString.h:559
RooCmdArg NoRecursion(Bool_t flag=kTRUE)
RooCmdArg RecycleConflictNodes(Bool_t flag=kTRUE)
RooCmdArg RenameConflictNodes(const char *suffix, Bool_t renameOrigNodes=kFALSE)
RooCmdArg Silence(Bool_t flag=kTRUE)
@ InputArguments
Definition: RooGlobalFunc.h:68
@ ObjectHandling
Definition: RooGlobalFunc.h:68