All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ReadArtConfiguration.h
Go to the documentation of this file.
1 /**
2  * @file icaruscode/Utilities/ReadArtConfiguration.h
3  * @brief Utilities to extract _art_ FHiCL configuration from different sources.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date February 18, 2021
6  * @see icaruscode/Utilities/ReadArtConfiguration.cxx
7  *
8  */
9 
10 #ifndef ICARUSCODE_UTILITIES_READARTCONFIGURATION_H
11 #define ICARUSCODE_UTILITIES_READARTCONFIGURATION_H
12 
13 
14 // framework libraries
15 #include "canvas/Persistency/Provenance/ProcessConfiguration.h"
16 #include "canvas/Persistency/Provenance/ProcessHistory.h"
17 #include "fhiclcpp/ParameterSet.h"
18 #include "fhiclcpp/ParameterSetID.h"
19 #include "fhiclcpp/ParameterSetRegistry.h" // also defines ParameterSetID hash
20 #include "cetlib_except/exception.h"
21 
22 // ROOT libraries
23 #include "TFile.h"
24 
25 // C/C++ standard libraries
26 #include <map>
27 
28 
29 // -----------------------------------------------------------------------------
30 namespace util {
31 
32  // ---------------------------------------------------------------------------
33  /**
34  * @brief Reads and returns the _art_ configuration stored in `sourceDir`.
35  * @param file ROOT file where the configuration is stored
36  * @return the full configuration
37  * @throw cet::exception (category: `"readConfigurationFromArtFile"`) on error
38  * @see `readConfigurationFromArtPrincipal()`
39  *
40  * The configuration is expected to be stored by _art_ in the way it does
41  * for _art_ ROOT files.
42  *
43  * The configuration is returned as a map of parameter set ID to parameter
44  * set.
45  */
46  std::map<fhicl::ParameterSetID, fhicl::ParameterSet>
48 
49 
50  // ---------------------------------------------------------------------------
51  /**
52  * @brief Reads and returns the complete _art_ configuration in the principal.
53  * @tparam Principal type of framework principal class (e.g. `art::Event`)
54  * @param principal the "principal" _art_ object to read the information from
55  * @return the full configuration, as map: process name -> FHiCL parameter set
56  * @throw cet::exception (category: `"readConfigurationFromArtPrincipal"`)
57  * on error
58  * @see `readConfigurationFromArtFile()`
59  *
60  * Compared to `readConfigurationFromArtFile()`, this function relays the same
61  * information after it has been conveniently extracted by the framework.
62  * A "principal" is framework jargon for `art::Event`, `art::SubRun` or
63  * `art::Run` (all derived from `art::DataViewImpl`).
64  * Therefore, this function can be called e.g. in `beginRun()` hook of a
65  * module using its `art::Run` argument, or in a `analyze()` hook using
66  * its `art::Event` argument.
67  *
68  * This function is supposed to be more solid than
69  * `readConfigurationFromArtFile()` because it relies less on internals of
70  * how the framework works. , this function relays the same
71  * information after it has been conveniently extracted by the framework.
72  * Also, this function should also be compatible with `gallery::Event` too
73  * (which is the reason why it is implemented as template instead of taking
74  * a `art::DataViewImpl`, which is _art_-specific), making the name of this
75  * function a misnomer.
76  *
77  * The configuration is returned as a map of process names to parameter sets
78  * (note that the key is different from the one returned by
79  * `readConfigurationFromArtFile()`).
80  */
81  template <typename Principal>
82  std::map<std::string, fhicl::ParameterSet>
83  readConfigurationFromArtPrincipal(Principal const& principal);
84 
85 
86  // ---------------------------------------------------------------------------
87 
88 } // namespace util
89 
90 
91 
92 // -----------------------------------------------------------------------------
93 // --- template implementation
94 // -----------------------------------------------------------------------------
95 template <typename Principal>
96 std::map<std::string, fhicl::ParameterSet>
97 util::readConfigurationFromArtPrincipal(Principal const& principal) {
98 
99  std::map<std::string, fhicl::ParameterSet> configMap;
100 
101  for (art::ProcessConfiguration const& procConfig: principal.processHistory())
102  {
103 
104  fhicl::ParameterSet config;
105  if (!fhicl::ParameterSetRegistry::get(procConfig.parameterSetID(), config))
106  {
107  // this would be, as far as I understand, a logic error
108  throw cet::exception("readConfigurationFromArtPrincipal")
109  << "Configuration of process '" << procConfig.processName()
110  << "' can't be found!\n";
111  }
112 
113  configMap[procConfig.processName()] = std::move(config);
114  } // for
115 
116  return configMap;
117 
118 } // util::readConfigurationFromArtPrincipal()
119 
120 
121 // -----------------------------------------------------------------------------
122 
123 
124 #endif // ICARUSCODE_UTILITIES_READARTCONFIGURATION_H
* file
Definition: file_to_url.sh:69
std::map< fhicl::ParameterSetID, fhicl::ParameterSet > readConfigurationFromArtFile(TFile &file)
Reads and returns the art configuration stored in sourceDir.
std::map< std::string, fhicl::ParameterSet > readConfigurationFromArtPrincipal(Principal const &principal)
Reads and returns the complete art configuration in the principal.