artdaq_core  v3_09_07
FragmentNameHelper.hh
1 #ifndef _artdaq_core_Plugins_FragmentNameHelper_hh_
2 #define _artdaq_core_Plugins_FragmentNameHelper_hh_
3 
4 #include <set>
5 #include <string>
6 #include <vector>
7 
8 #include <cetlib/BasicPluginFactory.h>
9 #include <cetlib/compiler_macros.h>
10 
11 #include "artdaq-core/Data/ContainerFragment.hh"
12 #include "artdaq-core/Data/Fragment.hh"
13 
14 #ifndef EXTERN_C_FUNC_DECLARE_START
15 #define EXTERN_C_FUNC_DECLARE_START \
16  extern "C" {
17 #endif
18 
23 #define DEFINE_ARTDAQ_FRAGMENT_NAME_HELPER(klass) \
24  EXTERN_C_FUNC_DECLARE_START \
25  std::shared_ptr<artdaq::FragmentNameHelper> make(std::string unidentified, std::vector<std::pair<artdaq::Fragment::type_t, std::string>> types) \
26  { \
27  return std::shared_ptr<artdaq::FragmentNameHelper>(new klass(unidentified, types)); \
28  } \
29  }
30 
31 namespace artdaq {
36 {
37 public:
41  virtual ~FragmentNameHelper() = default;
42 
48  FragmentNameHelper(std::string unidentified_instance_name, std::vector<std::pair<artdaq::Fragment::type_t, std::string>> extraTypes)
49  : type_map_()
50  , unidentified_instance_name_(unidentified_instance_name)
51  {
53  for (auto it = extraTypes.begin(); it != extraTypes.end(); ++it)
54  {
55  AddExtraType(it->first, it->second);
56  }
57  }
58 
62  void SetBasicTypes(std::map<artdaq::Fragment::type_t, std::string> const& type_map)
63  {
64  for (auto& type_pair : type_map)
65  {
66  type_map_[type_pair.first] = type_pair.second;
67  }
68  }
69 
73  void AddExtraType(artdaq::Fragment::type_t type_id, std::string const& type_name)
74  {
75  type_map_[type_id] = type_name;
76  }
77 
83 
87  virtual std::string GetInstanceNameForType(artdaq::Fragment::type_t type_id) const
88  {
89  if (type_map_.count(type_id) > 0) { return type_map_.at(type_id); }
91  }
92 
99  virtual std::set<std::string> GetAllProductInstanceNames() const
100  {
101  std::set<std::string> output;
102  for (const auto& map_iter : type_map_)
103  {
104  std::string instance_name = map_iter.second;
105  if (output.count(instance_name) == 0u)
106  {
107  output.insert(instance_name);
108  TLOG(TLVL_DEBUG + 33) << "Adding product instance name \"" << map_iter.second << "\" to list of expected names";
109  }
110  }
111 
112  auto container_type = type_map_.find(artdaq::Fragment::type_t(artdaq::Fragment::ContainerFragmentType));
113  if (container_type != type_map_.end())
114  {
115  std::string container_type_name = container_type->second;
116  std::set<std::string> tmp_copy = output;
117  for (const auto& set_iter : tmp_copy)
118  {
119  output.insert(container_type_name + set_iter);
120  }
121  }
122 
123  return output;
124  }
125 
133  virtual std::pair<bool, std::string>
135  {
136  auto type_map_end = type_map_.end();
137  bool success_code = true;
138  std::string instance_name;
139 
140  auto primary_type = type_map_.find(fragment.type());
141  if (primary_type != type_map_end)
142  {
143  TLOG(TLVL_DEBUG + 33) << "Found matching instance name " << primary_type->second << " for Fragment type " << static_cast<int>(fragment.type());
144  instance_name = primary_type->second;
146  {
147  artdaq::ContainerFragment cf(fragment);
148  auto contained_type = type_map_.find(cf.fragment_type());
149  if (contained_type != type_map_end)
150  {
151  instance_name += contained_type->second;
152  }
153  }
154  }
155  else
156  {
157  TLOG(TLVL_DEBUG + 33) << "Could not find match for Fragment type " << static_cast<int>(fragment.type()) << ", returning " << unidentified_instance_name_;
158  instance_name = unidentified_instance_name_;
159  success_code = false;
160  }
161 
162  return std::make_pair(success_code, instance_name);
163  }
164 
165 protected:
166  std::map<artdaq::Fragment::type_t, std::string> type_map_;
168 private:
169  FragmentNameHelper(FragmentNameHelper const&) = default;
171  FragmentNameHelper& operator=(FragmentNameHelper const&) = default;
172  FragmentNameHelper& operator=(FragmentNameHelper&&) = default;
173 };
174 
182 inline std::shared_ptr<FragmentNameHelper>
183 makeNameHelper(std::string const& plugin_name, std::string const& unidentified_instance_name, std::vector<std::pair<artdaq::Fragment::type_t, std::string>> extraTypes)
184 {
185  static cet::BasicPluginFactory bpf("fragmentNameHelper", "make");
186 
187  return bpf.makePlugin<std::shared_ptr<FragmentNameHelper>>(plugin_name, unidentified_instance_name, extraTypes);
188 }
189 } // namespace artdaq
190 
191 #endif //_artdaq_core_Plugins_FragmentNameHelper_hh_
void AddExtraType(artdaq::Fragment::type_t type_id, std::string const &type_name)
Adds an additional type to be translated.
The artdaq::ContainerFragment class represents a Fragment which contains other Fragments.
std::string unidentified_instance_name_
The name to use for unknown Fragment types.
virtual ~FragmentNameHelper()=default
Default virtual destructor.
detail::RawFragmentHeader::type_t type_t
typedef for type_t from RawFragmentHeader
Definition: Fragment.hh:137
std::map< artdaq::Fragment::type_t, std::string > type_map_
Map relating Fragment Type to strings.
Fragment::type_t fragment_type() const
Get the Fragment::type_t of stored Fragment objects.
virtual std::string GetInstanceNameForType(artdaq::Fragment::type_t type_id) const
Returns the basic translation for the specified type. Must be implemented by derived classes...
The FragmentNameHelper translates between Fragments and their instance names (usually by type...
static constexpr type_t ContainerFragmentType
Copy ContainerFragmentType from RawFragmentHeader.
Definition: Fragment.hh:156
FragmentNameHelper(std::string unidentified_instance_name, std::vector< std::pair< artdaq::Fragment::type_t, std::string >> extraTypes)
FragmentNameHelper Constructor.
static std::map< type_t, std::string > MakeSystemTypeMap()
Returns a map of the most commonly-used system types.
Definition: Fragment.hh:177
type_t type() const
Type of the Fragment, from the Fragment header.
Definition: Fragment.hh:853
std::string GetUnidentifiedInstanceName() const
Get the configured unidentified_instance_name.
std::shared_ptr< FragmentNameHelper > makeNameHelper(std::string const &plugin_name, std::string const &unidentified_instance_name, std::vector< std::pair< artdaq::Fragment::type_t, std::string >> extraTypes)
Create a FragmentNameHelper.
A Fragment contains the data from one piece of the DAQ system for one event The artdaq::Fragment is t...
Definition: Fragment.hh:85
void SetBasicTypes(std::map< artdaq::Fragment::type_t, std::string > const &type_map)
Sets the basic types to be translated. (Should not include &quot;container&quot; types.)
virtual std::pair< bool, std::string > GetInstanceNameForFragment(artdaq::Fragment const &fragment) const
Returns the product instance name for the specified fragment, based on the types that have been speci...
virtual std::set< std::string > GetAllProductInstanceNames() const
Returns the full set of product instance names which may be present in the data, based on the types t...