コード例 #1
0
ファイル: PapasSim.py プロジェクト: f-thiele/heppy
 def __init__(self, *args, **kwargs):
     super(PapasSim, self).__init__(*args, **kwargs)
     self.detector = self.cfg_ana.detector
     self.simulator = Simulator(self.detector, self.mainLogger)
     self.simname = '_'.join([self.instance_label, self.cfg_ana.sim_particles])
     self.tracksname = self.cfg_ana.tracks
     self.mergedecalsname = self.cfg_ana.merged_ecals
     self.mergedhcalsname = self.cfg_ana.merged_hcals
     self.historyname = self.cfg_ana.output_history
コード例 #2
0
ファイル: Papas.py プロジェクト: IPNL-ee/heppy
 def __init__(self, *args, **kwargs):
     super(Papas, self).__init__(*args, **kwargs)
     self.detector = self.cfg_ana.detector
     self.simulator = Simulator(self.detector,
                                self.mainLogger)
     self.is_display = self.cfg_ana.display
     if self.is_display:
         self.init_display()        
コード例 #3
0
ファイル: PapasSim.py プロジェクト: semkiv/heppy
 def __init__(self, *args, **kwargs):
     super(PapasSim, self).__init__(*args, **kwargs)
     self.detector = self.cfg_ana.detector
     self.simulator = Simulator(self.detector,
                                self.mainLogger)
     self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])
     self.recname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles])
     self.is_display = self.cfg_ana.display
     if self.is_display:
         self.init_display()
コード例 #4
0
ファイル: PapasSim.py プロジェクト: jlingema/heppy
 def __init__(self, *args, **kwargs):
     super(PapasSim, self).__init__(*args, **kwargs)
     self.detector = self.cfg_ana.detector
     self.simulator = Simulator(self.detector, self.mainLogger)
     self.simname = '_'.join([self.instance_label, self.cfg_ana.sim_particles])
     self.tracksname = self.cfg_ana.tracks
     self.mergedecalsname = self.cfg_ana.merged_ecals
     self.mergedhcalsname = self.cfg_ana.merged_hcals
     self.historyname = self.cfg_ana.output_history
     self.is_display = self.cfg_ana.display
     if self.is_display:
         self.init_display()
コード例 #5
0
ファイル: PapasSim.py プロジェクト: GaelTouquet/heppy
 def __init__(self, *args, **kwargs):
     super(PapasSim, self).__init__(*args, **kwargs)
     self.detector = self.cfg_ana.detector
     self.simulator = Simulator(self.detector, self.mainLogger)
     self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])
     self.tracksname =  self.cfg_ana.tracks  
     self.mergedecalsname = self.cfg_ana.merged_ecals
     self.mergedhcalsname = self.cfg_ana.merged_hcals
     self.historyname =  self.cfg_ana.output_history
     #decide if reconstruction is needed
     self.do_reconstruct = False
     if hasattr(self.cfg_ana, 'rec_particles') :
         self.do_reconstruct = True
         self.recname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles])
     #if hasattr(self.cfg_ana, 'rec_particles_no_leptons') :
     #    self.do_reconstruct = True
     #    self.rec_noleptonsname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles_no_leptons])        
     
     self.is_display = self.cfg_ana.display
     if self.is_display:
         self.init_display()        
コード例 #6
0
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.
    
    #This will need to redocumented once new papasdata structure arrives

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        merged_ecals = 'ecal_clusters',
        merged_hcals = 'hcal_clusters',
        tracks = 'tracks',
        #rec_particles = 'sim_rec_particles', # optional - will only do a simulation reconstruction if a name is provided
        output_history = 'history_nodes',
        display_filter_func = lambda ptc: ptc.e()>1.,
        display = False,
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    merged_ecals: Name for the merged clusters created by simulator
    merged_hcals: Name for the merged clusters created by simulator
    tracks:       Name for smeared tracks created by simulator
    rec_particles: Optional. Name extension for the reconstructed particles created by simulator
                   This is retained for the time being to allow two reconstructions to be compared
                   Reconstruction will occur if this parameter  or rec_particles_no_leptons is provided
                   Same comments as for the sim_particles parameter above.
    rec_particles_no_leptons: Optional. Name extension for the reconstructed particles created by simulator
                   without electrons and muons
                   Reconstruction will occur if this parameter  or rec_particles is provided
                   This is retained for the time being to allow two reconstructions to be compared
                   Same comments as for the sim_particles parameter above.
    smeared: Name for smeared leptons
    history: Optional name for the history nodes, set to None if not needed
    display      : Enable the event display
    verbose      : Enable the detailed printout.

        event must contain
          todo once history is implemented
        event will gain
          ecal_clusters:- smeared merged clusters from simulation
          hcal_clusters:- smeared merged clusters from simulation
          tracks:       - tracks from simulation
          baseline_particles:- simulated particles (excluding electrons and muons)
          sim_particles - simulated particles including electrons and muons
        
    '''
    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.detector = self.cfg_ana.detector
        self.simulator = Simulator(self.detector, self.mainLogger)
        self.simname = '_'.join(
            [self.instance_label, self.cfg_ana.sim_particles])
        self.tracksname = self.cfg_ana.tracks
        self.mergedecalsname = self.cfg_ana.merged_ecals
        self.mergedhcalsname = self.cfg_ana.merged_hcals
        self.historyname = self.cfg_ana.output_history
        self.is_display = self.cfg_ana.display
        if self.is_display:
            self.init_display()

    def init_display(self):
        self.display = Display(['xy', 'yz'])
        self.gdetector = GDetector(self.detector)
        self.display.register(self.gdetector, layer=0, clearable=False)
        self.is_display = True

    def process(self, event):

        event.simulator = self
        if self.is_display:
            self.display.clear()
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        try:
            self.simulator.simulate(gen_particles)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        pfsim_particles = self.simulator.ptcs
        if self.is_display:
            self.display.register(GTrajectories(pfsim_particles), layer=1)
        #these are the particles before simulation
        simparticles = sorted(pfsim_particles,
                              key=lambda ptc: ptc.e(),
                              reverse=True)
        setattr(event, self.simname, simparticles)

        #extract the tracks and clusters (extraction is prior to Colins merging step)
        event.tracks = dict()
        event.ecal_clusters = dict()
        event.hcal_clusters = dict()
        if "tracker" in self.simulator.pfinput.elements:
            for element in self.simulator.pfinput.elements["tracker"]:
                event.tracks[element.uniqueid] = element

        if "ecal_in" in self.simulator.pfinput.elements:
            for element in self.simulator.pfinput.elements["ecal_in"]:
                event.ecal_clusters[element.uniqueid] = element

        if "hcal_in" in self.simulator.pfinput.elements:
            for element in self.simulator.pfinput.elements["hcal_in"]:
                event.hcal_clusters[element.uniqueid] = element

        ruler = Distance()

        #create history node
        #note eventually history will be created by the simulator and passed in
        # as an argument and this will no longer be needed
        uniqueids = list(event.tracks.keys()) + list(
            event.ecal_clusters.keys()) + list(event.hcal_clusters.keys())
        history = dict((idt, Node(idt)) for idt in uniqueids)

        #Now merge the simulated clusters and tracks as a separate pre-stage (prior to new reconstruction)
        # and set the event to point to the merged cluster
        pfevent = PFEvent(event, 'tracks', 'ecal_clusters', 'hcal_clusters')
        merged_ecals = MergedClusterBuilder(pfevent.ecal_clusters, ruler,
                                            history)
        setattr(event, self.mergedecalsname, merged_ecals.merged)
        merged_hcals = MergedClusterBuilder(pfevent.hcal_clusters, ruler,
                                            merged_ecals.history_nodes)
        setattr(event, self.mergedhcalsname, merged_hcals.merged)
        setattr(event, self.historyname, merged_hcals.history_nodes)
コード例 #7
0
ファイル: PapasSim.py プロジェクト: HEP-FCC/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    verbose      : Enable the detailed printout.

        event must contain
          gen_particles
        event will gain
          papasevent - simulated objects (simulated particles, tracks, and clusters) and history
          simparticles - simulated particles of the papasevent, as a list. 
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.simulator = Simulator(self.cfg_ana.detector, self.mainLogger)
        self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])

    def process(self, event):
        #random.seed(0xdeadbeef) #Useful to make results reproducable between loops and single runs
        event.simulator = self
        event.papasevent = PapasEvent(event.iEv)   
        papasevent = event.papasevent
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        gen_particles_collection = {} #make a dict from the gen_particles list so that it can be stored into the papasevent collections  
        for g in gen_particles:
            g.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, g.objid()[0], 'g', g.p4().E()))
            gen_particles_collection[g.dagid()] = g
        def simparticle(ptc, index):
            '''Create a sim particle to be used in papas from an input particle.
            '''
            tp4 = ptc.p4()
            vertex = ptc.start_vertex().position()
            charge = ptc.q()
            pid = ptc.pdgid()
            simptc = Particle(tp4, vertex, charge, pid)
            simptc.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, index, 's', simptc.idvalue))
            pdebugger.info(" ".join(("Made", simptc.__str__())))
            #simptc.gen_ptc = ptc
            #record that sim particle derives from gen particle
            child = papasevent.history.setdefault(simptc.dagid(), Node(simptc.dagid())) #creates a new node if it is not there already
            parent = papasevent.history.setdefault(ptc.dagid(), Node(ptc.dagid()))
            parent.add_child(child)
            return simptc
        simptcs = [simparticle(ptc, index)
                   for index, ptc in enumerate(gen_particles)]
        try:
            self.simulator.simulate(simptcs, papasevent.history)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        #these are the particles before simulation
        simparticles = sorted(self.simulator.ptcs, key=P4.sort_key, reverse=True)
        setattr(event, self.simname, simparticles)
        papasevent.add_collection(gen_particles_collection)
        papasevent.add_collection(self.simulator.simulated_particles)
        papasevent.add_collection(self.simulator.true_tracks)
        papasevent.add_collection(self.simulator.smeared_tracks)
        papasevent.add_collection(self.simulator.smeared_hcals)
        papasevent.add_collection(self.simulator.true_hcals)
        papasevent.add_collection(self.simulator.smeared_ecals)
        papasevent.add_collection(self.simulator.true_ecals)  

        #todo move to separate analyzer
        self.merge_clusters(papasevent) #add to simulator class? 
        #useful when producing outputs from a papasevent
        papasevent.iEv = event.iEv
            

    def merge_clusters(self, papasevent): # todo move to a separate analyzer
        #For Now merge the simulated clusters as a separate pre-stage (prior to new reconstruction)        
        ruler = Distance()
        merged_ecals = dict()
        merged_hcals = dict()
        ecals = papasevent.get_collection('es')
        if ecals:
            merged_ecals = MergedClusterBuilder(papasevent.get_collection('es'), ruler, papasevent.history).merged_clusters
        hcals = papasevent.get_collection('hs')
        if hcals:        
            merged_hcals = MergedClusterBuilder(papasevent.get_collection('hs'), ruler, papasevent.history).merged_clusters
        papasevent.add_collection(merged_ecals)
        papasevent.add_collection(merged_hcals)
コード例 #8
0
ファイル: PapasSim.py プロジェクト: rsawada/heppy
 def __init__(self, *args, **kwargs):
     super(PapasSim, self).__init__(*args, **kwargs)
     self.simulator = Simulator(self.cfg_ana.detector, self.mainLogger)
     self.simname = '_'.join(
         [self.instance_label, self.cfg_ana.sim_particles])
コード例 #9
0
ファイル: PapasSim.py プロジェクト: rsawada/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    verbose      : Enable the detailed printout.

        event must contain
          gen_particles
        event will gain
          papasevent - simulated objects (simulated particles, tracks, and clusters) and history
          simparticles - simulated particles of the papasevent, as a list. 
    '''
    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.simulator = Simulator(self.cfg_ana.detector, self.mainLogger)
        self.simname = '_'.join(
            [self.instance_label, self.cfg_ana.sim_particles])

    def process(self, event):
        #random.seed(0xdeadbeef) #Useful to make results reproducable between loops and single runs
        event.simulator = self
        event.papasevent = PapasEvent(event.iEv)
        papasevent = event.papasevent
        gen_particles = getattr(event, self.cfg_ana.gen_particles)

        def simparticle(ptc, index):
            '''Create a sim particle to be used in papas from an input particle.
            '''
            tp4 = ptc.p4()
            vertex = ptc.start_vertex().position()
            charge = ptc.q()
            pid = ptc.pdgid()
            simptc = Particle(tp4, vertex, charge, index, pid)
            pdebugger.info(" ".join(("Made", simptc.__str__())))
            simptc.gen_ptc = ptc
            return simptc

        simptcs = [
            simparticle(ptc, index) for index, ptc in enumerate(gen_particles)
        ]
        try:
            self.simulator.simulate(simptcs, papasevent.history)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        #these are the particles before simulation
        simparticles = sorted(self.simulator.ptcs,
                              key=P4.sort_key,
                              reverse=True)
        setattr(event, self.simname, simparticles)
        papasevent.add_collection(self.simulator.simulated_particles)
        papasevent.add_collection(self.simulator.true_tracks)
        papasevent.add_collection(self.simulator.smeared_tracks)
        papasevent.add_collection(self.simulator.smeared_hcals)
        papasevent.add_collection(self.simulator.true_hcals)
        papasevent.add_collection(self.simulator.smeared_ecals)
        papasevent.add_collection(self.simulator.true_ecals)

        #todo move to separate analyzer
        self.merge_clusters(papasevent)  #add to simulator class?
        #useful when producing outputs from a papasevent
        papasevent.iEv = event.iEv

    def merge_clusters(self, papasevent):  # todo move to a separate analyzer
        #For Now merge the simulated clusters as a separate pre-stage (prior to new reconstruction)
        ruler = Distance()
        merged_ecals = dict()
        merged_hcals = dict()
        ecals = papasevent.get_collection('es')
        if ecals:
            merged_ecals = MergedClusterBuilder(
                papasevent.get_collection('es'), ruler,
                papasevent.history).merged_clusters
        hcals = papasevent.get_collection('hs')
        if hcals:
            merged_hcals = MergedClusterBuilder(
                papasevent.get_collection('hs'), ruler,
                papasevent.history).merged_clusters
        papasevent.add_collection(merged_ecals)
        papasevent.add_collection(merged_hcals)
コード例 #10
0
ファイル: PapasSim.py プロジェクト: clementhelsens/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    verbose      : Enable the detailed printout.

        event must contain
          gen_particles
        event will gain
          papasevent - simulated objects (simulated particles, tracks, and clusters) and history
          simparticles - simulated particles of the papasevent, as a list. 
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.simulator = Simulator(self.cfg_ana.detector, self.mainLogger)
        self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])

    def process(self, event):
        #random.seed(0xdeadbeef) #Useful to make results reproducable between loops and single runs
        event.simulator = self
        event.papasevent = PapasEvent(event.iEv)   
        papasevent = event.papasevent
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        try:
            self.simulator.simulate(gen_particles, papasevent.history)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        #these are the particles before simulation
        simparticles = sorted(self.simulator.ptcs, key=P4.sort_key, reverse=True)
        setattr(event, self.simname, simparticles)
        papasevent.add_collection(self.simulator.simulated_particles)
        papasevent.add_collection(self.simulator.true_tracks)
        papasevent.add_collection(self.simulator.smeared_tracks)
        papasevent.add_collection(self.simulator.smeared_hcals)
        papasevent.add_collection(self.simulator.true_hcals)
        papasevent.add_collection(self.simulator.smeared_ecals)
        papasevent.add_collection(self.simulator.true_ecals)  
        
        #todo move to separate analyzer
        self.merge_clusters(papasevent) #add to simulator class? 
        #useful when producing outputs from a papasevent
        papasevent.iEv = event.iEv
            

    def merge_clusters(self, papasevent): # todo move to a separate analyzer
        #For Now merge the simulated clusters as a separate pre-stage (prior to new reconstruction)        
        ruler = Distance()
        merged_ecals = dict()
        merged_hcals = dict()
        ecals = papasevent.get_collection('es')
        if ecals:
            merged_ecals = MergedClusterBuilder(papasevent.get_collection('es'), ruler, papasevent.history).merged_clusters
        hcals = papasevent.get_collection('hs')
        if hcals:        
            merged_hcals = MergedClusterBuilder(papasevent.get_collection('hs'), ruler, papasevent.history).merged_clusters
        papasevent.add_collection(merged_ecals)
        papasevent.add_collection(merged_hcals)
コード例 #11
0
ファイル: PapasSim.py プロジェクト: semkiv/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        rec_particles = 'rec_particles',
        display = False,
        verbose = False
    )

    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    rec_particles: Name extension for the output reconstructed particle collection.
                   Same comments as for the sim_particles parameter above.
    display      : Enable the event display
    verbose      : Enable the detailed printout.
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.detector = self.cfg_ana.detector
        self.simulator = Simulator(self.detector,
                                   self.mainLogger)
        self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])
        self.recname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles])
        self.is_display = self.cfg_ana.display
        if self.is_display:
            self.init_display()

    def init_display(self):
        self.display = Display(['xy','yz'])
        self.gdetector = GDetector(self.detector)
        self.display.register(self.gdetector, layer=0, clearable=False)
        self.is_display = True

    def process(self, event):
        event.simulator = self
        if self.is_display:
            self.display.clear()
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        self.simulator.simulate( gen_particles )
        pfsim_particles = self.simulator.ptcs
        if self.is_display:
            self.display.register( GTrajectories(pfsim_particles),
                                   layer=1)
        simparticles = sorted( pfsim_particles,
                               key = lambda ptc: ptc.e(), reverse=True)
        particles = sorted( self.simulator.particles,
                            key = lambda ptc: ptc.e(), reverse=True)
        setattr(event, self.simname, simparticles)
        pfinput = PFInput(simparticles)
        event.tracks = dict()
        event.ECALclusters = dict()
        event.HCALclusters = dict()
        for label, element in pfinput.elements.iteritems():
            if label == 'tracker':
                event.tracks[0,id(element)]=element
            elif label == 'ecal_in':
                event.ECALclusters[1,id(element)]=element
            elif label == 'hcal_in':
                event.HCALclusters[2,id(element)]=element
            else:
                print label
                assert(False)
        setattr(event, self.recname, particles)
コード例 #12
0
ファイル: Papas.py プロジェクト: IPNL-ee/heppy
class Papas(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation. 

    Papas reads a list of stable generated particles, 
    and creates a list of reconstruted particles. 
    First, the particles are extrapolated in the magnetic field
    through the tracker and to the calorimeters, 
    and the particle deposits are simulated using a parametrized simulation. 
    Then, a particle flow algorithm is used to connect the simulated tracks 
    and calorimeter energy deposits, and to identify and reconstruct 
    final state particles. 

    Example: 

    from heppy.analyzers.Papas import Papas
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
      Papas,
      instance_label = 'papas',
      detector = CMS(),
      gen_particles = 'gen_particles_stable',
      sim_particles = 'sim_particles',
      rec_particles = 'particles',
      display = False,
      verbose = True
    )

    detector:      Detector model to be used, here CMS.  
    gen_particles: Name of the input gen particle collection
    sim_particles: Name for the output sim particle collection. 
    rec_particles: Name for the output reconstructed particle collection.
    display      : Enable the event display
    verbose      : Enable the detailed printout.
    '''

    def __init__(self, *args, **kwargs):
        super(Papas, self).__init__(*args, **kwargs)
        self.detector = self.cfg_ana.detector
        self.simulator = Simulator(self.detector,
                                   self.mainLogger)
        self.is_display = self.cfg_ana.display
        if self.is_display:
            self.init_display()        
        
    def init_display(self):
        self.display = Display(['xy','yz'])
        self.gdetector = GDetector(self.detector)
        self.display.register(self.gdetector, layer=0, clearable=False)
        self.is_display = True
        
    def process(self, event):
        event.simulator = self 
        if self.is_display:
            self.display.clear()
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        self.simulator.simulate( gen_particles )
        pfsim_particles = self.simulator.ptcs
        if self.is_display:
            particles_for_display = pfsim_particles
            if hasattr(self.cfg_ana, 'display_filter_func'):
                particles_for_display = [ ptc for ptc in pfsim_particles if 
                                          self.cfg_ana.display_filter_func(ptc) ]
            self.display.register( GTrajectories(particles_for_display),
                                   layer=1)
        simparticles = sorted( pfsim_particles,
                               key = lambda ptc: ptc.e(), reverse=True)
        particles = sorted( self.simulator.particles,
                            key = lambda ptc: ptc.e(), reverse=True)
        setattr(event, self.cfg_ana.sim_particles, simparticles)
        setattr(event, self.cfg_ana.rec_particles, particles)
コード例 #13
0
ファイル: PapasSim.py プロジェクト: GaelTouquet/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration: 

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        merged_ecals = 'ecal_clusters',
        merged_hcals = 'hcal_clusters',
        tracks = 'tracks',
        #rec_particles = 'sim_rec_particles', # optional - will only do a simulation reconstruction if a name is provided
        output_history = 'history_nodes', 
        display_filter_func = lambda ptc: ptc.e()>1.,
        display = False,
        verbose = True
    )
    
    detector:      Detector model to be used. 
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection. 
                   Note that the instance label is prepended to this name. 
                   Therefore, in this particular case, the name of the output 
                   sim particle collection is "papas_sim_particles".
    merged_ecals: Name for the merged clusters created by simulator              
    merged_hcals: Name for the merged clusters created by simulator             
    tracks:       Name for smeared tracks created by simulator              
    
    rec_particles: Optional. Name extension for the reconstructed particles created by simulator
                   This is retained for the time being to allow two reconstructions to be compared
                   Reconstruction will occur if this parameter  or rec_particles_no_leptons is provided
                   Same comments as for the sim_particles parameter above.
    rec_particles_no_leptons: Optional. Name extension for the reconstructed particles created by simulator
                   without electrons and muons
                   Reconstruction will occur if this parameter  or rec_particles is provided
                   This is retained for the time being to allow two reconstructions to be compared
                   Same comments as for the sim_particles parameter above.
    smeared: Name for smeared leptons 
    history: Optional name for the history nodes, set to None if not needed
    display      : Enable the event display
    verbose      : Enable the detailed printout.
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.detector = self.cfg_ana.detector
        self.simulator = Simulator(self.detector, self.mainLogger)
        self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])
        self.tracksname =  self.cfg_ana.tracks  
        self.mergedecalsname = self.cfg_ana.merged_ecals
        self.mergedhcalsname = self.cfg_ana.merged_hcals
        self.historyname =  self.cfg_ana.output_history
        #decide if reconstruction is needed
        self.do_reconstruct = False
        if hasattr(self.cfg_ana, 'rec_particles') :
            self.do_reconstruct = True
            self.recname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles])
        #if hasattr(self.cfg_ana, 'rec_particles_no_leptons') :
        #    self.do_reconstruct = True
        #    self.rec_noleptonsname = '_'.join([self.instance_label,  self.cfg_ana.rec_particles_no_leptons])        
        
        self.is_display = self.cfg_ana.display
        if self.is_display:
            self.init_display()        

    def init_display(self):
        self.display = Display(['xy','yz'])
        self.gdetector = GDetector(self.detector)
        self.display.register(self.gdetector, layer=0, clearable=False)
        self.is_display = True

    def process(self, event):
        '''
           event must contain the
           
           event will gain
             ecal_clusters:- smeared merged clusters from simulation
             hcal_clusters:- smeared merged clusters from simulation
             tracks:       - tracks from simulation
             baseline_particles:- simulated particles (excluding electrons and muons)
             sim_particles - simulated particles including electrons and muons
             
             
        '''
        event.simulator = self 
        if self.is_display:
            self.display.clear()
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        self.simulator.simulate( gen_particles, self.do_reconstruct)
        pfsim_particles = self.simulator.ptcs
        if self.is_display:
            self.display.register( GTrajectories(pfsim_particles),
                                   layer=1)
        #these are the particles before simulation        
        simparticles = sorted( pfsim_particles,
                               key = lambda ptc: ptc.e(), reverse=True)     
        setattr(event, self.simname, simparticles)
        
        if self.do_reconstruct: # used for verification/ comparison of reconstruction methods
            #these are the reconstructed (via simulation) particles  including electrons and muons
            particles = sorted( self.simulator.particles,
                            key = lambda ptc: ptc.e(), reverse=True)
        
            #these are the reconstructed (via simulation) particles excluding muons and electrons         
            #origparticles = sorted( self.simulator.pfsequence.pfreco.particles,
            #                       key = lambda ptc: ptc.e(), reverse=True)
            if hasattr(self, 'recname')  :
                setattr(event, self.recname, particles)          
            #if hasattr(self, 'rec_noleptonsname')  :
            #    setattr(event, self.rec_noleptonsname, origparticles)
                

        #extract the tracks and clusters (extraction is prior to Colins merging step)
        event.tracks = dict()
        event.ecal_clusters = dict()
        event.hcal_clusters = dict()
        
        
        if "tracker" in self.simulator.pfinput.elements :
            for element in self.simulator.pfinput.elements["tracker"]:
                event.tracks[element.uniqueid] = element
                
        if "ecal_in" in self.simulator.pfinput.elements :        
            for element in self.simulator.pfinput.elements["ecal_in"]:
                event.ecal_clusters[element.uniqueid] = element
                
        if "hcal_in" in self.simulator.pfinput.elements :
            for element in self.simulator.pfinput.elements["hcal_in"]:
                event.hcal_clusters[element.uniqueid] = element
                
        ruler = Distance()

        #create history node
        #note eventually history will be created by the simulator and passed in
        # as an argument and this will no longer be needed
        uniqueids = list(event.tracks.keys()) + list(event.ecal_clusters.keys()) + list(event.hcal_clusters.keys())
        history =  dict( (idt, Node(idt)) for idt in uniqueids )                    
       
        #Now merge the simulated clusters and tracks as a separate pre-stage (prior to new reconstruction)        
        # and set the event to point to the merged cluster
        pfevent =  PFEvent(event, 'tracks', 'ecal_clusters', 'hcal_clusters')
        merged_ecals = MergedClusterBuilder(pfevent.ecal_clusters, ruler, history)
        setattr(event, self.mergedecalsname, merged_ecals.merged)
        merged_hcals = MergedClusterBuilder(pfevent.hcal_clusters, ruler, merged_ecals.history_nodes)
        setattr(event, self.mergedhcalsname, merged_hcals.merged)
        setattr(event,  self.historyname,  merged_hcals.history_nodes)
        
        ####if uncommented this will use the original reconstructions to provide the ready merged tracks and clusters
        #event.ecal_clusters = dict()
        #event.hcal_clusters = dict()        
        #for element in self.simulator.pfsequence.elements :
            #elif element.__class__.__name__ == 'SmearedCluster' and element.layer == 'ecal_in': 
                #event.ecal_clusters[element.uniqueid] = element
            #elif element.__class__.__name__ == 'SmearedCluster' and element.layer == 'hcal_in': 
                #event.hcal_clusters[element.uniqueid] = element
            #else :            
                #print element.__class__.__name__ 
                #assert(False)
       
        ###if uncommented will check that cluster merging is OK   (compare new merging module with Colins merging)    
        #event.origecal_clusters = dict()
        #event.orighcal_clusters = dict()
        #for element in self.simulator.pfsequence.elements :
            #if element.__class__.__name__ == 'SmearedCluster' and element.layer == 'ecal_in': 
                #event.origecal_clusters[element.uniqueid] = element
            #elif element.__class__.__name__ == 'SmearedCluster' and element.layer == 'hcal_in': 
                #event.orighcal_clusters[element.uniqueid] = element
        #ClusterComparer(event.origecal_clusters,event.ecal_clusters)
        #ClusterComparer(event.orighcal_clusters,event.hcal_clusters)
        #event.othertracks =  dict()
        #for element in self.simulator.pfsequence.elements :
            #if element.__class__.__name__ == 'SmearedTrack': 
                #event.othertracks[element.uniqueid] = element        
        #assert (len(event.tracks) == len(event.othertracks))
       
        pass
コード例 #14
0
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    verbose      : Enable the detailed printout.

        event must contain
          gen_particles
        event will gain
          papasevent - simulated objects (simulated particles, tracks, and clusters) and history
          simparticles - simulated particles of the papasevent, as a list. 
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.simulator = Simulator(self.cfg_ana.detector, self.mainLogger)
        self.simname = '_'.join([self.instance_label,  self.cfg_ana.sim_particles])

    def process(self, event):
        #random.seed(0xdeadbeef) #Useful to make results reproducable between loops and single runs
        event.simulator = self
        papasevent = PapasEvent(event.iEv)
        setattr(event, "papasevent", papasevent)        
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        try:
            self.simulator.simulate(gen_particles)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        pfsim_particles = self.simulator.ptcs

        #these are the particles before simulation
        simparticles = sorted(pfsim_particles,
                              key=lambda ptc: ptc.e(), reverse=True)     
        setattr(event, self.simname, simparticles)
    
        #create dicts of clusters, particles etc (todo?:move a lot of this into simulator)
        self.build_collections_and_history(papasevent, simparticles)
        
        #todo move to separate analyzer
        self.merge_clusters(papasevent) #add to simulator class? 
        #useful when producing outputs from a papasevent
        papasevent.iEv = event.iEv
    
    def build_collections_and_history(self, papasevent, sim_particles):  
        #todo this should be integrated into the simulator in the future
        simulated_particles = dict()
        tracks = dict()
        smeared_tracks=dict()
        smeared_hcals = dict()
        true_hcals = dict()
        smeared_ecals = dict()
        true_ecals = dict()    
        smeared_tracks = dict()
        true_tracks = dict()            
        
        history =  papasevent.history
        for ptc in sim_particles:
            uid = ptc.uniqueid
            simulated_particles[uid] = ptc
            history[uid] = Node(uid)
            if ptc.track:
                track_id = ptc.track.uniqueid
                true_tracks[track_id] = ptc.track
                history[track_id] = Node(track_id)
                history[uid].add_child(history[track_id])
                if ptc.track_smeared:
                    smtrack_id = ptc.track_smeared.uniqueid
                    smeared_tracks[smtrack_id] = ptc.track_smeared
                    history[smtrack_id] = Node(smtrack_id)
                    history[track_id].add_child(history[smtrack_id])    
            if len(ptc.clusters) > 0 : 
                for key, clust in ptc.clusters.iteritems():
                    if Identifier.get_type(clust.uniqueid) == Identifier.PFOBJECTTYPE.ECALCLUSTER:
                        true_ecals[clust.uniqueid] = clust                       
                    elif Identifier.get_type(clust.uniqueid) == Identifier.PFOBJECTTYPE.HCALCLUSTER:
                        true_hcals[clust.uniqueid] = clust
                    else:
                        assert(False)                    
                    history[clust.uniqueid] = Node(clust.uniqueid)
                    history[uid].add_child(history[clust.uniqueid])  
    
                    if len(ptc.clusters_smeared) > 0 :  #need to put in link between true and smeared cluster 
                        for key1, smclust in ptc.clusters_smeared.iteritems():
                            if (key == key1): 
                                if Identifier.get_type(smclust.uniqueid) == Identifier.PFOBJECTTYPE.ECALCLUSTER:
                                    smeared_ecals[smclust.uniqueid]=smclust
                                elif Identifier.get_type(smclust.uniqueid) == Identifier.PFOBJECTTYPE.HCALCLUSTER:
                                    smeared_hcals[smclust.uniqueid]=smclust 
                                history[smclust.uniqueid] = Node(smclust.uniqueid)
                                history[clust.uniqueid].add_child(history[smclust.uniqueid])
                            
        papasevent.add_collection(simulated_particles)
        papasevent.add_collection(true_tracks)
        papasevent.add_collection(smeared_tracks)
        papasevent.add_collection(smeared_hcals)
        papasevent.add_collection(true_hcals)
        papasevent.add_collection(smeared_ecals)
        papasevent.add_collection(true_ecals)    
            

    def merge_clusters(self, papasevent): # todo move to a separate analyzer
        #For Now merge the simulated clusters as a separate pre-stage (prior to new reconstruction)        
        ruler = Distance()
        merged_ecals = dict()
        merged_hcals = dict()
        ecals = papasevent.get_collection('es')
        if ecals:
            merged_ecals = MergedClusterBuilder(papasevent.get_collection('es'), ruler, papasevent.history).merged
        hcals = papasevent.get_collection('hs')
        if hcals:        
            merged_hcals = MergedClusterBuilder(papasevent.get_collection('hs'), ruler, papasevent.history).merged
        papasevent.add_collection(merged_ecals)
        papasevent.add_collection(merged_hcals)
コード例 #15
0
ファイル: PapasSim.py プロジェクト: jlingema/heppy
class PapasSim(Analyzer):
    '''Runs PAPAS, the PArametrized Particle Simulation.
    
    #This will need to redocumented once new papasdata structure arrives

    Example configuration:

    from heppy.analyzers.PapasSim import PapasSim
    from heppy.papas.detectors.CMS import CMS
    papas = cfg.Analyzer(
        PapasSim,
        instance_label = 'papas',
        detector = CMS(),
        gen_particles = 'gen_particles_stable',
        sim_particles = 'sim_particles',
        merged_ecals = 'ecal_clusters',
        merged_hcals = 'hcal_clusters',
        tracks = 'tracks',
        #rec_particles = 'sim_rec_particles', # optional - will only do a simulation reconstruction if a name is provided
        output_history = 'history_nodes',
        display_filter_func = lambda ptc: ptc.e()>1.,
        display = False,
        verbose = True
    )
    detector:      Detector model to be used.
    gen_particles: Name of the input gen particle collection
    sim_particles: Name extension for the output sim particle collection.
                   Note that the instance label is prepended to this name.
                   Therefore, in this particular case, the name of the output
                   sim particle collection is "papas_sim_particles".
    merged_ecals: Name for the merged clusters created by simulator
    merged_hcals: Name for the merged clusters created by simulator
    tracks:       Name for smeared tracks created by simulator
    rec_particles: Optional. Name extension for the reconstructed particles created by simulator
                   This is retained for the time being to allow two reconstructions to be compared
                   Reconstruction will occur if this parameter  or rec_particles_no_leptons is provided
                   Same comments as for the sim_particles parameter above.
    rec_particles_no_leptons: Optional. Name extension for the reconstructed particles created by simulator
                   without electrons and muons
                   Reconstruction will occur if this parameter  or rec_particles is provided
                   This is retained for the time being to allow two reconstructions to be compared
                   Same comments as for the sim_particles parameter above.
    smeared: Name for smeared leptons
    history: Optional name for the history nodes, set to None if not needed
    display      : Enable the event display
    verbose      : Enable the detailed printout.

        event must contain
          todo once history is implemented
        event will gain
          ecal_clusters:- smeared merged clusters from simulation
          hcal_clusters:- smeared merged clusters from simulation
          tracks:       - tracks from simulation
          baseline_particles:- simulated particles (excluding electrons and muons)
          sim_particles - simulated particles including electrons and muons
        
    '''

    def __init__(self, *args, **kwargs):
        super(PapasSim, self).__init__(*args, **kwargs)
        self.detector = self.cfg_ana.detector
        self.simulator = Simulator(self.detector, self.mainLogger)
        self.simname = '_'.join([self.instance_label, self.cfg_ana.sim_particles])
        self.tracksname = self.cfg_ana.tracks
        self.mergedecalsname = self.cfg_ana.merged_ecals
        self.mergedhcalsname = self.cfg_ana.merged_hcals
        self.historyname = self.cfg_ana.output_history
        self.is_display = self.cfg_ana.display
        if self.is_display:
            self.init_display()

    def init_display(self):
        self.display = Display(['xy', 'yz'])
        self.gdetector = GDetector(self.detector)
        self.display.register(self.gdetector, layer=0, clearable=False)
        self.is_display = True

    def process(self, event):
        
        event.simulator = self
        if self.is_display:
            self.display.clear()
        pfsim_particles = []
        gen_particles = getattr(event, self.cfg_ana.gen_particles)
        try:
            self.simulator.simulate(gen_particles)
        except (PropagationError, SimulationError) as err:
            self.mainLogger.error(str(err) + ' -> Event discarded')
            return False
        pfsim_particles = self.simulator.ptcs
        if self.is_display  :
            self.display.register(GTrajectories(pfsim_particles),
                                  layer=1)
        #these are the particles before simulation
        simparticles = sorted(pfsim_particles,
                              key=lambda ptc: ptc.e(), reverse=True)
        setattr(event, self.simname, simparticles)

        #extract the tracks and clusters (extraction is prior to Colins merging step)
        event.tracks = dict()
        event.ecal_clusters = dict()
        event.hcal_clusters = dict()
        if "tracker" in self.simulator.pfinput.elements :
            for element in self.simulator.pfinput.elements["tracker"]:
                event.tracks[element.uniqueid] = element

        if "ecal_in" in self.simulator.pfinput.elements :
            for element in self.simulator.pfinput.elements["ecal_in"]:
                event.ecal_clusters[element.uniqueid] = element

        if "hcal_in" in self.simulator.pfinput.elements :
            for element in self.simulator.pfinput.elements["hcal_in"]:
                event.hcal_clusters[element.uniqueid] = element

        ruler = Distance()

        #create history node
        #note eventually history will be created by the simulator and passed in
        # as an argument and this will no longer be needed
        uniqueids = list(event.tracks.keys()) + list(event.ecal_clusters.keys()) + list(event.hcal_clusters.keys())
        history = dict((idt, Node(idt)) for idt in uniqueids)

        #Now merge the simulated clusters and tracks as a separate pre-stage (prior to new reconstruction)
        # and set the event to point to the merged cluster
        pfevent = PFEvent(event, 'tracks', 'ecal_clusters', 'hcal_clusters')
        merged_ecals = MergedClusterBuilder(pfevent.ecal_clusters, ruler, history)
        setattr(event, self.mergedecalsname, merged_ecals.merged)
        merged_hcals = MergedClusterBuilder(pfevent.hcal_clusters, ruler, merged_ecals.history_nodes)
        setattr(event, self.mergedhcalsname, merged_hcals.merged)
        setattr(event, self.historyname, merged_hcals.history_nodes)