|
[yoda-svn] r416 - in trunk: . pyext/yoda/includeblackhole at projects.hepforge.org blackhole at projects.hepforge.orgWed Dec 7 11:28:34 GMT 2011
Author: buckley Date: Wed Dec 7 11:28:34 2011 New Revision: 416 Log: Properly adding Dbn1D accessors for Histo1D. Modified: trunk/ChangeLog trunk/pyext/yoda/include/20-Dbn1D.pyx trunk/pyext/yoda/include/20-Dbn2D.pyx trunk/pyext/yoda/include/30-HistoBin1D.pyx trunk/pyext/yoda/include/40-Histo1D.pyx Modified: trunk/ChangeLog ============================================================================== --- trunk/ChangeLog Wed Dec 7 10:58:11 2011 (r415) +++ trunk/ChangeLog Wed Dec 7 11:28:34 2011 (r416) @@ -1,5 +1,7 @@ 2011-12-07 Andy Buckley <andy.buckley at cern.ch> + * Properly adding Dbn1D accessors for Histo1D. + * Updating the Cython mappings to provide the totalDbn() methods and add a placeholder mapping for Dbn2D. Completed mappings are needed for Dbn{1,2,3}D and the Profile types. Modified: trunk/pyext/yoda/include/20-Dbn1D.pyx ============================================================================== --- trunk/pyext/yoda/include/20-Dbn1D.pyx Wed Dec 7 10:58:11 2011 (r415) +++ trunk/pyext/yoda/include/20-Dbn1D.pyx Wed Dec 7 11:28:34 2011 (r416) @@ -1,3 +1,33 @@ cdef extern from "YODA/Dbn1D.h" namespace "YODA": cdef cppclass cDbn1D "Dbn1D": pass + + +cdef class Dbn1D: + cdef cDbn1D *thisptr + cdef bool _dealloc + + def __cinit__(self): + self._dealloc = False + + def __dealloc__(self): + if self._dealloc: + del self.thisptr + + cdef Dbn1D setptr(self, cDbn1D *ptr, bool dealloc): + if self._dealloc: + del self.thisptr + + self.thisptr = ptr + self._dealloc = dealloc + return self + + cdef cDbn1D* ptr(self): + return self.thisptr + + +cdef Dbn1D Dbn1D_fromptr(cDbn1D *ptr, dealloc=False): + # Construct a Python Dbn1D from a pointer to a cDbn1D, + # without calling __init__ and excessive memory allocation + cdef Dbn1D dbn = Dbn1D.__new__(Dbn1D) + return dbn.setptr(ptr, dealloc) Modified: trunk/pyext/yoda/include/20-Dbn2D.pyx ============================================================================== --- trunk/pyext/yoda/include/20-Dbn2D.pyx Wed Dec 7 10:58:11 2011 (r415) +++ trunk/pyext/yoda/include/20-Dbn2D.pyx Wed Dec 7 11:28:34 2011 (r416) @@ -1,3 +1,25 @@ cdef extern from "YODA/Dbn2D.h" namespace "YODA": - cdef cppclass cDbn2D "Dbn1D": + cdef cppclass cDbn2D "Dbn2D": pass + +cdef class Dbn2D: + cdef cDbn2D *thisptr + cdef bool _dealloc + + def __cinit__(self): + self._dealloc = False + + def __dealloc__(self): + if self._dealloc: + del self.thisptr + + cdef Dbn2D setptr(self, cDbn2D *ptr, bool dealloc): + if self._dealloc: + del self.thisptr + + self.thisptr = ptr + self._dealloc = dealloc + return self + + cdef cDbn2D* ptr(self): + return self.thisptr Modified: trunk/pyext/yoda/include/30-HistoBin1D.pyx ============================================================================== --- trunk/pyext/yoda/include/30-HistoBin1D.pyx Wed Dec 7 10:58:11 2011 (r415) +++ trunk/pyext/yoda/include/30-HistoBin1D.pyx Wed Dec 7 11:28:34 2011 (r416) @@ -15,7 +15,7 @@ void scaleW(double factor) void scaleX(double factor) - + double lowEdge() double highEdge() double width() @@ -32,11 +32,13 @@ double sumWX() double sumWX2() + #Ugly hack using shim header for Cython 0.13 cdef extern from "shims.h": cHistoBin1D add_HistoBin1D (cHistoBin1D &, cHistoBin1D &) cHistoBin1D subtract_HistoBin1D (cHistoBin1D &, cHistoBin1D &) + cdef class HistoBin1D: cdef cHistoBin1D *thisptr cdef bool _dealloc @@ -63,7 +65,7 @@ def lowEdge(self): """The lower of the two bin edges.""" return self.ptr().lowEdge() - + xMin = lowEdge @property @@ -138,10 +140,10 @@ def area(self): """ b.area <==> b.sumW - + The area of the bin is the sum of weights of the bin; it is independent of width. - + """ return self.ptr().area() @@ -149,10 +151,10 @@ def height(self): """ b.height <==> b.area / b.width - + The height of the bin is defined as the area divided by the width. - + """ return self.ptr().height() @@ -161,7 +163,7 @@ """ Error computed using binomial statistics on squared sum of bin weights, i.e. s.areaErr = sqrt(s.sumW2) - + """ return self.ptr().areaErr() @@ -180,25 +182,26 @@ bin.scaleX(factor) -> bin Scale the x-axis of `bin` in-place by `factor`. - + """ self.ptr().scaleX(factor) def scaleW(self, double factor): """ - bin.scaleW(factor) -> bin + bin.scaleW(factor) -> bin Scale `bin` in-place as if all weights were scaled by given `factor` """ self.ptr().scaleX(factor) + def __repr__(self): return 'HistoBin1D(%r)' % self.area + cdef HistoBin1D HistoBin1D_fromptr(cHistoBin1D *ptr, dealloc=False): # Construct a Python HistoBin1D from a pointer to a cHistoBin1D, # without calling __init__ and excessive memory allocation cdef HistoBin1D bin = HistoBin1D.__new__(HistoBin1D) return bin.setptr(ptr, dealloc) - Modified: trunk/pyext/yoda/include/40-Histo1D.pyx ============================================================================== --- trunk/pyext/yoda/include/40-Histo1D.pyx Wed Dec 7 10:58:11 2011 (r415) +++ trunk/pyext/yoda/include/40-Histo1D.pyx Wed Dec 7 11:28:34 2011 (r416) @@ -4,8 +4,7 @@ #cScatter2D operator / (cHisto1D &, cHisto1D &)""" cdef cppclass cHisto1D "YODA::Histo1D"(cAnalysisObject): - cHisto1D(size_t nbins, double lower, double upper, string &path, - string &title) + cHisto1D(size_t nbins, double lower, double upper, string &path, string &title) cHisto1D(vector[double] &binedges, string &path, string &title) cHisto1D(vector[double] &binedges) cHisto1D(cHisto1D &h, string &path) @@ -33,9 +32,11 @@ double integral(size_t a, size_t b) double sumW(bool includeoverflows) double sumW2(bool includeoverflows) + double mean(bool includeoverflows) double variance(bool includeoverflows) double stdDev(bool includeoverflows) + cdef extern from "shims.h": cHisto1D add_Histo1D (cHisto1D &, cHisto1D &) cHisto1D subtract_Histo1D (cHisto1D &, cHisto1D &) @@ -44,6 +45,7 @@ from cython.operator cimport dereference as deref + cdef class Histo1D(AnalysisObject): def __init__(self, *args, **kwargs): self._dealloc = True @@ -63,9 +65,11 @@ else: raise ValueError('Histo1D: Expected 3 arguments') + cdef cHisto1D* ptr(self): return <cHisto1D *> self.thisptr + def asScatter(self): """ h.asScatter() -> Scatter2D @@ -77,6 +81,7 @@ s[0] = Scatter2D_mkScatter(self.ptr()[0]) return Scatter2D_fromptr(s, True) + def fill(self, double x, double weight=1.0): """ h.fill(x[, weight=1.0]) -> self @@ -87,6 +92,7 @@ self.ptr().fill(x, weight) return self + def reset(self): """ h.reset() -> self @@ -97,6 +103,7 @@ self.ptr().reset() return self + def scaleW(self, double factor): """ s.scaleW(factor) -> self @@ -117,6 +124,7 @@ """ self.ptr().mergeBins(a, b) + def rebin(self, int n): """ s.rebin(n) -> self @@ -126,6 +134,7 @@ """ self.ptr().rebin(n) + @property def bins(self): """ @@ -137,7 +146,6 @@ """ - cdef size_t numbins = self.ptr().numBins() cdef size_t i cdef HistoBin1D bin @@ -151,6 +159,7 @@ return out + @property def lowEdge(self): """ @@ -161,6 +170,7 @@ """ return self.ptr().lowEdge() + @property def highEdge(self): """ @@ -171,6 +181,19 @@ """ return self.ptr().highEdge() + + @property + def totalDbn(self): + """ + h.totalDbn -> Distribution1D + + Return the Distribution1D object representing the total distribution. + + """ + cdef HistoBin1D bin + return Dbn1D_fromptr(&self.ptr().totalDbn()) + + @property def underflow(self): """ @@ -179,14 +202,30 @@ Return the Distribution1D object representing the underflow. """ - pass + cdef HistoBin1D bin + return Dbn1D_fromptr(&self.ptr().underflow()) + + + @property + def overflow(self): + """ + h.overflow -> Distribution1D + + Return the Distribution1D object representing the overflow. + + """ + cdef HistoBin1D bin + return Dbn1D_fromptr(&self.ptr().overflow()) + def __delitem__(self, size_t ix): self.ptr().eraseBin(ix) + def __getitem__(self, size_t ix): return HistoBin1D_fromptr(& self.ptr().bin(ix)) + def integral(self, bool overflows=True): """ s.integral([overflows]) -> float @@ -197,6 +236,7 @@ """ return self.ptr().integral(overflows) + def sumW(self, bool overflows=True): """ s.sumW([overflows]) -> float @@ -208,6 +248,7 @@ return self.ptr().sumW(overflows) + def sumW2(self, bool overflows=True): """ s.sumW2([overflows]) -> float @@ -218,6 +259,7 @@ """ return self.ptr().sumW2(overflows) + def mean(self, bool overflows=True): """ s.mean([overflows]) -> float @@ -226,6 +268,8 @@ bins. """ + return self.ptr().mean(overflows) + def variance(self, bool overflows=True): """ @@ -237,6 +281,7 @@ """ return self.ptr().variance(overflows) + def stdDev(self, bool overflows=True): """ s.stdDev([overflows]) -> float @@ -247,14 +292,17 @@ """ return self.ptr().stdDev(overflows) + def __add__(Histo1D a, Histo1D b): cdef cHisto1D *res = new cHisto1D(add_Histo1D(a.ptr()[0], b.ptr()[0])) return Histo1D_fromptr(res, True) + def __sub__(Histo1D a, Histo1D b): cdef cHisto1D *res = new cHisto1D(subtract_Histo1D(a.ptr()[0], b.ptr()[0])) return Histo1D_fromptr(res, True) + def __mul__(x, y): """ Scalar multiplication. Equivalent to scaleW acting on a copy. @@ -273,6 +321,7 @@ res.scaleW(factor) return Histo1D_fromptr(res, True) + def _div_scalar(Histo1D x, double y): if y == 0: raise ArithmeticError('Histo1D: Divide by zero scalar') @@ -282,10 +331,12 @@ res.scaleW(1.0 / y) return Histo1D_fromptr(res, True) + def _div_histo(Histo1D x, Histo1D y): cdef cScatter2D s = divide_Histo1D(x.ptr()[0], y.ptr()[0]) return Scatter2D_fromptr(&s) + def __div__(x, y): """ Division by scalar (i.e. multiplication by reciprocal) or another @@ -301,9 +352,11 @@ raise RuntimeError('Cannot multiply %r by %r' % (tx, ty)) + def __repr__(self): return 'Histo1D%r' % self.bins + cdef Histo1D Histo1D_fromptr(cHisto1D *ptr, bool dealloc=False): cdef Histo1D histo = Histo1D.__new__(Histo1D) return histo.setptr(ptr, dealloc)
More information about the yoda-svn mailing list |