Ejemplo n.º 1
0
    def create_event(self):
        """
        Generate a neutrino event in the ice volume.

        Creates a neutrino with a random vertex in the volume, a random
        direction, and an energy based on ``get_energy``. Particle type is
        randomly chosen, and its interaction type is also randomly chosen based
        on the branching ratio. Weights the particles according to their
        survival probability through the Earth and their probability of
        interacting in the ice at their vertex. If Earth shadowing has been
        turned on then particles which don't survive transit through the Earth
        are skipped, and surviving particles are given a survival weight of 1.
        Currently each `Event` returned consists of only a single `Particle`.

        Returns
        -------
        Event
            Random neutrino event not shadowed by the Earth.

        See Also
        --------
        pyrex.Event : Class for storing a tree of `Particle` objects
                      representing an event.
        pyrex.Particle : Class for storing particle attributes.

        """
        self.count += 1
        vtx = self.get_vertex()
        u = self.get_direction()
        E = self.get_energy()
        particle_id = self.get_particle_type()
        particle = Particle(particle_id=particle_id,
                            vertex=vtx,
                            direction=u,
                            energy=E,
                            interaction_model=self.interaction_model)

        weights = self.get_weights(particle)
        if not self.shadow:
            particle.survival_weight = weights[0]
            particle.interaction_weight = weights[1]
            logger.debug(
                "Successfully created %s with survival weight %d and " +
                "interaction weight %d", particle, weights[0], weights[1])
            return Event(particle)
        elif np.random.rand() < weights[0]:
            particle.survival_weight = 1
            particle.interaction_weight = weights[1]
            logger.debug(
                "Successfully created %s with survival weight %d and " +
                "interaction weight %d", particle, weights[0], weights[1])
            return Event(particle)
        else:
            # Particle was shadowed by the earth. Try again
            logger.debug("Particle creation shadowed by the Earth")
            return self.create_event()
Ejemplo n.º 2
0
 def test_get_parent_fails(self, particle):
     """Test that get_parent fails if the parent isn't in the tree"""
     event = Event(particle)
     child1 = Particle(particle_id=Particle.Type.electron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     with pytest.raises(ValueError):
         event.get_parent(child1)
Ejemplo n.º 3
0
def events():
    """Fixture for forming basic list of Event objects"""
    return [
        Event(
            Particle(particle_id=Particle.Type.electron_neutrino,
                     vertex=[100, 200, -500],
                     direction=[0, 0, 1],
                     energy=1e9)),
        Event(
            Particle(particle_id=Particle.Type.electron_antineutrino,
                     vertex=[0, 0, 0],
                     direction=[0, 0, -1],
                     energy=1e9)),
    ]
Ejemplo n.º 4
0
def event():
    """Fixture for forming basic Event object"""
    return Event(
        Particle(particle_id=Particle.Type.electron_neutrino,
                 vertex=[100, 200, -500],
                 direction=[0, 0, 1],
                 energy=1e9))
Ejemplo n.º 5
0
 def test_creation(self, particle):
     """Test initialization of event"""
     event = Event(particle)
     assert event.roots[0] == particle
     assert len(event.roots) == 1
     assert len(event) == 1
     for p in event:
         assert p == particle
Ejemplo n.º 6
0
 def test_create_event(self, event):
     event2 = Event(
         Particle(particle_id="nu_e",
                  vertex=[0, 0, 0],
                  direction=[0, 0, -1],
                  energy=1e9))
     generator = ListGenerator([event, event2])
     assert generator.create_event() == event
     assert generator.create_event() == event2
Ejemplo n.º 7
0
def kernel():
    """Fixture for forming basic EventKernel object"""
    gen = ListGenerator(
        Event(
            Particle(particle_id="electron_neutrino",
                     vertex=[100, 200, -500],
                     direction=[0, 0, 1],
                     energy=1e9)))
    return EventKernel(generator=gen,
                       antennas=[Antenna(position=(0, 0, -100), noisy=False)])
Ejemplo n.º 8
0
 def __init__(self, events, loop=True):
     if (isinstance(events, Iterable) and not isinstance(events, Event)):
         self.events = events
     else:
         self.events = [events]
     for i, event in enumerate(self.events):
         if isinstance(event, Particle):
             self.events[i] = Event(event)
     self.loop = loop
     self._index = 0
     self._additional_counts = 0
Ejemplo n.º 9
0
 def test_add_children(self, particle):
     """Test the ability to add children to a particle"""
     event = Event(particle)
     child1 = Particle(particle_id=Particle.Type.electron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     child2 = Particle(particle_id=Particle.Type.positron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     event.add_children(particle, [child1, child2])
     assert len(event.roots) == 1
     assert len(event) == 3
     all_particles = [particle, child1, child2]
     for p in event:
         assert p in all_particles
         all_particles.remove(p)
     assert all_particles == []
     child3 = Particle(particle_id=Particle.Type.electron_antineutrino,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9)
     event.add_children(child1, child3)
     assert len(event) == 4
     all_particles = [particle, child1, child2, child3]
     for p in event:
         assert p in all_particles
         all_particles.remove(p)
     assert all_particles == []
Ejemplo n.º 10
0
    def _load_events(self):
        """
        Pulls the next chunk of events into memory.

        Reads events up to the ``slice_range`` into memory from the current
        file. If the current file is exhausted, loads the next file.

        Returns
        -------
        list
            List of `Event` objects read from the current file.

        Raises
        ------
        StopIteration
            If the end of the last file in the file list has been reached.

        """
        if self._file_index < 0 or self._event_index >= len(self._file):
            self._next_file()
        start = self._event_index
        stop = self._event_index + self.slice_range
        self._event_index += self.slice_range
        if stop > len(self._file):
            stop = len(self._file)
        self._events = []
        self._event_counts = []
        for file_event in self._file[start:stop]:
            info = file_event.get_particle_info()
            particles = []
            for p in info:
                part = Particle(particle_id=p['particle_id'],
                                vertex=(p['vertex_x'], p['vertex_y'],
                                        p['vertex_z']),
                                direction=(p['direction_x'], p['direction_y'],
                                           p['direction_z']),
                                energy=p['energy'],
                                interaction_model=self.interaction_model,
                                interaction_type=p['interaction_kind'])
                part.interaction.inelasticity = p['interaction_inelasticity']
                part.interaction.em_frac = p['interaction_em_frac']
                part.interaction.had_frac = p['interaction_had_frac']
                part.survival_weight = p['survival_weight']
                part.interaction_weight = p['interaction_weight']
                particles.append(part)
            self._events.append(Event(particles))
            self._event_counts.append(file_event.total_events_thrown)
Ejemplo n.º 11
0
    def create_event(self):
        """
        Generate a neutrino.

        Pulls the next `Particle` object from the file(s) and places it into
        an `Event` by itself.

        Returns
        -------
        Event
            Next neutrino `Event` object from the file(s).

        Raises
        ------
        StopIteration
            If the end of the last file in the file list has been reached.

        See Also
        --------
        pyrex.Event : Class for storing a tree of `Particle` objects
                      representing an event.
        pyrex.Particle : Class for storing particle attributes.

        """
        self._index += 1
        if self.vertices is None or self._index >= len(self.vertices):
            self._next_file()
            return self.create_event()
        if self.interactions is None:
            interaction = None
        else:
            interaction = self.interactions[self._index]
        if self.weights is None:
            weight = 1
        else:
            weight = self.weights[self._index]
        p = Particle(particle_id=self.ids[self._index],
                     vertex=self.vertices[self._index],
                     direction=self.directions[self._index],
                     energy=self.energies[self._index],
                     interaction_model=self.interaction_model,
                     interaction_type=interaction,
                     weight=weight)
        return Event(p)
Ejemplo n.º 12
0
 def test_loop(self, event):
     """Test that the loop property allows for turning on and off the
     re-iteration of the list of events"""
     event2 = Event(
         Particle(particle_id="nu_e",
                  vertex=[0, 0, 0],
                  direction=[0, 0, -1],
                  energy=1e9))
     generator = ListGenerator([event, event2])
     assert generator.create_event() == event
     assert generator.create_event() == event2
     assert generator.create_event() == event
     assert generator.create_event() == event2
     assert generator.create_event() == event
     generator = ListGenerator(event, loop=False)
     assert not generator.loop
     assert generator.create_event() == event
     with pytest.raises(StopIteration):
         generator.create_event()
Ejemplo n.º 13
0
 def test_get_parent(self, particle):
     """Test the ability to retrieve parent of a particle"""
     event = Event(particle)
     child1 = Particle(particle_id=Particle.Type.electron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     child2 = Particle(particle_id=Particle.Type.positron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     event.add_children(particle, [child1, child2])
     assert event.get_parent(child1) == particle
     assert event.get_parent(child2) == particle
     assert event.get_parent(particle) is None
Ejemplo n.º 14
0
 def test_get_children(self, particle):
     """Test the ability to retrieve children of a particle"""
     event = Event(particle)
     child1 = Particle(particle_id=Particle.Type.electron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     child2 = Particle(particle_id=Particle.Type.positron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     event.add_children(particle, [child1, child2])
     expected_children = [child1, child2]
     for child in event.get_children(particle):
         assert child in expected_children
         expected_children.remove(child)
     assert expected_children == []
     assert event.get_children(child1) == []
Ejemplo n.º 15
0
 def test_get_from_level(self, particle):
     """Test the ability to get particles from a level in the tree"""
     event = Event(particle)
     child1 = Particle(particle_id=Particle.Type.electron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     child2 = Particle(particle_id=Particle.Type.positron,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9,
                       interaction_model=Interaction)
     event.add_children(particle, [child1, child2])
     expected_level_0 = [particle]
     expected_level_1 = [child1, child2]
     for p in event.get_from_level(0):
         assert p in expected_level_0
         expected_level_0.remove(p)
     assert expected_level_0 == []
     for p in event.get_from_level(1):
         assert p in expected_level_1
         expected_level_1.remove(p)
     assert expected_level_1 == []
     assert event.get_from_level(2) == []
     child3 = Particle(particle_id=Particle.Type.electron_antineutrino,
                       vertex=[100, 200, -500],
                       direction=[0, 0, 1],
                       energy=1e9)
     event.add_children(child1, child3)
     expected_level_0 = [particle]
     expected_level_1 = [child1, child2]
     expected_level_2 = [child3]
     for p in event.get_from_level(0):
         assert p in expected_level_0
         expected_level_0.remove(p)
     assert expected_level_0 == []
     for p in event.get_from_level(1):
         assert p in expected_level_1
         expected_level_1.remove(p)
     assert expected_level_1 == []
     for p in event.get_from_level(2):
         assert p in expected_level_2
         expected_level_2.remove(p)
     assert expected_level_2 == []