Skip to content

Commit 5c3eee0

Browse files
committed
Merge pull request #57 from mccumbermike/calo_eval_looping
Calorimeter evaluator modifications from Mike
2 parents 5d9ac4e + 888756a commit 5c3eee0

8 files changed

Lines changed: 105 additions & 59 deletions

File tree

simulation/g4simulation/g4eval/CaloEvaluator.C

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
#include <TFile.h>
2222

2323
#include <iostream>
24-
//#include <vector>
24+
#include <set>
2525
//#include <algorithm>
2626
#include <cmath>
2727

@@ -31,6 +31,8 @@ CaloEvaluator::CaloEvaluator(const string &name, const string &caloname, const s
3131
: SubsysReco(name),
3232
_caloname(caloname),
3333
_ievent(0),
34+
_truth_trace_embed_flags(),
35+
_reco_e_threshold(0.0), // 0 GeV before reco is traced
3436
_do_gpoint_eval(true),
3537
_do_gshower_eval(true),
3638
_do_tower_eval(true),
@@ -90,7 +92,7 @@ int CaloEvaluator::process_event(PHCompositeNode *topNode) {
9092
//---------------------------
9193

9294
fillOutputNtuples(topNode);
93-
95+
9496
//--------------------------------------------------
9597
// Print out the ancestry information for this event
9698
//--------------------------------------------------
@@ -368,6 +370,11 @@ void CaloEvaluator::fillOutputNtuples(PHCompositeNode *topNode) {
368370
iter != map.end();
369371
++iter) {
370372
PHG4Particle* primary = iter->second;
373+
374+
if (!_truth_trace_embed_flags.empty()) {
375+
if (_truth_trace_embed_flags.find(trutheval->get_embed(primary)) ==
376+
_truth_trace_embed_flags.end()) continue;
377+
}
371378

372379
float gparticleID = primary->get_track_id();
373380
float gflavor = primary->get_pid();
@@ -466,6 +473,8 @@ void CaloEvaluator::fillOutputNtuples(PHCompositeNode *topNode) {
466473
for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter) {
467474
RawTower *tower = rtiter->second;
468475

476+
if (tower->get_energy() < _reco_e_threshold) continue;
477+
469478
float towerid = tower->get_id();
470479
float ieta = tower->get_bineta();
471480
float iphi = tower->get_binphi();
@@ -573,6 +582,8 @@ void CaloEvaluator::fillOutputNtuples(PHCompositeNode *topNode) {
573582
for (unsigned int icluster = 0; icluster < clusters->size(); icluster++) {
574583
RawCluster *cluster = clusters->getCluster(icluster);
575584

585+
if (cluster->get_energy() < _reco_e_threshold) continue;
586+
576587
float clusterID = cluster->get_id();
577588
float ntowers = cluster->getNTowers();
578589
float eta = cluster->get_eta();

simulation/g4simulation/g4eval/CaloEvaluator.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <TNtuple.h>
1414
#include <TFile.h>
1515

16+
#include <set>
1617
#include <string>
1718

1819
/// \class CaloEvaluator
@@ -36,12 +37,30 @@ class CaloEvaluator : public SubsysReco {
3637
int process_event(PHCompositeNode *topNode);
3738
int End(PHCompositeNode *topNode);
3839

40+
// forward trace only off of primaries with embed flags set
41+
void add_truth_tracing_embed_flag(int flag) {
42+
_truth_trace_embed_flags.insert(flag);
43+
}
44+
45+
// backward trace only if reco meets an energy threshold requirement
46+
void set_reco_tracing_energy_threshold(float thresh) {
47+
_reco_e_threshold = thresh;
48+
}
49+
50+
void set_do_gpoint_eval(bool b) {_do_gpoint_eval = b;}
51+
void set_do_gshower_eval(bool b) {_do_gshower_eval = b;}
52+
void set_do_tower_eval(bool b) {_do_tower_eval = b;}
53+
void set_do_cluster_eval(bool b) {_do_cluster_eval = b;}
54+
3955
private:
4056

4157
std::string _caloname;
4258

4359
unsigned long _ievent;
4460

61+
std::set<int> _truth_trace_embed_flags;
62+
float _reco_e_threshold;
63+
4564
//----------------------------------
4665
// evaluator output ntuples
4766

simulation/g4simulation/g4eval/CaloRawClusterEval.C

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,21 @@ std::set<PHG4Hit*> CaloRawClusterEval::all_truth_hits(RawCluster* cluster) {
6464
}
6565

6666
std::set<PHG4Hit*> truth_hits;
67-
67+
6868
// loop over all the clustered towers
6969
RawCluster::TowerConstRange begin_end = cluster->get_towers();
7070
RawCluster::TowerConstIterator iter;
7171
for (iter = begin_end.first; iter != begin_end.second; ++iter)
7272
{
7373
RawTower* tower = _towers->getTower(iter->first);
7474

75-
std::set<PHG4Hit*> new_hits = _towereval.all_truth_hits(tower);
76-
std::set<PHG4Hit*> union_hits;
75+
std::set<PHG4Hit*> new_hits = get_rawtower_eval()->all_truth_hits(tower);
7776

78-
std::set_union(truth_hits.begin(),truth_hits.end(),
79-
new_hits.begin(),new_hits.end(),
80-
std::inserter(union_hits,union_hits.begin()));
81-
82-
std::swap(truth_hits,union_hits); // swap union into truth_particles
77+
for (std::set<PHG4Hit*>::iterator iter = new_hits.begin();
78+
iter != new_hits.end();
79+
++iter) {
80+
truth_hits.insert(*iter);
81+
}
8382
}
8483

8584
if (_do_cache) _cache_all_truth_hits.insert(make_pair(cluster,truth_hits));
@@ -106,13 +105,12 @@ std::set<PHG4Particle*> CaloRawClusterEval::all_truth_primaries(RawCluster* clus
106105
{
107106
RawTower* tower = _towers->getTower(iter->first);
108107
std::set<PHG4Particle*> new_primaries = _towereval.all_truth_primaries(tower);
109-
std::set<PHG4Particle*> union_primaries;
110108

111-
std::set_union(truth_primaries.begin(),truth_primaries.end(),
112-
new_primaries.begin(),new_primaries.end(),
113-
std::inserter(union_primaries,union_primaries.begin()));
114-
115-
std::swap(truth_primaries,union_primaries); // swap union into truth_particles
109+
for (std::set<PHG4Particle*>::iterator iter = new_primaries.begin();
110+
iter != new_primaries.end();
111+
++iter) {
112+
truth_primaries.insert(*iter);
113+
}
116114
}
117115

118116
if (_do_cache) _cache_all_truth_primaries.insert(make_pair(cluster,truth_primaries));
@@ -141,6 +139,7 @@ PHG4Particle* CaloRawClusterEval::max_truth_primary_by_energy(RawCluster* cluste
141139

142140
PHG4Particle* primary = *iter;
143141
float e = get_energy_contribution(cluster,primary);
142+
if (isnan(e)) continue;
144143
if (e > max_e) {
145144
max_e = e;
146145
max_primary = primary;
@@ -154,15 +153,18 @@ PHG4Particle* CaloRawClusterEval::max_truth_primary_by_energy(RawCluster* cluste
154153

155154
std::set<RawCluster*> CaloRawClusterEval::all_clusters_from(PHG4Particle* primary) {
156155

157-
CaloTruthEval* trutheval = _towereval.get_truth_eval();
158-
if (!trutheval->is_primary(primary)) return std::set<RawCluster*>();
159-
160-
if ((_do_cache) &&
161-
(_cache_all_clusters_from_primary.find(primary) != _cache_all_clusters_from_primary.end())) {
162-
return _cache_all_clusters_from_primary[primary];
156+
if (!get_truth_eval()->is_primary(primary)) return std::set<RawCluster*>();
157+
158+
primary = get_truth_eval()->get_primary_particle(primary);
159+
160+
if (_do_cache) {
161+
std::map<PHG4Particle*,std::set<RawCluster*> >::iterator iter =
162+
_cache_all_clusters_from_primary.find(primary);
163+
if (iter != _cache_all_clusters_from_primary.end()) {
164+
return iter->second;
165+
}
163166
}
164167

165-
166168
std::set<RawCluster*> clusters;
167169

168170
// loop over all the clusters
@@ -191,8 +193,9 @@ std::set<RawCluster*> CaloRawClusterEval::all_clusters_from(PHG4Particle* primar
191193

192194
RawCluster* CaloRawClusterEval::best_cluster_from(PHG4Particle* primary) {
193195

194-
CaloTruthEval* trutheval = _towereval.get_truth_eval();
195-
if (!trutheval->is_primary(primary)) return NULL;
196+
if (!get_truth_eval()->is_primary(primary)) return NULL;
197+
198+
primary = get_truth_eval()->get_primary_particle(primary);
196199

197200
if (_do_cache) {
198201
std::map<PHG4Particle*,RawCluster*>::iterator iter =
@@ -203,13 +206,14 @@ RawCluster* CaloRawClusterEval::best_cluster_from(PHG4Particle* primary) {
203206
}
204207

205208
RawCluster* best_cluster = NULL;
206-
float best_energy = 0.0;
209+
float best_energy = FLT_MAX*-1.0;
207210
std::set<RawCluster*> clusters = all_clusters_from(primary);
208211
for (std::set<RawCluster*>::iterator iter = clusters.begin();
209212
iter != clusters.end();
210213
++iter) {
211214
RawCluster* cluster = *iter;
212215
float energy = get_energy_contribution(cluster,primary);
216+
if(isnan(energy)) continue;
213217
if (energy > best_energy) {
214218
best_cluster = cluster;
215219
best_energy = energy;
@@ -224,8 +228,10 @@ RawCluster* CaloRawClusterEval::best_cluster_from(PHG4Particle* primary) {
224228
// overlap calculations
225229
float CaloRawClusterEval::get_energy_contribution(RawCluster* cluster, PHG4Particle* primary) {
226230

227-
CaloTruthEval* trutheval = _towereval.get_truth_eval();
228-
if (!trutheval->is_primary(primary)) return NAN;
231+
if (!get_truth_eval()->is_primary(primary)) return NAN;
232+
233+
// reduce cache misses by using only pointer from PrimaryMap
234+
primary = get_truth_eval()->get_primary_particle(primary);
229235

230236
if (_do_cache) {
231237
std::map<std::pair<RawCluster*,PHG4Particle*>,float>::iterator iter =
@@ -241,7 +247,8 @@ float CaloRawClusterEval::get_energy_contribution(RawCluster* cluster, PHG4Parti
241247
iter != g4hits.end();
242248
++iter) {
243249
PHG4Hit* g4hit = *iter;
244-
if (g4hit->get_trkid() == primary->get_track_id()) {
250+
PHG4Particle* candidate = get_truth_eval()->get_primary_particle(g4hit);
251+
if (candidate->get_track_id() == primary->get_track_id()) {
245252
energy += g4hit->get_edep();
246253
}
247254
}

simulation/g4simulation/g4eval/CaloRawTowerEval.C

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,7 @@ std::set<PHG4Particle*> CaloRawTowerEval::all_truth_primaries(RawTower* tower) {
105105
iter != g4hits.end();
106106
++iter) {
107107
PHG4Hit* g4hit = *iter;
108-
PHG4Particle* particle = _truthinfo->GetHit( g4hit->get_trkid() );
109-
PHG4Particle* primary = _trutheval.get_primary_particle( particle );
110-
if (!primary) continue;
108+
PHG4Particle* primary = get_truth_eval()->get_primary_particle( g4hit );
111109
truth_primaries.insert(primary);
112110
}
113111

@@ -131,12 +129,14 @@ PHG4Particle* CaloRawTowerEval::max_truth_primary_by_energy(RawTower* tower) {
131129
PHG4Particle* max_primary = NULL;
132130
float max_e = FLT_MAX*-1.0;
133131
std::set<PHG4Particle*> primaries = all_truth_primaries(tower);
132+
134133
for (std::set<PHG4Particle*>::iterator iter = primaries.begin();
135134
iter != primaries.end();
136135
++iter) {
137136

138137
PHG4Particle* primary = *iter;
139138
float e = get_energy_contribution(tower,primary);
139+
if (isnan(e)) continue;
140140
if (e > max_e) {
141141
max_e = e;
142142
max_primary = primary;
@@ -151,6 +151,9 @@ PHG4Particle* CaloRawTowerEval::max_truth_primary_by_energy(RawTower* tower) {
151151
std::set<RawTower*> CaloRawTowerEval::all_towers_from(PHG4Particle* primary) {
152152

153153
if (!_trutheval.is_primary(primary)) return std::set<RawTower*>();
154+
155+
// use primary map pointer
156+
primary = get_truth_eval()->get_primary_particle(primary);
154157

155158
if (_do_cache) {
156159
std::map<PHG4Particle*,std::set<RawTower*> >::iterator iter =
@@ -189,7 +192,9 @@ std::set<RawTower*> CaloRawTowerEval::all_towers_from(PHG4Particle* primary) {
189192
RawTower* CaloRawTowerEval::best_tower_from(PHG4Particle* primary) {
190193

191194
if (!_trutheval.is_primary(primary)) return NULL;
192-
195+
196+
primary = get_truth_eval()->get_primary_particle(primary);
197+
193198
if (_do_cache) {
194199
std::map<PHG4Particle*,RawTower*>::iterator iter =
195200
_cache_best_tower_from_primary.find(primary);
@@ -199,13 +204,14 @@ RawTower* CaloRawTowerEval::best_tower_from(PHG4Particle* primary) {
199204
}
200205

201206
RawTower* best_tower = NULL;
202-
float best_energy = 0.0;
207+
float best_energy = FLT_MAX*-1.0;
203208
std::set<RawTower*> towers = all_towers_from(primary);
204209
for (std::set<RawTower*>::iterator iter = towers.begin();
205210
iter != towers.end();
206211
++iter) {
207212
RawTower* tower = *iter;
208213
float energy = get_energy_contribution(tower,primary);
214+
if (isnan(energy)) continue;
209215
if (energy > best_energy) {
210216
best_tower = tower;
211217
best_energy = energy;
@@ -221,6 +227,9 @@ RawTower* CaloRawTowerEval::best_tower_from(PHG4Particle* primary) {
221227
float CaloRawTowerEval::get_energy_contribution(RawTower* tower, PHG4Particle* primary) {
222228

223229
if (!_trutheval.is_primary(primary)) return NAN;
230+
231+
// reduce cache misses by using only pointer from PrimaryMap
232+
primary = get_truth_eval()->get_primary_particle(primary);
224233

225234
if (_do_cache) {
226235
std::map<std::pair<RawTower*,PHG4Particle*>, float>::iterator iter =
@@ -236,7 +245,8 @@ float CaloRawTowerEval::get_energy_contribution(RawTower* tower, PHG4Particle* p
236245
iter != g4hits.end();
237246
++iter) {
238247
PHG4Hit* g4hit = *iter;
239-
if (g4hit->get_trkid() == primary->get_track_id()) {
248+
PHG4Particle* candidate = get_truth_eval()->get_primary_particle(g4hit);
249+
if (candidate->get_track_id() == primary->get_track_id()) {
240250
energy += g4hit->get_edep();
241251
}
242252
}

simulation/g4simulation/g4eval/CaloTruthEval.C

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ CaloTruthEval::CaloTruthEval(PHCompositeNode* topNode,std::string caloname)
2323
_cache_all_truth_hits_g4particle(),
2424
_cache_get_primary_particle_g4particle(),
2525
_cache_get_primary_particle_g4hit(),
26-
_cache_is_primary(),
2726
_cache_get_shower_from_primary(),
2827
_cache_get_shower_moliere_radius(),
2928
_cache_get_shower_energy_deposit() {
@@ -35,7 +34,6 @@ void CaloTruthEval::next_event(PHCompositeNode* topNode) {
3534
_cache_all_truth_hits_g4particle.clear();
3635
_cache_get_primary_particle_g4particle.clear();
3736
_cache_get_primary_particle_g4hit.clear();
38-
_cache_is_primary.clear();
3937
_cache_get_shower_from_primary.clear();
4038
_cache_get_shower_moliere_radius.clear();
4139
_cache_get_shower_energy_deposit.clear();
@@ -72,7 +70,6 @@ std::set<PHG4Hit*> CaloTruthEval::all_truth_hits(PHG4Particle* particle) {
7270

7371
PHG4Particle* CaloTruthEval::get_parent_particle(PHG4Hit* g4hit) {
7472

75-
// expensive call, but only because it gets called a ton (i think)
7673
PHG4Particle* particle = _truthinfo->GetHit( g4hit->get_trkid() );
7774
return particle;
7875
}
@@ -86,11 +83,14 @@ PHG4Particle* CaloTruthEval::get_primary_particle(PHG4Particle* particle) {
8683
return iter->second;
8784
}
8885
}
89-
90-
PHG4Particle* returnval = particle;
91-
if ((returnval->get_primary_id() != returnval->get_track_id()) ||
92-
(returnval->get_primary_id() != -1)) {
93-
returnval = _truthinfo->GetHit( particle->get_primary_id() );
86+
87+
// always report the primary from the Primary Map regardless if a
88+
// primary from the full Map was the argument
89+
PHG4Particle* returnval = NULL;
90+
if (particle->get_primary_id() != -1) {
91+
returnval = _truthinfo->GetPrimaryHit( particle->get_primary_id() );
92+
} else {
93+
returnval = _truthinfo->GetPrimaryHit( particle->get_track_id() );
9494
}
9595

9696
if (_do_cache) _cache_get_primary_particle_g4particle.insert(make_pair(particle,returnval));
@@ -133,23 +133,13 @@ PHG4VtxPoint* CaloTruthEval::get_vertex(PHG4Particle* particle) {
133133

134134
bool CaloTruthEval::is_primary(PHG4Particle* particle) {
135135

136-
if (_do_cache) {
137-
std::map<PHG4Particle*,bool>::iterator iter =
138-
_cache_is_primary.find(particle);
139-
if (iter != _cache_is_primary.end()) {
140-
return iter->second;
141-
}
142-
}
143-
144-
bool is_primary = false;
145-
if (particle->get_primary_id() == particle->get_track_id()) {
146-
is_primary = true;
147-
} else if (particle->get_primary_id() == -1) {
148-
is_primary = true;
136+
bool is_primary = true;
137+
if (!_truthinfo->GetPrimaryHit(particle->get_track_id())) {
138+
// does the particle id appear in the primary map?
139+
// this way either copy (in Map or Primary Map) will report correctly
140+
is_primary = false;
149141
}
150142

151-
if (_do_cache) _cache_is_primary.insert(make_pair(particle,is_primary));
152-
153143
return is_primary;
154144
}
155145

simulation/g4simulation/g4eval/CaloTruthEval.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ class CaloTruthEval {
4848
std::map<PHG4Particle*,std::set<PHG4Hit*> > _cache_all_truth_hits_g4particle;
4949
std::map<PHG4Particle*,PHG4Particle*> _cache_get_primary_particle_g4particle;
5050
std::map<PHG4Hit*,PHG4Particle*> _cache_get_primary_particle_g4hit;
51-
std::map<PHG4Particle*,bool> _cache_is_primary;
5251
std::map<PHG4Particle*,std::set<PHG4Hit*> > _cache_get_shower_from_primary;
5352
std::map<PHG4Particle*,float> _cache_get_shower_moliere_radius;
5453
std::map<PHG4Particle*,float> _cache_get_shower_energy_deposit;

0 commit comments

Comments
 (0)