PlotSpacePoints_module.cc
Go to the documentation of this file.
1 // Christopher Backhouse - bckhouse@fnal.gov
2 
3 #include <string>
4 
11 #include "fhiclcpp/ParameterSet.h"
12 
17 
18 #include "TGraph.h"
19 #include "TString.h"
20 #include "TVirtualPad.h"
21 
22 namespace reco3d {
23 
25  public:
26  explicit PlotSpacePoints(const fhicl::ParameterSet& pset);
27 
28  private:
29  void analyze(const art::Event& evt) override;
30 
31  void Plot(const std::vector<recob::SpacePoint>& pts, const std::string& suffix) const;
32 
33  void Plot3D(const std::vector<recob::SpacePoint>& pts, const std::string& suffix) const;
34 
35  std::vector<recob::SpacePoint> TrueSpacePoints(detinfo::DetectorClocksData const& clockData,
36  art::Handle<std::vector<recob::Hit>> hits) const;
37 
39 
41 
43 
44  bool fPlots;
45  bool fPlots3D;
46  bool fPlotsTrue;
47  };
48 
50 
51  // ---------------------------------------------------------------------------
53  : EDAnalyzer(pset)
54  , fSpacePointTag(art::InputTag(pset.get<std::string>("SpacePointLabel"),
55  pset.get<std::string>("SpacePointInstanceLabel")))
56  , fHitLabel(pset.get<std::string>("HitLabel"))
57  , fSuffix(pset.get<std::string>("Suffix"))
58  , fPlots(pset.get<bool>("Plots"))
59  , fPlots3D(pset.get<bool>("Plots3D"))
60  , fPlotsTrue(pset.get<bool>("PlotsTrue"))
61  {
62  if (!fSuffix.empty()) fSuffix = "_" + fSuffix;
63  }
64 
65  // ---------------------------------------------------------------------------
66  void
67  PlotSpacePoints::Plot(const std::vector<recob::SpacePoint>& pts, const std::string& suffix) const
68  {
69  TGraph gZX;
70  TGraph gYX;
71  TGraph gZY;
72 
73  gZX.SetTitle(";z;x");
74  gYX.SetTitle(";y;x");
75  gZY.SetTitle(";z;y");
76 
77  for (const recob::SpacePoint& pt : pts) {
78  const double* xyz = pt.XYZ();
79  const double x = xyz[0];
80  const double y = xyz[1];
81  const double z = xyz[2];
82  gZX.SetPoint(gZX.GetN(), z, x);
83  gYX.SetPoint(gYX.GetN(), y, x);
84  gZY.SetPoint(gZY.GetN(), z, y);
85  }
86 
87  if (gZX.GetN() == 0) gZX.SetPoint(0, 0, 0);
88  if (gYX.GetN() == 0) gYX.SetPoint(0, 0, 0);
89  if (gZY.GetN() == 0) gZY.SetPoint(0, 0, 0);
90 
91  gZX.Draw("ap");
92  gPad->Print(("plots/evd" + suffix + ".png").c_str());
93 
94  gYX.Draw("ap");
95  gPad->Print(("plots/evd_ortho" + suffix + ".png").c_str());
96  gZY.Draw("ap");
97  gPad->Print(("plots/evd_zy" + suffix + ".png").c_str());
98  }
99 
100  // ---------------------------------------------------------------------------
101  std::vector<recob::SpacePoint>
103  art::Handle<std::vector<recob::Hit>> hits) const
104  {
105  std::vector<recob::SpacePoint> pts_true;
106 
107  const double err[6] = {
108  0,
109  };
110 
112  for (unsigned int i = 0; i < hits->size(); ++i) {
113  try {
114  const std::vector<double> xyz = bt_serv->HitToXYZ(clockData, art::Ptr<recob::Hit>(hits, i));
115  pts_true.emplace_back(&xyz[0], err, 0);
116  }
117  catch (...) {
118  } // some hits have no electrons?
119  }
120 
121  return pts_true;
122  }
123 
124  // ---------------------------------------------------------------------------
125  void
126  PlotSpacePoints::Plot3D(const std::vector<recob::SpacePoint>& pts,
127  const std::string& suffix) const
128  {
129  int frame = 0;
130  for (int phase = 0; phase < 4; ++phase) {
131  const int Nang = 20;
132  for (int iang = 0; iang < Nang; ++iang) {
133  const double ang = M_PI / 2 * iang / double(Nang);
134 
135  TGraph g;
136 
137  for (const recob::SpacePoint& p : pts) {
138  const double* xyz = p.XYZ();
139 
140  double x{}, y{};
141  if (phase == 0) {
142  x = cos(ang) * xyz[1] + sin(ang) * xyz[2];
143  y = xyz[0];
144  }
145  if (phase == 1) {
146  x = xyz[2];
147  y = cos(ang) * xyz[0] + sin(ang) * xyz[1];
148  }
149  if (phase == 2) {
150  x = cos(ang) * xyz[2] - sin(ang) * xyz[0];
151  y = xyz[1];
152  }
153  if (phase == 3) {
154  x = -cos(ang) * xyz[0] + sin(ang) * xyz[1];
155  y = cos(ang) * xyz[1] + sin(ang) * xyz[0];
156  }
157 
158  // const double phi = phase/3.*M_PI/2 + ang/3;
159  const double phi = 0;
160  g.SetPoint(g.GetN(), cos(phi) * x + sin(phi) * y, cos(phi) * y - sin(phi) * x);
161  }
162 
164  TString::Format(("anim/evd3d" + suffix + "_%03d.png").c_str(), frame++).Data();
165  g.SetTitle(fname.c_str());
166  if (g.GetN()) g.Draw("ap");
167  gPad->Print(fname.c_str());
168  }
169  }
170  }
171 
172  void
174  {
175  if (fPlots) {
177  evt.getByLabel(fSpacePointTag, pts);
178 
179  const std::string suffix = TString::Format("%s_%d", fSuffix.c_str(), evt.event()).Data();
180 
181  if (!pts->empty()) {
182  Plot(*pts, suffix);
183 
184  if (fPlots3D) Plot3D(*pts, suffix);
185  }
186  }
187 
188  if (fPlotsTrue) {
190  evt.getByLabel(fHitLabel, hits);
191  auto const clockData =
193  const std::vector<recob::SpacePoint> pts = TrueSpacePoints(clockData, hits);
194 
195  const std::string suffix = TString::Format("%s_true_%d", fSuffix.c_str(), evt.event()).Data();
196 
197  Plot(pts, suffix);
198 
199  if (fPlots3D) Plot3D(pts, suffix);
200  }
201  }
202 
203 } // namespace
std::vector< recob::SpacePoint > TrueSpacePoints(detinfo::DetectorClocksData const &clockData, art::Handle< std::vector< recob::Hit >> hits) const
EventNumber_t event() const
Definition: DataViewImpl.cc:85
Format
Definition: utils.h:7
static constexpr double g
Definition: Units.h:144
std::string string
Definition: nybbler.cc:12
PlotSpacePoints(const fhicl::ParameterSet &pset)
STL namespace.
std::vector< double > HitToXYZ(detinfo::DetectorClocksData const &clockData, const recob::Hit &hit) const
EDAnalyzer(fhicl::ParameterSet const &pset)
Definition: EDAnalyzer.h:25
bool getByLabel(std::string const &label, std::string const &instance, Handle< PROD > &result) const
Definition: DataViewImpl.h:633
#define DEFINE_ART_MODULE(klass)
Definition: ModuleMacros.h:67
p
Definition: test.py:223
void err(const char *fmt,...)
Definition: message.cpp:226
void Plot3D(const std::vector< recob::SpacePoint > &pts, const std::string &suffix) const
#define M_PI
Definition: includeROOT.h:54
Fw2dFFT::Data Data
Declaration of signal hit object.
Contains all timing reference information for the detector.
list x
Definition: train.py:276
TCEvent evt
Definition: DataStructs.cxx:7
auto const & get(AssnsNode< L, R, D > const &r)
Definition: AssnsNode.h:115
void Plot(const std::vector< recob::SpacePoint > &pts, const std::string &suffix) const
void analyze(const art::Event &evt) override