13 #ifndef ROOCFUNCTION2BINDING
14 #define ROOCFUNCTION2BINDING
30 typedef Double_t (*CFUNCD2DD)(Double_t,Double_t) ;
31 typedef Double_t (*CFUNCD2ID)(Int_t,Double_t) ;
32 typedef Double_t (*CFUNCD2UD)(UInt_t,Double_t) ;
33 typedef Double_t (*CFUNCD2DI)(Double_t,Int_t) ;
34 typedef Double_t (*CFUNCD2II)(Int_t,Int_t) ;
37 RooAbsReal* bindFunction(
const char* name,CFUNCD2DD func,RooAbsReal& x, RooAbsReal& y) ;
38 RooAbsReal* bindFunction(
const char* name,CFUNCD2ID func,RooAbsReal& x, RooAbsReal& y) ;
39 RooAbsReal* bindFunction(
const char* name,CFUNCD2UD func,RooAbsReal& x, RooAbsReal& y) ;
40 RooAbsReal* bindFunction(
const char* name,CFUNCD2DI func,RooAbsReal& x, RooAbsReal& y) ;
41 RooAbsReal* bindFunction(
const char* name,CFUNCD2II func,RooAbsReal& x, RooAbsReal& y) ;
42 RooAbsPdf* bindPdf(
const char* name,CFUNCD2DD func,RooAbsReal& x, RooAbsReal& y) ;
43 RooAbsPdf* bindPdf(
const char* name,CFUNCD2ID func,RooAbsReal& x, RooAbsReal& y) ;
44 RooAbsPdf* bindPdf(
const char* name,CFUNCD2UD func,RooAbsReal& x, RooAbsReal& y) ;
45 RooAbsPdf* bindPdf(
const char* name,CFUNCD2DI func,RooAbsReal& x, RooAbsReal& y) ;
46 RooAbsPdf* bindPdf(
const char* name,CFUNCD2II func,RooAbsReal& x, RooAbsReal& y) ;
50 template<
class VO,
class VI1,
class VI2>
51 class RooCFunction2Map {
53 RooCFunction2Map() {} ;
55 void add(
const char* name, VO (*ptr)(VI1,VI2),
const char* arg1name=
"x",
const char* arg2name=
"y") {
58 _namemap[ptr] = name ;
59 _argnamemap[ptr].push_back(arg1name) ;
60 _argnamemap[ptr].push_back(arg2name) ;
64 const char* lookupName(VO (*ptr)(VI1,VI2)) {
66 return _namemap[ptr].c_str() ;
69 VO (*lookupPtr(
const char* name))(VI1,VI2) {
71 return _ptrmap[name] ;
74 const char* lookupArgName(VO (*ptr)(VI1,VI2), UInt_t iarg) {
77 if (iarg<_argnamemap[ptr].size()) {
78 return (_argnamemap[ptr])[iarg].c_str() ;
91 std::map<std::string,VO (*)(VI1,VI2)> _ptrmap ;
92 std::map<VO (*)(VI1,VI2),std::string> _namemap ;
93 std::map<VO (*)(VI1,VI2),std::vector<std::string> > _argnamemap ;
99 template<
class VO,
class VI1,
class VI2>
100 class RooCFunction2Ref :
public TObject {
102 RooCFunction2Ref(VO (*ptr)(VI1,VI2)=0) : _ptr(ptr) {
105 ~RooCFunction2Ref() {} ;
107 VO operator()(VI1 x,VI2 y)
const {
109 return (*_ptr)(x,y) ;
112 const char* name()
const {
116 const char* result = fmap().lookupName(_ptr) ;
117 if (result && strlen(result)) {
125 temp._funcptr = _ptr;
126 return Form(
"(%p)",temp._ptr) ;
129 const char* argName(Int_t iarg) {
131 return fmap().lookupArgName(_ptr,iarg) ;
134 static RooCFunction2Map<VO,VI1,VI2>& fmap() {
137 _fmap =
new RooCFunction2Map<VO,VI1,VI2> ;
144 static VO dummyFunction(VI1,VI2) {
150 typedef VO (*func_t)(VI1,VI2);
153 static RooCFunction2Map<VO,VI1,VI2>* _fmap ;
155 ClassDef(RooCFunction2Ref,1)
159 template<
class VO,
class VI1,
class VI2>
160 RooCFunction2Map<VO,VI1,VI2>* RooCFunction2Ref<VO,VI1,VI2>::_fmap = 0;
163 template<
class VO,
class VI1,
class VI2>
164 void RooCFunction2Ref<VO,VI1,VI2>::Streamer(TBuffer &R__b)
175 typedef ::RooCFunction2Ref<VO,VI1,VI2> thisClass;
178 if (R__b.IsReading()) {
181 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
185 tmpName.Streamer(R__b) ;
187 if (tmpName==
"UNKNOWN" && R__v>0) {
189 coutW(ObjectHandling) <<
"WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
190 _ptr = dummyFunction ;
195 _ptr = fmap().lookupPtr(tmpName.Data()) ;
198 coutW(ObjectHandling) <<
"ERROR: Objected embeds pointer to function named " << tmpName
199 <<
" but no such function is registered, object will not be functional" << std::endl ;
204 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
209 R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
212 TString tmpName = fmap().lookupName(_ptr) ;
213 if (tmpName.Length()==0) {
214 coutW(ObjectHandling) <<
"WARNING: Cannot persist unknown function pointer " << Form(
"0x%lx", (ULong_t)_ptr)
215 <<
" written object will not be functional when read back" << std::endl ;
220 tmpName.Streamer(R__b) ;
222 R__b.SetByteCount(R__c, kTRUE);
229 template<
class VO,
class VI1,
class VI2>
230 class RooCFunction2Binding :
public RooAbsReal {
232 RooCFunction2Binding() {
235 RooCFunction2Binding(
const char *name,
const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
236 RooCFunction2Binding(
const RooCFunction2Binding& other,
const char* name=0) ;
237 virtual TObject* clone(
const char* newname)
const {
return new RooCFunction2Binding(*
this,newname); }
238 inline virtual ~RooCFunction2Binding() { }
240 void printArgs(std::ostream& os)
const {
242 os <<
"[ function=" << func.name() <<
" " ;
243 for (Int_t i=0 ; i<numProxies() ; i++) {
244 RooAbsProxy* p = getProxy(i) ;
245 if (!TString(p->name()).BeginsWith(
"!")) {
255 RooCFunction2Ref<VO,VI1,VI2> func ;
259 Double_t evaluate()
const {
266 ClassDef(RooCFunction2Binding,1)
269 template<
class VO,
class VI1,
class VI2>
270 RooCFunction2Binding<VO,VI1,VI2>::RooCFunction2Binding(
const char *name,
const char *title, VO (*_func)(VI1,VI2),
271 RooAbsReal& _x, RooAbsReal& _y) :
272 RooAbsReal(name,title),
274 x(func.argName(0),func.argName(0),this,_x),
275 y(func.argName(1),func.argName(1),this,_y)
284 template<
class VO,
class VI1,
class VI2>
285 RooCFunction2Binding<VO,VI1,VI2>::RooCFunction2Binding(
const RooCFunction2Binding& other,
const char* name) :
286 RooAbsReal(other,name),
297 template<
class VO,
class VI1,
class VI2>
298 class RooCFunction2PdfBinding :
public RooAbsPdf {
300 RooCFunction2PdfBinding() {
303 RooCFunction2PdfBinding(
const char *name,
const char *title, VO (*_func)(VI1,VI2), RooAbsReal& _x, RooAbsReal& _y);
304 RooCFunction2PdfBinding(
const RooCFunction2PdfBinding& other,
const char* name=0) ;
305 virtual TObject* clone(
const char* newname)
const {
return new RooCFunction2PdfBinding(*
this,newname); }
306 inline virtual ~RooCFunction2PdfBinding() { }
308 void printArgs(std::ostream& os)
const {
310 os <<
"[ function=" << func.name() <<
" " ;
311 for (Int_t i=0 ; i<numProxies() ; i++) {
312 RooAbsProxy* p = getProxy(i) ;
313 if (!TString(p->name()).BeginsWith(
"!")) {
323 RooCFunction2Ref<VO,VI1,VI2> func ;
327 Double_t evaluate()
const {
334 ClassDef(RooCFunction2PdfBinding,1)
337 template<
class VO,
class VI1,
class VI2>
338 RooCFunction2PdfBinding<VO,VI1,VI2>::RooCFunction2PdfBinding(
const char *name,
const char *title, VO (*_func)(VI1,VI2),
339 RooAbsReal& _x, RooAbsReal& _y) :
340 RooAbsPdf(name,title),
342 x(func.argName(0),func.argName(0),this,_x),
343 y(func.argName(1),func.argName(1),this,_y)
352 template<
class VO,
class VI1,
class VI2>
353 RooCFunction2PdfBinding<VO,VI1,VI2>::RooCFunction2PdfBinding(
const RooCFunction2PdfBinding& other,
const char* name) :
354 RooAbsPdf(other,name),