delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
MainHybrid.cpp
Go to the documentation of this file.
1// MainHybridWriter — pass-2 implementation.
2
4
5#include <edm4hep/ClusterCollection.h>
6#include <edm4hep/MutableReconstructedParticle.h>
7#include <edm4hep/MutableVertex.h>
8#include <edm4hep/ReconstructedParticleCollection.h>
9#include <edm4hep/TrackCollection.h>
10#include <edm4hep/VertexCollection.h>
11
12#include <cstddef>
13#include <string>
14
16
17namespace {
18
19// Clone the scalar (non-relation) fields of a RecoParticle.
20void cloneScalars(edm4hep::MutableReconstructedParticle dst,
21 const edm4hep::ReconstructedParticle& src) {
22 dst.setPDG (src.getPDG());
23 dst.setEnergy (src.getEnergy());
24 dst.setMomentum (src.getMomentum());
25 dst.setReferencePoint(src.getReferencePoint());
26 dst.setCharge (src.getCharge());
27 dst.setMass (src.getMass());
28 dst.setGoodnessOfPID (src.getGoodnessOfPID());
29 dst.setCovMatrix (src.getCovMatrix());
30}
31
32// Clone the scalar fields of a Vertex.
33void cloneScalars(edm4hep::MutableVertex dst, const edm4hep::Vertex& src) {
34 dst.setType (src.getType());
35 dst.setChi2 (src.getChi2());
36 dst.setNdf (src.getNdf());
37 dst.setPosition (src.getPosition());
38 dst.setCovMatrix (src.getCovMatrix());
39 dst.setAlgorithmType(src.getAlgorithmType());
40 for (std::size_t k = 0; k < src.parameters_size(); ++k) {
41 dst.addToParameters(src.getParameters(k));
42 }
43}
44
45// Clone a Vertex collection (scalars only — caller re-points the
46// particles relation via the fDST particle map).
47void cloneVerticesWithRepointedParticles(
48 const edm4hep::VertexCollection& src,
49 edm4hep::VertexCollection& dst,
50 const edm4hep::ReconstructedParticleCollection& fdst_particles)
51{
52 const std::size_t n_fdst = fdst_particles.size();
53 for (const auto& sv : src) {
54 auto dv = dst.create();
55 cloneScalars(dv, sv);
56 for (const auto& sp : sv.getParticles()) {
57 const auto idx = sp.getObjectID().index;
58 if (idx >= 0 && static_cast<std::size_t>(idx) < n_fdst) {
59 dv.addToParticles(fdst_particles[idx]);
60 }
61 }
62 }
63}
64
65} // namespace
66
68{
69 const auto& sdst_particles =
70 frame_.get<edm4hep::ReconstructedParticleCollection>(sdstName("MAIN", "Particles"));
71 const auto& fdst_tracks =
72 frame_.get<edm4hep::TrackCollection>("fDST_TRAC_Tracks");
73 const std::size_t n_fdst_tracks = fdst_tracks.size();
74
75 // Calorimeter showers. ShowerHybridWriter runs BEFORE this writer and
76 // has already cloned the sDST EM/HAD neutral showers into the fDST_*
77 // collections (1:1 order). The Particle→Cluster relation lives on the
78 // particle, so it must be re-pointed here while main_out is mutable
79 // (it cannot be wired from the cluster side after the fact). sDST
80 // particles reference both sDST_EMNC and sDST_HCNC, so disambiguate by
81 // the reference's collectionID.
82 const auto& fdst_emnc = frame_.get<edm4hep::ClusterCollection>("fDST_EMNC_Showers");
83 const auto& fdst_hcnc = frame_.get<edm4hep::ClusterCollection>("fDST_HCNC_Showers");
84 const auto emnc_id = frame_.get<edm4hep::ClusterCollection>(sdstName("EMNC", "Showers")).getID();
85 const auto hcnc_id = frame_.get<edm4hep::ClusterCollection>(sdstName("HCNC", "Showers")).getID();
86 const std::size_t n_emnc = fdst_emnc.size();
87 const std::size_t n_hcnc = fdst_hcnc.size();
88
89 // Phase 1: create fDST_MAIN_Particles with scalars only (no relations
90 // yet — subParticles need the full collection to exist first).
91 edm4hep::ReconstructedParticleCollection main_out;
92 for (const auto& sp : sdst_particles) {
93 auto dp = main_out.create();
94 cloneScalars(dp, sp);
95 }
96
97 // Phase 2: wire relations. Tracks re-point to fDST_TRAC_Tracks
98 // (sDST_TRAC_Tracks[i] ↔ fDST_TRAC_Tracks[i], 1:1 order). Clusters
99 // re-point to the fDST EMNC/HCNC shower clones (also 1:1 order).
100 // ParticleIDs are NOT wired here — they are stored on the ParticleID
101 // side, so PidHybridWriter re-points them via setParticle() after this
102 // writer runs. SubParticles (e.g. V0 daughters listed on a parent
103 // particle) re-point to the newly-created fDST_MAIN_Particles entries.
104 for (std::size_t i = 0; i < sdst_particles.size(); ++i) {
105 auto dp = main_out[i];
106 const auto& sp = sdst_particles[i];
107 for (const auto& st : sp.getTracks()) {
108 const auto idx = st.getObjectID().index;
109 if (idx >= 0 && static_cast<std::size_t>(idx) < n_fdst_tracks) {
110 dp.addToTracks(fdst_tracks[idx]);
111 }
112 }
113 for (const auto& sc : sp.getClusters()) {
114 const auto oid = sc.getObjectID();
115 if (oid.collectionID == emnc_id &&
116 oid.index >= 0 && static_cast<std::size_t>(oid.index) < n_emnc) {
117 dp.addToClusters(fdst_emnc[oid.index]);
118 } else if (oid.collectionID == hcnc_id &&
119 oid.index >= 0 && static_cast<std::size_t>(oid.index) < n_hcnc) {
120 dp.addToClusters(fdst_hcnc[oid.index]);
121 }
122 }
123 for (const auto& sub : sp.getParticles()) {
124 const auto idx = sub.getObjectID().index;
125 if (idx >= 0 && static_cast<std::size_t>(idx) < main_out.size()) {
126 dp.addToParticles(main_out[idx]);
127 }
128 }
129 }
130
131 // Vertex cascade. Each of PV / Vertices / V0 / PHC is a separate
132 // VertexCollection; re-emit with addToParticles re-pointed.
133 edm4hep::VertexCollection pv_out;
134 edm4hep::VertexCollection vtx_out;
135 edm4hep::VertexCollection v0_out;
136 edm4hep::VertexCollection phc_out;
137
138 const auto& sdst_pv = frame_.get<edm4hep::VertexCollection>(sdstName("PV", "PrimaryVertex"));
139 const auto& sdst_vtx = frame_.get<edm4hep::VertexCollection>(sdstName("PV", "Vertices"));
140 const auto& sdst_v0 = frame_.get<edm4hep::VertexCollection>(sdstName("V0", "V0Candidates"));
141 const auto& sdst_phc = frame_.get<edm4hep::VertexCollection>(sdstName("PHC", "PhotonConversions"));
142
143 cloneVerticesWithRepointedParticles(sdst_pv, pv_out, main_out);
144 cloneVerticesWithRepointedParticles(sdst_vtx, vtx_out, main_out);
145 cloneVerticesWithRepointedParticles(sdst_v0, v0_out, main_out);
146 cloneVerticesWithRepointedParticles(sdst_phc, phc_out, main_out);
147
148 put(std::move(main_out), "MAIN", "Particles", Provenance::Derived);
149 put(std::move(pv_out), "PV", "PrimaryVertex", Provenance::Transcribed);
150 put(std::move(vtx_out), "PV", "Vertices", Provenance::Transcribed);
151 put(std::move(v0_out), "V0", "V0Candidates", Provenance::Transcribed);
152 put(std::move(phc_out), "PHC", "PhotonConversions", Provenance::Transcribed);
153}
154
155} // namespace delphi_edm4hep::main_hybrid
std::string sdstName(std::string_view bank, std::string_view readable) const
const Coll & put(Coll &&coll, std::string_view bank, std::string_view readable, Provenance prov)