Ejemplo n.º 1
0
def unpack_hist1(output, dtype, kwargs={}):
    dtype = dtype or output.datatype()
    data = output.data()

    edges = np.array(dtype.edgesNd[0], dtype='d')
    widths = edges[1:] - edges[:-1]
    rel_offsets = np.fabs(widths - widths[0]) / widths.max()

    name = kwargs.pop('name', '')
    title = kwargs.pop('label', kwargs.pop('title', ''))

    if (rel_offsets < 1.e-9).all():
        # Constant width histogram
        hist = R.TH1D(name, title, edges.size - 1, edges[0], edges[-1])
    else:
        hist = R.TH1D(name, title, edges.size - 1, edges)

    buffer = root2numpy.get_buffer_hist1(hist)
    buffer[:] = data
    hist.SetEntries(data.sum())

    set_axes(hist, kwargs)

    if kwargs:
        raise Exception('Unparsed options in extra arguments for TH1D')

    return hist
Ejemplo n.º 2
0
    def build(self):
        file = R.TFile(self.cfg.filename, 'READ')
        if file.IsZombie():
            raise Exception('Can not read ROOT file ' + file.GetName())

        print('Read input file {}:'.format(file.GetName()))

        normalize = self.cfg.get('normalize', False)
        for it in self.nidx.iterate():
            if it.ndim() > 0:
                subst, = it.current_values()
            else:
                subst = ''
            hname = self.groups.format(subst, self.cfg.format)
            h = file.Get(hname)
            if not h:
                raise Exception('Can not read {hist} from {file}'.format(
                    hist=hname, file=file.GetName()))

            print('  read{}: {}'.format(' ' + subst, hname), end=' ')

            edges = get_bin_edges_axis(h.GetXaxis())
            data = get_buffer_hist1(h)
            data = N.ascontiguousarray(data, dtype='d')
            if isinstance(normalize, slice):
                print('[normalized]', end=' ')
                data = data / data[normalize].sum()
            elif normalize:
                print('[normalized]', end=' ')
                data = data / data.sum()
            else:
                print()

            xscale = self.cfg.get('xscale', None)
            yscale = self.cfg.get('yscale', None)
            if xscale is not None:
                print('[xscale]', end=' ')
                edges *= xscale
            if yscale is not None:
                print('[yscale]', end=' ')
                data *= yscale

            print()

            fmt = self.cfg.get('label', 'hist {name}\n{autoindex}')
            hist = Histogram(edges,
                             data,
                             labels=it.current_format(fmt, name=self.cfg.name))
            self.set_output(self.cfg.name, it, hist.single())

            self.context.objects[('hist', subst)] = hist

        file.Close()
Ejemplo n.º 3
0
    def init_data(self):
        dtype_spower = [('e', 'd'), ('temp1', 'd'), ('temp2', 'd'),
                        ('dedx', 'd')]
        self.stopping_power = N.loadtxt(self.cfg.stopping_power,
                                        dtype=dtype_spower)

        from mpl_tools.root2numpy import get_buffer_hist1, get_bin_edges_axis
        cfg = self.cfg.annihilation_electrons
        file = R.TFile(cfg.file, 'read')
        hist = file.Get(cfg.histogram)
        buf = get_buffer_hist1(hist).copy()
        buf *= cfg.scale
        edges = get_bin_edges_axis(hist.GetXaxis())

        self.annihilation_electrons_p_input = buf
        self.annihilation_electrons_edges_input = edges
        self.annihilation_electrons_centers_input = 0.5 * (edges[1:] +
                                                           edges[:-1])

        file.Close()