delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
PidHybrid.cpp
Go to the documentation of this file.
1// PidHybridWriter — pass-2 implementation.
2
4
5#include <edm4hep/MutableParticleID.h>
6#include <edm4hep/MutableRecDqdx.h>
7#include <edm4hep/ParticleIDCollection.h>
8#include <edm4hep/RecDqdxCollection.h>
9#include <edm4hep/ReconstructedParticleCollection.h>
10#include <edm4hep/TrackCollection.h>
11
12#include <cstddef>
13#include <stdexcept>
14#include <string>
15
17
18namespace {
19
20// dE/dx collections are named for the routine that produced them, and which
21// routine runs depends on the DST version (ParticleId.cpp), so exactly one of
22// these is on the pass-1 frame. Resolve it by presence and keep the name.
23constexpr const char* kDedxAlgorithms[] = {"BBDXGET", "GETDEDX"};
24
25std::string dedxAlgorithm(const podio::Frame& frame) {
26 for (const char* algo : kDedxAlgorithms) {
27 if (frame.get(bank::make(bank::Pass::Sdst, algo, "Dedx"))) return algo;
28 }
29 throw std::runtime_error(
30 "pass-1 frame carries no dE/dx collection; expected sDST_BBDXGET_Dedx"
31 " or sDST_GETDEDX_Dedx");
32}
33
34// Clone a ParticleID collection, re-pointing setParticle() at the
35// matching fDST_MAIN_Particles entry (by 1:1 identity index).
36void clonePidWithRepoint(
37 const edm4hep::ParticleIDCollection& src,
38 edm4hep::ParticleIDCollection& dst,
39 const edm4hep::ReconstructedParticleCollection& fdst_particles)
40{
41 const std::size_t n_fdst = fdst_particles.size();
42 for (const auto& sp : src) {
43 auto dp = dst.create();
44 dp.setType (sp.getType());
45 dp.setPDG (sp.getPDG());
46 dp.setAlgorithmType(sp.getAlgorithmType());
47 dp.setLikelihood (sp.getLikelihood());
48 for (std::size_t k = 0; k < sp.parameters_size(); ++k) {
49 dp.addToParameters(sp.getParameters(k));
50 }
51 const auto& src_part = sp.getParticle();
52 const auto idx = src_part.getObjectID().index;
53 if (idx >= 0 && static_cast<std::size_t>(idx) < n_fdst) {
54 dp.setParticle(fdst_particles[idx]);
55 }
56 }
57}
58
59} // namespace
60
62{
63 const auto& fdst_particles =
64 frame_.get<edm4hep::ReconstructedParticleCollection>("fDST_MAIN_Particles");
65 const auto& fdst_tracks =
66 frame_.get<edm4hep::TrackCollection>("fDST_TRAC_Tracks");
67 const std::size_t n_fdst_tracks = fdst_tracks.size();
68
69 const std::string dedx_algo = dedxAlgorithm(frame_);
70 const std::string dedx_name = sdstName(dedx_algo, "Dedx");
71
72 edm4hep::ParticleIDCollection haid_out, muid_out, elid_out, dedx_out;
73 clonePidWithRepoint(
74 frame_.get<edm4hep::ParticleIDCollection>(sdstName("HAID", "HadronID")),
75 haid_out, fdst_particles);
76 clonePidWithRepoint(
77 frame_.get<edm4hep::ParticleIDCollection>(sdstName("MUID", "MuonID")),
78 muid_out, fdst_particles);
79 clonePidWithRepoint(
80 frame_.get<edm4hep::ParticleIDCollection>(sdstName("ELID", "ElectronID")),
81 elid_out, fdst_particles);
82 clonePidWithRepoint(
83 frame_.get<edm4hep::ParticleIDCollection>(dedx_name),
84 dedx_out, fdst_particles);
85
86 // RecDqdx: scalar (Quantity DQdx) + setTrack relation.
87 edm4hep::RecDqdxCollection dqdx_out;
88 const auto& sdst_dqdx =
89 frame_.get<edm4hep::RecDqdxCollection>(dedx_name + "RecDqdx");
90 for (const auto& sq : sdst_dqdx) {
91 auto dq = dqdx_out.create();
92 dq.setDQdx(sq.getDQdx());
93 const auto& src_trk = sq.getTrack();
94 const auto tidx = src_trk.getObjectID().index;
95 if (tidx >= 0 && static_cast<std::size_t>(tidx) < n_fdst_tracks) {
96 dq.setTrack(fdst_tracks[tidx]);
97 }
98 }
99
100 put(std::move(haid_out), "HAID", "HadronID", Provenance::Transcribed);
101 put(std::move(muid_out), "MUID", "MuonID", Provenance::Transcribed);
102 put(std::move(elid_out), "ELID", "ElectronID", Provenance::Transcribed);
103 put(std::move(dedx_out), dedx_algo, "Dedx", Provenance::Derived);
104 put(std::move(dqdx_out), dedx_algo, "DedxRecDqdx", Provenance::Derived);
105}
106
107} // namespace delphi_edm4hep::pid_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)
std::string make(Pass pass, std::string_view bank, std::string_view readable_name)