ClusterAna_module.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 //
3 // ClusterAna class
4 //
5 // Bruce Baller
6 //
7 ////////////////////////////////////////////////////////////////////////
8 
9 #include <TProfile.h>
10 #include <array>
11 #include <fstream>
12 #include <iomanip>
13 #include <string>
14 
15 //Framework includes
20 #include "art_root_io/TFileService.h"
21 #include "canvas/Persistency/Common/FindManyP.h"
24 #include "fhiclcpp/ParameterSet.h"
26 
27 //LArSoft includes
36 #include "nug4/ParticleNavigation/ParticleList.h"
38 
40 
41 /// Cluster finding and building
42 namespace cluster {
43 
44  class ClusterAna : public art::EDAnalyzer {
45  public:
46  explicit ClusterAna(fhicl::ParameterSet const& pset);
47 
48  private:
49  void analyze(const art::Event& evt) override;
50  void beginJob() override;
51  void endJob() override;
52 
53  TH1F* fNClusters;
55  // Cosmic Rays
56  TH1F* fCREP2;
57  TH1F* fCRE;
58  TH1F* fCRP;
59  // Neutrino interactions
60  TH1F* fNuKE_elec;
61  TH1F* fNuKE_muon;
62  TH1F* fNuKE_pion;
63  TH1F* fNuKE_kaon;
64  TH1F* fNuKE_prot;
65  // TH1F* fNuEP2;
66  TH1F* fNuEP2_elec;
67  TH1F* fNuEP2_muon;
68  TH1F* fNuEP2_pion;
69  TH1F* fNuEP2_kaon;
70  TH1F* fNuEP2_prot;
71  TH1F* fNuE_elec;
72  TH1F* fNuE_muon;
73  TH1F* fNuE_pion;
74  TH1F* fNuE_kaon;
75  TH1F* fNuE_prot;
76  TH1F* fNuP_elec;
77  TH1F* fNuP_muon;
78  TH1F* fNuP_pion;
79  TH1F* fNuP_kaon;
80  TH1F* fNuP_prot;
81 
82  TH1F* fNuVtx_dx;
83  TH1F* fNuVtx_dy;
84  TH1F* fNuVtx_dz;
85 
86  TProfile* fNuEP2_KE_elec;
87  TProfile* fNuEP2_KE_muon;
88  TProfile* fNuEP2_KE_pion;
89  TProfile* fNuEP2_KE_kaon;
90  TProfile* fNuEP2_KE_prot;
91 
95  std::vector<float> fElecKERange;
96  std::vector<float> fMuonKERange;
97  std::vector<float> fPionKERange;
98  std::vector<float> fKaonKERange;
99  std::vector<float> fProtKERange;
102  // float fMergeAngleCut;
104  short fPrintLevel;
105  short moduleID;
106 
107  std::ofstream outFile;
108 
109  }; // class ClusterAna
110 
111 }
112 
113 namespace cluster {
114 
115  //--------------------------------------------------------------------
117  : EDAnalyzer(pset)
118  , fHitsModuleLabel(pset.get<std::string>("HitsModuleLabel"))
119  , fClusterModuleLabel(pset.get<std::string>("ClusterModuleLabel"))
120  , fVertexModuleLabel(pset.get<std::string>("VertexModuleLabel"))
121  , fElecKERange(pset.get<std::vector<float>>("ElecKERange"))
122  , fMuonKERange(pset.get<std::vector<float>>("MuonKERange"))
123  , fPionKERange(pset.get<std::vector<float>>("PionKERange"))
124  , fKaonKERange(pset.get<std::vector<float>>("KaonKERange"))
125  , fProtKERange(pset.get<std::vector<float>>("ProtKERange"))
126  , fTrackWeightOption(pset.get<short>("TrackWeightOption"))
127  , fMergeDaughters(pset.get<bool>("MergeDaughters"))
128  , fSkipCosmics(pset.get<bool>("SkipCosmics"))
129  , fPrintLevel(pset.get<short>("PrintLevel"))
130  {
131 
132  if (fPrintLevel == -1) {
133  // encode the clustermodule label into an integer
134  moduleID = 0;
135  size_t found = fClusterModuleLabel.find("traj");
136  if (found != std::string::npos) moduleID = 1;
137  found = fClusterModuleLabel.find("line");
138  if (found != std::string::npos) moduleID = 2;
139  found = fClusterModuleLabel.find("fuzz");
140  if (found != std::string::npos) moduleID = 3;
141  found = fClusterModuleLabel.find("pand");
142  if (found != std::string::npos) moduleID = 4;
143  std::cout << "fClusterModuleLabel " << fClusterModuleLabel << " ID " << moduleID << "\n";
144 
146  outFile.open(fileName);
147  }
148  }
149 
150  //------------------------------------------------------------------
151  void
153  {
154 
155  // get access to the TFile service
157 
158  fNClusters = tfs->make<TH1F>("fNoClustersInEvent", "Number of Clusters", 40, 0, 400);
159  fNHitInCluster = tfs->make<TH1F>("fNHitInCluster", "NHitInCluster", 100, 0, 100);
160 
161  if (!fSkipCosmics) {
162  // Cosmic ray histos
163  fCREP2 = tfs->make<TH1F>("CREP2", "CREP2", 50, 0, 1);
164  fCRE = tfs->make<TH1F>("CRE", "CR Efficiency", 50, 0, 1);
165  fCRP = tfs->make<TH1F>("CRP", "CR Purity", 50, 0, 1);
166  }
167  // Neutrino Int histos
168  fNuKE_elec = tfs->make<TH1F>("NuKE_elec", "NuKE electron", 100, 0, 4000);
169  fNuKE_muon = tfs->make<TH1F>("NuKE_muon", "NuKE muon", 100, 0, 4000);
170  fNuKE_pion = tfs->make<TH1F>("NuKE_pion", "NuKE pion", 100, 0, 4000);
171  fNuKE_kaon = tfs->make<TH1F>("NuKE_kaon", "NuKE kaon", 100, 0, 4000);
172  fNuKE_prot = tfs->make<TH1F>("NuKE_prot", "NuKE proton", 100, 0, 4000);
173 
174  fNuEP2_elec = tfs->make<TH1F>("NuEP2_elec", "NuEP2 electron", 50, 0, 1);
175  fNuEP2_muon = tfs->make<TH1F>("NuEP2_muon", "NuEP2 muon", 50, 0, 1);
176  fNuEP2_pion = tfs->make<TH1F>("NuEP2_pion", "NuEP2 pion", 50, 0, 1);
177  fNuEP2_kaon = tfs->make<TH1F>("NuEP2_kaon", "NuEP2 kaon", 50, 0, 1);
178  fNuEP2_prot = tfs->make<TH1F>("NuEP2_prot", "NuEP2 proton", 50, 0, 1);
179 
180  fNuE_elec = tfs->make<TH1F>("NuE_elec", "Nu Efficiency electron", 50, 0, 1);
181  fNuE_muon = tfs->make<TH1F>("NuE_muon", "Nu Efficiency muon", 50, 0, 1);
182  fNuE_pion = tfs->make<TH1F>("NuE_pion", "Nu Efficiency pion", 50, 0, 1);
183  fNuE_kaon = tfs->make<TH1F>("NuE_kaon", "Nu Efficiency kaon", 50, 0, 1);
184  fNuE_prot = tfs->make<TH1F>("NuE_prot", "Nu Efficiency proton", 50, 0, 1);
185 
186  fNuP_elec = tfs->make<TH1F>("NuP_elec", "Nu Purity electron", 50, 0, 1);
187  fNuP_muon = tfs->make<TH1F>("NuP_muon", "Nu Purity muon", 50, 0, 1);
188  fNuP_pion = tfs->make<TH1F>("NuP_pion", "Nu Purity pion", 50, 0, 1);
189  fNuP_kaon = tfs->make<TH1F>("NuP_kaon", "Nu Purity kaon", 50, 0, 1);
190  fNuP_prot = tfs->make<TH1F>("NuP_prot", "Nu Purity proton", 50, 0, 1);
191 
192  // True - Reco vertex difference
193  fNuVtx_dx = tfs->make<TH1F>("Vtx dx", "Vtx dx", 80, -10, 10);
194  fNuVtx_dy = tfs->make<TH1F>("Vtx dy", "Vtx dy", 80, -10, 10);
195  fNuVtx_dz = tfs->make<TH1F>("Vtx dz", "Vtx dz", 80, -10, 10);
196 
197  fNuEP2_KE_elec = tfs->make<TProfile>("NuEP2_KE_elec", "NuEP2 electron vs KE", 200, 0, 2000);
198  fNuEP2_KE_muon = tfs->make<TProfile>("NuEP2_KE_muon", "NuEP2 muon vs KE", 200, 0, 2000);
199  fNuEP2_KE_pion = tfs->make<TProfile>("NuEP2_KE_pion", "NuEP2 pion vs KE", 200, 0, 2000);
200  fNuEP2_KE_kaon = tfs->make<TProfile>("NuEP2_KE_kaon", "NuEP2 kaon vs KE", 200, 0, 2000);
201  fNuEP2_KE_prot = tfs->make<TProfile>("NuEP2_KE_prot", "NuEP2 proton vs KE", 200, 0, 2000);
202  }
203 
204  void
206  {
207  if (fPrintLevel == -1) outFile.close();
208  }
209 
210  void
212  {
213  // code stolen from TrackAna_module.cc
215  if (geom->Nplanes() > 3) {
216  mf::LogError("ClusterAna") << "Too many planes for this code...";
217  return;
218  }
219 
220  // get all hits in the event
222  evt.getByLabel(fHitsModuleLabel, hitListHandle);
223  std::vector<art::Ptr<recob::Hit>> allhits;
224  art::fill_ptr_vector(allhits, hitListHandle);
225  if (allhits.size() == 0) return;
226 
227  // get clusters and cluster-hit associations
228  art::Handle<std::vector<recob::Cluster>> clusterListHandle;
229  evt.getByLabel(fClusterModuleLabel, clusterListHandle);
230  art::FindManyP<recob::Hit> fmh(clusterListHandle, evt, fClusterModuleLabel);
231  if (clusterListHandle->size() == 0) {
232  std::cout << "No clusters in event. Hits size " << allhits.size() << "\n";
233  // don't return or the statistics will be off
234  }
235 
236  // get 3D vertices
237  art::Handle<std::vector<recob::Vertex>> vertexListHandle;
238  double xyz[3] = {0, 0, 0};
239  art::PtrVector<recob::Vertex> recoVtxList;
240  if (vertexListHandle.isValid()) {
241  evt.getByLabel(fVertexModuleLabel, vertexListHandle);
242  for (unsigned int ii = 0; ii < vertexListHandle->size(); ++ii) {
243  art::Ptr<recob::Vertex> vertex(vertexListHandle, ii);
244  recoVtxList.push_back(vertex);
245  vertex->XYZ(xyz);
246  } // ii
247  } // vertexListHandle
248 
249  // list of all true particles
252  sim::ParticleList const& plist = pi_serv->ParticleList();
253  // list of all true particles that will be considered
254  std::vector<const simb::MCParticle*> plist2;
255  // true (reconstructed) hits for each particle in plist2
256  std::vector<std::vector<art::Ptr<recob::Hit>>> hlist2;
257  // index of cluster matched to each particle in plist2 in each plane
258  std::vector<std::vector<short>> truToCl;
259  // number of true hits in each plane and cluster
260  std::vector<std::vector<unsigned short>> nTruHitInCl;
261  //number of reconstructed hits in all clusters
262  std::vector<unsigned short> nRecHitInCl;
263 
264  // calculate average EP2 for every event to facilitate code development
265  // Beam Neutrinos - muons and not-muons
266 
267  float aveNuEP2mu = 0.;
268  float numNuEP2mu = 0.;
269  float aveNuEP2nm = 0.;
270  float numNuEP2nm = 0.;
271  // Cosmic Rays
272  float aveCREP2 = 0.;
273  float numCREP2 = 0.;
274 
275  // track ID of the neutrino (or single particle)
276  int neutTrackID = -1;
277  std::vector<int> tidlist;
278  float neutEnergy = -1.;
279  int neutIntType = -1;
280  int neutCCNC = -1;
281  bool isNeutrino = false;
282  bool isSingleParticle = false;
283 
284  auto const clockData = art::ServiceHandle<detinfo::DetectorClocksService const>()->DataFor(evt);
285 
286  for (sim::ParticleList::const_iterator ipart = plist.begin(); ipart != plist.end(); ++ipart) {
287  const simb::MCParticle* part = (*ipart).second;
288  assert(part != 0);
289  int pdg = abs(part->PdgCode());
290  int trackID = part->TrackId();
291  art::Ptr<simb::MCTruth> theTruth = pi_serv->TrackIdToMCTruth_P(trackID);
292  if (fSkipCosmics && theTruth->Origin() == simb::kCosmicRay) continue;
293  if (theTruth->Origin() == simb::kBeamNeutrino) isNeutrino = true;
294  if (theTruth->Origin() == simb::kSingleParticle) isSingleParticle = true;
295  float KE = 1000 * (part->E() - part->Mass());
296 
297  // Get the neutrino track ID. Assume that there is only one neutrino
298  // interaction and it is first in the list of BeamNeutrino particles
299  if ((isNeutrino || isSingleParticle) && neutTrackID < 0) {
300  neutTrackID = trackID;
301  if (isNeutrino) {
302  simb::MCNeutrino theNeutrino = theTruth->GetNeutrino();
303  neutEnergy = 1000. * theNeutrino.Nu().E();
304  neutIntType = theNeutrino.InteractionType();
305  neutCCNC = theNeutrino.CCNC();
306  }
307 
308  for (unsigned short iv = 0; iv < recoVtxList.size(); ++iv) {
309  recoVtxList[iv]->XYZ(xyz);
310  fNuVtx_dx->Fill(part->Vx() - xyz[0]);
311  fNuVtx_dy->Fill(part->Vy() - xyz[1]);
312  fNuVtx_dz->Fill(part->Vz() - xyz[2]);
313  } // iv
314  } // theTruth->Origin() == simb::kBeamNeutrino && neutTrackID <
315 
316  if (fPrintLevel > 3)
317  mf::LogVerbatim("ClusterAna") << "Origin " << theTruth->Origin() << " trackID " << trackID
318  << " PDG " << part->PdgCode() << " KE " << (int)KE << " MeV "
319  << " neutTrackID " << neutTrackID << " Mother "
320  << part->Mother() << " Proc " << part->Process();
321 
322  bool isCharged = (pdg == 11) || (pdg == 13) || (pdg == 211) || (pdg == 321) || (pdg == 2212);
323 
324  if (!isCharged) continue;
325 
326  // KE (MeV) cuts
327  if (pdg == 11) {
328  if (fElecKERange[0] < 0) continue;
329  // only allow primary electrons
330  if (part->Process() != "primary") continue;
331  if (KE < fElecKERange[0] || KE > fElecKERange[1]) continue;
332  }
333  if (pdg == 13) {
334  if (fMuonKERange[0] < 0) continue;
335  if (KE < fMuonKERange[0] || KE > fMuonKERange[1]) continue;
336  }
337  if (pdg == 211) {
338  if (fPionKERange[0] < 0) continue;
339  if (KE < fPionKERange[0] || KE > fPionKERange[1]) continue;
340  }
341  if (pdg == 321) {
342  if (fKaonKERange[0] < 0) continue;
343  if (KE < fKaonKERange[0] || KE > fKaonKERange[1]) continue;
344  }
345  if (pdg == 2212) {
346  if (fProtKERange[0] < 0) continue;
347  if (KE < fProtKERange[0] || KE > fProtKERange[1]) continue;
348  }
349  // ignore secondaries from neutron interactions
350  if (part->Process() == "NeutronInelastic") continue;
351  plist2.push_back(part);
352  tidlist.push_back(trackID);
353  // initialize the true->(cluster,plane) association
354  std::vector<short> temp{-1, -1, -1};
355  truToCl.push_back(temp);
356  // initialize the true hit count
357  std::vector<unsigned short> temp2(3);
358  nTruHitInCl.push_back(temp2);
359 
360  if (fPrintLevel > 2)
361  mf::LogVerbatim("ClusterAna")
362  << "plist2[" << plist2.size() - 1 << "]"
363  << " Origin " << theTruth->Origin() << " trackID " << trackID << " PDG "
364  << part->PdgCode() << " KE " << (int)KE << " Mother " << part->Mother() + neutTrackID - 1
365  << " Proc " << part->Process();
366  }
367 
368  if (plist2.size() == 0) return;
369 
370  // get the hits (in all planes) that are matched to the true tracks
371  hlist2 = bt_serv->TrackIdsToHits_Ps(clockData, tidlist, allhits);
372  if (hlist2.size() != plist2.size()) {
373  mf::LogError("ClusterAna") << "MC particle list size " << plist2.size()
374  << " != size of MC particle true hits lists " << hlist2.size();
375  return;
376  }
377  tidlist.clear();
378 
379  // vector of (mother, daughter) pairs
380  std::vector<std::pair<unsigned short, unsigned short>> moda;
381  // Deal with mother-daughter tracks
382  if (fMergeDaughters && neutTrackID >= 0) {
383  // Assume that daughters appear later in the list. Step backwards
384  // to accumulate all generations of daughters
385  unsigned short ii, dpl, jj, jpl, kpl;
386  for (ii = 0; ii < plist2.size(); ++ii) {
387  dpl = plist2.size() - 1 - ii;
388  // no mother
389  if (plist2[dpl]->Mother() == 0) continue;
390  // electron
391  if (abs(plist2[dpl]->PdgCode()) == 11) continue;
392  // the actual mother trackID is offset from the neutrino trackID
393  int motherID = neutTrackID + plist2[dpl]->Mother() - 1;
394  // ensure that we are only looking at BeamNeutrino or single particle daughters
395  if (motherID != neutTrackID) continue;
396  // count the number of daughters
397  int ndtr = 0;
398  for (kpl = 0; kpl < plist2.size(); ++kpl) {
399  if (plist2[kpl]->Mother() == motherID) ++ndtr;
400  }
401  // require only one daughter
402  if (ndtr > 1) continue;
403  // find the mother in the list
404  int mpl = -1;
405  for (jj = 0; jj < plist2.size(); ++jj) {
406  jpl = plist2.size() - 1 - jj;
407  if (plist2[jpl]->TrackId() == motherID) {
408  mpl = jpl;
409  break;
410  }
411  } // jpl
412  // mother not found for some reason
413  if (mpl < 0) {
414  mf::LogVerbatim("ClusterAna")
415  << " mother of daughter " << dpl << " not found. mpl = " << mpl;
416  continue;
417  }
418  // ensure that PDG code for mother and daughter are the same
419  if (plist2[dpl]->PdgCode() != plist2[mpl]->PdgCode()) continue;
420  moda.push_back(std::make_pair(mpl, dpl));
421  } // dpl
422  } // MergeDaughters
423 
424  // Now match reconstructed clusters to true particles.
426  for (unsigned int ii = 0; ii < clusterListHandle->size(); ++ii) {
427  art::Ptr<recob::Cluster> clusterHolder(clusterListHandle, ii);
428  clusters.push_back(clusterHolder);
429  }
430 
431  fNClusters->Fill(clusterListHandle->size());
432  nRecHitInCl.resize(clusters.size());
433 
434  // get the plane from the view. Perhaps there is a method that does
435  // this somewhere...
436  std::map<geo::View_t, unsigned int> ViewToPlane;
437  for (unsigned int plane = 0; plane < geom->Nplanes(); ++plane) {
438  geo::View_t view = geom->Plane(plane).View();
439  ViewToPlane[view] = plane;
440  }
441  for (size_t icl = 0; icl < clusters.size(); ++icl) {
442  unsigned int plane = ViewToPlane[clusters[icl]->View()];
443  std::vector<art::Ptr<recob::Hit>> cluhits = fmh.at(icl);
444  fNHitInCluster->Fill(cluhits.size());
445  nRecHitInCl[icl] = cluhits.size();
446  // count the number of hits matched to each true particle in plist2
447  std::vector<unsigned short> nHitInPl2(plist2.size());
448  for (size_t iht = 0; iht < cluhits.size(); ++iht) {
449 
450  // look for this hit in all of the truth hit lists
451  short hitInPl2 = -1;
452  for (unsigned short ipl = 0; ipl < plist2.size(); ++ipl) {
453  unsigned short imat = 0;
454  for (imat = 0; imat < hlist2[ipl].size(); ++imat) {
455  if (cluhits[iht] == hlist2[ipl][imat]) break;
456  } // imat
457  if (imat < hlist2[ipl].size()) {
458  hitInPl2 = ipl;
459  break;
460  }
461  } // ipl
462  if (hitInPl2 < 0) continue;
463  // Assign the hit count to the mother if this is a daughter.
464  // Mother-daughter pairs are entered in the moda vector in reverse
465  // order, so assign daughter hits to the highest generation mother.
466  for (unsigned short imd = 0; imd < moda.size(); ++imd) {
467  if (moda[imd].second == hitInPl2) hitInPl2 = moda[imd].first;
468  }
469  // count
470  ++nHitInPl2[hitInPl2];
471  } // iht
472  // Associate the cluster with the truth particle that has the highest
473  // number of cluster hits
474  unsigned short nhit = 0;
475  short imtru = -1;
476  for (unsigned int ipl = 0; ipl < nHitInPl2.size(); ++ipl) {
477  if (nHitInPl2[ipl] > nhit) {
478  nhit = nHitInPl2[ipl];
479  imtru = ipl;
480  }
481  } // ipl
482  // make the cluster->(true,plane) association and save the
483  // number of true hits in the cluster
484  if (imtru != -1) {
485  // clobber a previously made association?
486  if (nhit > nTruHitInCl[imtru][plane]) {
487  truToCl[imtru][plane] = icl;
488  nTruHitInCl[imtru][plane] = nhit;
489  }
490  } // imtru != 1
491  } // icl
492 
493  // ready to calculate Efficiency, Purity in each plane and EP2
494  for (unsigned short ipl = 0; ipl < plist2.size(); ++ipl) {
495  // ignore daughters
496  bool skipit = false;
497  for (unsigned short ii = 0; ii < moda.size(); ++ii) {
498  if (moda[ii].second == ipl) {
499  skipit = true;
500  break;
501  }
502  } // ii
503  if (skipit) continue;
504  // ignore true particles with few true hits. Outside the detector
505  // or not reconstructable?
506  if (hlist2[ipl].size() < 3) continue;
507 
508  int trackID = plist2[ipl]->TrackId();
509  art::Ptr<simb::MCTruth> theTruth = pi_serv->TrackIdToMCTruth_P(trackID);
510  bool isCosmic = (theTruth->Origin() == simb::kCosmicRay);
511  float KE = 1000 * (plist2[ipl]->E() - plist2[ipl]->Mass());
512  int PDG = abs(plist2[ipl]->PdgCode());
513 
514  std::vector<short> nTru(geom->Nplanes());
515  std::vector<short> nRec(geom->Nplanes());
516  std::vector<short> nTruRec(geom->Nplanes());
517  std::vector<float> eff(geom->Nplanes());
518  std::vector<float> pur(geom->Nplanes());
519  std::vector<float> ep(geom->Nplanes());
520  for (unsigned int plane = 0; plane < geom->Nplanes(); ++plane) {
521  // count the number of true hits in this plane for the true particle.
522  // First count the mother hits
523  for (unsigned short ii = 0; ii < hlist2[ipl].size(); ++ii) {
524  if (ViewToPlane[hlist2[ipl][ii]->View()] == plane) ++nTru[plane];
525  } // ii
526 
527  // next look for daughters and count those hits in all generations
528  unsigned short mom = ipl;
529  std::vector<std::pair<unsigned short, unsigned short>>::reverse_iterator rit =
530  moda.rbegin();
531  while (rit != moda.rend()) {
532  if ((*rit).first == mom) {
533  unsigned short dau = (*rit).second;
534  for (unsigned short jj = 0; jj < hlist2[dau].size(); ++jj) {
535  if (ViewToPlane[hlist2[dau][jj]->View()] == plane) ++nTru[plane];
536  } // jj
537  // It is likely that one hit appears in the mother list
538  // as well as the daughter list, so subtract one from the count
539  mom = (*rit).second;
540  } // (*rit).first == mom
541  ++rit;
542  } // rit
543 
544  if (nTru[plane] == 0) continue;
545  // Ignore short truth clusters
546  if (nTru[plane] < 3) continue;
547  short icl = truToCl[ipl][plane];
548  if (icl < 0) { nRec[plane] = 0; }
549  else {
550  nRec[plane] = nRecHitInCl[icl];
551  }
552  nTruRec[plane] = nTruHitInCl[ipl][plane];
553  eff[plane] = 0;
554  pur[plane] = 0;
555  if (nTru[plane] > 0) eff[plane] = (float)nTruRec[plane] / (float)nTru[plane];
556  if (nRec[plane] > 0) pur[plane] = (float)nTruRec[plane] / (float)nRec[plane];
557  // do this to prevent histogram under/over flow
558  if (eff[plane] == 0) { eff[plane] = 0.001; }
559  if (pur[plane] == 0) { pur[plane] = 0.001; }
560  if (eff[plane] >= 1) { eff[plane] = 0.999; }
561  if (pur[plane] >= 1) { pur[plane] = 0.999; }
562  ep[plane] = eff[plane] * pur[plane];
563  if (fPrintLevel == -1)
564  outFile << moduleID << " " << evt.id().event() << " " << trackID << " " << PDG << " "
565  << (int)KE << " " << plane << " " << nTru[plane] << " " << std::setprecision(3)
566  << eff[plane] << " " << pur[plane] << "\n";
567  } // plane
568  // sort the ep values in ascending order
569  std::vector<float> temp;
570  temp = ep;
571  std::sort(temp.begin(), temp.end());
572  // EP2 is the second highest value
573  unsigned short ii = temp.size() - 2;
574  float ep2 = temp[ii];
575  // find the plane that defined EP2
576  short ep2Plane = 0;
577  short ep2Cluster = 0;
578  for (unsigned short jj = 0; jj < temp.size(); ++jj) {
579  if (ep[jj] == ep2) {
580  ep2Plane = jj;
581  ep2Cluster = truToCl[ipl][ep2Plane];
582  break;
583  }
584  } // jj
585  // find the US and DS ends of the cluster for printing
586  std::array<double, 2> clBeg, clEnd;
587  if (ep2Cluster >= 0) {
588  clBeg[0] = clusters[ep2Cluster]->StartWire();
589  clBeg[1] = clusters[ep2Cluster]->StartTick();
590  clEnd[0] = clusters[ep2Cluster]->EndWire();
591  clEnd[1] = clusters[ep2Cluster]->EndTick();
592  }
593  else {
594  clBeg.fill(0.);
595  clEnd.fill(0.);
596  }
597  // fill histograms
598  if (isCosmic) {
599  fCREP2->Fill(ep2);
600  fCRE->Fill(eff[ep2Plane]);
601  fCRP->Fill(pur[ep2Plane]);
602  aveCREP2 += ep2;
603  numCREP2 += 1.;
604  if (fPrintLevel > 1)
605  mf::LogVerbatim("ClusterAna")
606  << ">>>CREP2 " << std::fixed << std::setprecision(2) << ep2 << " E " << eff[ep2Plane]
607  << std::setprecision(2) << " P " << pur[ep2Plane] << " P:W:T " << ep2Plane << ":"
608  << (int)clBeg[0] << ":" << (int)clBeg[1] << "-" << ep2Plane << ":" << (int)clEnd[0]
609  << ":" << (int)clEnd[1] << " PDG " << PDG << " KE " << (int)KE << " MeV";
610  } // isCosmic
611  else {
612  float wght = 1.;
613  if (fTrackWeightOption == 1) wght = KE;
614  // accumulate statistics for muons and not-muons
615  if (PDG == 13) {
616  aveNuEP2mu += ep2 * wght;
617  numNuEP2mu += wght;
618  }
619  else {
620  aveNuEP2nm += ep2 * wght;
621  numNuEP2nm += wght;
622  }
623  if (PDG == 11) {
624  fNuKE_elec->Fill(KE, wght);
625  fNuE_elec->Fill(eff[ep2Plane], wght);
626  fNuP_elec->Fill(pur[ep2Plane], wght);
627  fNuEP2_elec->Fill(ep2, wght);
628  fNuEP2_KE_elec->Fill(KE, ep2, wght);
629  }
630  else if (PDG == 13) {
631  fNuKE_muon->Fill(KE, wght);
632  fNuE_muon->Fill(eff[ep2Plane], wght);
633  fNuP_muon->Fill(pur[ep2Plane], wght);
634  fNuEP2_muon->Fill(ep2, wght);
635  fNuEP2_KE_muon->Fill(KE, ep2, wght);
636  }
637  else if (PDG == 211) {
638  fNuKE_pion->Fill(KE, wght);
639  fNuE_pion->Fill(eff[ep2Plane], wght);
640  fNuP_pion->Fill(pur[ep2Plane], wght);
641  fNuEP2_pion->Fill(ep2, wght);
642  fNuEP2_KE_pion->Fill(KE, ep2, wght);
643  }
644  else if (PDG == 321) {
645  fNuKE_kaon->Fill(KE, wght);
646  fNuE_kaon->Fill(eff[ep2Plane], wght);
647  fNuP_kaon->Fill(pur[ep2Plane], wght);
648  fNuEP2_kaon->Fill(ep2, wght);
649  fNuEP2_KE_kaon->Fill(KE, ep2, wght);
650  }
651  else if (PDG == 2212) {
652  fNuKE_prot->Fill(KE, wght);
653  fNuE_prot->Fill(eff[ep2Plane], wght);
654  fNuP_prot->Fill(pur[ep2Plane], wght);
655  fNuEP2_prot->Fill(ep2, wght);
656  fNuEP2_KE_prot->Fill(KE, ep2, wght);
657  }
658  if (fPrintLevel > 1)
659  mf::LogVerbatim("ClusterAna")
660  << ">>>NuEP2 " << std::fixed << std::setprecision(2) << ep2 << " E " << eff[ep2Plane]
661  << std::setprecision(2) << " P " << pur[ep2Plane] << " P:W:T " << ep2Plane << ":"
662  << (int)clBeg[0] << ":" << (int)clBeg[1] << "-" << ep2Plane << ":" << (int)clEnd[0]
663  << ":" << (int)clEnd[1] << " PDG " << PDG << " KE " << (int)KE << " MeV ";
664  if (fPrintLevel > 2) {
665  // print out the begin/end true hits
666  mf::LogVerbatim mfp("ClusterAna");
667  mfp << " Truth P:W:T ";
668  for (unsigned int plane = 0; plane < geom->Nplanes(); ++plane) {
669  unsigned short loW = 9999;
670  unsigned short loT = 0;
671  unsigned short hiW = 0;
672  unsigned short hiT = 0;
673  for (unsigned short ii = 0; ii < hlist2[ipl].size(); ++ii) {
674  if (ViewToPlane[hlist2[ipl][ii]->View()] == plane) {
675  art::Ptr<recob::Hit> theHit = hlist2[ipl][ii];
676  if (theHit->WireID().Wire < loW) {
677  loW = theHit->WireID().Wire;
678  loT = theHit->PeakTime();
679  }
680  if (theHit->WireID().Wire > hiW) {
681  hiW = theHit->WireID().Wire;
682  hiT = theHit->PeakTime();
683  }
684  } // correct view
685  } // ii
686  mfp << plane << ":" << loW << ":" << loT << "-" << plane << ":" << hiW << ":" << hiT
687  << " ";
688  } // plane
689  } // fPrintLevel > 2
690  } // !isCosmic
691  } // ipl
692 
693  float ave1 = -1.;
694  if (numNuEP2mu > 0.) ave1 = aveNuEP2mu / numNuEP2mu;
695 
696  float ave2 = -1.;
697  if (numNuEP2nm > 0.) ave2 = aveNuEP2nm / numNuEP2nm;
698 
699  float ave3 = -1.;
700  if (numCREP2 > 0.) ave3 = aveCREP2 / numCREP2;
701 
702  if (fPrintLevel > 0) {
703  std::string nuType = "Other";
704  if (neutCCNC == simb::kCC) {
705  if (neutIntType == 1001) nuType = "CCQE";
706  if (neutIntType == 1091) nuType = "DIS";
707  if (neutIntType == 1097) nuType = "COH";
708  if (neutIntType > 1002 && neutIntType < 1091) nuType = "RES";
709  }
710  else if (neutCCNC == simb::kNC) {
711  nuType = "NC";
712  }
713  else {
714  nuType = "Unknown";
715  }
716  mf::LogVerbatim("ClusterAna")
717  << "EvtEP2 " << evt.id().event() << " NuType " << nuType << " Enu " << std::fixed
718  << std::setprecision(0) << neutEnergy << std::right << std::fixed << std::setprecision(2)
719  << " NuMuons " << ave1 << " NuPiKp " << ave2 << " CosmicRays " << ave3 << " CCNC "
720  << neutCCNC << " IntType " << neutIntType;
721  }
722  } // analyze
723 
724 } // end namespace
725 
double E(const int i=0) const
Definition: MCParticle.h:233
void XYZ(double *xyz) const
Legacy method to access vertex position, preserved to avoid breaking code. Please try to use Vertex::...
Definition: Vertex.cxx:36
MaybeLogger_< ELseverityLevel::ELsev_info, true > LogVerbatim
int PdgCode() const
Definition: MCParticle.h:212
int CCNC() const
Definition: MCNeutrino.h:148
std::string fHitsModuleLabel
const simb::MCNeutrino & GetNeutrino() const
Definition: MCTruth.h:77
PlaneGeo const & Plane(unsigned int const p, unsigned int const tpc=0, unsigned int const cstat=0) const
Returns the specified wire.
AdcChannelData::View View
enum geo::_plane_proj View_t
Enumerate the possible plane projections.
std::string string
Definition: nybbler.cc:12
geo::WireID WireID() const
Definition: Hit.h:233
int Mother() const
Definition: MCParticle.h:213
const simb::MCParticle & Nu() const
Definition: MCNeutrino.h:146
simb::Origin_t Origin() const
Definition: MCTruth.h:74
double Mass() const
Definition: MCParticle.h:239
STL namespace.
WireID_t Wire
Index of the wire within its plane.
Definition: geo_types.h:580
intermediate_table::const_iterator const_iterator
MaybeLogger_< ELseverityLevel::ELsev_error, false > LogError
Cluster finding and building.
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
std::string Process() const
Definition: MCParticle.h:215
Q_EXPORT QTSManip setprecision(int p)
Definition: qtextstream.h:343
art framework interface to geometry description
void analyze(const art::Event &evt) override
int TrackId() const
Definition: MCParticle.h:210
std::vector< float > fElecKERange
bool isValid() const noexcept
Definition: Handle.h:191
const uint PDG
Definition: qregexp.cpp:140
decltype(auto) constexpr size(T &&obj)
ADL-aware version of std::size.
Definition: StdUtils.h:92
int InteractionType() const
Definition: MCNeutrino.h:150
T abs(T value)
View_t View() const
Which coordinate does this plane measure.
Definition: PlaneGeo.h:184
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
unsigned int Nplanes(unsigned int tpc=0, unsigned int cstat=0) const
Returns the total number of wire planes in the specified TPC.
int PdgCode(Resonance_t res, int Q)
(resonance id, charge) -> PDG code
ClusterAna(fhicl::ParameterSet const &pset)
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
fileName
Definition: dumpTree.py:9
void push_back(Ptr< U > const &p)
Definition: PtrVector.h:435
const art::Ptr< simb::MCTruth > & TrackIdToMCTruth_P(int id) const
single particles thrown at the detector
Definition: MCTruth.h:26
uint8_t nhit
Definition: CRTFragment.hh:201
std::vector< float > fProtKERange
std::vector< float > fKaonKERange
std::vector< std::vector< art::Ptr< recob::Hit > > > TrackIdsToHits_Ps(detinfo::DetectorClocksData const &clockData, std::vector< int > const &tkIds, std::vector< art::Ptr< recob::Hit >> const &hitsIn) const
std::string fVertexModuleLabel
void endJob() override
size_type size() const
Definition: PtrVector.h:302
void beginJob() override
const sim::ParticleList & ParticleList() const
double Vx(const int i=0) const
Definition: MCParticle.h:221
float PeakTime() const
Time of the signal peak, in tick units.
Definition: Hit.h:218
Declaration of signal hit object.
Encapsulate the construction of a single detector plane.
double Vz(const int i=0) const
Definition: MCParticle.h:223
EventNumber_t event() const
Definition: EventID.h:116
std::string fClusterModuleLabel
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void fill_ptr_vector(std::vector< Ptr< T >> &ptrs, H const &h)
Definition: Ptr.h:297
second_as<> second
Type of time stored in seconds, in double precision.
Definition: spacetime.h:85
Event generator information.
Definition: MCNeutrino.h:18
std::vector< float > fPionKERange
int bool
Definition: qglobal.h:345
std::vector< float > fMuonKERange
EventID id() const
Definition: Event.cc:34
double Vy(const int i=0) const
Definition: MCParticle.h:222
Cosmic rays.
Definition: MCTruth.h:24
Beam neutrinos.
Definition: MCTruth.h:23
vertex reconstruction