Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHG4DstCompressReco.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHG4DstCompressReco.cc
1 #include "PHG4DstCompressReco.h"
2 
3 #include <g4main/PHG4Hit.h>
5 #include <g4main/PHG4Particle.h>
6 #include <g4main/PHG4Shower.h>
8 
10 
11 #include <calobase/RawTower.h>
12 #include <calobase/RawTowerContainer.h>
13 
16 
18 #include <fun4all/SubsysReco.h>
19 
20 #include <phool/PHCompositeNode.h>
21 #include <phool/PHIODataNode.h>
22 #include <phool/PHNode.h>
23 #include <phool/PHNodeIterator.h>
25 #include <phool/getClass.h>
26 
27 #include <iostream>
28 #include <utility>
29 
31  : SubsysReco(name)
32 {
33 }
34 
36 {
37  _truth_info = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
38  if (!_truth_info)
39  {
40  std::cout << "PHG4DstCompressReco::InitRun(): Can't find G4TruthInfo" << std::endl;
42  }
43 
44  SearchG4HitNodes(topNode);
45 
46  for (const auto& name : _compress_g4cell_names)
47  {
48  PHG4CellContainer* g4cells = findNode::getClass<PHG4CellContainer>(topNode, name.c_str());
49  if (g4cells)
50  {
51  _g4cells.insert(g4cells);
52  }
53  }
54 
55  for (const auto& name : _compress_tower_names)
56  {
57  RawTowerContainer* towers = findNode::getClass<RawTowerContainer>(topNode, name.c_str());
58  if (towers)
59  {
60  _towers.insert(towers);
61  }
62  }
63 
65  {
66  _recoTruthMap = findNode::getClass<SvtxPHG4ParticleMap>(topNode, "SvtxPHG4ParticleMap");
67  if (_recoTruthMap == nullptr)
68  {
69  std::cout << __PRETTY_FUNCTION__ << " Fatal error: missing SvtxPHG4ParticleMap while m_keepRecoTrackMatchedParticles is set. "
70  << "Was PHG4DstCompressReco called before this module?"
71  << std::endl;
72  exit(1);
73  }
74 
75  _truthRecoMap = findNode::getClass<PHG4ParticleSvtxMap>(topNode, "PHG4ParticleSvtxMap");
76  if (_truthRecoMap == nullptr)
77  {
78  std::cout << __PRETTY_FUNCTION__ << " Fatal error: missing PHG4ParticleSvtxMap while m_keepRecoTrackMatchedParticles is set. "
79  << "Was PHG4DstCompressReco called before this module?"
80  << std::endl;
81  exit(1);
82  }
83  } // if (m_keepRecoTrackMatchedParticles)
84 
86 }
87 
89 {
90  if (_g4hits.empty() && _g4cells.empty() && _towers.empty())
91  {
93  }
94 
95  //---cells--------------------------------------------------------------------
96 
97  for (auto cells : _g4cells)
98  {
99  cells->Reset(); // DROP ALL COMPRESSED G4CELLS
100  }
101 
102  //---hits---------------------------------------------------------------------
103 
104  for (auto hits : _g4hits)
105  {
106  hits->Reset(); // DROP ALL COMPRESSED G4HITS
107  }
108 
109  //---secondary particles and vertexes-----------------------------------------
110 
111  std::set<int> keep_particle_ids;
112  for (auto hits : _keep_g4hits)
113  {
114  for (PHG4HitContainer::ConstIterator jter = hits->getHits().first;
115  jter != hits->getHits().second;
116  ++jter)
117  {
118  PHG4Hit* hit = jter->second;
119  keep_particle_ids.insert(hit->get_trkid());
120  // this will need to include all parents too in a trace back to
121  // the primary, but let's start here for now
122  }
123  }
124 
125  //---tracker truth map, if set-----------------------------------------
126  if (_truthRecoMap)
127  {
128  for (const auto& [particle_id, map] : *_truthRecoMap)
129  {
130  keep_particle_ids.insert(particle_id);
131  }
132  }
133  if (_recoTruthMap)
134  {
135  for (const auto& [track_id, weighted_truth_track_map] : *_recoTruthMap)
136  {
137  for (const auto& [weight, particle_set] : weighted_truth_track_map)
138  {
139  if (weight > 0)
140  {
141  for (const auto& particle_id : particle_set)
142  {
143  keep_particle_ids.insert(particle_id);
144  }
145  }
146  }
147  } // for (const auto& [track_id, weighted_truth_track_map] : *_recoTruthMap)
148 
149  } // if (_recoTruthMap)
150 
151  std::set<int> keep_vertex_ids;
153  for (PHG4TruthInfoContainer::Iterator iter = range.first;
154  iter != range.second;)
155  {
156  int id = iter->first;
157  PHG4Particle* particle = iter->second;
158 
159  if (keep_particle_ids.find(id) != keep_particle_ids.end())
160  {
161  ++iter;
162  keep_vertex_ids.insert(particle->get_vtx_id());
163  continue;
164  }
165  else
166  {
167  _truth_info->delete_particle(iter++); // DROP PARTICLES NOT ASSOCIATED TO A PRESERVED HIT
168  }
169  }
170 
172  for (PHG4TruthInfoContainer::VtxIterator iter = vrange.first;
173  iter != vrange.second;)
174  {
175  int id = iter->first;
176 
177  if (keep_vertex_ids.find(id) != keep_vertex_ids.end())
178  {
179  ++iter;
180  continue;
181  }
182  else
183  {
184  _truth_info->delete_vtx(iter++); // DROP VERTEXES NOT ASSOCIATED TO A PRESERVED HIT
185  }
186  }
187 
188  //---shower entries-----------------------------------------------------------
189 
191  for (PHG4TruthInfoContainer::ShowerIterator iter = srange.first;
192  iter != srange.second;
193  ++iter)
194  {
195  PHG4Shower* shower = iter->second;
196 
197  shower->clear_g4particle_id();
198  shower->clear_g4vertex_id();
199  shower->clear_g4hit_id();
200  }
201 
202  //---tower cell entries-------------------------------------------------------
203  for (auto towers : _towers)
204  {
205  // loop over all the towers
206  for (RawTowerContainer::Iterator jter = towers->getTowers().first;
207  jter != towers->getTowers().second;
208  ++jter)
209  {
210  RawTower* tower = jter->second;
211  tower->clear_g4cells();
212  }
213  }
214 
216 }
217 
219 {
220  // fill a lookup map between the g4hit container ids and the containers
221  // themselves
222  // without knowing what the container names are in advance, only that they
223  // begin G4HIT_*
224 
225  // separate the names into those in the compression list and those not in the
226  // compression list
227 
228  PHNodeIterator nodeiter(top);
229  PHPointerListIterator<PHNode> iter(nodeiter.ls());
230  PHNode* thisNode;
231  while ((thisNode = iter()))
232  {
233  if (thisNode->getType() == "PHCompositeNode")
234  {
235  SearchG4HitNodes(static_cast<PHCompositeNode*>(thisNode));
236  }
237  else if (thisNode->getType() == "PHIODataNode")
238  {
239  if (thisNode->getName().find("G4HIT_") == 0)
240  {
242  static_cast<PHIODataNode<PHG4HitContainer>*>(thisNode);
243  if (DNode)
244  {
245  PHG4HitContainer* object =
246  dynamic_cast<PHG4HitContainer*>(DNode->getData());
247  if (object)
248  {
249  if (_compress_g4hit_names.find(thisNode->getName()) !=
250  _compress_g4hit_names.end())
251  {
252  _g4hits.insert(object);
253  }
254  else
255  {
256  _keep_g4hits.insert(object);
257  }
258  }
259  }
260  }
261  }
262  }
263 }