Exemple #1
0
def test_viewrear_hist_04_auto_threshold():
    """Test ViewRear on Histogram (auto start and len), enable threshold"""
    edges = N.arange(13, dtype='d')
    arr = N.zeros(edges.size-1, dtype=context.current_precision_short())

    ranges = [ (0, 3), (0, 12), (1, 3), (6, 6)]
    for rng in ranges:
        hist_main = C.Histogram(edges, arr)
        start, len = rng

        pedges = edges[start:start+len+1].copy()
        print('Original edges', pedges)
        pedges[0]  = 0.5*(pedges[:2].sum())
        pedges[-1] = 0.5*(pedges[-2:].sum())
        print('Modified edges', pedges)
        arr_sub = N.arange(start, start+len, dtype=arr.dtype)
        hist_sub=C.Histogram(pedges, arr_sub, True)

        view = C.ViewRear(hist_main);
        view.allowThreshold()
        hist_sub >> view.view.rear

        res = view.view.result.data()
        expect_edges = N.array(view.view.result.datatype().edges)
        expect = arr.copy()
        expect[start:start+len]=arr_sub
        print('Range', rng)
        print('Result', res)
        print('Expect', expect)
        print('Result (edges)', edges)
        print('Expect (edges)', expect_edges)
        print()
        assert (res==expect).all()
        assert (edges==expect_edges).all()
        assert (hist_main.single().data()==arr).all()
Exemple #2
0
def test_viewrear_hist_02_auto():
    """Test ViewRear on Histogram (determine start and length)"""
    edges = N.arange(13, dtype='d')
    arr = N.zeros(edges.size-1, dtype=context.current_precision_short())

    ranges = [ (0, 3), (0, 12), (1, 3), (6, 6), (6, 1)]
    for rng in ranges:
        print('Range', rng)
        hist_main = C.Histogram(edges, arr)
        start, len = rng
        pedges = edges[start:start+len+1]
        arr_sub = N.arange(start, start+len, dtype=arr.dtype)
        hist_sub=C.Histogram(pedges, arr_sub, True)

        view = C.ViewRear(hist_main);
        hist_sub >> view.view.rear

        res = view.view.result.data()
        expect_edges = N.array(view.view.result.datatype().edges)
        expect = arr.copy()
        expect[start:start+len]=arr_sub
        print('Result', res)
        print('Expect', expect)
        print('Result (edges)', edges)
        print('Expect (edges)', expect_edges)
        print()
        assert (res==expect).all()
        assert (edges==expect_edges).all()
        assert (hist_main.single().data()==arr).all()
Exemple #3
0
def test_viewrear_points_04_auto_fromhist_edges():
    """Test ViewRear on Histogram (determine start and length)"""
    edges = N.arange(13, dtype='d')
    arr = N.zeros(edges.size, dtype=context.current_precision_short())
    points_main = C.Points(arr)

    ranges = [ (0, 3), (0, 12), (1, 3), (6, 6), (6, 1)]
    for rng in ranges:
        print('Range', rng)
        hist_main = C.Histogram(edges, arr[:-1])
        start, len = rng
        pedges = edges[start:start+len+1]
        arr_sub = N.arange(start, start+len+1, dtype=arr.dtype)
        hist_sub=C.Histogram(pedges, arr_sub[:-1], True)

        points_sub=C.Points(arr_sub, True)

        view = C.ViewRear()
        view.determineOffset(hist_main, hist_sub, True)
        points_main >> view.view.original
        points_sub >> view.view.rear

        res = view.view.result.data()
        expect = arr.copy()
        expect[start:start+len+1]=arr_sub
        print('Result', res)
        print('Expect', expect)
        print()
        assert (res==expect).all()
Exemple #4
0
def test_viewrear_points_01_start_len(function_name):
    """Test ViewRear on Points (start, len)"""
    arr = N.zeros(12, dtype=context.current_precision_short())
    ns = env.env.globalns(function_name)
    names=[]
    for i in range(arr.size):
        name='val_%02i'%i
        names.append(name)
        ns.defparameter( name, central=i, fixed=True, label='Value %i'%i )

    ranges = [ (0, 3), (0, 12), (1, 3), (6, 6), (6, 1)]
    for rng in ranges:
        start, len = rng

        pview = arr[start:start+len]
        points = C.Points(arr)
        view = C.ViewRear(points, start, len);

        cnames = names[start:start+len]
        with ns:
            vararray = C.VarArray(cnames)

        vararray >> view.view.rear

        print('Range', rng)
        for ichange, iname in enumerate(['']+cnames, -1):
            if iname:
                print('  Change', ichange)
                par=ns[iname]
                par.set(par.value()+1.0)

            res0 = vararray.single().data()
            res = view.view.result.data()
            expect = arr.copy()
            for i in range(start, start+len):
                expect[i] = ns[names[i]].value()

            expect0 = []
            for i in range(start, start+len):
                expect0.append(ns[names[i]].value())

            print('    Result 0', res0)
            print('    Expect 0', expect0)
            print('    Result', res)
            print('    Expect', expect)
            print('    Original data', points.single().data())
            print('    Original data (expect)', arr)

            assert (res==expect).all()
            assert (res0==expect0).all()
            assert (res[start:start+len]==res0).all()
            assert (points.single().data()==arr).all()
            print()
Exemple #5
0
    def build(self):
        with entryContext(subgraph='LSNL'):
            self.init_data()

            #
            # Initialize bin edges
            #
            self.histoffset = C.HistEdgesOffset(
                self.doubleme, labels='Offset/threshold bin edges (Evis, Te+)')
            histedges = self.histoffset.histedges

            # Declare open input
            self.set_input('evis_edges_hist',
                           None,
                           histedges.hist_in,
                           argument_number=0)

            histedges.points.setLabel('Evis (full range)')
            histedges.points_truncated.setLabel('Evis (truncated)')
            histedges.points_threshold.setLabel('Evis (threshold)')
            histedges.points_offset.setLabel('Te+')
            histedges.hist_truncated.setLabel('Hist Evis (truncated)')
            histedges.hist_threshold.setLabel('Hist Evis (threshold)')
            histedges.hist_offset.setLabel('Hist Te+')

            #
            # Birk's model integration
            #
            birks_e_input, birks_quenching_input = self.stopping_power[
                'e'], self.stopping_power['dedx']
            self.birks_e_p, self.birks_quenching_p = C.Points(
                birks_e_input,
                labels='Te (input)'), C.Points(birks_quenching_input,
                                               labels='Stopping power (dE/dx)')

            birksns = self.namespace('birks')
            with birksns:
                self.birks_integrand_raw = C.PolyRatio(
                    [],
                    list(sorted(birksns.storage.keys())),
                    labels="Birk's integrand")
            self.birks_quenching_p >> self.birks_integrand_raw.polyratio.points

            self.doubleemass_point = C.Points([-self.doubleme],
                                              labels='2me offset')

            self.integrator_ekin = C.IntegratorGL(
                self.histoffset.histedges.hist_offset,
                self.cfg.integration_order,
                labels=('Te sampler (GL)', "Birk's integrator (GL)"))

            self.birks_integrand_interpolator = C.InterpLogx(
                self.birks_e_p,
                self.integrator_ekin.points.x,
                labels=("Birk's InSegment", "Birk's interpolator"))
            self.birks_integrand_interpolated = self.birks_integrand_interpolator.add_input(
                self.birks_integrand_raw.polyratio.ratio)
            self.birks_integral = self.integrator_ekin.add_input(
                self.birks_integrand_interpolated)

            self.birks_accumulator = C.PartialSum(0.,
                                                  labels="Birk's Evis|[MeV]")
            self.birks_integral >> self.birks_accumulator.reduction

            #
            # Cherenkov model
            #
            with self.namespace('cherenkov'):
                self.cherenkov = C.Cherenkov_Borexino(labels='Npe Cherenkov')
            self.histoffset.histedges.points_offset >> self.cherenkov.cherenkov

            #
            # Electron energy model
            #
            with self.namespace:
                self.electron_model = C.WeightedSum(
                    ['kC', 'Npescint'], [
                        self.cherenkov.cherenkov.ch_npe,
                        self.birks_accumulator.reduction.out
                    ],
                    labels='Npe: electron responce')

            #
            # 2 511 keV gamma model
            #
            self.annihilation_electrons_centers = C.Points(
                self.annihilation_electrons_centers_input,
                labels='Annihilation gamma E centers')
            self.annihilation_electrons_p = C.Points(
                self.annihilation_electrons_p_input,
                labels='Annihilation gamma weights')

            self.view_lowe = C.ViewHistBased(
                self.histoffset.histedges.hist_offset,
                0.0,
                self.annihilation_electrons_edges_input[-1],
                labels=('Low E indices', 'Low E view'))
            self.ekin_edges_lowe = self.view_lowe.add_input(
                self.histoffset.histedges.points_offset)
            self.electron_model_lowe = self.view_lowe.add_input(
                self.electron_model.single())

            self.ekin_edges_lowe.setLabel('Te+ edges (low Te view)')
            self.electron_model_lowe.setLabel(
                'Npe: electron responce (low Te view)')

            self.electron_model_lowe_interpolator = C.InterpLinear(
                self.ekin_edges_lowe,
                self.annihilation_electrons_centers,
                labels=('Annihilation E InSegment',
                        'Annihilation gamma interpolator'))
            self.electron_model_lowe_interpolated = self.electron_model_lowe_interpolator.add_input(
                self.electron_model_lowe)

            with self.namespace:
                self.npe_positron_offset = C.Convolution(
                    'ngamma', labels='e+e- annihilation Evis [MeV]')
                self.electron_model_lowe_interpolated >> self.npe_positron_offset.normconvolution.fcn
                self.annihilation_electrons_p >> self.npe_positron_offset.normconvolution.weights

            #
            # Total positron model
            #
            self.positron_model = C.SumBroadcast(
                outputs=[
                    self.electron_model.sum.sum,
                    self.npe_positron_offset.normconvolution.result
                ],
                labels='Npe: positron responce')

            self.positron_model_scaled = C.FixedPointScale(
                self.histoffset.histedges.points_truncated,
                self.namespace['normalizationEnergy'],
                labels=('Fixed point index',
                        'Positron energy model|Evis, MeV'))
            self.positron_model_scaled = self.positron_model_scaled.add_input(
                self.positron_model.sum.outputs[0])
            self.positron_model_scaled_full_view = C.ViewRear(
                -1.0, labels='Positron Energy nonlinearity|full range')
            self.positron_model_scaled_full_view.determineOffset(
                self.histoffset.histedges.hist,
                self.histoffset.histedges.hist_truncated, True)
            self.histoffset.histedges.points >> self.positron_model_scaled_full_view.view.original
            self.positron_model_scaled >> self.positron_model_scaled_full_view.view.rear
            self.positron_model_scaled_full = self.positron_model_scaled_full_view.view.result

            #
            # Relative positron model
            #
            self.positron_model_relative = C.Ratio(
                self.positron_model_scaled,
                self.histoffset.histedges.points_truncated,
                labels='Positron energy nonlinearity')
            self.positron_model_relative_full_view = C.ViewRear(
                0.0, labels='Positron Energy nonlinearity|full range')
            self.positron_model_relative_full_view.determineOffset(
                self.histoffset.histedges.hist,
                self.histoffset.histedges.hist_truncated, True)
            self.histoffset.histedges.points >> self.positron_model_relative_full_view.view.original
            self.positron_model_relative >> self.positron_model_relative_full_view.view.rear
            self.positron_model_relative_full = self.positron_model_relative_full_view.view.result

        #
        # Hist Smear
        #
        self.pm_histsmear = C.HistNonlinearity(
            self.cfg.get('fill_matrix', False),
            labels=('Nonlinearity matrix', 'Nonlinearity smearing'))
        self.pm_histsmear.set_range(-0.5, 20.0)
        self.positron_model_scaled_full >> self.pm_histsmear.matrix.EdgesModified

        self.histoffset.histedges.hist >> self.pm_histsmear.matrix.Edges

        trans = self.pm_histsmear.transformations.back()
        for i, it in enumerate(self.nidx.iterate()):
            # if i:
            # trans = self.pm_histsmear.add_transformation()
            inp = self.pm_histsmear.add_input()

            trans.setLabel(
                it.current_format('Nonlinearity smearing {autoindex}'))

            self.set_input('lsnl', it, inp, argument_number=0)
            self.set_output('lsnl', it, trans.outputs.back())