-
Notifications
You must be signed in to change notification settings - Fork 184
/
SvtxVertexEval.cc
469 lines (393 loc) · 9.16 KB
/
SvtxVertexEval.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include "SvtxVertexEval.h"
#include "SvtxTrackEval.h"
#include "SvtxTruthEval.h"
#include <trackbase_historic/SvtxTrackMap.h>
#include <globalvertex/Vertex.h>
#include <globalvertex/SvtxVertexMap.h>
#include <g4main/PHG4Particle.h>
#include <g4main/PHG4TruthInfoContainer.h>
#include <g4main/PHG4VtxPoint.h>
#include <phool/getClass.h>
#include <cassert>
#include <iostream>
#include <set>
class SvtxTrack;
SvtxVertexEval::SvtxVertexEval(PHCompositeNode* topNode)
: _trackeval(topNode)
{
set_track_nodename("SvtxTrackMap");
}
SvtxVertexEval::~SvtxVertexEval()
{
if (_verbosity > 0)
{
if ((_errors > 0) || (_verbosity > 1))
{
std::cout << "SvtxVertexEval::~SvtxVertexEval() - Error Count: " << _errors << std::endl;
}
}
}
void SvtxVertexEval::next_event(PHCompositeNode* topNode)
{
_cache_all_truth_particles.clear();
_cache_all_truth_points.clear();
_cache_max_truth_point_by_ntracks.clear();
_cache_all_vertexes_from_point.clear();
_cache_best_vertex_from_point.clear();
_cache_get_ntracks_contribution.clear();
_trackeval.next_event(topNode);
get_node_pointers(topNode);
}
std::set<PHG4Particle*> SvtxVertexEval::all_truth_particles(const Vertex* vertex)
{
if (!has_node_pointers())
{
++_errors;
return std::set<PHG4Particle*>();
}
if (_strict)
{
assert(vertex);
}
else if (!vertex)
{
++_errors;
return std::set<PHG4Particle*>();
}
if (_do_cache)
{
std::map<const Vertex*, std::set<PHG4Particle*> >::iterator iter =
_cache_all_truth_particles.find(vertex);
if (iter != _cache_all_truth_particles.end())
{
return iter->second;
}
}
std::set<PHG4Particle*> all_particles;
// loop over all tracks on vertex
for (Vertex::TrackIter iter = vertex->begin_tracks();
iter != vertex->end_tracks();
++iter)
{
SvtxTrack* track = _trackmap->get(*iter);
if (_strict)
{
assert(track);
}
else if (!track)
{
++_errors;
continue;
}
PHG4Particle* max_particle = _trackeval.max_truth_particle_by_nclusters(track);
if (_strict)
{
assert(max_particle);
}
else if (!max_particle)
{
++_errors;
continue;
}
all_particles.insert(max_particle);
}
if (_do_cache)
{
_cache_all_truth_particles.insert(std::make_pair(vertex, all_particles));
}
return all_particles;
}
std::set<PHG4VtxPoint*> SvtxVertexEval::all_truth_points(const Vertex* vertex)
{
if (!has_node_pointers())
{
++_errors;
return std::set<PHG4VtxPoint*>();
}
if (_strict)
{
assert(vertex);
}
else if (!vertex)
{
++_errors;
return std::set<PHG4VtxPoint*>();
}
if (_do_cache)
{
std::map<const Vertex*, std::set<PHG4VtxPoint*> >::iterator iter =
_cache_all_truth_points.find(vertex);
if (iter != _cache_all_truth_points.end())
{
return iter->second;
}
}
std::set<PHG4VtxPoint*> points;
std::set<PHG4Particle*> particles = all_truth_particles(vertex);
for (auto particle : particles)
{
PHG4VtxPoint* point = get_truth_eval()->get_vertex(particle);
if (_strict)
{
assert(point);
}
else if (!point)
{
++_errors;
continue;
}
points.insert(point);
}
if (_do_cache)
{
_cache_all_truth_points.insert(std::make_pair(vertex, points));
}
return points;
}
PHG4VtxPoint* SvtxVertexEval::max_truth_point_by_ntracks(const Vertex* vertex)
{
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
if (_strict)
{
assert(vertex);
}
else if (!vertex)
{
++_errors;
return nullptr;
}
if (_do_cache)
{
std::map<const Vertex*, PHG4VtxPoint*>::iterator iter =
_cache_max_truth_point_by_ntracks.find(vertex);
if (iter != _cache_max_truth_point_by_ntracks.end())
{
return iter->second;
}
}
std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
PHG4VtxPoint* max_point = nullptr;
unsigned int max_ntracks = 0;
for (auto candidate : points)
{
unsigned int ntracks = get_ntracks_contribution(vertex, candidate);
if (ntracks > max_ntracks)
{
max_ntracks = ntracks;
max_point = candidate;
}
}
if (_do_cache)
{
_cache_max_truth_point_by_ntracks.insert(std::make_pair(vertex, max_point));
}
return max_point;
}
std::set<const Vertex*> SvtxVertexEval::all_vertexes_from(PHG4VtxPoint* truthpoint)
{
if (!has_node_pointers())
{
++_errors;
return std::set<const Vertex*>();
}
if (_strict)
{
assert(truthpoint);
}
else if (!truthpoint)
{
++_errors;
return std::set<const Vertex*>();
}
if (_do_cache)
{
std::map<PHG4VtxPoint*, std::set<const Vertex*> >::iterator iter =
_cache_all_vertexes_from_point.find(truthpoint);
if (iter != _cache_all_vertexes_from_point.end())
{
return iter->second;
}
}
std::set<const Vertex*> all_vertexes;
// loop over all vertexes on node
for (auto& iter : *_vertexmap)
{
const Vertex* vertex = iter.second;
std::set<PHG4VtxPoint*> points = all_truth_points(vertex);
for (auto point : points)
{
if (get_truth_eval()->are_same_vertex(point, truthpoint))
{
all_vertexes.insert(vertex);
}
}
}
if (_do_cache)
{
_cache_all_vertexes_from_point.insert(std::make_pair(truthpoint, all_vertexes));
}
return all_vertexes;
}
const Vertex* SvtxVertexEval::best_vertex_from(PHG4VtxPoint* truthpoint)
{
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
if (_strict)
{
assert(truthpoint);
}
else if (!truthpoint)
{
++_errors;
return nullptr;
}
if (_do_cache)
{
std::map<PHG4VtxPoint*, const Vertex*>::iterator iter =
_cache_best_vertex_from_point.find(truthpoint);
if (iter != _cache_best_vertex_from_point.end())
{
return iter->second;
}
}
unsigned int best_count = 0;
std::set<const Vertex*> tracks = all_vertexes_from(truthpoint);
std::set<const Vertex*>::iterator best_vertex_iter = tracks.begin();
for (auto it = tracks.begin(); it != tracks.end(); ++it)
{
const Vertex* vertex = *it;
unsigned int count = get_ntracks_contribution(vertex, truthpoint);
if (count > best_count)
{
best_vertex_iter = it;
best_count = count;
}
}
const Vertex* the_best = *best_vertex_iter;
if (_do_cache)
{
_cache_best_vertex_from_point.insert(std::make_pair(truthpoint, the_best));
}
return the_best;
}
// overlap calculations
unsigned int SvtxVertexEval::get_ntracks_contribution(const Vertex* vertex, PHG4VtxPoint* truthpoint)
{
if (!has_node_pointers())
{
++_errors;
return 0;
}
if (_strict)
{
assert(vertex);
assert(truthpoint);
}
else if (!vertex || !truthpoint)
{
++_errors;
return 0;
}
if (_do_cache)
{
std::map<std::pair<const Vertex*, PHG4VtxPoint*>, unsigned int>::iterator iter =
_cache_get_ntracks_contribution.find(std::make_pair(vertex, truthpoint));
if (iter != _cache_get_ntracks_contribution.end())
{
return iter->second;
}
}
unsigned int ntracks = 0;
for (Vertex::TrackIter iter = vertex->begin_tracks();
iter != vertex->end_tracks();
++iter)
{
SvtxTrack* track = _trackmap->get(*iter);
PHG4Particle* particle = _trackeval.max_truth_particle_by_nclusters(track);
PHG4VtxPoint* candidate = get_truth_eval()->get_vertex(particle);
if (_strict)
{
assert(candidate);
}
else if (!candidate)
{
++_errors;
continue;
}
if (get_truth_eval()->are_same_vertex(candidate, truthpoint))
{
++ntracks;
}
}
if (_do_cache)
{
_cache_get_ntracks_contribution.insert(std::make_pair(std::make_pair(vertex, truthpoint), ntracks));
}
return ntracks;
}
void SvtxVertexEval::get_node_pointers(PHCompositeNode* topNode)
{
// need things off the DST...
if (_use_initial_vertex)
{
_vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMap"); // always there, initial vertices
}
else if (_use_genfit_vertex)
{
_vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapRefit"); // Rave vertices
}
else
{
_vertexmap = findNode::getClass<SvtxVertexMap>(topNode, "SvtxVertexMapActs"); // Acts vertices
}
if (!_vertexmap)
{
std::cout << PHWHERE << "Did not find_vertexmap on node tree" << std::endl;
}
_trackmap = findNode::getClass<SvtxTrackMap>(topNode, m_TrackNodeName);
_truthinfo = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
return;
}
bool SvtxVertexEval::has_node_pointers()
{
if (_strict)
{
assert(_vertexmap);
}
else if (!_vertexmap)
{
std::cout << PHWHERE << " did not find _vertexmap " << std::endl;
return false;
}
if (_strict)
{
assert(_trackmap);
}
else if (!_trackmap)
{
std::cout << PHWHERE << " did not find _trackmap " << std::endl;
return false;
}
if (_strict)
{
assert(_truthinfo);
}
else if (!_truthinfo)
{
std::cout << PHWHERE << " did not find _truthinfo " << std::endl;
return false;
}
return true;
}
void SvtxVertexEval::set_track_nodename(const std::string& name)
{
m_TrackNodeName = name;
_trackeval.set_track_nodename(name);
}