LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
ClusterTrackAna_module.cc
Go to the documentation of this file.
1 // Class: ClusterTrackAna
3 // Plugin Type: analyzer (art v3_03_01)
4 // File: ClusterTrackAna_module.cc
5 //
6 // Calculates the performance of 2D cluster and 3D track reconstruction modules.
7 // The metrics Efficiency, Purity, and Efficiency * Purity (EP) are calculated using
8 // MC truth matched hits in each TPC and Plane. Note that the metrics are calculated in
9 // each TPC and plane for clusters or tracks that span multiple TPCs. The metrics are summed and
10 // reported at the end of the job for each selected true particle type. The metrics are also
11 // summed over ALL selected true particle types. Note that the term "cluster" refers to
12 // a subset of hits in a recob::Track or recob::Cluster that reside in one TPC and one plane
13 // and may be matched to a simb::MCParticle
14 //
15 // Generated at Wed Jan 8 10:33:20 2020 by Bruce Baller using cetskelgen
16 // from cetlib version v3_08_00.
18 
27 #include "fhiclcpp/ParameterSet.h"
29 
31 
41 
42 namespace cluster {
43  class ClusterTrackAna;
44 }
45 
47 public:
48  explicit ClusterTrackAna(fhicl::ParameterSet const& p);
49 
50  ClusterTrackAna(ClusterTrackAna const&) = delete;
51  ClusterTrackAna(ClusterTrackAna&&) = delete;
52  ClusterTrackAna& operator=(ClusterTrackAna const&) = delete;
54 
55  // Required functions.
56  void analyze(art::Event const& e) override;
57 
58 private:
59  void endJob() override;
60 
65  std::vector<int> fSkipPDGCodes;
66  short fPrintLevel;
67  unsigned int fInTPC;
68  float fBadEP;
69 
70  // Count of the number of events considered
71  unsigned int fEventCnt{0};
72  // Count of the number of poorly reconstructed clusters
73  unsigned int fNBadEP{0};
74  // count of EP entries for electrons(0), muons(1), pions(2), kaons(3), protons(4)
75  std::array<float, 5> Cnts{{0}};
76  std::array<float, 5> EPSums{{0}};
77  // same for Efficiency
78  std::array<float, 5> ESums{{0}};
79  // and for Purity
80  std::array<float, 5> PSums{{0}};
81 
83  std::vector<unsigned int> fHitMCPIndex;
84 
86  true};
87  bool fFirstPrint{true};
88  unsigned int fCurrentRun{0};
89 
90  void FirstLastHitInPlane(unsigned int tpc,
91  unsigned int plane,
92  unsigned int mcpi,
93  unsigned int& firstHitIndex,
94  unsigned int& lastHitIndex);
95  std::string HitLocation(
96  unsigned int iht);
97 };
98 
100 {
101  fHitModuleLabel = pset.get<art::InputTag>("HitModuleLabel");
102  bool validModuleLabel = false;
103  fClusterModuleLabel = "NA";
104  fTrackModuleLabel = "NA";
105  fClusterModuleLabel = pset.get<art::InputTag>("ClusterModuleLabel", "NA");
106  if (fClusterModuleLabel != "NA") validModuleLabel = true;
107  fTrackModuleLabel = pset.get<art::InputTag>("TrackModuleLabel", "NA");
108  if (validModuleLabel && fTrackModuleLabel != "NA")
109  throw cet::exception("ClusterTrackAna")
110  << "You must specify either a ClusterModuleLabel OR a TrackModuleLabel\n";
111  if (!validModuleLabel && fTrackModuleLabel != "NA") validModuleLabel = true;
112  // origin = 0 (anything), 1(nu), 2(cosmics), 3(SN nu), 4(SingleParticle)
113  int tmp = pset.get<int>("TruthOrigin", 0);
115  fPrintLevel = pset.get<short>("PrintLevel", 0);
116  if (pset.has_key("SkipPDGCodes")) fSkipPDGCodes = pset.get<std::vector<int>>("SkipPDGCodes");
117  fBadEP = pset.get<float>("BadEP", 0.);
118  fInTPC = UINT_MAX;
119  int intpc = pset.get<int>("InTPC", -1);
120  if (intpc >= 0) fInTPC = intpc;
121  // do some initialization
122  Cnts.fill(0.);
123  EPSums.fill(0.);
124  ESums.fill(0.);
125  PSums.fill(0.);
126 } // ClusterTrackAna constructor
127 
130 {
131  // Match hits to MCParticles, then consider reconstructed hits in each TPC and plane
132  // to calculate Efficiency, Purity and Efficiency * Purity (aka EP).
133 
134  ++fEventCnt;
135  auto const* geom = lar::providerFrom<geo::Geometry>();
136  auto const& wireReadoutGeom = art::ServiceHandle<geo::WireReadout>()->Get();
137  // auto hitsHandle = art::Handle<std::vector<recob::Hit>>();
139  throw cet::exception("ClusterTrackAna")
140  << "Failed to get a handle to hit collection '" << fHitModuleLabel.label() << "'\n";
141  // get a reference to the MCParticles
142  auto mcpHandle = art::Handle<std::vector<simb::MCParticle>>();
143  if (!evt.getByLabel("largeant", mcpHandle))
144  throw cet::exception("ClusterTrackAna")
145  << "Failed to get a handle to MCParticles using largeant\n";
146 
147  // decide whether to consider cluster -> hit -> MCParticle for any MCParticle origin or for
148  // a specific user-specified origin
149  bool anySource = (fTruthOrigin == simb::kUnknown);
150 
151  if (fPrintLevel > 0 && evt.run() != fCurrentRun) {
152  mf::LogVerbatim("ClusterTrackAna") << "Run: " << evt.run();
153  fCurrentRun = evt.run();
154  }
155 
158  sim::ParticleList const& plist = pi_serv->ParticleList();
159  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
160 
161  // make a list of Hit -> MCParticle assns in all TPCs. The first step is
162  // to make a list of Geant TrackIDs whose origin was requested by the user.
163  std::vector<int> trkIDs;
164  // and a vector of MCParticle indices into the mcpHandle vector indexed by trkIDs
165  std::vector<unsigned int> MCPIs;
166  for (sim::ParticleList::const_iterator ipart = plist.begin(); ipart != plist.end(); ++ipart) {
167  const simb::MCParticle* part = (*ipart).second;
168  int trackID = part->TrackId();
169  art::Ptr<simb::MCTruth> theTruth = pi_serv->TrackIdToMCTruth_P(trackID);
170  if (!anySource && theTruth->Origin() != fTruthOrigin) continue;
171  int pdg = abs(part->PdgCode());
172  bool isCharged = (pdg == 11) || (pdg == 13) || (pdg == 211) || (pdg == 321) || (pdg == 2212);
173  if (!isCharged) continue;
174  if (std::find(fSkipPDGCodes.begin(), fSkipPDGCodes.end(), pdg) != fSkipPDGCodes.end()) continue;
175  float TMeV = 1000 * (part->E() - part->Mass());
176  // ignore low energy particles
177  if (TMeV < 1) continue;
178  // find the MCParticle index and stash it in trkMCP
179  unsigned int mcpi = UINT_MAX;
180  for (mcpi = 0; mcpi < (*mcpHandle).size(); ++mcpi)
181  if ((*mcpHandle)[mcpi].TrackId() == trackID) break;
182  if (mcpi == UINT_MAX) {
183  std::cout << "Failed to find a MCParticle from ParticleList\n";
184  return;
185  }
186  trkIDs.push_back(trackID);
187  MCPIs.push_back(mcpi);
188  } // ipart
189  if (trkIDs.empty()) return;
190 
191  // next construct a companion vector of MCParticle indices indexed to the full hit collection
192  fHitMCPIndex.resize((*fHitHandle).size(), UINT_MAX);
193  // Make a list of the <first, last> MC-matched hit in each TPC. This will be used
194  // to only iterate through the range of hits that are interesting
195  std::vector<std::pair<unsigned int, unsigned int>> hitRange(geom->NTPC() + 1);
196  size_t noMatch = 0;
197  size_t nMatch = 0;
198  size_t nInTPC = 0;
199  for (auto& hr : hitRange)
200  hr = std::make_pair(UINT_MAX, UINT_MAX);
201  for (size_t iht = 0; iht < (*fHitHandle).size(); ++iht) {
202  auto& hit = (*fHitHandle)[iht];
203  // only consider hits in a single TPC?
204  unsigned int tpc = hit.WireID().TPC;
205  if (fInTPC != UINT_MAX && tpc != fInTPC) continue;
206  ++nInTPC;
207  auto tides = bt_serv->HitToTrackIDEs(clockData, hit);
208  if (tides.empty()) {
209  ++noMatch;
210  continue;
211  }
212  for (auto& tide : tides) {
213  // declare a match to a MCParticle if > 50% of energy is from it
214  if (tide.energyFrac < 0.5) continue;
215  int trackID = tide.trackID;
216  // find the MCParticle index and define the Hit -> MCParticle assn
217  for (size_t indx = 0; indx < trkIDs.size(); ++indx) {
218  if (trkIDs[indx] == trackID) {
219  fHitMCPIndex[iht] = MCPIs[indx];
220  break;
221  }
222  } // indx
223  break;
224  } // tide
225  if (fHitMCPIndex[iht] == UINT_MAX) continue;
226  ++nMatch;
227  // populate hitRange
228  if (hitRange[tpc].first == UINT_MAX) hitRange[tpc].first = iht;
229  hitRange[tpc].second = iht;
230  } // iht
231 
232  if (nMatch == 0) {
233  fHitMCPIndex.resize(0);
234  return;
235  }
236 
237  if (fPrintLevel > 3) {
238  // Print the gory details
239  mf::LogVerbatim myprt("ClusterTrackAna");
240  myprt << "Checking " << trkIDs.size() << " selected MCParticles with the requested TruthOrigin";
241  if (fInTPC == UINT_MAX) { myprt << " in all TPCs\n"; }
242  else {
243  myprt << " in TPC " << fInTPC;
244  }
245  myprt << " in Run " << evt.run() << " Event " << evt.event();
246  myprt << "\n";
247  myprt << "Found " << nMatch << " MC-matched hits with the requested TruthOrigin";
248  myprt << " out of " << nInTPC << " total hits.\n";
249  myprt << "Found " << noMatch << " hits not matched to ANY MCParticle.\n";
250  // count the number of hits matched to each MCParticle in the list
251  // mcpi count
252  std::vector<std::pair<unsigned int, unsigned int>> mcpCnt;
253  for (auto mcpi : fHitMCPIndex) {
254  if (mcpi == UINT_MAX) continue;
255  unsigned short mIndx = 0;
256  for (mIndx = 0; mIndx < mcpCnt.size(); ++mIndx)
257  if (mcpCnt[mIndx].first == mcpi) break;
258  if (mIndx == mcpCnt.size()) mcpCnt.push_back(std::make_pair(mcpi, 0));
259  // increment the count of MC-matched hits
260  ++mcpCnt[mIndx].second;
261  } // mcpi
262  myprt << " MCPI TrackID PDGCode T(MeV) nHits Process";
263  for (auto mcpcnt : mcpCnt) {
264  myprt << "\n";
265  unsigned int mcpi = mcpcnt.first;
266  auto& mcp = (*mcpHandle)[mcpi];
267  myprt << std::setw(5) << mcpi;
268  myprt << std::setw(8) << mcp.TrackId();
269  myprt << std::setw(8) << abs(mcp.PdgCode());
270  int TMeV = 1000 * (mcp.E() - mcp.Mass());
271  myprt << std::setw(9) << TMeV;
272  myprt << std::setw(8) << mcpcnt.second;
273  myprt << " " << mcp.Process();
274  } // mcpcnt
275  } // fPrintLevel > 3
276 
277  // fill a vector of hits indices from all clusters or all tracks
278  std::vector<std::vector<unsigned int>> recoHits;
279  // and a companion vector of indices into the cluster or track collection
280  std::vector<unsigned int> recoIndex;
281  // handles to these collections to enable printing the cluster or track ID. The
282  // handle will be valid for either clusters or tracks
285  if (fClusterModuleLabel.label() != "NA") {
286  // get MC-matched hits from clusters
287  if (!evt.getByLabel(fClusterModuleLabel, inputClusters))
288  throw cet::exception("ClusterTrackAna")
289  << "Failed to get a handle to the cluster collection '" << fClusterModuleLabel.label()
290  << "'\n";
291  art::FindManyP<recob::Hit> hitsFromCls(inputClusters, evt, fClusterModuleLabel);
292  if (!hitsFromCls.isValid())
293  throw cet::exception("ClusterTrackAna") << "Failed to get a handle to Cluster -> Hit assns\n";
294  for (unsigned int icl = 0; icl < (*inputClusters).size(); ++icl) {
295  std::vector<art::Ptr<recob::Hit>> cluhits = hitsFromCls.at(icl);
296  if (cluhits.empty()) continue;
297  if (fCompareProductIDs) {
298  if (cluhits[0].id() != fHitHandle.id())
299  throw cet::exception("ClusterTrackAna")
300  << "Hits associated with ClusterModuleLabel are in a different collection than "
301  "HitModuleLabel.\n";
302  fCompareProductIDs = false;
303  } // fCompareProductIDs
304  // only load MC-matched hits. Hits that are not MC-matched were either matched to a
305  // MCParticle that was not selected or were mis-reconstructed by the hit finder. Neither
306  // of these conditions warrant penalizing the reconstruction module performance
307  std::vector<unsigned int> hitsIndex;
308  for (auto& cluhit : cluhits) {
309  if (fHitMCPIndex[cluhit.key()] != UINT_MAX) hitsIndex.push_back(cluhit.key());
310  }
311  if (hitsIndex.empty()) continue;
312  recoIndex.push_back(icl);
313  recoHits.push_back(hitsIndex);
314  } // icl
315  }
316  else {
317  // get MC-matched hits from tracks
318  if (!evt.getByLabel(fTrackModuleLabel, inputTracks))
319  throw cet::exception("ClusterTrackAna")
320  << "Failed to get a handle to the track collection '" << fTrackModuleLabel.label() << "'\n";
321  art::FindManyP<recob::Hit> hitsFromTrk(inputTracks, evt, fTrackModuleLabel);
322  if (!hitsFromTrk.isValid())
323  throw cet::exception("ClusterTrackAna") << "Failed to get a handle to Track -> Hit assns\n";
324  for (unsigned int itk = 0; itk < (*inputTracks).size(); ++itk) {
325  std::vector<art::Ptr<recob::Hit>> trkhits = hitsFromTrk.at(itk);
326  if (trkhits.empty()) continue;
327  if (fCompareProductIDs) {
328  if (trkhits[0].id() != fHitHandle.id())
329  throw cet::exception("ClusterTrackAna")
330  << "Hits associated with TrackModuleLabel are in a different collection than "
331  "HitModuleLabel.\n";
332  fCompareProductIDs = false;
333  } // fCompareProductIDs
334  std::vector<unsigned int> hitsIndex;
335  for (auto& trkhit : trkhits) {
336  if (fHitMCPIndex[trkhit.key()] != UINT_MAX) hitsIndex.push_back(trkhit.key());
337  }
338  if (hitsIndex.empty()) continue;
339  recoIndex.push_back(itk);
340  recoHits.push_back(hitsIndex);
341  } // itk
342  } // get hits from tracks
343 
344  for (const auto& tpcid : geom->Iterate<geo::TPCID>()) {
345  unsigned int tpc = tpcid.TPC;
346  if (hitRange[tpc].first == UINT_MAX) continue;
347  // iterate over planes
348  for (unsigned short plane = 0; plane < wireReadoutGeom.Nplanes(); ++plane) {
349  unsigned int tpcMatHitCnt = 0;
350  unsigned int tpcTotHitCnt = 0;
351  // create a list of (MCParticle index, matched hit count> pairs
352  // mcParticle plane
353  std::vector<std::pair<unsigned int, float>> mcpCnt;
354  // count MCParticle, Cluster/Track hit counts - size matched to mcpCnt
355  std::vector<std::vector<std::pair<unsigned int, float>>> mcpClsCnt;
356  for (unsigned int iht = hitRange[tpc].first; iht <= hitRange[tpc].second; ++iht) {
357  auto& hit = (*fHitHandle)[iht];
358  if (hit.WireID().TPC != tpc) continue;
359  if (hit.WireID().Plane != plane) continue;
360  ++tpcTotHitCnt;
361  // ignore hits that were either unmatched to the selected set of PDGCodes
362  // or have a not-requested origin
363  if (fHitMCPIndex[iht] == UINT_MAX) continue;
364  ++tpcMatHitCnt;
365  unsigned int mcpi = fHitMCPIndex[iht];
366  unsigned short mIndx = 0;
367  for (mIndx = 0; mIndx < mcpCnt.size(); ++mIndx)
368  if (mcpCnt[mIndx].first == mcpi) break;
369  if (mIndx == mcpCnt.size()) {
370  // found a MCParticle not in the list yet, so add it
371  mcpCnt.push_back(std::make_pair(mcpi, 0));
372  mcpClsCnt.resize(mcpCnt.size());
373  }
374  // increment the number of MC-matched hits
375  ++mcpCnt[mIndx].second;
376  } // iht
377  // ignore TPCs/planes with few MC-matched hits
378  if (fPrintLevel > 2) {
379  mf::LogVerbatim myprt("ClusterTrackAna");
380  myprt << "TPC:" << tpc << " Plane:" << plane << " has " << tpcMatHitCnt << "/"
381  << tpcTotHitCnt;
382  myprt << " (MC-matched hits) / (all hits)";
383  }
384  if (tpcMatHitCnt < 3) continue;
385  // next iterate over all clusters/tracks and count mc-matched hits that are in this TPC/plane
386  for (unsigned int ii = 0; ii < recoHits.size(); ++ii) {
387  float nRecoHitsInPlane = 0;
388  float nRecoMatHitsInPlane = 0;
389  for (auto iht : recoHits[ii]) {
390  auto& hit = (*fHitHandle)[iht];
391  if (hit.WireID().TPC != tpc) continue;
392  if (hit.WireID().Plane != plane) continue;
393  ++nRecoHitsInPlane;
394  if (fHitMCPIndex[iht] == UINT_MAX) continue;
395  ++nRecoMatHitsInPlane;
396  unsigned int mcpi = fHitMCPIndex[iht];
397  // find the MCParticle index in mcpCnt and use it to count the match entry
398  // in mcpClsCnt
399  unsigned short mIndx = 0;
400  for (mIndx = 0; mIndx < mcpCnt.size(); ++mIndx)
401  if (mcpCnt[mIndx].first == mcpi) break;
402  if (mIndx == mcpCnt.size()) {
403  std::cout << "Logic error: fHitMCPIndex = " << fHitMCPIndex[iht]
404  << " is valid but isn't in the list of MCParticles to use. Please send email "
405  "to baller@fnal.gov.\n";
406  continue;
407  }
408  unsigned short cIndx = 0;
409  for (cIndx = 0; cIndx < mcpClsCnt[mIndx].size(); ++cIndx)
410  if (mcpClsCnt[mIndx][cIndx].first == ii) break;
411  if (cIndx == mcpClsCnt[mIndx].size()) mcpClsCnt[mIndx].push_back(std::make_pair(ii, 0));
412  ++mcpClsCnt[mIndx][cIndx].second;
413  } // cluhit
414  if (nRecoMatHitsInPlane == 0) continue;
415  } // ii
416  // find the cluster that has the most hits matched to each MC Particle
417  for (unsigned short mIndx = 0; mIndx < mcpCnt.size(); ++mIndx) {
418  // require at least 3 MC-matched hits
419  if (mcpCnt[mIndx].second < 3) continue;
420  unsigned int mcpi = mcpCnt[mIndx].first;
421  auto& mcp = (*mcpHandle)[mcpi];
422  unsigned int pdgCode = abs(mcp.PdgCode());
423  unsigned short pIndx = USHRT_MAX;
424  if (pdgCode == 11) pIndx = 0;
425  if (pdgCode == 13) pIndx = 1;
426  if (pdgCode == 211) pIndx = 2;
427  if (pdgCode == 321) pIndx = 3;
428  if (pdgCode == 2212) pIndx = 4;
429  if (mcpClsCnt[mIndx].empty()) {
430  // Un-reconstructed MCParticle hits
431  ++Cnts[pIndx];
432  ++fNBadEP;
433  if (fPrintLevel > 0) {
434  mf::LogVerbatim myprt("ClusterTrackAna");
435  myprt << " MCPI " << mcpi;
436  int TMeV = 1000 * (mcp.E() - mcp.Mass());
437  myprt << " " << TMeV << " MeV";
438  std::string partName = std::to_string(pdgCode);
439  if (pdgCode == 11) partName = "El";
440  if (pdgCode == 13) partName = "Mu";
441  if (pdgCode == 211) partName = "Pi";
442  if (pdgCode == 311) partName = "Ka";
443  if (pdgCode == 2212) partName = "Pr";
444  myprt << std::setw(5) << partName;
445  // print out the range of truth-matched hits
446  unsigned int firstHitIndex = UINT_MAX;
447  unsigned int lastHitIndex = UINT_MAX;
448  FirstLastHitInPlane(tpc, plane, mcpi, firstHitIndex, lastHitIndex);
449  myprt << " Failed to reconstruct. Truth-matched hit range from ";
450  myprt << HitLocation(firstHitIndex);
451  myprt << " to ";
452  myprt << HitLocation(lastHitIndex);
453  myprt << " <- EP = 0!";
454  } // fPrintLevel > 0
455  continue;
456  } // (mcpClsCnt[mIndx].empty()
457  std::pair<unsigned int, float> big = std::make_pair(UINT_MAX, 0);
458  for (unsigned short cIndx = 0; cIndx < mcpClsCnt[mIndx].size(); ++cIndx) {
459  auto& mcc = mcpClsCnt[mIndx][cIndx];
460  if (mcc.second > big.second) big = mcc;
461  } // cIndx
462  if (big.first == UINT_MAX) {
463  if (fPrintLevel > 2) {
464  mf::LogVerbatim myprt("ClusterTrackAna");
465  unsigned int mcpi = mcpCnt[mIndx].first;
466  auto& mcp = (*mcpHandle)[mcpi];
467  myprt << "Match failed: mcpi " << mcpi << " pdg " << mcp.PdgCode();
468  }
469  std::cout << "match failed mIndx " << mIndx << "\n";
470  continue;
471  } // big.first == UINT_MAX
472  unsigned int ii = big.first;
473  float eff = big.second / mcpCnt[mIndx].second;
474  float nRecoHitsInPlane = 0;
475  // define some variables to print the range of the cluster/track
476  unsigned int firstRecoHitIndex = UINT_MAX;
477  unsigned int lastRecoHitIndex = UINT_MAX;
478  for (auto iht : recoHits[ii]) {
479  auto& hit = (*fHitHandle)[iht];
480  if (hit.WireID().TPC != tpc) continue;
481  if (hit.WireID().Plane != plane) continue;
482  ++nRecoHitsInPlane;
483  if (firstRecoHitIndex == UINT_MAX) {
484  firstRecoHitIndex = iht;
485  lastRecoHitIndex = iht;
486  }
487  unsigned int wire = (*fHitHandle)[iht].WireID().Wire;
488  if (wire < (*fHitHandle)[firstRecoHitIndex].WireID().Wire) firstRecoHitIndex = iht;
489  if (wire > (*fHitHandle)[lastRecoHitIndex].WireID().Wire) lastRecoHitIndex = iht;
490  } // iht
491  float pur = big.second / nRecoHitsInPlane;
492  ++Cnts[pIndx];
493  float ep = eff * pur;
494  EPSums[pIndx] += ep;
495  ESums[pIndx] += eff;
496  PSums[pIndx] += pur;
497  bool hasBadEP = (ep < fBadEP);
498  if (hasBadEP) ++fNBadEP;
499  bool prt = fPrintLevel > 1 || (fPrintLevel == 1 && hasBadEP);
500  if (prt) {
501  mf::LogVerbatim myprt("ClusterTrackAna");
502  if (fFirstPrint) {
503  myprt << "Hit location format is TPC:Plane:Wire:Tick\n";
504  myprt << " MCPI Ptcl T(MeV) nMCPHits ____From____ _____To_____";
505  if (inputClusters.isValid()) { myprt << " clsID"; }
506  else {
507  myprt << " trkID";
508  }
509  myprt
510  << " ____From____ _____To_____ nRecoHits nMCPRecoHits Eff Pur EP\n";
511  fFirstPrint = false;
512  } // fFirstPrint
513  myprt << std::setw(5) << mcpi;
514  // convert the PDG code into nicer format
515  std::string partName = std::to_string(pdgCode);
516  if (pdgCode == 11) partName = " Electron";
517  if (pdgCode == 13) partName = " Muon";
518  if (pdgCode == 211) partName = " Pion";
519  if (pdgCode == 311) partName = " Kaon";
520  if (pdgCode == 2212) partName = " Proton";
521  myprt << partName;
522  int TMeV = 1000 * (mcp.E() - mcp.Mass());
523  myprt << std::setw(7) << TMeV;
524  // nMCPHits
525  myprt << std::setw(10) << mcpCnt[mIndx].second;
526  unsigned int firstTruHitIndex = UINT_MAX;
527  unsigned int lastTruHitIndex = UINT_MAX;
528  FirstLastHitInPlane(tpc, plane, mcpi, firstTruHitIndex, lastTruHitIndex);
529  myprt << std::setw(14) << HitLocation(firstTruHitIndex);
530  myprt << std::setw(14) << HitLocation(lastTruHitIndex);
531  int id = -1;
532  if (inputClusters.isValid()) {
533  // print cluster info
534  auto& cls = (*inputClusters)[recoIndex[ii]];
535  id = cls.ID();
536  }
537  else if (inputTracks.isValid()) {
538  // print track info
539  auto& trk = (*inputTracks)[recoIndex[ii]];
540  id = trk.ID();
541  }
542  myprt << std::setw(6) << id;
543  myprt << std::setw(14) << HitLocation(firstRecoHitIndex);
544  myprt << std::setw(14) << HitLocation(lastRecoHitIndex);
545  myprt << std::setw(12) << (int)nRecoHitsInPlane;
546  myprt << std::setw(13) << (int)big.second;
547  myprt << std::fixed << std::setprecision(2);
548  myprt << std::setw(8) << eff;
549  myprt << std::setw(8) << pur;
550  myprt << std::setw(8) << ep;
551  if (hasBadEP) myprt << " <- BadEP";
552  myprt << " Evt: " << evt.event();
553  myprt << " Evt Cnt " << fEventCnt;
554  } // prt
555  } // mIndx
556  } // plane
557  } // tpcid
558 
559  fHitMCPIndex.resize(0);
560 
561 } // analyze
562 
564 std::string cluster::ClusterTrackAna::HitLocation(unsigned int iht)
565 {
566  // Put the hit location into a compact human-readable format
567  if (iht >= (*fHitHandle).size()) return "NA";
568  auto& hit = (*fHitHandle)[iht];
569  return std::to_string(hit.WireID().TPC) + ":" + std::to_string(hit.WireID().Plane) + ":" +
570  std::to_string(hit.WireID().Wire) + ":" + std::to_string((int)hit.PeakTime());
571 } // HitLocation
572 
575  unsigned int plane,
576  unsigned int mcpi,
577  unsigned int& firstHitIndex,
578  unsigned int& lastHitIndex)
579 {
580  // Returns the index of the first hit (lowest wire number) and last hit (highest wire number)
581  // matched to the MCParticle indexed by mcpi in the requested tpc, plane
582  firstHitIndex = UINT_MAX;
583  lastHitIndex = UINT_MAX;
584  for (unsigned int iht = 0; iht < (*fHitHandle).size(); ++iht) {
585  if (fHitMCPIndex[iht] != mcpi) continue;
586  auto& hit = (*fHitHandle)[iht];
587  if (hit.WireID().TPC != tpc) continue;
588  if (hit.WireID().Plane != plane) continue;
589  if (firstHitIndex == UINT_MAX) {
590  firstHitIndex = iht;
591  lastHitIndex = iht;
592  }
593  unsigned int wire = (*fHitHandle)[iht].WireID().Wire;
594  if (wire < (*fHitHandle)[firstHitIndex].WireID().Wire) firstHitIndex = iht;
595  if (wire > (*fHitHandle)[lastHitIndex].WireID().Wire) lastHitIndex = iht;
596  } // iht
597 } // FirstLastHitInPlane
598 
601 {
602  // output results
603  mf::LogVerbatim myprt("ClusterTrackAna");
604  myprt << "ClusterTrackAna summary results for " << fEventCnt;
605  if (fClusterModuleLabel.label() != "NA") {
606  myprt << " events using ClusterModuleLabel: " << fClusterModuleLabel.label();
607  }
608  else {
609  myprt << " events using TrackModuleLabel: " << fTrackModuleLabel.label();
610  }
611  myprt << " Origin: " << fTruthOrigin;
612  if (fInTPC != UINT_MAX) { myprt << " in TPC " << fInTPC; }
613  else {
614  myprt << " in all TPCs";
615  }
616  myprt << "\n";
617  float cnts = 0;
618  for (unsigned short pIndx = 0; pIndx < 5; ++pIndx)
619  cnts += Cnts[pIndx];
620  if (cnts == 0) {
621  myprt << "No ClusterTrackAna results";
622  return;
623  }
624  float sumEP = 0;
625  float sumE = 0;
626  float sumP = 0;
627  myprt << "Efficiency (Eff), Purity (Pur) and Eff * Pur (EP) by selected truth particle types\n";
628  std::array<std::string, 5> pName = {{"El", "Mu", "Pi", "K ", "P "}};
629  myprt << "particle Eff Pur EP\n";
630  myprt << "-------------------------------";
631  for (unsigned short pIndx = 0; pIndx < 5; ++pIndx) {
632  if (Cnts[pIndx] == 0) continue;
633  float ave;
634  myprt << "\n " << pName[pIndx] << " ";
635  myprt << std::fixed << std::setprecision(3);
636  ave = ESums[pIndx] / Cnts[pIndx];
637  myprt << std::setw(8) << ave;
638  ave = PSums[pIndx] / Cnts[pIndx];
639  myprt << std::setw(8) << ave;
640  ave = EPSums[pIndx] / Cnts[pIndx];
641  myprt << std::setw(8) << ave;
642  if (pIndx == 0) continue;
643  sumEP += EPSums[pIndx];
644  sumE += ESums[pIndx];
645  sumP += PSums[pIndx];
646  } // pIndx
647  if (cnts == 0) return;
648  myprt << "\n";
649  myprt << "Averages for all selected truth particles\n";
650  myprt << "Ave Eff " << sumE / cnts;
651  myprt << " Ave Pur " << sumP / cnts;
652  myprt << " Ave EP " << sumEP / cnts;
653  myprt << " nBadEP " << fNBadEP;
654  myprt << " (EP < " << std::fixed << std::setprecision(2) << fBadEP << ")";
655  myprt << "\n";
656  myprt << "MCParticle counts in all TPCs and Planes:";
657  for (unsigned short pIndx = 0; pIndx < 5; ++pIndx) {
658  if (Cnts[pIndx] == 0) continue;
659  myprt << " " << pName[pIndx] << " " << (int)Cnts[pIndx];
660  } // pIndx
661 } // endJob
double E(const int i=0) const
Definition: MCParticle.h:234
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
ClusterTrackAna(fhicl::ParameterSet const &p)
int PdgCode() const
Definition: MCParticle.h:213
Utilities related to art service access.
Int_t ipart
Definition: Style.C:10
std::vector< sim::TrackIDE > HitToTrackIDEs(detinfo::DetectorClocksData const &clockData, recob::Hit const &hit) const
ClusterTrackAna & operator=(ClusterTrackAna const &)=delete
Declaration of signal hit object.
std::array< float, 5 > PSums
list_type::const_iterator const_iterator
Definition: ParticleList.h:132
simb::Origin_t Origin() const
Definition: MCTruth.h:74
double Mass() const
Definition: MCParticle.h:240
enum simb::_ev_origin Origin_t
event origin types
constexpr auto abs(T v)
Returns the absolute value of the argument.
cout<< "Opened file "<< fin<< " ixs= "<< ixs<< endl;if(ixs==0) hhh=(TH1F *) fff-> Get("h1")
Definition: AddMC.C:8
Float_t tmp
Definition: plot.C:35
Cluster finding and building.
std::string HitLocation(unsigned int iht)
packs hit WireID and PeakTime into a compact format
std::array< float, 5 > Cnts
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
int TrackId() const
Definition: MCParticle.h:211
bool isValid() const noexcept
Definition: Handle.h:203
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:101
std::string const & label() const noexcept
Definition: InputTag.cc:79
TString part[npart]
Definition: Style.C:32
void analyze(art::Event const &e) override
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
IDparameter< geo::WireID > WireID
Member type of validated geo::WireID parameter.
decltype(auto) constexpr to_string(T &&obj)
ADL-aware version of std::to_string.
const art::Ptr< simb::MCTruth > & TrackIdToMCTruth_P(int id) const
std::array< float, 5 > EPSums
iterator begin()
Definition: ParticleList.h:305
EventNumber_t event() const
Definition: Event.cc:41
void FirstLastHitInPlane(unsigned int tpc, unsigned int plane, unsigned int mcpi, unsigned int &firstHitIndex, unsigned int &lastHitIndex)
The data type to uniquely identify a TPC.
Definition: geo_types.h:306
Declaration of cluster object.
Detector simulation of raw signals on wires.
const sim::ParticleList & ParticleList() const
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Provides recob::Track data product.
bool fCompareProductIDs
compare Hit and Cluster-> Hit art product IDs on the first event
art::Handle< std::vector< recob::Hit > > fHitHandle
TCEvent evt
Definition: DataStructs.cxx:8
std::array< float, 5 > ESums
Float_t e
Definition: plot.C:35
RunNumber_t run() const
Definition: Event.cc:29
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:82
ProductID id() const
Definition: Handle.h:224
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:109
std::vector< unsigned int > fHitMCPIndex