delphi-edm4hep
DELPHI SDST/FDST -> EDM4hep converter
Loading...
Searching...
No Matches
CollectionWriter.cpp
Go to the documentation of this file.
1// Provenance registry.
2//
3// Collects the source of every collection emitted during a job and prints a
4// summary at the end. The summary flags collections whose values SKELANA
5// produced but whose name carries a DST bank mnemonic, since that naming
6// implies a transcription.
7
9
10#include <algorithm>
11#include <cstdio>
12#include <iostream>
13#include <map>
14#include <string>
15
16namespace delphi_edm4hep {
17
18namespace {
19
20// Collection name -> provenance, first writer wins. Collections are emitted
21// once per event, so this deduplicates across the job.
22std::map<std::string, Provenance, std::less<>> g_seen;
23
24// PA extra-module (blocklet) names accepted by PHDST's LPHPA, plus the
25// event-level bank mnemonics used in collection names. A name built from one
26// of these asserts that its values are stored DST content.
27constexpr std::string_view kBankMnemonics[] = {
28 "BSP", "EL", "ELID", "ELTR", "EMCA", "EMNC", "HAID", "HCAL",
29 "HCMU", "HCNC", "HCRO", "MAIN", "MRIC", "MTPC", "MU", "MUFI",
30 "MUID", "ODHI", "PHC", "PHOT", "PXTD", "SSTC", "STIC", "STSH", "TBL", "TDHA",
31 "TDID",
32 "TDVD", "TEAD", "TEFA", "TEFB", "TEID", "TEOD", "TERB", "TERF", "TEST",
33 "TETP", "TEVF", "TOF", "TRAC", "TRAX", "V0",
34};
35
36// Extract the "<bank>" field of "<source>_<bank>_<readable>".
37std::string_view bankField(std::string_view name) {
38 const auto first = name.find('_');
39 if (first == std::string_view::npos) return {};
40 const auto second = name.find('_', first + 1);
41 if (second == std::string_view::npos) return {};
42 return name.substr(first + 1, second - first - 1);
43}
44
45bool isBankMnemonic(std::string_view bank) {
46 return std::find(std::begin(kBankMnemonics), std::end(kBankMnemonics), bank)
47 != std::end(kBankMnemonics);
48}
49
50} // namespace
51
52std::optional<edm4hep::ReconstructedParticle>
54 // Pass 1. TrackingWriter set ctx_.tracking earlier in this event, and its
55 // pa_to_particle is indexed by the same PA-walk position.
56 if (ctx_.tracking) {
57 const auto& t = *ctx_.tracking;
58 // The map stops at the last PA Tracking looked at, so a higher index is
59 // simply absent rather than an error.
60 if (paIdx < 0 || paIdx >= static_cast<int>(t.pa_to_particle.size())) {
61 return std::nullopt;
62 }
63 // -1 marks a PA that yielded no particle -- Tracking skips those it cannot
64 // give a momentum.
65 const int i = t.pa_to_particle[paIdx];
66 if (i < 0) return std::nullopt;
67 return t.particle_handles[i];
68 }
69
70 // Pass 2. ctx_.tracking is unset here because TrackingWriter does not run;
71 // MatchProvenanceWriter supplies the perigee match instead, whose entries
72 // index the fDST particles cloned from pass 1.
73 if (ctx_.fdst_pa_to_sdst_particle) {
74 const auto& map = *ctx_.fdst_pa_to_sdst_particle;
75 if (paIdx < 0 || paIdx >= static_cast<int>(map.size())) return std::nullopt;
76 // -1 marks a fullDST PA whose perigee matched no pass-1 track.
77 const int i = map[paIdx];
78 if (i < 0) return std::nullopt;
79 const auto& particles =
80 frame_.get<edm4hep::ReconstructedParticleCollection>(
81 makeName("MAIN", "Particles"));
82 // Guard the clone being shorter than the map: better to return nothing
83 // than to link the wrong particle.
84 if (i >= static_cast<int>(particles.size())) return std::nullopt;
85 return particles[i];
86 }
87
88 // Neither map is present -- the writer is running before Tracking or
89 // MatchProvenance, or on an event where they produced nothing.
90 return std::nullopt;
91}
92
93const char* label(Provenance prov) {
94 switch (prov) {
95 case Provenance::Derived: return "derived";
96 case Provenance::Custom: return "custom";
97 default: return "transcribed";
98 }
99}
100
101void noteProvenance(std::string_view name, Provenance prov) {
102 g_seen.emplace(std::string(name), prov);
103}
104
106 ProvenanceRecord record;
107 record.collections.reserve(g_seen.size());
108 record.sources.reserve(g_seen.size());
109 for (const auto& [name, prov] : g_seen) {
110 record.collections.push_back(name);
111 record.sources.emplace_back(label(prov));
112 }
113 return record;
114}
115
117 if (g_seen.empty()) return;
118
119 std::size_t transcribed = 0, derived = 0, custom = 0;
120 std::cout << "delphi_edm4hep: provenance summary\n";
121 for (const auto& [name, prov] : g_seen) {
122 switch (prov) {
123 case Provenance::Transcribed: ++transcribed; break;
124 case Provenance::Derived: ++derived; break;
125 case Provenance::Custom: ++custom; break;
126 }
127 // A bank mnemonic asserts stored DST content, so anything not
128 // transcribed is wearing a name it has not earned.
129 const bool mismatch = prov != Provenance::Transcribed
130 && isBankMnemonic(bankField(name));
131 std::printf(" %-11s %s%s\n", label(prov), name.c_str(),
132 mismatch ? " [bank mnemonic]" : "");
133 }
134 std::cout << " " << transcribed << " transcribed, " << derived
135 << " derived, " << custom << " custom\n";
136}
137
138} // namespace delphi_edm4hep
std::string makeName(std::string_view bank, std::string_view readable) const
std::optional< edm4hep::ReconstructedParticle > particleForPa(int paIdx) const
void noteProvenance(std::string_view name, Provenance prov)
ProvenanceRecord provenanceRecord()
const char * label(Provenance prov)
std::vector< std::string > sources
std::vector< std::string > collections