DetectorPropertiesStandardTestHelpers.h
Go to the documentation of this file.
1 /**
2  * @file DetectorPropertiesStandardTestHelpers.h
3  * @brief Helpers for support of DetectorPropertiesService in GArSoft 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 // GArSoft libraries
21 #include "DetectorInfo/DetectorPropertiesStandard.h"
22 #include "larcore/TestUtils/ProviderTestHelpers.h"
23 
24 // framework and utility libraries
25 #include "fhiclcpp/ParameterSet.h"
27 
28 // C/C++ standard libraries
29 #include <string>
30 #include <memory> // std::unique_ptr<>
31 
32 
33 namespace testing {
34 
35  /**
36  * @brief Set up a detinfo::DetectorPropertiesStandard from a parameter set
37  * @param pset parameter set for the configuration
38  * @param providers pack of providers needed by DetectorPropertiesService
39  * @return pointer to a newly created and set up DetectorPropertiesService
40  *
41  * This class specialisation enables the support of `SetupProvider()`
42  * methods of `testing::TesterEnvironment`.
43  * The resulting setup call will be something like:
44  *
45  * env.SetupProviderFor
46  * <detinfo::DetectorPropertiesStandard, detinfo::DetectorProperties>
47  * (pset, env.ProviderPackFor<detinfo::DetectorPropertiesStandard>());
48  *
49  * (note the use of `SetupProviderFor()` to register the service also as an
50  * implementation of DetectorProperties interface).
51  * This implicitly assumes that whatever service providers
52  * detinfo::DetectorPropertiesStandard needs, they have already been set up
53  * in the environment.
54  */
55  template <>
56  struct ProviderSetupClass<detinfo::DetectorPropertiesStandard> {
57 
58  static std::unique_ptr<detinfo::DetectorPropertiesStandard> setup
59  (
60  fhicl::ParameterSet const& pset,
62  )
63  {
64  // some feedback about whether we are using the right configuration
65  std::string ServiceProviderPath;
66  if (pset.get_if_present("service_provider", ServiceProviderPath)) {
67  std::string ServiceProviderName = ServiceProviderPath;
68  size_t iSlash = ServiceProviderPath.rfind('/');
69  if (iSlash != std::string::npos)
70  ServiceProviderName.erase(0, iSlash + 1);
71 
72  if (ServiceProviderName == "DetectorPropertiesServiceStandardGAr") {
73  LOG_TRACE("ProviderSetup") << "Verified service implementation for "
74  "DetectorPropertiesService"
75  ": '" << ServiceProviderPath << "'";
76  }
77  else {
78  // this means you should not be using the simple set up...
79  mf::LogWarning("setupProvider")
80  << "This set up is for a DetectorPropertiesStandard provider.\n"
81  "Your configuration specifies a '" << ServiceProviderPath
82  << "' service implementation"
83  " that is not known to use that provider.";
84  }
85  }
86 
87  /// @todo remove this debug stuff!
88  //
89  // create the new DetectorPropertiesStandard service provider
90  //
91  // we choose to ignore a configuration parameter that is known to be used
92  // by the art services but unknown to the provider; in this way we can use
93  // for this test the same configuration as for art-based tests.
94  //
95  std::set<std::string> ignore_keys({ "InheritNumberTimeSamples" });
96  {
97  mf::LogInfo debug("setupProvider<DetectorPropertiesStandard>");
98 
99  debug << "Asked to ignore " << ignore_keys.size() << " keys:";
100  for (auto const& key: ignore_keys) debug << " '" << key << "'";
101  }
102 
103  return std::make_unique<detinfo::DetectorPropertiesStandard>
104  (pset, providers, ignore_keys);
105  } // setup()
106 
107  }; // ProviderSetupClass<DetectorPropertiesStandard>
108 
109 
110  /**
111  * @brief Environment setup helper for DetectorPropertiesStandard
112  * @tparam TestEnv type of environment to set up
113  * @see simpleEnvironmentSetup()
114  *
115  * A service provider is set up in the environment, associated with the types
116  * detinfo::DetectorPropertiesStandard and detinfo::DetectorProperties.
117  * Its configuration is read from "services.DetectorPropertiesService".
118  *
119  * The environment is expected to expose an interface equivalent to the one
120  * of `testing::TesterEnvironment`.
121  *
122  * This class specialisation enables the support of `SimpleProviderSetup()`
123  * methods of `testing::TesterEnvironment`.
124  * It should be possible to set up a testing environment by calling:
125  *
126  * env.SimpleProviderSetup<detinfo::DetectorPropertiesStandard>();
127  *
128  * The provider will be available from any of these two calls:
129  *
130  * env.Provider<detinfo::DetectorPropertiesStandard>();
131  * env.Provider<detinfo::DetectorProperties>();
132  *
133  * @note This function assumes there is a `DetectorPropertiesService` service
134  * configuration available in the environment, and that the service providers
135  * geo::GeometryCore, detinfo::GArProperties and detinfo::DetectorClocks are
136  * configured and available.
137  */
138  template <typename TestEnv>
140  <detinfo::DetectorPropertiesStandard, TestEnv>
141  {
143  {
144  return env.template SetupProviderFor
146  (
147  env.ServiceParameters("DetectorPropertiesService"),
148  env.template ProviderPackFor<detinfo::DetectorPropertiesStandard>()
149  );
150  } // setup()
151 
152  }; // SimpleEnvironmentSetupClass<detinfo::DetectorPropertiesStandard>
153 
154 
155 } // namespace testing
156 
157 
158 #endif // LARDATA_DETECTORINFO_DETECTORPROPERTIESSTANDARDTESTHELPERS_H
LArSoft test utilities.
std::string string
Definition: nybbler.cc:12
static std::unique_ptr< Prov > setup(Args &&...args)
Instantiates a new provider with specified arguments for constructor.
Environment helper to set up a service provider.
def key(type, name=None)
Definition: graph.py:13
Class to create and set up a new provider.
General LArSoft Utilities.
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