Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RecoInfoExport.C
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RecoInfoExport.C
1 #include "RecoInfoExport.h"
2 
3 #include <phool/getClass.h>
7 #include <phool/getClass.h>
8 
10 #include <g4main/PHG4Particle.h>
11 #include <g4main/PHG4VtxPoint.h>
12 #include <g4main/PHG4Hit.h>
13 
14 #include <g4hough/SvtxVertexMap.h>
15 #include <g4hough/SvtxVertex.h>
16 #include <g4hough/SvtxTrackMap.h>
17 #include <g4hough/SvtxTrack.h>
18 
19 #include <g4eval/SvtxEvalStack.h>
20 #include <g4eval/SvtxTrackEval.h>
21 #include <g4eval/SvtxVertexEval.h>
22 #include <g4eval/SvtxTruthEval.h>
23 
24 #include <calobase/RawTowerContainer.h>
25 #include <calobase/RawTowerGeomContainer.h>
26 #include <calobase/RawTower.h>
27 #include <calobase/RawClusterContainer.h>
28 #include <calobase/RawCluster.h>
29 
30 #include <TH1D.h>
31 #include <TH2D.h>
32 #include <TDatabasePDG.h>
33 #include <TVector3.h>
34 
35 #include <iostream>
36 #include <cassert>
37 #include <fstream>
38 #include <sstream>
39 #include <boost/format.hpp>
40 #include <boost/math/special_functions/sign.hpp>
41 
42 using namespace std;
43 
45  SubsysReco(name), _event(0), _calo_names(
46  { "CEMC", "HCALIN", "HCALOUT" }), _tower_threshold(0), _pT_threshold(0), _min_track_hit_dist(
47  0)
48 {
49 }
50 
51 int
53 {
54 
55  return 0;
56 }
57 
58 int
60 {
61  ++_event;
62 
63  stringstream fname;
64  fname << _file_prefix << "_Event" << _event << ".dat";
65  fstream fdata(fname.str(), ios_base::out);
66 
67  for (auto & calo_name : _calo_names)
68  {
69  string towernodename = "TOWER_CALIB_" + calo_name;
70  // Grab the towers
71  RawTowerContainer* towers = findNode::getClass<RawTowerContainer>(topNode,
72  towernodename.c_str());
73  if (!towers)
74  {
75  std::cout << PHWHERE << ": Could not find node "
76  << towernodename.c_str() << std::endl;
78  }
79  string towergeomnodename = "TOWERGEOM_" + calo_name;
81  RawTowerGeomContainer>(topNode, towergeomnodename.c_str());
82  if (!towergeom)
83  {
84  cout << PHWHERE << ": Could not find node "
85  << towergeomnodename.c_str() << endl;
87  }
88 
89  set<const RawTower *> good_towers;
90 
91  RawTowerContainer::ConstRange begin_end = towers->getTowers();
93  for (rtiter = begin_end.first; rtiter != begin_end.second; ++rtiter)
94  {
95  const RawTower *tower = rtiter->second;
96  assert(tower);
97  if (tower->get_energy() > _tower_threshold)
98  {
99  good_towers.insert(tower);
100  }
101  }
102 
103  fdata
104  << (boost::format("%1% (1..%2% hits)") % calo_name
105  % good_towers.size()) << endl;
106 
107  bool first = true;
108  for (const auto & tower : good_towers)
109  {
110  assert(tower);
111 
112  float eta = towergeom->get_etacenter(tower->get_bineta());
113  float phi = towergeom->get_phicenter(tower->get_binphi());
114 
115  phi = atan2(cos(phi), sin(phi));
116 
117  if (first)
118  {
119  first = false;
120  }
121  else
122  fdata << ",";
123 
124  fdata
125  << (boost::format("[%1%,%2%,%3%]") % eta % phi
126  % tower->get_energy());
127 
128  }
129 
130  fdata << endl;
131  }
132 
133  {
134  fdata << "Track list" << endl;
135 
136  // need things off of the DST...
138  PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
139  if (!truthinfo)
140  {
141  cerr << PHWHERE << " ERROR: Can't find G4TruthInfo" << endl;
142  exit(-1);
143  }
144 
145  // create SVTX eval stack
146  SvtxEvalStack svtxevalstack(topNode);
147 
148 // SvtxVertexEval* vertexeval = svtxevalstack.get_vertex_eval();
149 // SvtxTrackEval* trackeval = svtxevalstack.get_track_eval();
150  SvtxTruthEval* trutheval = svtxevalstack.get_truth_eval();
151 
152  // loop over all truth particles
154  truthinfo->GetPrimaryParticleRange();
155  for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
156  iter != range.second; ++iter)
157  {
158  PHG4Particle* g4particle = iter->second;
159 
160  const TVector3 mom(g4particle->get_px(), g4particle->get_py(),
161  g4particle->get_pz());
162 
163  std::set<PHG4Hit*> g4hits = trutheval->all_truth_hits(g4particle);
164 
165  map<float, PHG4Hit *> time_sort;
166  map<float, PHG4Hit *> layer_sort;
167  for (auto & hit : g4hits)
168  {
169  if (hit)
170  {
171  time_sort[hit->get_avg_t()] = hit;
172  }
173  }
174 
175  for (auto & hit_pair : time_sort)
176  {
177 
178  if (hit_pair.second->get_layer() != UINT_MAX
179  and layer_sort.find(hit_pair.second->get_layer())
180  == layer_sort.end())
181  {
182  layer_sort[hit_pair.second->get_layer()] = hit_pair.second;
183  }
184  }
185 
186  if (layer_sort.size() > 5 and mom.Pt() > _pT_threshold) // minimal track length cut
187  {
188 
189  stringstream spts;
190 
191  TVector3 last_pos(0, 0, 0);
192 
193  bool first = true;
194  for (auto & hit_pair : layer_sort)
195  {
196  TVector3 pos(hit_pair.second->get_avg_x(),
197  hit_pair.second->get_avg_y(),
198  hit_pair.second->get_avg_z());
199 
200  // hit step cuts
201  if ((pos - last_pos).Mag() < _min_track_hit_dist
202  and hit_pair.first != (layer_sort.rbegin()->first)
203  and hit_pair.first != (layer_sort.begin()->first))
204  continue;
205 
206  last_pos = pos;
207 
208  if (first)
209  {
210  first = false;
211  }
212  else
213  spts << ",";
214 
215  spts << "[";
216  spts << pos.x();
217  spts << ",";
218  spts << pos.y();
219  spts << ",";
220  spts << pos.z();
221  spts << "]";
222  }
223 
224  const int abs_pid = abs(g4particle->get_pid());
225  int t = 5;
226  if (abs_pid
227  == TDatabasePDG::Instance()->GetParticle("pi+")->PdgCode())
228  {
229  t = 1;
230  }
231  else if (abs_pid
232  == TDatabasePDG::Instance()->GetParticle("proton")->PdgCode())
233  {
234  t = 2;
235  }
236  else if (abs_pid
237  == TDatabasePDG::Instance()->GetParticle("K+")->PdgCode())
238  {
239  t = 3;
240  }
241  else if (abs_pid
242  == TDatabasePDG::Instance()->GetParticle("e-")->PdgCode())
243  {
244  t = 3;
245  }
246 
247  const TParticlePDG * pdg_part =
248  TDatabasePDG::Instance()->GetParticle(11);
249  const int c =
250  (pdg_part != nullptr) ? (copysign(1, pdg_part->Charge())) : 0;
251 
252  fdata
253  << (boost::format(
254  "{ \"pt\": %1%, \"t\": %2%, \"e\": %3%, \"p\": %4%, \"c\": %5%, \"pts\":[ %6% ]}")
255  % mom.Pt() % t % mom.PseudoRapidity() % mom.Phi() % c
256  % spts.str()) << endl;
257 
258  }
259  }
260  }
261 
262  fdata.close();
263  return 0;
264 }
265 
266 int
268 {
269  return 0;
270 }