Sampled.h
Go to the documentation of this file.
1 #ifndef canvas_Persistency_Common_Sampled_h
2 #define canvas_Persistency_Common_Sampled_h
3 
4 // ==============================================================================
5 // Sampled template
6 //
7 // The Sampled template associates dataset names to values of type T
8 // for that dataset. It is intended to be used to gather different
9 // instances of a (sub)run data product that have been written to
10 // separate files.
11 //
12 // N.B. This is a first version of a wrapper that collects all objects
13 // for a given product signature from a (sub)run. It is likely
14 // that optimizations can be made to it once it has seen some use
15 // in the wild.
16 //
17 // A Sampled<T> object is created by providing an input tag
18 // corresponding to the original product signature of the gathered
19 // products. This input tag can be accessed by calling the
20 // Sampled<T>::originalInputTag() member function. The input, along
21 // with the type T can be used to disambiguate between sampled
22 // products for a given run or subrun. Since this object is intended
23 // to be retrieved within the context of a run or subrun, we do not
24 // additionally specify the BranchType.
25 //
26 // To access the stored values for products of the type 'int', module
27 // label 'm1', an empty instance name, and process name 'MakeInts',
28 // follow the pattern:
29 //
30 // InputTag const tag_to_retrieve{"m1", "", sampled_from("MakeInts")};
31 // auto const& sampledInts = r.getValidHandle<Sampled<int>>(tag_to_retrieve);
32 //
33 // // Access specific number for a specified dataset and run
34 // cet::exempt_ptr<int const> p = sampledInts.get(some_dataset, some_run_id);
35 // if (p) {
36 // std::cout << *p << '\n';
37 // }
38 //
39 // If a value does not exist for the provided dataset name and
40 // (sub)run ID, the 'get' function will return a null exempt pointer.
41 // This means that the validity of the pointer should be checked
42 // before dereferencing it. The dataset names and (sub)run IDs used
43 // by the SamplingInput source can be retrieved from the
44 // Sampled(Sub)RunInfo product that the SamplingInput source creates.
45 //
46 // N.B. To access sampled products with the process name 'MakeInts',
47 // the provided process name while retrieving the corresponding
48 // sampled product is 'SampledFromMakeInts', where the
49 // 'SampledFrom' *must be prepended* to the original process
50 // name. The art::sampled_from("MakeInts") function can be used
51 // for this purpose.
52 // ==============================================================================
53 
58 #include "cetlib/exempt_ptr.h"
59 #include "cetlib_except/demangle.h"
60 
61 #include <map>
62 #include <string>
63 #include <type_traits>
64 #include <utility>
65 #include <vector>
66 
67 namespace art {
68 
69  template <typename T>
70  class Sampled {
71  // TODO: An unordered map may end up being better.
72  using container_t = std::map<std::string, std::map<SubRunID, T>>;
73 
74  public:
76 
77  Sampled() = default;
78  explicit Sampled(InputTag const& tag) noexcept(false);
79 
80  bool empty() const;
81 
82  InputTag const& originalInputTag() const;
83 
84  cet::exempt_ptr<T const> get(std::string const& dataset,
85  RunID const& id) const;
86 
87  cet::exempt_ptr<T const> get(std::string const& dataset,
88  SubRunID const& id) const;
89 
90  // Expert interface below
91  void
92  insert(std::string const& dataset, SubRunID const& id, T&& value)
93  {
94  products_[dataset].emplace(id, std::forward<T>(value));
95  }
96 
97  // MUST UPDATE WHEN CLASS IS CHANGED!
98  static short
100  {
101  return 10;
102  }
103 
104  private:
107  };
108 
109  inline auto
110  sampled_from(std::string process_name)
111  {
112  return process_name.insert(0, "SampledFrom");
113  }
114 
115  // Implementation below
116  template <typename T>
117  Sampled<T>::Sampled(InputTag const& tag) noexcept(false) : tag_{tag}
118  {
119  // Due to Ptr reseeding issues, Assns or Ptr types (or
120  // containers thereof) are not supported. This checking cannot
121  // be done at compile time because creating a Wrapper requires a
122  // Sampled instantiation, even if that instantiation will never
123  // be used.
124  auto const type_name = cet::demangle_symbol(typeid(*this).name());
125  if (type_name.find("art::Assns") != std::string::npos ||
126  type_name.find("art::Ptr") != std::string::npos) {
128  << "An attempt was made to create the type "
129  << cet::demangle_symbol(typeid(T).name())
130  << ".\n"
131  "This is not allowed. Please contact artists@fnal.gov for "
132  "guidance.";
133  }
134  }
135 
136  template <typename T>
137  InputTag const&
139  {
140  return tag_;
141  }
142 
143  template <typename T>
144  bool
146  {
147  return products_.empty();
148  }
149 
150  template <typename T>
152  Sampled<T>::get(std::string const& dataset, RunID const& id) const
153  {
154  return get(dataset, SubRunID::invalidSubRun(id));
155  }
156 
157  template <typename T>
159  Sampled<T>::get(std::string const& dataset, SubRunID const& id) const
160  {
162  auto dataset_it = products_.find(dataset);
163  if (dataset_it == products_.cend()) {
164  return result;
165  }
166 
167  auto const& ids = dataset_it->second;
168  auto id_it = ids.find(id);
169  if (id_it == ids.cend()) {
170  return result;
171  }
172 
173  return cet::make_exempt_ptr(&id_it->second);
174  }
175 
176 }
177 
178 #endif /* canvas_Persistency_Common_Sampled_h */
179 
180 // Local Variables:
181 // mode: c++
182 // End:
static QCString name
Definition: declinfo.cpp:673
static short Class_Version()
Definition: Sampled.h:99
static QCString result
InputTag const & originalInputTag() const
Definition: Sampled.h:138
std::string string
Definition: nybbler.cc:12
auto sampled_from(std::string process_name)
Definition: Sampled.h:110
intermediate_table::const_iterator const_iterator
int find(char c, int index=0, bool cs=TRUE) const
Definition: qcstring.cpp:41
bool empty() const
Definition: Sampled.h:145
container_t products_
Definition: Sampled.h:106
std::map< std::string, std::map< SubRunID, T >> container_t
Definition: Sampled.h:72
constexpr exempt_ptr< E > make_exempt_ptr(E *) noexcept
cet::exempt_ptr< T const > get(std::string const &dataset, RunID const &id) const
Definition: Sampled.h:152
void insert(std::string const &dataset, SubRunID const &id, T &&value)
Definition: Sampled.h:92
typename container_t::const_iterator const_iterator
Definition: Sampled.h:75
Sampled()=default
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static SubRunID invalidSubRun(RunID const &rID)
Definition: SubRunID.h:165
InputTag tag_
Definition: Sampled.h:105