Skip to content
This repository has been archived by the owner on Nov 18, 2020. It is now read-only.

Commit

Permalink
rename Identifier class to Idcoder issue #39
Browse files Browse the repository at this point in the history
  • Loading branch information
Alice committed Nov 15, 2017
1 parent fcc9f7b commit 3269125
Show file tree
Hide file tree
Showing 24 changed files with 148 additions and 148 deletions.
6 changes: 3 additions & 3 deletions analyzers/PapasFromFccsw.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from heppy.framework.analyzer import Analyzer
from heppy.papas.data.papasevent import PapasEvent
from heppy.papas.graphtools.DAG import Node
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder

class PapasFromFccsw(Analyzer):
'''Sets up a papas event containing gen and rec particles from a ROOT file (eg using FCCSW papas run output)
Expand Down Expand Up @@ -41,7 +41,7 @@ def process(self, event):
gen_particles_collection = {}
for g in gen_particles:
#set the papas identifiers for use in DAG
g.set_dagid(Identifier.make_id(Identifier.PFOBJECTTYPE.PARTICLE, g.objid()[0], 'g', g.p4().E()))
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
Expand All @@ -54,7 +54,7 @@ def process(self, event):
rec_particles_collection = {}
for r in rec_particles:
#set the papas identifiers for use in DAG
r.set_dagid(Identifier.make_id(Identifier.PFOBJECTTYPE.PARTICLE, r.objid()[0], 'r', r.p4().E()))
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
Expand Down
2 changes: 1 addition & 1 deletion analyzers/PapasPFReconstructor.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from heppy.framework.analyzer import Analyzer
from heppy.papas.pfalgo.pfreconstructor import PFReconstructor as PFReconstructor
from heppy.papas.pfalgo.distance import Distance
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder

class PapasPFReconstructor(Analyzer):
''' Module to reconstruct particles from blocks of events
Expand Down
6 changes: 3 additions & 3 deletions analyzers/PapasSim.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from heppy.papas.papas_exceptions import PropagationError, SimulationError
from heppy.papas.data.papasevent import PapasEvent
from heppy.papas.simulator import Simulator
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder
from heppy.papas.graphtools.DAG import Node
from heppy.papas.pfalgo.distance import Distance
from heppy.papas.mergedclusterbuilder import MergedClusterBuilder
Expand Down Expand Up @@ -54,7 +54,7 @@ def process(self, event):
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(Identifier.make_id(Identifier.PFOBJECTTYPE.PARTICLE, g.objid()[0], 'g', g.p4().E()))
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.
Expand All @@ -64,7 +64,7 @@ def simparticle(ptc, index):
charge = ptc.q()
pid = ptc.pdgid()
simptc = Particle(tp4, vertex, charge, pid)
simptc.set_dagid(Identifier.make_id(Identifier.PFOBJECTTYPE.PARTICLE, index, 's', simptc.idvalue))
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
Expand Down
2 changes: 1 addition & 1 deletion analyzers/ParametrizedBTagger.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from heppy.papas.data.historyhelper import HistoryHelper
from heppy.analyzers.ChargedHadronsFromB import is_ptc_from_b
from heppy.particles.genbrowser import GenBrowser
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder

def is_matched_to_b(jet):
'''returns true if jet.match is a b quark.
Expand Down
6 changes: 3 additions & 3 deletions papas/data/historyhelper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'''Papas history helper'''
from heppy.papas.graphtools.DAG import BreadthFirstSearchIterative, DAGFloodFill
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder

class HistoryHelper(object):
'''Tool to assist with printing, plotting and reconstructing histories.
Expand Down Expand Up @@ -61,7 +61,7 @@ def filter_ids(self, ids, type_and_subtype):
@param type_and_subtype: a two letter type and subtype eg 'es' for smeared ecal
'''
return [uid for uid in ids
if Identifier.type_and_subtype(uid) == type_and_subtype]
if IdCoder.type_and_subtype(uid) == type_and_subtype]

def id_from_pretty(self, pretty):
''' Searches to find the true id given a pretty id string
Expand All @@ -70,7 +70,7 @@ def id_from_pretty(self, pretty):
@param: pretty is the easily readable name from the Identifier class which is shown in prints and plots eg 'et103'
'''
for uid in self.history.keys():
if Identifier.pretty(uid) == pretty:
if IdCoder.pretty(uid) == pretty:
return uid
return None

Expand Down
64 changes: 32 additions & 32 deletions papas/data/identifier.py → papas/data/idcoder.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

'''Unique Identifer'''
'''IdCoder for creating Unique Identifers'''
import struct
from itertools import count
from heppy.utils.pdebug import pdebugger

class Identifier(long):
'''The Identifier is a uniqueid that contains encoded information about an element
class IdCoder(long):
'''The Idcoder creates/manages an identifier (a uniqueid) that contains encoded information about an element
Given an identifier, we can determine whether the element is for example an ecal_cluster
Given an uniqueid, we can determine whether the element is for example an ecal_cluster
and then retrieve the cluster from a cluster dict.
The Identifier class consists of a set of static methods that can be used
The Idcoder class consists of a set of static methods that can be used
to create and to dissect identifiers.
The identifier is 64 bits wide and stores info as follows
Expand Down Expand Up @@ -46,15 +46,15 @@ def make_id(cls, type, index, subtype='u', value = 0.):
assert(value >= 0) #actually I would like it to work with negative numbers but need to change float to bit conversions
#shift all the parts and join together
typeshift = type << 61
valueshift = Identifier._float_to_bits(value) << 21
valueshift = IdCoder._float_to_bits(value) << 21
subtypeshift = ord(subtype.lower()) << 53
uid = subtypeshift | valueshift | typeshift | index
#verify
assert (Identifier.get_index(uid) == index )
assert (IdCoder.get_index(uid) == index )
if value != 0:
assert(abs(Identifier.get_value(uid) - value) < abs(value) * 10 ** -6)
assert (Identifier.get_type(uid) == type)
assert (Identifier.get_subtype(uid) == subtype)
assert(abs(IdCoder.get_value(uid) - value) < abs(value) * 10 ** -6)
assert (IdCoder.get_type(uid) == type)
assert (IdCoder.get_subtype(uid) == subtype)
if index >= 2**(21 -1):
raise ValueError('identifer index has exceeded maximum value allowed')
return uid
Expand All @@ -70,14 +70,14 @@ def get_unique_id( ident):
'''The unique id combines the index, type and subtype to form a shorter unique identifier (without the value)
Its not strictly needed for Python at the moment '''
bitshift = 21 + 61 - 53
typeshift = Identifier.get_type(ident) << 29
subtypeshift = ord(Identifier.get_subtype(ident)) << 21
uniqueid = subtypeshift | typeshift | Identifier.get_index(ident)
typeshift = IdCoder.get_type(ident) << 29
subtypeshift = ord(IdCoder.get_subtype(ident)) << 21
uniqueid = subtypeshift | typeshift | IdCoder.get_index(ident)
#verify
assert (uniqueid >> bitshift & 0b111 == Identifier.get_type(ident) )
assert (uniqueid >> 61 -53 & 0b11111111 == Identifier.get_subtype(ident) )
assert (uniqueid >> 61 -53 & 0b11111111 == Identifier.get_subtype(ident) )
assert ( (ident >> 0b111111111111111111111) == Identifier.get_index(ident))
assert (uniqueid >> bitshift & 0b111 == IdCoder.get_type(ident) )
assert (uniqueid >> 61 -53 & 0b11111111 == IdCoder.get_subtype(ident) )
assert (uniqueid >> 61 -53 & 0b11111111 == IdCoder.get_subtype(ident) )
assert ( (ident >> 0b111111111111111111111) == IdCoder.get_index(ident))
return uniqueid

@staticmethod
Expand Down Expand Up @@ -108,59 +108,59 @@ def get_value (ident):
'''returns the float value encoded in the identifier
@param ident: unique identifier'''
bitvalue = ident >> 21 & 0b11111111111111111111111111111111
return Identifier._bits_to_float(bitvalue)
return IdCoder._bits_to_float(bitvalue)

@staticmethod
def is_ecal ( ident):
'''boolean test of whether it is an ecal
@param ident: unique identifier'''
return Identifier.get_type(ident) == Identifier.PFOBJECTTYPE.ECALCLUSTER
return IdCoder.get_type(ident) == IdCoder.PFOBJECTTYPE.ECALCLUSTER

@staticmethod
def is_hcal ( ident):
'''boolean test of whether it is an hcal
@param ident: unique identifier'''
return Identifier.get_type(ident) == Identifier.PFOBJECTTYPE.HCALCLUSTER
return IdCoder.get_type(ident) == IdCoder.PFOBJECTTYPE.HCALCLUSTER

@staticmethod
def is_track ( ident):
'''boolean test of whether it is a track
@param ident: unique identifier'''
return Identifier.get_type(ident) == Identifier.PFOBJECTTYPE.TRACK
return IdCoder.get_type(ident) == IdCoder.PFOBJECTTYPE.TRACK

@staticmethod

def is_block ( ident):
'''boolean test of whether it is a block
@param ident: unique identifier'''
return Identifier.get_type(ident) == Identifier.PFOBJECTTYPE.BLOCK
return IdCoder.get_type(ident) == IdCoder.PFOBJECTTYPE.BLOCK

@staticmethod
def is_particle ( ident):
'''boolean test of whether it is an ecal
@param ident: unique identifier'''
return Identifier.get_type(ident) == Identifier.PFOBJECTTYPE.PARTICLE
return IdCoder.get_type(ident) == IdCoder.PFOBJECTTYPE.PARTICLE

@staticmethod
def type_letter(ident): #character/letter for this type
'''returns a single letter representation of the PFOBJECTTYPE type eg 'p' for particle
@param ident: unique identifier'''
typelist=".ehtpb..." #the enum value (0 to 8) will index into this and return E is it is ECAL etc
return typelist[Identifier.get_type(ident)]
return typelist[IdCoder.get_type(ident)]

@staticmethod
def type_and_subtype(ident):
'''returns a two letter representation of the type/ subtype eg 'pr' for reconstructed particle
@param ident: unique identifier
'''
return Identifier.type_letter(ident) + Identifier.get_subtype(ident)
return IdCoder.type_letter(ident) + IdCoder.get_subtype(ident)

@staticmethod
def pretty(ident):
'''returns a pretty string representation of the identifier with the two letter typ_and_subtype and the uniqueid
@param ident: unique identifier
'''
return Identifier.type_and_subtype(ident) + str(Identifier.get_index(ident))
return IdCoder.type_and_subtype(ident) + str(IdCoder.get_index(ident))

@staticmethod
def _float_to_bits (floatvalue): #standard float packing
Expand Down Expand Up @@ -188,20 +188,20 @@ def reset(cls):
def id_str(ident):
''' string formatted for outputs'''
return '{pretty:6}:{uid}:'.format(
pretty=Identifier.pretty(ident),
pretty=IdCoder.pretty(ident),
uid=ident)

if __name__ == '__main__':

uid = Identifier.make_id(Identifier.PFOBJECTTYPE.TRACK, 1, 's', 1.23456)
id1 = Identifier.make_id(Identifier.PFOBJECTTYPE.TRACK, 2, 's', 12.782)
uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, 1, 's', 1.23456)
id1 = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, 2, 's', 12.782)

assert (Identifier.pretty(id1) == 'ts2')
assert (IdCoder.pretty(id1) == 'ts2')
ids = []
for i in range(0,100):
uid = Identifier.make_id(Identifier.PFOBJECTTYPE.TRACK, i, 's', 2**(-i) )
uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, i, 's', 2**(-i) )
ids.append(uid)
ids = sorted(ids, reverse = True)
for uid in ids:
print Identifier.pretty(uid) + ": " + str(Identifier.get_value(uid))
print IdCoder.pretty(uid) + ": " + str(IdCoder.get_value(uid))
pass
8 changes: 4 additions & 4 deletions papas/data/papasevent.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from heppy.papas.data.identifier import Identifier
from heppy.papas.data.idcoder import IdCoder
from heppy.framework.event import Event


Expand Down Expand Up @@ -42,7 +42,7 @@ class PapasEvent(Event):

def __init__(self, iEv):
super(PapasEvent, self).__init__(iEv)
Identifier.reset()
IdCoder.reset()
self.collections = dict()
self.history = dict()

Expand All @@ -53,7 +53,7 @@ def add_collection(self, collection):
#find all the type_and_subtypes in the incoming collection
if len(collection) == 0:
return
types = set(map(Identifier.type_and_subtype, collection.keys()))
types = set(map(IdCoder.type_and_subtype, collection.keys()))
if len(types) > 1:
raise ValueError('More than one type')
the_type = types.pop()
Expand All @@ -70,7 +70,7 @@ def get_object(self, uid):
#would it be better to let it fail when asking for something that does not exist like this:
# return self.get_collections(Identifier.type_and_subtype(uid))[uid]
#
collection = self.get_collection(Identifier.type_and_subtype(uid))
collection = self.get_collection(IdCoder.type_and_subtype(uid))
if collection:
return collection.get(uid, None)
return None
Expand Down
8 changes: 4 additions & 4 deletions papas/data/test_historyhelper.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import unittest
import itertools
import copy
from identifier import Identifier
from heppy.papas.data.idcoder import IdCoder
from papasevent import PapasEvent
from historyhelper import HistoryHelper
from heppy.papas.graphtools.DAG import Node
Expand All @@ -17,15 +17,15 @@ def test_papasevent(self):
mixed = dict()

for i in range(0, 2):
uid = Identifier.make_id(Identifier.PFOBJECTTYPE.ECALCLUSTER, i, 't', 4.5)
uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, i, 't', 4.5)
ecals[uid] = uid
papasevent.history[uid] = Node(uid)
uidt = Identifier.make_id(Identifier.PFOBJECTTYPE.TRACK, i, 's', 4.5)
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 = Identifier.make_id(Identifier.PFOBJECTTYPE.ECALCLUSTER, 3, 't', 3)
lastid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.ECALCLUSTER, 3, 't', 3)
ecals[lastid] = lastid
papasevent.history[lastid] = Node(lastid)
papasevent.add_collection(ecals)
Expand Down
27 changes: 27 additions & 0 deletions papas/data/test_idcoder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import unittest
import itertools
from heppy.papas.data.idcoder import IdCoder

class TestIdentifier(unittest.TestCase):

def test_identifier(self):
IdCoder.reset()
uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, 0, 's', 1.23456)
id1 = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, 1, 's', 12.782)

self.assertTrue (IdCoder.pretty(id1) == 'ts1')
ids = []
for i in range(0, 5) :
uid = IdCoder.make_id(IdCoder.PFOBJECTTYPE.TRACK, i , 's', 2**(i-2) )
ids.append(uid)
ids = sorted(ids, reverse = True)
self.assertTrue(IdCoder.pretty(ids[0]) == 'ts4')
self.assertTrue(IdCoder.get_value(ids[0]) == 4.0)
self.assertTrue(IdCoder.pretty(ids[3]) == 'ts1')
self.assertTrue(IdCoder.get_value(ids[3]) == 0.5)

if __name__ == '__main__':
unittest.main()



27 changes: 0 additions & 27 deletions papas/data/test_identifier.py

This file was deleted.

Loading

0 comments on commit 3269125

Please sign in to comment.