Exemplo n.º 1
0
    def test(self):
        global filename
        if filename is None:
            filename = corsika.example_data_dir + '/DAT000002-32'
        assert os.path.exists(filename)

        raw = corsika.RawStream(filename)
        block = corsika.Block()

        # get the run header, event header and first particle block
        raw.get_next_block(block)
        assert block.ID == 'RUNH'
        raw.get_next_block(block)
        assert block.ID == 'EVTH'
        raw.get_next_block(block)
        assert numpy.all(numpy.isclose(reference, block.data.reshape((-1, 7))))
        n_blocks = 3
        while raw.get_next_block(block):
            n_blocks += 1

        # check total number of blocks
        assert n_blocks == 4725
        raw.close()

        # check particle iteration
        raw = corsika.RawStream(filename)
        raw.get_next_block(block)
        raw.get_next_block(block)
        particles = raw.particles(
        )  # this works because it is positioned right before the particle block
        for i, p in enumerate(raw.particles()):
            if i >= reference.shape[0]: break
            assert numpy.all(
                numpy.isclose([p.px, p.py, p.pz, p.x, p.y, p.t_or_z],
                              reference[i][1:], 3))
Exemplo n.º 2
0
import sys
try:
    import icecube
    within_icecube = True
except:
    within_icecube = False

if within_icecube: from icecube import corsika
else: import corsika

if len(sys.argv) > 1:
    filename = sys.argv[1]
else:
    filename = corsika.example_data_dir + '/DAT000002-32'
raw = corsika.RawStream(filename)

block = corsika.Block()

raw.get_next_block(block)
print block.ID
raw.get_next_block(block)
print block.ID

particles = raw.particles()
p = particles.next()

n_blocks = 2
while raw.get_next_block(block):
    n_blocks += 1
Exemplo n.º 3
0
    def test(self):
        global files
        if not files:
            files = [
                corsika.example_data_dir + '/DAT000011-proton-EHISTORY-MUPROD'
            ]
        for f in files:
            if not os.path.exists(f): print f, 'does not exist'
            assert os.path.exists(f)

        for f in files:
            print '\n', f
            raw = corsika.RawStream(f)
            block = corsika.Block()
            raw.get_next_block(block)
            raw.get_next_block(block)
            particles = raw.particles()
            descriptions = numpy.array([
                int(p.description / 1000) for p in particles
                if int(p.description / 1000) != 0
            ])

            exclude = []
            ehistory = (not numpy.all(descriptions > 0))
            if ehistory:
                print 'File with EHISTORY option'
            if 75 in descriptions or 76 in descriptions:
                print 'File with MUADDI option'
            if 95 in descriptions or 96 in descriptions:
                print 'File with MUPROD option'

            muaddi = numpy.where((descriptions == 75) +
                                 (descriptions == 76))[0]
            if sum(muaddi):
                print 'Muon additional information:', set(descriptions[muaddi])
                exclude += [muaddi]
                if ehistory:
                    print '               grandparents:', set(
                        descriptions[muaddi + 1])
                    print '                    parents:', set(
                        descriptions[muaddi + 2])
                    exclude += [muaddi + 1, muaddi + 2]
                    assert sorted(set(descriptions[muaddi + 3])) == [5, 6]
                else:
                    assert sorted(set(descriptions[muaddi + 1])) == [5, 6]

            muprod = numpy.where((descriptions == 85) +
                                 (descriptions == 86))[0]
            if sum(muprod):
                print 'Decaying muons:             ', set(descriptions[muprod])
                exclude += [muprod]
                if ehistory:
                    print '               grandparents:', set(
                        descriptions[muprod + 1])
                    print '                    parents:', set(
                        descriptions[muprod + 2])
                    exclude += [muprod + 1, muprod + 2]
                    assert sorted(set(descriptions[muaddi + 3])) == [5, 6]
                else:
                    assert sorted(set(descriptions[muaddi + 1])) == [5, 6]

            m = numpy.ones_like(descriptions, dtype=bool)
            m[numpy.concatenate(exclude)] = False
            print 'the rest:                   ', set(descriptions[m])
Exemplo n.º 4
0
    def test(self):
        global filename
        if filename is None:
            filename = corsika.example_data_dir + '/DAT000011-proton-EHISTORY-MUPROD'
        assert os.path.exists(filename)

        #print 'opening', filename
        f = corsika.ShowerFile(filename)
        f.find_event(1)
        shower = f.current_shower
        particle_it = shower.particles

        raw = corsika.RawStream(filename)
        block = corsika.Block()
        raw.get_next_block(block)  # RUNH
        raw.get_next_block(block)  # EVTH
        raw_particle_it = raw.particles()

        raw_particles = numpy.zeros(len([p for p in raw_particle_it]),
                                    dtype=raw_ptype)
        raw_particle_it.rewind()
        i = 0
        for p in raw_particle_it:
            raw_particle2dtype(p, raw_particles[i])
            i += 1

        particle_it = shower.particles
        l = len([p for p in particle_it])
        particles = numpy.zeros(l, dtype=ptype)
        parents = numpy.zeros(l, dtype=ptype)
        grand_parents = numpy.zeros(l, dtype=ptype)
        parents['code'] = -99999999999
        grand_parents['code'] = -99999999999
        particle_it.rewind()
        i = 0
        for p in particle_it:
            particle2dtype(p, particles[i])
            if p.has_parent:
                particle2dtype(p.parent, parents[i])
                particle2dtype(p.grand_parent, grand_parents[i])
            i += 1

        parent_mask = parents['code'] != -99999999999
        grandparent_mask = grand_parents['code'] != -99999999999

        muon_mask = (particles['code'] == 6) + (particles['code'] == 5) + (
            particles['code'] == 95) + (particles['code'] == 96)
        electron_mask = (particles['code'] == 3) + (particles['code'] == 2)
        gamma_mask = (particles['code'] == 1)

        raw_muon_mask = (raw_particles['code'] == 6) + (
            raw_particles['code'] == 5) + (raw_particles['code'] == 95) + (
                raw_particles['code'] == 96)
        raw_electron_mask = (raw_particles['code']
                             == 3) + (raw_particles['code'] == 2)
        raw_gamma_mask = (raw_particles['code'] == 1)

        # the following raw entries are filtered by the high-level interface:
        raw_invalid_mask = raw_particles[
            'description'] == 0  # these are trailing particles, between the last particle and the end of the block
        raw_history_mask = raw_particles[
            'description'] < 0  # these are EHISTORY particles, which are accessed as members of high-level particles
        raw_undefined_mask = raw_particles[
            'pdg'] == 0  # these entries have no entry in the particle table (very rare particles)

        muons = particles[muon_mask]
        raw_muons = raw_particles[raw_muon_mask * ~raw_invalid_mask *
                                  ~raw_history_mask * ~raw_undefined_mask *
                                  (raw_particles['level'] == 1)]

        def test(condition, message):
            if not condition: print 'FAIL:', message
            assert test

        def test_equal(left, right, message):
            if not left == right:
                print 'FAIL:', message, ":", left, "!=", right
            assert left == right

        test_equal(len(raw_particles), 294645, "total number of raw particles")

        test_equal(len(particles), 254188, "total number of particles")

        test_equal(
            len(particles),
            len(raw_particles[~raw_invalid_mask * ~raw_history_mask *
                              ~raw_undefined_mask *
                              (raw_particles['level'] == 1)]),
            "discrepancy in number of particles")

        test_equal(
            len(raw_particles[raw_muon_mask * ~raw_invalid_mask *
                              ~raw_history_mask * ~raw_undefined_mask *
                              (raw_particles['level'] == 1)]),
            len(particles[muon_mask]), "discrepancy in number of muons")

        test_equal(
            len(raw_particles[raw_electron_mask * ~raw_invalid_mask *
                              ~raw_history_mask * ~raw_undefined_mask *
                              (raw_particles['level'] == 1)]),
            len(particles[electron_mask]),
            "discrepancy in number of electrons")

        test_equal(
            len(raw_particles[raw_gamma_mask * ~raw_invalid_mask *
                              ~raw_history_mask * ~raw_undefined_mask *
                              (raw_particles['level'] == 1)]),
            len(particles[gamma_mask]), "discrepancy in number of gammas")

        test_equal(len(particles[parent_mask * muon_mask]),
                   len(particles[muon_mask]),
                   "discrepancy in number of muons and muon parents")

        test(numpy.all(raw_muons['x'] == muons['x']), 'x differs')
        test(numpy.all(raw_muons['y'] == muons['y']), 'y differs')
        test(numpy.all(raw_muons['px'] == muons['px']), 'px differs')
        test(numpy.all(raw_muons['py'] == muons['py']), 'py differs')

        t_offset = numpy.mean(
            raw_muons['z_or_t'] -
            muons['z_or_t'])  # particle times might be shifted
        test(
            numpy.all(
                numpy.abs(raw_muons['z_or_t'] - muons['z_or_t'] -
                          t_offset) < 1e-1), 'T or z differs')
Exemplo n.º 5
0
except:
    within_icecube = False

if within_icecube: from icecube import corsika
else: import corsika

import sys

if len(sys.argv) > 1:
    files = sys.argv[1:]
else:
    files = [corsika.example_data_dir + '/DAT000011-proton-EHISTORY-MUPROD']

for f in files:
    print '\n', f
    raw = corsika.RawStream(f)
    block = corsika.Block()
    raw.get_next_block(block)
    raw.get_next_block(block)
    particles = raw.particles()
    descriptions = numpy.array([
        int(p.description / 1000) for p in particles
        if int(p.description / 1000) != 0
    ])

    exclude = []
    ehistory = (not numpy.all(descriptions > 0))
    if ehistory:
        print 'File with EHISTORY option'
    if 75 in descriptions or 76 in descriptions:
        print 'File with MUADDI option'