DetectorPropertiesStandardTestHelpers.h
Go to the documentation of this file.
1 /**
2  * @file DetectorPropertiesStandardTestHelpers.h
3  * @brief Helpers for support of DetectorPropertiesService in LArSoft tests
4  * @author Gianluca Petrillo (petrillo@fnal.gov)
5  * @date May 6th, 2016
6  *
7  * This library is a pure header.
8  * It requires linkage with:
9  *
10  * * `lardata_DetectorInfo`
11  * * `mf_MessageLogger`
12  * * `mf_Utilities`
13  * * `fhiclcpp`
14  *
15  */
16 
17 #ifndef LARDATA_DETECTORINFO_DETECTORPROPERTIESSTANDARDTESTHELPERS_H
18 #define LARDATA_DETECTORINFO_DETECTORPROPERTIESSTANDARDTESTHELPERS_H 1
19 
20 // LArSoft libraries
23 
24 // framework and utility libraries
25 #include "fhiclcpp/ParameterSet.h"
27 
28 // C/C++ standard libraries
29 #include <memory> // std::unique_ptr<>
30 #include <string>
31 
32 namespace testing {
33 
34  /**
35  * @brief Set up a detinfo::DetectorPropertiesStandard from a parameter set
36  * @param pset parameter set for the configuration
37  * @param providers pack of providers needed by DetectorPropertiesService
38  * @return pointer to a newly created and set up DetectorPropertiesService
39  *
40  * This class specialisation enables the support of `SetupProvider()`
41  * methods of `testing::TesterEnvironment`.
42  * The resulting setup call will be something like:
43  *
44  * env.SetupProviderFor
45  * <detinfo::DetectorPropertiesStandard, detinfo::DetectorProperties>
46  * (pset, env.ProviderPackFor<detinfo::DetectorPropertiesStandard>());
47  *
48  * (note the use of `SetupProviderFor()` to register the service also as an
49  * implementation of DetectorProperties interface).
50  * This implicitly assumes that whatever service providers
51  * detinfo::DetectorPropertiesStandard needs, they have already been set up
52  * in the environment.
53  */
54  template <>
55  struct ProviderSetupClass<detinfo::DetectorPropertiesStandard> {
56 
57  static std::unique_ptr<detinfo::DetectorPropertiesStandard>
60  {
61  // some feedback about whether we are using the right configuration
62  std::string ServiceProviderPath;
63  if (pset.get_if_present("service_provider", ServiceProviderPath)) {
64  std::string ServiceProviderName = ServiceProviderPath;
65  size_t iSlash = ServiceProviderPath.rfind('/');
66  if (iSlash != std::string::npos) ServiceProviderName.erase(0, iSlash + 1);
67 
68  if (ServiceProviderName == "DetectorPropertiesServiceStandard") {
69  MF_LOG_TRACE("ProviderSetup") << "Verified service implementation for "
70  "DetectorPropertiesService"
71  ": '"
72  << ServiceProviderPath << "'";
73  }
74  else {
75  // this means you should not be using the simple set up...
76  mf::LogWarning("setupProvider")
77  << "This set up is for a DetectorPropertiesStandard provider.\n"
78  "Your configuration specifies a '"
79  << ServiceProviderPath
80  << "' service implementation"
81  " that is not known to use that provider.";
82  }
83  }
84 
85  //
86  // create the new DetectorPropertiesStandard service provider
87  //
88  // we choose to ignore a configuration parameter that is known to be used
89  // by the art services but unknown to the provider; in this way we can use
90  // for this test the same configuration as for art-based tests.
91  //
92  std::set<std::string> ignore_keys({"InheritNumberTimeSamples"});
93  return std::make_unique<detinfo::DetectorPropertiesStandard>(
94  pset,
95  providers.get<geo::GeometryCore>(),
96  providers.get<detinfo::LArProperties>(),
97  ignore_keys);
98  } // setup()
99 
100  }; // ProviderSetupClass<DetectorPropertiesStandard>
101 
102  /**
103  * @brief Environment setup helper for DetectorPropertiesStandard
104  * @tparam TestEnv type of environment to set up
105  * @see simpleEnvironmentSetup()
106  *
107  * A service provider is set up in the environment, associated with the types
108  * detinfo::DetectorPropertiesStandard and detinfo::DetectorProperties.
109  * Its configuration is read from "services.DetectorPropertiesService".
110  *
111  * The environment is expected to expose an interface equivalent to the one
112  * of `testing::TesterEnvironment`.
113  *
114  * This class specialisation enables the support of `SimpleProviderSetup()`
115  * methods of `testing::TesterEnvironment`.
116  * It should be possible to set up a testing environment by calling:
117  *
118  * env.SimpleProviderSetup<detinfo::DetectorPropertiesStandard>();
119  *
120  * The provider will be available from any of these two calls:
121  *
122  * env.Provider<detinfo::DetectorPropertiesStandard>();
123  * env.Provider<detinfo::DetectorProperties>();
124  *
125  * @note This function assumes there is a `DetectorPropertiesService` service
126  * configuration available in the environment, and that the service providers
127  * geo::GeometryCore, detinfo::LArProperties and detinfo::DetectorClocks are
128  * configured and available.
129  */
130  template <typename TestEnv>
131  struct SimpleEnvironmentSetupClass<detinfo::DetectorPropertiesStandard, TestEnv> {
133  setup(TestEnv& env)
134  {
135  auto* detp = env.template SetupProviderFor<detinfo::DetectorProperties,
137  env.ServiceParameters("DetectorPropertiesService"),
138  env.template ProviderPackFor<detinfo::DetectorPropertiesStandard>());
139  return detp;
140  } // setup()
141 
142  }; // SimpleEnvironmentSetupClass<detinfo::DetectorPropertiesStandard>
143 
144 } // namespace testing
145 
146 #endif // LARDATA_DETECTORINFO_DETECTORPROPERTIESSTANDARDTESTHELPERS_H
LArSoft test utilities.
static std::unique_ptr< detinfo::DetectorPropertiesStandard > setup(fhicl::ParameterSet const &pset, detinfo::DetectorPropertiesStandard::providers_type const &providers)
std::string string
Definition: nybbler.cc:12
Provider const * get() const
Returns the provider with the specified type.
Definition: ProviderPack.h:193
Environment helper to set up a service provider.
#define MF_LOG_TRACE(id)
General LArSoft Utilities.
Description of geometry of one entire detector.
Container for a list of pointers to providers.
Definition: ProviderPack.h:114
std::optional< T > get_if_present(std::string const &key) const
Definition: ParameterSet.h:224
MaybeLogger_< ELseverityLevel::ELsev_warning, false > LogWarning