Exemplo n.º 1
0
    def test_seed(self):

        #unseeded
        r0 = random.uniform(0, 1)
        r1 = random.expovariate(3000)
        r2 = random.gauss(1, 3)

        #seed
        random.seed(0xdeadbeef)
        a0 = random.uniform(0, 1)
        a1 = random.expovariate(3)
        a2 = random.gauss(1, 3)

        #reseed
        random.seed(0xdeadbeef)
        b0 = random.uniform(0, 1)
        b1 = random.expovariate(3)
        b2 = random.gauss(1, 3)

        #unseeded should be different to seeded
        self.assertFalse(a0 == r0)
        self.assertFalse(a1 == r1)
        self.assertFalse(a2 == r2)

        #reseeded should be same as seeded
        self.assertEqual(a0, b0)
        self.assertEqual(a1, b1)
        self.assertEqual(a2, b2)
Exemplo n.º 2
0
 def make_and_store_smeared_cluster(self, cluster, detector, accept=False, acceptance=None):
     '''Returns a copy of cluster, after a gaussian smearing of the energy.
     
     The smeared cluster is stored for further processing.
     
     @param cluster: the cluster to be smeared.
     @param detector: detector object from which the energy resolution, energy response
       and acceptance parametrizations are taken.
     @param accept: if set to true, always accept the cluster after smearing
     @param acceptance: optional detedctor object for acceptance.
       if provided, and if accept is False, used in place of detector.acceptance
     '''
     eres = detector.energy_resolution(cluster.energy, cluster.position.Eta())
     response = detector.energy_response(cluster.energy, cluster.position.Eta())
     energy = cluster.energy * random.gauss(response, eres)
     clusters = self.cluster_collection(cluster.layer, smeared=True)
     smeared_cluster = SmearedCluster(cluster,
                                      energy,
                                      cluster.position,
                                      cluster.size(),
                                      cluster.layer,
                                      len(clusters),
                                      cluster.particle)
     pdebugger.info(str('Made {}'.format(smeared_cluster)))
     det = acceptance if acceptance else detector
     if det.acceptance(smeared_cluster) or accept:
         clusters[smeared_cluster.uniqueid] = smeared_cluster                      
         self.update_history(cluster.uniqueid, smeared_cluster.uniqueid)            
         return smeared_cluster
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_cluster)))
         return None
Exemplo n.º 3
0
 def __smear(self, obj):
     '''returns the smeared object (obj is deepcopied).'''
     mu, sigma = self.cfg_ana.mu_sigma
     smear_factor = random.gauss(mu, sigma)
     smeared = copy.deepcopy(obj)
     smeared._tlv *= smear_factor
     return smeared
Exemplo n.º 4
0
 def make_and_store_smeared_cluster(self,
                                    cluster,
                                    detector,
                                    accept=False,
                                    acceptance=None):
     '''Returns a copy of self with a smeared energy.
     If accept is False (default), returns None if the smeared
     cluster is not in the detector acceptance. '''
     eres = detector.energy_resolution(cluster.energy,
                                       cluster.position.Eta())
     response = detector.energy_response(cluster.energy,
                                         cluster.position.Eta())
     energy = cluster.energy * random.gauss(response, eres)
     clusters = self.smeared_cluster_collection(cluster.layer)
     smeared_cluster = SmearedCluster(cluster, energy, cluster.position,
                                      cluster.size(), cluster.layer,
                                      len(clusters), cluster.particle)
     pdebugger.info(str('Made {}'.format(smeared_cluster)))
     det = acceptance if acceptance else detector
     if det.acceptance(smeared_cluster) or accept:
         clusters[smeared_cluster.uniqueid] = smeared_cluster
         self.update_history(cluster.uniqueid, smeared_cluster.uniqueid)
         return smeared_cluster
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_cluster)))
         return None
Exemplo n.º 5
0
 def make_and_store_smeared_cluster(self, cluster, detector, accept=False, acceptance=None):
     '''Returns a copy of cluster, after a gaussian smearing of the energy.
     
     The smeared cluster is stored for further processing.
     
     @param cluster: the cluster to be smeared.
     @param detector: detector object from which the energy resolution, energy response
       and acceptance parametrizations are taken.
     @param accept: if set to true, always accept the cluster after smearing
     @param acceptance: optional detedctor object for acceptance.
       if provided, and if accept is False, used in place of detector.acceptance
     '''
     eres = detector.energy_resolution(cluster.energy, cluster.position.Eta())
     response = detector.energy_response(cluster.energy, cluster.position.Eta())
     energy = cluster.energy * random.gauss(response, eres)
     clusters = self.cluster_collection(cluster.layer, smeared=True)
     smeared_cluster = SmearedCluster(cluster,
                                      energy,
                                      cluster.position,
                                      cluster.size(),
                                      cluster.layer,
                                      len(clusters),
                                      cluster.particle)
     pdebugger.info(str('Made {}'.format(smeared_cluster)))
     det = acceptance if acceptance else detector
     if det.acceptance(smeared_cluster) or accept:
         clusters[smeared_cluster.uniqueid] = smeared_cluster                      
         self.update_history(cluster.uniqueid, smeared_cluster.uniqueid)
         return smeared_cluster
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_cluster)))
         return None
Exemplo n.º 6
0
 def __smear(self, obj):
     '''returns the smeared object (obj is deepcopied).'''
     mu, sigma = self.cfg_ana.mu_sigma
     smear_factor = random.gauss(mu, sigma) 
     smeared = copy.deepcopy(obj)
     smeared._tlv *= smear_factor
     return smeared
Exemplo n.º 7
0
 def make_smeared_track(self, track, resolution):
     '''create a new smeared track'''
     #TODO smearing depends on particle type!
     scale_factor = random.gauss(1, resolution)
     smeared_track = SmearedTrack(track,
                                  track.p3 * scale_factor,
                                  track.charge,
                                  track.path,
                                  index = len(self.smeared_tracks))
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     return smeared_track  
Exemplo n.º 8
0
 def smear_track(self, track, detector, accept=False):
     # TODO smearing depends on particle type!
     ptres = detector.pt_resolution(track)
     scale_factor = random.gauss(1, ptres)
     smeared_track = SmearedTrack(track, track.p3 * scale_factor, track.charge, track.path)
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if detector.acceptance(smeared_track) or accept:
         return smeared_track
     else:
         pdebugger.info(str("Rejected {}".format(smeared_track)))
         return None
Exemplo n.º 9
0
 def make_smeared_track(self, track, resolution):
     '''create a new smeared track'''
     #TODO smearing depends on particle type!
     scale_factor = random.gauss(1, resolution)
     smeared_track = SmearedTrack(track,
                                  track.p3 * scale_factor,
                                  track.charge,
                                  track.path,
                                  index=len(self.smeared_tracks))
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     return smeared_track
Exemplo n.º 10
0
 def smear_track(self, track, detector, accept=False):
     #TODO smearing depends on particle type!
     ptres = detector.pt_resolution(track)
     scale_factor = random.gauss(1, ptres)
     smeared_track = SmearedTrack(track, track.p3 * scale_factor,
                                  track.charge, track.path)
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if detector.acceptance(smeared_track) or accept:
         return smeared_track
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_track)))
         return None
Exemplo n.º 11
0
    def smear_cluster(self, cluster, detector, accept=False, acceptance=None):
        """Returns a copy of self with a smeared energy.
        If accept is False (default), returns None if the smeared
        cluster is not in the detector acceptance. """

        eres = detector.energy_resolution(cluster.energy, cluster.position.Eta())
        response = detector.energy_response(cluster.energy, cluster.position.Eta())
        energy = cluster.energy * random.gauss(response, eres)
        smeared_cluster = SmearedCluster(
            cluster, energy, cluster.position, cluster.size(), cluster.layer, cluster.particle
        )
        pdebugger.info(str("Made {}".format(smeared_cluster)))
        det = acceptance if acceptance else detector
        if det.acceptance(smeared_cluster) or accept:
            return smeared_cluster
        else:
            pdebugger.info(str("Rejected {}".format(smeared_cluster)))
            return None
Exemplo n.º 12
0
 def make_and_store_smeared_track(self, ptc, track,
                                  detector_resolution, detector_acceptance):
     '''create a new smeared track'''
     #TODO smearing depends on particle type!
     resolution = detector_resolution(track)
     scale_factor = random.gauss(1, resolution)
     smeared_track = SmearedTrack(track,
                                  track._p3 * scale_factor,
                                  track.charge,
                                  track.path,
                                  index = len(self.smeared_tracks))
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if detector_acceptance(track):
         self.smeared_tracks[smeared_track.uniqueid] = smeared_track
         self.update_history(track.uniqueid, smeared_track.uniqueid)   
         ptc.track_smeared = smeared_track             
         return smeared_track  
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_track)))
         return None
Exemplo n.º 13
0
 def make_and_store_smeared_track(self, ptc, track,
                                  detector_resolution, detector_acceptance):
     '''create a new smeared track'''
     #TODO smearing depends on particle type!
     resolution = detector_resolution(ptc)
     scale_factor = random.gauss(1, resolution)
     smeared_track = SmearedTrack(track,
                                  track.p3 * scale_factor,
                                  track.charge,
                                  track.path,
                                  index = len(self.smeared_tracks))
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if detector_acceptance(smeared_track):
         self.smeared_tracks[smeared_track.uniqueid] = smeared_track
         self.update_history(track.uniqueid, smeared_track.uniqueid )   
         ptc.track_smeared = smeared_track             
         return smeared_track  
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_track)))
         return None
Exemplo n.º 14
0
    def smear_cluster(self, cluster, detector, accept=False, acceptance=None):
        '''Returns a copy of self with a smeared energy.
        If accept is False (default), returns None if the smeared
        cluster is not in the detector acceptance. '''

        eres = detector.energy_resolution(cluster.energy,
                                          cluster.position.Eta())
        response = detector.energy_response(cluster.energy,
                                            cluster.position.Eta())
        energy = cluster.energy * random.gauss(response, eres)
        smeared_cluster = SmearedCluster(cluster, energy, cluster.position,
                                         cluster.size(), cluster.layer,
                                         cluster.particle)
        pdebugger.info(str('Made {}'.format(smeared_cluster)))
        det = acceptance if acceptance else detector
        if det.acceptance(smeared_cluster) or accept:
            return smeared_cluster
        else:
            pdebugger.info(str('Rejected {}'.format(smeared_cluster)))
            return None
Exemplo n.º 15
0
 def simulate_muon(self, ptc):
     '''Simulate a muon corresponding to gen particle ptc
     
     Uses the methods detector.muon_energy_resolution
     and detector.muon_acceptance to smear the muon track.
     Later on, the particle flow algorithm will use the tracks
     coming from a muon to reconstruct muons.
     
     This method does not simulate energy deposits in the calorimeters
     '''
     pdebugger.info("Simulating Muon")
     self.propagate(ptc)
     ptres = self.detector.muon_pt_resolution(ptc)
     scale_factor = random.gauss(1, ptres)
     track = ptc.track
     smeared_track = SmearedTrack(track, track.p3 * scale_factor,
                                  track.charge, track.path)
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if self.detector.muon_acceptance(smeared_track):
         ptc.track_smeared = smeared_track
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_track)))
Exemplo n.º 16
0
 def make_and_store_smeared_cluster(self, cluster, detector, accept=False, acceptance=None):
     '''Returns a copy of self with a smeared energy.
     If accept is False (default), returns None if the smeared
     cluster is not in the detector acceptance. '''
     eres = detector.energy_resolution(cluster.energy, cluster.position.Eta())
     response = detector.energy_response(cluster.energy, cluster.position.Eta())
     energy = cluster.energy * random.gauss(response, eres)
     clusters = self.smeared_cluster_collection(cluster.layer)
     smeared_cluster = SmearedCluster(cluster,
                                      energy,
                                      cluster.position,
                                      cluster.size(),
                                      cluster.layer,
                                      len(clusters),
                                      cluster.particle)
     pdebugger.info(str('Made {}'.format(smeared_cluster)))
     det = acceptance if acceptance else detector
     if det.acceptance(smeared_cluster) or accept:
         clusters[smeared_cluster.uniqueid] = smeared_cluster                      
         self.update_history(cluster.uniqueid, smeared_cluster.uniqueid)            
         return smeared_cluster
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_cluster)))
         return None
Exemplo n.º 17
0
 def simulate_electron(self, ptc):
     '''Simulate an electron corresponding to gen particle ptc.
     
     Uses the methods detector.electron_energy_resolution
     and detector.electron_acceptance to smear the electron track.
     Later on, the particle flow algorithm will use the tracks
     coming from an electron to reconstruct electrons.
     
     This method does not simulate an electron energy deposit in the ECAL.
     '''
     pdebugger.info("Simulating Electron")
     ecal = self.detector.elements['ecal']
     propagator(ptc.q()).propagate_one(
         ptc, ecal.volume.inner, self.detector.elements['field'].magnitude)
     eres = self.detector.electron_energy_resolution(ptc)
     scale_factor = random.gauss(1, eres)
     track = ptc.track
     smeared_track = SmearedTrack(track, track.p3 * scale_factor,
                                  track.charge, track.path)
     pdebugger.info(" ".join(("Made", smeared_track.__str__())))
     if self.detector.electron_acceptance(smeared_track):
         ptc.track_smeared = smeared_track
     else:
         pdebugger.info(str('Rejected {}'.format(smeared_track)))
Exemplo n.º 18
0
def multiple_scattering( particle, detector_element, field ):
    '''This function computes the scattering of a particle while propagating through the detector.
    
    As described in the pdg booklet, Passage of particles through matter, multiple scattering through small angles.
    the direction of a charged particle is modified.
    
    This function takes a particle (that has been propagated until the detector element
    where it will be scattered) and the detector element responsible for the scattering.
    The magnetic field has to be specified in order to create the new trajectory.
    
    Then this function computes the new direction, randomly choosen according to
    Moliere's theory of multiple scattering (see pdg booklet) and replaces the
    initial path of the particle by this new scattered path.
    
    The particle can now be propagated in the next part of the detector.
    '''

    if not particle.q():
        return
    # reject particles that could not be extrapolated to detector element
    # (particle created too late, out of the detector element)
    surface_in = '{}_in'.format(detector_element.name)
    surface_out = '{}_out'.format(detector_element.name)
    if not surface_in in particle.path.points or \
        not surface_out in particle.path.points:
        return
    
    #TODOCOLIN : check usage of private attributes
    in_point = particle.path.points[surface_in]
    out_point = particle.path.points[surface_out]
    phi_in = particle.path.phi( in_point.X(), in_point.Y())
    phi_out = particle.path.phi( out_point.X(), out_point.Y())
    t_scat = particle.path.time_at_phi((phi_in+phi_out)*0.5)
    # compute p4_t = p4 at t_scat :
    p4_0 = particle.path.p4.Clone()
    p4tx = p4_0.X()*math.cos(particle.path.omega*t_scat)\
           + p4_0.Y()*math.sin(particle.path.omega*t_scat)
    p4ty =-p4_0.X()*math.sin(particle.path.omega*t_scat)\
           + p4_0.Y()*math.cos(particle.path.omega*t_scat)
    p4tz = p4_0.Z()
    p4tt = p4_0.T()
    p4_t = TLorentzVector(p4tx, p4ty, p4tz, p4tt)

    # now, p4t will be modified with respect to the multiple scattering
    # first one has to determine theta_0 the width of the gaussian :
    P = p4_t.Vect().Dot(p4_t.Vect().Unit())
    deltat = particle.path.time_at_phi(phi_out)-particle.path.time_at_phi(phi_in)
    x = abs(particle.path.path_length(deltat))
    X_0 = detector_element.material.x0

    theta_0 = 1.0*13.6e-3/(1.0*particle.path.speed/constants.c*P)*abs(particle.path.charge)
    theta_0 *= (1.0*x/X_0)**(1.0/2)*(1+0.038*math.log(1.0*x/X_0))

    # now, make p4_t change due to scattering :
    theta_space = random.gauss(0, theta_0*2.0**(1.0/2))
    psi = constants.pi*random.uniform(0,1) #double checked
    p3i = p4_t.Vect().Clone()
    e_z = TVector3(0,0,1)
    #first rotation : theta, in the xy plane
    a = p3i.Cross(e_z)
    #this may change the sign, but randomly, as the sign of theta already is
    p4_t.Rotate(theta_space,a)
    #second rotation : psi (isotropic around initial direction)
    p4_t.Rotate(psi,p3i.Unit())

    # creating new helix, ref at scattering point :
    helix_new_t = Helix(field, particle.path.charge, p4_t,
                        particle.path.point_at_time(t_scat))

    # now, back to t=0
    p4sx = p4_t.X()*math.cos(-particle.path.omega*t_scat)\
           + p4_t.Y()*math.sin(-particle.path.omega*t_scat)
    p4sy =-p4_t.X()*math.sin(-particle.path.omega*t_scat)\
           + p4_t.Y()*math.cos(-particle.path.omega*t_scat)
    p4sz = p4_t.Z()
    p4st = p4_t.T()
    p4_scat = TLorentzVector(p4sx, p4sy, p4sz, p4st)

    # creating new helix, ref at new t0 point :
    helix_new_0 = Helix(field, particle.path.charge, p4_scat,
                        helix_new_t.point_at_time(-t_scat))

    # replacing the particle's path with the scatterd one :
    particle.set_path(helix_new_0, option = 'w')
Exemplo n.º 19
0
 def smear(self, obj):
     mu, sigma = self.cfg_ana.mu_sigma
     smear_factor = random.gauss(mu, sigma)
     smeared = copy.deepcopy(obj)
     smeared._tlv *= smear_factor
     return smeared
Exemplo n.º 20
0
def multiple_scattering(particle, detector_element, field):
    '''This function computes the scattering of a particle while propagating through the detector.
    
    As described in the pdg booklet, Passage of particles through matter, multiple scattering through small angles.
    the direction of a charged particle is modified.
    
    This function takes a particle (that has been propagated until the detector element
    where it will be scattered) and the detector element responsible for the scattering.
    The magnetic field has to be specified in order to create the new trajectory.
    
    Then this function computes the new direction, randomly choosen according to
    Moliere's theory of multiple scattering (see pdg booklet) and replaces the
    initial path of the particle by this new scattered path.
    
    The particle can now be propagated in the next part of the detector.
    '''

    if not particle.q():
        return
    # reject particles that could not be extrapolated to detector element
    # (particle created too late, out of the detector element)
    surface_in = '{}_in'.format(detector_element.name)
    surface_out = '{}_out'.format(detector_element.name)
    if not surface_in in particle.path.points or \
        not surface_out in particle.path.points:
        return

    #TODOCOLIN : check usage of private attributes
    in_point = particle.path.points[surface_in]
    out_point = particle.path.points[surface_out]
    phi_in = particle.path.phi(in_point.X(), in_point.Y())
    phi_out = particle.path.phi(out_point.X(), out_point.Y())
    t_scat = particle.path.time_at_phi((phi_in + phi_out) * 0.5)
    # compute p4_t = p4 at t_scat :
    p4_0 = particle.path.p4.Clone()
    p4tx = p4_0.X()*math.cos(particle.path.omega*t_scat)\
           + p4_0.Y()*math.sin(particle.path.omega*t_scat)
    p4ty =-p4_0.X()*math.sin(particle.path.omega*t_scat)\
           + p4_0.Y()*math.cos(particle.path.omega*t_scat)
    p4tz = p4_0.Z()
    p4tt = p4_0.T()
    p4_t = TLorentzVector(p4tx, p4ty, p4tz, p4tt)

    # now, p4t will be modified with respect to the multiple scattering
    # first one has to determine theta_0 the width of the gaussian :
    P = p4_t.Vect().Dot(p4_t.Vect().Unit())
    deltat = particle.path.time_at_phi(phi_out) - particle.path.time_at_phi(
        phi_in)
    x = abs(particle.path.path_length(deltat))
    X_0 = detector_element.material.x0

    theta_0 = 1.0 * 13.6e-3 / (1.0 * particle.path.speed / constants.c *
                               P) * abs(particle.path.charge)
    theta_0 *= (1.0 * x / X_0)**(1.0 / 2) * (1 +
                                             0.038 * math.log(1.0 * x / X_0))

    # now, make p4_t change due to scattering :
    theta_space = random.gauss(0, theta_0 * 2.0**(1.0 / 2))
    psi = constants.pi * random.uniform(0, 1)  #double checked
    p3i = p4_t.Vect().Clone()
    e_z = TVector3(0, 0, 1)
    #first rotation : theta, in the xy plane
    a = p3i.Cross(e_z)
    #this may change the sign, but randomly, as the sign of theta already is
    p4_t.Rotate(theta_space, a)
    #second rotation : psi (isotropic around initial direction)
    p4_t.Rotate(psi, p3i.Unit())

    # creating new helix, ref at scattering point :
    helix_new_t = Helix(field, particle.path.charge, p4_t,
                        particle.path.point_at_time(t_scat))

    # now, back to t=0
    p4sx = p4_t.X()*math.cos(-particle.path.omega*t_scat)\
           + p4_t.Y()*math.sin(-particle.path.omega*t_scat)
    p4sy =-p4_t.X()*math.sin(-particle.path.omega*t_scat)\
           + p4_t.Y()*math.cos(-particle.path.omega*t_scat)
    p4sz = p4_t.Z()
    p4st = p4_t.T()
    p4_scat = TLorentzVector(p4sx, p4sy, p4sz, p4st)

    # creating new helix, ref at new t0 point :
    helix_new_0 = Helix(field, particle.path.charge, p4_scat,
                        helix_new_t.point_at_time(-t_scat))

    # replacing the particle's path with the scatterd one :
    particle.set_path(helix_new_0, option='w')
Exemplo n.º 21
0
 def smear(self, obj):
     mu, sigma = self.cfg_ana.mu_sigma
     smear_factor = random.gauss(mu, sigma) 
     smeared = copy.deepcopy(obj)
     smeared._tlv *= smear_factor
     return smeared