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