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