GeometryGArConfigurationWriter_service.cc
Go to the documentation of this file.
1 /**
2  * @file Geometry/GeometryGArConfigurationWriter_service.cc
3  * @brief Service writing geometry configuration information into _art_ runs.
4  * @date March 22, 2021
5  * @author Eldwan Brianne (ebrianne@fnal.gov)
6  *
7  * Producing services have no header.
8  */
9 
10 // GArSoft libraries
11 #include "Geometry/GeometryGAr.h"
14 
15 // framework libraries
22 
23 // C/C++ standard libraries
24 #include <memory> // std::make_unique()
25 
26 
27 // -----------------------------------------------------------------------------
28 namespace gar {
29  namespace geo {
30  class GeometryGArConfigurationWriter;
31  }
32 }
33 
34 /**
35  * @brief Writes geometry configuration information into _art_ runs.
36  *
37  * This service is part of the mandatory version check of `gar::geo::Geometry`
38  * service.
39  * It does not require any special configuration, but it must be listed
40  * in the configuration in order for `Geometry` to work:
41  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
42  * services: {
43  *
44  * GeometryGArConfigurationWriter: {}
45  *
46  * Geometry: @local::experiment_geometry
47  *
48  * # ...
49  *
50  * }
51  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
52  *
53  * The configuration check is described in the documentation of `gar::geo::Geometry`
54  * service.
55  *
56  *
57  * Produced data products
58  * -----------------------
59  *
60  * The service guarantees that configuration information of type
61  * `sumdata::GeometryConfigurationInfo` is present into the run, accessible with
62  * an input tag `GeometryGArConfigurationWriter`:
63  *
64  * * if such information is already available in the run, no further information
65  * is added
66  * * legacy: if there is no such information, but there is a `sumdata::RunData`
67  * data product, a reduced version of the configuration information is created
68  * from the information in that data product (the first one, if multiple are
69  * present)
70  * * finally, if no information is present neither in the full
71  * `sumdata::GeometryConfigurationInfo` form nor in the legacy
72  * `sumdata::RunData` form, information is put together based on the current
73  * configuration of the `Geometry` service.
74  *
75  *
76  * Service dependencies
77  * ---------------------
78  *
79  * * `Geometry` service
80  * (for obtaining the current configuration to put into the event)
81  *
82  */
84 
85  public:
86 
87  /// Service configuration.
88  struct Config {
89  // no configuration parameters at this time
90  };
91 
93 
94  /// Constructor: gets its configuration and does nothing with it.
96 
97  private:
98 
99  /// Writes the information from the service configuration into the `run`.
100  virtual void postReadRun(art::Run& run) override;
101 
102  private:
103 
104  /// Alias for the pointer to the data product object to be put into the run.
105  using InfoPtr_t = std::unique_ptr<gar::sumdata::GeometryConfigurationInfo>;
106 
107 
108  /// Loads the geometry information from the `run` (either directly or legacy).
109  InfoPtr_t loadInfo(art::Run& run) const;
110 
111  /// Creates configuration information based on the current `Geometry` service.
113 
114  /// Reads geometry information from the run (returns null pointer if none).
116 
117  /// Upgrades legacy `sumdata::RunData` in `run` to geometry information
118  /// (returns null pointer if no legacy information is present).
120 
121  /// Returns a pointer to the `sumdata::RunData` in `run` (nullptr if none).
123 
124  /// Converts the legacy `data` into geometry configuration information.
126 
127  /// Alias to `std::make_unique<sumdata::GeometryConfigurationInfo>`.
128  static InfoPtr_t makeInfoPtr(gar::sumdata::GeometryConfigurationInfo const& info) { return std::make_unique<gar::sumdata::GeometryConfigurationInfo>(info); }
129 
130 
131 }; // gar::geo::GeometryGArConfigurationWriter
132 
133 
134 // -----------------------------------------------------------------------------
135 // --- implementation
136 // -----------------------------------------------------------------------------
138 {
139  produces<gar::sumdata::GeometryConfigurationInfo, art::InRun>();
140 }
141 
142 
143 // -----------------------------------------------------------------------------
145 
147 
148  if (!confInfo) confInfo = extractInfoFromGeometry();
149 
150  run.put(std::move(confInfo), art::fullRun());
151 
152 } // gar::geo::GeometryGArConfigurationWriter::postReadRun()
153 
154 
155 // -----------------------------------------------------------------------------
157 {
158 
159  /*
160  * Read geometry configuration information from the run:
161  *
162  * 1. first attempt to directly read information from past runs of this
163  * service
164  * 2. if none is found, attempt reading legacy information and upgrade it
165  * 3. if no legacy information is found either, return a null pointer
166  *
167  */
169 
170  return info ? std::move(info) : makeInfoFromRunData(run);
171 
172 } // gar::geo::GeometryGArConfigurationWriter::loadInfo()
173 
174 
175 // -----------------------------------------------------------------------------
177 
179 
180  MF_LOG_DEBUG("GeometryGArConfigurationWriter") << "Geometry configuration information from service:\n"
181  << confInfo;
182 
183  return makeInfoPtr(std::move(confInfo));
184 
185 } // gar::geo::GeometryGArConfigurationWriter::extractInfoFromGeometry()
186 
187 
188 // -----------------------------------------------------------------------------
190 {
191  auto infoHandle = run.getHandle<gar::sumdata::GeometryConfigurationInfo>(art::InputTag{"GeometryGArConfigurationWriter"});
192  if (infoHandle)
193  {
194  return makeInfoPtr(*infoHandle);
195  }
196  return InfoPtr_t{};
197 
198 } // gar::geo::GeometryGArConfigurationWriter::hasGeometryInformation()
199 
200 
201 // -----------------------------------------------------------------------------
203 {
204 
205  gar::sumdata::RunData const* runData = readRunData(run);
206  return runData ? convertRunDataToGeometryInformation(*runData): InfoPtr_t{};
207 
208 } // gar::geo::GeometryGArConfigurationWriter::makeInfoFromRunData()
209 
210 
211 // -----------------------------------------------------------------------------
213 {
214  auto allRunData = run.getMany<gar::sumdata::RunData>();
215 
216  return allRunData.empty() ? nullptr: allRunData.front().product();
217 } // gar::geo::GeometryGArConfigurationWriter::readRunData()
218 
219 
220 // -----------------------------------------------------------------------------
222 {
223 
225 
226  // we use the simplest version 1 data format (legacy format)
228  confInfo.detectorName = data.DetName();
229 
230  MF_LOG_DEBUG("GeometryGArConfigurationWriter")
231  << "Built geometry configuration information from run data:\n"
232  << confInfo;
233 
234  return makeInfoPtr(std::move(confInfo));
235 
236 } // gar::geo::GeometryGArConfigurationWriter::convertRunDataToGeometryInformation()
237 
238 
239 // -----------------------------------------------------------------------------
241 
242 
243 // -----------------------------------------------------------------------------
constexpr auto fullRun()
DataVersion_t dataVersion
Version of the data in this object (0 is invalid version).
Definition: Run.h:17
std::unique_ptr< gar::sumdata::GeometryConfigurationInfo > InfoPtr_t
Alias for the pointer to the data product object to be put into the run.
InfoPtr_t loadInfo(art::Run &run) const
Loads the geometry information from the run (either directly or legacy).
virtual void postReadRun(art::Run &run) override
Writes the information from the service configuration into the run.
Description of the current configuration of detector geometry.
std::vector< Handle< PROD > > getMany(SelectorBase const &selector=MatchAllSelector{}) const
Definition: DataViewImpl.h:479
def move(depos, offset)
Definition: depos.py:107
unsigned int DataVersion_t
Type used for the version of data.
gar::sumdata::RunData const * readRunData(art::Run &run) const
Returns a pointer to the sumdata::RunData in run (nullptr if none).
ProductID put(std::unique_ptr< PROD > &&edp, std::string const &instance={})
Definition: DataViewImpl.h:686
#define DEFINE_ART_PRODUCING_SERVICE(klass)
static InfoPtr_t makeInfoPtr(gar::sumdata::GeometryConfigurationInfo const &info)
Alias to std::make_unique<sumdata::GeometryConfigurationInfo>.
General GArSoft Utilities.
GeometryGArConfigurationWriter(Parameters const &)
Constructor: gets its configuration and does nothing with it.
Writes geometry configuration information into art runs.
#define MF_LOG_DEBUG(id)
LArSoft geometry interface.
Definition: ChannelGeo.h:16
art framework interface to geometry description
static InfoPtr_t extractInfoFromGeometry()
Creates configuration information based on the current Geometry service.
static InfoPtr_t convertRunDataToGeometryInformation(gar::sumdata::RunData const &data)
Converts the legacy data into geometry configuration information.
InfoPtr_t readGeometryInformation(art::Run &run) const
Reads geometry information from the run (returns null pointer if none).