hpstr
The Heavy Photon Search Toolkit for Reconstruction (hpstr) provides an interface to physics data from the HPS experiment saved in the LCIO format and converts it into an ROOT based format.
Loading...
Searching...
No Matches
TruthVertexMatch2021.cxx
Go to the documentation of this file.
1/*
2 * @file TruthVertexMatch2021.cxx
3 * @brief Truth-A' -> reconstructed-vertex matching study for 2021 displaced A'/SIMP signal MC.
4 * @author Matthew Gignac
5 * @date June 2026
6 */
7
9
10#include <cmath>
11#include <set>
12
14 isData_ = parameters.getInteger("isData", 0) != 0;
15 isApSignal_ = parameters.getInteger("isApSignal", 0) != 0;
16 isSimpSignal_ = parameters.getInteger("isSimpSignal", 0) != 0;
17 apPDG_ = parameters.getInteger("apPDG", apPDG_);
18 calTimeOffset_ = parameters.getDouble("calTimeOffset", 0.0);
19 vertexMatchZ_ = parameters.getDouble("vertexMatchZ", vertexMatchZ_);
20 massSelect_ = parameters.getDouble("massSelect", massSelect_);
21 massWindow_ = parameters.getDouble("massWindow", massWindow_);
22 vtxZMin_ = parameters.getDouble("vtxZMin", vtxZMin_);
23 vtxZMax_ = parameters.getDouble("vtxZMax", vtxZMax_);
24 eleMomCut_ = parameters.getDouble("eleMomCut", eleMomCut_);
25 eleClusEMin_ = parameters.getDouble("eleClusEMin", eleClusEMin_);
26 debug_ = parameters.getInteger("debug", 0) != 0;
27
28 auto vtxColl = parameters.getString("vtxCollection", "");
29 if (not vtxColl.empty()) vtxColl_ = vtxColl;
30 auto hitColl = parameters.getString("hitCollection", "");
31 if (not hitColl.empty()) hitColl_ = hitColl;
32
33 // mother PDG of the signal e+/e-: 625 for SIMP vector decay, apPDG_ for A'
35
36 std::cout << "[TruthVertexMatch2021] isData=" << isData_ << " isApSignal=" << isApSignal_
37 << " isSimpSignal=" << isSimpSignal_ << " signalMomPDG=" << signalMomPDG_
38 << " vtxColl=" << vtxColl_;
39 if (massSelect_ > 0.)
40 std::cout << " massSelect=" << massSelect_ << " +/- " << massWindow_ << " GeV";
41 if (vtxZMin_ > -1.0e8 || vtxZMax_ < 1.0e8)
42 std::cout << " vtxZ in [" << vtxZMin_ << ", " << vtxZMax_ << "] mm";
43 std::cout << " eleMomCut=" << eleMomCut_ << " GeV eleClusEMin=" << eleClusEMin_ << " GeV" << std::endl;
44}
45
47 _ah = std::make_shared<AnaHelpers>();
48
49 bus_.board_input<EventHeader>(tree, "EventHeader");
50 bus_.board_input<TSData>(tree, "TSBank");
51 if (not trkColl_.empty()) bus_.board_input<std::vector<Track*>>(tree, trkColl_);
52 bus_.board_input<std::vector<Vertex*>>(tree, vtxColl_);
53 bus_.board_input<std::vector<CalCluster*>>(tree, "RecoEcalClusters");
54 if (not isData_ and not mcColl_.empty()) bus_.board_input<std::vector<MCParticle*>>(tree, mcColl_);
55
56 // Truth-hit collection is only present when the upstream recoTuple_cfg.py run had
57 // -sh/--truthHits 1 enabled. board_input() throws if the branch is missing, so check for the
58 // branch on the tree first and simply skip boarding it if absent -- n_truth_hits_ele/pos will
59 // report the -1 sentinel in that case rather than the processor crashing or aborting the run.
60 hasTruthHits_ = (not isData_) && (tree->GetBranch(hitColl_.c_str()) != nullptr);
61 if (hasTruthHits_) {
62 bus_.board_input<std::vector<TrackerHit*>>(tree, hitColl_);
63 } else if (not isData_) {
64 std::cout << "[TruthVertexMatch2021] NOTE: truth-hit collection '" << hitColl_
65 << "' not found in input tree -- n_truth_hits_ele/pos will use the -1 sentinel."
66 << std::endl;
67 }
68
69 // quick at-a-glance efficiency chain (in addition to the tree)
70 event_cf_.add("truth_signal_found", 2, -0.5, 1.5);
71 event_cf_.add("single_truth_signal", 2, -0.5, 1.5);
72 event_cf_.add("at_least_one_v0", 10, 0.0, 10.0);
73 event_cf_.add("v0_within_z", 50, 0.0, 50.0);
74 event_cf_.init();
75 event_cf_.set_label_names({"readout", "truth A' (or VD) found", "exactly one truth signal",
76 "N_{v0}^{raw} #geq 1", "v0 within z of truth"});
77}
78
79void TruthVertexMatch2021::setFile(TFile* out_file) {
80 Processor::setFile(out_file);
81
82 output_tree_ = std::make_unique<TTree>("truth_vertex_match", "Truth A' -> reco vertex matching", 99, out_file);
83
84 bus_.board_output<double>(output_tree_.get(), "weight");
85 bus_.board_output<int>(output_tree_.get(), "run");
86 bus_.board_output<int>(output_tree_.get(), "event");
87
88 // truth signal-mother (A' / SIMP vector) quantities
89 bus_.board_output<int>(output_tree_.get(), "n_signal"); // number of truth signal mothers found
90 bus_.board_output<bool>(output_tree_.get(), "truth_signal_found");
91 bus_.board_output<TVector3>(output_tree_.get(), "true_ap_p");
92 for (const auto& name : {"true_ap_E", "true_ap_mass", "true_betagamma", "true_decay_len"}) {
93 bus_.board_output<double>(output_tree_.get(), name);
94 }
95 // truth decay vertex (A' endpoint = where e+e- are produced) and production vertex
96 for (const auto& name : {"true_vtx_x", "true_vtx_y", "true_vtx_z",
97 "true_prod_x", "true_prod_y", "true_prod_z"}) {
98 bus_.board_output<double>(output_tree_.get(), name);
99 }
100
101 // truth e+/e- decay products
102 bus_.board_output<TVector3>(output_tree_.get(), "true_ele_p");
103 bus_.board_output<TVector3>(output_tree_.get(), "true_pos_p");
104 for (const auto& name : {"true_ele_E", "true_pos_E", "true_vertex_invM", "true_vertex_psum"}) {
105 bus_.board_output<double>(output_tree_.get(), name);
106 }
107 // truth e+/e- geometry/kinematics (production momentum): pt, pz, polar angle, and the
108 // bend-plane (px/pz) and vertical (py/pz ~ tanLambda) slopes -> for studying acceptance.
109 for (const auto& name : {"true_ele_pt", "true_ele_pz", "true_ele_theta", "true_ele_slopeX", "true_ele_slopeY",
110 "true_pos_pt", "true_pos_pz", "true_pos_theta", "true_pos_slopeX", "true_pos_slopeY"}) {
111 bus_.board_output<double>(output_tree_.get(), name);
112 }
113
114 // reconstructed track inventory (KalmanFullTracks, no truth match)
115 bus_.board_output<int>(output_tree_.get(), "n_trk_pos"); // # positive (e+) tracks in event
116 bus_.board_output<int>(output_tree_.get(), "n_trk_ele"); // # negative (e-) tracks in event
117
118 // reconstructed-vertex matching (step 2)
119 bus_.board_output<int>(output_tree_.get(), "n_v0"); // number of reco v0s (no selection)
120 bus_.board_output<bool>(output_tree_.get(), "has_v0"); // n_v0 >= 1
121 bus_.board_output<bool>(output_tree_.get(), "matched_z"); // >=1 v0 within vertexMatchZ_ in z of truth
122 // properties of the closest-in-z v0 (sentinels when no v0):
123 for (const auto& name : {"best_dz", "best_vtx_x", "best_vtx_y", "best_vtx_z", "best_vtx_chi2"}) {
124 bus_.board_output<double>(output_tree_.get(), name);
125 }
126
127 // track-based vertex matching (step 3): matches the v0's two constituent tracks to the truth
128 // e-/e+ by majority-hit MCParticle ID, independent of the reconstructed vertex z.
129 bus_.board_output<int>(output_tree_.get(), "n_v0_trk_matched"); // # v0s that are track-matched
130 bus_.board_output<bool>(output_tree_.get(), "matched_trk"); // n_v0_trk_matched >= 1
131 bus_.board_output<bool>(output_tree_.get(), "matched_z_or_trk");
132 bus_.board_output<bool>(output_tree_.get(), "matched_z_and_trk");
133 // z offset (reco - truth) of the closest-in-z track-matched v0 (sentinel when none):
134 bus_.board_output<double>(output_tree_.get(), "best_trk_dz");
135 // single-leg track matches (no requirement on the other leg): how often does a v0 capture
136 // just the truth e- or just the truth e+, vs. both (matched_trk above)?
137 bus_.board_output<bool>(output_tree_.get(), "matched_trk_ele");
138 bus_.board_output<bool>(output_tree_.get(), "matched_trk_pos");
139
140 // Truth-hit counts: number of 2D SVT hits (RotatedHelicalTrackHits, tagged via
141 // SVTTrueHitRelations) whose majority-contributing MCParticle is the truth e-/e+, i.e. real
142 // detector hits the true signal track left behind, independent of whether tracking/vertexing
143 // succeeded. Lets us bin v0-finding efficiency vs. the number of truth hits available.
144 // has_truth_hits distinguishes "0 real hits" from "collection unavailable in this file"
145 // (n_truth_hits_ele/pos report the -1 sentinel in the latter case).
146 bus_.board_output<bool>(output_tree_.get(), "has_truth_hits");
147 bus_.board_output<int>(output_tree_.get(), "n_truth_hits_ele");
148 bus_.board_output<int>(output_tree_.get(), "n_truth_hits_pos");
149 // Unique-layer versions of the above: TrackerHit::getLayer() distinguishes axial vs. stereo
150 // sub-layers (0-13 for the 7-layer SVT), and the same physical hit can occasionally show up
151 // twice (split-cluster artifacts) -- both inflate a raw hit count without adding real tracking
152 // information. n_truth_layers_ele/pos count DISTINCT layer_ values instead, so they saturate
153 // at the number of sub-layers the true track actually crossed.
154 bus_.board_output<int>(output_tree_.get(), "n_truth_layers_ele");
155 bus_.board_output<int>(output_tree_.get(), "n_truth_layers_pos");
156
157 // ECal cluster info for the truth-matched e-/e+ legs of the "best" track-matched v0, where
158 // "best" is picked by cluster-completeness tier first and z-closeness as a tiebreaker (NOT
159 // simply the closest-in-z v0 like best_trk_dz -- see the tiered selection in process(), which
160 // guarantees these branches describe a v0 actually consistent with matched_trk_clus_both /
161 // matched_trk_clus_pos_only when those are true). utils::buildParticle only calls setCluster()
162 // when the LCIO ReconstructedParticle had an associated cluster, so has_*_clus is a real
163 // "was this leg cluster-matched" flag. NOTE: presence is tested via getEnergy() > sentinel,
164 // NOT getNHits() -- utils::buildCalCluster never calls addHit(), so getNHits() is always 0
165 // even for a real cluster; energy/position/time ARE populated.
166 bus_.board_output<bool>(output_tree_.get(), "has_ele_clus");
167 bus_.board_output<bool>(output_tree_.get(), "has_pos_clus");
168 for (const auto& name : {"ele_clus_E", "ele_clus_x", "ele_clus_y", "ele_clus_z", "ele_clus_t",
169 "pos_clus_E", "pos_clus_x", "pos_clus_y", "pos_clus_z", "pos_clus_t"}) {
170 bus_.board_output<double>(output_tree_.get(), name);
171 }
172 // Reco track momentum (Kalman fit, |p|) of the same two legs, for track-p vs cluster-E
173 // (E/p-style) diagnostics.
174 bus_.board_output<double>(output_tree_.get(), "ele_trk_p");
175 bus_.board_output<double>(output_tree_.get(), "pos_trk_p");
176 // Tracker hit count (Track::getTrackerHitCount(), max 14 for a full 7-layer Kalman track) of
177 // the same two legs, for track-quality-vs-momentum studies.
178 bus_.board_output<int>(output_tree_.get(), "ele_trk_nhits");
179 bus_.board_output<int>(output_tree_.get(), "pos_trk_nhits");
180
181 // Cluster-aware track matching: quantifies what relaxing the electron ECal-cluster
182 // requirement gains over requiring clusters on BOTH legs (see finalize() for the headline
183 // acceptance-gain number). All are event-level ORs over v0s, same convention as matched_trk*.
184 bus_.board_output<bool>(output_tree_.get(), "matched_trk_ele_clus"); // >=1 v0: e- track has a cluster (any e+)
185 bus_.board_output<bool>(output_tree_.get(), "matched_trk_pos_clus"); // >=1 v0: e+ track has a cluster (any e-)
186 bus_.board_output<bool>(output_tree_.get(), "matched_trk_clus_both"); // >=1 v0: both tracks + both clusters
187 bus_.board_output<bool>(output_tree_.get(), "matched_trk_clus_pos_only"); // >=1 v0: both tracks + e+ cluster only
188 bus_.board_output<bool>(output_tree_.get(), "matched_trk_clus_ele_only"); // >=1 v0: both tracks + e- cluster only
189}
190
192 // Reset output branches so conditionally-filled ones do not carry over the
193 // previous event's value into this event's entry.
194 bus_.clear_outputs();
195
196 const auto& eh{bus_.get<EventHeader>("EventHeader")};
197
198 event_cf_.begin_event();
199
200 // ----- defaults (sentinels) -----
201 bus_.set("weight", 1.);
202 bus_.set("run", eh.getRunNumber());
203 bus_.set("event", eh.getEventNumber());
204 bus_.set("n_signal", 0);
205 bus_.set("truth_signal_found", false);
206 bus_.set("true_ap_p", TVector3(0., 0., 0.));
207 bus_.set("true_ele_p", TVector3(0., 0., 0.));
208 bus_.set("true_pos_p", TVector3(0., 0., 0.));
209 for (const auto& name : {"true_ap_E", "true_ap_mass", "true_betagamma", "true_decay_len",
210 "true_vtx_x", "true_vtx_y", "true_vtx_z",
211 "true_prod_x", "true_prod_y", "true_prod_z",
212 "true_ele_E", "true_pos_E", "true_vertex_invM", "true_vertex_psum",
213 "true_ele_pt", "true_ele_pz", "true_ele_theta", "true_ele_slopeX", "true_ele_slopeY",
214 "true_pos_pt", "true_pos_pz", "true_pos_theta", "true_pos_slopeX", "true_pos_slopeY"}) {
215 bus_.set<double>(name, -9999.);
216 }
217 bus_.set("n_trk_pos", 0);
218 bus_.set("n_trk_ele", 0);
219 bus_.set("n_v0", 0);
220 bus_.set("has_v0", false);
221 bus_.set("matched_z", false);
222 for (const auto& name : {"best_dz", "best_vtx_x", "best_vtx_y", "best_vtx_z", "best_vtx_chi2"}) {
223 bus_.set<double>(name, -9999.);
224 }
225 bus_.set("n_v0_trk_matched", 0);
226 bus_.set("matched_trk", false);
227 bus_.set("matched_z_or_trk", false);
228 bus_.set("matched_z_and_trk", false);
229 bus_.set<double>("best_trk_dz", -9999.);
230 bus_.set("matched_trk_ele", false);
231 bus_.set("matched_trk_pos", false);
232 bus_.set("has_truth_hits", hasTruthHits_);
233 bus_.set<int>("n_truth_hits_ele", -1);
234 bus_.set<int>("n_truth_hits_pos", -1);
235 bus_.set<int>("n_truth_layers_ele", -1);
236 bus_.set<int>("n_truth_layers_pos", -1);
237 bus_.set("has_ele_clus", false);
238 bus_.set("has_pos_clus", false);
239 for (const auto& name : {"ele_clus_E", "ele_clus_x", "ele_clus_y", "ele_clus_z", "ele_clus_t",
240 "pos_clus_E", "pos_clus_x", "pos_clus_y", "pos_clus_z", "pos_clus_t",
241 "ele_trk_p", "pos_trk_p"}) {
242 bus_.set<double>(name, -9999.);
243 }
244 bus_.set<int>("ele_trk_nhits", 0);
245 bus_.set<int>("pos_trk_nhits", 0);
246 bus_.set("matched_trk_ele_clus", false);
247 bus_.set("matched_trk_pos_clus", false);
248 bus_.set("matched_trk_clus_both", false);
249 bus_.set("matched_trk_clus_pos_only", false);
250 bus_.set("matched_trk_clus_ele_only", false);
251
252 if (not bus_.has(mcColl_)) {
253 // no truth available (e.g. data): nothing to do for this study
254 output_tree_->Fill();
255 return true;
256 }
257
258 // ===================== truth block (mirrors PreselectAndCategorize2021) =====================
259 // Find the signal mother (A' = apPDG_, or SIMP vector = 625) and its e+/e- decay products,
260 // identified by PDG and mother-PDG. n_signal counts the mothers so we can drop the very rare
261 // double-signal / no-signal beam-overlay events downstream.
262 const auto& mcParticles{bus_.get<std::vector<MCParticle*>>(mcColl_)};
263 MCParticle* mother{nullptr};
264 int n_signal{0};
265 double true_vtx_z = -9999.; // truth decay-vertex z (A' endpoint), used for reco matching
266 ROOT::Math::PxPyPzEVector trueEleP, truePosP;
267 bool foundEle{false}, foundPos{false};
268 // LCIO IDs of the truth e-/e+, used for track-based vertex matching (independent of z)
269 int trueEleID{-1}, truePosID{-1};
270 for (MCParticle* ptr : mcParticles) {
271 std::vector<double> lP = ptr->getMomentum();
272 if (ptr->getPDG() == apPDG_ || (isSimpSignal_ && ptr->getPDG() == 625)) {
273 n_signal++;
274 mother = ptr;
275 } else if (ptr->getPDG() == 11 && ptr->getMomPDG() == signalMomPDG_) {
276 trueEleP = ROOT::Math::PxPyPzEVector(lP.at(0), lP.at(1), lP.at(2), ptr->getEnergy());
277 trueEleID = ptr->getID();
278 foundEle = true;
279 } else if (ptr->getPDG() == -11 && ptr->getMomPDG() == signalMomPDG_) {
280 truePosP = ROOT::Math::PxPyPzEVector(lP.at(0), lP.at(1), lP.at(2), ptr->getEnergy());
281 truePosID = ptr->getID();
282 foundPos = true;
283 }
284 }
285
286 // Truth-hit counts: tally every 2D SVT hit by its majority... actually by ALL contributing
287 // MCParticle IDs (a hit can list more than one via addMCPartID in Tracker2DHitProcessor when
288 // multiple raw strip hits/particles feed the same 2D hit), then look up the counts for the
289 // specific truth e-/e+ found above. Building the full per-MCParticle map (rather than only
290 // counting hits matching trueEleID/truePosID as we go) keeps this generic across all truth
291 // particles in the event, not just the two signal legs.
292 if (hasTruthHits_) {
293 const auto& truthHits{bus_.get<std::vector<TrackerHit*>>(hitColl_)};
294 std::map<int, int> nHitsByMcpID;
295 std::map<int, std::set<int>> layersByMcpID;
296 for (TrackerHit* hit : truthHits) {
297 for (int mcpID : hit->getMCPartIDs()) {
298 nHitsByMcpID[mcpID]++;
299 layersByMcpID[mcpID].insert(hit->getLayer());
300 }
301 }
302 if (trueEleID >= 0) {
303 bus_.set<int>("n_truth_hits_ele", nHitsByMcpID[trueEleID]);
304 bus_.set<int>("n_truth_layers_ele", static_cast<int>(layersByMcpID[trueEleID].size()));
305 }
306 if (truePosID >= 0) {
307 bus_.set<int>("n_truth_hits_pos", nHitsByMcpID[truePosID]);
308 bus_.set<int>("n_truth_layers_pos", static_cast<int>(layersByMcpID[truePosID].size()));
309 }
310 }
311
312 // Optional truth e+e- invariant-mass window (slices the rad continuum to a fixed mass so it
313 // is a mass-matched control). Disabled when massSelect_ < 0.
314 bool mass_ok = true;
315 if (massSelect_ > 0.) {
316 double invM = (foundEle && foundPos) ? (trueEleP + truePosP).M() : -1.;
317 mass_ok = (invM > 0.) && (std::fabs(invM - massSelect_) < massWindow_);
318 }
319
320 // Optional truth decay-vertex z fiducial window (apply BEFORE any acceptance evaluation).
321 bool zfid_ok = true;
322 if (mother) {
323 double decay_z = mother->getEndPoint().at(2);
324 zfid_ok = (decay_z >= vtxZMin_ && decay_z <= vtxZMax_);
325 }
326
327 bool truth_signal_found = (n_signal > 0) && mass_ok && zfid_ok;
328 bus_.set("n_signal", n_signal);
329 bus_.set("truth_signal_found", truth_signal_found);
330
331 event_cf_.apply("truth_signal_found", truth_signal_found);
332 event_cf_.fill_nm1("truth_signal_found", truth_signal_found ? 1 : 0);
333 event_cf_.apply("single_truth_signal", n_signal == 1);
334 event_cf_.fill_nm1("single_truth_signal", n_signal);
335
336 if (foundEle) {
337 bus_.set("true_ele_p", TVector3(trueEleP.Px(), trueEleP.Py(), trueEleP.Pz()));
338 bus_.set<double>("true_ele_E", trueEleP.E());
339 bus_.set<double>("true_ele_pt", trueEleP.Pt());
340 bus_.set<double>("true_ele_pz", trueEleP.Pz());
341 bus_.set<double>("true_ele_theta", trueEleP.Theta());
342 bus_.set<double>("true_ele_slopeX", trueEleP.Pz() != 0 ? trueEleP.Px() / trueEleP.Pz() : -9999.);
343 bus_.set<double>("true_ele_slopeY", trueEleP.Pz() != 0 ? trueEleP.Py() / trueEleP.Pz() : -9999.);
344 }
345 if (foundPos) {
346 bus_.set("true_pos_p", TVector3(truePosP.Px(), truePosP.Py(), truePosP.Pz()));
347 bus_.set<double>("true_pos_E", truePosP.E());
348 bus_.set<double>("true_pos_pt", truePosP.Pt());
349 bus_.set<double>("true_pos_pz", truePosP.Pz());
350 bus_.set<double>("true_pos_theta", truePosP.Theta());
351 bus_.set<double>("true_pos_slopeX", truePosP.Pz() != 0 ? truePosP.Px() / truePosP.Pz() : -9999.);
352 bus_.set<double>("true_pos_slopeY", truePosP.Pz() != 0 ? truePosP.Py() / truePosP.Pz() : -9999.);
353 }
354 if (foundEle && foundPos) {
355 bus_.set<double>("true_vertex_invM", (trueEleP + truePosP).M());
356 bus_.set<double>("true_vertex_psum", trueEleP.P() + truePosP.P());
357 }
358
359 if (mother) {
360 std::vector<double> prodPos = mother->getVertexPosition(); // where the A' is produced
361 std::vector<double> endPos = mother->getEndPoint(); // where the A' decays -> e+e- vertex
362 bus_.set<double>("true_prod_x", prodPos[0]);
363 bus_.set<double>("true_prod_y", prodPos[1]);
364 bus_.set<double>("true_prod_z", prodPos[2]);
365 bus_.set<double>("true_vtx_x", endPos[0]);
366 bus_.set<double>("true_vtx_y", endPos[1]);
367 bus_.set<double>("true_vtx_z", endPos[2]);
368 true_vtx_z = endPos[2];
369
370 // NOTE: for the A' MCParticle getVertexPosition()==getEndPoint() (both store the DECAY
371 // vertex), so end-prod is identically 0. The decay length is the decay-vertex distance
372 // from the target (~origin). (The same end-prod formula in PreselectAndCategorize2021
373 // likewise yields true_decay_len==0.)
374 bus_.set<double>("true_decay_len",
375 std::sqrt(endPos[0] * endPos[0] + endPos[1] * endPos[1] + endPos[2] * endPos[2]));
376
377 std::vector<double> mMom = mother->getMomentum();
378 double mp = std::sqrt(mMom[0] * mMom[0] + mMom[1] * mMom[1] + mMom[2] * mMom[2]);
379 bus_.set("true_ap_p", TVector3(mMom[0], mMom[1], mMom[2]));
380 bus_.set<double>("true_ap_E", mother->getEnergy());
381 bus_.set<double>("true_ap_mass", mother->getMass());
382 if (mother->getMass() > 0) bus_.set<double>("true_betagamma", mp / mother->getMass());
383 }
384
385 // ===================== reco-vertex matching (step 2) =====================
386 // Count reconstructed v0s (NO selection) and find the one closest in z to the truth vertex.
387 const auto& vtxs{bus_.get<std::vector<Vertex*>>(vtxColl_)};
388 int n_v0 = static_cast<int>(vtxs.size());
389 bool has_v0 = (n_v0 >= 1);
390
391 // Majority-hit truth match of a track (see utils::getTruthPDG): returns the LCIO ID of the
392 // MCParticle contributing the most hits to this track, or -1 if it has no truth-linked hits.
393 auto majorityMcpID = [](Track& trk) -> int {
394 std::map<int, int> count_per_id;
395 for (const auto& hit : trk.getMcpHits()) count_per_id[hit.second]++;
396 int best_id{-1}, best_n{0};
397 for (const auto& [id, n] : count_per_id) {
398 if (n > best_n) {
399 best_n = n;
400 best_id = id;
401 }
402 }
403 return best_id;
404 };
405
406 double best_abs_dz = 1e9, best_dz = -9999.;
407 Vertex* best_vtx = nullptr;
408 int n_v0_trk_matched = 0;
409 int n_v0_ele_trk_matched = 0, n_v0_pos_trk_matched = 0;
410 int n_v0_ele_trk_matched_clus = 0, n_v0_pos_trk_matched_clus = 0;
411 int n_v0_trk_matched_clus_both = 0, n_v0_trk_matched_clus_pos_only = 0;
412 int n_v0_trk_matched_clus_ele_only = 0;
413 double best_trk_abs_dz = 1e9, best_trk_dz = -9999.;
414 // Separate "best" bookkeeping for the cluster/track-p branches: picked by cluster-completeness
415 // tier first (2 = both clusters, 1 = e+ cluster only, 0 = neither), z-closeness as tiebreaker,
416 // so a v0 satisfying matched_trk_clus_both is guaranteed to be the one these branches describe
417 // (a pure closest-in-z pick, like best_trk_dz, could otherwise report values from a DIFFERENT,
418 // cluster-incomplete v0 when more than one track-matched v0 exists in the event).
419 int best_clus_tier = -1;
420 double best_clus_abs_dz = 1e9;
421 CalCluster best_ele_clus, best_pos_clus;
422 double best_ele_trk_p = -9999., best_pos_trk_p = -9999.;
423 int best_ele_trk_nhits = 0, best_pos_trk_nhits = 0;
424 bool have_best_trk_clus = false;
425 bool matched_z_and_trk = false;
426 for (Vertex* vtx : vtxs) {
427 double dz = vtx->getZ() - true_vtx_z;
428 if (std::fabs(dz) < best_abs_dz) {
429 best_abs_dz = std::fabs(dz);
430 best_dz = dz;
431 best_vtx = vtx;
432 }
433
434 // Track-based match: this v0's two constituent tracks (independent of its z position)
435 // majority-hit truth-match the true signal e- and e+ specifically (by MCParticle ID).
436 // ele/pos matches are also checked individually (no requirement on the other leg) to see
437 // how often a v0 captures just one of the two truth tracks vs. both.
438 bool this_ele_matched = false, this_pos_matched = false;
439 bool this_ele_has_clus = false, this_pos_has_clus = false;
440 Particle *eleP{nullptr}, *posP{nullptr}; // tree-owned; stay valid past this if-block
441 TRefArray parts = vtx->getParticles();
442 if (parts.GetEntries() >= 2) {
443 Particle* p0 = static_cast<Particle*>(parts.At(0));
444 Particle* p1 = static_cast<Particle*>(parts.At(1));
445 if (p0 && p1) {
446 Track t0 = p0->getTrack();
447 Track t1 = p1->getTrack();
448 Track *eleT{nullptr}, *posT{nullptr};
449 if (t0.getCharge() < 0 && t1.getCharge() > 0) {
450 eleT = &t0;
451 posT = &t1;
452 eleP = p0;
453 posP = p1;
454 } else if (t1.getCharge() < 0 && t0.getCharge() > 0) {
455 eleT = &t1;
456 posT = &t0;
457 eleP = p1;
458 posP = p0;
459 }
460 if (eleT && trueEleID >= 0) this_ele_matched = (majorityMcpID(*eleT) == trueEleID);
461 if (posT && truePosID >= 0) this_pos_matched = (majorityMcpID(*posT) == truePosID);
462 // utils::buildParticle only calls setCluster() when the LCIO particle actually had
463 // a track-cluster-matched cluster. Test via energy, not getNHits() (see note in
464 // setFile() -- utils::buildCalCluster never populates the hit count). The electron
465 // leg additionally requires eleClusEMin_ as a sanity floor against noise-level matches.
466 if (eleP) this_ele_has_clus = (eleP->getCluster().getEnergy() > eleClusEMin_);
467 if (posP) this_pos_has_clus = (posP->getCluster().getEnergy() > -9000.);
468
469 if (this_ele_matched && this_ele_has_clus) n_v0_ele_trk_matched_clus++;
470 if (this_pos_matched && this_pos_has_clus) n_v0_pos_trk_matched_clus++;
471 if (this_ele_matched && this_pos_matched) {
472 if (this_ele_has_clus && this_pos_has_clus) n_v0_trk_matched_clus_both++;
473 if (this_pos_has_clus) n_v0_trk_matched_clus_pos_only++; // e- cluster not required
474 if (this_ele_has_clus) n_v0_trk_matched_clus_ele_only++; // e+ cluster not required
475 }
476 }
477 }
478 bool this_trk_matched = this_ele_matched && this_pos_matched;
479 if (this_ele_matched) n_v0_ele_trk_matched++;
480 if (this_pos_matched) n_v0_pos_trk_matched++;
481 if (this_trk_matched) {
482 n_v0_trk_matched++;
483 if (std::fabs(dz) < best_trk_abs_dz) {
484 best_trk_abs_dz = std::fabs(dz);
485 best_trk_dz = dz;
486 }
487 if (eleP && posP) {
488 int this_tier = this_pos_has_clus ? (this_ele_has_clus ? 2 : 1) : 0;
489 bool better = (this_tier > best_clus_tier) ||
490 (this_tier == best_clus_tier && std::fabs(dz) < best_clus_abs_dz);
491 if (better) {
492 best_clus_tier = this_tier;
493 best_clus_abs_dz = std::fabs(dz);
494 best_ele_clus = eleP->getCluster();
495 best_pos_clus = posP->getCluster();
496 best_ele_trk_p = eleP->getTrack().getP();
497 best_pos_trk_p = posP->getTrack().getP();
498 best_ele_trk_nhits = eleP->getTrack().getTrackerHitCount();
499 best_pos_trk_nhits = posP->getTrack().getTrackerHitCount();
500 have_best_trk_clus = true;
501 }
502 }
503 if (std::fabs(dz) < vertexMatchZ_) matched_z_and_trk = true;
504 }
505 }
506 bool matched_z = has_v0 && (true_vtx_z > -9000.) && (best_abs_dz < vertexMatchZ_);
507 bool matched_trk = (n_v0_trk_matched >= 1);
508 bool matched_trk_ele = (n_v0_ele_trk_matched >= 1); // >=1 v0 has the truth e- (no requirement on e+)
509 bool matched_trk_pos = (n_v0_pos_trk_matched >= 1); // >=1 v0 has the truth e+ (no requirement on e-)
510 bool matched_z_or_trk = matched_z || matched_trk;
511 bool matched_trk_ele_clus = (n_v0_ele_trk_matched_clus >= 1);
512 bool matched_trk_pos_clus = (n_v0_pos_trk_matched_clus >= 1);
513 bool matched_trk_clus_both = (n_v0_trk_matched_clus_both >= 1);
514 bool matched_trk_clus_pos_only = (n_v0_trk_matched_clus_pos_only >= 1);
515 bool matched_trk_clus_ele_only = (n_v0_trk_matched_clus_ele_only >= 1);
516
517 bus_.set("n_v0", n_v0);
518 bus_.set("has_v0", has_v0);
519 bus_.set("matched_z", matched_z);
520 if (best_vtx) {
521 bus_.set<double>("best_dz", best_dz);
522 bus_.set<double>("best_vtx_x", best_vtx->getX());
523 bus_.set<double>("best_vtx_y", best_vtx->getY());
524 bus_.set<double>("best_vtx_z", best_vtx->getZ());
525 bus_.set<double>("best_vtx_chi2", best_vtx->getChi2());
526 }
527 bus_.set("n_v0_trk_matched", n_v0_trk_matched);
528 bus_.set("matched_trk", matched_trk);
529 bus_.set("matched_z_or_trk", matched_z_or_trk);
530 bus_.set("matched_z_and_trk", matched_z_and_trk);
531 if (n_v0_trk_matched > 0) bus_.set<double>("best_trk_dz", best_trk_dz);
532 bus_.set("matched_trk_ele", matched_trk_ele);
533 bus_.set("matched_trk_pos", matched_trk_pos);
534 bus_.set("matched_trk_ele_clus", matched_trk_ele_clus);
535 bus_.set("matched_trk_pos_clus", matched_trk_pos_clus);
536 bus_.set("matched_trk_clus_both", matched_trk_clus_both);
537 bus_.set("matched_trk_clus_pos_only", matched_trk_clus_pos_only);
538 bus_.set("matched_trk_clus_ele_only", matched_trk_clus_ele_only);
539 if (have_best_trk_clus) {
540 bool ele_has_clus = best_ele_clus.getEnergy() > eleClusEMin_;
541 bool pos_has_clus = best_pos_clus.getEnergy() > -9000.;
542 bus_.set("has_ele_clus", ele_has_clus);
543 bus_.set("has_pos_clus", pos_has_clus);
544 bus_.set<double>("ele_trk_p", best_ele_trk_p);
545 bus_.set<double>("pos_trk_p", best_pos_trk_p);
546 bus_.set<int>("ele_trk_nhits", best_ele_trk_nhits);
547 bus_.set<int>("pos_trk_nhits", best_pos_trk_nhits);
548 if (ele_has_clus) {
549 std::vector<double> xyz = best_ele_clus.getPosition();
550 bus_.set<double>("ele_clus_E", best_ele_clus.getEnergy());
551 bus_.set<double>("ele_clus_x", xyz[0]);
552 bus_.set<double>("ele_clus_y", xyz[1]);
553 bus_.set<double>("ele_clus_z", xyz[2]);
554 bus_.set<double>("ele_clus_t", best_ele_clus.getTime());
555 }
556 if (pos_has_clus) {
557 std::vector<double> xyz = best_pos_clus.getPosition();
558 bus_.set<double>("pos_clus_E", best_pos_clus.getEnergy());
559 bus_.set<double>("pos_clus_x", xyz[0]);
560 bus_.set<double>("pos_clus_y", xyz[1]);
561 bus_.set<double>("pos_clus_z", xyz[2]);
562 bus_.set<double>("pos_clus_t", best_pos_clus.getTime());
563 }
564 }
565
566 event_cf_.apply("at_least_one_v0", has_v0);
567 event_cf_.fill_nm1("at_least_one_v0", n_v0);
568 event_cf_.apply("v0_within_z", matched_z);
569 event_cf_.fill_nm1("v0_within_z", best_abs_dz < 1e8 ? best_abs_dz : 49.999);
570
571 // ---- reconstructed track inventory (NO truth match): are the e+/e- tracks there? ----
572 // For the no-v0 events this separates tracking (no tracks reconstructed) from vertexing
573 // (both tracks reconstructed but no v0 formed). charge>0 = e+, charge<0 = e-.
574 int n_trk_pos = 0, n_trk_ele = 0;
575 if (bus_.has(trkColl_)) {
576 const auto& tracks{bus_.get<std::vector<Track*>>(trkColl_)};
577 for (Track* trk : tracks) {
578 if (trk->getCharge() > 0) n_trk_pos++;
579 else if (trk->getCharge() < 0) n_trk_ele++;
580 }
581 }
582 bus_.set("n_trk_pos", n_trk_pos);
583 bus_.set("n_trk_ele", n_trk_ele);
584
585 if (truth_signal_found) {
586 n_truth_++;
587 if (has_v0) n_has_v0_++;
588 if (matched_z) n_matched_z_++;
589 if (matched_trk) n_matched_trk_++;
590 if (matched_z_and_trk) n_matched_z_and_trk_++;
591 if (matched_trk_ele) n_matched_trk_ele_++;
592 if (matched_trk_pos) n_matched_trk_pos_++;
593 if (matched_trk_ele_clus) n_matched_trk_ele_clus_++;
594 if (matched_trk_pos_clus) n_matched_trk_pos_clus_++;
595 if (matched_trk_clus_both) n_matched_trk_clus_both_++;
596 if (matched_trk_clus_pos_only) n_matched_trk_clus_pos_only_++;
597 if (matched_trk_clus_ele_only) n_matched_trk_clus_ele_only_++;
598 if (not has_v0) { // tracking-vs-vertexing breakdown for the no-v0 events
599 bool hp = (n_trk_pos > 0), he = (n_trk_ele > 0);
600 if (hp && he) n_novtx_both_trk_++;
601 else if (hp && not he) n_novtx_pos_only_++;
602 else if (he && not hp) n_novtx_ele_only_++;
603 else n_novtx_neither_++;
604 }
605 // secondary category: events with an energetic truth electron (p > eleMomCut_)
606 if (foundEle && trueEleP.P() > eleMomCut_) {
607 n_truth_ehi_++;
608 if (has_v0) n_has_v0_ehi_++;
609 if (matched_z) n_matched_z_ehi_++;
610 }
611 }
612
613 output_tree_->Fill();
614 return true;
615}
616
618 auto pct = [](long num, long den) { return den > 0 ? 100.0 * num / den : 0.0; };
619 std::cout << "\n============= TruthVertexMatch2021 summary =============\n"
620 << " truth-signal events : " << n_truth_ << "\n"
621 << " (1) NO reco v0 in event : " << (n_truth_ - n_has_v0_)
622 << " (" << pct(n_truth_ - n_has_v0_, n_truth_) << "%)\n"
623 << " (2) >=1 reco v0 (no selection) : " << n_has_v0_
624 << " (" << pct(n_has_v0_, n_truth_) << "%)\n"
625 << " (3) >=1 v0 within " << vertexMatchZ_ << " mm in z of truth : " << n_matched_z_
626 << " (" << pct(n_matched_z_, n_truth_) << "% of truth, "
627 << pct(n_matched_z_, n_has_v0_) << "% of has-v0)\n";
628 std::cout << " --- track-based match: v0's 2 tracks truth-match signal e-/e+ (any z) ---\n"
629 << " (4) >=1 v0 track-matched : " << n_matched_trk_
630 << " (" << pct(n_matched_trk_, n_truth_) << "% of truth, "
631 << pct(n_matched_trk_, n_has_v0_) << "% of has-v0)\n"
632 << " (5) >=1 v0 BOTH z- and trk-matched : " << n_matched_z_and_trk_
633 << " (" << pct(n_matched_z_and_trk_, n_truth_) << "% of truth)\n"
634 << " z-matched but NOT track-matched : " << (n_matched_z_ - n_matched_z_and_trk_)
635 << " <- z-window catching wrong/combinatoric tracks\n"
636 << " track-matched but NOT z-matched : " << (n_matched_trk_ - n_matched_z_and_trk_)
637 << " <- correct tracks vertexed outside the +/-" << vertexMatchZ_ << " mm window\n"
638 << " --- single-leg track match: is it harder to get BOTH tracks than just one? ---\n"
639 << " >=1 v0 has truth e- (any e+) : " << n_matched_trk_ele_
640 << " (" << pct(n_matched_trk_ele_, n_truth_) << "% of truth)\n"
641 << " >=1 v0 has truth e+ (any e-) : " << n_matched_trk_pos_
642 << " (" << pct(n_matched_trk_pos_, n_truth_) << "% of truth)\n"
643 << " >=1 v0 has BOTH (matched_trk) : " << n_matched_trk_
644 << " (" << pct(n_matched_trk_, n_truth_) << "% of truth)\n";
645 std::cout << " --- ECal cluster requirement (within matched_trk = both tracks correct) ---\n"
646 << " the singles trigger only needs the e+ cluster; is requiring an e- cluster too costing us?\n"
647 << " >=1 v0: e- track has cluster (any e+) : " << n_matched_trk_ele_clus_
648 << " (" << pct(n_matched_trk_ele_clus_, n_truth_) << "% of truth)\n"
649 << " >=1 v0: e+ track has cluster (any e-) : " << n_matched_trk_pos_clus_
650 << " (" << pct(n_matched_trk_pos_clus_, n_truth_) << "% of truth)\n"
651 << " STRICT both tracks + BOTH clusters : " << n_matched_trk_clus_both_
652 << " (" << pct(n_matched_trk_clus_both_, n_truth_) << "% of truth, "
653 << pct(n_matched_trk_clus_both_, n_matched_trk_) << "% of matched_trk)\n"
654 << " RELAXED both tracks + e+ cluster ONLY : " << n_matched_trk_clus_pos_only_
655 << " (" << pct(n_matched_trk_clus_pos_only_, n_truth_) << "% of truth, "
656 << pct(n_matched_trk_clus_pos_only_, n_matched_trk_) << "% of matched_trk)\n"
657 << " both tracks + e- cluster ONLY (e+ optional) : " << n_matched_trk_clus_ele_only_
658 << " (" << pct(n_matched_trk_clus_ele_only_, n_truth_) << "% of truth, "
659 << pct(n_matched_trk_clus_ele_only_, n_matched_trk_) << "% of matched_trk)\n"
660 << " ACCEPTANCE GAIN from dropping the e- cluster requirement: +"
663 << " pp of truth)\n";
664 long n_novtx = n_truth_ - n_has_v0_;
665 std::cout << " --- NO-v0 track inventory (KalmanFullTracks, no truth match) ---\n"
666 << " no-v0 events : " << n_novtx << "\n"
667 << " both e+ and e- track present : " << n_novtx_both_trk_
668 << " (" << pct(n_novtx_both_trk_, n_novtx) << "%) <- VERTEXING (tracks exist, no v0)\n"
669 << " e+ track only (no e-) : " << n_novtx_pos_only_
670 << " (" << pct(n_novtx_pos_only_, n_novtx) << "%)\n"
671 << " e- track only (no e+) : " << n_novtx_ele_only_
672 << " (" << pct(n_novtx_ele_only_, n_novtx) << "%)\n"
673 << " neither track : " << n_novtx_neither_
674 << " (" << pct(n_novtx_neither_, n_novtx) << "%) <- TRACKING (no tracks)\n";
675 std::cout << " --- secondary category: truth e- momentum > " << eleMomCut_ << " GeV ---\n"
676 << " truth-signal (e- p>cut) events : " << n_truth_ehi_ << "\n"
677 << " (1) NO reco v0 : " << (n_truth_ehi_ - n_has_v0_ehi_)
678 << " (" << pct(n_truth_ehi_ - n_has_v0_ehi_, n_truth_ehi_) << "%)\n"
679 << " (2) >=1 reco v0 (no selection) : " << n_has_v0_ehi_
680 << " (" << pct(n_has_v0_ehi_, n_truth_ehi_) << "%)\n"
681 << " (3) >=1 v0 within " << vertexMatchZ_ << " mm in z : " << n_matched_z_ehi_
682 << " (" << pct(n_matched_z_ehi_, n_truth_ehi_) << "% of these, "
683 << pct(n_matched_z_ehi_, n_has_v0_ehi_) << "% of has-v0)\n"
684 << "=======================================================\n" << std::endl;
685
686 outF_->cd();
687 output_tree_->Write();
688 event_cf_.save();
689 outF_->Close();
690}
691
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
double getEnergy() const
Definition CalCluster.h:73
std::vector< double > getPosition() const
Definition CalCluster.h:63
double getTime() const
Definition CalCluster.h:83
Definition IEvent.h:7
std::vector< double > getMomentum() const
std::vector< double > getVertexPosition() const
double getEnergy() const
Definition MCParticle.h:172
std::vector< double > getEndPoint() const
double getMass() const
Definition MCParticle.h:175
description
Track getTrack() const
Definition Particle.h:48
CalCluster getCluster() const
Definition Particle.h:62
TFile * outF_
Definition Processor.h:125
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95
virtual void setFile(TFile *outFile)
Set output TFile for AnaProcessors.
Definition Processor.h:88
Definition Track.h:32
int getTrackerHitCount() const
Definition Track.h:347
int getCharge() const
Definition Track.h:249
double getP() const
Definition Track.h:293
virtual void setFile(TFile *out_file) final
Set output TFile for AnaProcessors.
std::shared_ptr< AnaHelpers > _ah
virtual void initialize(TTree *tree) final
Callback for the Processor to take any necessary action when the processing of events starts,...
virtual void finalize() final
Callback for the Processor to take any necessary action when the processing of events finishes,...
virtual void configure(const ParameterSet &parameters) final
Callback for the Processor to configure itself from the given set of parameters.
std::unique_ptr< TTree > output_tree_
double getX() const
Definition Vertex.h:109
double getChi2() const
Definition Vertex.h:103
double getZ() const
Definition Vertex.h:115
double getY() const
Definition Vertex.h:112