def test_papasevent(self): #create a dummy papasevent papasevent = PapasEvent(0) ecals = dict() tracks = dict() mixed = dict() for i in range(0, 2): uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, i, 't', 4.5) ecals[uid] = uid papasevent.history[uid] = Node(uid) uidt = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, i, 's', 4.5) tracks[uidt] = uidt papasevent.history[uidt] = Node(uidt) papasevent.history[uidt].add_child(papasevent.history[uid]) lastid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, 3, 't', 3) ecals[lastid] = lastid papasevent.history[lastid] = Node(lastid) papasevent.add_collection(ecals) papasevent.add_collection(tracks) #create HistoryHelper hhelper = HistoryHelper(papasevent) #get all ids in event ids = hhelper.event_ids() self.assertTrue(len(ids) == 5) #check id_from_pretty self.assertTrue(hhelper.id_from_pretty('et3') == lastid) #check get_linked_ids linked = hhelper.get_linked_ids(lastid) #everything linked to lastid (which is just lastid) self.assertTrue(linked[0] == lastid and len(linked) == 1) self.assertTrue( hhelper.get_linked_ids( ids[0], direction="undirected")[1] == hhelper.id_from_pretty('ts0')) self.assertTrue( hhelper.get_linked_ids( ids[0], direction="parents")== hhelper.get_linked_ids( ids[0], direction="undirected")) self.assertTrue( hhelper.get_linked_ids(ids[0], direction="children") == [hhelper.id_from_pretty('et0')]) #filter_ids self.assertTrue( len(hhelper.filter_ids(ids, 'ts')) == 2) self.assertTrue( hhelper.filter_ids(ids, 'no') == []) #get_collection self.assertTrue( len( hhelper.get_collection(ids[1:2], 'no')) == 0) self.assertTrue( len( hhelper.get_collection([99], 'no')) == 0) self.assertTrue( len(hhelper.get_collection(ids[0:2], 'ts')) == 1) pass #get_history_subgroups subgroups = hhelper.get_history_subgroups() self.assertTrue(len(subgroups) == 3) #get_linked_collection self.assertTrue(hhelper.get_linked_collection( hhelper.id_from_pretty('et0'), 'ts').keys() == [hhelper.id_from_pretty('ts0')]) self.assertRaises(KeyError, hhelper.get_linked_collection, 0, 'ts') self.assertTrue(len(hhelper.get_linked_collection( hhelper.id_from_pretty('et0'), 'no')) == 0)
def process(self, event): event.papasevent = PapasEvent(event.iEv) papasevent = event.papasevent #make a dict from the gen_particles list so that it can be stored into the papasevent collections gen_particles = getattr(event, self.cfg_ana.gen_particles) gen_particles_collection = {} for g in gen_particles: #set the papas identifiers for use in DAG g.set_dagid( IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, g.objid()[0], 'g', g.p4().E())) gen_particles_collection[g.dagid()] = g #make a dict from the rec_particles list so that it can be stored into the papasevent collections rec_particles = getattr(event, self.cfg_ana.rec_particles) #if there are no rec_particles we assume this was an evernt discarded during reconstruction and skip it if len(rec_particles) == 0: self.mainLogger.error( 'no reconsrtucted particles found -> Event discarded') return False rec_particles_collection = {} for r in rec_particles: #set the papas identifiers for use in DAG r.set_dagid( IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, r.objid()[0], 'r', r.p4().E())) rec_particles_collection[r.dagid()] = r #create the history links for relationship between gen and rec particles particle_links = getattr(event, self.cfg_ana.gen_rec_links) for plink in particle_links: genid = None recid = None for g in gen_particles: if g.objid() == plink.id1(): genid = g.dagid() break for g in rec_particles: if g.objid() == plink.id2(): recid = g.dagid() break if recid == None or genid == None: self.mainLogger.error( 'Error: One of the particles in the Particle Link was not found-> discarding event' ) return False child = papasevent.history.setdefault( recid, Node(recid)) #creates a new node if it is not there already parent = papasevent.history.setdefault(genid, Node(genid)) parent.add_child(child) papasevent.add_collection(gen_particles_collection) papasevent.add_collection(rec_particles_collection)
def __init__(self, uid, layer): ''' uid is unique integer from 101-199 for ecal cluster unique integer from 201-299 for hcal cluster layer is ecal/hcal ''' if (layer == 'ecal_in'): self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, uid, 't') elif (layer == 'hcal_in'): self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.HCALCLUSTER, uid, 't') else: assert false self.layer = layer self.uid = uid self.energy=0
def __init__(self, uid): ''' uid is unique integer from 1-99 ''' self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, uid, 't') self.uid = uid self.layer = 'tracker' self.energy=0
def __init__(self, uid,pdgid): ''' uid is unique integer from 601-699 pdgid is particle uid eg 22 for photon ''' self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, uid,'r') self.pdgid = pdgid self.uid = uid
def __init__(self, uid): ''' uid is unique integer from 1-99 ''' self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, uid, 't') self.uid = uid self.layer = 'tracker' self.energy = 0
def __init__(self, uid, layer): ''' uid is unique integer from 101-199 for ecal cluster unique integer from 201-299 for hcal cluster layer is ecal/hcal ''' if (layer == 'ecal_in'): self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, uid, 't') elif (layer == 'hcal_in'): self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.HCALCLUSTER, uid, 't') else: assert false self.layer = layer self.uid = uid self.energy = 0
def process(self, event): event.papasevent = PapasEvent(event.iEv) papasevent = event.papasevent #make a dict from the gen_particles list so that it can be stored into the papasevent collections gen_particles = getattr(event, self.cfg_ana.gen_particles) gen_particles_collection = {} for g in gen_particles: #set the papas identifiers for use in DAG g.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, g.objid()[0], 'g', g.p4().E())) gen_particles_collection[g.dagid()] = g #make a dict from the rec_particles list so that it can be stored into the papasevent collections rec_particles = getattr(event, self.cfg_ana.rec_particles) #if there are no rec_particles we assume this was an evernt discarded during reconstruction and skip it if len(rec_particles) == 0: self.mainLogger.error('no reconsrtucted particles found -> Event discarded') return False rec_particles_collection = {} for r in rec_particles: #set the papas identifiers for use in DAG r.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, r.objid()[0], 'r', r.p4().E())) rec_particles_collection[r.dagid()] = r #create the history links for relationship between gen and rec particles particle_links = getattr(event, self.cfg_ana.gen_rec_links) for plink in particle_links: genid = None recid = None for g in gen_particles: if g.objid() == plink.id1() : genid = g.dagid() break for g in rec_particles: if g.objid() == plink.id2() : recid = g.dagid() break if recid==None or genid ==None: self.mainLogger.error('Error: One of the particles in the Particle Link was not found-> discarding event') return False child = papasevent.history.setdefault(recid, Node(recid)) #creates a new node if it is not there already parent = papasevent.history.setdefault(genid, Node(genid)) parent.add_child(child) papasevent.add_collection(gen_particles_collection) papasevent.add_collection(rec_particles_collection)
def __init__(self, uid, pdgid): ''' uid is unique integer from 301-399 pdgid is particle uid eg 22 for photon ''' self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, uid, 'g') #print "particle: ",self.uniqueid," ",uid self.pdgid = pdgid self.uid = uid
def __init__(self, uid, pdgid): ''' uid is unique integer from 601-699 pdgid is particle uid eg 22 for photon ''' self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, uid, 'r') self.pdgid = pdgid self.uid = uid
def __init__(self, pfobjecttype, index, subtype='u', identifiervalue = 0.0): '''@param pfobjecttype: type of the object to be created (used in Identifier class) eg Identifier.PFOBJECTTYPE.ECALCLUSTER @param subtype: Identifier subtype, eg 'm' for merged @param identifiervalue: The value to be encoded into the Identifier eg energy or pt ''' super(PFObject, self).__init__() self.linked = [] self.locked = False self.block_label = None self.uniqueid=IdCoder.make_id(pfobjecttype, index, subtype, identifiervalue)
def __init__(self, pfobjecttype, index, subtype='u', identifiervalue=0.0): '''@param pfobjecttype: type of the object to be created (used in Identifier class) eg Identifier.PFOBJECTTYPE.ECALCLUSTER @param subtype: Identifier subtype, eg 'm' for merged @param identifiervalue: The value to be encoded into the Identifier eg energy or pt ''' super(PFObject, self).__init__() self.linked = [] self.locked = False self.block_label = None self.uniqueid = IdCoder.make_id(pfobjecttype, index, subtype, identifiervalue)
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 test_papasevent(self): papasevent = PapasEvent(0) ecals = dict() tracks = dict() mixed = dict() for i in range(0, 2): uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, i, 't', 4.5) ecals[uid] = uid for i in range(0, 2): uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, i, 's', 4.5) tracks[uid] = uid lastid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, 3, 't', 3) ecals[lastid] = lastid papasevent.add_collection(ecals) papasevent.add_collection(tracks) #check that adding the same collection twice fails self.assertRaises(ValueError, papasevent.add_collection, ecals) #check that adding a mixed collection fails mixed = ecals.copy() mixed.update(tracks) self.assertRaises(ValueError, papasevent.add_collection, mixed) #get we can get back collections OK self.assertTrue(len( papasevent.get_collection('zz')) == 0) # this one does not exist self.assertTrue(len(papasevent.get_collection('et')) == 3) #check get_object self.assertTrue(IdCoder.pretty(papasevent.get_object(lastid)) == 'et3') self.assertTrue(papasevent.get_object(499) is None)
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
def reconstruct_cluster(self, cluster, layer, parent_ids, energy=None, vertex=None): '''construct a photon if it is an ecal construct a neutral hadron if it is an hcal ''' if self.locked[cluster.uniqueid]: return if vertex is None: vertex = TVector3() pdg_id = None propagate_to = None if layer=='ecal_in': pdg_id = 22 #photon propagate_to = [ self.detector.elements['ecal'].volume.inner ] elif layer=='hcal_in': pdg_id = 130 #K0 propagate_to = [ self.detector.elements['ecal'].volume.inner, self.detector.elements['hcal'].volume.inner ] else: raise ValueError('layer must be equal to ecal_in or hcal_in') assert(pdg_id) mass, charge = particle_data[pdg_id] if energy is None: energy = cluster.energy if energy < mass: return None if mass == 0: momentum = energy #avoid sqrt for zero mass else: momentum = math.sqrt(energy**2 - mass**2) p3 = cluster.position.Unit() * momentum p4 = TLorentzVector(p3.Px(), p3.Py(), p3.Pz(), energy) #mass is not accurate here particle = Particle(p4, vertex, charge, pdg_id) particle.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, len(self.particles), 'r', particle.idvalue)) # alice: this may be a bit strange because we can make a photon # with a path where the point is actually that of the hcal? # nb this only is problem if the cluster and the assigned layer # are different propagator(charge).propagate([particle], propagate_to) #merge Nov 10th 2016 not sure about following line (was commented out in papasevent branch) particle.clusters[layer] = cluster # not sure about this either when hcal is used to make an ecal cluster? self.locked[cluster.uniqueid] = True #just OK but not nice if hcal used to make ecal. pdebugger.info(str('Made {} from {}'.format(particle, cluster))) self.insert_particle(parent_ids, particle)
def reconstruct_track(self, track, pdgid, parent_ids, clusters=None): # cluster argument does not ever seem to be used at present '''construct a charged hadron from the track ''' if self.locked[track.uniqueid]: return vertex = track.path.points['vertex'] pdgid = pdgid * track.charge mass, charge = particle_data[pdgid] p4 = TLorentzVector() p4.SetVectM(track.p3() , mass) particle = Particle(p4, vertex, charge, pdgid) particle.set_dagid(IdCoder.make_id(IdCoder.PFOBJECTTYPE.PARTICLE, len(self.particles), 'r', particle.idvalue)) #todo fix this so it picks up smeared track points (need to propagagte smeared track) particle.set_track(track) #refer to existing track rather than make a new one self.locked[track.uniqueid] = True pdebugger.info(str('Made {} from {}'.format(particle, track))) self.insert_particle(parent_ids, particle) return particle
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
def __init__(self, element_ids, edges, index, subtype): ''' @param element_ids: list of the uniqueids of the elements to go in this block [id1,id2,...] @param edges: is a dictionary of edges, it must contain at least all needed edges. It is not a problem if it contains additional edges as only the ones needed will be extracted @param index: index into the collection of blocks into which new block will be added @param subtype: used when making unique identifier, will normally be 'r' for reconstructed blocks and 's' for split blocks ''' #make a uniqueid for this block self.uniqueid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.BLOCK, index, subtype, len(element_ids)) #this will sort by type eg ecal, hcal, track and then by energy (biggest first) self.element_uniqueids = sorted(element_ids, reverse=True) #sequential numbering of blocks, not essential but helpful for debugging self.block_count = PFBlock.temp_block_count PFBlock.temp_block_count += 1 #extract the relevant parts of the complete set of edges and store this within the block self.edges = dict() for id1, id2 in itertools.combinations(self.element_uniqueids, 2): key = Edge.make_key(id1, id2) self.edges[key] = edges[key]
def test_papasevent(self): #create a dummy papasevent papasevent = PapasEvent(0) ecals = dict() tracks = dict() mixed = dict() for i in range(0, 2): uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, i, 't', 4.5) ecals[uid] = uid papasevent.history[uid] = Node(uid) uidt = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, i, 's', 4.5) tracks[uidt] = uidt papasevent.history[uidt] = Node(uidt) papasevent.history[uidt].add_child(papasevent.history[uid]) lastid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, 3, 't', 3) ecals[lastid] = lastid papasevent.history[lastid] = Node(lastid) papasevent.add_collection(ecals) papasevent.add_collection(tracks) #create HistoryHelper hhelper = HistoryHelper(papasevent) #get all ids in event ids = hhelper.event_ids() self.assertTrue(len(ids) == 5) #check id_from_pretty self.assertTrue(hhelper.id_from_pretty('et3') == lastid) #check get_linked_ids linked = hhelper.get_linked_ids( lastid) #everything linked to lastid (which is just lastid) self.assertTrue(linked[0] == lastid and len(linked) == 1) self.assertTrue( hhelper.get_linked_ids(ids[0], direction="undirected")[1] == hhelper.id_from_pretty('ts0')) self.assertTrue( hhelper.get_linked_ids(ids[0], direction="parents") == hhelper.get_linked_ids(ids[0], direction="undirected")) self.assertTrue( hhelper.get_linked_ids(ids[0], direction="children") == [hhelper.id_from_pretty('et0')]) #filter_ids self.assertTrue(len(hhelper.filter_ids(ids, 'ts')) == 2) self.assertTrue(hhelper.filter_ids(ids, 'no') == []) #get_collection self.assertTrue(len(hhelper.get_collection(ids[1:2], 'no')) == 0) self.assertTrue(len(hhelper.get_collection([99], 'no')) == 0) self.assertTrue(len(hhelper.get_collection(ids[0:2], 'ts')) == 1) pass #get_history_subgroups subgroups = hhelper.get_history_subgroups() self.assertTrue(len(subgroups) == 3) #get_linked_collection self.assertTrue( hhelper.get_linked_collection(hhelper.id_from_pretty( 'et0'), 'ts').keys() == [hhelper.id_from_pretty('ts0')]) self.assertRaises(KeyError, hhelper.get_linked_collection, 0, 'ts') self.assertTrue( len( hhelper.get_linked_collection(hhelper.id_from_pretty('et0'), 'no')) == 0)
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