All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ROIFromDecoder_tool.cc
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////
2 /// \file ROIFromDecoder_tool.cc
3 /// \author T. Usher
4 ////////////////////////////////////////////////////////////////////////
5 
6 #include <cmath>
8 #include "art/Utilities/ToolMacros.h"
9 #include "art/Utilities/make_tool.h"
10 #include "art_root_io/TFileService.h"
11 #include "messagefacility/MessageLogger/MessageLogger.h"
12 #include "cetlib_except/exception.h"
15 #include "larcore/CoreUtils/ServiceUtil.h" // lar::providerFrom()
17 
18 #include "TH1F.h"
19 #include "TH2F.h"
20 #include "TProfile.h"
21 
22 #include <fstream>
23 
24 namespace icarus_tool
25 {
26 
28 {
29 public:
30  explicit ROIFromDecoder(const fhicl::ParameterSet& pset);
31 
33 
34  void configure(const fhicl::ParameterSet& pset) override;
35  void initializeHistograms(art::TFileDirectory&) override {return;}
36 
37  void FindROIs(const art::Event&, const ArrayFloat&, const std::vector<raw::ChannelID_t>&, const geo::PlaneID&, ArrayFloat&, ArrayBool&) override;
38 
39 private:
40  // A magic map because all tools need them
41  using TPCIDToLabelMap = std::map<geo::TPCID,std::string>;
42 
43  TPCIDToLabelMap fTPCIDToLabelMap; ///< Translate from TPCID to a substring
44 
45  // fhicl parameters
46  std::vector<art::InputTag> fROILabelVec; ///< List of input files to search
47 
48  const geo::GeometryCore* fGeometry = lar::providerFrom<geo::Geometry>();
49 };
50 
51 //----------------------------------------------------------------------
52 // Constructor.
53 ROIFromDecoder::ROIFromDecoder(const fhicl::ParameterSet& pset)
54 {
55  std::cout << "*** In ROIFromDecoder constructor ***" << std::endl;
56  configure(pset);
57 }
58 
60 {
61 }
62 
63 void ROIFromDecoder::configure(const fhicl::ParameterSet& pset)
64 {
65  // Start by recovering the parameters
66  fROILabelVec = pset.get<std::vector<art::InputTag>>("ROILabelVec", {""});
67 
68  // Build our map
69  fTPCIDToLabelMap[geo::TPCID(0,0)] = "EE";
70  fTPCIDToLabelMap[geo::TPCID(0,1)] = "EE";
71  fTPCIDToLabelMap[geo::TPCID(0,2)] = "EW";
72  fTPCIDToLabelMap[geo::TPCID(0,3)] = "EW";
73  fTPCIDToLabelMap[geo::TPCID(1,0)] = "WE";
74  fTPCIDToLabelMap[geo::TPCID(1,1)] = "WE";
75  fTPCIDToLabelMap[geo::TPCID(1,2)] = "WW";
76  fTPCIDToLabelMap[geo::TPCID(1,3)] = "WW";
77 
78  return;
79 }
80 
81 void ROIFromDecoder::FindROIs(const art::Event& event, const ArrayFloat& inputImage, const std::vector<raw::ChannelID_t>& channelVec, const geo::PlaneID& planeID, ArrayFloat& output, ArrayBool& outputROIs)
82 {
83  // First thing is find the correct data product to recover ROIs
84  TPCIDToLabelMap::const_iterator tpcItr = fTPCIDToLabelMap.find(planeID.asTPCID());
85 
86  if (tpcItr == fTPCIDToLabelMap.end())
87  {
88  std::cout << "Can't happen? for planeID: " << planeID << std::endl;
89  return;
90  }
91 
92  art::InputTag roiLabel("");
93 
94  for(const auto& tag : fROILabelVec)
95  {
96  if (tag.instance().find(tpcItr->second) != std::string::npos)
97  {
98  roiLabel = tag;
99  break;
100  }
101  }
102 
103  if (roiLabel != "")
104  {
105  // do stuff here
106 
107  // Read in the collection of full length deconvolved waveforms
108  // Note we assume this list is sorted in increasing channel number!
109  art::Handle< std::vector<recob::Wire>> wireVecHandle;
110 
111  event.getByLabel(roiLabel, wireVecHandle);
112 
113  // The first task is to build a mapping between channel and ROIs from the input data product.
114  // The input data product will contain information for every plane in a physical TPC... unfortunately, we only want a single plane within
115  // a logical TPC... so we need to loop through to find what we want
116  std::unordered_map<raw::ChannelID_t,const recob::Wire*> channelToWireMap;
117 
118  for(const auto& wireData : *wireVecHandle)
119  {
120  std::vector<geo::WireID> wireIDVec = fGeometry->ChannelToWire(wireData.Channel());
121 
122  for(const auto& wireID : wireIDVec)
123  {
124  if (wireID.asPlaneID() != planeID) continue;
125 
126  if (wireID.Wire > outputROIs.size())
127  {
128  std::cout << "#################################### Wire out of bounds! Wire: " << wireID.Wire << ", max: " << outputROIs.size() << " #################" << std::endl;
129  continue;
130  }
131 
132  channelToWireMap[wireData.Channel()] = &wireData;
133  }
134  }
135 
136  // Now we loop through the input image channels and make the new ROIs
137  for(size_t channelIdx = 0; channelIdx < channelVec.size(); channelIdx++)
138  {
139  raw::ChannelID_t channel = channelVec[channelIdx];
140 
141  std::unordered_map<raw::ChannelID_t,const recob::Wire*>::const_iterator wireItr = channelToWireMap.find(channel);
142 
143  if (wireItr != channelToWireMap.end())
144  {
145  const recob::Wire& wireData = *(wireItr->second);
146 
147  // Get the WireIDs (again)
148  std::vector<geo::WireID> wireIDVec = fGeometry->ChannelToWire(channel);
149 
150  for(const auto& wireID : wireIDVec)
151  {
152  if (wireID.asPlaneID() != planeID) continue;
153 
154  // Recover this wire's output vector
155  VectorBool& channelData = outputROIs[wireID.Wire];
156 
157  // What we need to do is find the ROIs in the input wire data and then translate to the output
158  const recob::Wire::RegionsOfInterest_t signalROIs = wireData.SignalROI();
159 
160  for(const auto& range : signalROIs.get_ranges())
161  {
162  size_t startTick = range.begin_index();
163  size_t roiLen = range.data().size();
164  size_t stopTick = startTick + roiLen;
165 
166  if (startTick > channelData.size())
167  {
168  std::cout << "*** ROI decoder has start tick larger than output array, start: " << startTick << ", array size: " << channelData.size() << std::endl;
169  continue;
170  }
171 
172  if (stopTick > channelData.size())
173  {
174  std::cout << "*** ROI decoder has ROI length larger than output array, start: " << startTick << ", end: " << stopTick << ", array size: " << channelData.size() << std::endl;
175  continue;
176  }
177 
178  std::fill(channelData.begin() + startTick, channelData.begin() + stopTick, true);
179  }
180 
181  if (planeID.Cryostat == 0 && planeID.TPC == 0 && planeID.Plane == 0) std::cout << std::endl;
182  }
183  }
184  else
185  {
186  std::cout << "*** Not finding ROI information for channel: " << channel << std::endl;
187  }
188  }
189  }
190 
191  return;
192 }
193 
194 DEFINE_ART_CLASS_TOOL(ROIFromDecoder)
195 }
std::vector< bool > VectorBool
Definition: IROILocator.h:34
std::map< geo::TPCID, std::string > TPCIDToLabelMap
Utilities related to art service access.
std::vector< VectorBool > ArrayBool
Definition: IROILocator.h:36
size_type size() const
Returns the size of the vector.
The data type to uniquely identify a Plane.
Definition: geo_types.h:472
void FindROIs(const art::Event &, const ArrayFloat &, const std::vector< raw::ChannelID_t > &, const geo::PlaneID &, ArrayFloat &, ArrayBool &) override
std::vector< geo::WireID > ChannelToWire(raw::ChannelID_t const channel) const
Returns a list of wires connected to the specified TPC channel.
CryostatID_t Cryostat
Index of cryostat.
Definition: geo_types.h:212
const range_list_t & get_ranges() const
Returns the internal list of non-void ranges.
std::vector< art::InputTag > fROILabelVec
List of input files to search.
std::vector< VectorFloat > ArrayFloat
Definition: IROILocator.h:37
ROIFromDecoder(const fhicl::ParameterSet &pset)
TPCIDToLabelMap fTPCIDToLabelMap
Translate from TPCID to a substring.
void initializeHistograms(art::TFileDirectory &) override
const RegionsOfInterest_t & SignalROI() const
Returns the list of regions of interest.
Definition: Wire.h:228
void fill(const art::PtrVector< recob::Hit > &hits, int only_plane)
PlaneID_t Plane
Index of the plane within its TPC.
Definition: geo_types.h:493
Description of geometry of one entire detector.
constexpr TPCID const & asTPCID() const
Conversion to TPCID (for convenience of notation).
Definition: geo_types.h:446
BEGIN_PROLOG sequence::SlidingWindowTriggerPatternsOppositeWindows END_PROLOG simSlidingORM6O6 effSlidingORW output
const geo::GeometryCore * fGeometry
This provides an interface for tools which are tasked with finding ROI&#39;s in input waveforms...
Class holding the regions of interest of signal from a channel.
Definition: Wire.h:118
void configure(const fhicl::ParameterSet &pset) override
Declaration of basic channel signal object.
IDparameter< geo::TPCID > TPCID
Member type of validated geo::TPCID parameter.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
TPCID_t TPC
Index of the TPC within its cryostat.
Definition: geo_types.h:406
art framework interface to geometry description
BEGIN_PROLOG could also be cout