Logo ROOT  
Reference Guide
RooEffGenContext.cxx
Go to the documentation of this file.
1/*****************************************************************************
2 * Project: RooFit *
3 * Package: RooFitCore *
4 * @(#)root/roofitcore:$Id$
5 * Authors: *
6 * GR, Gerhard Raven, NIKHEF/VU, Gerhard.Raven@nikhf.nl *
7 * *
8 * Copyright (c) 2005, NIKHEF. All rights reserved. *
9 * *
10 * Redistribution and use in source and binary forms, *
11 * with or without modification, are permitted according to the terms *
12 * listed in LICENSE (http://roofit.sourceforge.net/license.txt) *
13 *****************************************************************************/
14
15
16/**
17\file RooEffGenContext.cxx
18\class RooEffGenContext
19\ingroup Roofitcore
20
21RooEffGenContext is a specialized generator context for p.d.fs represented
22by class RooEffProd, which are p.d.fs multiplied with an efficiency function.
23This generator context generates events from such products by first
24generating events from a dedicated generator context of the input p.d.f.
25and applying an extra rejection step based on the efficiency function.
26**/
27
28#include <memory>
29
30#include "RooEffGenContext.h"
31#include "RooAbsPdf.h"
32#include "RooRandom.h"
33
34using namespace std;
35
37
38////////////////////////////////////////////////////////////////////////////////
39/// Constructor of generator context for RooEffProd products
40
42 const RooAbsPdf& pdf, const RooAbsReal& eff,
43 const RooArgSet &vars,
44 const RooDataSet *prototype, const RooArgSet* auxProto,
45 Bool_t verbose, const RooArgSet* /*forceDirect*/) :
46 RooAbsGenContext(model, vars, prototype, auxProto, verbose), _maxEff(0.)
47{
48 RooArgSet x(eff,eff.GetName());
49 _cloneSet = static_cast<RooArgSet*>(x.snapshot(kTRUE));
50 _eff = dynamic_cast<RooAbsReal*>(_cloneSet->find(eff.GetName()));
51 _generator = pdf.genContext(vars, prototype, auxProto, verbose);
52 _vars = static_cast<RooArgSet*>(vars.snapshot(kTRUE));
53}
54
55////////////////////////////////////////////////////////////////////////////////
56/// Destructor
57
59{
60 delete _generator;
61 delete _cloneSet;
62 delete _vars;
63}
64
65////////////////////////////////////////////////////////////////////////////////
66/// One-time initialization of generator.
67
69{
71 _generator->initGenerator(theEvent);
72
73 // Check if PDF supports maximum finding
74 Int_t code = _eff->getMaxVal(*_vars);
75 if (!code) {
76 _maxEff = 1.;
77 } else {
78 _maxEff = _eff->maxVal(code);
79 }
80}
81
82////////////////////////////////////////////////////////////////////////////////
83/// Generate one event. Generate an event from the p.d.f and
84/// then perform an accept/reject sampling based on the efficiency
85/// function
86
88{
89 while (true) {
90 _generator->generateEvent(theEvent, remaining);
91 double val = _eff->getVal();
92 if (val > _maxEff && !_eff->getMaxVal(*_vars)) {
93 coutE(Generation) << ClassName() << "::" << GetName()
94 << ":generateEvent: value of efficiency is larger than assumed maximum of 1." << std::endl;
95 continue;
96 }
97 if (val > RooRandom::uniform() * _maxEff) {
98 break;
99 }
100 }
101}
#define coutE(a)
Definition: RooMsgService.h:33
const Bool_t kTRUE
Definition: RtypesCore.h:100
#define ClassImp(name)
Definition: Rtypes.h:375
Bool_t recursiveRedirectServers(const RooAbsCollection &newServerList, Bool_t mustReplaceAll=kFALSE, Bool_t nameChange=kFALSE, Bool_t recurseInNewSet=kTRUE)
Recursively replace all servers with the new servers in newSet.
Definition: RooAbsArg.cxx:1197
RooAbsArg * find(const char *name) const
Find object with given name in list.
RooAbsGenContext is the abstract base class for generator contexts of RooAbsPdf objects.
virtual void initGenerator(const RooArgSet &theEvent)
Interface function to initialize context for generation for given set of observables.
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)=0
virtual RooAbsGenContext * genContext(const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE) const
Interface function to create a generator context from a p.d.f.
Definition: RooAbsPdf.cxx:1903
RooAbsReal is the common abstract base class for objects that represent a real value and implements f...
Definition: RooAbsReal.h:63
Double_t getVal(const RooArgSet *normalisationSet=nullptr) const
Evaluate object.
Definition: RooAbsReal.h:93
virtual Double_t maxVal(Int_t code) const
Return maximum value for set of observables identified by code assigned in getMaxVal.
virtual Int_t getMaxVal(const RooArgSet &vars) const
Advertise capability to determine maximum value of function for given set of observables.
RooArgSet is a container object that can hold multiple RooAbsArg objects.
Definition: RooArgSet.h:57
RooArgSet * snapshot(bool deepCopy=true) const
Use RooAbsCollection::snapshot(), but return as RooArgSet.
Definition: RooArgSet.h:180
RooDataSet is a container class to hold unbinned data.
Definition: RooDataSet.h:55
RooEffGenContext is a specialized generator context for p.d.fs represented by class RooEffProd,...
void generateEvent(RooArgSet &theEvent, Int_t remaining) override
Generate one event.
RooArgSet * _cloneSet
Internal clone of p.d.f.
void initGenerator(const RooArgSet &theEvent) override
One-time initialization of generator.
RooArgSet * _vars
Vars to generate.
RooEffGenContext(const RooAbsPdf &model, const RooAbsPdf &pdf, const RooAbsReal &eff, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t verbose=kFALSE, const RooArgSet *forceDirect=0)
Constructor of generator context for RooEffProd products.
RooAbsGenContext * _generator
Generator context for p.d.f.
~RooEffGenContext() override
Destructor.
double _maxEff
Maximum of efficiency in vars.
RooAbsReal * _eff
Pointer to efficiency function.
static Double_t uniform(TRandom *generator=randomGenerator())
Return a number uniformly distributed from (0,1)
Definition: RooRandom.cxx:81
const char * GetName() const override
Returns name of object.
Definition: TNamed.h:47
virtual const char * ClassName() const
Returns name of class to which the object belongs.
Definition: TObject.cxx:130
Double_t x[n]
Definition: legend1.C:17
@ Generation
Definition: RooGlobalFunc.h:63