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
VertexProcessor.cxx
Go to the documentation of this file.
1
6#include "VertexProcessor.h"
7
8#include <cmath>
9#include <iomanip>
10#include <sstream>
11
12#include "utilities.h"
13
15
17
19 std::cout << "Configuring VertexProcessor" << std::endl;
20 try {
21 debug_ = parameters.getInteger("debug", debug_);
22 vtxCollLcio_ = parameters.getString("vtxCollLcio", vtxCollLcio_);
23 vtxCollRoot_ = parameters.getString("vtxCollRoot", vtxCollRoot_);
24 partCollRoot_ = parameters.getString("partCollRoot", partCollRoot_);
25 kinkRelCollLcio_ = parameters.getString("kinkRelCollLcio", kinkRelCollLcio_);
26 trkRelCollLcio_ = parameters.getString("trkRelCollLcio", trkRelCollLcio_);
27 trackStateLocation_ = parameters.getString("trackStateLocation", trackStateLocation_);
28 hitFitsCollLcio_ = parameters.getString("hitFitsCollLcio", hitFitsCollLcio_);
29 trkhitCollRoot_ = parameters.getString("trkhitCollRoot", trkhitCollRoot_);
30 rawhitCollRoot_ = parameters.getString("rawhitCollRoot", rawhitCollRoot_);
31 mcPartRelLcio_ = parameters.getString("mcPartRelLcio", mcPartRelLcio_);
32 mcPartCollLcio_ = parameters.getString("mcPartCollLcio", mcPartCollLcio_);
33 rawHitCollLcio_ = parameters.getString("rawHitCollLcio", rawHitCollLcio_);
34 truthClosure_ = parameters.getInteger("truthClosure", truthClosure_);
35 useTrackerHits_ = parameters.getInteger("useTrackerHits", useTrackerHits_);
36 bfield_ = parameters.getDouble("bfield", bfield_);
37 } catch (std::runtime_error& error) {
38 std::cout << error.what() << std::endl;
39 }
40}
41
42void VertexProcessor::initialize(TTree* tree) {
43 // Add branches to tree
44 tree->Branch(vtxCollRoot_.c_str(), &vtxs_);
45 tree->Branch(partCollRoot_.c_str(), &parts_);
46 if (!trkhitCollRoot_.empty()) tree->Branch(trkhitCollRoot_.c_str(), &hits_);
47
48 if (!rawhitCollRoot_.empty()) tree->Branch(rawhitCollRoot_.c_str(), &rawhits_);
49}
50
52 if (debug_ > 0) std::cout << "VertexProcessor: Clear output vector" << std::endl;
53
54 if (hits_.size() > 0) {
55 for (std::vector<TrackerHit*>::iterator it = hits_.begin(); it != hits_.end(); ++it) {
56 delete *it;
57 }
58 hits_.clear();
59 }
60
61 if (rawhits_.size() > 0) {
62 for (std::vector<RawSvtHit*>::iterator it = rawhits_.begin(); it != rawhits_.end(); ++it) {
63 delete *it;
64 }
65 rawhits_.clear();
66 }
67
68 for (int i = 0; i < vtxs_.size(); i++) delete vtxs_.at(i);
69 vtxs_.clear();
70 for (int i = 0; i < parts_.size(); i++) delete parts_.at(i);
71 parts_.clear();
72
73 Event* event = static_cast<Event*>(ievent);
74 UTIL::LCRelationNavigator* mcPartRel_nav;
75
76 // Get the collection of vertices from the LCIO event. If no such collection
77 // exist, a DataNotAvailableException is thrown
78 if (debug_ > 0) std::cout << "VertexProcessor: Get LCIO Collection " << vtxCollLcio_ << std::endl;
79 EVENT::LCCollection* lc_vtxs = nullptr;
80 try {
81 lc_vtxs = event->getLCCollection(vtxCollLcio_.c_str());
82 } catch (EVENT::DataNotAvailableException e) {
83 std::cout << e.what() << std::endl;
84 return false;
85 }
86
87 // Get the collection of LCRelations between GBL tracks and kink data and track data variables.
88 EVENT::LCCollection* gbl_kink_data{nullptr};
89 EVENT::LCCollection* track_data{nullptr};
90
91 try {
92 if (!kinkRelCollLcio_.empty())
93 gbl_kink_data = static_cast<EVENT::LCCollection*>(event->getLCCollection(kinkRelCollLcio_.c_str()));
94 if (!trkRelCollLcio_.empty())
95 track_data = static_cast<EVENT::LCCollection*>(event->getLCCollection(trkRelCollLcio_.c_str()));
96 } catch (EVENT::DataNotAvailableException e) {
97 std::cout << e.what() << std::endl;
98 if (!gbl_kink_data) std::cout << "Failed retrieving " << kinkRelCollLcio_ << std::endl;
99 if (!track_data) std::cout << "Failed retrieving " << trkRelCollLcio_ << std::endl;
100 }
101
102 bool rotateHits = true;
103 int hitType = 0;
104 EVENT::LCCollection* raw_svt_hit_fits = nullptr;
105
106 auto evColls = event->getLCEvent()->getCollectionNames();
107 auto it = std::find(evColls->begin(), evColls->end(), hitFitsCollLcio_.c_str());
108 bool hasFits = true;
109 if (it == evColls->end()) hasFits = false;
110 if (hasFits) {
111 raw_svt_hit_fits = event->getLCCollection(hitFitsCollLcio_.c_str());
112 }
113 // Check to see if MC Particles are in the file
114 it = std::find(evColls->begin(), evColls->end(), mcPartRelLcio_.c_str());
115 bool hasMCParts = true;
116 EVENT::LCCollection* mcPartRel;
117 if (it == evColls->end()) hasMCParts = false;
118 if (hasMCParts) {
119 mcPartRel = event->getLCCollection(mcPartRelLcio_.c_str());
120 // Heap an LCRelation navigator which will allow faster access
121 mcPartRel_nav = new UTIL::LCRelationNavigator(mcPartRel);
122 }
123
130 std::map<int, int> mcp_pdg;
131 if (truthClosure_) {
132 closure_.n_events++;
133 if (hasMCParts) closure_.n_events_with_rel++;
134
135 auto mcp_it = std::find(evColls->begin(), evColls->end(), mcPartCollLcio_.c_str());
136 if (mcp_it != evColls->end()) {
137 closure_.n_events_with_mcp++;
138 EVENT::LCCollection* lc_mcps = event->getLCCollection(mcPartCollLcio_.c_str());
139 for (int impc = 0; impc < lc_mcps->getNumberOfElements(); ++impc) {
140 auto* mcp = static_cast<EVENT::MCParticle*>(lc_mcps->getElementAt(impc));
141 mcp_pdg[mcp->id()] = mcp->getPDG();
142 }
143 }
144
151 auto raw_it = std::find(evColls->begin(), evColls->end(), rawHitCollLcio_.c_str());
152 if (raw_it != evColls->end() and hasMCParts) {
153 EVENT::LCCollection* lc_raws = event->getLCCollection(rawHitCollLcio_.c_str());
154 closure_.n_raw_event += lc_raws->getNumberOfElements();
155 for (int iraw = 0; iraw < lc_raws->getNumberOfElements(); ++iraw) {
156 if (not mcPartRel_nav->getRelatedToObjects(lc_raws->getElementAt(iraw)).empty()) {
157 closure_.n_raw_event_with_rel++;
158 }
159 }
160 }
161 }
162
163 if (debug_ > 0) std::cout << "VertexProcessor: Converting Verteces" << std::endl;
164 for (int ivtx = 0; ivtx < lc_vtxs->getNumberOfElements(); ++ivtx) {
165 if (debug_ > 0) std::cout << "VertexProcessor: Converting Vertex " << ivtx << std::endl;
166 EVENT::Vertex* lc_vtx{nullptr};
167 lc_vtx = static_cast<EVENT::Vertex*>(lc_vtxs->getElementAt(ivtx));
168
169 if (debug_ > 0) std::cout << "VertexProcessor: Build Vertex" << std::endl;
170 Vertex* vtx = utils::buildVertex(lc_vtx);
171
172 if (debug_ > 0) std::cout << "VertexProcessor: Get Particles" << std::endl;
173 std::vector<EVENT::ReconstructedParticle*> lc_parts = lc_vtx->getAssociatedParticle()->getParticles();
174 for (auto lc_part : lc_parts) {
175 if (debug_ > 0) std::cout << "VertexProcessor: Build particle" << std::endl;
176 Particle* part = utils::buildParticle(lc_part, trackStateLocation_, gbl_kink_data, track_data);
177 EVENT::Track* lc_track =
178 lc_part->getTracks().empty() ? nullptr : static_cast<EVENT::Track*>(lc_part->getTracks()[0]);
179 Track* track = nullptr;
180 if (lc_track) {
181 if (debug_ > 0)
182 std::cout << "[VertexProcessor] calling buildTrack: vtx=" << ivtx
183 << " trackStateLocation=" << trackStateLocation_
184 << " has_gbl_kink=" << (gbl_kink_data ? "yes" : "no")
185 << " has_track_data=" << (track_data ? "yes" : "no") << std::endl;
186 track = utils::buildTrack(lc_track, trackStateLocation_, gbl_kink_data, track_data);
187 // buildTrack returns nullptr when the requested track state is missing
188 if (!track)
189 std::cout << "[VertexProcessor] WARNING: track " << lc_track->id() << " has no '"
190 << trackStateLocation_ << "' track state; particle stored without a track"
191 << std::endl;
192 }
193 if (track) {
194 int nHits = 0;
195 if (bfield_ > 0.0) track->setMomentum(bfield_);
196 if (track->isKalmanTrack()) hitType = 1; // SiClusters
197
198 if (!useTrackerHits_) {
199 auto hitPattern = lc_track->getSubdetectorHitNumbers();
200 for (int h = 0; h < hitPattern.size(); h++) {
201 if (hitPattern[h] > 0) {
202 track->addHitLayer(h);
203 nHits++;
204 }
205 }
206 if (nHits == 0) {
207 std::cout << "[VertexProcessor::process] WARNING Track with no hits -- likely "
208 "getSubdetectorHitNumbers isn't filled!"
209 << std::endl;
210 std::cout << "[VertexProcessor::process] WARNING If hit collections are saved, you may want to "
211 "turn on useTrackerHits"
212 << std::endl;
213 }
214 track->setTrackerHitCount(nHits);
215 } else {
216 EVENT::TrackerHitVec lc_tracker_hits = lc_track->getTrackerHits();
217 for (auto lc_tracker_hit : lc_tracker_hits) {
218 TrackerHit* tracker_hit = utils::buildTrackerHit(
219 static_cast<IMPL::TrackerHitImpl*>(lc_tracker_hit), rotateHits, hitType);
220 std::vector<RawSvtHit*> rawSvthitsOn3d;
221 utils::addRawInfoTo3dHit(tracker_hit, static_cast<IMPL::TrackerHitImpl*>(lc_tracker_hit),
222 raw_svt_hit_fits, &rawSvthitsOn3d, hitType);
223
224 int hitLayer = tracker_hit->getLayer();
225 nHits++;
226 if (truthClosure_) closure_.n_trackerhits++;
227 EVENT::LCObjectVec rawHits = lc_tracker_hit->getRawHits();
228 if (truthClosure_) closure_.n_raw += rawHits.size();
229 for (int irawhit = 0; irawhit < rawHits.size(); ++irawhit) {
230 EVENT::LCObject* rawhit = rawHits.at(irawhit);
231 if (debug_ > 0) std::cout << "rawhit on track has lcio id: " << rawhit->id() << std::endl;
232
233 if (hasMCParts) {
234 // Get the list of fit params associated with the raw tracker hit
235 EVENT::LCObjectVec lc_simtrackerhits = mcPartRel_nav->getRelatedToObjects(rawhit);
236
237 if (truthClosure_) {
238 if (not lc_simtrackerhits.empty()) closure_.n_raw_with_rel++;
239 closure_.n_simhits += lc_simtrackerhits.size();
240 }
241
242 // Loop over SimTrackerHits to get MCParticles
243 for (int isimhit = 0; isimhit < lc_simtrackerhits.size(); isimhit++) {
244 EVENT::SimTrackerHit* lc_simhit =
245 static_cast<EVENT::SimTrackerHit*>(lc_simtrackerhits.at(isimhit));
246 EVENT::MCParticle* lc_mcp = lc_simhit->getMCParticle();
253 if (lc_mcp == nullptr) {
254 if (truthClosure_) closure_.n_simhits_null_mcp++;
255 if (debug_ > 0) std::cout << "mcp is null" << std::endl;
256 continue;
257 }
258 if (truthClosure_) {
259 closure_.n_expected_entries++;
260 if (not mcp_pdg.empty() and mcp_pdg.count(lc_mcp->id()) == 0) {
261 closure_.n_simhits_dangling++;
262 }
263 }
264 track->addMcpHit(hitLayer, lc_mcp->id());
265 if (debug_ > 0) {
266 std::cout << "simtrackerhit lcio id: " << lc_simhit->id() << std::endl;
267 std::cout << "mcp lcio id: " << lc_mcp->id() << std::endl;
268 }
269 }
270 }
271 } // Loop over raw tracker hits
272
273 track->addHitLayer(hitLayer);
274 track->addHit(tracker_hit);
275 hits_.push_back(tracker_hit);
276 for (std::vector<RawSvtHit*>::iterator it = rawSvthitsOn3d.begin(); it != rawSvthitsOn3d.end();
277 ++it) {
278 delete *it;
279 }
280 rawSvthitsOn3d.clear();
281
282 } // Loop over hits on track
283
284 track->setTrackerHitCount(nHits);
285
286 } // !useTrackerHits
287
288 part->setTrack(track);
295 Track stored = part->getTrack();
296 closeTruthLink(&stored, mcp_pdg, not mcp_pdg.empty());
297 }
298 delete track;
299 }
300 // =========================================
301 if (debug_ > 0) std::cout << "VertexProcessor: Add particle" << std::endl;
302 parts_.push_back(part);
303 vtx->addParticle(part);
304 }
305
306 if (debug_ > 0) std::cout << "VertexProcessor: Add Vertex" << std::endl;
307 vtxs_.push_back(vtx);
308 }
309
310 if (hasMCParts) delete mcPartRel_nav;
311 if (debug_ > 0) std::cout << "VertexProcessor: End process" << std::endl;
312 return true;
313}
314
315void VertexProcessor::closeTruthLink(Track* track, const std::map<int, int>& mcp_pdg, bool have_mcp) {
316 closure_.n_tracks++;
317
318 const auto mcp_hits = track->getMcpHits();
319 closure_.n_stored_entries += mcp_hits.size();
320
321 if (mcp_hits.empty()) {
322 closure_.n_trk_no_entry++;
323 return;
324 }
325
332 std::map<int, int> per_strip;
333 std::map<int, int> per_layer;
334 std::set<std::pair<int, int>> seen;
335 for (const auto& [layer_id, particle_id] : mcp_hits) {
336 per_strip[particle_id]++;
337 if (seen.insert(std::make_pair(layer_id, particle_id)).second) per_layer[particle_id]++;
338 }
339
340 auto plurality = [](const std::map<int, int>& counts) {
341 int best_id{-1}, best_n{0};
342 for (const auto& [particle_id, count] : counts) {
343 if (count > best_n) {
344 best_id = particle_id;
345 best_n = count;
346 }
347 }
348 return best_id;
349 };
350
351 const int strip_id = plurality(per_strip);
352 if (strip_id != plurality(per_layer)) closure_.n_trk_plurality_differs++;
353
354 if (not have_mcp) return;
355
362 auto found = mcp_pdg.find(strip_id);
363 if (found == mcp_pdg.end()) {
364 closure_.n_trk_dangling++;
365 } else if (std::abs(found->second) != 11) {
366 closure_.n_trk_not_electron++;
367 } else {
368 closure_.n_trk_electron++;
369 }
370}
371
373 const auto& c = closure_;
374 auto frac = [](long num, long den) {
375 std::ostringstream os;
376 if (den > 0) os << " (" << std::fixed << std::setprecision(4) << double(num) / double(den) << ")";
377 return os.str();
378 };
379
380 std::cout << "\n===== VertexProcessor truth-link closure [" << vtxCollLcio_ << "] =====\n"
381 << " events : " << c.n_events << "\n"
382 << " with " << mcPartRelLcio_ << " : " << c.n_events_with_rel << "\n"
383 << " with " << mcPartCollLcio_ << " : " << c.n_events_with_mcp << "\n";
384
385 if (c.n_events_with_rel == 0) {
386 std::cout << " no relation collection seen -- nothing to close.\n"
387 << "==========================================================\n" << std::endl;
388 return;
389 }
390
391 std::cout << " -- relation table, whole event (" << rawHitCollLcio_ << ") --\n"
392 << " raw hits : " << c.n_raw_event << "\n"
393 << " with >=1 relation : " << c.n_raw_event_with_rel
394 << frac(c.n_raw_event_with_rel, c.n_raw_event) << "\n"
395 << " -- relation table, on track --\n"
396 << " tracks : " << c.n_tracks << "\n"
397 << " tracker hits : " << c.n_trackerhits << "\n"
398 << " raw hits : " << c.n_raw << "\n"
399 << " with >=1 relation : " << c.n_raw_with_rel
400 << frac(c.n_raw_with_rel, c.n_raw) << "\n"
401 << " with 0 relations : " << (c.n_raw - c.n_raw_with_rel)
402 << frac(c.n_raw - c.n_raw_with_rel, c.n_raw) << "\n"
403 << " SimTrackerHits reached : " << c.n_simhits << "\n"
404 << " null MCParticle : " << c.n_simhits_null_mcp
405 << frac(c.n_simhits_null_mcp, c.n_simhits) << "\n"
406 << " MCParticle id NOT in " << mcPartCollLcio_ << " : " << c.n_simhits_dangling
407 << frac(c.n_simhits_dangling, c.n_simhits) << "\n"
408 << " -- closure: LCIO walk vs stored Track::getMcpHits() --\n"
409 << " entries from the walk : " << c.n_expected_entries << "\n"
410 << " entries on the stored Track : " << c.n_stored_entries << "\n"
411 << " "
412 << (c.n_expected_entries == c.n_stored_entries ? "CLOSED" : "*** MISMATCH: entries were lost ***")
413 << "\n"
414 << " -- what the plurality resolves to (what PreselectAndCategorize2021 sees) --\n"
415 << " tracks with no mcp-hit entry : " << c.n_trk_no_entry
416 << frac(c.n_trk_no_entry, c.n_tracks) << "\n"
417 << " plurality id not in MCParticle coll : " << c.n_trk_dangling
418 << frac(c.n_trk_dangling, c.n_tracks) << "\n"
419 << " plurality id is not an e+- : " << c.n_trk_not_electron
420 << frac(c.n_trk_not_electron, c.n_tracks) << "\n"
421 << " plurality id IS an e+- : " << c.n_trk_electron
422 << frac(c.n_trk_electron, c.n_tracks) << "\n"
423 << " (the first three are indistinguishable downstream:\n"
424 << " utils::hasTruthMatch returns false for all of them)\n"
425 << " -- weighting cross-check --\n"
426 << " strip-level and layer-unique plurality disagree : " << c.n_trk_plurality_differs
427 << frac(c.n_trk_plurality_differs, c.n_tracks) << "\n"
428 << "==========================================================\n" << std::endl;
429}
430
434
#define DECLARE_PROCESSOR(CLASS)
Macro which allows the framework to construct a producer given its name during configuration.
Definition Processor.h:139
Class used to convert LCIO Vertex collections into ROOT collections.
Definition Event.h:35
Definition IEvent.h:7
description
Track getTrack() const
Definition Particle.h:48
void setTrack(Track *track)
Definition Particle.h:42
Processor(const std::string &name, Process &process)
Class constructor.
Definition Processor.cxx:10
virtual bool process()
Process the histograms and generate analysis output.
Definition Processor.h:95
Definition Track.h:32
int getLayer() const
Definition TrackerHit.h:109
Insert description here. more details.
void closeTruthLink(Track *track, const std::map< int, int > &mcp_pdg, bool have_mcp)
Tally the closure counters for one converted track.
std::string mcPartRelLcio_
virtual bool process(IEvent *ievent)
Process the event and put new data products into it.
std::string rawHitCollLcio_
event-level raw hits, for the closure control
std::string partCollRoot_
description
std::string trkRelCollLcio_
description
std::string trackStateLocation_
select track state for tracks DEFAULT AtIP
int debug_
Debug Level.
std::string trkhitCollRoot_
double bfield_
magnetic field
int useTrackerHits_
Load hit collections, otherwise get from getSubdetectorHitNumbers.
virtual void configure(const ParameterSet &parameters)
Callback for the Processor to configure itself from the given set of parameters.
virtual void finalize()
Callback for the Processor to take any necessary action when the processing of events finishes.
virtual void initialize(TTree *tree)
Callback for the Processor to take any necessary action when the processing of events starts.
std::string hitFitsCollLcio_
std::vector< RawSvtHit * > rawhits_
std::vector< Vertex * > vtxs_
description
std::vector< TrackerHit * > hits_
std::string mcPartCollLcio_
collection the stored MCParticle ids come from
std::vector< Particle * > parts_
description
std::string vtxCollRoot_
description
int truthClosure_
Run the truth-link closure test and print a summary in finalize().
void reportTruthLinkClosure() const
Print the closure summary. Called from finalize().
std::string kinkRelCollLcio_
description
std::string vtxCollLcio_
description
TruthLinkClosure closure_
std::string rawhitCollRoot_
VertexProcessor(const std::string &name, Process &process)
Class constructor.
bool addRawInfoTo3dHit(TrackerHit *tracker_hit, IMPL::TrackerHitImpl *lc_tracker_hit, EVENT::LCCollection *raw_svt_fits, std::vector< RawSvtHit * > *rawHits=nullptr, int type=0, bool storeRawHit=true)
description
Track * buildTrack(EVENT::Track *lc_track, std::string trackstate_location, EVENT::LCCollection *gbl_kink_data, EVENT::LCCollection *track_data)
description
Vertex * buildVertex(EVENT::Vertex *lc_vertex)
description
Definition utilities.cxx:27
TrackerHit * buildTrackerHit(IMPL::TrackerHitImpl *lc_trackerHit, bool rotate=true, int type=0)
description
Particle * buildParticle(EVENT::ReconstructedParticle *lc_particle, std::string trackstate_location, EVENT::LCCollection *gbl_kink_data, EVENT::LCCollection *track_data)
description
Definition utilities.cxx:47