Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RooAddGenContext.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * File: $Id: RooAddGenContext.h,v 1.12 2007/05/11 09:11:30 verkerke Exp $
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#ifndef ROO_ADD_GEN_CONTEXT
17#define ROO_ADD_GEN_CONTEXT
18
19#include "RooAbsGenContext.h"
20#include "RooArgSet.h"
21#include "RooAddPdf.h"
22#include "RooAddModel.h"
23#include "RooGenContext.h"
24#include "RooMsgService.h"
25
26#include <memory>
27#include <vector>
28
29class AddCacheElem;
30class RooDataSet;
31
33public:
34 RooAddGenContext(const RooAddPdf &model, const RooArgSet &vars, const RooDataSet *prototype=nullptr,
35 const RooArgSet* auxProto=nullptr, bool _verbose= false);
36 RooAddGenContext(const RooAddModel &model, const RooArgSet &vars, const RooDataSet *prototype=nullptr,
37 const RooArgSet* auxProto=nullptr, bool _verbose= false);
38
39 void setProtoDataOrder(Int_t* lut) override ;
40
41 void attach(const RooArgSet& params) override ;
42
43 void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override ;
44
45 template<class Pdf_t>
46 static std::unique_ptr<RooAbsGenContext> create(const Pdf_t &pdf, const RooArgSet &vars,
47 const RooDataSet *prototype,
48 const RooArgSet* auxProto, bool verbose);
49
50protected:
51
52 void initGenerator(const RooArgSet &theEvent) override;
53 void generateEvent(RooArgSet &theEvent, Int_t remaining) override;
54 void updateThresholds() ;
55
57
58 std::unique_ptr<RooArgSet> _vars ;
59 std::unique_ptr<RooArgSet> _pdfSet ; ///< Set owned all nodes of internal clone of p.d.f
60 RooAbsPdf *_pdf ; ///< Pointer to cloned p.d.f
61 std::vector<std::unique_ptr<RooAbsGenContext>> _gcList ; ///< List of component generator contexts
62 Int_t _nComp ; ///< Number of PDF components
63 std::vector<double> _coefThresh ; ///<[_nComp] Array of coefficient thresholds
64 bool _isModel ; ///< Are we generating from a RooAddPdf or a RooAddModel
65 AddCacheElem* _pcache = nullptr; ///<! RooAddPdf cache element
66
67 ClassDefOverride(RooAddGenContext,0) // Specialized context for generating a dataset from a RooAddPdf
68};
69
70
71/// Returns a RooAddGenContext if possible, or, if the RooAddGenContext doesn't
72/// support this particular RooAddPdf or RooAddModel because it has negative
73/// coefficients, returns a generic RooGenContext.
74///
75/// Templated function to support both RooAddPdf and RooAddModel without code
76/// duplication and without type checking at runtime.
77
78template<class Pdf_t>
79std::unique_ptr<RooAbsGenContext> RooAddGenContext::create(const Pdf_t &pdf, const RooArgSet &vars,
80 const RooDataSet *prototype,
81 const RooArgSet* auxProto, bool verbose)
82{
83 // Check if any coefficient is negative. We can use getVal() without the
84 // normalization set, as normalization doesn't change the coefficients sign.
85 auto hasNegativeCoefs = [&]() {
86 for(auto * coef : static_range_cast<RooAbsReal*>(pdf._coefList)) {
87 if(coef->getVal() < 0) return true;
88 }
89 return false;
90 };
91
92 // The RooAddGenContext doesn't support negative coefficients, so we create a
93 // generic RooGenContext.
94 if(hasNegativeCoefs()) {
95 oocxcoutI(&pdf, Generation) << pdf.ClassName() << "::genContext():"
96 << " using a generic generator context instead of the RooAddGenContext for the "
97 << pdf.ClassName() << " \"" << pdf.GetName() << "\", because the pdf has negative coefficients." << std::endl;
98 return std::make_unique<RooGenContext>(pdf, vars, prototype, auxProto, verbose);
99 }
100
101 return std::make_unique<RooAddGenContext>(pdf, vars, prototype, auxProto,verbose) ;
102}
103
104#endif
#define oocxcoutI(o, a)
#define ClassDefOverride(name, id)
Definition Rtypes.h:341
static void indent(ostringstream &buf, int indent_level)
Abstract base class for generator contexts of RooAbsPdf objects.
bool _verbose
Verbose messaging?
Abstract interface for all probability density functions.
Definition RooAbsPdf.h:40
Efficient implementation of the generator context specific for RooAddPdf PDFs.
void setProtoDataOrder(Int_t *lut) override
Forward the setProtoDataOrder call to the component generator contexts.
std::vector< double > _coefThresh
[_nComp] Array of coefficient thresholds
std::unique_ptr< RooArgSet > _pdfSet
Set owned all nodes of internal clone of p.d.f.
std::unique_ptr< RooArgSet > _vars
bool _isModel
Are we generating from a RooAddPdf or a RooAddModel.
void initGenerator(const RooArgSet &theEvent) override
One-time initialization of generator context.
std::vector< std::unique_ptr< RooAbsGenContext > > _gcList
List of component generator contexts.
static std::unique_ptr< RooAbsGenContext > create(const Pdf_t &pdf, const RooArgSet &vars, const RooDataSet *prototype, const RooArgSet *auxProto, bool verbose)
Returns a RooAddGenContext if possible, or, if the RooAddGenContext doesn't support this particular R...
void updateThresholds()
Update the cumulative threshold table from the current coefficient values.
void printMultiline(std::ostream &os, Int_t content, bool verbose=false, TString indent="") const override
Print the details of the context.
AddCacheElem * _pcache
! RooAddPdf cache element
RooAddGenContext(const RooAddGenContext &other)
void attach(const RooArgSet &params) override
Attach given set of variables to internal p.d.f. clone.
Int_t _nComp
Number of PDF components.
RooAbsPdf * _pdf
Pointer to cloned p.d.f.
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
Randomly choose one of the component contexts to generate this event, with a probability proportional...
RooAddModel is an efficient implementation of a sum of PDFs of the form.
Definition RooAddModel.h:27
Efficient implementation of a sum of PDFs of the form.
Definition RooAddPdf.h:33
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition RooArgSet.h:55
Container class to hold unbinned data.
Definition RooDataSet.h:57
Basic string class.
Definition TString.h:139