All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PMTconfigurationExtraction_module.cc
Go to the documentation of this file.
1 /**
2  * @file PMTconfigurationExtraction_module.cc
3  * @brief Writes PMT configuration from FHiCL into data product.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date February 23, 2021
6  */
7 
8 // ICARUS libraries
12 
13 // framework libraries
14 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
15 #include "canvas/Persistency/Provenance/ProcessHistory.h"
16 #include "fhiclcpp/ParameterSet.h"
17 
18 #include "art/Framework/Services/Registry/ServiceHandle.h"
19 #include "art/Framework/Core/EDProducer.h"
20 #include "art/Framework/Core/ModuleMacros.h"
21 #include "art/Framework/Core/FileBlock.h"
22 #include "art/Framework/Principal/Run.h"
23 #include "messagefacility/MessageLogger/MessageLogger.h"
24 #include "fhiclcpp/types/Atom.h"
25 #include "cetlib_except/exception.h"
26 
27 // C/C++ standard libraries
28 #include <memory> // std::unique_ptr<>
29 #include <optional>
30 #include <string>
31 #include <utility> // std::move()
32 #include <cassert>
33 
34 
35 // -----------------------------------------------------------------------------
36 namespace icarus { class PMTconfigurationExtraction; }
37 /**
38  * @brief Writes PMT configuration from FHiCL into a data product.
39  *
40  * This module reads the configuration related to PMT from the FHiCL
41  * configuration of the input runs and puts it into each run as a data product.
42  *
43  * Input
44  * ------
45  *
46  * This module requires any input with _art_ run objects in it.
47  * The format expected for that configuration is defined in
48  * `icarus::PMTconfigurationExtractor`, which is the utility used for the actual
49  * extraction.
50  *
51  *
52  * Output
53  * -------
54  *
55  * A data product of type `sbn::PMTconfiguration` is placed in each run.
56  * Note that the module itself does not enforce any coherence in the
57  * configuration.
58  *
59  *
60  * Configuration parameters
61  * -------------------------
62  *
63  * The following configuration parameters are supported:
64  *
65  * * **AssignOfflineChannelIDs** (flag, default: `true`): when set, service
66  * `IICARUSChannelMap` is used to track the channel numbers back to the
67  * LArSoft PMT channel IDs, and this information is saved together with
68  * the channel information; if the service is not available, this flag
69  * should be set to `false`, in which case the channel ID will be marked
70  * as unknown (`sbn::V1730channelConfiguration::NoChannelID`).
71  * * **RequireConsistentPMTconfigurations** (flag, default: `true`): requires
72  * that all input files contain compatible PMT configuration; while this is
73  * in general desired during decoding, when mixing files with different
74  * runs in the same process this check might fail.
75  * * **Verbose** (flag, default: `false`): if set to `true`, it will print in
76  * full the configuration of the PMT the first time it is read and each time
77  * a different one is found.
78  * * **LogCategory** (string, default: `PMTconfigurationExtraction`):
79  * category tag used for messages to message facility.
80  *
81  *
82  * Multithreading
83  * ---------------
84  *
85  * This module does not support multithreading, and _art_ does not provide
86  * multithreading for its functionality anyway: the only action is performed
87  * at run and input file level, and the only concurrency in _art_ is currently
88  * (_art_ 3.7) at event level.
89  *
90  */
91 class icarus::PMTconfigurationExtraction: public art::EDProducer {
92 
93  /// Current PMT configuration (may be still unassigned).
94  std::optional<sbn::PMTconfiguration> fPMTconfig;
95 
96  /// Pointer to the online channel mapping service.
98 
99  /// Whether PMT configuration inconsistency is fatal.
100  bool fRequireConsistency = true;
101 
102  bool fVerbose = false; ///< Whether to print the configuration we read.
103 
104  std::string fLogCategory; ///< Category tag for messages.
105 
106  public:
107 
108  /// Configuration of the module.
109  struct Config {
110 
111  fhicl::Atom<bool> AssignOfflineChannelIDs {
112  fhicl::Name("AssignOfflineChannelIDs"),
113  fhicl::Comment
114  ("retrieves LArSoft channel ID of each PMT; requires IICARUSChannelMap service"),
115  true // default
116  };
117 
119  fhicl::Name("RequireConsistentPMTconfigurations"),
120  fhicl::Comment
121  ("checks that all input files carry a compatible PMT configuration"),
122  true // default
123  };
124 
125  fhicl::Atom<bool> Verbose {
126  fhicl::Name("Verbose"),
127  fhicl::Comment("print in full each new PMT configuration read"),
128  false // default
129  };
130 
131  fhicl::Atom<std::string> LogCategory {
132  fhicl::Name("LogCategory"),
133  fhicl::Comment("category tag used for messages to message facility"),
134  "PMTconfigurationExtraction" // default
135  };
136 
137  }; // struct Config
138 
139  using Parameters = art::EDProducer::Table<Config>;
140 
141 
142  /// Constructor: just reads the configuration.
143  PMTconfigurationExtraction(Parameters const& config);
144 
145 
146  /// Action on new run: configuration is written.
147  virtual void beginRun(art::Run& run) override;
148 
149  /// Mandatory method, unused.
150  virtual void produce(art::Event&) override {}
151 
152 
153  private:
154 
155  /// Throws an exception if the `newConfig` is not compatible with the current.
156  bool checkConsistency
157  (sbn::PMTconfiguration const& newConfig, std::string const& srcName) const;
158 
159 
160 }; // icarus::PMTconfigurationExtraction
161 
162 
163 // -----------------------------------------------------------------------------
165  (Parameters const& config)
166  : art::EDProducer(config)
167  , fChannelMap(config().AssignOfflineChannelIDs()
168  ? art::ServiceHandle<icarusDB::IICARUSChannelMap const>{}.get()
169  : nullptr
170  )
171  , fRequireConsistency(config().RequireConsistentPMTconfigurations())
172  , fVerbose(config().Verbose())
173  , fLogCategory(config().LogCategory())
174 {
175 
176  // no consummation here (except for FHiCL configuration)
177 
178  produces<sbn::PMTconfiguration, art::InRun>();
179 
180 } // icarus::PMTconfigurationExtraction::PMTconfigurationExtraction()
181 
182 
183 // -----------------------------------------------------------------------------
185 
188 
189  checkConsistency(config, "run " + std::to_string(run.run()));
190  if (!fPMTconfig.has_value() && fVerbose)
191  mf::LogInfo(fLogCategory) << "PMT readout:" << config;
192 
193  fPMTconfig = std::move(config);
194 
195  // put a copy of the current configuration
196  run.put(std::make_unique<sbn::PMTconfiguration>(fPMTconfig.value()));
197 
198 } // icarus::PMTconfigurationExtraction::beginRun()
199 
200 
201 // -----------------------------------------------------------------------------
203  (sbn::PMTconfiguration const& config, std::string const& srcName) const
204 {
205  if (!fPMTconfig.has_value() || (*fPMTconfig == config)) return true;
206 
207  // see debug information for more details
208  if (fRequireConsistency) {
209  throw cet::exception("PMTconfigurationExtraction")
210  << "Configuration from " << srcName
211  << " is incompatible with the previously found configuration.\n"
212  ;
213  } // if consistency is required
214 
215  mf::LogWarning(fLogCategory)
216  << "Configuration from " << srcName
217  << " is incompatible with the previously found configuration.\n"
218  ;
219  if (fVerbose) mf::LogVerbatim(fLogCategory) << "PMT readout:" << config;
220  return false;
221 
222 
223 } // icarus::PMTconfigurationExtraction::checkConsistency()
224 
225 
226 // -----------------------------------------------------------------------------
227 DEFINE_ART_MODULE(icarus::PMTconfigurationExtraction)
228 
229 
230 // -----------------------------------------------------------------------------
icarusDB::IICARUSChannelMap const * fChannelMap
Pointer to the online channel mapping service.
virtual void beginRun(art::Run &run) override
Action on new run: configuration is written.
sbn::PMTconfiguration extractPMTreadoutConfiguration(std::string const &srcFileName, icarus::PMTconfigurationExtractor extractor)
fDetProps &fDetProps fDetProps &fDetProps fLogCategory
Writes PMT configuration from FHiCL into a data product.
virtual void produce(art::Event &) override
Mandatory method, unused.
bool fRequireConsistency
Whether PMT configuration inconsistency is fatal.
bool fVerbose
Whether to print the configuration we read.
BEGIN_PROLOG vertical distance to the surface Name
Information from the configuration of PMT readout.
bool checkConsistency(sbn::PMTconfiguration const &newConfig, std::string const &srcName) const
Throws an exception if the newConfig is not compatible with the current.
std::string fLogCategory
Category tag for messages.
Class to extract PMT readout board configuration.
PMTconfigurationExtraction(Parameters const &config)
Constructor: just reads the configuration.
std::string to_string(WindowPattern const &pattern)
Utility to extract PMT readout configuration from data.
Class containing configuration for PMT readout.
std::optional< sbn::PMTconfiguration > fPMTconfig
Current PMT configuration (may be still unassigned).