13 #ifndef ROOCFUNCTION1BINDING
14 #define ROOCFUNCTION1BINDING
31 typedef Double_t (*CFUNCD1D)(Double_t) ;
32 typedef Double_t (*CFUNCD1I)(Int_t) ;
34 RooAbsReal* bindFunction(
const char* name,CFUNCD1D func,RooAbsReal& x) ;
35 RooAbsReal* bindFunction(
const char* name,CFUNCD1I func,RooAbsReal& x) ;
36 RooAbsPdf* bindPdf(
const char* name,CFUNCD1D func,RooAbsReal& x) ;
37 RooAbsPdf* bindPdf(
const char* name,CFUNCD1I func,RooAbsReal& x) ;
42 template<
class VO,
class VI>
43 class RooCFunction1Map {
45 RooCFunction1Map() {} ;
47 void add(
const char* name, VO (*ptr)(VI),
const char* arg1name=
"x") {
50 _namemap[ptr] = name ;
51 _argnamemap[ptr].push_back(arg1name) ;
55 const char* lookupName(VO (*ptr)(VI)) {
57 return _namemap[ptr].c_str() ;
60 VO (*lookupPtr(
const char* name))(VI) {
62 return _ptrmap[name] ;
65 const char* lookupArgName(VO (*ptr)(VI), UInt_t iarg) {
68 if (iarg<_argnamemap[ptr].size()) {
69 return (_argnamemap[ptr])[iarg].c_str() ;
82 std::map<std::string,VO (*)(VI)> _ptrmap ;
83 std::map<VO (*)(VI),std::string> _namemap ;
84 std::map<VO (*)(VI),std::vector<std::string> > _argnamemap ;
90 template<
class VO,
class VI>
91 class RooCFunction1Ref :
public TObject {
93 RooCFunction1Ref(VO (*ptr)(VI)=0) : _ptr(ptr) {
96 ~RooCFunction1Ref() {} ;
98 VO operator()(VI x)
const {
103 const char* name()
const {
107 const char* result = fmap().lookupName(_ptr) ;
108 if (result && strlen(result)) {
116 temp._funcptr = _ptr;
117 return Form(
"(%p)",temp._ptr) ;
120 const char* argName(Int_t iarg) {
122 return fmap().lookupArgName(_ptr,iarg) ;
125 static RooCFunction1Map<VO,VI>& fmap();
129 static VO dummyFunction(VI) {
135 typedef VO (*func_t)(VI);
138 static RooCFunction1Map<VO,VI>* _fmap ;
140 ClassDef(RooCFunction1Ref,1)
144 template<
class VO,
class VI>
145 RooCFunction1Map<VO,VI>* RooCFunction1Ref<VO,VI>::_fmap = 0;
147 template<
class VO,
class VI>
148 void RooCFunction1Ref<VO,VI>::Streamer(TBuffer &R__b)
159 typedef ::RooCFunction1Ref<VO,VI> thisClass;
162 if (R__b.IsReading()) {
165 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
169 tmpName.Streamer(R__b) ;
171 if (tmpName==
"UNKNOWN" && R__v>0) {
173 coutW(ObjectHandling) <<
"WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
174 _ptr = dummyFunction ;
179 _ptr = fmap().lookupPtr(tmpName.Data()) ;
182 coutW(ObjectHandling) <<
"ERROR: Objected embeds pointer to function named " << tmpName
183 <<
" but no such function is registered, object will not be functional" << std::endl ;
188 R__b.CheckByteCount(R__s, R__c, thisClass::IsA());
193 R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);
196 TString tmpName = fmap().lookupName(_ptr) ;
197 if (tmpName.Length()==0) {
203 temp._funcptr = _ptr;
204 coutW(ObjectHandling) <<
"WARNING: Cannot persist unknown function pointer " << Form(
"%p",temp._ptr)
205 <<
" written object will not be functional when read back" << std::endl ;
210 tmpName.Streamer(R__b) ;
212 R__b.SetByteCount(R__c, kTRUE);
219 template<
class VO,
class VI>
220 class RooCFunction1Binding :
public RooAbsReal {
222 RooCFunction1Binding() {
225 RooCFunction1Binding(
const char *name,
const char *title, VO (*_func)(VI), RooAbsReal& _x);
226 RooCFunction1Binding(
const RooCFunction1Binding& other,
const char* name=0) ;
227 virtual TObject* clone(
const char* newname)
const {
return new RooCFunction1Binding(*
this,newname); }
228 inline virtual ~RooCFunction1Binding() { }
230 void printArgs(std::ostream& os)
const {
232 os <<
"[ function=" << func.name() <<
" " ;
233 for (Int_t i=0 ; i<numProxies() ; i++) {
234 RooAbsProxy* p = getProxy(i) ;
235 if (!TString(p->name()).BeginsWith(
"!")) {
245 RooCFunction1Ref<VO,VI> func ;
248 Double_t evaluate()
const {
255 ClassDef(RooCFunction1Binding,1)
259 template<
class VO,
class VI>
260 RooCFunction1Binding<VO,VI>::RooCFunction1Binding(
const char *name,
const char *title, VO (*_func)(VI), RooAbsReal& _x) :
261 RooAbsReal(name,title),
263 x(func.argName(0),func.argName(0),this,_x)
272 template<
class VO,
class VI>
273 RooCFunction1Binding<VO,VI>::RooCFunction1Binding(
const RooCFunction1Binding& other,
const char* name) :
274 RooAbsReal(other,name),
283 template<
class VO,
class VI>
284 class RooCFunction1PdfBinding :
public RooAbsPdf {
286 RooCFunction1PdfBinding() {
289 RooCFunction1PdfBinding(
const char *name,
const char *title, VO (*_func)(VI), RooAbsReal& _x);
290 RooCFunction1PdfBinding(
const RooCFunction1PdfBinding& other,
const char* name=0) ;
291 virtual TObject* clone(
const char* newname)
const {
return new RooCFunction1PdfBinding(*
this,newname); }
292 inline virtual ~RooCFunction1PdfBinding() { }
294 void printArgs(std::ostream& os)
const {
296 os <<
"[ function=" << func.name() <<
" " ;
297 for (Int_t i=0 ; i<numProxies() ; i++) {
298 RooAbsProxy* p = getProxy(i) ;
299 if (!TString(p->name()).BeginsWith(
"!")) {
309 RooCFunction1Ref<VO,VI> func ;
312 Double_t evaluate()
const {
319 ClassDef(RooCFunction1PdfBinding,1)
323 template<
class VO,
class VI>
324 RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(
const char *name,
const char *title, VO (*_func)(VI), RooAbsReal& _x) :
325 RooAbsPdf(name,title),
327 x(func.argName(0),func.argName(0),this,_x)
336 template<
class VO,
class VI>
337 RooCFunction1PdfBinding<VO,VI>::RooCFunction1PdfBinding(
const RooCFunction1PdfBinding& other,
const char* name) :
338 RooAbsPdf(other,name),