-
Notifications
You must be signed in to change notification settings - Fork 184
/
SvtxClusterEval.cc
1513 lines (1345 loc) · 43.1 KB
/
SvtxClusterEval.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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "SvtxClusterEval.h"
#include "SvtxHitEval.h"
#include "SvtxTruthEval.h"
#include <trackbase/TrkrCluster.h>
#include <trackbase/TrkrClusterContainer.h>
#include <trackbase/TrkrClusterHitAssoc.h>
#include <trackbase/TrkrDefs.h>
#include <trackbase/TrkrHitSet.h>
#include <trackbase/TrkrHitTruthAssoc.h>
#include <trackbase_historic/ActsTransformations.h>
#include <g4main/PHG4Hit.h>
#include <g4main/PHG4HitContainer.h>
#include <g4main/PHG4HitDefs.h>
#include <g4main/PHG4Particle.h>
#include <g4main/PHG4TruthInfoContainer.h>
#include <g4main/PHG4VtxPoint.h>
#include <phool/PHTimer.h>
#include <phool/getClass.h>
#include <TVector3.h>
#include <cassert>
#include <cfloat>
#include <cmath>
#include <iostream> // for operator<<, basic_ostream
#include <map>
#include <set>
SvtxClusterEval::SvtxClusterEval(PHCompositeNode* topNode)
: _hiteval(topNode)
{
get_node_pointers(topNode);
}
SvtxClusterEval::~SvtxClusterEval()
{
if (_verbosity > 0)
{
if ((_errors > 0) || (_verbosity > 1))
{
std::cout << "SvtxClusterEval::~SvtxClusterEval() - Error Count: " << _errors << std::endl;
}
}
}
void SvtxClusterEval::next_event(PHCompositeNode* topNode)
{
_cache_all_truth_hits.clear();
_cache_all_truth_clusters.clear();
_cache_max_truth_hit_by_energy.clear();
_cache_max_truth_cluster_by_energy.clear();
_cache_all_truth_particles.clear();
_cache_max_truth_particle_by_energy.clear();
_cache_max_truth_particle_by_cluster_energy.clear();
_cache_all_clusters_from_particle.clear();
_cache_all_clusters_from_g4hit.clear();
_cache_best_cluster_from_g4hit.clear();
_cache_get_energy_contribution_g4particle.clear();
_cache_get_energy_contribution_g4hit.clear();
_cache_best_cluster_from_gtrackid_layer.clear();
_clusters_per_layer.clear();
// _g4hits_per_layer.clear();
_hiteval.next_event(topNode);
get_node_pointers(topNode);
}
std::map<TrkrDefs::cluskey, std::shared_ptr<TrkrCluster>> SvtxClusterEval::all_truth_clusters(TrkrDefs::cluskey cluster_key)
{
if (_do_cache)
{
const auto iter = _cache_all_truth_clusters.find(cluster_key);
if (iter != _cache_all_truth_clusters.end())
{
return iter->second;
}
}
std::map<TrkrDefs::cluskey, std::shared_ptr<TrkrCluster>> truth_clusters;
unsigned int cluster_layer = TrkrDefs::getLayer(cluster_key);
std::set<PHG4Particle*> particles = all_truth_particles(cluster_key);
for (auto particle : particles)
{
for (const auto& [ckey, cluster] : get_truth_eval()->all_truth_clusters(particle))
{
if (TrkrDefs::getLayer(ckey) == cluster_layer)
{
truth_clusters.insert(std::make_pair(ckey, cluster));
}
}
}
return _cache_all_truth_clusters.insert(std::make_pair(cluster_key, truth_clusters)).first->second;
}
std::pair<TrkrDefs::cluskey, std::shared_ptr<TrkrCluster>> SvtxClusterEval::max_truth_cluster_by_energy(TrkrDefs::cluskey cluster_key)
{
if (_do_cache)
{
const auto iter = _cache_max_truth_cluster_by_energy.find(cluster_key);
if (iter != _cache_max_truth_cluster_by_energy.end())
{
return iter->second;
}
}
unsigned int cluster_layer = TrkrDefs::getLayer(cluster_key);
PHG4Particle* max_particle = max_truth_particle_by_cluster_energy(cluster_key);
if (!max_particle)
{
return std::make_pair(0, nullptr);
}
if (_verbosity > 0)
{
std::cout << " max truth particle by cluster energy has trackID " << max_particle->get_track_id() << std::endl;
}
TrkrCluster* reco_cluster = _clustermap->findCluster(cluster_key);
auto global = getGlobalPosition(cluster_key, reco_cluster);
double reco_x = global(0);
double reco_y = global(1);
double reco_z = global(2);
double r = sqrt(reco_x * reco_x + reco_y * reco_y);
// double reco_rphi = r*fast_approx_atan2(reco_y, reco_x);
double reco_rphi = r * atan2(reco_y, reco_x);
const std::map<TrkrDefs::cluskey, std::shared_ptr<TrkrCluster>> gclusters = get_truth_eval()->all_truth_clusters(max_particle);
for (const auto& [ckey, candidate_truth_cluster] : gclusters)
{
if (TrkrDefs::getLayer(ckey) != cluster_layer)
{
continue;
}
double gx = candidate_truth_cluster->getX();
double gy = candidate_truth_cluster->getY();
double gz = candidate_truth_cluster->getZ();
double gr = sqrt(gx * gx + gy * gy);
double grphi = gr * atan2(gy, gx);
// double grphi = gr*fast_approx_atan2(gy, gx);
// Find the difference in position from the reco cluster
double dz = reco_z - gz;
double drphi = reco_rphi - grphi;
// approximate 4 sigmas cut
if (cluster_layer > 6 && cluster_layer < 23)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_inner &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
if (cluster_layer > 22 && cluster_layer < 39)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_mid &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
if (cluster_layer > 38 && cluster_layer < 55)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_outer &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
else if (cluster_layer < 3)
{
if (fabs(drphi) < 4.0 * sig_mvtx_rphi &&
fabs(dz) < 4.0 * sig_mvtx_z)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
else if (cluster_layer == 55)
{
if (fabs(drphi) < 4.0 * sig_mms_rphi_55)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
else if (cluster_layer == 56)
{
if (fabs(dz) < 4.0 * sig_mms_z_56)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
else
{
if (fabs(drphi) < 4.0 * sig_intt_rphi &&
fabs(dz) < range_intt_z)
{
return std::make_pair(ckey, candidate_truth_cluster);
}
}
}
return std::make_pair(0, nullptr);
}
std::pair<TrkrDefs::cluskey, TrkrCluster*> SvtxClusterEval::reco_cluster_from_truth_cluster(TrkrDefs::cluskey ckey, const std::shared_ptr<TrkrCluster>& gclus)
{
if (_do_cache)
{
/* this does not work. Cache is not filled in the code below, so always remains empty */
const auto iter = _cache_reco_cluster_from_truth_cluster.find(gclus);
if (iter != _cache_reco_cluster_from_truth_cluster.end())
{
return iter->second;
}
}
double gx = gclus->getX();
double gy = gclus->getY();
double gz = gclus->getZ();
double gr = sqrt(gx * gx + gy * gy);
double grphi = gr * atan2(gy, gx);
// double grphi = gr*fast_approx_atan2(gy, gx);
unsigned int truth_layer = TrkrDefs::getLayer(ckey);
std::set<TrkrDefs::cluskey> reco_cluskeys;
std::set<PHG4Hit*> contributing_hits = get_truth_eval()->get_truth_hits_from_truth_cluster(ckey);
for (auto cont_g4hit : contributing_hits)
{
std::set<TrkrDefs::cluskey> cluskeys = all_clusters_from(cont_g4hit); // this returns clusters from this hit in any layer using TrkrAssoc maps
if (_verbosity > 0)
{
std::cout << " contributing g4hitID " << cont_g4hit->get_hit_id() << " g4trackID " << cont_g4hit->get_trkid() << std::endl;
}
for (unsigned long iter : cluskeys)
{
unsigned int clus_layer = TrkrDefs::getLayer(iter);
if (clus_layer != truth_layer)
{
continue;
}
reco_cluskeys.insert(iter);
}
}
unsigned int nreco = reco_cluskeys.size();
if (nreco > 0)
{
// Find a matching reco cluster with position inside 4 sigmas, and replace reco_cluskey
for (const auto& this_ckey : reco_cluskeys)
{
// get the cluster
TrkrCluster* this_cluster = _clustermap->findCluster(this_ckey);
auto global = getGlobalPosition(this_ckey, this_cluster);
double this_x = global(0);
double this_y = global(1);
double this_z = global(2);
double this_rphi = gr * atan2(this_y, this_x);
// double this_rphi = gr*fast_approx_atan2(this_y, this_x);
// Find the difference in position from the g4cluster
double dz = this_z - gz;
double drphi = this_rphi - grphi;
// approximate 4 sigmas cut
if (truth_layer > 6 && truth_layer < 23)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_inner &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(this_ckey, this_cluster);
}
}
if (truth_layer > 22 && truth_layer < 39)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_mid &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(this_ckey, this_cluster);
}
}
if (truth_layer > 38 && truth_layer < 55)
{
if (fabs(drphi) < 4.0 * sig_tpc_rphi_outer &&
fabs(dz) < 4.0 * sig_tpc_z)
{
return std::make_pair(this_ckey, this_cluster);
}
}
else if (truth_layer < 3)
{
if (fabs(drphi) < 4.0 * sig_mvtx_rphi &&
fabs(dz) < 4.0 * sig_mvtx_z)
{
return std::make_pair(this_ckey, this_cluster);
}
}
else if (truth_layer == 55)
{
if (fabs(drphi) < 4.0 * sig_mms_rphi_55)
{
return std::make_pair(this_ckey, this_cluster);
}
}
else if (truth_layer == 56)
{
if (fabs(dz) < 4.0 * sig_mms_z_56)
{
return std::make_pair(this_ckey, this_cluster);
}
}
else
{
if (fabs(drphi) < 4.0 * sig_intt_rphi &&
fabs(dz) < range_intt_z)
{
return std::make_pair(this_ckey, this_cluster);
}
}
}
}
return std::make_pair(0, nullptr);
}
std::set<PHG4Hit*> SvtxClusterEval::all_truth_hits(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return std::set<PHG4Hit*>();
}
if (_do_cache)
{
std::map<TrkrDefs::cluskey, std::set<PHG4Hit*>>::iterator iter =
_cache_all_truth_hits.find(cluster_key);
if (iter != _cache_all_truth_hits.end())
{
return iter->second;
}
}
std::set<PHG4Hit*> truth_hits;
// get all truth hits for this cluster
//_cluster_hit_map->identify();
std::pair<std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator, std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator>
hitrange = _cluster_hit_map->getHits(cluster_key); // returns range of pairs {cluster key, hit key} for this cluskey
for (std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator
clushititer = hitrange.first;
clushititer != hitrange.second; ++clushititer)
{
TrkrDefs::hitkey hitkey = clushititer->second;
// TrkrHitTruthAssoc uses a map with (hitsetkey, std::pair(hitkey, g4hitkey)) - get the hitsetkey from the cluskey
TrkrDefs::hitsetkey hitsetkey = TrkrDefs::getHitSetKeyFromClusKey(cluster_key);
// get all of the g4hits for this hitkey
std::multimap<TrkrDefs::hitsetkey, std::pair<TrkrDefs::hitkey, PHG4HitDefs::keytype>> temp_map;
_hit_truth_map->getG4Hits(hitsetkey, hitkey, temp_map);
// returns pairs (hitsetkey, std::pair(hitkey, g4hitkey)) for this hitkey only
for (auto& htiter : temp_map)
{
// extract the g4 hit key here and add the hits to the set
PHG4HitDefs::keytype g4hitkey = htiter.second.second;
PHG4Hit* g4hit = nullptr;
unsigned int trkrid = TrkrDefs::getTrkrId(hitsetkey);
switch (trkrid)
{
case TrkrDefs::tpcId:
g4hit = _g4hits_tpc->findHit(g4hitkey);
break;
case TrkrDefs::inttId:
g4hit = _g4hits_intt->findHit(g4hitkey);
break;
case TrkrDefs::mvtxId:
g4hit = _g4hits_mvtx->findHit(g4hitkey);
break;
case TrkrDefs::micromegasId:
g4hit = _g4hits_mms->findHit(g4hitkey);
break;
default:
break;
}
if (g4hit)
{
truth_hits.insert(g4hit);
}
} // end loop over g4hits associated with hitsetkey and hitkey
} // end loop over hits associated with cluskey
if (_do_cache)
{
_cache_all_truth_hits.insert(std::make_pair(cluster_key, truth_hits));
}
return truth_hits;
}
PHG4Hit* SvtxClusterEval::all_truth_hits_by_nhit(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
// if (_strict)
// {
// assert(cluster_key);
// }
// else if (!cluster_key)
// {
// ++_errors;
// return std::set<PHG4Hit*>();
// }
/*
if (_do_cache)
{
std::map<TrkrDefs::cluskey, std::set<PHG4Hit*> >::iterator iter =
_cache_all_truth_hits.find(cluster_key);
if (iter != _cache_all_truth_hits.end())
{
return iter->second;
}
}
*/
TrkrCluster* cluster = _clustermap->findCluster(cluster_key);
auto glob = getGlobalPosition(cluster_key, cluster);
TVector3 cvec(glob(0), glob(1), glob(2));
unsigned int layer = TrkrDefs::getLayer(cluster_key);
std::set<PHG4Hit*> truth_hits;
std::multimap<PHG4HitDefs::keytype, TrkrDefs::hitkey> g4keyperhit;
std::vector<PHG4HitDefs::keytype> g4hitkeys;
// get all truth hits for this cluster
//_cluster_hit_map->identify();
TrkrDefs::hitsetkey hitsetkey = TrkrDefs::getHitSetKeyFromClusKey(cluster_key);
std::pair<std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator, std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator>
hitrange = _cluster_hit_map->getHits(cluster_key); // returns range of pairs {cluster key, hit key} for this cluskey
for (std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator
clushititer = hitrange.first;
clushititer != hitrange.second; ++clushititer)
{
TrkrDefs::hitkey hitkey = clushititer->second;
// TrkrHitTruthAssoc uses a map with (hitsetkey, std::pair(hitkey, g4hitkey)) - get the hitsetkey from the cluskey
// get all of the g4hits for this hitkey
std::multimap<TrkrDefs::hitsetkey, std::pair<TrkrDefs::hitkey, PHG4HitDefs::keytype>> temp_map;
_hit_truth_map->getG4Hits(hitsetkey, hitkey, temp_map); // returns pairs (hitsetkey, std::pair(hitkey, g4hitkey)) for this hitkey only
for (auto& htiter : temp_map)
{
// extract the g4 hit key here and add the hits to the set
PHG4HitDefs::keytype g4hitkey = htiter.second.second;
if (_verbosity > 2)
{
std::cout << " g4key: " << g4hitkey << " layer: " << layer << std::endl;
}
TrkrDefs::hitkey local_hitkey = htiter.second.first;
/* if(layer>=7){
PHG4Hit *match_g4hit = _g4hits_tpc->findHit(g4hitkey);
if(layer != match_g4hit->get_layer() ) continue;
}
*/
g4keyperhit.insert(std::pair<PHG4HitDefs::keytype, TrkrDefs::hitkey>(g4hitkey, local_hitkey));
std::vector<PHG4HitDefs::keytype>::iterator itg4keys = find(g4hitkeys.begin(), g4hitkeys.end(), g4hitkey);
if (itg4keys == g4hitkeys.end())
{
g4hitkeys.push_back(g4hitkey);
}
} // end loop over g4hits associated with hitsetkey and hitkey
} // end loop over hits associated with cluskey
// if (_do_cache) _cache_all_truth_hits.insert(std::make_pair(cluster_key, truth_hits));
PHG4HitDefs::keytype max_key = 0;
unsigned int n_max = 0;
if (g4hitkeys.size() == 1)
{
std::vector<PHG4HitDefs::keytype>::iterator it = g4hitkeys.begin();
max_key = *it;
}
else
{
for (unsigned long long& g4hitkey : g4hitkeys)
{
unsigned int ng4hit = g4keyperhit.count(g4hitkey);
PHG4Hit* this_g4hit = _g4hits_tpc->findHit(g4hitkey);
if (layer >= 7)
{ // in tpc
if (this_g4hit != nullptr)
{
unsigned int glayer = this_g4hit->get_layer();
if (layer != glayer)
{
continue;
}
TVector3 vec(this_g4hit->get_avg_x(), this_g4hit->get_avg_y(), this_g4hit->get_avg_z());
// std::cout << "layer: " << layer << " (" << glayer << ") " << " gtrackID: " << this_g4hit->get_trkid() << " novlp: " << ng4hit << " phi: " << vec.Phi() << " z: " << this_g4hit->get_avg_z() << " r: " << vec.Perp() << " keyg4: " << *it << std::endl; //<< " keyrec: "<< *it.second << std::endl;
}
/*else{
std::cout << "g4hit == NULL " << std::endl;
}
*/
}
if (ng4hit > n_max)
{
max_key = g4hitkey;
n_max = ng4hit;
}
}
}
PHG4Hit* g4hit = nullptr;
unsigned int trkrid = TrkrDefs::getTrkrId(hitsetkey);
switch (trkrid)
{
case TrkrDefs::tpcId:
g4hit = _g4hits_tpc->findHit(max_key);
break;
case TrkrDefs::inttId:
g4hit = _g4hits_intt->findHit(max_key);
break;
case TrkrDefs::mvtxId:
g4hit = _g4hits_mvtx->findHit(max_key);
break;
case TrkrDefs::micromegasId:
g4hit = _g4hits_mms->findHit(max_key);
break;
default:
break;
}
if (g4hit)
{
truth_hits.insert(g4hit);
}
return g4hit;
}
std::pair<int, int> SvtxClusterEval::gtrackid_and_layer_by_nhit(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return std::make_pair(0, 0);
}
// if (_strict)
// {
// assert(cluster_key);
// }
// else if (!cluster_key)
// {
// ++_errors;
// return std::set<PHG4Hit*>();
// }
/*
if (_do_cache)
{
std::map<TrkrDefs::cluskey, std::set<PHG4Hit*> >::iterator iter =
_cache_all_truth_hits.find(cluster_key);
if (iter != _cache_all_truth_hits.end())
{
return iter->second;
}
}
*/
std::pair<int, int> out_pair;
out_pair.first = 0;
out_pair.second = -1;
TrkrCluster* cluster = _clustermap->findCluster(cluster_key);
auto global = getGlobalPosition(cluster_key, cluster);
TVector3 cvec(global(0), global(1), global(2));
unsigned int layer = TrkrDefs::getLayer(cluster_key);
std::multimap<PHG4HitDefs::keytype, TrkrDefs::hitkey> g4keyperhit;
std::vector<PHG4HitDefs::keytype> g4hitkeys;
// get all truth hits for this cluster
//_cluster_hit_map->identify();
TrkrDefs::hitsetkey hitsetkey = TrkrDefs::getHitSetKeyFromClusKey(cluster_key);
std::pair<std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator, std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator>
hitrange = _cluster_hit_map->getHits(cluster_key); // returns range of pairs {cluster key, hit key} for this cluskey
for (std::multimap<TrkrDefs::cluskey, TrkrDefs::hitkey>::const_iterator
clushititer = hitrange.first;
clushititer != hitrange.second; ++clushititer)
{
TrkrDefs::hitkey hitkey = clushititer->second;
// TrkrHitTruthAssoc uses a map with (hitsetkey, std::pair(hitkey, g4hitkey)) - get the hitsetkey from the cluskey
// get all of the g4hits for this hitkey
std::multimap<TrkrDefs::hitsetkey, std::pair<TrkrDefs::hitkey, PHG4HitDefs::keytype>> temp_map;
_hit_truth_map->getG4Hits(hitsetkey, hitkey, temp_map); // returns pairs (hitsetkey, std::pair(hitkey, g4hitkey)) for this hitkey only
for (auto& htiter : temp_map)
{
// extract the g4 hit key here and add the hits to the set
PHG4HitDefs::keytype g4hitkey = htiter.second.second;
if (_verbosity > 2)
{
std::cout << " g4key: " << g4hitkey << " layer: " << layer << std::endl;
}
TrkrDefs::hitkey local_hitkey = htiter.second.first;
/* if(layer>=7){
PHG4Hit *match_g4hit = _g4hits_tpc->findHit(g4hitkey);
if(layer != match_g4hit->get_layer() ) continue;
}
*/
g4keyperhit.insert(std::pair<PHG4HitDefs::keytype, TrkrDefs::hitkey>(g4hitkey, local_hitkey));
std::vector<PHG4HitDefs::keytype>::iterator itg4keys = find(g4hitkeys.begin(), g4hitkeys.end(), g4hitkey);
if (itg4keys == g4hitkeys.end())
{
g4hitkeys.push_back(g4hitkey);
}
} // end loop over g4hits associated with hitsetkey and hitkey
} // end loop over hits associated with cluskey
PHG4HitDefs::keytype max_key = 0;
unsigned int n_max = 0;
if (_verbosity > 2)
{
std::cout << " n matches found: " << g4hitkeys.size() << " phi: " << cvec.Phi() << " z: " << cvec.Z() << " ckey: " << cluster_key << std::endl;
}
if (g4hitkeys.size() == 1)
{
std::vector<PHG4HitDefs::keytype>::iterator it = g4hitkeys.begin();
max_key = *it;
}
else
{
for (unsigned long long& g4hitkey : g4hitkeys)
{
unsigned int ng4hit = g4keyperhit.count(g4hitkey);
PHG4Hit* this_g4hit = _g4hits_tpc->findHit(g4hitkey);
if (layer >= 7)
{ // in tpc
if (this_g4hit != nullptr)
{
unsigned int glayer = this_g4hit->get_layer();
// if(layer != glayer) continue;
TVector3 vec(this_g4hit->get_avg_x(), this_g4hit->get_avg_y(), this_g4hit->get_avg_z());
if (_verbosity > 2)
{
std::cout << "layer: " << layer << " (" << glayer << ") "
<< " gtrackID: " << this_g4hit->get_trkid() << " novlp: " << ng4hit << " phi: " << vec.Phi() << " z: " << this_g4hit->get_avg_z() << " r: " << vec.Perp() << " keyg4: " << g4hitkey << " cz: " << cluster->getZ() << std::endl; //<< " keyrec: "<< *it.second << std::endl;
}
}
}
if (ng4hit > n_max)
{
max_key = g4hitkey;
n_max = ng4hit;
}
}
}
if (_verbosity > 2)
{
std::cout << "found in layer: " << layer << " n_max: " << n_max << " max_key: " << max_key << " ckey: " << cluster_key << std::endl;
}
if (max_key != 0)
{
PHG4Hit* g4hit = nullptr;
unsigned int trkrid = TrkrDefs::getTrkrId(hitsetkey);
switch (trkrid)
{
case TrkrDefs::tpcId:
g4hit = _g4hits_tpc->findHit(max_key);
break;
case TrkrDefs::inttId:
g4hit = _g4hits_intt->findHit(max_key);
break;
case TrkrDefs::mvtxId:
g4hit = _g4hits_mvtx->findHit(max_key);
break;
case TrkrDefs::micromegasId:
g4hit = _g4hits_mms->findHit(max_key);
break;
default:
break;
}
// check if we on a looper
PHG4Particle* g4particle = _truthinfo->GetParticle(g4hit->get_trkid());
PHG4VtxPoint* vtx = _truthinfo->GetVtx(g4particle->get_vtx_id());
float vtx_z = vtx->get_z();
float gpx = g4particle->get_px();
float gpy = g4particle->get_py();
float gpz = g4particle->get_pz();
float gpeta = NAN;
TVector3 gv(gpx, gpy, gpz);
gpeta = gv.Eta();
TVector3 this_vec(g4hit->get_avg_x(),
g4hit->get_avg_y(),
g4hit->get_avg_z() - vtx_z);
double deta = TMath::Abs(gpeta - this_vec.Eta());
int is_loop = 0;
if (layer >= 7)
{
// std::cout << " in tpc " << std::endl;
if (deta > 0.1)
{
is_loop = 1;
}
}
out_pair.first = g4hit->get_trkid();
if (!is_loop)
{
out_pair.second = layer;
}
}
return out_pair;
}
PHG4Hit* SvtxClusterEval::max_truth_hit_by_energy(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
if (_do_cache)
{
std::map<TrkrDefs::cluskey, PHG4Hit*>::iterator iter =
_cache_max_truth_hit_by_energy.find(cluster_key);
if (iter != _cache_max_truth_hit_by_energy.end())
{
return iter->second;
}
}
std::set<PHG4Hit*> hits = all_truth_hits(cluster_key);
PHG4Hit* max_hit = nullptr;
float max_e = FLT_MAX * -1.0;
for (auto hit : hits)
{
if (hit->get_edep() > max_e)
{
max_e = hit->get_edep();
max_hit = hit;
}
}
if (_do_cache)
{
_cache_max_truth_hit_by_energy.insert(std::make_pair(cluster_key, max_hit));
}
return max_hit;
}
std::set<PHG4Particle*> SvtxClusterEval::all_truth_particles(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return std::set<PHG4Particle*>();
}
if (_do_cache)
{
std::map<TrkrDefs::cluskey, std::set<PHG4Particle*>>::iterator iter =
_cache_all_truth_particles.find(cluster_key);
if (iter != _cache_all_truth_particles.end())
{
return iter->second;
}
}
std::set<PHG4Particle*> truth_particles;
std::set<PHG4Hit*> g4hits = all_truth_hits(cluster_key);
for (auto hit : g4hits)
{
PHG4Particle* particle = get_truth_eval()->get_particle(hit);
// std::cout << "cluster key " << cluster_key << " has hit " << hit->get_hit_id() << " and has particle " << particle->get_track_id() << std::endl;
if (_strict)
{
assert(particle);
}
else if (!particle)
{
++_errors;
continue;
}
truth_particles.insert(particle);
}
if (_do_cache)
{
_cache_all_truth_particles.insert(std::make_pair(cluster_key, truth_particles));
}
return truth_particles;
}
PHG4Particle* SvtxClusterEval::max_truth_particle_by_cluster_energy(TrkrDefs::cluskey cluster_key)
{
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
if (_do_cache)
{
std::map<TrkrDefs::cluskey, PHG4Particle*>::iterator iter =
_cache_max_truth_particle_by_cluster_energy.find(cluster_key);
if (iter != _cache_max_truth_particle_by_cluster_energy.end())
{
return iter->second;
}
}
unsigned int layer = TrkrDefs::getLayer(cluster_key);
// loop over all particles associated with this cluster and
// get the energy contribution for each one, record the max
PHG4Particle* max_particle = nullptr;
float max_e = FLT_MAX * -1.0;
std::set<PHG4Particle*> particles = all_truth_particles(cluster_key);
for (auto particle : particles)
{
std::map<TrkrDefs::cluskey, std::shared_ptr<TrkrCluster>> truth_clus = get_truth_eval()->all_truth_clusters(particle);
for (const auto& [ckey, cluster] : truth_clus)
{
if (TrkrDefs::getLayer(ckey) == layer)
{
float e = cluster->getError(0, 0);
if (e > max_e)
{
max_e = e;
max_particle = particle;
}
}
}
}
if (_do_cache)
{
_cache_max_truth_particle_by_cluster_energy.insert(std::make_pair(cluster_key, max_particle));
}
return max_particle;
}
PHG4Particle* SvtxClusterEval::max_truth_particle_by_energy(TrkrDefs::cluskey cluster_key)
{
// Note: this does not quite work correctly for the TPC - it assumes one g4hit per layer
// use max_truth_particle_by_cluster_energy instead
if (!has_node_pointers())
{
++_errors;
return nullptr;
}
if (_do_cache)
{
std::map<TrkrDefs::cluskey, PHG4Particle*>::iterator iter =
_cache_max_truth_particle_by_energy.find(cluster_key);
if (iter != _cache_max_truth_particle_by_energy.end())
{
return iter->second;
}
}
// loop over all particles associated with this cluster and
// get the energy contribution for each one, record the max
PHG4Particle* max_particle = nullptr;
float max_e = FLT_MAX * -1.0;
std::set<PHG4Particle*> particles = all_truth_particles(cluster_key);
for (auto particle : particles)
{
float e = get_energy_contribution(cluster_key, particle);
if (e > max_e)
{
max_e = e;
max_particle = particle;
}
}
if (_do_cache)
{
_cache_max_truth_particle_by_energy.insert(std::make_pair(cluster_key, max_particle));
}
return max_particle;
}
std::set<TrkrDefs::cluskey> SvtxClusterEval::all_clusters_from(PHG4Particle* truthparticle)
{
if (!has_node_pointers())
{
++_errors;
return std::set<TrkrDefs::cluskey>();
}
if (_strict)
{
assert(truthparticle);
}
else if (!truthparticle)
{
++_errors;
return std::set<TrkrDefs::cluskey>();
}
// check if cache is filled, if not fill it.
// if(_cache_all_clusters_from_particle.count(truthparticle)==0){
if (_cache_all_clusters_from_particle.empty())
{
FillRecoClusterFromG4HitCache();
}
if (_do_cache)
{
std::map<PHG4Particle*, std::set<TrkrDefs::cluskey>>::iterator iter =
_cache_all_clusters_from_particle.find(truthparticle);
if (iter != _cache_all_clusters_from_particle.end())
{
return iter->second;
}
}
std::set<TrkrDefs::cluskey> clusters;
return clusters;
}
void SvtxClusterEval::FillRecoClusterFromG4HitCache()
{
auto Mytimer = std::make_unique<PHTimer>("ReCl_timer");
Mytimer->stop();
Mytimer->restart();
std::multimap<PHG4Particle*, TrkrDefs::cluskey> temp_clusters_from_particles;
// loop over all the clusters
for (const auto& hitsetkey : _clustermap->getHitSetKeys())
{
auto range = _clustermap->getClusters(hitsetkey);
for (auto iter = range.first; iter != range.second; ++iter)
{
TrkrDefs::cluskey cluster_key = iter->first;
// loop over all truth particles connected to this cluster
std::set<PHG4Particle*> particles = all_truth_particles(cluster_key);
for (auto candidate : particles)
{
temp_clusters_from_particles.insert(std::make_pair(candidate, cluster_key));
}
}
}
// Loop over particles and fill cache
PHG4TruthInfoContainer::ConstRange range = _truthinfo->GetParticleRange();
for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
iter != range.second; ++iter)
{
PHG4Particle* g4particle = iter->second;
std::set<TrkrDefs::cluskey> clusters;
std::multimap<PHG4Particle*, TrkrDefs::cluskey>::const_iterator lower_bound = temp_clusters_from_particles.lower_bound(g4particle);
std::multimap<PHG4Particle*, TrkrDefs::cluskey>::const_iterator upper_bound = temp_clusters_from_particles.upper_bound(g4particle);
std::multimap<PHG4Particle*, TrkrDefs::cluskey>::const_iterator cfp_iter;
for (cfp_iter = lower_bound; cfp_iter != upper_bound; ++cfp_iter)
{
TrkrDefs::cluskey cluster_key = cfp_iter->second;
clusters.insert(cluster_key);
}
_cache_all_clusters_from_particle.insert(std::make_pair(g4particle, clusters));
}
Mytimer->stop();