All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Geometry.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
16 
17 // Framework includes
18 #include "art/Framework/Principal/Run.h"
19 #include "art/Framework/Services/Registry/ActivityRegistry.h"
20 #include "art/Framework/Services/Registry/ServiceHandle.h"
21 #include "messagefacility/MessageLogger/MessageLogger.h"
22 #include "canvas/Utilities/InputTag.h"
23 #include "canvas/Utilities/Exception.h"
24 #include "fhiclcpp/types/Table.h"
25 #include "fhiclcpp/ParameterSet.h"
26 #include "cetlib_except/exception.h"
27 #include "cetlib/search_path.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")) {
52  throw art::Exception(art::errors::Configuration)
53  << "Geometry service does not support `ForceUseFCLOnly` configuration parameter any more.\n";
54  }
55 
56  // add a final directory separator ("/") to fRelPath if not already there
57  if (!fRelPath.empty() && (fRelPath.back() != '/')) fRelPath += '/';
58 
59  // register a callback to be executed when a new run starts
60  reg.sPreBeginRun.watch(this, &Geometry::preBeginRun);
61 
62  //......................................................................
63  // 5.15.12 BJR: use the gdml file for both the fGDMLFile and fROOTFile
64  // variables as ROOT v5.30.06 is once again able to read in gdml files
65  // during batch operation, in this case think of fROOTFile meaning the
66  // file used to make the ROOT TGeoManager. I don't want to remove
67  // the separate variables in case ROOT breaks again
68  std::string GDMLFileName = pset.get<std::string>("GDML");
69  std::string ROOTFileName = pset.get<std::string>("GDML");
70 
71  // load the geometry
72  LoadNewGeometry(GDMLFileName, ROOTFileName);
73 
75 
76  } // Geometry::Geometry()
77 
78 
79  void Geometry::preBeginRun(art::Run const& run)
80  {
81 
82  sumdata::GeometryConfigurationInfo const inputGeomInfo
83  = ReadConfigurationInfo(run);
84  if (!CheckConfigurationInfo(inputGeomInfo)) {
85  if (fNonFatalConfCheck) {
86  // disable the non-fatal option if you need the details
87  mf::LogWarning("Geometry") << "Geometry used for " << run.id()
88  << " is incompatible with the one configured in the job.";
89  }
90  else {
91  throw cet::exception("Geometry")
92  << "Geometry used for run " << run.id()
93  << " is incompatible with the one configured in the job!"
94  << "\n=== job configuration " << std::string(50, '=')
95  << "\n" << fConfInfo
96  << "\n=== run configuration " << std::string(50, '=')
97  << "\n" << inputGeomInfo
98  << "\n======================" << std::string(50, '=')
99  << "\n";
100  }
101  }
102 
103  } // Geometry::preBeginRun()
104 
105 
106  //......................................................................
108  {
109  // the channel map is responsible of calling the channel map configuration
110  // of the geometry
111  art::ServiceHandle<geo::ExptGeoHelperInterface const> helper{};
112  auto channelMapAlg = helper->ConfigureChannelMapAlg(fSortingParameters,
113  DetectorName());
114  if (!channelMapAlg) {
115  throw cet::exception("ChannelMapLoadFail")
116  << " failed to load new channel map";
117  }
118  ApplyChannelMap(move(channelMapAlg));
119  } // Geometry::InitializeChannelMap()
120 
121  //......................................................................
123  std::string gdmlfile, std::string /* rootfile */,
124  bool bForceReload /* = false */
125  ) {
126  // start with the relative path
128 
129  // add the base file names
130  ROOTFileName.append(gdmlfile); // not rootfile (why?)
131  GDMLFileName.append(gdmlfile);
132 
133  // special for GDML if geometry with no wires is used for Geant4 simulation
135  GDMLFileName.insert(GDMLFileName.find(".gdml"), "_nowires");
136 
137  // Search all reasonable locations for the GDML file that contains
138  // the detector geometry.
139  // cet::search_path constructor decides if initialized value is a path
140  // or an environment variable
141  cet::search_path const sp{"FW_SEARCH_PATH"};
142 
143  std::string GDMLfile;
144  if( !sp.find_file(GDMLFileName, GDMLfile) ) {
145  throw cet::exception("Geometry")
146  << "cannot find the gdml geometry file:"
147  << "\n" << GDMLFileName
148  << "\nbail ungracefully.\n";
149  }
150 
151  std::string ROOTfile;
152  if( !sp.find_file(ROOTFileName, ROOTfile) ) {
153  throw cet::exception("Geometry")
154  << "cannot find the root geometry file:\n"
155  << "\n" << ROOTFileName
156  << "\nbail ungracefully.\n";
157  }
158 
159  {
160  fhicl::Table<geo::GeometryBuilderStandard::Config> const config{fBuilderParameters, {"tool_type"}};
161  geo::GeometryBuilderStandard builder{config()};
162 
163  // initialize the geometry with the files we have found
164  LoadGeometryFile(GDMLfile, ROOTfile, builder, bForceReload);
165  }
166 
167  // now update the channel map
169 
170  } // Geometry::LoadNewGeometry()
171 
172  //......................................................................
174  (fhicl::ParameterSet const& config)
175  {
176 
179 
180  // version 1+:
181  confInfo.detectorName = DetectorName();
182 
183  // version 2+:
184  confInfo.geometryServiceConfiguration = config.to_indented_string();
185  fConfInfo = std::move(confInfo);
186 
187  MF_LOG_TRACE("Geometry")
188  << "Geometry configuration information:\n" << fConfInfo;
189 
190  } // Geometry::FillGeometryConfigurationInfo()
191 
192  //......................................................................
195  {
196 
197  MF_LOG_DEBUG("Geometry") << "New geometry information:\n" << other;
198 
199  return CompareConfigurationInfo(fConfInfo, other);
200 
201  } // Geometry::CheckConfigurationInfo()
202 
203  //......................................................................
205  (art::Run const& run)
206  {
207 
208  try {
209  return run.getProduct<sumdata::GeometryConfigurationInfo>
210  (art::InputTag{"GeometryConfigurationWriter"});
211  }
212  catch (art::Exception const& e) {
213  throw art::Exception{
214  e.categoryCode(),
215  "Can't read geometry configuration information.\n"
216  "Is `GeometryConfigurationWriter` service configured?\n",
217  e
218  };
219  }
220 
221  } // Geometry::ReadConfigurationInfo()
222 
223 
224  //......................................................................
228  )
229  {
230  /*
231  * Implemented criteria:
232  *
233  * * both informations must be valid
234  * * the detector names must exactly match
235  *
236  */
237 
238  if (!A.isDataValid()) {
239  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
240  "invalid version for configuration A:\n" << A;
241  return false;
242  }
243  if (!B.isDataValid()) {
244  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
245  "invalid version for configuration B:\n" << B;
246  return false;
247  }
248 
249  // currently used only in debug mode (assert())
250  [[maybe_unused]] auto const commonVersion = std::min(A.dataVersion, B.dataVersion);
251 
252  assert(commonVersion >= 1);
253 
254  if (A.detectorName != B.detectorName) { // case sensitive so far
255  mf::LogWarning("Geometry") << "Geometry::CompareConfigurationInfo(): "
256  "detector name mismatch: '" << A.detectorName << "' vs. '"
257  << B.detectorName << "'";
258  return false;
259  }
260 
261  return true;
262  } // CompareConfigurationInfo()
263 
264 } // namespace geo
double std(const std::vector< short > &wf, const double ped_mean, size_t start, size_t nsample)
Definition: UtilFunc.cxx:42
Utilities related to art service access.
fhicl::ParameterSet fSortingParameters
Parameter set to define the channel map sorting.
Definition: Geometry.h:240
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:174
bool fNonFatalConfCheck
Definition: Geometry.h:238
static constexpr bool
bool fDisableWiresInG4
Definition: Geometry.h:236
static bool CompareConfigurationInfo(sumdata::GeometryConfigurationInfo const &A, sumdata::GeometryConfigurationInfo const &B)
Returns if A and B are compatible geometry service configurations.
Definition: Geometry.cc:225
bool CheckConfigurationInfo(sumdata::GeometryConfigurationInfo const &other) const
Returns if the other configuration is compatible with our current.
Definition: Geometry.cc:194
std::string ROOTFileName
void preBeginRun(art::Run const &run)
Updates the geometry if needed at the beginning of each new run.
Definition: Geometry.cc:79
static sumdata::GeometryConfigurationInfo const & ReadConfigurationInfo(art::Run const &run)
Reads and returns the geometry configuration information from the run.
Definition: Geometry.cc:205
bool isDataValid() const noexcept
Protocol: whether the data content is valid.
fhicl::ParameterSet fBuilderParameters
Parameter set for geometry builder.
Definition: Geometry.h:241
std::string DetectorName() const
Returns a string with the name of the detector, as configured.
std::string fRelPath
Definition: Geometry.h:234
void LoadNewGeometry(std::string gdmlfile, std::string rootfile, bool bForceReload=false)
Expands the provided paths and loads the geometry description(s)
Definition: Geometry.cc:122
Interface to a service that handles any experiment-specific knowledge that is needed by the Geometry ...
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:243
Description of the current configuration of detector geometry.
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.
std::string GDMLFileName
do i e
float A
Definition: dedx.py:137
Extracts of LArSoft geometry information from ROOT.
void InitializeChannelMap()
Definition: Geometry.cc:107
art framework interface to geometry description
Description of the current configuration of detector geometry.