MINT2
RooEffConvGenContext.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * @(#)root/roofitcore:$Id: RooEffConvGenContext.cxx 28259 2009-04-16 16:21:16Z wouter $
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 
18 //
19 // BEGIN_HTML
20 // RooEffConvGenContext is an efficient implementation of the generator context
21 // specific for RooAbsAnaConvPdf objects. The physics model is generated
22 // with a truth resolution model and the requested resolution model is generated
23 // separately as a PDF. The convolution variable of the physics model is
24 // subsequently explicitly smeared with the resolution model distribution.
25 // END_HTML
26 //
27 
28 #include <cassert>
29 
30 #include "RooMsgService.h"
31 #include "RooArgSet.h"
32 #include "RooRandom.h"
33 class RooAbsAnaConvPdf;
34 class RooNumConvPdf;
35 class RooFFTConvPdf;
36 class RooDataSet;
37 
39 #include "Mint/RooEffResModel.h"
40 
41 using std::endl;
42 
44 { }
45 
46 //_____________________________________________________________________________
47 RooEffConvGenContext::RooEffConvGenContext(const RooAbsAnaConvPdf &model, const RooArgSet &vars,
48  const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose)
49  : RooConvGenContext(model, vars, prototype, auxProto, verbose)
50 {
51  cxcoutI(Generation) << "RooEffConvGenContext::ctor() setting up special generator context "
52  << "to apply an efficiency to the analytical convolution p.d.f. "
53  << reinterpret_cast<const RooAbsPdf*>(&model)->GetName() << " for generation of observable(s) " << vars << endl ;
55 }
56 
57 //_____________________________________________________________________________
58 RooEffConvGenContext::RooEffConvGenContext(const RooNumConvPdf &model, const RooArgSet &vars,
59  const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose)
60  : RooConvGenContext(model, vars, prototype, auxProto, verbose)
61 {
62  cxcoutI(Generation) << "RooEffConvGenContext::ctor() setting up special generator context "
63  << "to apply an efficiency to the analytical convolution p.d.f. "
64  << reinterpret_cast<const RooAbsPdf*>(&model)->GetName() << " for generation of observable(s) " << vars << endl ;
66 }
67 
68 //_____________________________________________________________________________
69 RooEffConvGenContext::RooEffConvGenContext(const RooFFTConvPdf &model, const RooArgSet &vars,
70  const RooDataSet *prototype, const RooArgSet* auxProto, Bool_t verbose)
71  : RooConvGenContext(model, vars, prototype, auxProto, verbose)
72 {
73  cxcoutI(Generation) << "RooEffConvGenContext::ctor() setting up special generator context "
74  << "to apply an efficiency to the analytical convolution p.d.f. "
75  << reinterpret_cast<const RooAbsPdf*>(&model)->GetName() << " for generation of observable(s) " << vars << endl ;
77 }
78 
79 //_____________________________________________________________________________
80 void RooEffConvGenContext::attach(const RooArgSet& theEvent)
81 {
82  RooConvGenContext::initGenerator(theEvent);
83 
84  // Replace observables in efficiency function by the generated observables.
85  // Attach the output value of the convolution variable to the efficiencies,
86  // so the final hit-miss is with respect to the correct (smeared) value;
87  RooAbsReal* eff = const_cast<RooAbsReal*>(efficiency());
88  RooArgSet* effVars
89  = (RooArgSet*)_modelVars->selectCommon(*eff->getVariables());
90  effVars->remove(*_cvModel, kTRUE, kTRUE);
91  effVars->add(*_cvOut);
92  eff->recursiveRedirectServers(*effVars, kTRUE, kFALSE, kTRUE);
93  delete effVars;
94 }
95 //_____________________________________________________________________________
96 void RooEffConvGenContext::initGenerator(const RooArgSet& theEvent)
97 { return attach(theEvent); }
98 //_____________________________________________________________________________
99 void RooEffConvGenContext::generateEvent(RooArgSet &theEvent, Int_t remaining)
100 {
101  // Generate a single event
102 
103  while(1) {
104  // Generate pdf and model data
105  _modelGen->generateEvent(*_modelVars, remaining);
106  _pdfGen->generateEvent(*_pdfVars, remaining);
107 
108  // Construct smeared convolution variable
109  Double_t convValSmeared = _cvPdf->getVal() + _cvModel->getVal();
110 
111  if (!_cvOut->isValidReal(convValSmeared)) continue;
112 
113  // Hit-miss on the efficiency
114  // This has to be set first to get the proper value of the efficiency
115  _cvOut->setVal(convValSmeared);
116 
117  double val = efficiency()->getVal();
118  if (val > _maxEff && !efficiency()->getMaxVal(*_modelVars)) {
119  coutE(Generation) << ClassName() << "::" << GetName()
120  << ":generateEvent: value of efficiency is larger "
121  << "than assumed maximum of 1." << endl;
122  continue;
123  }
124  if (val > RooRandom::uniform() * _maxEff) {
125  // Smeared value in acceptance range, transfer values to output set
126  theEvent = *_modelVars;
127  theEvent = *_pdfVars;
128  _cvOut->setVal(convValSmeared);
129  return;
130  }
131  }
132 }
133 
134 //_____________________________________________________________________________
136 {
137  // Check if efficiency supports maximum finding
138  const RooAbsEffResModel* model = dynamic_cast<const RooAbsEffResModel*>(_modelCloneSet->first());
139  assert(model);
140  RooAbsReal* eff = const_cast<RooAbsReal*>(model->efficiency());
141  Int_t maxCode = eff->getMaxVal(*_modelVars);
142  if (!maxCode) {
143  _maxEff = 1.;
144  } else {
145  Double_t maxVal = eff->maxVal(maxCode);
146  if (maxVal > _maxEff) _maxEff = maxVal;
147  }
148 }
149 
150 //_____________________________________________________________________________
152 {
153  const RooAbsEffResModel* model = dynamic_cast<const RooAbsEffResModel*>(_modelCloneSet->first());
154  assert(model);
155  const RooAbsReal* efficiency = model->efficiency();
156  assert(efficiency);
157  return efficiency;
158 }
RooEffConvGenContext(const RooFFTConvPdf &model, const RooArgSet &vars, const RooDataSet *prototype=0, const RooArgSet *auxProto=0, Bool_t _verbose=kFALSE)
virtual void initGenerator(const RooArgSet &theEvent)
virtual void generateEvent(RooArgSet &theEvent, Int_t remaining)
virtual const RooAbsReal * efficiency() const =0
const RooAbsReal * efficiency()
virtual void attach(const RooArgSet &params)