Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FillTruthRecoMatchTree.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FillTruthRecoMatchTree.cc
2 
3 #include <TFile.h>
4 #include <TH2D.h>
5 #include <TTree.h>
13 #include <phool/PHCompositeNode.h>
14 #include <phool/PHDataNode.h>
15 #include <phool/PHIODataNode.h>
16 #include <phool/PHNode.h>
17 #include <phool/PHNodeIterator.h>
18 #include <phool/PHObject.h> // for PHObject
19 #include <phool/getClass.h>
20 #include <phool/phool.h> // for PHWHERE
21 #include <trackbase/TrkrCluster.h>
22 #include <trackbase/TrkrDefs.h>
26 #include <iostream>
27 
28 using std::cout;
29 using std::endl;
30 //____________________________________________________________________________..
32  bool _fill_clusters, bool _fill_SvUnMatched, float _cluster_nzwidths, float _cluster_nphiwidths, const std::string& _outfile_name)
33  : m_cluster_comp{_cluster_nphiwidths, _cluster_nzwidths}
34  , m_fill_clusters{_fill_clusters}
35  , m_fill_SvU{_fill_SvUnMatched}
36  , m_outfile_name{_outfile_name}
37 {
38  m_cluscntr.set_comparer(&m_cluster_comp);
39  PHTFileServer::get().open(m_outfile_name, "RECREATE");
40 
41  h2_G4_nPixelsPhi = new TH2D("G4_nPixelsPhi", "PHG4 Emb Tracks; cluster pixel width Phi; layer",
42  100, -0.5, 99.5, 56, -0.5, 55.5);
43  h2_G4_nPixelsZ = new TH2D("G4_nPixelsZ", "PHG4 Emb Tracks; cluster pixel width Z; layer",
44  100, -0.5, 99.5, 56, -0.5, 55.5);
45  h2_Sv_nPixelsPhi = new TH2D("Sv_nPixelsPhi", "Svtx Reco Tracks; cluster pixel width Phi; layer",
46  100, -0.5, 99.5, 56, -0.5, 55.5);
47  h2_Sv_nPixelsZ = new TH2D("Sv_nPixelsZ", "Svtx Reco Tracks; cluster pixel width Z; layer",
48  100, -0.5, 99.5, 56, -0.5, 55.5);
49 
50  m_ttree = new TTree("T", "Tracks (and sometimes clusters)");
51 
52  m_ttree->Branch("event", &nevent);
53  m_ttree->Branch("nphg4_part", &nphg4_part);
54  m_ttree->Branch("centrality", &centrality);
55  m_ttree->Branch("ntrackmatches", &ntrackmatches);
56  m_ttree->Branch("nphg4", &nphg4);
57  m_ttree->Branch("nsvtx", &nsvtx);
58 
59  m_ttree->Branch("trackid", &b_trackid);
60  m_ttree->Branch("is_G4track", &b_is_g4track);
61  m_ttree->Branch("is_Svtrack", &b_is_Svtrack);
62  m_ttree->Branch("is_matched", &b_is_matched);
63 
64  m_ttree->Branch("trkpt", &b_trkpt);
65  m_ttree->Branch("trketa", &b_trketa);
66  m_ttree->Branch("trkphi", &b_trkphi);
67 
68  m_ttree->Branch("nclus", &b_nclus);
69  m_ttree->Branch("nclustpc", &b_nclustpc);
70  m_ttree->Branch("nclusmvtx", &b_nclusmvtx);
71  m_ttree->Branch("nclusintt", &b_nclusintt);
72  m_ttree->Branch("matchrat", &b_matchrat);
73  m_ttree->Branch("matchrat_intt", &b_matchrat_intt);
74  m_ttree->Branch("matchrat_mvtx", &b_matchrat_mvtx);
75  m_ttree->Branch("matchrat_tpc", &b_matchrat_tpc);
76 
77  if (m_fill_clusters)
78  {
79  m_ttree->Branch("clus_match", &b_clusmatch);
80  m_ttree->Branch("clus_x", &b_clus_x);
81  m_ttree->Branch("clus_y", &b_clus_y);
82  m_ttree->Branch("clus_z", &b_clus_z);
83  m_ttree->Branch("clus_r", &b_clus_r);
84  m_ttree->Branch("clus_layer", &b_clus_layer);
85  m_ttree->Branch("nphibins", &b_clus_nphibins);
86  m_ttree->Branch("nzbins", &b_clus_ntbins);
87  }
88 }
89 
90 //____________________________________________________________________________..
92 
93 //____________________________________________________________________________..
95 {
96  if (Verbosity() > 1)
97  {
98  std::cout << " Beginning FillTruthRecoMatchTree " << std::endl;
99  topNode->print();
100  }
101 
103 }
104 
105 //____________________________________________________________________________..
107 {
108  auto init_status = m_cluster_comp.init(topNode);
109  if (init_status == Fun4AllReturnCodes::ABORTRUN)
110  {
111  return init_status;
112  }
113 
115  {
117  }
118 
120 }
121 
123 {
124  PHNodeIterator iter(topNode);
125 
126  PHCompositeNode* dstNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "DST"));
127 
128  if (!dstNode)
129  {
130  std::cout << PHWHERE << " DST node is missing, quitting" << std::endl;
131  std::cerr << PHWHERE << " DST node is missing, quitting" << std::endl;
132  throw std::runtime_error("Failed to find DST node in FillTruthRecoMatchTree::createNodes");
133  }
134 
135  m_EmbRecoMatchContainer = findNode::getClass<EmbRecoMatchContainer>(topNode, "TRKR_EMBRECOMATCHCONTAINER");
137  {
138  std::cout << PHWHERE << " Cannot find node TRKR_EMBRECOMATCHCONTAINER on node tree; quitting " << std::endl;
139  std::cerr << PHWHERE << " Cannot find node TRKR_EMBRECOMATCHCONTAINER on node tree; quitting " << std::endl;
140  throw std::runtime_error(" Cannot find node TRKR_EMBRECOMATCHCONTAINER on node tree; quitting");
141  }
142 
143  PHCompositeNode* svtxNode = dynamic_cast<PHCompositeNode*>(iter.findFirst("PHCompositeNode", "SVTX"));
144  if (!svtxNode)
145  {
146  svtxNode = new PHCompositeNode("SVTX");
147  dstNode->addNode(svtxNode);
148  }
149 
150  m_PHG4TruthInfoContainer = findNode::getClass<PHG4TruthInfoContainer>(topNode, "G4TruthInfo");
152  {
153  std::cout << "Could not locate G4TruthInfo node when running "
154  << "\"TruthRecoTrackMatching\" module." << std::endl;
156  }
157 
158  m_SvtxTrackMap = findNode::getClass<SvtxTrackMap>(topNode, "SvtxTrackMap");
159  if (!m_SvtxTrackMap)
160  {
161  std::cout << "Could not locate SvtxTrackMap node when running "
162  << "\"TruthRecoTrackMatching\" module." << std::endl;
164  }
165 
166  /* m_TruthClusterContainer = findNode::getClass<TrkrClusterContainer>(topNode, */
167  /* "TRKR_TRUTHCLUSTERCONTAINER"); */
168  /* if (!m_TruthClusterContainer) */
169  /* { */
170  /* std::cout << "Could not locate TRKR_TRUTHCLUSTERCONTAINER node when running " */
171  /* << "\"TruthRecoTrackMatching\" module." << std::endl; */
172  /* return Fun4AllReturnCodes::ABORTEVENT; */
173  /* } */
174 
175  /* m_RecoClusterContainer = findNode::getClass<TrkrClusterContainer>(topNode, "TRKR_CLUSTER"); */
176  /* if (!m_RecoClusterContainer) */
177  /* { */
178  /* std::cout << "Could not locate TRKR_CLUSTER node when running " */
179  /* << "\"TruthRecoTrackMatching\" module." << std::endl; */
180  /* return Fun4AllReturnCodes::ABORTEVENT; */
181  /* } */
182 
183  m_TrkrTruthTrackContainer = findNode::getClass<TrkrTruthTrackContainer>(topNode,
184  "TRKR_TRUTHTRACKCONTAINER");
186  {
187  std::cout << "Could not locate TRKR_TRUTHTRACKCONTAINER node when running "
188  << "\"TruthRecoTrackMatching\" module." << std::endl;
190  }
191 
193 }
194 
196 {
197  if (Verbosity() > 5)
198  {
199  cout << " FillTruthRecoMatchTree::process_event() " << endl;
200  }
201 
202  // fill in the event data
203  ++nevent;
207  // get centrality later...
208 
209  // fill in pixel widths on truth tracks
211  {
212  float layer = (float) TrkrDefs::getLayer(hitsetkey);
214  for (auto iter = range.first; iter != range.second; ++iter)
215  {
216  auto& cluster = iter->second;
217  h2_G4_nPixelsPhi->Fill((float) cluster->getPhiSize(), layer);
218  h2_G4_nPixelsZ->Fill((float) cluster->getZSize(), layer);
219  }
220  }
221  // fill in pixel widths on reco tracks
223  {
224  float layer = (float) TrkrDefs::getLayer(hitsetkey);
226  for (auto iter = range.first; iter != range.second; ++iter)
227  {
228  auto& cluster = iter->second;
229  h2_Sv_nPixelsPhi->Fill((float) cluster->getPhiSize(), layer);
230  h2_Sv_nPixelsZ->Fill((float) cluster->getZSize(), layer);
231  }
232  }
233 
234  nphg4_part = 0;
236  for (PHG4TruthInfoContainer::ConstIterator iter = range.first;
237  iter != range.second; ++iter)
238  {
239  nphg4_part++;
240  }
241 
242  // unmatched tracks are only entered once
243  // matches can repeat a given svtx or phg4 track, depending on the
244  // parameters in teh matching in filltruthrecomatchtree
245  //
246  // (1) fill unmatched phg4
247  // (2) fill unmatched svtx
248  // (3) fill matched phg4 and svtx
249  clear_clusvecs(" nothing ");
250 
251  if (Verbosity() > 2)
252  {
253  std::cout << " getting" << (int) m_EmbRecoMatchContainer->getMatches().size() << std::endl;
254  }
255  for (auto& match : m_EmbRecoMatchContainer->getMatches())
256  {
257  unsigned int g4_trkid = match->idTruthTrack();
258  int sv_trkid = match->idRecoTrack();
259 
260  auto g4trk = m_TrkrTruthTrackContainer->getTruthTrack(g4_trkid);
261  auto svtrk = m_SvtxTrackMap->get(sv_trkid);
262 
263  m_cluscntr.addClusKeys(g4trk);
264  m_cluscntr.addClusKeys(svtrk);
266 
267  // <- <- <- <- G4 Matched Tracks
268  b_is_matched = true;
269  b_is_g4track = true;
270  b_is_Svtrack = false;
271 
272  b_trackid = g4_trkid;
273  b_trkpt = g4trk->getPt();
274  b_trketa = g4trk->getPseudoRapidity();
275  b_trkphi = g4trk->getPhi();
276 
277  auto cnt = m_cluscntr.phg4_cntclus();
278  auto cnt_match = m_cluscntr.phg4_cnt_matchedclus();
279 
280  b_nclus = cnt[4];
281  b_nclusmvtx = cnt[0];
282  b_nclusintt = cnt[1];
283  b_nclustpc = cnt[2];
284 
285  b_matchrat = (float) cnt_match[4] / cnt[4];
286  b_matchrat_mvtx = (float) cnt_match[0] / cnt[0];
287  b_matchrat_intt = (float) cnt_match[1] / cnt[1];
288  b_matchrat_tpc = (float) cnt_match[2] / cnt[2];
289 
290  if (m_fill_clusters)
291  {
293  for (auto& loc : clusters)
294  {
295  b_clusmatch.push_back(false);
296  b_clus_layer.push_back(std::get<0>(loc));
297  auto x = std::get<1>(loc)[0];
298  auto y = std::get<1>(loc)[1];
299  b_clus_x.push_back(x);
300  b_clus_y.push_back(y);
301  b_clus_z.push_back(std::get<1>(loc)[2]);
302  b_clus_r.push_back(pow(x * x + y * y, 0.5));
303  b_clus_nphibins.push_back(std::get<2>(loc));
304  b_clus_ntbins.push_back(std::get<3>(loc));
305  }
306 
307  clusters = m_cluscntr.clusloc_matched();
308  for (auto& loc : clusters)
309  {
310  b_clusmatch.push_back(true);
311  b_clus_layer.push_back(std::get<0>(loc));
312  auto x = std::get<1>(loc)[0];
313  auto y = std::get<1>(loc)[1];
314  b_clus_x.push_back(x);
315  b_clus_y.push_back(y);
316  b_clus_z.push_back(std::get<1>(loc)[2]);
317  b_clus_r.push_back(pow(x * x + y * y, 0.5));
318  b_clus_nphibins.push_back(std::get<2>(loc));
319  b_clus_ntbins.push_back(std::get<3>(loc));
320  }
321  }
322  m_ttree->Fill();
323  clear_clusvecs();
324  /* clear_clusvecs("apple0 g4_matched"); */
325 
326  // <- <- <- <- Svtx Matched Tracks
327  b_is_g4track = false;
328  b_is_Svtrack = true;
329  b_trackid = sv_trkid;
330  b_trkpt = svtrk->get_pt();
331  b_trketa = svtrk->get_eta();
332  b_trkphi = svtrk->get_phi();
333 
334  cnt = m_cluscntr.svtx_cntclus();
335  b_nclus = cnt[4];
336  b_nclusmvtx = cnt[0];
337  b_nclusintt = cnt[1];
338  b_nclustpc = cnt[2];
339 
340  b_matchrat = (float) cnt_match[4] / cnt[4];
341  b_matchrat_mvtx = (float) cnt_match[0] / cnt[0];
342  b_matchrat_intt = (float) cnt_match[1] / cnt[1];
343  b_matchrat_tpc = (float) cnt_match[2] / cnt[2];
344 
345  /* int _ = 0; */
346  if (m_fill_clusters)
347  {
349  for (auto& loc : clusters)
350  {
351  b_clusmatch.push_back(false);
352  b_clus_layer.push_back(std::get<0>(loc));
353  auto x = std::get<1>(loc)[0];
354  auto y = std::get<1>(loc)[1];
355  /* if (_==0) cout << " apple x: " << x << " y: " << y << endl; */
356  /* _ += 1; */
357  b_clus_x.push_back(x);
358  b_clus_y.push_back(y);
359  b_clus_z.push_back(std::get<1>(loc)[2]);
360  b_clus_r.push_back(pow(x * x + y * y, 0.5));
361  b_clus_nphibins.push_back(std::get<2>(loc));
362  b_clus_ntbins.push_back(std::get<3>(loc));
363  }
364 
365  clusters = m_cluscntr.clusloc_matched();
366  for (auto& loc : clusters)
367  {
368  b_clusmatch.push_back(true);
369  b_clus_layer.push_back(std::get<0>(loc));
370  auto x = std::get<1>(loc)[0];
371  auto y = std::get<1>(loc)[1];
372  b_clus_x.push_back(x);
373  b_clus_y.push_back(y);
374  b_clus_z.push_back(std::get<1>(loc)[2]);
375  b_clus_r.push_back(pow(x * x + y * y, 0.5));
376  b_clus_nphibins.push_back(std::get<2>(loc));
377  b_clus_ntbins.push_back(std::get<3>(loc));
378  }
379  }
380  m_ttree->Fill();
381  clear_clusvecs();
382  /* clear_clusvecs("apple1 s4_matched"); */
383  }
384 
385  // <- <- <- <- G4 un-matched Tracks
386  b_is_matched = false;
387  b_is_g4track = true;
388  b_is_Svtrack = false;
389  for (auto& g4_trkid : m_EmbRecoMatchContainer->ids_TruthUnmatched())
390  {
391  auto g4trk = m_TrkrTruthTrackContainer->getTruthTrack(g4_trkid);
392  m_cluscntr.addClusKeys(g4trk);
393 
394  b_trackid = g4_trkid;
395  b_trkpt = g4trk->getPt();
396  b_trketa = g4trk->getPseudoRapidity();
397  b_trkphi = g4trk->getPhi();
398 
399  auto cnt = m_cluscntr.phg4_cntclus();
400  b_nclus = cnt[4];
401  b_nclusmvtx = cnt[0];
402  b_nclusintt = cnt[1];
403  b_nclustpc = cnt[2];
404 
405  if (m_fill_clusters)
406  {
408  for (auto& loc : clusters)
409  {
410  b_clusmatch.push_back(false);
411  b_clus_layer.push_back(std::get<0>(loc));
412  auto x = std::get<1>(loc)[0];
413  auto y = std::get<1>(loc)[1];
414  b_clus_x.push_back(x);
415  b_clus_y.push_back(y);
416  b_clus_z.push_back(std::get<1>(loc)[2]);
417  b_clus_r.push_back(pow(x * x + y * y, 0.5));
418  b_clus_nphibins.push_back(std::get<2>(loc));
419  b_clus_ntbins.push_back(std::get<3>(loc));
420  }
421  // this is an unmatched track, so there are no matched clusters
422  }
423  m_ttree->Fill();
424  clear_clusvecs();
425  /* clear_clusvecs("apple2 g4_unmatched"); */
426  }
427 
428  // <- <- <- <- Svtx un-matched Tracks
429  b_is_matched = false;
430  b_is_matched = false;
431  b_is_g4track = false;
432  b_is_Svtrack = true;
433 
434  // just put in all svtx tracks, period...
435  if (m_fill_SvU)
436  {
438  {
439  auto svtrk = m_SvtxTrackMap->get(sv_trkid);
440  m_cluscntr.addClusKeys(svtrk);
441  b_trackid = sv_trkid;
442  b_trkpt = svtrk->get_pt();
443  b_trketa = svtrk->get_eta();
444  b_trkphi = svtrk->get_phi();
445 
446  auto cnt = m_cluscntr.svtx_cntclus();
447  b_nclus = cnt[4];
448  b_nclusmvtx = cnt[0];
449  b_nclusintt = cnt[1];
450  b_nclustpc = cnt[2];
451 
452  if (m_fill_clusters)
453  {
455  for (auto& loc : clusters)
456  {
457  b_clusmatch.push_back(false);
458  b_clus_layer.push_back(std::get<0>(loc));
459  auto x = std::get<1>(loc)[0];
460  auto y = std::get<1>(loc)[1];
461  b_clus_x.push_back(x);
462  b_clus_y.push_back(y);
463  b_clus_z.push_back(std::get<1>(loc)[2]);
464  b_clus_r.push_back(pow(x * x + y * y, 0.5));
465  b_clus_nphibins.push_back(std::get<2>(loc));
466  b_clus_ntbins.push_back(std::get<3>(loc));
467  }
468  }
469  m_ttree->Fill();
470  clear_clusvecs();
471  }
472  }
473 
474  if (Verbosity() > 100)
475  {
477  }
479 }
480 
482 {
483  std::cout << "To do: "
484  << " (1) number of truth tracks and total number of mvtx and ratio " << std::endl
485  << " (2) ditto for reco tracks " << std::endl;
486 
487  double n_PHG4_tracks = m_TrkrTruthTrackContainer->getMap().size();
488  // count how many mvtx clusters in the phg4 tracks
489  double n_in_PHG4_tracks{0.};
490  for (auto& pair : m_TrkrTruthTrackContainer->getMap())
491  {
492  m_cluscntr.addClusKeys(pair.second);
493  n_in_PHG4_tracks += m_cluscntr.phg4_cntclus()[0];
494  }
495  // count how mant mvtx clusters in truth container (should be the same)
496  double n_in_PHG4_clusters{0.};
498  {
499  if (TrkrDefs::getLayer(hitsetkey) > 2)
500  {
501  continue;
502  }
504  for (auto r = range.first; r != range.second; ++r)
505  {
506  n_in_PHG4_clusters += 1.;
507  }
508  }
509 
510  // count how many svtx tracks
511  double n_SVTX_tracks = m_SvtxTrackMap->size();
512  // count how many mvtx clusters in svtx tracks
513  double n_in_SVTX_tracks{0.};
514  for (auto& entry : *m_SvtxTrackMap)
515  {
516  m_cluscntr.addClusKeys(entry.second);
517  n_in_SVTX_tracks += m_cluscntr.svtx_cntclus()[0];
518  }
519  // count how many mvtx are total in the container
520  double n_in_SVTX_clusters{0.};
522  {
523  if (TrkrDefs::getLayer(hitsetkey) > 2)
524  {
525  continue;
526  }
528  for (auto r = range.first; r != range.second; ++r)
529  {
530  n_in_SVTX_clusters += 1.;
531  }
532  }
533 
534  std::cout << Form(
535  "MVTX"
536  "\nPHG4: Tracks(%.0f) Clusters In tracks(%.0f) Total (%.0f)"
537  "\n ave. per track: %6.3f ratio in all tracks: %6.2f",
538  n_PHG4_tracks, n_in_PHG4_tracks, n_in_PHG4_clusters, (n_in_PHG4_tracks / n_PHG4_tracks), (n_in_PHG4_tracks / n_in_PHG4_clusters))
539  << std::endl;
540  std::cout << Form(
541  "\nSVTX: Tracks(%.0f) Clusters In tracks(%.0f) Total (%.0f)"
542  "\n ave. per track: %6.3f ratio in all tracks: %6.2f",
543  n_SVTX_tracks, n_in_SVTX_tracks, n_in_SVTX_clusters, (n_in_SVTX_tracks / n_SVTX_tracks), (n_in_SVTX_tracks / n_in_SVTX_clusters))
544  << std::endl;
545 }
546 
548 {
549  if (Verbosity() > 2)
550  {
551  std::cout << PHWHERE << ": ending FillTruthRecoMatchTree" << std::endl;
552  }
554 
555  h2_G4_nPixelsPhi->Write();
556  h2_G4_nPixelsZ->Write();
557  h2_Sv_nPixelsPhi->Write();
558  h2_Sv_nPixelsZ->Write();
559 
560  m_ttree->Write();
562 }
563 
565 {
566  /* cout << " banana |" << tag << "|"<<endl; */
567  if (tag != "")
568  {
569  cout << endl
570  << " pear printing " << tag << " x(" << b_clus_x.size() << ") ";
571  for (auto x : b_clus_x)
572  {
573  cout << x << " ";
574  }
575  cout << endl;
576  }
577  // Tracks and clustes
578  b_clusmatch.clear();
579  b_clus_x.clear();
580  b_clus_y.clear();
581  b_clus_z.clear();
582  b_clus_r.clear();
583  b_clus_layer.clear();
584  b_clus_nphibins.clear();
585  b_clus_ntbins.clear();
586 }