-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a script to create an EDM4hep file making use of the complete datamodel #272
Merged
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
1203689
Add a script to create an EDM4hep file
jmcarcell 33fda00
Add a comment
jmcarcell 22c0201
Add a few comments and fix warnings related to Vector3d
jmcarcell 32ad7f9
Add an rntuple option
jmcarcell d86f7d0
Fix the RNTuple names
jmcarcell b2823d3
Format
jmcarcell d5e3d4a
Update script with the Type -> PDG rename
jmcarcell 2d1b2f7
Remove casts
jmcarcell d3da24b
Update the script
jmcarcell f6df9ff
Update the covariance matrixes
jmcarcell 0529b49
Fix all covmatrix setters
jmcarcell d7b196f
Add a workflow to generate a file
jmcarcell ffbdc31
Fix path
jmcarcell 9d482a1
Fix workflow
jmcarcell 91e42ec
Fix the name
jmcarcell 6d817c3
Add check
jmcarcell 62cac3a
Add ls
jmcarcell 2d1ed57
Run the script to create a file
jmcarcell 54206b8
Update script
jmcarcell 76c0baa
Add a test in CMakeLists.txt
jmcarcell a4e6da3
Add PYTHONPATH
jmcarcell fd7f750
Fix the script for the current version of cppyy
jmcarcell cec370e
Fix setting arrays
jmcarcell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,373 @@ | ||
# Description: Create a file with EDM4hep data using the EDM4hep python bindings | ||
# The purpose of the script is to use all the classes of the EDM4hep library | ||
# to create a file with dummy data. | ||
import podio | ||
import edm4hep | ||
from itertools import count | ||
import argparse | ||
import sys | ||
|
||
frames = 3 # How many frames or events will be written | ||
vectorsize = 5 # For vector members, each vector member will have this size | ||
counter = count() # next(counter) will return 0, 1, 2, ... | ||
# used to generate the dummy data | ||
output_file = "output.root" | ||
|
||
parser = argparse.ArgumentParser(description="Create a file with EDM4hep data") | ||
parser.add_argument( | ||
"--rntuple", action="store_true", help="Use a ROOT ntuple instead of EDM4hep" | ||
) | ||
args = parser.parse_args() | ||
|
||
if args.rntuple: | ||
try: | ||
writer = podio.root_io.RNTupleWriter(output_file) | ||
except AttributeError: | ||
print("The RNTuple writer from podio is not available but was requested") | ||
sys.exit(1) | ||
else: | ||
writer = podio.root_io.Writer(output_file) | ||
|
||
for i in range(frames): | ||
print(f"Writing frame {i}") | ||
frame = podio.Frame() | ||
|
||
# Make covariance matrices needed later | ||
|
||
# With the current version of cppyy (from ROOT 6.30.06) | ||
# it's not possible to initialize an std::array from a list | ||
# In future versions (works with 6.32.02) it will be possible | ||
cov3f = edm4hep.CovMatrix3f() | ||
for i in range(6): | ||
cov3f[i] = next(counter) | ||
cov4f = edm4hep.CovMatrix4f() | ||
for i in range(10): | ||
cov4f[i] = next(counter) | ||
cov6f = edm4hep.CovMatrix6f() | ||
for i in range(21): | ||
cov6f[i] = next(counter) | ||
|
||
header = edm4hep.EventHeaderCollection() | ||
h = header.create() | ||
h.setEventNumber(next(counter)) | ||
h.setRunNumber(next(counter)) | ||
h.setTimeStamp(next(counter)) | ||
h.setWeight(1.0) | ||
for j in range(vectorsize): | ||
h.addToWeights(next(counter)) | ||
frame.put(header, "EventHeader") | ||
|
||
particles = edm4hep.MCParticleCollection() | ||
for i in range(3): | ||
particle = particles.create() | ||
particle.setPDG(next(counter)) | ||
particle.setGeneratorStatus(next(counter)) | ||
particle.setSimulatorStatus(next(counter)) | ||
particle.setCharge(next(counter)) | ||
particle.setTime(next(counter)) | ||
particle.setMass(next(counter)) | ||
particle.setVertex( | ||
edm4hep.Vector3d(next(counter), next(counter), next(counter)) | ||
) | ||
particle.setEndpoint( | ||
edm4hep.Vector3d(next(counter), next(counter), next(counter)) | ||
) | ||
particle.setMomentum( | ||
edm4hep.Vector3d(next(counter), next(counter), next(counter)) | ||
) | ||
particle.setMomentumAtEndpoint( | ||
edm4hep.Vector3d(next(counter), next(counter), next(counter)) | ||
) | ||
particle.setSpin( | ||
edm4hep.Vector3f(next(counter), next(counter), next(counter)) | ||
) | ||
particle.setColorFlow(edm4hep.Vector2i(next(counter), next(counter))) | ||
|
||
particles[0].addToDaughters(particles[1]) | ||
particles[0].addToParents(particles[2]) | ||
particle = particles[0] | ||
frame.put(particles, "MCParticleCollection") | ||
|
||
hits = edm4hep.SimTrackerHitCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setEDep(next(counter)) | ||
hit.setTime(next(counter)) | ||
hit.setPathLength(next(counter)) | ||
hit.setQuality(next(counter)) | ||
hit.setPosition(edm4hep.Vector3d(next(counter), next(counter), next(counter))) | ||
hit.setMomentum(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
hit.setParticle(particle) | ||
simtracker_hit = hit | ||
frame.put(hits, "SimTrackerHitCollection") | ||
|
||
hits = edm4hep.CaloHitContributionCollection() | ||
hit = hits.create() | ||
hit.setPDG(next(counter)) | ||
hit.setEnergy(next(counter)) | ||
hit.setTime(next(counter)) | ||
hit.setStepPosition(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
hit.setParticle(particle) | ||
calohit = hit | ||
frame.put(hits, "CaloHitContributionCollection") | ||
|
||
hits = edm4hep.SimCalorimeterHitCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setEnergy(next(counter)) | ||
hit.setPosition(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
hit.addToContributions(calohit) | ||
simcalo_hit = hit | ||
frame.put(hits, "SimCalorimeterHitCollection") | ||
|
||
hits = edm4hep.RawCalorimeterHitCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setAmplitude(next(counter)) | ||
hit.setTimeStamp(next(counter)) | ||
frame.put(hits, "RawCalorimeterHitCollection") | ||
|
||
hits = edm4hep.CalorimeterHitCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setEnergy(next(counter)) | ||
hit.setEnergyError(next(counter)) | ||
hit.setTime(next(counter)) | ||
hit.setPosition(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
hit.setType(next(counter)) | ||
calo_hit = hit | ||
frame.put(hits, "CalorimeterHitCollection") | ||
|
||
pids = edm4hep.ParticleIDCollection() | ||
pid = pids.create() | ||
pid.setType(next(counter)) | ||
pid.setPDG(next(counter)) | ||
pid.setAlgorithmType(next(counter)) | ||
pid.setLikelihood(next(counter)) | ||
for j in range(vectorsize): | ||
pid.addToParameters(next(counter)) | ||
frame.put(pids, "ParticleIDCollection") | ||
|
||
clusters = edm4hep.ClusterCollection() | ||
cluster = clusters.create() | ||
cluster.setType(next(counter)) | ||
cluster.setEnergy(next(counter)) | ||
cluster.setEnergyError(next(counter)) | ||
cluster.setPosition(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
cluster.setPositionError(cov3f) | ||
cluster.setITheta(next(counter)) | ||
cluster.setPhi(next(counter)) | ||
cluster.setDirectionError( | ||
edm4hep.Vector3f(next(counter), next(counter), next(counter)) | ||
) | ||
for j in range(vectorsize): | ||
cluster.addToShapeParameters(next(counter)) | ||
cluster.addToSubdetectorEnergies(next(counter)) | ||
cluster.addToClusters(cluster) | ||
cluster.addToHits(calo_hit) | ||
frame.put(clusters, "ClusterCollection") | ||
|
||
hits = edm4hep.TrackerHit3DCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setType(next(counter)) | ||
hit.setQuality(next(counter)) | ||
hit.setTime(next(counter)) | ||
hit.setEDep(next(counter)) | ||
hit.setEDepError(next(counter)) | ||
hit.setPosition(edm4hep.Vector3d(next(counter), next(counter), next(counter))) | ||
hit.setCovMatrix(cov3f) | ||
tracker_hit = hit | ||
frame.put(hits, "TrackerHit3DCollection") | ||
|
||
hits = edm4hep.TrackerHitPlaneCollection() | ||
hit = hits.create() | ||
hit.setCellID(next(counter)) | ||
hit.setType(next(counter)) | ||
hit.setQuality(next(counter)) | ||
hit.setTime(next(counter)) | ||
hit.setEDep(next(counter)) | ||
hit.setEDepError(next(counter)) | ||
hit.setU(edm4hep.Vector2f(next(counter), next(counter))) | ||
hit.setV(edm4hep.Vector2f(next(counter), next(counter))) | ||
hit.setDu(next(counter)) | ||
hit.setDv(next(counter)) | ||
hit.setPosition(edm4hep.Vector3d(next(counter), next(counter), next(counter))) | ||
hit.setCovMatrix(cov3f) | ||
frame.put(hits, "TrackerHitPlaneCollection") | ||
|
||
series = edm4hep.RawTimeSeriesCollection() | ||
serie = series.create() | ||
serie.setCellID(next(counter)) | ||
serie.setQuality(next(counter)) | ||
serie.setTime(next(counter)) | ||
serie.setCharge(next(counter)) | ||
serie.setInterval(next(counter)) | ||
for j in range(vectorsize): | ||
serie.addToAdcCounts(next(counter)) | ||
frame.put(series, "RawTimeSeriesCollection") | ||
|
||
tracks = edm4hep.TrackCollection() | ||
track = tracks.create() | ||
track.setType(next(counter)) | ||
track.setChi2(next(counter)) | ||
track.setNdf(next(counter)) | ||
for j in range(vectorsize): | ||
track.addToSubdetectorHitNumbers(next(counter)) | ||
state = edm4hep.TrackState() | ||
state.location = next(counter) | ||
state.d0 = next(counter) | ||
state.phi = next(counter) | ||
state.omega = next(counter) | ||
state.z0 = next(counter) | ||
state.tanLambda = next(counter) | ||
state.time = next(counter) | ||
state.referencePoint = edm4hep.Vector3f( | ||
next(counter), next(counter), next(counter) | ||
) | ||
state.CovMatrix = cov6f | ||
track.addToTrackStates(state) | ||
track.addToTrackerHits(tracker_hit) | ||
track.addToTracks(track) | ||
frame.put(tracks, "TrackCollection") | ||
|
||
vertex = edm4hep.VertexCollection() | ||
v = vertex.create() | ||
v.setType(next(counter)) | ||
v.setChi2(next(counter)) | ||
v.setNdf(next(counter)) | ||
v.setPosition(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
v.setCovMatrix(cov3f) | ||
v.setAlgorithmType(next(counter)) | ||
for j in range(vectorsize): | ||
v.addToParameters(next(counter)) | ||
frame.put(vertex, "VertexCollection") | ||
|
||
rparticles = edm4hep.ReconstructedParticleCollection() | ||
rparticle = rparticles.create() | ||
rparticle.setPDG(next(counter)) | ||
rparticle.setEnergy(next(counter)) | ||
rparticle.setMomentum(edm4hep.Vector3f(next(counter), next(counter), next(counter))) | ||
rparticle.setReferencePoint( | ||
edm4hep.Vector3f(next(counter), next(counter), next(counter)) | ||
) | ||
rparticle.setCharge(next(counter)) | ||
rparticle.setMass(next(counter)) | ||
rparticle.setGoodnessOfPID(next(counter)) | ||
rparticle.setCovMatrix(cov4f) | ||
rparticle.setDecayVertex(v) | ||
rparticle.addToClusters(cluster) | ||
rparticle.addToTracks(track) | ||
rparticle.addToParticles(rparticle) | ||
reco_particle = rparticle | ||
frame.put(rparticles, "ReconstructedParticleCollection") | ||
|
||
v.addToParticles(reco_particle) | ||
pid.setParticle(reco_particle) | ||
|
||
# TODO: Add when the PID PR is merged | ||
# if not args.use_pre1: | ||
# pid.setParticle(reco_particle) | ||
|
||
assocs = edm4hep.MCRecoParticleAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(reco_particle) | ||
assoc.setSim(particle) | ||
frame.put(assocs, "MCRecoParticleAssociationCollection") | ||
|
||
assocs = edm4hep.MCRecoCaloAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(calo_hit) | ||
assoc.setSim(simcalo_hit) | ||
frame.put(assocs, "MCRecoCaloAssociationCollection") | ||
|
||
assocs = edm4hep.MCRecoTrackerAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(tracker_hit) | ||
assoc.setSim(simtracker_hit) | ||
frame.put(assocs, "MCRecoTrackerAssociationCollection") | ||
|
||
assocs = edm4hep.MCRecoCaloParticleAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(calo_hit) | ||
assoc.setSim(particle) | ||
frame.put(assocs, "MCRecoCaloParticleAssociationCollection") | ||
|
||
assocs = edm4hep.MCRecoClusterParticleAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(cluster) | ||
assoc.setSim(particle) | ||
frame.put(assocs, "MCRecoClusterParticleAssociationCollection") | ||
|
||
assocs = edm4hep.MCRecoTrackParticleAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(track) | ||
assoc.setSim(particle) | ||
frame.put(assocs, "MCRecoTrackParticleAssociationCollection") | ||
|
||
assocs = edm4hep.RecoParticleVertexAssociationCollection() | ||
assoc = assocs.create() | ||
assoc.setWeight(next(counter)) | ||
assoc.setRec(reco_particle) | ||
assoc.setVertex(v) | ||
frame.put(assocs, "MCRecoParticleVertexAssociationCollection") | ||
|
||
timeseries = edm4hep.TimeSeriesCollection() | ||
serie = timeseries.create() | ||
serie.setCellID(next(counter)) | ||
serie.setTime(next(counter)) | ||
serie.setInterval(next(counter)) | ||
for j in range(vectorsize): | ||
serie.addToAmplitude(next(counter)) | ||
frame.put(timeseries, "TimeSeriesCollection") | ||
|
||
recdqdx = edm4hep.RecDqdxCollection() | ||
dqdx = recdqdx.create() | ||
q = edm4hep.Quantity() | ||
q.type = next(counter) | ||
q.value = next(counter) | ||
q.error = next(counter) | ||
dqdx.setDQdx(q) | ||
dqdx.setTrack(track) | ||
frame.put(recdqdx, "RecDqdxCollection") | ||
|
||
gep_coll = edm4hep.GeneratorEventParametersCollection() | ||
gep = gep_coll.create() | ||
gep.setEventScale(next(counter)) | ||
gep.setAlphaQED(next(counter)) | ||
gep.setAlphaQCD(next(counter)) | ||
gep.setSignalProcessId(next(counter)) | ||
gep.setSqrts(next(counter)) | ||
|
||
for i in range(vectorsize): | ||
gep.addToCrossSections(next(counter)) | ||
gep.addToCrossSectionErrors(next(counter)) | ||
gep.addToSignalVertex(particle) | ||
|
||
gpi_coll = edm4hep.GeneratorPdfInfoCollection() | ||
gpi = gpi_coll.create() | ||
# Doesn't work with ROOT 6.30.06 | ||
# gpi.setPartonId([next(counter), next(counter)]) | ||
gpi.setPartonId(0, next(counter)) | ||
gpi.setPartonId(1, next(counter)) | ||
# Doesn't work with ROOT 6.30.06 | ||
# gpi.setLhapdfId([next(counter), next(counter)]) | ||
gpi.setLhapdfId(0, next(counter)) | ||
gpi.setLhapdfId(1, next(counter)) | ||
# Doesn't work with ROOT 6.30.06 | ||
# gpi.setX([next(counter), next(counter)]) | ||
gpi.setX(0, next(counter)) | ||
gpi.setX(1, next(counter)) | ||
# Doesn't work with ROOT 6.30.06 | ||
# gpi.setXf([next(counter), next(counter)]) | ||
gpi.setXf(0, next(counter)) | ||
gpi.setXf(1, next(counter)) | ||
gpi.setScale(next(counter)) | ||
|
||
writer.write_frame(frame, "events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We either need to add the podio that we have used for building here as well to
PYTHONPATH
, or we have to do it in the workflow file in the step where we build and install podio. CI is partially failing, because it picks up an older version of podio from the LCG stack:https://github.com/key4hep/EDM4hep/actions/runs/10045234420/job/27762066625?pr=272#step:4:817