Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FillTruthRecoMatchMap.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FillTruthRecoMatchMap.cc
2 
5 
9 
11 
12 #include <phool/PHCompositeNode.h>
13 #include <phool/PHDataNode.h>
14 #include <phool/PHIODataNode.h>
15 #include <phool/PHNode.h>
16 #include <phool/PHNodeIterator.h>
17 #include <phool/PHObject.h> // for PHObject
18 #include <phool/getClass.h>
19 #include <phool/phool.h> // for PHWHERE
20 
21 #include <iostream>
22 
23 //____________________________________________________________________________..
25  : SubsysReco(name)
26 {
27 }
28 
29 //____________________________________________________________________________..
31 
32 //____________________________________________________________________________..
34 {
35  if (Verbosity() > 1)
36  {
37  topNode->print();
38  }
39 
41 }
42 
43 //____________________________________________________________________________..
45 {
47  {
49  }
50 
52 }
53 
55 {
56  PHNodeIterator iter(topNode);
57 
58  PHCompositeNode *dstNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "DST"));
59 
60  if (!dstNode)
61  {
62  std::cout << PHWHERE << " DST node is missing, quitting" << std::endl;
63  throw std::runtime_error("Failed to find DST node in FillTruthRecoMatchMap::createNodes");
64  }
65 
66  m_EmbRecoMatchContainer = findNode::getClass<EmbRecoMatchContainer>(topNode, "TRKR_EMBRECOMATCHCONTAINER");
68  {
69  std::cout << PHWHERE << " Cannot find node TRKR_EMBRECOMATCHCONTAINER on node tree; quitting " << std::endl;
70  throw std::runtime_error(" Cannot find node TRKR_EMBRECOMATCHCONTAINER on node tree; quitting");
71  }
72 
73  PHCompositeNode *svtxNode = dynamic_cast<PHCompositeNode *>(iter.findFirst("PHCompositeNode", "SVTX"));
74  if (!svtxNode)
75  {
76  svtxNode = new PHCompositeNode("SVTX");
77  dstNode->addNode(svtxNode);
78  }
79 
80  m_PHG4ParticleSvtxMap = findNode::getClass<PHG4ParticleSvtxMap>(topNode, "PHG4ParticleToRecoMap");
82  {
84  PHIODataNode<PHObject> *truthNode =
85  new PHIODataNode<PHObject>(m_PHG4ParticleSvtxMap, "PHG4ParticleToRecoMap", "PHObject");
86  svtxNode->addNode(truthNode);
87  }
88 
89  m_SvtxPHG4ParticleMap = findNode::getClass<SvtxPHG4ParticleMap>(topNode, "RecoToPHG4ParticleMap");
91  {
93  PHIODataNode<PHObject> *recoNode =
94  new PHIODataNode<PHObject>(m_SvtxPHG4ParticleMap, "RecoToPHG4ParticleMap", "PHObject");
95  svtxNode->addNode(recoNode);
96  }
97 
99 }
100 
102 {
103  if (Verbosity() > 5)
104  {
105  std::cout << " FillTruthRecoMatchMap::process_event() " << std::endl;
106  }
107  // make maps to all the matches for both truth and reco and fill the
108  /* auto matches = m_EmbRecoMatchContainer->getMatches(); */
109 
110  /* map<unsigned short, set<int>> truth_entries; */
111  /* map<unsigned short, set<int>> m_reco_entries; */
112  /* int cnt {0}; */
113  /* for (auto& match : m_EmbRecoMatchContainer->getMatches()) { */
114 
115  /* } */
116 
117  SvtxPHG4ParticleMap::Map map_RtoT{}; // RtoT = "Reco to Truth"
118  PHG4ParticleSvtxMap::Map map_TtoR{}; // TtoR = "Truth to Reco"
119 
120  for (auto &match : m_EmbRecoMatchContainer->getMatches())
121  {
122  // every match is a unique match from truth to embedded particles
123  const int gtrackID = match->idTruthTrack();
124  const unsigned int id_reco = match->idRecoTrack();
125  const unsigned short n_match = match->nClustersMatched();
126  const unsigned short n_truth = match->nClustersTruth();
127  const unsigned short n_reco = match->nClustersReco();
128 
129  // fill the map_TtoR
130  /* PHG4ParticleSvtxMap::WeightedRecoTrackMap* entry_TtoR; */
131  if (map_TtoR.find(gtrackID) == map_TtoR.end())
132  {
134  }
135  auto &entry_TtoR = map_TtoR[gtrackID];
136  float weight_TtoR = (float) n_match + (float) n_truth / 100.;
137  if (entry_TtoR.find(weight_TtoR) == entry_TtoR.end())
138  {
139  entry_TtoR[weight_TtoR] = {id_reco}; // i.e. std::set<unsigned int> { id_reco };
140  }
141  else
142  {
143  entry_TtoR[weight_TtoR].insert(id_reco);
144  }
145 
146  // fill the map_RtoT
147  /* SvtxPHG4ParticleMap::WeightedTruthTrackMap* entry_RtoT; */
148  if (map_RtoT.find(id_reco) == map_RtoT.end())
149  {
150  map_RtoT[id_reco] = SvtxPHG4ParticleMap::WeightedTruthTrackMap{};
151  }
152  auto &entry_RtoT = map_RtoT[id_reco];
153  float weight_RtoT = (float) n_match + (float) n_reco / 100.;
154  if (entry_RtoT.find(weight_RtoT) == entry_RtoT.end())
155  {
156  entry_RtoT[weight_RtoT] = {gtrackID}; // i.e. std::set<int> { gtrackID }
157  }
158  else
159  {
160  entry_RtoT[weight_RtoT].insert(gtrackID);
161  }
162 
163  if (Verbosity() > 20)
164  {
165  printf("EmbRecoMatch: gtrackID(%2i) id_reco(%2i) nclusters:match(%i),gtrack(%2i),reco(%2i)\n",
166  gtrackID, (int) id_reco, (int) n_match, (int) n_truth, (int) n_reco);
167  printf(" -> in SvtxPHG4ParticleMap {id_reco->{weight->id_true}} = {%2i->{%5.2f->%2i}}\n",
168  (int) id_reco, weight_RtoT, (int) gtrackID);
169  printf(" -> in PHG4ParticleSvtxMap {id_true->{weight->id_reco}} = {%2i->{%5.2f->%2i}}\n",
170  gtrackID, weight_TtoR, (int) id_reco);
171  }
172  }
173 
174  // fill the output maps
175  for (auto &entry : map_TtoR)
176  {
177  m_PHG4ParticleSvtxMap->insert(entry.first, entry.second);
178  }
179  for (auto &entry : map_RtoT)
180  {
181  m_SvtxPHG4ParticleMap->insert(entry.first, entry.second);
182  }
183 
184  if (Verbosity() > 15)
185  {
186  std::cout << PHWHERE << " Print out: " << std::endl
187  << " Contents of SvtxPHG4ParticleMap (node \"RecoToPHG4ParticleMap\")" << std::endl
188  << " and PHG4ParticleSvtxMap (node \"PHG4ParticleToRecoMap\")" << std::endl;
189 
190  std::cout << " --BEGIN-- Contents of SvtxPHG4ParticleMap: " << std::endl;
191  for (auto &iter : *m_SvtxPHG4ParticleMap)
192  {
193  printf(" { %2i ", (int) iter.first); // id_reco
194  auto n_matches = iter.second.size();
195  long unsigned int cnt_matches = 0;
196  for (const auto &matches : iter.second)
197  {
198  if (cnt_matches == 0)
199  {
200  printf("-> { %5.2f -> ", matches.first);
201  }
202  else
203  {
204  printf(" -> { %5.2f -> ", matches.first);
205  }
206  auto size = matches.second.size();
207  long unsigned int i = 0;
208  for (auto id_true : matches.second)
209  {
210  if (i < size - 1)
211  {
212  printf("%2i, ", id_true);
213  }
214  else
215  {
216  printf("%2i }", id_true);
217  }
218  ++i;
219  } // end matches cnt
220  if (cnt_matches < (n_matches - 1))
221  {
222  printf(",\n");
223  }
224  else
225  {
226  printf("}\n");
227  }
228  ++cnt_matches;
229  }
230  }
231  std::cout << " --END-- Contents of SvtxPHG4ParticleMap: " << std::endl
232  << std::endl;
233 
234  std::cout << " --BEGIN-- Contents of PHG4ParticleToRecoMap: " << std::endl;
235  for (auto &iter : *m_PHG4ParticleSvtxMap)
236  {
237  printf(" { %2i ", iter.first); // id_true
238  auto n_matches = iter.second.size();
239  long unsigned int cnt_matches = 0;
240  for (const auto &matches : iter.second)
241  {
242  if (cnt_matches == 0)
243  {
244  printf("-> { %5.2f -> ", matches.first);
245  }
246  else
247  {
248  printf(" -> { %5.2f -> ", matches.first);
249  }
250  auto size = matches.second.size();
251  long unsigned int i = 0;
252  for (auto id_reco : matches.second)
253  {
254  if (i < size - 1)
255  {
256  printf("%2i, ", id_reco);
257  }
258  else
259  {
260  printf("%2i }", id_reco);
261  }
262  ++i;
263  } // end matches cnt
264  if (cnt_matches < (n_matches - 1))
265  {
266  printf(",\n");
267  }
268  else
269  {
270  printf("}\n");
271  }
272  ++cnt_matches;
273  }
274  }
275  std::cout << " --END-- Contents of SvtxPHG4ParticleMap: " << std::endl
276  << std::endl;
277  }
278 
280 }
281 
283 {
285 }