Exemplo n.º 1
0
    def _plot(self, varexp, cut, options='', *args, **kwargs):
        '''
        Primitive method to produce histograms and projections
        '''

        if kwargs: print 'kwargs', kwargs
        dirsentry = utils.TH1AddDirSentry()
        sumsentry = utils.TH1Sumw2Sentry()
        options = 'goff ' + options
        self._log.debug('varexp:  \'%s\'', varexp)
        self._log.debug('cut:     \'%s\'', cut)
        self._log.debug('options: \'%s\'', options)

        n = self._chain.Draw(varexp, cut, options, *args, **kwargs)
        h = self._chain.GetHistogram()
        if h.__nonzero__():
            #             print h.GetDirectory()
            self._log.debug('entries  %d integral %f', h.GetEntries(),
                            h.Integral())
            h.Scale(self._scale)
            self._log.debug('scale:   %f integral %f', self._scale,
                            h.Integral())
            return h
        else:
            self._log.debug('entries  %d', n)
            return None
Exemplo n.º 2
0
    def yields(self, cut='', options='', *args, **kwargs):
        cut = self._cutexpr(cut)
        # DO add the histogram, and set sumw2 (why not using TH1::Sumw2()?
        dirsentry = utils.TH1AddDirSentry(True)
        sumsentry = utils.TH1Sumw2Sentry()
        # new name per call or? what about using always the same histogram?
        tname = 'counter'  #'counter_%s' % uuid.uuid1()
        # it might look like an overkill, but the double here helps
        counter = ROOT.TH1D(tname, tname, 1, 0., 1.)
        h = self._plot('0. >> ' + tname, cut, options, *args, **kwargs)

        xax = h.GetXaxis()
        err = ctypes.c_double(0.)
        int = h.IntegralAndError(xax.GetFirst(), xax.GetLast(), err)

        return Yield(int, err.value)
Exemplo n.º 3
0
    def _projexpr(name, bins=None):
        '''
        Prepares the target for plotting if the binning is standard (n,min,max)
        then return a string else, it's variable binning, make the htemp and
        return it
        '''
        if not bins:
            return 0, name, None
        elif not isinstance(bins, tuple):
            raise TypeError('bin must be an ntuple or an array')

        l = len(bins)
        # if the tuple is made of lists
        #         if l in [1,2] and all(map(lambda o: isinstance(o,list),bins)):
        if (l in [1, 2] and all(map(lambda o: isinstance(o, list),
                                    bins))) or (l in [3, 6]):
            dirsentry = utils.TH1AddDirSentry()
            sumsentry = utils.TH1Sumw2Sentry()

            # get the hshape
            hdim, hclass, hargs = _bins2hclass(bins)

            # make the histogram
            htemp = hclass(name, name, *hargs)

            return hdim, name, htemp

        else:
            # standard approach, the string goes into the expression
            # WARNING: this way the constuction of the histogram is delegated to TTree::Draw, which produces a TH1F
            # IT would be better to retain the control over the histogram type selection (i.e. being able to make TH1D always)
            # In order to do so, we need to mimik the with which TTree Draw creates its histograms:
            #   - x-check the dimention in varexp and projexp
            #   - initial xmin,xmax (and likewise for y)
            #   - default number of bins
            if l in [1]:
                # nx (free xmin, xmax)
                ndim = 1
            elif l in [4]:
                # nx,xmin,xmax,ny (free ymin,ymax)
                ndim = 2
            else:
                # only 1d or 2 d hist
                raise RuntimeError('What a mess!!! bin malformed!')

            hdef = '(' + ','.join([str(x) for x in bins]) + ')' if bins else ''
            return ndim, name + hdef, None