LArSoft  v10_04_05
Liquid Argon Software toolkit - https://larsoft.org/
SeedAna_module.cc
Go to the documentation of this file.
1 //
2 // Name: SeedAna_module.cc
3 //
4 // Purpose: Module SeedAna.
5 //
6 // Configuration parameters.
7 //
8 // SeedModuleLabel: Seed module label.
9 // MCTrackModuleLabel: MCTrack module label.
10 // MinMCKE: Minimum MC particle kinetic energy.
11 // MatchColinearity: Minimum colinearity for mc-seed matching.
12 // MatchDisp: Maximum uv displacement for mc-seed matching.
13 //
14 // Created: 2-Aug-2011 H. Greenlee
15 //
16 
17 #include <cmath>
18 #include <iomanip>
19 #include <map>
20 #include <sstream>
21 
25 #include "art_root_io/TFileService.h"
27 #include "cetlib_except/exception.h"
29 
34 
35 #include "TH1F.h"
36 #include "TH2F.h"
37 #include "TMatrixD.h"
38 
39 namespace {
40 
41  // Calculate distance to boundary.
42  //----------------------------------------------------------------------------
43  double bdist(const TVector3& pos)
44  {
45  // Get geometry.
46 
47  auto const& tpc = art::ServiceHandle<geo::Geometry const>()->TPC({0, 0});
48 
49  double d1 = pos.X(); // Distance to right side (wires).
50  double d2 = 2. * tpc.HalfWidth() - pos.X(); // Distance to left side (cathode).
51  double d3 = pos.Y() + tpc.HalfHeight(); // Distance to bottom.
52  double d4 = tpc.HalfHeight() - pos.Y(); // Distance to top.
53  double d5 = pos.Z(); // Distance to front.
54  double d6 = tpc.Length() - pos.Z(); // Distance to back.
55 
56  return std::min({d1, d2, d3, d4, d5, d6});
57  }
58 
59  // Find the closest matching mc trajectory point (sim::MCStep) for a given seed.
60  // Returned value is index of the trajectory point.
61  // Return -1 in case of no match.
62  int mcmatch(detinfo::DetectorPropertiesData const& detProp,
63  const sim::MCTrack& mctrk,
64  const recob::Seed& seed)
65  {
66  // Get seed point.
67 
68  double pos[3];
69  double err[3];
70  seed.GetPoint(pos, err);
71 
72  // Calculate the x offset due to nonzero mc particle time.
73  double mctime = mctrk.Start().T(); // nsec
74  double mcdx = mctime * 1.e-3 * detProp.DriftVelocity(); // cm
75 
76  // Loop over trajectory points.
77 
78  int best_traj = -1;
79  double max_dist = 0.;
80  int ntraj = mctrk.size();
81  for (int itraj = 0; itraj < ntraj; ++itraj) {
82  const TLorentzVector& vec = mctrk[itraj].Position();
83  double dx = pos[0] - vec.X() - mcdx;
84  double dy = pos[1] - vec.Y();
85  double dz = pos[2] - vec.Z();
86  double dist = std::sqrt(dx * dx + dy * dy + dz * dz);
87  if (best_traj < 0 || dist < max_dist) {
88  best_traj = itraj;
89  max_dist = dist;
90  }
91  }
92  return best_traj;
93  }
94 
95  // Length of MCTrack.
96  // In this function, the extracted start and end momenta are converted to GeV
97  // (MCTrack stores momenta in Mev).
98  //----------------------------------------------------------------------------
99  double length(detinfo::DetectorPropertiesData const& detProp,
100  const sim::MCTrack& mctrk,
101  double dx,
102  TVector3& start,
103  TVector3& end,
104  TVector3& startmom,
105  TVector3& endmom)
106  {
107  // Get services.
108 
109  auto const& tpc = art::ServiceHandle<geo::Geometry const>()->TPC({0, 0});
110 
111  // Get fiducial volume boundary.
112 
113  double xmin = 0.;
114  double xmax = 2. * tpc.HalfWidth();
115  double ymin = -tpc.HalfHeight();
116  double ymax = tpc.HalfHeight();
117  double zmin = 0.;
118  double zmax = tpc.Length();
119  double result = 0.;
120  TVector3 disp;
121  int n = mctrk.size();
122  bool first = true;
123 
124  for (int i = 0; i < n; ++i) {
125  TVector3 pos = mctrk[i].Position().Vect();
126 
127  // Make fiducial cuts. Require the particle to be within the physical volume of
128  // the tpc, and also require the apparent x position to be within the expanded
129  // readout frame.
130 
131  if (pos.X() >= xmin && pos.X() <= xmax && pos.Y() >= ymin && pos.Y() <= ymax &&
132  pos.Z() >= zmin && pos.Z() <= zmax) {
133  pos[0] += dx;
134  double ticks = detProp.ConvertXToTicks(pos[0], 0, 0, 0);
135  if (ticks >= 0. && ticks < detProp.ReadOutWindowSize()) {
136  if (first) {
137  start = pos;
138  startmom = 0.001 * mctrk[i].Momentum().Vect();
139  }
140  else {
141  disp -= pos;
142  result += disp.Mag();
143  }
144  first = false;
145  disp = pos;
146  end = pos;
147  endmom = 0.001 * mctrk[i].Momentum().Vect();
148  }
149  }
150  }
151 
152  return result;
153  }
154 
155  // Fill efficiency histogram assuming binomial errors.
156 
157  void effcalc(const TH1* hnum, const TH1* hden, TH1* heff)
158  {
159  int nbins = hnum->GetNbinsX();
160  if (nbins != hden->GetNbinsX())
161  throw cet::exception("SeedAna") << "effcalc[" __FILE__ "]: incompatible histograms (I)\n";
162  if (nbins != heff->GetNbinsX())
163  throw cet::exception("SeedAna") << "effcalc[" __FILE__ "]: incompatible histograms (II)\n";
164 
165  // Loop over bins, including underflow and overflow.
166 
167  for (int ibin = 0; ibin <= nbins + 1; ++ibin) {
168  double num = hnum->GetBinContent(ibin);
169  double den = hden->GetBinContent(ibin);
170  if (den == 0.) {
171  heff->SetBinContent(ibin, 0.);
172  heff->SetBinError(ibin, 0.);
173  }
174  else {
175  double eff = num / den;
176  if (eff < 0.) eff = 0.;
177  if (eff > 1.) eff = 1.;
178  double err = std::sqrt(eff * (1. - eff) / den);
179  heff->SetBinContent(ibin, eff);
180  heff->SetBinError(ibin, err);
181  }
182  }
183 
184  heff->SetMinimum(0.);
185  heff->SetMaximum(1.05);
186  heff->SetMarkerStyle(20);
187  }
188 
189  // Fill multiplicity histogram.
190 
191  void mulcalc(const TH1* hnum, const TH1* hden, TH1* hmul)
192  {
193  int nbins = hnum->GetNbinsX();
194  if (nbins != hden->GetNbinsX())
195  throw cet::exception("SeedAna") << "mulcalc[" __FILE__ "]: incompatible histograms (I)\n";
196  if (nbins != hmul->GetNbinsX())
197  throw cet::exception("SeedAna") << "mulcalc[" __FILE__ "]: incompatible histograms (II)\n";
198 
199  // Loop over bins, including underflow and overflow.
200 
201  for (int ibin = 0; ibin <= nbins + 1; ++ibin) {
202  double num = hnum->GetBinContent(ibin);
203  double den = hden->GetBinContent(ibin);
204  if (den == 0.) {
205  hmul->SetBinContent(ibin, 0.);
206  hmul->SetBinError(ibin, 0.);
207  }
208  else {
209  double mul = num / den;
210  if (mul < 0.) mul = 0.;
211  double err = std::sqrt((1. + mul) * mul / den);
212  hmul->SetBinContent(ibin, mul);
213  hmul->SetBinError(ibin, err);
214  }
215  }
216 
217  hmul->SetMinimum(0.);
218  hmul->SetMarkerStyle(20);
219  }
220 }
221 
222 namespace trkf {
223 
224  class SeedAna : public art::EDAnalyzer {
225  public:
226  // Embedded structs.
227 
228  // Struct for histograms that depend on seeds only.
229 
230  struct RecoHists {
231  RecoHists(const std::string& subdir);
232 
233  // Pure reco seed histograms.
234 
235  TH1F* fHx{nullptr}; // Seed x position.
236  TH1F* fHy{nullptr}; // Seed y position.
237  TH1F* fHz{nullptr}; // Seed z position.
238  TH1F* fHdist{nullptr}; // Seed distance to boundary.
239  TH1F* fHtheta{nullptr}; // Theta.
240  TH1F* fHphi{nullptr}; // Phi.
241  TH1F* fHtheta_xz{nullptr}; // Theta_xz.
242  TH1F* fHtheta_yz{nullptr}; // Theta_yz.
243  };
244 
245  // Struct for mc particles and mc-matched tracks.
246 
247  struct MCHists {
248  MCHists(const std::string& subdir);
249 
250  // Reco-MC matching.
251 
252  TH2F* fHduvcosth{nullptr}; // 2D mc vs. data matching, duv vs. cos(theta).
253  TH1F* fHcosth{nullptr}; // 1D direction matching, cos(theta).
254  TH1F* fHmcu{nullptr}; // 1D endpoint truth u.
255  TH1F* fHmcv{nullptr}; // 1D endpoint truth v.
256  TH1F* fHmcw{nullptr}; // 1D endpoint truth w.
257  TH1F* fHmcdudw{nullptr}; // Truth du/dw.
258  TH1F* fHmcdvdw{nullptr}; // Truth dv/dw.
259 
260  // Pure MC particle histograms (efficiency denominator).
261 
262  TH1F* fHmcstartx{nullptr}; // Starting x position.
263  TH1F* fHmcstarty{nullptr}; // Starting y position.
264  TH1F* fHmcstartz{nullptr}; // Starting z position.
265  TH1F* fHmcendx{nullptr}; // Ending x position.
266  TH1F* fHmcendy{nullptr}; // Ending y position.
267  TH1F* fHmcendz{nullptr}; // Ending z position.
268  TH1F* fHmctheta{nullptr}; // Theta.
269  TH1F* fHmcphi{nullptr}; // Phi.
270  TH1F* fHmctheta_xz{nullptr}; // Theta_xz.
271  TH1F* fHmctheta_yz{nullptr}; // Theta_yz.
272  TH1F* fHmcmom{nullptr}; // Momentum.
273  TH1F* fHmclen{nullptr}; // Length.
274 
275  // Matched seed histograms (multiplicity numerator).
276 
277  TH1F* fHmstartx{nullptr}; // Starting x position.
278  TH1F* fHmstarty{nullptr}; // Starting y position.
279  TH1F* fHmstartz{nullptr}; // Starting z position.
280  TH1F* fHmendx{nullptr}; // Ending x position.
281  TH1F* fHmendy{nullptr}; // Ending y position.
282  TH1F* fHmendz{nullptr}; // Ending z position.
283  TH1F* fHmtheta{nullptr}; // Theta.
284  TH1F* fHmphi{nullptr}; // Phi.
285  TH1F* fHmtheta_xz{nullptr}; // Theta_xz.
286  TH1F* fHmtheta_yz{nullptr}; // Theta_yz.
287  TH1F* fHmmom{nullptr}; // Momentum.
288  TH1F* fHmlen{nullptr}; // Length.
289 
290  // Matched seed histograms (efficiency numerator).
291 
292  TH1F* fHgstartx{nullptr}; // Starting x position.
293  TH1F* fHgstarty{nullptr}; // Starting y position.
294  TH1F* fHgstartz{nullptr}; // Starting z position.
295  TH1F* fHgendx{nullptr}; // Ending x position.
296  TH1F* fHgendy{nullptr}; // Ending y position.
297  TH1F* fHgendz{nullptr}; // Ending z position.
298  TH1F* fHgtheta{nullptr}; // Theta.
299  TH1F* fHgphi{nullptr}; // Phi.
300  TH1F* fHgtheta_xz{nullptr}; // Theta_xz.
301  TH1F* fHgtheta_yz{nullptr}; // Theta_yz.
302  TH1F* fHgmom{nullptr}; // Momentum.
303  TH1F* fHglen{nullptr}; // Length.
304 
305  // Multiplicity histograms.
306 
307  TH1F* fHmulstartx{nullptr}; // Starting x position.
308  TH1F* fHmulstarty{nullptr}; // Starting y position.
309  TH1F* fHmulstartz{nullptr}; // Starting z position.
310  TH1F* fHmulendx{nullptr}; // Ending x position.
311  TH1F* fHmulendy{nullptr}; // Ending y position.
312  TH1F* fHmulendz{nullptr}; // Ending z position.
313  TH1F* fHmultheta{nullptr}; // Theta.
314  TH1F* fHmulphi{nullptr}; // Phi.
315  TH1F* fHmultheta_xz{nullptr}; // Theta_xz.
316  TH1F* fHmultheta_yz{nullptr}; // Theta_yz.
317  TH1F* fHmulmom{nullptr}; // Momentum.
318  TH1F* fHmullen{nullptr}; // Length.
319 
320  // Efficiency histograms.
321 
322  TH1F* fHestartx{nullptr}; // Starting x position.
323  TH1F* fHestarty{nullptr}; // Starting y position.
324  TH1F* fHestartz{nullptr}; // Starting z position.
325  TH1F* fHeendx{nullptr}; // Ending x position.
326  TH1F* fHeendy{nullptr}; // Ending y position.
327  TH1F* fHeendz{nullptr}; // Ending z position.
328  TH1F* fHetheta{nullptr}; // Theta.
329  TH1F* fHephi{nullptr}; // Phi.
330  TH1F* fHetheta_xz{nullptr}; // Theta_xz.
331  TH1F* fHetheta_yz{nullptr}; // Theta_yz.
332  TH1F* fHemom{nullptr}; // Momentum.
333  TH1F* fHelen{nullptr}; // Length.
334  };
335 
336  explicit SeedAna(fhicl::ParameterSet const& pset);
337 
338  private:
339  void analyze(const art::Event& evt) override;
340  void endJob() override;
341 
342  // Fcl Attributes.
343 
344  std::string fSeedModuleLabel;
345  std::string fMCTrackModuleLabel;
346  int fDump; // Number of events to dump to debug message facility.
347  double fMinMCKE; // Minimum MC particle kinetic energy (GeV).
348  double fMinMCLen; // Minimum MC particle length in tpc (cm).
349  double fMatchColinearity; // Minimum matching colinearity.
350  double fMatchDisp; // Maximum matching displacement.
351  bool fIgnoreSign; // Ignore sign of mc particle if true.
352 
353  // Histograms.
354 
355  std::map<int, MCHists> fMCHistMap; // Indexed by pdg id.
356  std::map<int, RecoHists> fRecoHistMap; // Indexed by pdg id.
357 
358  // Statistics.
359 
361  };
362 
364 
365  // RecoHists methods.
366 
367  SeedAna::RecoHists::RecoHists(const std::string& subdir)
368  {
369  // Get services.
370 
372  auto const& tpc = art::ServiceHandle<geo::Geometry const>()->TPC({0, 0});
373 
374  // Make histogram directory.
375 
376  art::TFileDirectory topdir = tfs->mkdir("seedana", "SeedAna histograms");
377  art::TFileDirectory dir = topdir.mkdir(subdir);
378 
379  // Book histograms.
380 
381  fHx = dir.make<TH1F>("x", "X Position", 100, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
382  fHy = dir.make<TH1F>("y", "Y Position", 100, -tpc.HalfHeight(), tpc.HalfHeight());
383  fHz = dir.make<TH1F>("z", "Z Position", 100, 0., tpc.Length());
384  fHdist = dir.make<TH1F>("dist", "Position Distance to Boundary", 100, -10., tpc.HalfWidth());
385  fHtheta = dir.make<TH1F>("theta", "Theta", 100, 0., 3.142);
386  fHphi = dir.make<TH1F>("phi", "Phi", 100, -3.142, 3.142);
387  fHtheta_xz = dir.make<TH1F>("theta_xz", "Theta_xz", 100, -3.142, 3.142);
388  fHtheta_yz = dir.make<TH1F>("theta_yz", "Theta_yz", 100, -3.142, 3.142);
389  }
390 
391  // MCHists methods.
392 
393  SeedAna::MCHists::MCHists(const std::string& subdir)
394  {
395  // Get services.
396 
398  auto const& tpc = art::ServiceHandle<geo::Geometry const>()->TPC({0, 0});
399 
400  // Make histogram directory.
401 
402  art::TFileDirectory topdir = tfs->mkdir("seedana", "SeedAna histograms");
403  art::TFileDirectory dir = topdir.mkdir(subdir);
404 
405  // Book histograms.
406 
407  fHduvcosth =
408  dir.make<TH2F>("duvcosth", "Delta(uv) vs. Colinearity", 100, 0.95, 1., 100, 0., 1.);
409  fHcosth = dir.make<TH1F>("colin", "Colinearity", 100, 0.95, 1.);
410  fHmcu = dir.make<TH1F>("mcu", "MC Truth U", 100, -5., 5.);
411  fHmcv = dir.make<TH1F>("mcv", "MC Truth V", 100, -5., 5.);
412  fHmcw = dir.make<TH1F>("mcw", "MC Truth W", 100, -20., 20.);
413  fHmcdudw = dir.make<TH1F>("mcdudw", "MC Truth U Slope", 100, -0.2, 0.2);
414  fHmcdvdw = dir.make<TH1F>("mcdvdw", "MV Truth V Slope", 100, -0.2, 0.2);
415 
416  fHmcstartx = dir.make<TH1F>(
417  "mcxstart", "MC X Start Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
418  fHmcstarty =
419  dir.make<TH1F>("mcystart", "MC Y Start Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
420  fHmcstartz = dir.make<TH1F>("mczstart", "MC Z Start Position", 10, 0., tpc.Length());
421  fHmcendx = dir.make<TH1F>(
422  "mcxend", "MC X End Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
423  fHmcendy =
424  dir.make<TH1F>("mcyend", "MC Y End Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
425  fHmcendz = dir.make<TH1F>("mczend", "MC Z End Position", 10, 0., tpc.Length());
426  fHmctheta = dir.make<TH1F>("mctheta", "MC Theta", 20, 0., 3.142);
427  fHmcphi = dir.make<TH1F>("mcphi", "MC Phi", 10, -3.142, 3.142);
428  fHmctheta_xz = dir.make<TH1F>("mctheta_xz", "MC Theta_xz", 40, -3.142, 3.142);
429  fHmctheta_yz = dir.make<TH1F>("mctheta_yz", "MC Theta_yz", 40, -3.142, 3.142);
430  fHmcmom = dir.make<TH1F>("mcmom", "MC Momentum", 10, 0., 10.);
431  fHmclen = dir.make<TH1F>("mclen", "MC Particle Length", 10, 0., 1.1 * tpc.Length());
432 
433  fHmstartx = dir.make<TH1F>(
434  "mxstart", "Matched X Start Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
435  fHmstarty = dir.make<TH1F>(
436  "mystart", "Matched Y Start Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
437  fHmstartz = dir.make<TH1F>("mzstart", "Matched Z Start Position", 10, 0., tpc.Length());
438  fHmendx = dir.make<TH1F>(
439  "mxend", "Matched X End Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
440  fHmendy =
441  dir.make<TH1F>("myend", "Matched Y End Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
442  fHmendz = dir.make<TH1F>("mzend", "Matched Z End Position", 10, 0., tpc.Length());
443  fHmtheta = dir.make<TH1F>("mtheta", "Matched Theta", 20, 0., 3.142);
444  fHmphi = dir.make<TH1F>("mphi", "Matched Phi", 10, -3.142, 3.142);
445  fHmtheta_xz = dir.make<TH1F>("mtheta_xz", "Matched Theta_xz", 40, -3.142, 3.142);
446  fHmtheta_yz = dir.make<TH1F>("mtheta_yz", "Matched Theta_yz", 40, -3.142, 3.142);
447  fHmmom = dir.make<TH1F>("mmom", "Matched Momentum", 10, 0., 10.);
448  fHmlen = dir.make<TH1F>("mlen", "Matched Particle Length", 10, 0., 1.1 * tpc.Length());
449 
450  fHgstartx = dir.make<TH1F>(
451  "gxstart", "Good X Start Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
452  fHgstarty =
453  dir.make<TH1F>("gystart", "Good Y Start Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
454  fHgstartz = dir.make<TH1F>("gzstart", "Good Z Start Position", 10, 0., tpc.Length());
455  fHgendx = dir.make<TH1F>(
456  "gxend", "Good X End Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
457  fHgendy =
458  dir.make<TH1F>("gyend", "Good Y End Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
459  fHgendz = dir.make<TH1F>("gzend", "Good Z End Position", 10, 0., tpc.Length());
460  fHgtheta = dir.make<TH1F>("gtheta", "Good Theta", 20, 0., 3.142);
461  fHgphi = dir.make<TH1F>("gphi", "Good Phi", 10, -3.142, 3.142);
462  fHgtheta_xz = dir.make<TH1F>("gtheta_xz", "Good Theta_xz", 40, -3.142, 3.142);
463  fHgtheta_yz = dir.make<TH1F>("gtheta_yz", "Good Theta_yz", 40, -3.142, 3.142);
464  fHgmom = dir.make<TH1F>("gmom", "Good Momentum", 10, 0., 10.);
465  fHglen = dir.make<TH1F>("glen", "Good Particle Length", 10, 0., 1.1 * tpc.Length());
466 
467  fHmulstartx = dir.make<TH1F>("mulxstart",
468  "Multiplicity vs. X Start Position",
469  10,
470  -2. * tpc.HalfWidth(),
471  4. * tpc.HalfWidth());
472  fHmulstarty = dir.make<TH1F>(
473  "mulystart", "Multiplicity vs. Y Start Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
474  fHmulstartz =
475  dir.make<TH1F>("mulzstart", "Multiplicity vs. Z Start Position", 10, 0., tpc.Length());
476  fHmulendx = dir.make<TH1F>("mulxend",
477  "Multiplicity vs. X End Position",
478  10,
479  -2. * tpc.HalfWidth(),
480  4. * tpc.HalfWidth());
481  fHmulendy = dir.make<TH1F>(
482  "mulyend", "Multiplicity vs. Y End Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
483  fHmulendz = dir.make<TH1F>("mulzend", "Multiplicity vs. Z End Position", 10, 0., tpc.Length());
484  fHmultheta = dir.make<TH1F>("multheta", "Multiplicity vs. Theta", 20, 0., 3.142);
485  fHmulphi = dir.make<TH1F>("mulphi", "Multiplicity vs. Phi", 10, -3.142, 3.142);
486  fHmultheta_xz = dir.make<TH1F>("multheta_xz", "Multiplicity vs. Theta_xz", 40, -3.142, 3.142);
487  fHmultheta_yz = dir.make<TH1F>("multheta_yz", "Multiplicity vs. Theta_yz", 40, -3.142, 3.142);
488  fHmulmom = dir.make<TH1F>("mulmom", "Multiplicity vs. Momentum", 10, 0., 10.);
489  fHmullen =
490  dir.make<TH1F>("mullen", "Multiplicity vs. Particle Length", 10, 0., 1.1 * tpc.Length());
491 
492  fHestartx = dir.make<TH1F>("exstart",
493  "Efficiency vs. X Start Position",
494  10,
495  -2. * tpc.HalfWidth(),
496  4. * tpc.HalfWidth());
497  fHestarty = dir.make<TH1F>(
498  "eystart", "Efficiency vs. Y Start Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
499  fHestartz = dir.make<TH1F>("ezstart", "Efficiency vs. Z Start Position", 10, 0., tpc.Length());
500  fHeendx = dir.make<TH1F>(
501  "exend", "Efficiency vs. X End Position", 10, -2. * tpc.HalfWidth(), 4. * tpc.HalfWidth());
502  fHeendy = dir.make<TH1F>(
503  "eyend", "Efficiency vs. Y End Position", 10, -tpc.HalfHeight(), tpc.HalfHeight());
504  fHeendz = dir.make<TH1F>("ezend", "Efficiency vs. Z End Position", 10, 0., tpc.Length());
505  fHetheta = dir.make<TH1F>("etheta", "Efficiency vs. Theta", 20, 0., 3.142);
506  fHephi = dir.make<TH1F>("ephi", "Efficiency vs. Phi", 10, -3.142, 3.142);
507  fHetheta_xz = dir.make<TH1F>("etheta_xz", "Efficiency vs. Theta_xz", 40, -3.142, 3.142);
508  fHetheta_yz = dir.make<TH1F>("etheta_yz", "Efficiency vs. Theta_yz", 40, -3.142, 3.142);
509  fHemom = dir.make<TH1F>("emom", "Efficiency vs. Momentum", 10, 0., 10.);
510  fHelen = dir.make<TH1F>("elen", "Efficiency vs. Particle Length", 10, 0., 1.1 * tpc.Length());
511  }
512 
514  : EDAnalyzer(pset)
515  , fSeedModuleLabel(pset.get<std::string>("SeedModuleLabel"))
516  , fMCTrackModuleLabel(pset.get<std::string>("MCTrackModuleLabel"))
517  , fDump(pset.get<int>("Dump"))
518  , fMinMCKE(pset.get<double>("MinMCKE"))
519  , fMinMCLen(pset.get<double>("MinMCLen"))
520  , fMatchColinearity(pset.get<double>("MatchColinearity"))
521  , fMatchDisp(pset.get<double>("MatchDisp"))
522  , fIgnoreSign(pset.get<bool>("IgnoreSign"))
523  , fNumEvent(0)
524  {
525  mf::LogInfo("SeedAna") << "SeedAna configured with the following parameters:\n"
526  << " SeedModuleLabel = " << fSeedModuleLabel << "\n"
527  << " MCTrackModuleLabel = " << fMCTrackModuleLabel << "\n"
528  << " Dump = " << fDump << "\n"
529  << " MinMCKE = " << fMinMCKE << "\n"
530  << " MinMCLen = " << fMinMCLen;
531  }
532 
534  {
535  ++fNumEvent;
536 
537  // Optional dump stream.
538 
539  std::unique_ptr<mf::LogInfo> pdump;
540  if (fDump > 0) {
541  --fDump;
542  pdump = std::make_unique<mf::LogInfo>("TrackAna");
543  }
544 
545  // Make sure histograms are booked.
546 
547  bool mc = !evt.isRealData();
548 
549  // Get seed handle.
550 
552  evt.getByLabel(fSeedModuleLabel, seedh);
553 
554  // Seed->mc track matching map.
555 
556  std::map<const recob::Seed*, int> seedmap;
557 
558  if (mc) {
559 
560  // Get MCTracks.
561 
563  evt.getByLabel(fMCTrackModuleLabel, mctrackh);
564 
565  // Dump MCTracks.
566 
567  if (pdump) {
568  *pdump << "MC Tracks\n"
569  << " Id pdg x y z dx dy dz "
570  " p\n"
571  << "--------------------------------------------------------------------------------"
572  "-----------\n";
573  }
574 
575  // Loop over mc tracks, and fill histograms that depend only
576  // on mc particles.
577  auto const detProp =
579 
580  for (auto const& mctrk : *mctrackh) {
581  int pdg = mctrk.PdgCode();
582  if (fIgnoreSign) pdg = std::abs(pdg);
583 
584  // Ignore everything except stable charged nonshowering particles.
585  int apdg = std::abs(pdg);
586  if (apdg != 13 && // Muon
587  apdg != 211 && // Charged pion
588  apdg != 321 && // Charged kaon
589  apdg != 2212) { // (Anti)proton
590  continue;
591  }
592 
593  // Apply minimum energy cut.
594 
595  if (mctrk.Start().E() < mctrk.Start().Momentum().Mag() + 1000. * fMinMCKE) { continue; }
596 
597  // Calculate the x offset due to nonzero mc particle time.
598 
599  double mctime = mctrk.Start().T(); // nsec
600  double mcdx = mctime * 1.e-3 * detProp.DriftVelocity(); // cm
601 
602  // Calculate the length of this mc particle inside the fiducial volume.
603 
604  TVector3 mcstart;
605  TVector3 mcend;
606  TVector3 mcstartmom;
607  TVector3 mcendmom;
608  double plen = length(detProp, mctrk, mcdx, mcstart, mcend, mcstartmom, mcendmom);
609 
610  // Apply minimum fiducial length cut. Always reject particles that have
611  // zero length in the tpc regardless of the configured cut.
612 
613  if (plen <= std::max(fMinMCLen, 0.)) { continue; }
614 
615  // Dump MC particle information here.
616 
617  if (pdump) {
618  double pstart = mcstartmom.Mag();
619  double pend = mcendmom.Mag();
620  *pdump << "\nOffset" << std::setw(3) << mctrk.TrackID() << std::setw(6) << mctrk.PdgCode()
621  << " " << std::fixed << std::setprecision(2) << std::setw(10) << mcdx
622  << "\nStart " << std::setw(3) << mctrk.TrackID() << std::setw(6) << mctrk.PdgCode()
623  << " " << std::fixed << std::setprecision(2) << std::setw(10) << mcstart[0]
624  << std::setw(10) << mcstart[1] << std::setw(10) << mcstart[2];
625  if (pstart > 0.) {
626  *pdump << " " << std::fixed << std::setprecision(3) << std::setw(10)
627  << mcstartmom[0] / pstart << std::setw(10) << mcstartmom[1] / pstart
628  << std::setw(10) << mcstartmom[2] / pstart;
629  }
630  else
631  *pdump << std::setw(32) << " ";
632  *pdump << std::setw(12) << std::fixed << std::setprecision(3) << pstart;
633  *pdump << "\nEnd " << std::setw(3) << mctrk.TrackID() << std::setw(6) << mctrk.PdgCode()
634  << " " << std::fixed << std::setprecision(2) << std::setw(10) << mcend[0]
635  << std::setw(10) << mcend[1] << std::setw(10) << mcend[2];
636  if (pend > 0.01) {
637  *pdump << " " << std::fixed << std::setprecision(3) << std::setw(10)
638  << mcendmom[0] / pend << std::setw(10) << mcendmom[1] / pend << std::setw(10)
639  << mcendmom[2] / pend;
640  }
641  else
642  *pdump << std::setw(32) << " ";
643  *pdump << std::setw(12) << std::fixed << std::setprecision(3) << pend << "\n";
644  }
645 
646  // Fill histograms.
647 
648  if (fMCHistMap.count(pdg) == 0) {
649  std::ostringstream ostr;
650  ostr << "MC" << (fIgnoreSign ? "All" : (pdg > 0 ? "Pos" : "Neg")) << std::abs(pdg);
651  fMCHistMap.emplace(pdg, MCHists{ostr.str()});
652  }
653  const MCHists& mchists = fMCHistMap.at(pdg);
654 
655  double mctheta_xz = std::atan2(mcstartmom.X(), mcstartmom.Z());
656  double mctheta_yz = std::atan2(mcstartmom.Y(), mcstartmom.Z());
657 
658  mchists.fHmcstartx->Fill(mcstart.X());
659  mchists.fHmcstarty->Fill(mcstart.Y());
660  mchists.fHmcstartz->Fill(mcstart.Z());
661  mchists.fHmcendx->Fill(mcend.X());
662  mchists.fHmcendy->Fill(mcend.Y());
663  mchists.fHmcendz->Fill(mcend.Z());
664  mchists.fHmctheta->Fill(mcstartmom.Theta());
665  mchists.fHmcphi->Fill(mcstartmom.Phi());
666  mchists.fHmctheta_xz->Fill(mctheta_xz);
667  mchists.fHmctheta_yz->Fill(mctheta_yz);
668  mchists.fHmcmom->Fill(mcstartmom.Mag());
669  mchists.fHmclen->Fill(plen);
670 
671  // Loop over seeds and do matching.
672 
673  int nmatch = 0;
674  if (!seedh.isValid()) { continue; }
675 
676  // Loop over seeds.
677 
678  int nseed = seedh->size();
679  for (int i = 0; i < nseed; ++i) {
680  art::Ptr<recob::Seed> pseed(seedh, i);
681  const recob::Seed& seed = *pseed;
682  if (seedmap.count(&seed) == 0) seedmap[&seed] = -1;
683 
684  // Get parameters of this seed.
685 
686  TVector3 pos;
687  TVector3 dir;
688  double err[3];
689  seed.GetPoint(&pos[0], err);
690  seed.GetDirection(&dir[0], err);
691 
692  // Calculate the global-to-local rotation matrix.
693  // Copied from Track.cxx.
694 
695  TMatrixD rot(3, 3);
696  double dirmag = dir.Mag();
697  double diryz = std::sqrt(dir.Y() * dir.Y() + dir.Z() * dir.Z());
698 
699  double sinth = dir.X() / dirmag;
700  double costh = diryz / dirmag;
701  double sinphi = 0.;
702  double cosphi = 1.;
703  if (diryz != 0) {
704  sinphi = -dir.Y() / diryz;
705  cosphi = dir.Z() / diryz;
706  }
707  rot(0, 0) = costh;
708  rot(1, 0) = 0.;
709  rot(2, 0) = sinth;
710  rot(0, 1) = sinth * sinphi;
711  rot(1, 1) = cosphi;
712  rot(2, 1) = -costh * sinphi;
713  rot(0, 2) = -sinth * cosphi;
714  rot(1, 2) = sinphi;
715  rot(2, 2) = costh * cosphi;
716 
717  // Get best matching mc trajectory point.
718 
719  int itraj = mcmatch(detProp, mctrk, seed);
720  if (itraj < 0) { continue; }
721 
722  // Get mc relative position and direction at matching trajectory point.
723 
724  TVector3 mcpos = mctrk[itraj].Position().Vect() - pos;
725  TVector3 mcmom = mctrk[itraj].Momentum().Vect();
726  mcpos[0] += mcdx;
727 
728  // Rotate the momentum and position to the
729  // seed-local coordinate system.
730 
731  TVector3 mcmoml = rot * mcmom;
732  TVector3 mcposl = rot * mcpos;
733 
734  if (mcmoml.Z() < 0.) mcmoml = -mcmoml;
735  costh = mcmoml.Z() / mcmoml.Mag();
736 
737  double u = mcposl.X();
738  double v = mcposl.Y();
739  double w = mcposl.Z();
740 
741  double pu = mcmoml.X();
742  double pv = mcmoml.Y();
743  double pw = mcmoml.Z();
744 
745  double dudw = pu / pw;
746  double dvdw = pv / pw;
747 
748  double u0 = u - w * dudw;
749  double v0 = v - w * dvdw;
750  double uv0 = std::sqrt(u0 * u0 + v0 * v0);
751 
752  // Fill matching histograms.
753 
754  mchists.fHduvcosth->Fill(costh, uv0);
755  if (std::abs(uv0) < fMatchDisp) {
756 
757  // Fill slope matching histograms.
758 
759  mchists.fHmcdudw->Fill(dudw);
760  mchists.fHmcdvdw->Fill(dvdw);
761  }
762  mchists.fHcosth->Fill(costh);
763  if (costh <= fMatchColinearity) { continue; }
764 
765  // Fill displacement matching histograms.
766 
767  mchists.fHmcu->Fill(u0);
768  mchists.fHmcv->Fill(v0);
769  mchists.fHmcw->Fill(w);
770 
771  if (std::abs(uv0) >= fMatchDisp) { continue; }
772 
773  // Now we have passed all matching cuts and we have a matching
774  // mc particle + seed pair.
775 
776  ++nmatch;
777  seedmap[&seed] = mctrk.TrackID();
778 
779  // Fill matched seed histograms (seed multiplicity).
780 
781  mchists.fHmstartx->Fill(mcstart.X());
782  mchists.fHmstarty->Fill(mcstart.Y());
783  mchists.fHmstartz->Fill(mcstart.Z());
784  mchists.fHmendx->Fill(mcend.X());
785  mchists.fHmendy->Fill(mcend.Y());
786  mchists.fHmendz->Fill(mcend.Z());
787  mchists.fHmtheta->Fill(mcstartmom.Theta());
788  mchists.fHmphi->Fill(mcstartmom.Phi());
789  mchists.fHmtheta_xz->Fill(mctheta_xz);
790  mchists.fHmtheta_yz->Fill(mctheta_yz);
791  mchists.fHmmom->Fill(mcstartmom.Mag());
792  mchists.fHmlen->Fill(plen);
793  }
794 
795  // If we found at least one matched seed, fill good
796  // particle histograms.
797 
798  if (nmatch == 0) { continue; }
799 
800  mchists.fHgstartx->Fill(mcstart.X());
801  mchists.fHgstarty->Fill(mcstart.Y());
802  mchists.fHgstartz->Fill(mcstart.Z());
803  mchists.fHgendx->Fill(mcend.X());
804  mchists.fHgendy->Fill(mcend.Y());
805  mchists.fHgendz->Fill(mcend.Z());
806  mchists.fHgtheta->Fill(mcstartmom.Theta());
807  mchists.fHgphi->Fill(mcstartmom.Phi());
808  mchists.fHgtheta_xz->Fill(mctheta_xz);
809  mchists.fHgtheta_yz->Fill(mctheta_yz);
810  mchists.fHgmom->Fill(mcstartmom.Mag());
811  mchists.fHglen->Fill(plen);
812  }
813  }
814 
815  // Loop over seeds and fill reco-only seed histograms.
816 
817  if (seedh.isValid()) {
818 
819  // Loop over seeds.
820 
821  int nseed = seedh->size();
822 
823  if (nseed > 0 && pdump != 0) {
824  *pdump << "\nReconstructed Seeds\n"
825  << " MCid x y z dx dy dz "
826  " p\n"
827  << "--------------------------------------------------------------------------------"
828  "-----------\n";
829  }
830 
831  for (int i = 0; i < nseed; ++i) {
832  art::Ptr<recob::Seed> pseed(seedh, i);
833  const recob::Seed& seed = *pseed;
834 
835  // Fill histograms involving reco seeds only.
836 
837  TVector3 pos;
838  TVector3 dir;
839  double err[3];
840  seed.GetPoint(&pos[0], err);
841  seed.GetDirection(&dir[0], err);
842  double mdir = dir.Mag();
843  if (mdir != 0.) { dir *= (1. / mdir); }
844 
845  double dpos = bdist(pos);
846  double theta_xz = std::atan2(dir.X(), dir.Z());
847  double theta_yz = std::atan2(dir.Y(), dir.Z());
848 
849  // Dump seed information here.
850 
851  if (pdump) {
852  int mcid = seedmap[&seed];
853  *pdump << std::setw(15) << mcid << " " << std::fixed << std::setprecision(2)
854  << std::setw(10) << pos[0] << std::setw(10) << pos[1] << std::setw(10) << pos[2]
855  << " " << std::fixed << std::setprecision(3) << std::setw(10) << dir[0]
856  << std::setw(10) << dir[1] << std::setw(10) << dir[2] << "\n";
857  }
858 
859  // Fill histograms.
860 
861  if (fRecoHistMap.count(0) == 0) fRecoHistMap.emplace(0, RecoHists{"Reco"});
862  const RecoHists& rhists = fRecoHistMap.at(0);
863 
864  rhists.fHx->Fill(pos.X());
865  rhists.fHy->Fill(pos.Y());
866  rhists.fHz->Fill(pos.Z());
867  rhists.fHdist->Fill(dpos);
868  rhists.fHtheta->Fill(dir.Theta());
869  rhists.fHphi->Fill(dir.Phi());
870  rhists.fHtheta_xz->Fill(theta_xz);
871  rhists.fHtheta_yz->Fill(theta_yz);
872  }
873  }
874  }
875 
877  {
878  // Print summary.
879 
880  mf::LogInfo("SeedAna") << "SeedAna statistics:\n"
881  << " Number of events = " << fNumEvent;
882 
883  // Fill multiplicity histograms.
884 
885  for (std::map<int, MCHists>::const_iterator i = fMCHistMap.begin(); i != fMCHistMap.end();
886  ++i) {
887  const MCHists& mchists = i->second;
888  mulcalc(mchists.fHmstartx, mchists.fHmcstartx, mchists.fHmulstartx);
889  mulcalc(mchists.fHmstarty, mchists.fHmcstarty, mchists.fHmulstarty);
890  mulcalc(mchists.fHmstartz, mchists.fHmcstartz, mchists.fHmulstartz);
891  mulcalc(mchists.fHmendx, mchists.fHmcendx, mchists.fHmulendx);
892  mulcalc(mchists.fHmendy, mchists.fHmcendy, mchists.fHmulendy);
893  mulcalc(mchists.fHmendz, mchists.fHmcendz, mchists.fHmulendz);
894  mulcalc(mchists.fHmtheta, mchists.fHmctheta, mchists.fHmultheta);
895  mulcalc(mchists.fHmphi, mchists.fHmcphi, mchists.fHmulphi);
896  mulcalc(mchists.fHmtheta_xz, mchists.fHmctheta_xz, mchists.fHmultheta_xz);
897  mulcalc(mchists.fHmtheta_yz, mchists.fHmctheta_yz, mchists.fHmultheta_yz);
898  mulcalc(mchists.fHmmom, mchists.fHmcmom, mchists.fHmulmom);
899  mulcalc(mchists.fHmlen, mchists.fHmclen, mchists.fHmullen);
900  }
901  // Fill efficiency histograms.
902 
903  for (std::map<int, MCHists>::const_iterator i = fMCHistMap.begin(); i != fMCHistMap.end();
904  ++i) {
905  const MCHists& mchists = i->second;
906  effcalc(mchists.fHgstartx, mchists.fHmcstartx, mchists.fHestartx);
907  effcalc(mchists.fHgstarty, mchists.fHmcstarty, mchists.fHestarty);
908  effcalc(mchists.fHgstartz, mchists.fHmcstartz, mchists.fHestartz);
909  effcalc(mchists.fHgendx, mchists.fHmcendx, mchists.fHeendx);
910  effcalc(mchists.fHgendy, mchists.fHmcendy, mchists.fHeendy);
911  effcalc(mchists.fHgendz, mchists.fHmcendz, mchists.fHeendz);
912  effcalc(mchists.fHgtheta, mchists.fHmctheta, mchists.fHetheta);
913  effcalc(mchists.fHgphi, mchists.fHmcphi, mchists.fHephi);
914  effcalc(mchists.fHgtheta_xz, mchists.fHmctheta_xz, mchists.fHetheta_xz);
915  effcalc(mchists.fHgtheta_yz, mchists.fHmctheta_yz, mchists.fHetheta_yz);
916  effcalc(mchists.fHgmom, mchists.fHmcmom, mchists.fHemom);
917  effcalc(mchists.fHglen, mchists.fHmclen, mchists.fHelen);
918  }
919  }
920 }
std::string fMCTrackModuleLabel
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
Float_t den
Definition: plot.C:35
void GetPoint(double *Pt, double *Err) const
Definition: Seed.cxx:89
void endJob() override
constexpr auto abs(T v)
Returns the absolute value of the argument.
std::string fSeedModuleLabel
STL namespace.
intermediate_table::const_iterator const_iterator
double T() const
Definition: MCStep.h:41
bool isRealData() const
Definition: Event.cc:53
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.cc:6
tick ticks
Alias for common language habits.
Definition: electronics.h:76
MCHists(const std::string &subdir)
void analyze(const art::Event &evt) override
bool isValid() const noexcept
Definition: Handle.h:203
decltype(auto) constexpr end(T &&obj)
ADL-aware version of std::end.
Definition: StdUtils.h:77
double fMatchColinearity
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:65
long seed
Definition: chem4.cc:67
double ConvertXToTicks(double X, int p, int t, int c) const
double DriftVelocity(double efield=0., double temperature=0.) const
cm/us
Class def header for mctrack data container.
std::map< int, RecoHists > fRecoHistMap
int PdgCode() const
Definition: MCTrack.h:39
const TLorentzVector & Momentum() const
Definition: MCStep.h:34
SeedAna(fhicl::ParameterSet const &pset)
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
std::map< int, MCHists > fMCHistMap
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
double dist(const TReal *x, const TReal *y, const unsigned int dimension)
TDirectory * dir
Definition: macro.C:5
double E() const
Definition: MCStep.h:45
const MCStep & Start() const
Definition: MCTrack.h:42
Char_t n[5]
unsigned int TrackID() const
Definition: MCTrack.h:40
TCEvent evt
Definition: DataStructs.cxx:8
Float_t w
Definition: plot.C:20
void GetDirection(double *Dir, double *Err) const
Definition: Seed.cxx:80
Definition: fwd.h:26
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
RecoHists(const std::string &subdir)