Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
RooTemplateProxy.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * Project: RooFit *
3  * Package: RooFitCore *
4  * File: $Id: RooRealProxy.h,v 1.23 2007/07/12 20:30:28 wouter 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_TEMPLATE_PROXY
17 #define ROO_TEMPLATE_PROXY
18 
19 #include "RooAbsReal.h"
20 #include "RooArgProxy.h"
21 #include "RooAbsRealLValue.h"
22 
23 /**
24 \class RooTemplateProxy
25 \ingroup Roofitcore
26 
27 A RooTemplateProxy is used to hold references to other objects in an expression tree.
28 A `RooGaussian(..., x, mean, sigma)` e.g. stores `x, mean, sigma` as `RooRealProxy (=
29 RooTemplateProxy<RooAbsReal>`.
30 
31 This allows access to their current values, to retrieve batches of observable data, and it
32 automatically registers these variables as "servers" of the Gaussian.
33 
34 Renaming or exchanging objects that serve values to the owner of the proxy is handled automatically.
35 
36 A few typedefs have been defined:
37 - `RooRealProxy = RooTemplateProxy<RooAbsReal>`. Any generic object that converts to a real value.
38 - `RooPdfProxy = RooTemplateProxy<RooAbsPdf>`. Handle to PDFs.
39 - `RooLVarProxy = RooTemplateProxy<RooAbsRealLValue>`. Handle to real values that can be assigned to.
40 - `RooRealVarProxy = RooTemplateProxy<RooRealVar>`. Handle to RooRealVars.
41 **/
42 
43 template<class T>
44 class RooTemplateProxy : public RooArgProxy {
45 public:
46 
47  // Constructors, assignment etc.
48  RooTemplateProxy() {} ;
49 
50  ////////////////////////////////////////////////////////////////////////////////
51  /// Constructor with owner.
52  RooTemplateProxy(const char* theName, const char* desc, RooAbsArg* owner,
53  Bool_t valueServer=true, Bool_t shapeServer=false, Bool_t proxyOwnsArg=false)
54  : RooArgProxy(theName, desc, owner, valueServer, shapeServer, proxyOwnsArg) { }
55 
56  ////////////////////////////////////////////////////////////////////////////////
57  /// Constructor with owner and proxied real-valued object. The propagation
58  /// of value and shape dirty flags of the contained arg to the owner is
59  /// controlled by the valueServer and shapeServer flags. If ownArg is true
60  /// the proxy will take ownership of the contained arg.
61  RooTemplateProxy(const char* theName, const char* desc, RooAbsArg* owner, T& ref,
62  Bool_t valueServer=true, Bool_t shapeServer=false, Bool_t proxyOwnsArg=false) :
63  RooArgProxy(theName, desc, owner, ref, valueServer, shapeServer, proxyOwnsArg) { }
64 
65 
66  ////////////////////////////////////////////////////////////////////////////////
67  /// Copy constructor.
68  RooTemplateProxy(const char* theName, RooAbsArg* owner, const RooTemplateProxy& other) :
69  RooArgProxy(theName, owner, other) { }
70 
71  virtual TObject* Clone(const char* newName=0) const { return new RooTemplateProxy<T>(newName,_owner,*this); }
72 
73  inline operator Double_t() const {
74  return arg().getVal(_nset);
75  }
76 
77  RooSpan<const double> getValBatch(std::size_t begin, std::size_t batchSize) const {
78  return arg().getValBatch(begin, batchSize, _nset);
79  }
80 
81  /// Return reference to object held in proxy.
82  inline const T& arg() const { return static_cast<T&>(*_arg); }
83 
84  ////////////////////////////////////////////////////////////////////////////////
85  /// Change object held in proxy into newRef
86  Bool_t setArg(T& newRef) {
87  if (absArg()) {
88  if (TString(arg().GetName()!=newRef.GetName())) {
89  newRef.setAttribute(Form("ORIGNAME:%s",arg().GetName())) ;
90  }
91  return changePointer(RooArgSet(newRef),kTRUE) ;
92  } else {
93  return changePointer(RooArgSet(newRef),kFALSE,kTRUE);
94  }
95  }
96 
97 
98  /// Assign a new value to the object pointed to by the proxy. This requires the payload to be assignable (RooAbsRealLValue or derived).
99  RooTemplateProxy<T>& operator=(const Double_t& value) { lvptr()->setVal(value) ; return *this ; }
100  /// Query lower limit of range. This requires the payload to be RooAbsRealLValue or derived.
101  Double_t min(const char* rname=0) const { return lvptr()->getMin(rname) ; }
102  /// Query upper limit of range. This requires the payload to be RooAbsRealLValue or derived.
103  Double_t max(const char* rname=0) const { return lvptr()->getMax(rname) ; }
104  /// Check if the range has a lower bound. This requires the payload to be RooAbsRealLValue or derived.
105  Bool_t hasMin(const char* rname=0) const { return lvptr()->hasMin(rname) ; }
106  /// Check if the range has a upper bound. This requires the payload to be RooAbsRealLValue or derived.
107  Bool_t hasMax(const char* rname=0) const { return lvptr()->hasMax(rname) ; }
108 
109 
110 private:
111  ////////////////////////////////////////////////////////////////////////////////
112  /// Return l-value pointer to contents. If the contents derive from RooAbsLValue, the function
113  /// simply returns the pointer.
114  /// If the template parameter of this proxy does not derive from RooAbsLValue, then
115  /// - in a debug build, a dynamic_cast is attempted.
116  /// - in a release build, a static_cast is forced, irrespective of what the type of the object actually is. This
117  /// is dangerous, but equivalent to the behaviour before refactoring.
118  /// \deprecated This function is unneccessary if the template parameter is RooAbsRealLValue or a
119  /// derived type, as arg() will always return the correct type.
120  RooAbsRealLValue* lvptr() const {
121  return lvptr_impl(static_cast<T*>(nullptr));
122  }
123 
124  /// Overload with RooAbsRealLValue and derived types. Just returns the pointer.
125  RooAbsRealLValue* lvptr_impl(RooAbsRealLValue*) const {
126  return _arg;
127  }
128 
129  /// Overload with base types. Attempts a cast.
130  /// \deprecated The payload of this proxy should be at least RooAbsRealLValue or more derived for
131  /// this function to be called safely.
132  RooAbsRealLValue* lvptr_impl(RooAbsArg*) const
133  R__SUGGEST_ALTERNATIVE("The template argument of RooTemplateProxy needs to derive from RooAbsRealLValue.") {
134 #ifdef NDEBUG
135  return static_cast<RooAbsRealLValue*>(_arg);
136 #else
137  auto theArg = dynamic_cast<RooAbsRealLValue*>(_arg);
138  assert(theArg);
139  return theArg;
140 #endif
141  }
142 
143  ClassDef(RooTemplateProxy,1) // Proxy for a RooAbsReal object
144 };
145 
146 /// Compatibility typedef for old uses of this proxy.
147 using RooRealProxy = RooTemplateProxy<RooAbsReal>;
148 using RooPdfProxy = RooTemplateProxy<RooAbsPdf>;
149 using RooLVarProxy = RooTemplateProxy<RooAbsRealLValue>;
150 using RooRealVarProxy = RooTemplateProxy<RooRealVar>;
151 
152 #endif