All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DumpChannelMap_module.cc
Go to the documentation of this file.
1 /**
2  * @file DumpChannelMap_module.cc
3  * @brief Prints on screen the current channel-wire map
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date October 27th, 2015
6  *
7  */
8 
9 // LArSoft libraries
13 #include "larcoreobj/SimpleTypesAndConstants/RawTypes.h" // raw::ChannelID_t
14 #include "larcoreobj/SimpleTypesAndConstants/geo_types.h" // geo::WireID
15 
16 // framework libraries
17 #include "art/Framework/Services/Registry/ServiceHandle.h"
18 #include "art/Framework/Core/EDAnalyzer.h"
19 #include "art/Framework/Core/ModuleMacros.h"
20 #include "canvas/Utilities/Exception.h"
21 #include "messagefacility/MessageLogger/MessageLogger.h"
22 #include "fhiclcpp/types/Atom.h"
23 #include "fhiclcpp/types/Comment.h"
24 #include "fhiclcpp/types/Name.h"
25 
26 // C/C++ standard libraries
27 #include <string>
28 
29 namespace geo {
30  class DumpChannelMap;
31 }
32 
33 /** ****************************************************************************
34  * @brief Prints on screen the current channel-wire and optical detector maps.
35  *
36  * One print is performed at the beginning of each run.
37  *
38  *
39  * Configuration parameters
40  * =========================
41  *
42  * - *ChannelToWires* (boolean, default: true): prints all the wires
43  * corresponding to each channel
44  * - *WireToChannel* (boolean, default: false): prints which channel covers
45  * each wire
46  * - *OpDetChannels* (boolean, default: false): prints for each optical detector
47  * channel ID the optical detector ID and its center
48  * - *FirstChannel* (integer, default: no limit): ID of the lowest channel to be
49  * printed
50  * - *LastChannel* (integer, default: no limit): ID of the highest channel to be
51  * printed
52  * - *OutputCategory* (string, default: DumpChannelMap): output category used
53  * by the message facility to output information (INFO level)
54  *
55  */
56 
57 
58 class geo::DumpChannelMap: public art::EDAnalyzer {
59 public:
60 
61  /// Module configuration.
62  struct Config {
63  using Name = fhicl::Name;
64  using Comment = fhicl::Comment;
65 
66  fhicl::Atom<std::string> OutputCategory {
67  Name("OutputCategory"),
68  Comment(
69  "output category used by the message facility to output information (INFO level)"
70  ),
71  "DumpChannelMap"
72  };
73 
74  fhicl::Atom<bool> ChannelToWires {
75  Name("ChannelToWires"),
76  Comment("print all the wires corresponding to each channel"),
77  true
78  };
79 
80  fhicl::Atom<bool> WireToChannel {
81  Name("WireToChannel"),
82  Comment("print which channel covers each wire"),
83  false
84  };
85 
86  fhicl::Atom<bool> OpDetChannels {
87  Name("OpDetChannels"),
88  Comment(
89  "print for each optical detector channel ID the optical detector ID and its center"
90  ),
91  false
92  };
93 
94  fhicl::Atom<raw::ChannelID_t> FirstChannel {
95  Name("FirstChannel"),
96  Comment("ID of the lowest channel to be printed (default: no limit)"),
98  };
99 
100  fhicl::Atom<raw::ChannelID_t> LastChannel {
101  Name("LastChannel"),
102  Comment("ID of the highest channel to be printed (default: no limit)"),
104  };
105 
106  }; // Config
107 
108  using Parameters = art::EDAnalyzer::Table<Config>;
109 
110 
111  explicit DumpChannelMap(Parameters const& config);
112 
113  // Plugins should not be copied or assigned.
114  DumpChannelMap(DumpChannelMap const &) = delete;
115  DumpChannelMap(DumpChannelMap &&) = delete;
116  DumpChannelMap & operator = (DumpChannelMap const &) = delete;
118 
119  // Required functions
120  void analyze(art::Event const&) override {}
121 
122  /// Drives the dumping
123  void beginRun(art::Run const&) override;
124 
125  private:
126 
127  std::string OutputCategory; ///< Name of the category for output.
128  bool DoChannelToWires; ///< Dump channel -> wires mapping.
129  bool DoWireToChannel; ///< Dump wire -> channel mapping.
130  bool DoOpDetChannels; ///< Dump optical detector channel -> optical detector.
131 
132  raw::ChannelID_t FirstChannel; ///< First channel to be printed.
133  raw::ChannelID_t LastChannel; ///< Last channel to be printed.
134 
135 }; // geo::DumpChannelMap
136 
137 
138 //==============================================================================
139 //=== Algorithms declaration
140 //===
141 
142 namespace {
143 
144  /// Dumps channel-to-wires mapping
145  class DumpChannelToWires {
146  public:
147 
148  /// Constructor; includes a working default configuration
149  DumpChannelToWires()
150  : FirstChannel(raw::InvalidChannelID)
151  , LastChannel(raw::InvalidChannelID)
152  {}
153 
154  /// Sets up the required environment
155  void Setup(geo::GeometryCore const& geometry)
156  { pGeom = &geometry; }
157 
158  /// Sets the lowest and highest channel ID to be printed (inclusive)
159  void SetLimits
160  (raw::ChannelID_t first_channel, raw::ChannelID_t last_channel)
161  { FirstChannel = first_channel; LastChannel = last_channel; }
162 
163  /// Dumps to the specified output category
164  void Dump(std::string OutputCategory) const;
165 
166 
167  protected:
168  geo::GeometryCore const* pGeom = nullptr; ///< pointer to geometry
169 
170  raw::ChannelID_t FirstChannel; ///< lowest channel to be printed
171  raw::ChannelID_t LastChannel; ///< highest channel to be printed
172 
173  /// Throws an exception if the object is not ready to dump
174  void CheckConfig() const;
175 
176  }; // class DumpChannelToWires
177 
178 
179  /// Dumps wire-to-channel mapping
180  class DumpWireToChannel {
181  public:
182 
183  /// Constructor; includes a working default configuration
184  DumpWireToChannel() {}
185 
186  /// Sets up the required environment
187  void Setup(geo::GeometryCore const& geometry)
188  { pGeom = &geometry; }
189 
190  /// Dumps to the specified output category
191  void Dump(std::string OutputCategory) const;
192 
193 
194  protected:
195  geo::GeometryCore const* pGeom = nullptr; ///< pointer to geometry
196 
197  /// Throws an exception if the object is not ready to dump
198  void CheckConfig() const;
199 
200  }; // class DumpWireToChannel
201 
202 
203  /// Dumps optical detector channel-to-optical detector mapping.
204  class DumpOpticalDetectorChannels {
205  public:
206 
207  /// Constructor; includes a working default configuration
208  DumpOpticalDetectorChannels() {}
209 
210  /// Sets up the required environment
211  void Setup(geo::GeometryCore const& geometry)
212  { pGeom = &geometry; }
213 
214  /// Dumps to the specified output category
215  void Dump(std::string OutputCategory) const;
216 
217 
218  protected:
219  geo::GeometryCore const* pGeom = nullptr; ///< pointer to geometry
220 
221  /// Throws an exception if the object is not ready to dump
222  void CheckConfig() const;
223 
224  /// Returns the optical detector serving `channelID`,
225  /// `nullptr` if not found.
226  geo::OpDetGeo const* getOpticalDetector(unsigned int channelID) const;
227 
228  }; // class DumpOpticalDetectorChannels
229 
230 
231 } // local namespace
232 
233 
234 //==============================================================================
235 //=== Module implementation
236 //===
237 
238 //------------------------------------------------------------------------------
240  : art::EDAnalyzer(config)
241  , OutputCategory (config().OutputCategory())
242  , DoChannelToWires(config().ChannelToWires())
243  , DoWireToChannel (config().WireToChannel())
244  , DoOpDetChannels (config().OpDetChannels())
245  , FirstChannel (config().FirstChannel())
246  , LastChannel (config().LastChannel())
247 {
248 
249 } // geo::DumpChannelMap::DumpChannelMap()
250 
251 //------------------------------------------------------------------------------
252 void geo::DumpChannelMap::beginRun(art::Run const&) {
253 
254  geo::GeometryCore const& geom = *(art::ServiceHandle<geo::Geometry const>());
255 
256  if (DoChannelToWires) {
257  DumpChannelToWires dumper;
258  dumper.Setup(geom);
259  dumper.SetLimits(FirstChannel, LastChannel);
260  dumper.Dump(OutputCategory);
261  }
262 
263  if (DoWireToChannel) {
264  DumpWireToChannel dumper;
265  dumper.Setup(geom);
266  // dumper.SetLimits(FirstChannel, LastChannel);
267  dumper.Dump(OutputCategory);
268  }
269 
270  if (DoOpDetChannels) {
271  DumpOpticalDetectorChannels dumper;
272  dumper.Setup(geom);
273  dumper.Dump(OutputCategory);
274  }
275 
276 } // geo::DumpChannelMap::beginRun()
277 
278 //==============================================================================
279 //=== Algorithm implementation
280 //===
281 
282 
283 //------------------------------------------------------------------------------
284 //--- DumpChannelToWires
285 //------------------------------------------------------------------------------
286 void DumpChannelToWires::CheckConfig() const {
287 
288  /// check that the configuration is complete
289  if (!pGeom) {
290  throw art::Exception(art::errors::LogicError)
291  << "DumpChannelToWires: no valid geometry available!";
292  }
293 } // DumpChannelToWires::CheckConfig()
294 
295 //------------------------------------------------------------------------------
296 void DumpChannelToWires::Dump(std::string OutputCategory) const {
297 
298  /// check that the configuration is complete
299  CheckConfig();
300 
301  /// extract general channel range information
302  unsigned int const NChannels = pGeom->Nchannels();
303 
304  if (NChannels == 0) {
305  mf::LogError(OutputCategory)
306  << "Nice detector we have here, with no channels.";
307  return;
308  }
309 
310  raw::ChannelID_t const PrintFirst
311  = raw::isValidChannelID(FirstChannel)? FirstChannel: raw::ChannelID_t(0);
312  raw::ChannelID_t const PrintLast
313  = raw::isValidChannelID(LastChannel)? LastChannel: raw::ChannelID_t(NChannels-1);
314 
315  // print intro
316  unsigned int const NPrintedChannels = (PrintLast - PrintFirst) + 1;
317  if (NPrintedChannels == NChannels) {
318  mf::LogInfo(OutputCategory) << "Printing all " << NChannels << " channels";
319  }
320  else {
321  mf::LogInfo(OutputCategory) << "Printing channels from " << PrintFirst
322  << " to " << LastChannel << " (" << NPrintedChannels
323  << " channels out of " << NChannels << ")";
324  }
325 
326  // print map
327  mf::LogVerbatim log(OutputCategory);
328  for (raw::ChannelID_t channel = PrintFirst; channel <= PrintLast; ++channel) {
329  std::vector<geo::WireID> const Wires = pGeom->ChannelToWire(channel);
330 
331  log << "\n " << ((int) channel) << " ->";
332  switch (Wires.size()) {
333  case 0: log << " no wires"; break;
334  case 1: break;
335  default: log << " [" << Wires.size() << " wires]"; break;
336  } // switch
337 
338  for (geo::WireID const& wireID: Wires)
339  log << " { " << std::string(wireID) << " };";
340 
341  } // for (channels)
342 
343 } // DumpChannelToWires::Dump()
344 
345 //------------------------------------------------------------------------------
346 //--- DumpWireToChannel
347 //------------------------------------------------------------------------------
348 void DumpWireToChannel::CheckConfig() const {
349 
350  /// check that the configuration is complete
351  if (!pGeom) {
352  throw art::Exception(art::errors::LogicError)
353  << "DumpWireToChannel: no valid geometry available!";
354  }
355 } // DumpWireToChannel::CheckConfig()
356 
357 //------------------------------------------------------------------------------
358 void DumpWireToChannel::Dump(std::string OutputCategory) const {
359 
360  /// check that the configuration is complete
361  CheckConfig();
362 
363  /// extract general channel range information
364  unsigned int const NChannels = pGeom->Nchannels();
365 
366  if (NChannels == 0) {
367  mf::LogError(OutputCategory)
368  << "Nice detector we have here, with no channels.";
369  return;
370  }
371 
372  // print intro
373  mf::LogInfo(OutputCategory)
374  << "Printing wire channels for up to " << NChannels << " channels";
375 
376  // print map
377  mf::LogVerbatim log(OutputCategory);
378  for (geo::WireID const& wireID: pGeom->IterateWireIDs()) {
379  raw::ChannelID_t channel = pGeom->PlaneWireToChannel(wireID);
380  log << "\n { " << std::string(wireID) << " } => ";
381  if (raw::isValidChannelID(channel)) log << channel;
382  else log << "invalid!";
383  } // for
384 
385 } // DumpWireToChannel::Dump()
386 
387 
388 //------------------------------------------------------------------------------
389 //--- DumpOpticalDetectorChannels
390 //------------------------------------------------------------------------------
391 void DumpOpticalDetectorChannels::CheckConfig() const {
392 
393  /// check that the configuration is complete
394  if (!pGeom) {
395  throw art::Exception(art::errors::LogicError)
396  << "DumpOpticalDetectorChannels: no valid geometry available!";
397  }
398 } // DumpOpticalDetectorChannels::CheckConfig()
399 
400 
401 //------------------------------------------------------------------------------
402 geo::OpDetGeo const* DumpOpticalDetectorChannels::getOpticalDetector
403  (unsigned int channelID) const
404 {
405  try {
406  return &(pGeom->OpDetGeoFromOpChannel(channelID));
407  }
408  catch (cet::exception const&) {
409  return nullptr;
410  }
411 } // DumpOpticalDetectorChannels::getOpticalDetector()
412 
413 
414 //------------------------------------------------------------------------------
415 void DumpOpticalDetectorChannels::Dump(std::string OutputCategory) const {
416 
417  /// check that the configuration is complete
418  CheckConfig();
419 
420 
421  /// extract general channel range information
422  unsigned int const NChannels = pGeom->NOpChannels();
423 
424  if (NChannels == 0) {
425  mf::LogError(OutputCategory)
426  << "Nice detector we have here, with no optical channels.";
427  return;
428  }
429 
430  // print intro
431  mf::LogInfo(OutputCategory)
432  << "Printing optical detectors for up to " << NChannels << " channels";
433 
434  // print map
435  mf::LogVerbatim log(OutputCategory);
436  for (unsigned int channelID = 0; channelID < NChannels; ++channelID) {
437  log << "\nChannel " << channelID << " => ";
438  geo::OpDetGeo const* opDet = getOpticalDetector(channelID);
439  if (!opDet) {
440  log << "invalid";
441  continue;
442  }
443  log << opDet->ID() << " at " << opDet->GetCenter() << " cm";
444  } // for
445 
446 } // DumpOpticalDetectorChannels::Dump()
447 
448 DEFINE_ART_MODULE(geo::DumpChannelMap)
bool DoOpDetChannels
Dump optical detector channel -&gt; optical detector.
const geo::GeometryCore * geometry
std::string OutputCategory
Name of the category for output.
fhicl::Atom< std::string > OutputCategory
void GetCenter(double *xyz, double localz=0.0) const
Definition: OpDetGeo.cxx:40
DumpChannelMap & operator=(DumpChannelMap const &)=delete
Access the description of detector geometry.
constexpr ChannelID_t InvalidChannelID
ID of an invalid channel.
Definition: RawTypes.h:32
void beginRun(art::Run const &) override
Drives the dumping.
constexpr bool isValidChannelID(raw::ChannelID_t channel)
Definition: RawTypes.h:37
BEGIN_PROLOG vertical distance to the surface Name
bool DoWireToChannel
Dump wire -&gt; channel mapping.
Description of geometry of one entire detector.
Definition of data types for geometry description.
art::EDAnalyzer::Table< Config > Parameters
bool DoChannelToWires
Dump channel -&gt; wires mapping.
raw::ChannelID_t LastChannel
Last channel to be printed.
Encapsulate the geometry of an optical detector.
fhicl::Atom< raw::ChannelID_t > FirstChannel
fhicl::Atom< raw::ChannelID_t > LastChannel
Prints on screen the current channel-wire and optical detector maps.
geo::OpDetID const & ID() const
Returns the geometry ID of this optical detector.
Definition: OpDetGeo.h:72
raw::ChannelID_t FirstChannel
First channel to be printed.
unsigned int ChannelID_t
Type representing the ID of a readout channel.
Definition: RawTypes.h:28
DumpChannelMap(Parameters const &config)
void analyze(art::Event const &) override
art framework interface to geometry description