All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DrawGausHits_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file DrawGausHits_tool.cc
3 /// \author T. Usher
4 ////////////////////////////////////////////////////////////////////////
5 
10 
11 #include "nuevdb/EventDisplayBase/EventHolder.h"
12 #include "nuevdb/EventDisplayBase/View2D.h"
13 
14 #include "art/Framework/Principal/Handle.h"
15 #include "art/Framework/Services/Registry/ServiceHandle.h"
16 #include "art/Utilities/ToolMacros.h"
17 #include "canvas/Persistency/Common/FindManyP.h"
18 #include "cetlib_except/exception.h"
19 #include "messagefacility/MessageLogger/MessageLogger.h"
20 
21 #include "TF1.h"
22 #include "TPolyLine.h"
23 
24 #include <fstream>
25 
26 namespace evdb_tool {
27 
28  class DrawGausHits : public IWFHitDrawer {
29  public:
30  explicit DrawGausHits(const fhicl::ParameterSet& pset);
31 
32  ~DrawGausHits();
33 
34  void configure(const fhicl::ParameterSet& pset) override;
35  void Draw(evdb::View2D&, raw::ChannelID_t&) const override;
36 
37  private:
38  using HitParams_t = struct HitParams_t {
39  float hitCenter;
40  float hitSigma;
41  float hitHeight;
42  float hitStart;
43  float hitEnd;
44  };
45 
46  using ROIHitParamsVec = std::vector<HitParams_t>;
47  using HitParamsVec = std::vector<ROIHitParamsVec>;
48 
51  std::vector<int> fColorVec;
52  mutable std::vector<std::unique_ptr<TF1>> fHitFunctionVec;
53  };
54 
55  //----------------------------------------------------------------------
56  // Constructor.
57  DrawGausHits::DrawGausHits(const fhicl::ParameterSet& pset) { configure(pset); }
58 
60 
61  void
62  DrawGausHits::configure(const fhicl::ParameterSet& pset)
63  {
64  fNumPoints = pset.get<int>("NumPoints", 1000);
65  fFloatBaseline = pset.get<bool>("FloatBaseline", false);
66 
67  fColorVec.clear();
68  fColorVec.push_back(kRed);
69  fColorVec.push_back(kGreen);
70  fColorVec.push_back(kMagenta);
71  fColorVec.push_back(kCyan);
72 
73  fHitFunctionVec.clear();
74 
75  return;
76  }
77 
78  void
79  DrawGausHits::Draw(evdb::View2D& view2D, raw::ChannelID_t& channel) const
80  {
81  art::ServiceHandle<evd::RecoDrawingOptions const> recoOpt;
82 
83  //grab the singleton with the event
84  const art::Event* event = evdb::EventHolder::Instance()->GetEvent();
85  if (!event) return;
86 
87  fHitFunctionVec.clear();
88 
89  for (size_t imod = 0; imod < recoOpt->fHitLabels.size(); ++imod) {
90  // Step one is to recover the hits for this label that match the input channel
91  art::InputTag const which = recoOpt->fHitLabels[imod];
92 
93  art::Handle<std::vector<recob::Hit>> hitVecHandle;
94  event->getByLabel(which, hitVecHandle);
95 
96  // Get a container for the subset of hits we are drawing
97  art::PtrVector<recob::Hit> hitPtrVec;
98 
99  try {
100  for (size_t hitIdx = 0; hitIdx < hitVecHandle->size(); hitIdx++) {
101  art::Ptr<recob::Hit> hit(hitVecHandle, hitIdx);
102 
103  if (hit->Channel() == channel) hitPtrVec.push_back(hit);
104  }
105  }
106  catch (cet::exception& e) {
107  mf::LogWarning("DrawGausHits") << "DrawGausHits"
108  << " failed with message:\n"
109  << e;
110  }
111 
112  if (hitPtrVec.empty()) continue;
113 
114  // Apparently you cannot trust some hit producers to put the hits in the correct order!
115  std::sort(hitPtrVec.begin(), hitPtrVec.end(), [](const auto& left, const auto& right) {
116  return left->PeakTime() < right->PeakTime();
117  });
118 
119  // Get associations to wires
120  art::FindManyP<recob::Wire> wireAssnsVec(hitPtrVec, *event, which);
121  std::vector<float> wireDataVec;
122 
123  // Recover the full (zero-padded outside ROI's) deconvolved waveform for this wire
124  if (wireAssnsVec.isValid() && wireAssnsVec.size() > 0) {
125  auto hwafp = wireAssnsVec.at(0).front();
126  if (!hwafp.isNull() && hwafp.isAvailable()) { wireDataVec = hwafp->Signal(); }
127  }
128 
129  // Now go through and process the hits back into the hit parameters
130  using ROIHitParamsVec = std::vector<HitParams_t>;
131  using HitParamsVec = std::vector<ROIHitParamsVec>;
132 
133  // Get an initial container for common hits on ROI
134  HitParamsVec hitParamsVec;
135  ROIHitParamsVec roiHitParamsVec;
136  raw::TDCtick_t lastEndTick(10000);
137 
138  for (const auto& hit : hitPtrVec) {
139  // check roi end condition
140  if (hit->PeakTime() - 3. * hit->RMS() > lastEndTick) {
141  if (!roiHitParamsVec.empty()) hitParamsVec.push_back(roiHitParamsVec);
142  roiHitParamsVec.clear();
143  }
144 
145  HitParams_t hitParams;
146 
147  hitParams.hitCenter = hit->PeakTime();
148  hitParams.hitSigma = hit->RMS();
149  hitParams.hitHeight = hit->PeakAmplitude();
150  hitParams.hitStart = hit->StartTick(); //PeakTime() - 3. * hit->RMS();
151  hitParams.hitEnd = hit->EndTick(); //PeakTime() + 3. * hit->RMS();
152 
153  lastEndTick = hitParams.hitEnd;
154 
155  roiHitParamsVec.emplace_back(hitParams);
156 
157  } //end loop over reco hits
158 
159  // Just in case (probably never called...)
160  if (!roiHitParamsVec.empty()) hitParamsVec.push_back(roiHitParamsVec);
161 
162  size_t roiCount(0);
163 
164  for (const auto& roiHitParamsVec : hitParamsVec) {
165  // Create a histogram here...
166  double roiStart = roiHitParamsVec.front().hitStart;
167  double roiStop = roiHitParamsVec.back().hitEnd;
168 
169  std::string funcString = "gaus(0)";
170  std::string funcName = Form("hitshape_%05zu_c%02zu", size_t(channel), roiCount++);
171 
172  for (size_t idx = 1; idx < roiHitParamsVec.size(); idx++)
173  funcString += "+gaus(" + std::to_string(3 * idx) + ")";
174 
175  // Include a baseline
176  float baseline(0.);
177 
178  if (fFloatBaseline && !wireDataVec.empty()) baseline = wireDataVec.at(roiStart);
179 
180  funcString += "+" + std::to_string(baseline);
181 
182  fHitFunctionVec.emplace_back(
183  std::make_unique<TF1>(funcName.c_str(), funcString.c_str(), roiStart, roiStop));
184  TF1& hitFunc = *fHitFunctionVec.back().get();
185 
186  hitFunc.SetLineColor(fColorVec[imod % fColorVec.size()]);
187 
188  size_t idx(0);
189  for (const auto& hitParams : roiHitParamsVec) {
190  hitFunc.SetParameter(idx + 0, hitParams.hitHeight);
191  hitFunc.SetParameter(idx + 1, hitParams.hitCenter);
192  hitFunc.SetParameter(idx + 2, hitParams.hitSigma);
193 
194  TPolyLine& hitHeight = view2D.AddPolyLine(2, kBlack, 1, 1);
195 
196  hitHeight.SetPoint(0, hitParams.hitCenter, baseline);
197  hitHeight.SetPoint(1, hitParams.hitCenter, hitParams.hitHeight + baseline);
198 
199  hitHeight.Draw("same");
200 
201  TPolyLine& hitSigma = view2D.AddPolyLine(2, kGray, 1, 1);
202 
203  hitSigma.SetPoint(
204  0, hitParams.hitCenter - hitParams.hitSigma, 0.6 * hitParams.hitHeight + baseline);
205  hitSigma.SetPoint(
206  1, hitParams.hitCenter + hitParams.hitSigma, 0.6 * hitParams.hitHeight + baseline);
207 
208  hitSigma.Draw("same");
209 
210  idx += 3;
211  }
212 
213  hitFunc.Draw("same");
214  }
215  } //end loop over HitFinding modules
216 
217  return;
218  }
219 
220  DEFINE_ART_CLASS_TOOL(DrawGausHits)
221 }
std::vector< HitParams_t > ROIHitParamsVec
void configure(const fhicl::ParameterSet &pset) override
Declaration of signal hit object.
walls no right
Definition: selectors.fcl:105
std::vector< ROIHitParamsVec > HitParamsVec
process_name hit
Definition: cheaterreco.fcl:51
int TDCtick_t
Type representing a TDC tick.
Definition: RawTypes.h:25
void Draw(evdb::View2D &, raw::ChannelID_t &) const override
BEGIN_PROLOG baseline
walls no left
Definition: selectors.fcl:105
std::vector< int > fColorVec
std::string to_string(WindowPattern const &pattern)
do i e
Declaration of basic channel signal object.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
std::vector< std::unique_ptr< TF1 > > fHitFunctionVec
DrawGausHits(const fhicl::ParameterSet &pset)
struct HitParams_t{float hitCenter HitParams_t