Writing hepfiles from Dictionaries#
[1]:
# imports
import hepfile as hf
Say that we have two events in a particle physics experiment and we measured the x and y momentum for both the jets and muons for each as well as the number of particles emitted. Each event can be represented as a nested dictionary:
[2]:
event1 = {
'jet': {
'px': [1,2,3],
'py': [1,2,3]
},
'muons': {
'px': [1,2,3],
'py': [1,2,3]
},
'nParticles': 3
}
event2 = {
'jet': {
'px': [3,4,6,7],
'py': [3,4,6,7]
},
'muons': {
'px': [3,4,6,7],
'py': [3,4,6,7],
},
'nParticles': 4
}
Note how these events do not have the same number of particles emitted and therefore could not be stored in a typical “homogeneous” data structure. This is where hepfile becomes useful!
To easily create a hepfile from these events we can store them in a list:
[3]:
to_write_to_hepfile = [event1, event2]
Then, to write this out to a hepfile, we simply need to use the dict_tools
module of hepfile
.
[4]:
out_filename = 'output_from_dict.hdf5'
data = hf.dict_tools.dictlike_to_hepfile(to_write_to_hepfile, out_filename)
data
[4]:
{'_GROUPS_': {'_SINGLETONS_GROUP_': ['COUNTER', 'nParticles'],
'jet': ['njet', 'px', 'py'],
'muons': ['nmuons', 'px', 'py']},
'_MAP_DATASETS_TO_COUNTERS_': {'_SINGLETONS_GROUP_': '_SINGLETONS_GROUP_/COUNTER',
'jet': 'jet/njet',
'jet/px': 'jet/njet',
'jet/py': 'jet/njet',
'muons': 'muons/nmuons',
'muons/px': 'muons/nmuons',
'muons/py': 'muons/nmuons',
'nParticles': '_SINGLETONS_GROUP_/COUNTER'},
'_LIST_OF_COUNTERS_': ['_SINGLETONS_GROUP_/COUNTER',
'jet/njet',
'muons/nmuons'],
'_SINGLETONS_GROUP_/COUNTER': [1, 1],
'_MAP_DATASETS_TO_DATA_TYPES_': {'_SINGLETONS_GROUP_/COUNTER': int,
'jet/njet': int,
'jet/px': numpy.int64,
'jet/py': numpy.int64,
'muons/nmuons': int,
'muons/px': numpy.int64,
'muons/py': numpy.int64,
'nParticles': numpy.int64},
'_META_': {},
'jet/njet': [3, 4],
'jet/px': [1, 2, 3, 3, 4, 6, 7],
'jet/py': [1, 2, 3, 3, 4, 6, 7],
'muons/nmuons': [3, 4],
'muons/px': [1, 2, 3, 3, 4, 6, 7],
'muons/py': [1, 2, 3, 3, 4, 6, 7],
'nParticles': [3, 4]}