ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PHTpcEventExporter.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PHTpcEventExporter.cc
1 
6 #include "PHTpcEventExporter.h"
7 
8 #include "Track.h" // for Track
9 
10 #include "externals/kdfinder.hpp" // for get_track_color, TrackCa...
11 
12 #include <trackbase/TrkrCluster.h>
14 #include <trackbase/TrkrHitSet.h>
16 
17 #include <phool/PHLog.h>
18 
19 #include <log4cpp/CategoryStream.hh> // for CategoryStream
20 
21 #include <TVector3.h> // for TVector3
22 
23 #include <cstddef> // for size_t
24 #include <map> // for _Rb_tree_const_iterator
25 #include <ostream> // for operator<<, basic_ostream
26 #include <utility> // for pair
27 
29 {
30 }
31 
33  double B, const std::string& filename)
34 {
35  // export hits + seeds to json for checks
36  LOG_DEBUG("tracking.PHTpcEventExporter.exportEvent") << "exporting event, hits + seeds => file " << filename;
37 
38  std::stringstream ofs;
39 
40  ofs << "{ \n"
41  << " \"EVENT\": { \n"
42  << " \"runid\": " << 0 << ", \n"
43  << " \"evtid\": " << 0 << ", \n"
44  << " \"time\": " << 0 << ", \n"
45  << " \"B\": " << B << " \n"
46  << " }, \n";
47 
48  ofs << "\"META\": { \n"
49  << " \"HITS\": { \n"
50  << " \"TPC\": { \n"
51  << " \"type\": \"3D\", \n"
52  << " \"options\": { \n"
53  << " \"size\": 2, \n"
54  << " \"color\": 16777215 \n"
55  << " }\n"
56  << " }\n"
57  << " },\n"
58  << " \"TRACKS\": { \n"
59  << " \"TPC\": { \n"
60  << " \"r_min\": 0,\n"
61  << " \"r_max\": 800,\n"
62  << " \"size\": 2, \n"
63  << " \"thickness\": 2 \n"
64  << " }\n"
65  << " }\n"
66  << "},\n"
67  << " \"TRACKS\": {\n"
68  << " \"TPC\": [\n";
69 
70  for (int i = 0, ilen = candidates.size(); i < ilen; i++)
71  {
72  std::vector<double> hit = candidates[i]->getPosForHit(0);
73  std::vector<double> mom = candidates[i]->getMomForHit(0);
74  size_t nhits = candidates[i]->nhits();
75  double pt = candidates[i]->Pt();
76  double sign = candidates[i]->sign();
77  double length = candidates[i]->approxLength();
78  size_t color = kdfinder::get_track_color<double>(pt);
79  ofs << "{ \"color\": " << color << ", \"pt\": " << pt << ", \"xyz\":[" << hit[0] << "," << hit[1] << "," << hit[2]
80  << "], \"pxyz\":[" << mom[0] << "," << mom[1] << "," << mom[2]
81  << "],\"l\":" << length << ",\"nh\":" << nhits << ",\"q\":" << sign << "}";
82  if (i != (ilen - 1))
83  {
84  ofs << ",";
85  }
86  else
87  {
88  ofs << "\n";
89  }
90  }
91 
92  ofs << " ]\n"
93  << " },\n"
94  << " \"HITS\": {\n"
95  << " \"TPC\": [\n";
96 
97  auto hitsetrange = hitsets->getHitSets(TrkrDefs::TrkrId::tpcId);
98  for (auto hitsetitr = hitsetrange.first;
99  hitsetitr != hitsetrange.second;
100  ++hitsetitr){
101  std::string separator = "";
102  auto range = cluster_map->getClusters(hitsetitr->first);
103  for( auto it = range.first; it != range.second; ++it )
104  {
105  TrkrCluster* cluster = it->second;
106  if ((std::pow((double) cluster->getPosition(0), 2) +
107  std::pow((double) cluster->getPosition(1), 2) +
108  std::pow((double) cluster->getPosition(2), 2)) > (25.0 * 25.0))
109  {
110  ofs << "[ " << cluster->getPosition(0) << "," << cluster->getPosition(1) << "," << cluster->getPosition(2) << " ]";
111  separator = ",";
112  }
113  else
114  {
115  separator = "";
116  }
117  ++it;
118  if (it != range.second)
119  {
120  ofs << separator;
121  }
122  else
123  {
124  ofs << "\n";
125  break;
126  }
127  }
128  }
129 
130  ofs << " ]\n"
131  << " }\n"
132  << "}\n";
133 
134  std::ofstream ofile;
135  ofile.open(filename);
136  ofile << ofs.str();
137  ofile.close();
138 }
139 
140 void PHTpcEventExporter::exportEvent(TrkrClusterContainer* cluster_map, TrkrHitSetContainer* hitsets, std::vector<PHGenFit2::Track*> gtracks,
141  double B, const std::string& filename)
142 {
143  // export hits + reco-d tracks to json for checks
144  LOG_DEBUG("tracking.PHTpcEventExporter.exportEvent") << "exporting event, hits + GenFit tracks => file " << filename;
145 
146  std::stringstream ofs;
147 
148  ofs << "{ \n"
149  << " \"EVENT\": { \n"
150  << " \"runid\": " << 0 << ", \n"
151  << " \"evtid\": " << 0 << ", \n"
152  << " \"time\": " << 0 << ", \n"
153  << " \"B\": " << B << " \n"
154  << " }, \n";
155 
156  ofs << "\"META\": { \n"
157  << " \"HITS\": { \n"
158  << " \"TPC\": { \n"
159  << " \"type\": \"3D\", \n"
160  << " \"options\": { \n"
161  << " \"size\": 2, \n"
162  << " \"color\": 16777215 \n"
163  << " }\n"
164  << " }\n"
165  << " },\n"
166  << " \"TRACKS\": { \n"
167  << " \"TPC\": { \n"
168  << " \"r_min\": 0,\n"
169  << " \"r_max\": 800,\n"
170  << " \"size\": 2, \n"
171  << " \"thickness\": 2 \n"
172  << " }\n"
173  << " }\n"
174  << "},\n"
175  << " \"TRACKS\": {\n"
176  << " \"TPC\": [\n";
177 
178  TVector3 pos, mom;
179  double charge, length, pt;
180  int nhits;
181  size_t color;
182  for (int i = 0, ilen = gtracks.size(); i < ilen; i++)
183  {
184  bool rc = true;
185  try
186  {
187  rc = gtracks[i]->get_track_info(pos, mom, charge, nhits, length);
188  }
189  catch (...)
190  {
191  // GenFit tracklength exception?
192  continue;
193  }
194  if (!rc)
195  {
196  LOG_WARN("tracking.PHTpcEventExporter.exportEvent") << "got track with broken track info, id: " << i;
197  continue;
198  }
199  pt = mom.Perp();
200  color = kdfinder::get_track_color<double>(pt);
201  ofs << "{ \"color\": " << color << ", \"pt\": " << pt << ", \"xyz\":[" << pos.X() << "," << pos.Y() << "," << pos.Z()
202  << "], \"pxyz\":[" << mom.X() << "," << mom.Y() << "," << mom.Z() << "],\"l\":"
203  << length << ",\"nh\":" << nhits << ",\"q\":" << (charge < 0 ? -1 : 1) << "}";
204  if (i != (ilen - 1))
205  {
206  ofs << ",";
207  }
208  else
209  {
210  ofs << "\n";
211  }
212  }
213 
214  ofs << " ]\n"
215  << " },\n"
216  << " \"HITS\": {\n"
217  << " \"TPC\": [\n";
218  auto hitsetrange = hitsets->getHitSets(TrkrDefs::TrkrId::tpcId);
219  for (auto hitsetitr = hitsetrange.first;
220  hitsetitr != hitsetrange.second;
221  ++hitsetitr){
222  std::string separator = "";
223  auto range = cluster_map->getClusters(hitsetitr->first);
224  for( auto it = range.first; it != range.second; ++it )
225  {
226  TrkrCluster* cluster = it->second;
227  if ((std::pow((double) cluster->getPosition(0), 2) +
228  std::pow((double) cluster->getPosition(1), 2) +
229  std::pow((double) cluster->getPosition(2), 2)) > (25.0 * 25.0))
230  {
231  ofs << "[ " << cluster->getPosition(0) << "," << cluster->getPosition(1) << "," << cluster->getPosition(2) << " ]";
232  separator = ",";
233  }
234  else
235  {
236  separator = "";
237  }
238  ++it;
239  if (it != range.second)
240  {
241  ofs << separator;
242  }
243  else
244  {
245  ofs << "\n";
246  break;
247  }
248  }
249  }
250 
251  ofs << " ]\n"
252  << " }\n"
253  << "}\n";
254 
255  std::ofstream ofile;
256  ofile.open(filename);
257  ofile << ofs.str();
258  ofile.close();
259 }
260 
261 void PHTpcEventExporter::exportEvent(std::vector<PHGenFit2::Track*> gtracks,
262  double B, const std::string& filename)
263 {
264  // export final reco-d tracks to json purely for live display purposes (no hits = much smaller file size)
265  LOG_DEBUG("tracking.PHTpcEventExporter.exportEvent") << "exporting event, just GenFit tracks => file " << filename;
266 
267  std::stringstream ofs;
268 
269  ofs << "{ \n"
270  << " \"EVENT\": { \n"
271  << " \"runid\": " << 0 << ", \n"
272  << " \"evtid\": " << 0 << ", \n"
273  << " \"time\": " << 0 << ", \n"
274  << " \"B\": " << B << " \n"
275  << " }, \n";
276 
277  ofs << "\"META\": { \n"
278  << " \"HITS\": { \n"
279  << " \"TPC\": { \n"
280  << " \"type\": \"3D\", \n"
281  << " \"options\": { \n"
282  << " \"size\": 2, \n"
283  << " \"color\": 16777215 \n"
284  << " }\n"
285  << " }\n"
286  << " },\n"
287  << " \"TRACKS\": { \n"
288  << " \"TPC\": { \n"
289  << " \"r_min\": 0,\n"
290  << " \"r_max\": 800,\n"
291  << " \"size\": 2, \n"
292  << " \"thickness\": 2 \n"
293  << " }\n"
294  << " }\n"
295  << "},\n"
296  << " \"TRACKS\": {\n"
297  << " \"TPC\": [\n";
298 
299  TVector3 pos, mom;
300  double charge, length, pt;
301  int nhits;
302  size_t color;
303  for (int i = 0, ilen = gtracks.size(); i < ilen; i++)
304  {
305  gtracks[i]->get_track_info(pos, mom, charge, nhits, length);
306  pt = mom.Perp();
307  color = kdfinder::get_track_color<double>(pt);
308  ofs << "{ \"color\": " << color << ", \"pt\": " << pt << ", \"xyz\":[" << pos.X() << "," << pos.Y() << "," << pos.Z()
309  << "], \"pxyz\":[" << mom.X() << "," << mom.Y() << "," << mom.Z() << "],\"l\":"
310  << length << ",\"nh\":" << nhits << ",\"q\":" << (charge < 0 ? -1 : 1) << "}";
311  if (i != (ilen - 1))
312  {
313  ofs << ",";
314  }
315  else
316  {
317  ofs << "\n";
318  }
319  }
320 
321  ofs << " ]\n"
322  << " }\n"
323  << "}\n";
324 
325  std::ofstream ofile;
326  ofile.open(filename);
327  ofile << ofs.str();
328  ofile.close();
329 }