File: RooCFunction3Binding.h

package info (click to toggle)
root-system 5.34.19%2Bdfsg-1.2
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 210,548 kB
  • ctags: 209,442
  • sloc: cpp: 1,488,197; ansic: 345,435; sh: 19,468; fortran: 12,570; makefile: 7,873; python: 7,683; xml: 555; ruby: 553; csh: 356; perl: 329; objc: 38; sql: 14; tcl: 4
file content (374 lines) | stat: -rw-r--r-- 13,168 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/*****************************************************************************
 * Project: RooFit                                                           *
 * Package: RooFitCore                                                       *
 *    File: $Id$
 * Authors:                                                                   *   WV, Wouter Verkerke, NIKHEF, verkerke@nikhef.nl                         *
 *                                                                           *
 * Copyright (c) 2000-2008, NIKHEF, Regents of the University of California  *
 *                          and Stanford University. All rights reserved.    *
 *                                                                           *
 *****************************************************************************/

#ifndef ROOCFUNCTION3BINDING
#define ROOCFUNCTION3BINDING

#include "TString.h"
#include "RooAbsReal.h"
#include "RooRealProxy.h"
#include "RooMsgService.h"
#include "RooAbsPdf.h"
#include <string>
#include <map>
#include <vector>


namespace RooFit {

typedef Double_t (*CFUNCD3DDD)(Double_t,Double_t,Double_t) ;
typedef Double_t (*CFUNCD3DDB)(Double_t,Double_t,Bool_t) ;
typedef Double_t (*CFUNCD3DII)(Double_t,Int_t,Int_t) ;
typedef Double_t (*CFUNCD3UDU)(UInt_t,Double_t,UInt_t) ;
typedef Double_t (*CFUNCD3UDD)(UInt_t,Double_t,Double_t) ;
typedef Double_t (*CFUNCD3UUD)(UInt_t,UInt_t,Double_t) ;

RooAbsReal* bindFunction(const char* name,CFUNCD3DDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsReal* bindFunction(const char* name,CFUNCD3DDB func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsReal* bindFunction(const char* name,CFUNCD3DII func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsReal* bindFunction(const char* name,CFUNCD3UDU func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsReal* bindFunction(const char* name,CFUNCD3UDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsReal* bindFunction(const char* name,CFUNCD3UUD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3DDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3DDB func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3DII func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3UDU func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3UDD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;
RooAbsPdf* bindPdf(const char* name,CFUNCD3UUD func,RooAbsReal& x, RooAbsReal& y, RooAbsReal& z) ;

}

template<class VO, class VI1, class VI2, class VI3>
class RooCFunction3Map {
 public:
  RooCFunction3Map() {} ;

  void add(const char* name, VO (*ptr)(VI1,VI2,VI3), const char* arg1name="x", const char* arg2name="y", const char* arg3name="z") {
    // Register function with given name and argument name
    _ptrmap[name] = ptr ;
    _namemap[ptr] = name ;
    _argnamemap[ptr].push_back(arg1name) ;
    _argnamemap[ptr].push_back(arg2name) ;
    _argnamemap[ptr].push_back(arg3name) ;
  }
  

  const char* lookupName(VO (*ptr)(VI1,VI2,VI3)) {
    // Return name of function given by pointer
    return _namemap[ptr].c_str() ;
  }
    
  VO (*lookupPtr(const char* name))(VI1,VI2,VI3) {
    // Return pointer of function given by name
    return _ptrmap[name] ;
  }

  const char* lookupArgName(VO (*ptr)(VI1,VI2,VI3), UInt_t iarg) {
    // Return name of i-th argument of function. If function is
    // not registered, argument names 0,1,2 are x,y,z
    if (iarg<_argnamemap[ptr].size()) {
      return (_argnamemap[ptr])[iarg].c_str() ;
    }
    switch (iarg) {
    case 0: return "x" ;
    case 1: return "y" ;
    case 2: return "z" ;
    }
    return "w" ;
  }
  
 private:

#ifndef __CINT__
  std::map<std::string,VO (*)(VI1,VI2,VI3)> _ptrmap ; // Pointer-to-name map
  std::map<VO (*)(VI1,VI2,VI3),std::string> _namemap ; // Name-to-pointer map
  std::map<VO (*)(VI1,VI2,VI3),std::vector<std::string> > _argnamemap ; // Pointer-to-argnamelist map
#endif
} ;



template<class VO, class VI1, class VI2, class VI3>
class RooCFunction3Ref : public TObject {
 public:
  RooCFunction3Ref(VO (*ptr)(VI1,VI2,VI3)=0) : _ptr(ptr) {
    // Constructor of persistable function reference
  } ;
  ~RooCFunction3Ref() {} ;

  VO operator()(VI1 x,VI2 y, VI3 z) const {
    // Evaluate embedded function
    return (*_ptr)(x,y,z) ;
  }

  const char* name() const {
    // Return registered name of embedded function. If function
    // is not registered return string with hex presentation
    // of function pointer value
    const char* result = fmap().lookupName(_ptr) ;
    if (result && strlen(result)) {
      return result ;
    } 
    // This union is to avoid a warning message:
    union { 
       void *_ptr;
       func_t _funcptr;
    } temp;
    temp._funcptr = _ptr;
    return Form("(%p)",temp._ptr) ;
  }

  const char* argName(Int_t iarg) {
    // Return suggested name for i-th argument
    return fmap().lookupArgName(_ptr,iarg) ;    
  }

  static RooCFunction3Map<VO,VI1,VI2,VI3>& fmap() {
    // Return reference to function pointer-to-name mapping service
    if (!_fmap) {
      _fmap = new RooCFunction3Map<VO,VI1,VI2,VI3> ;
    }
    return *_fmap ;
  }

 private:

  static VO dummyFunction(VI1,VI2,VI3) {
    // Dummy function used when registered function was not
    // found in un-persisting object
    return 0 ;
  }


  typedef VO (*func_t)(VI1,VI2,VI3) ; //! Pointer to embedded function
  func_t _ptr; //! Pointer to embedded function

  static RooCFunction3Map<VO,VI1,VI2,VI3>* _fmap ; // Pointer to mapping service object

  ClassDef(RooCFunction3Ref,1) // Persistable reference to C function pointer
} ;



template<class VO, class VI1, class VI2, class VI3>
void RooCFunction3Ref<VO,VI1,VI2,VI3>::Streamer(TBuffer &R__b)
{
  // Custom streamer for function pointer reference object. When writing,
  // the function pointer is substituted by its registered name. When function
  // is unregistered name 'UNKNOWN' is written and a warning is issues. When
  // reading back, the embedded name is converted back to a function pointer
  // using the mapping service. When name UNKNOWN is encountered a warning is
  // issues and a dummy null function is substituted. When the registered function
  // name can not be mapped to a function pointer an ERROR is issued and a pointer
  // to the dummy null function is substituted

  typedef ::RooCFunction3Ref<VO,VI1,VI2,VI3> thisClass;

   // Stream an object of class RooCFunction3Ref
   if (R__b.IsReading()) {

     UInt_t R__s, R__c;
     Version_t R__v = R__b.ReadVersion(&R__s, &R__c);      

     // Read name from file
     TString tmpName ;
     tmpName.Streamer(R__b) ;       

     if (tmpName=="UNKNOWN" && R__v>0) {

       coutW(ObjectHandling) << "WARNING: Objected embeds function pointer to unknown function, object will not be functional" << std::endl ;
       _ptr = dummyFunction ;

     } else {
     
       // Lookup pointer to C function wih given name
       _ptr = fmap().lookupPtr(tmpName.Data()) ;

       if (_ptr==0) {
	 coutW(ObjectHandling) << "ERROR: Objected embeds pointer to function named " << tmpName 
			       << " but no such function is registered, object will not be functional" << std::endl ;
       }
     }


     R__b.CheckByteCount(R__s, R__c, thisClass::IsA());

   } else {
     
     UInt_t R__c;
     R__c = R__b.WriteVersion(thisClass::IsA(), kTRUE);    

     // Lookup name of reference C function
     TString tmpName = fmap().lookupName(_ptr) ;
     if (tmpName.Length()==0) {
       // This union is to avoid a warning message:
       union { 
          void *_ptr;
          func_t _funcptr;
       } temp;
       temp._funcptr = _ptr;
       coutW(ObjectHandling) << "WARNING: Cannot persist unknown function pointer " << Form("%p",temp._ptr) 
			     << " written object will not be functional when read back" <<  std::endl ;
       tmpName="UNKNOWN" ;
     } 
     
     // Persist the name
     tmpName.Streamer(R__b) ;            

     R__b.SetByteCount(R__c, kTRUE);
     
   }
}



template<class VO,class VI1, class VI2, class VI3>
class RooCFunction3Binding : public RooAbsReal {
public:
  RooCFunction3Binding() {
    // Default constructor
  } ; 
  RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
  RooCFunction3Binding(const RooCFunction3Binding& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooCFunction3Binding(*this,newname); }
  inline virtual ~RooCFunction3Binding() { }

  void printArgs(std::ostream& os) const {
    // Print object arguments and name/address of function pointer
    os << "[ function=" << func.name() << " " ;    
    for (Int_t i=0 ; i<numProxies() ; i++) {
      RooAbsProxy* p = getProxy(i) ;
      if (!TString(p->name()).BeginsWith("!")) {
	p->print(os) ;
	os << " " ;
      }
    }    
    os << "]" ;  
  }

protected:

  RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
  RooRealProxy x ;              // Argument reference
  RooRealProxy y ;              // Argument reference
  RooRealProxy z ;              // Argument reference
  
  Double_t evaluate() const {
    // Return value of embedded function using value of referenced variable x
    return func(x,y,z) ;
  }

private:

  ClassDef(RooCFunction3Binding,1) // RooAbsReal binding to external C functions
};



template<class VO,class VI1, class VI2, class VI3>
RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), 
						       RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
  RooAbsReal(name,title), 
  func(_func),
  x(func.argName(0),func.argName(0),this,_x),
  y(func.argName(1),func.argName(1),this,_y),
  z(func.argName(2),func.argName(2),this,_z)
{ 
  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
  // a RooWorkspace
} 


template<class VO,class VI1, class VI2, class VI3>
RooCFunction3Binding<VO,VI1,VI2,VI3>::RooCFunction3Binding(const RooCFunction3Binding& other, const char* name) :  
  RooAbsReal(other,name), 
  func(other.func),
  x("x",this,other.x),
  y("y",this,other.y),
  z("z",this,other.z)
{ 
  // Copy constructor
} 


template<class VO,class VI1, class VI2, class VI3>
class RooCFunction3PdfBinding : public RooAbsPdf {
public:
  RooCFunction3PdfBinding() {
    // Default constructor
  } ; 
  RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z);
  RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name=0) ;
  virtual TObject* clone(const char* newname) const { return new RooCFunction3PdfBinding(*this,newname); }
  inline virtual ~RooCFunction3PdfBinding() { }

  void printArgs(std::ostream& os) const {
    // Print object arguments and name/address of function pointer
    os << "[ function=" << func.name() << " " ;    
    for (Int_t i=0 ; i<numProxies() ; i++) {
      RooAbsProxy* p = getProxy(i) ;
      if (!TString(p->name()).BeginsWith("!")) {
	p->print(os) ;
	os << " " ;
      }
    }    
    os << "]" ;  
  }

protected:

  RooCFunction3Ref<VO,VI1,VI2,VI3> func ; // Function pointer reference
  RooRealProxy x ;              // Argument reference
  RooRealProxy y ;              // Argument reference
  RooRealProxy z ;              // Argument reference
  
  Double_t evaluate() const {
    // Return value of embedded function using value of referenced variable x
    return func(x,y,z) ;
  }

private:

  ClassDef(RooCFunction3PdfBinding,1) // RooAbsReal binding to external C functions
};



template<class VO,class VI1, class VI2, class VI3>
RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const char *name, const char *title, VO (*_func)(VI1,VI2,VI3), 
						       RooAbsReal& _x, RooAbsReal& _y, RooAbsReal& _z) :
  RooAbsPdf(name,title), 
  func(_func),
  x(func.argName(0),func.argName(0),this,_x),
  y(func.argName(1),func.argName(1),this,_y),
  z(func.argName(2),func.argName(2),this,_z)
{ 
  // Constructor of C function binding object given a pointer to a function and a RooRealVar to which the function
  // argument should be bound. This object is fully functional as a RooFit function object. The only restriction is
  // if the referenced function is _not_ a standard ROOT TMath or MathCore function it can not be persisted in a
  // a RooWorkspace
} 


template<class VO,class VI1, class VI2, class VI3>
RooCFunction3PdfBinding<VO,VI1,VI2,VI3>::RooCFunction3PdfBinding(const RooCFunction3PdfBinding& other, const char* name) :  
  RooAbsPdf(other,name), 
  func(other.func),
  x("x",this,other.x),
  y("y",this,other.y),
  z("z",this,other.z)
{ 
  // Copy constructor
} 

#endif