EIC 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 
15 #include <fun4all/SubsysReco.h>
16 
17 #include <phool/PHCompositeNode.h>
18 #include <phool/PHIODataNode.h>
19 #include <phool/PHNode.h>
20 #include <phool/PHNodeIterator.h>
22 #include <phool/getClass.h>
23 
24 #include <iostream>
25 #include <utility>
26 
27 using namespace std;
28 
30  : SubsysReco(name)
31  , _truth_info(nullptr)
32  , _compress_g4hit_names()
33  , _compress_g4cell_names()
34  , _g4cells()
35  , _g4hits()
36  , _keep_g4hits()
37 {
38 }
39 
41 {
42  _truth_info = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
43  if (!_truth_info)
44  {
45  cout << "PHG4DstCompressReco::InitRun(): Can't find G4TruthInfo" << endl;
47  }
48 
49  SearchG4HitNodes(topNode);
50 
51  for (std::set<std::string>::iterator iter = _compress_g4cell_names.begin();
52  iter != _compress_g4cell_names.end(); ++iter)
53  {
54  std::string name = *iter;
55 
56  PHG4CellContainer* g4cells = findNode::getClass<PHG4CellContainer>(topNode, name.c_str());
57  if (g4cells)
58  {
59  _g4cells.insert(g4cells);
60  }
61  }
62 
63  for (std::set<std::string>::iterator iter = _compress_tower_names.begin();
64  iter != _compress_tower_names.end(); ++iter)
65  {
66  std::string name = *iter;
67 
68  RawTowerContainer* towers = findNode::getClass<RawTowerContainer>(topNode, name.c_str());
69  if (towers)
70  {
71  _towers.insert(towers);
72  }
73  }
74 
76 }
77 
79 {
80  if (_g4hits.empty() && _g4cells.empty() && _towers.empty()) return Fun4AllReturnCodes::EVENT_OK;
81 
82  //---cells--------------------------------------------------------------------
83 
84  for (std::set<PHG4CellContainer*>::iterator iter = _g4cells.begin();
85  iter != _g4cells.end();
86  ++iter)
87  {
88  PHG4CellContainer* cells = *iter;
89  cells->Reset(); // DROP ALL COMPRESSED G4CELLS
90  }
91 
92  //---hits---------------------------------------------------------------------
93 
94  for (std::set<PHG4HitContainer*>::iterator iter = _g4hits.begin();
95  iter != _g4hits.end();
96  ++iter)
97  {
98  PHG4HitContainer* hits = *iter;
99  hits->Reset(); // DROP ALL COMPRESSED G4HITS
100  }
101 
102  //---secondary particles and vertexes-----------------------------------------
103 
104  std::set<int> keep_particle_ids;
105  for (std::set<PHG4HitContainer*>::iterator iter = _keep_g4hits.begin();
106  iter != _keep_g4hits.end();
107  ++iter)
108  {
109  PHG4HitContainer* hits = *iter;
110 
111  for (PHG4HitContainer::ConstIterator jter = hits->getHits().first;
112  jter != hits->getHits().second;
113  ++jter)
114  {
115  PHG4Hit* hit = jter->second;
116  keep_particle_ids.insert(hit->get_trkid());
117  // this will need to include all parents too in a trace back to
118  // the primary, but let's start here for now
119  }
120  }
121 
122  std::set<int> keep_vertex_ids;
124  for (PHG4TruthInfoContainer::Iterator iter = range.first;
125  iter != range.second;)
126  {
127  int id = iter->first;
128  PHG4Particle* particle = iter->second;
129 
130  if (keep_particle_ids.find(id) != keep_particle_ids.end())
131  {
132  ++iter;
133  keep_vertex_ids.insert(particle->get_vtx_id());
134  continue;
135  }
136  else
137  {
138  _truth_info->delete_particle(iter++); // DROP PARTICLES NOT ASSOCIATED TO A PRESERVED HIT
139  }
140  }
141 
143  for (PHG4TruthInfoContainer::VtxIterator iter = vrange.first;
144  iter != vrange.second;)
145  {
146  int id = iter->first;
147 
148  if (keep_vertex_ids.find(id) != keep_vertex_ids.end())
149  {
150  ++iter;
151  continue;
152  }
153  else
154  {
155  _truth_info->delete_vtx(iter++); // DROP VERTEXES NOT ASSOCIATED TO A PRESERVED HIT
156  }
157  }
158 
159  //---shower entries-----------------------------------------------------------
160 
162  for (PHG4TruthInfoContainer::ShowerIterator iter = srange.first;
163  iter != srange.second;
164  ++iter)
165  {
166  PHG4Shower* shower = iter->second;
167 
168  shower->clear_g4particle_id();
169  shower->clear_g4vertex_id();
170  shower->clear_g4hit_id();
171  }
172 
173  //---tower cell entries-------------------------------------------------------
174  for (std::set<RawTowerContainer*>::iterator iter = _towers.begin();
175  iter != _towers.end();
176  ++iter)
177  {
178  RawTowerContainer* towers = *iter;
179 
180  // loop over all the towers
181  for (RawTowerContainer::Iterator jter = towers->getTowers().first;
182  jter != towers->getTowers().second;
183  ++jter)
184  {
185  RawTower* tower = jter->second;
186  tower->clear_g4cells();
187  }
188  }
189 
191 }
192 
194 {
195  // fill a lookup map between the g4hit container ids and the containers
196  // themselves
197  // without knowing what the container names are in advance, only that they
198  // begin G4HIT_*
199 
200  // separate the names into those in the compression list and those not in the
201  // compression list
202 
203  PHNodeIterator nodeiter(top);
204  PHPointerListIterator<PHNode> iter(nodeiter.ls());
205  PHNode* thisNode;
206  while ((thisNode = iter()))
207  {
208  if (thisNode->getType() == "PHCompositeNode")
209  {
210  SearchG4HitNodes(static_cast<PHCompositeNode*>(thisNode));
211  }
212  else if (thisNode->getType() == "PHIODataNode")
213  {
214  if (thisNode->getName().find("G4HIT_") == 0)
215  {
217  static_cast<PHIODataNode<PHG4HitContainer>*>(thisNode);
218  if (DNode)
219  {
220  PHG4HitContainer* object =
221  dynamic_cast<PHG4HitContainer*>(DNode->getData());
222  if (object)
223  {
224  if (_compress_g4hit_names.find(thisNode->getName()) !=
225  _compress_g4hit_names.end())
226  {
227  _g4hits.insert(object);
228  }
229  else
230  {
231  _keep_g4hits.insert(object);
232  }
233  }
234  }
235  }
236  }
237  }
238 }