LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
Geometry.cc
Go to the documentation of this file.
1 
8 // class header
10 
11 // lar includes
16 
17 // Framework includes
23 #include "cetlib/search_path.h"
24 #include "cetlib_except/exception.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "fhiclcpp/types/Table.h"
28 
29 // C/C++ standard libraries
30 #include <algorithm> // std::min()
31 #include <cassert>
32 #include <string>
33 #include <utility> // std::move()
34 
35 // check that the requirements for geo::Geometry are satisfied
37 
38 namespace geo {
39 
40  //......................................................................
41  // Constructor.
42  Geometry::Geometry(fhicl::ParameterSet const& pset, art::ActivityRegistry& reg)
43  : GeometryCore(pset)
44  , fRelPath(pset.get<std::string>("RelativePath", ""))
45  , fDisableWiresInG4(pset.get<bool>("DisableWiresInG4", false))
46  , fNonFatalConfCheck(pset.get<bool>("SkipConfigurationCheck", false))
47  , fSortingParameters(pset.get<fhicl::ParameterSet>("SortingParameters", fhicl::ParameterSet()))
48  , fBuilderParameters(pset.get<fhicl::ParameterSet>("Builder", fhicl::ParameterSet()))
49  {
50 
51  if (pset.has_key("ForceUseFCLOnly")) {
53  << "Geometry service does not support `ForceUseFCLOnly` configuration parameter any "
54  "more.\n";
55  }
56 
57  // add a final directory separator ("/") to fRelPath if not already there
58  if (!fRelPath.empty() && (fRelPath.back() != '/')) fRelPath += '/';
59 
60  // register a callback to be executed when a new run starts
61  reg.sPreBeginRun.watch(this, &Geometry::preBeginRun);
62 
63  //......................................................................
64  // 5.15.12 BJR: use the gdml file for both the fGDMLFile and fROOTFile
65  // variables as ROOT v5.30.06 is once again able to read in gdml files
66  // during batch operation, in this case think of fROOTFile meaning the
67  // file used to make the ROOT TGeoManager. I don't want to remove
68  // the separate variables in case ROOT breaks again
69  std::string GDMLFileName = pset.get<std::string>("GDML");
70  std::string ROOTFileName = pset.get<std::string>("GDML");
71 
72  // load the geometry
73  LoadNewGeometry(GDMLFileName, ROOTFileName);
74 
76 
77  } // Geometry::Geometry()
78 
80  {
81 
83  if (!CheckConfigurationInfo(inputGeomInfo)) {
84  if (fNonFatalConfCheck) {
85  // disable the non-fatal option if you need the details
86  mf::LogWarning("Geometry") << "Geometry used for " << run.id()
87  << " is incompatible with the one configured in the job.";
88  }
89  else {
90  throw cet::exception("Geometry")
91  << "Geometry used for run " << run.id()
92  << " is incompatible with the one configured in the job!"
93  << "\n=== job configuration " << std::string(50, '=') << "\n"
94  << fConfInfo << "\n=== run configuration " << std::string(50, '=') << "\n"
95  << inputGeomInfo << "\n======================" << std::string(50, '=') << "\n";
96  }
97  }
98 
99  } // Geometry::preBeginRun()
100 
101  //......................................................................
103  {
104  // the channel map is responsible of calling the channel map configuration
105  // of the geometry
107  auto channelMapAlg = helper->ConfigureChannelMapAlg(fSortingParameters, DetectorName());
108  if (!channelMapAlg) {
109  throw cet::exception("ChannelMapLoadFail") << " failed to load new channel map";
110  }
111  ApplyChannelMap(move(channelMapAlg));
112  } // Geometry::InitializeChannelMap()
113 
114  //......................................................................
115  void Geometry::LoadNewGeometry(std::string gdmlfile,
116  std::string /* rootfile */,
117  bool bForceReload /* = false */
118  )
119  {
120  // start with the relative path
121  std::string GDMLFileName(fRelPath), ROOTFileName(fRelPath);
122 
123  // add the base file names
124  ROOTFileName.append(gdmlfile); // not rootfile (why?)
125  GDMLFileName.append(gdmlfile);
126 
127  // special for GDML if geometry with no wires is used for Geant4 simulation
128  if (fDisableWiresInG4) GDMLFileName.insert(GDMLFileName.find(".gdml"), "_nowires");
129 
130  // Search all reasonable locations for the GDML file that contains
131  // the detector geometry.
132  // cet::search_path constructor decides if initialized value is a path
133  // or an environment variable
134  cet::search_path const sp{"FW_SEARCH_PATH"};
135 
136  std::string GDMLfile;
137  if (!sp.find_file(GDMLFileName, GDMLfile)) {
138  throw cet::exception("Geometry") << "cannot find the gdml geometry file:"
139  << "\n"
140  << GDMLFileName << "\nbail ungracefully.\n";
141  }
142 
143  std::string ROOTfile;
144  if (!sp.find_file(ROOTFileName, ROOTfile)) {
145  throw cet::exception("Geometry") << "cannot find the root geometry file:\n"
146  << "\n"
147  << ROOTFileName << "\nbail ungracefully.\n";
148  }
149 
150  {
152  {"tool_type"}};
153  geo::GeometryBuilderStandard builder{config()};
154 
155  // initialize the geometry with the files we have found
156  LoadGeometryFile(GDMLfile, ROOTfile, builder, bForceReload);
157  }
158 
159  // now update the channel map
161 
162  } // Geometry::LoadNewGeometry()
163 
164  //......................................................................
166  {
167 
170 
171  // version 1+:
172  confInfo.detectorName = DetectorName();
173 
174  // version 2+:
176  fConfInfo = std::move(confInfo);
177 
178  MF_LOG_TRACE("Geometry") << "Geometry configuration information:\n" << fConfInfo;
179 
180  } // Geometry::FillGeometryConfigurationInfo()
181 
182  //......................................................................
184  {
185 
186  MF_LOG_DEBUG("Geometry") << "New geometry information:\n" << other;
187 
188  return CompareConfigurationInfo(fConfInfo, other);
189 
190  } // Geometry::CheckConfigurationInfo()
191 
192  //......................................................................
194  {
195 
196  try {
198  art::InputTag{"GeometryConfigurationWriter"});
199  }
200  catch (art::Exception const& e) {
201  throw art::Exception{e.categoryCode(),
202  "Can't read geometry configuration information.\n"
203  "Is `GeometryConfigurationWriter` service configured?\n",
204  e};
205  }
206 
207  } // Geometry::ReadConfigurationInfo()
208 
209  //......................................................................
212  {
213  /*
214  * Implemented criteria:
215  *
216  * * both informations must be valid
217  * * the detector names must exactly match
218  *
219  */
220 
221  if (!A.isDataValid()) {
222  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
223  "invalid version for configuration A:\n"
224  << A;
225  return false;
226  }
227  if (!B.isDataValid()) {
228  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
229  "invalid version for configuration B:\n"
230  << B;
231  return false;
232  }
233 
234  // currently used only in debug mode (assert())
235  [[maybe_unused]] auto const commonVersion = std::min(A.dataVersion, B.dataVersion);
236 
237  assert(commonVersion >= 1);
238 
239  if (A.detectorName != B.detectorName) { // case sensitive so far
240  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
241  "detector name mismatch: '"
242  << A.detectorName << "' vs. '" << B.detectorName << "'";
243  return false;
244  }
245 
246  return true;
247  } // CompareConfigurationInfo()
248 
249 } // namespace geo
void LoadGeometryFile(std::string gdmlfile, std::string rootfile, GeometryBuilder &builder, bool bForceReload=false)
Loads the geometry information from the specified files.
Utilities related to art service access.
RunID id() const
Definition: Run.cc:21
fhicl::ParameterSet fSortingParameters
Parameter set to define the channel map sorting.
Definition: Geometry.h:228
DataVersion_t dataVersion
Version of the data in this object (0 is invalid version).
std::string geometryServiceConfiguration
geo::Geometry service configuration, as FHiCL table.
void FillGeometryConfigurationInfo(fhicl::ParameterSet const &config)
Fills the service configuration information into fConfInfo.
Definition: Geometry.cc:165
bool fNonFatalConfCheck
Definition: Geometry.h:226
STL namespace.
bool fDisableWiresInG4
Definition: Geometry.h:224
static bool CompareConfigurationInfo(sumdata::GeometryConfigurationInfo const &A, sumdata::GeometryConfigurationInfo const &B)
Returns if A and B are compatible geometry service configurations.
Definition: Geometry.cc:210
bool CheckConfigurationInfo(sumdata::GeometryConfigurationInfo const &other) const
Returns if the other configuration is compatible with our current.
Definition: Geometry.cc:183
void preBeginRun(art::Run const &run)
Updates the geometry if needed at the beginning of each new run.
Definition: Geometry.cc:79
Definition: Run.h:37
static sumdata::GeometryConfigurationInfo const & ReadConfigurationInfo(art::Run const &run)
Reads and returns the geometry configuration information from the run.
Definition: Geometry.cc:193
fhicl::ParameterSet fBuilderParameters
Parameter set for geometry builder.
Definition: Geometry.h:229
parameter set interface
T get(std::string const &key) const
Definition: ParameterSet.h:314
bool isDataValid() const noexcept
Protocol: whether the data content is valid.
#define MF_LOG_TRACE(id)
std::string fRelPath
Definition: Geometry.h:222
void LoadNewGeometry(std::string gdmlfile, std::string rootfile, bool bForceReload=false)
Expands the provided paths and loads the geometry description(s)
Definition: Geometry.cc:115
std::string to_indented_string() const
bool has_key(std::string const &key) const
Interface to a service that handles any experiment-specific knowledge that is needed by the Geometry ...
Description of geometry of one entire detector.
Definition: GeometryCore.h:119
sumdata::GeometryConfigurationInfo fConfInfo
Summary of service configuration.
Definition: Geometry.h:231
Description of the current configuration of detector geometry.
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
GlobalSignal< detail::SignalResponseType::FIFO, void(Run const &)> sPreBeginRun
Standard implementation of geometry extractor.
decltype(auto) get(T &&obj)
ADL-aware version of std::to_string.
Definition: StdUtils.h:120
unsigned int DataVersion_t
Type used for the version of data.
void ApplyChannelMap(std::unique_ptr< ChannelMapAlg > pChannelMap)
Initializes the geometry to work with this channel map.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
#define MF_LOG_DEBUG(id)
std::string const & DetectorName() const
Returns a string with the name of the detector, as configured.
Definition: GeometryCore.h:203
Float_t e
Definition: plot.C:35
Namespace collecting geometry-related classes utilities.
PROD const & getProduct(InputTag const &tag) const
Extracts of LArSoft geometry information from ROOT.
void InitializeChannelMap()
Definition: Geometry.cc:102
art framework interface to geometry description
cet::coded_exception< error, detail::translate > exception
Definition: exception.h:33
Description of the current configuration of detector geometry.