Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
CFunctionBinding.h
Go to the documentation of this file.
1/*
2 * Project: RooFit
3 * Authors:
4 * WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl
5 *
6 * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California
7 * and Stanford University. All rights reserved.
8 *
9 * Redistribution and use in source and binary forms,
10 * with or without modification, are permitted according to the terms
11 * listed in LICENSE (http://roofit.sourceforge.net/license.txt)
12 */
13
14#ifndef RooFit_Detail_CFunctionBinding_h
15#define RooFit_Detail_CFunctionBinding_h
16
17// Implementation details shared by the RooCFunction[1-4]Binding classes.
18
19#include <RooAbsArg.h>
20#include <RooAbsProxy.h>
21#include <RooMsgService.h>
22
23#include <TBuffer.h>
24#include <TString.h>
25
26#include <cstring>
27#include <map>
28#include <ostream>
29#include <string>
30#include <vector>
31
32namespace RooFit {
33namespace Detail {
34
35/// Name <-> pointer mapping service for C functions of type `Func`, used to
36/// persist the RooCFunction[1-4]Ref function pointer references.
37template <class Func>
39public:
40 /// Register function with given name and argument names. Arguments without
41 /// a name are called x, y, z, w.
42 template <class... ArgNames>
43 void add(const char *name, Func ptr, ArgNames... argNames)
44 {
45 _ptrmap[name] = ptr;
46 _namemap[ptr] = name;
47 (_argnamemap[ptr].emplace_back(argNames), ...);
48 }
49
50 /// Return name of function given by pointer.
51 const char *lookupName(Func ptr) { return _namemap[ptr].c_str(); }
52
53 /// Return pointer of function given by name.
54 Func lookupPtr(const char *name) { return _ptrmap[name]; }
55
56 /// Return name of i-th argument of function. If no name was registered,
57 /// the arguments are called x, y, z, w.
58 const char *lookupArgName(Func ptr, UInt_t iarg)
59 {
60 if (iarg < _argnamemap[ptr].size()) {
61 return (_argnamemap[ptr])[iarg].c_str();
62 }
63 switch (iarg) {
64 case 0: return "x";
65 case 1: return "y";
66 case 2: return "z";
67 case 3: return "w";
68 }
69 return "v";
70 }
71
72 /// Return registered name of a function. If the function is not
73 /// registered, return a string with the hex representation of the pointer.
74 const char *nameOrAddress(Func ptr)
75 {
76 const char *result = lookupName(ptr);
77 if (result && strlen(result)) {
78 return result;
79 }
80 return Form("(%p)", toVoidPtr(ptr));
81 }
82
83 static void *toVoidPtr(Func ptr)
84 {
85 // This union is to avoid a warning message:
86 union {
87 void *_ptr;
88 Func _funcptr;
89 } temp;
90 temp._funcptr = ptr;
91 return temp._ptr;
92 }
93
94private:
95 std::map<std::string, Func> _ptrmap; // Name-to-pointer map
96 std::map<Func, std::string> _namemap; // Pointer-to-name map
97 std::map<Func, std::vector<std::string>> _argnamemap; // Pointer-to-argnamelist map
98};
99
100/// Custom streamer for function pointer reference objects. When writing,
101/// the function pointer is substituted by its registered name. When function
102/// is unregistered name 'UNKNOWN' is written and a warning is issued. When
103/// reading back, the embedded name is converted back to a function pointer
104/// using the mapping service. When name UNKNOWN is encountered a warning is
105/// issued and a dummy null function is substituted. When the registered function
106/// name can not be mapped to a function pointer an ERROR is issued.
107template <class Func>
108void streamCFunctionRef(TBuffer &R__b, TObject const &self, Func &ptr, Func dummyFunction, CFunctionMap<Func> &fmap)
109{
110 if (R__b.IsReading()) {
111
112 UInt_t R__s;
113 UInt_t R__c;
114 Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
115
116 // Read name from file
117 TString tmpName;
118 tmpName.Streamer(R__b);
119
120 if (tmpName == "UNKNOWN" && R__v > 0) {
121
122 oocoutW(&self, ObjectHandling)
123 << "WARNING: Objected embeds function pointer to unknown function, object will not be functional"
124 << std::endl;
125 ptr = dummyFunction;
126
127 } else {
128
129 // Lookup pointer to C function with given name
130 ptr = fmap.lookupPtr(tmpName.Data());
131
132 if (ptr == nullptr) {
133 oocoutW(&self, ObjectHandling)
134 << "ERROR: Objected embeds pointer to function named " << tmpName
135 << " but no such function is registered, object will not be functional" << std::endl;
136 }
137 }
138
139 R__b.CheckByteCount(R__s, R__c, self.IsA());
140
141 } else {
142
143 UInt_t R__c;
144 R__c = R__b.WriteVersion(self.IsA(), true);
145
146 // Lookup name of reference C function
147 TString tmpName = fmap.lookupName(ptr);
148 if (tmpName.Length() == 0) {
149 oocoutW(&self, ObjectHandling) << "WARNING: Cannot persist unknown function pointer "
151 << " written object will not be functional when read back" << std::endl;
152 tmpName = "UNKNOWN";
153 }
154
155 // Persist the name
156 tmpName.Streamer(R__b);
157
158 R__b.SetByteCount(R__c, true);
159 }
160}
161
162/// Print arguments of a function binding and name/address of the function pointer.
163inline void printCFunctionArgs(std::ostream &os, RooAbsArg const &arg, const char *funcName)
164{
165 os << "[ function=" << funcName << " ";
166 for (Int_t i = 0; i < arg.numProxies(); i++) {
167 RooAbsProxy *p = arg.getProxy(i);
168 if (!TString(p->name()).BeginsWith("!")) {
169 p->print(os);
170 os << " ";
171 }
172 }
173 os << "]";
174}
175
176} // namespace Detail
177} // namespace RooFit
178
179#endif
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
#define oocoutW(o, a)
short Version_t
Class version identifier (short)
Definition RtypesCore.h:80
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t result
char name[80]
Definition TGX11.cxx:142
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2571
Common abstract base class for objects that represent a value and a "shape" in RooFit.
Definition RooAbsArg.h:77
Int_t numProxies() const
Return the number of registered proxies.
RooAbsProxy * getProxy(Int_t index) const
Return the nth proxy from the proxy list.
Abstract interface for proxy classes.
Definition RooAbsProxy.h:37
Name <-> pointer mapping service for C functions of type Func, used to persist the RooCFunction[1-4]R...
Func lookupPtr(const char *name)
Return pointer of function given by name.
const char * nameOrAddress(Func ptr)
Return registered name of a function.
void add(const char *name, Func ptr, ArgNames... argNames)
Register function with given name and argument names.
const char * lookupArgName(Func ptr, UInt_t iarg)
Return name of i-th argument of function.
static void * toVoidPtr(Func ptr)
std::map< Func, std::vector< std::string > > _argnamemap
std::map< Func, std::string > _namemap
const char * lookupName(Func ptr)
Return name of function given by pointer.
std::map< std::string, Func > _ptrmap
Buffer base class used for serializing objects.
Definition TBuffer.h:43
virtual Version_t ReadVersion(UInt_t *start=nullptr, UInt_t *bcnt=nullptr, const TClass *cl=nullptr)=0
virtual void SetByteCount(UInt_t cntpos, Bool_t packInVersion=kFALSE)=0
virtual Int_t CheckByteCount(UInt_t startpos, UInt_t bcnt, const TClass *clss)=0
Bool_t IsReading() const
Definition TBuffer.h:86
virtual UInt_t WriteVersion(const TClass *cl, Bool_t useBcnt=kFALSE)=0
Mother of all ROOT objects.
Definition TObject.h:42
virtual TClass * IsA() const
Definition TObject.h:248
Basic string class.
Definition TString.h:137
Ssiz_t Length() const
Definition TString.h:426
const char * Data() const
Definition TString.h:385
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:633
virtual void Streamer(TBuffer &)
Stream a string object.
Definition TString.cxx:1493
void printCFunctionArgs(std::ostream &os, RooAbsArg const &arg, const char *funcName)
Print arguments of a function binding and name/address of the function pointer.
void streamCFunctionRef(TBuffer &R__b, TObject const &self, Func &ptr, Func dummyFunction, CFunctionMap< Func > &fmap)
Custom streamer for function pointer reference objects.
The namespace RooFit contains mostly switches that change the behaviour of functions of PDFs (or othe...
Definition CodegenImpl.h:73
@ ObjectHandling