LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
geometry_unit_test_base.h
Go to the documentation of this file.
1 
30 #ifndef TEST_GEOMETRY_UNIT_TEST_BASE_H
31 #define TEST_GEOMETRY_UNIT_TEST_BASE_H
32 
33 // LArSoft libraries
37 
38 // utility libraries
39 #include "fhiclcpp/ParameterSet.h"
41 
42 // CET libraries
43 #include "cetlib/filepath_maker.h"
44 #include "cetlib/filesystem.h" // cet::is_absolute_filepath()
45 #include "cetlib/search_path.h"
46 
47 // C/C++ standard libraries
48 #include <iostream> // for output before message facility is set up
49 #include <map>
50 #include <memory> // std::unique_ptr<>
51 #include <string>
52 
53 namespace testing {
54 
66  template <typename CHANNELMAP>
68  using ChannelMapClass = CHANNELMAP;
69 
72 
75  : BasicEnvironmentConfiguration(argc, argv)
76  {
77  LocalInit();
78  }
79 
82  {
83  LocalInit();
84  }
85 
86  BasicGeometryEnvironmentConfiguration(int argc, char** argv, std::string name)
87  : BasicEnvironmentConfiguration(argc, argv, name)
88  {
89  LocalInit();
90  }
91 
95  std::string GeometryParameterSetPath() const
96  {
98  }
99 
101  std::string DefaultGeometryConfiguration() const
102  {
104  }
105 
107 
110 
112  void SetGeometryParameterSetPath(std::string path)
113  {
115  }
116 
118  void SetDefaultGeometryConfiguration(std::string cfg)
119  {
121  }
122 
124 
126  static std::string GeometryServiceName() { return "Geometry"; }
127 
128  protected:
130  void LocalInit()
131  {
133  SurfaceY: 200. # in cm, vertical distance to the surface
134  Name: "lartpcdetector"
135  GDML: "LArTPCdetector.gdml"
136  ROOT: "LArTPCdetector.gdml"
137  SortingParameters: {} # empty parameter set for default
138  )");
139  } // LocalInit()
140 
141  }; // class BasicGeometryEnvironmentConfiguration<>
142 
189  template <typename ConfigurationClass>
190  class GeometryTesterEnvironment : public TesterEnvironment<ConfigurationClass> {
191 
194 
197 
198  public:
200 
207  GeometryTesterEnvironment(bool bSetup = true) : TesterEnvironment_t(false)
208  {
209  if (bSetup) Setup();
210  }
211 
213 
225  GeometryTesterEnvironment(ConfigurationClass const& cfg_obj, bool bSetup = true)
226  : TesterEnvironment_t(cfg_obj, false)
227  {
228  if (bSetup) Setup();
229  }
230  GeometryTesterEnvironment(ConfigurationClass&& cfg_obj, bool bSetup = true)
231  : TesterEnvironment_t(std::move(cfg_obj), false)
232  {
233  if (bSetup) Setup();
234  }
236 
238  virtual ~GeometryTesterEnvironment();
239 
241  geo::GeometryCore const* Geometry() const { return geom.get(); }
243  SharedGeoPtr_t SharedGeometry() const { return geom; }
245 
248  static geo::GeometryCore const* GlobalGeometry() { return &GeoResources_t::Resource(); }
249 
251  static SharedGeoPtr_t SharedGlobalGeometry() { return GeoResources_t::ShareResource(); }
252 
253  protected:
254  using ChannelMapClass = typename ConfigurationClass::ChannelMapClass;
255 
257  virtual void Setup();
258 
260  virtual std::unique_ptr<geo::GeometryCore> CreateNewGeometry() const;
261 
263  virtual void RegisterGeometry(SharedGeoPtr_t new_geom);
265  virtual void RegisterGeometry(geo::GeometryCore const* new_geom)
266  {
267  RegisterGeometry(SharedGeoPtr_t(new_geom));
268  }
270 
272  virtual void SetupGeometry();
273 
274  private:
275  ConfigurationClass config;
276 
278 
279  }; // class GeometryTesterEnvironment<>
280 
281  //****************************************************************************
282  template <typename ConfigurationClass>
284  {
285 
286  mf::LogInfo("Test") << config.ApplicationName() << " completed.";
287 
288  } // GeometryTesterEnvironment<>::~GeometryTesterEnvironment()
289 
300  template <typename ConfigurationClass>
301  std::unique_ptr<geo::GeometryCore>
303  {
304 
305  std::string ProviderParameterSetPath = this->Config().GeometryParameterSetPath();
306 
307  //
308  // create the new geometry service provider
309  //
310  fhicl::ParameterSet ProviderConfig =
311  this->Parameters().template get<fhicl::ParameterSet>(ProviderParameterSetPath);
312  auto new_geom = std::make_unique<geo::GeometryCore>(ProviderConfig);
313 
314  std::string RelativePath = ProviderConfig.get<std::string>("RelativePath", "");
315 
316  std::string GDMLFileName = RelativePath + ProviderConfig.get<std::string>("GDML"),
317  ROOTFileName = RelativePath + ProviderConfig.get<std::string>("ROOT");
318 
319  // Search all reasonable locations for the geometry file;
320  // we see if by any chance art's FW_SEARCH_PATH directory is set and try
321  // there;
322  // if not, we do expect the path to be complete enough for ROOT to cope.
323  cet::search_path sp("FW_SEARCH_PATH");
324 
325  std::string ROOTfile;
326  if (!sp.find_file(ROOTFileName, ROOTfile)) ROOTfile = ROOTFileName;
327 
328  // we really don't care of GDML file, since we are not going to run Geant4
329  std::string GDMLfile;
330  if (!sp.find_file(GDMLFileName, GDMLfile)) {
331  mf::LogWarning("CreateNewGeometry") << "GDML file '" << GDMLfile << "' not found.";
332  }
333 
334  // initialize the geometry with the files we have found
335  new_geom->LoadGeometryFile(GDMLfile, ROOTfile);
336 
337  //
338  // create the new channel map
339  //
340  auto const SortingParameters = ProviderConfig.get<fhicl::ParameterSet>("SortingParameters", {});
341 
342  // connect the channel map with the geometry, that shares ownsership
343  // (we give up ours at the end of this method)
344  new_geom->ApplyChannelMap(std::make_unique<ChannelMapClass>(SortingParameters));
345 
346  return new_geom;
347  } // GeometryTesterEnvironment<>::CreateNewGeometry()
348 
349  template <typename ConfigurationClass>
351  {
352  // update the current geometry, that becomes owner;
353  // also update the global one if it happens to be already our previous
354  // (in this case, it becomes co-owner)
355  SharedGeoPtr_t my_old_geom = geom;
356  geom = new_geom;
357  // if the global geometry is already the one we register, don't bother
358  if (SharedGlobalGeometry() != new_geom)
359  GeoResources_t::ReplaceDefaultSharedResource(my_old_geom, new_geom);
360  } // GeometryTesterEnvironment<>::RegisterGeometry()
361 
362  template <typename ConfigurationClass>
364  {
365  //
366  // horrible, shameful hack to support the "new" testing environment
367  // while the old one, informally deprecated, is still around;
368  // we will have TWO versions of GeometryCore around.
369  // Ugh.
370  //
371  RegisterGeometry(CreateNewGeometry()); // old
372  // new
373  this->template AcquireProvider<geo::GeometryCore>(CreateNewGeometry());
374  } // GeometryTesterEnvironment<>::SetupGeometry()
375 
376  template <typename ConfigurationClass>
378  {
379 
380  //
381  // parse configuration, set up message facility
382  //
383  TesterEnvironment_t::Setup();
384 
385  //
386  // set up the geometry
387  //
388  SetupGeometry();
389 
390  mf::LogInfo("Test") << config.ApplicationName() << " Geometry setup complete.";
391 
392  } // GeometryTesterEnvironment<>::Setup()
393 
394 } // namespace testing
395 
396 #endif // TEST_GEOMETRY_UNIT_TEST_BASE_H
virtual ~GeometryTesterEnvironment()
Destructor: closing remarks.
LArSoft test utilities.
static std::string GeometryServiceName()
Returns the name of the service.
virtual void RegisterGeometry(geo::GeometryCore const *new_geom)
Get ownership of the specified geometry and registers it as global.
BasicGeometryEnvironmentConfiguration()
Default constructor; this is what is used in Boost unit test.
void LocalInit()
Initialize with some default values.
MaybeLogger_< ELseverityLevel::ELsev_info, false > LogInfo
static geo::GeometryCore const * GlobalGeometry()
std::shared_ptr< Resource_t > ResourcePtr_t
std::string DefaultGeometryConfiguration() const
A string describing the default parameter set to configure geometry.
virtual void RegisterGeometry(SharedGeoPtr_t new_geom)
Get ownership of the specified geometry and registers it as global.
Class holding a configuration for a test environment.
STL namespace.
std::string GeometryParameterSetPath() const
A string describing the default parameter set to configure geometry.
SharedGeoPtr_t SharedGeometry() const
Returns a pointer to the geometry.
typename ConfigurationClass::ChannelMapClass ChannelMapClass
SharedGeoPtr_t geom
pointer to the geometry
static SharedGeoPtr_t SharedGlobalGeometry()
Returns the current global geometry instance (may be nullptr if none)
void AddDefaultServiceConfiguration(std::string service_name, std::string service_cfg)
Adds a default configuration for the specified service.
Access the description of detector geometry.
std::string ServiceParameterSetPath(std::string name) const
FHiCL path for the configuration of the service.
ConfigurationClass config
instance of the configurer
BasicGeometryEnvironmentConfiguration(int argc, char **argv)
Constructor: acquires parameters from the command line.
void SetGeometryParameterSetPath(std::string path)
Sets the FHiCL path for the geometry configuration.
GeoResources_t::ResourcePtr_t SharedGeoPtr_t
std::string DefaultServiceConfiguration(std::string service_name) const
A string describing the default parameter set to configure the test.
Environment for a geometry test.
T get(std::string const &key) const
Definition: ParameterSet.h:314
BasicGeometryEnvironmentConfiguration(int argc, char **argv, std::string name)
GeometryTesterEnvironment(bool bSetup=true)
Constructor: sets everything up and declares the test started.
Utility class providing singleton objects to the derived classes.
void SetDefaultGeometryConfiguration(std::string cfg)
Sets a string describing the default parameter set to configure geometry.
Description of geometry of one entire detector.
Definition: GeometryCore.h:119
virtual void SetupGeometry()
Sets up the geometry (creates and registers it)
A test environment with some support for service providers.
Class holding a configuration for a test environment.
std::unique_ptr< geo::GeometryCore > SetupGeometry(fhicl::ParameterSet const &pset, Args &&...args)
Initializes a LArSoft geometry object.
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning
virtual std::unique_ptr< geo::GeometryCore > CreateNewGeometry() const
Creates a new geometry.
BasicGeometryEnvironmentConfiguration(std::string name)
Constructor; accepts the name as parameter.
GeometryTesterEnvironment(ConfigurationClass const &cfg_obj, bool bSetup=true)
Setup from a configuration.
virtual void Setup()
The complete initialization, ran at construction by default.
Interface to algorithm class for a specific detector channel mapping.
Base class for unit tests using FHiCL configuration.
GeometryTesterEnvironment(ConfigurationClass &&cfg_obj, bool bSetup=true)
Setup from a configuration.
void SetServiceParameterSetPath(std::string service_name, std::string path)
Sets the FHiCL path for the configuration of a test algorithm.