All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
DumpArtDAQfragments_module.cc
Go to the documentation of this file.
1 /**
2  * @file DumpArtDAQfragments_module.cc
3  * @brief Dumps on console the content of `artdaq::Fragment` data products.
4  * @author Gianluca Petrillo (petrillo@slac.stanford.edu)
5  * @date June 10, 2021
6  */
7 
8 // SBN libraries
10 
11 // LArSoft libraries
14 
15 // framework libraries
16 #include "art/Framework/Principal/Event.h"
17 #include "art/Framework/Principal/Handle.h"
18 #include "art/Framework/Core/ModuleMacros.h"
19 #include "art/Framework/Core/EDAnalyzer.h"
20 #include "canvas/Persistency/Provenance/ProductToken.h"
21 #include "canvas/Persistency/Provenance/EventID.h"
22 #include "canvas/Utilities/InputTag.h"
23 #include "messagefacility/MessageLogger/MessageLogger.h"
24 #include "fhiclcpp/types/Sequence.h"
25 #include "fhiclcpp/types/Atom.h"
26 
27 // C/C++ standard libraries
28 #include <algorithm> // std::transform()
29 #include <vector>
30 #include <string>
31 
32 
33 //------------------------------------------------------------------------------
34 namespace sbn { class DumpArtDAQfragments; }
35 
36 /**
37  * @brief Dumps on console the content of `artdaq::Fragment` collections.
38  *
39  *
40  *
41  * Input data products
42  * ====================
43  *
44  * * `std::vector<artdaq::Fragment>`: data to be dumped; the dump is binary,
45  * and it does not attempt an interpretation of the content of the fragment
46  * (in future, it could at least attempt unpacking the container fragments).
47  *
48  *
49  *
50  * Configuration parameters
51  * =========================
52  *
53  * A terse description of the parameters is printed by running
54  * `lar --print-description DumpArtDAQfragments`.
55  *
56  * * `FragmentTags` (list of data product input tags): the tags identifying the
57  * data products to dump.
58  * * `OutputCategory` (string, default: `DumpArtDAQfragments`): name of the
59  * message facility output stream to dump the information into
60  *
61  */
62 class sbn::DumpArtDAQfragments: public art::EDAnalyzer {
63 
64  public:
65 
66  // --- BEGIN Configuration ---------------------------------------------------
67  struct Config {
68 
69  using Name = fhicl::Name;
70  using Comment = fhicl::Comment;
71 
72  fhicl::Sequence<art::InputTag> FragmentTags {
73  Name("FragmentTags"),
74  Comment("tag of fragment collection data products to be dumped")
75  };
76 
77  fhicl::Atom<std::string> OutputCategory {
78  Name("OutputCategory"),
79  Comment("name of the category used for the output"),
80  "DumpArtDAQfragments"
81  };
82 
83  }; // struct Config
84 
85  using Parameters = art::EDAnalyzer::Table<Config>;
86  // --- END Configuration -----------------------------------------------------
87 
88 
89  // --- BEGIN Constructors ----------------------------------------------------
90  explicit DumpArtDAQfragments(Parameters const& config);
91 
92  // --- END Constructors ------------------------------------------------------
93 
94 
95  // --- BEGIN Framework hooks -------------------------------------------------
96 
97  /// Does nothing, but it is mandatory.
98  virtual void analyze(art::Event const& event) override;
99 
100  // --- END Framework hooks ---------------------------------------------------
101 
102 
103  private:
104 
105  // --- BEGIN Configuration variables -----------------------------------------
106 
107  /// Input data tokens.
108  std::vector<art::InputTag> const fInputTags;
109 
110  /// Input data tokens.
111  std::vector<art::ProductToken<artdaq::Fragments>> const fInputTokens;
112 
113  /// Category used for message facility stream.
114  std::string const fOutputCategory;
115 
116  // --- END Configuration variables -------------------------------------------
117 
118 
119  /// Get art tokens for specified input data products.
120  std::vector<art::ProductToken<artdaq::Fragments>> getFragmentTokens
121  (std::vector<art::InputTag> const& tags);
122 
123  /// Dumps a single fragment collection.
124  void dumpFragments(
125  art::Event const& event,
126  art::InputTag const& inputTag,
127  art::ProductToken<artdaq::Fragments> const& inputToken
128  ) const;
129 
130 }; // sbn::DumpArtDAQfragments
131 
132 
133 //------------------------------------------------------------------------------
134 //--- Implementation
135 //------------------------------------------------------------------------------
136 //--- sbn::DumpArtDAQfragments
137 //------------------------------------------------------------------------------
139  (Parameters const& config)
140  : art::EDAnalyzer(config)
141  // configuration
142  , fInputTags (config().FragmentTags())
143  , fInputTokens (getFragmentTokens(fInputTags))
144  , fOutputCategory(config().OutputCategory())
145 {
146  assert(fInputTags.size() == fInputTokens.size());
147 } // sbn::DumpArtDAQfragments::DumpArtDAQfragments()
148 
149 
150 //------------------------------------------------------------------------------
151 void sbn::DumpArtDAQfragments::analyze(art::Event const& event) {
152 
153  mf::LogVerbatim(fOutputCategory) << event.id() << ":";
154 
155  for (auto const& [ tag, token ]: util::zip(fInputTags, fInputTokens))
156  dumpFragments(event, tag, token);
157 
158 } // sbn::DumpArtDAQfragments::analyze()
159 
160 
161 //------------------------------------------------------------------------------
163  art::Event const& event,
164  art::InputTag const& inputTag,
165  art::ProductToken<artdaq::Fragments> const& inputToken
166 ) const {
167 
168  art::Handle<artdaq::Fragments> fragmentHandle;
169  //bool found = event.getByToken(inputToken, fragmentHandle);
170 
171  if ( !(fragmentHandle = event.getHandle<artdaq::Fragments>(inputToken)) ) {
172  mf::LogVerbatim(fOutputCategory)
173  << "No fragment collection found as '" << inputTag.encode() << "'.";
174  return;
175  }
176 
177  artdaq::Fragments const& fragments = *fragmentHandle;
178  mf::LogVerbatim log { fOutputCategory };
179  log << "Data product '" << inputTag.encode() << "' has " << fragments.size()
180  << " fragments.";
181  for (auto const& [ iFragment, fragment ]: util::enumerate(fragments)) {
182  // TODO special case for artdaq::ContainerFragment?
183  log << "\n[#" << iFragment << "] " << sbndaq::dumpFragment(fragment);
184  } // for
185 
186 } // sbn::DumpArtDAQfragments::dumpFragments()
187 
188 
189 //------------------------------------------------------------------------------
190 std::vector<art::ProductToken<artdaq::Fragments>>
192  (std::vector<art::InputTag> const& tags)
193 {
194  auto getToken = [this](art::InputTag const& tag)
195  { return consumes<artdaq::Fragments>(tag); };
196 
197  std::vector<art::ProductToken<artdaq::Fragments>> tokens;
198  std::transform(begin(tags), end(tags), back_inserter(tokens), getToken);
199  return tokens;
200 } // sbn::DumpArtDAQfragments::getFragmentTokens()
201 
202 
203 //------------------------------------------------------------------------------
204 DEFINE_ART_MODULE(sbn::DumpArtDAQfragments)
205 
206 
207 //------------------------------------------------------------------------------
std::vector< art::ProductToken< artdaq::Fragments > > const fInputTokens
Input data tokens.
Definition of util::zip().
static constexpr Sample_t transform(Sample_t sample)
Definition of util::enumerate().
std::string const fOutputCategory
Category used for message facility stream.
auto enumerate(Iterables &&...iterables)
Range-for loop helper tracking the number of iteration.
Definition: enumerate.h:69
fhicl::Sequence< art::InputTag > FragmentTags
void dumpFragments(art::Event const &event, art::InputTag const &inputTag, art::ProductToken< artdaq::Fragments > const &inputToken) const
Dumps a single fragment collection.
std::vector< art::InputTag > const fInputTags
Input data tokens.
Dumps on console the content of artdaq::Fragment collections.
auto end(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:585
BEGIN_PROLOG vertical distance to the surface Name
std::vector< art::ProductToken< artdaq::Fragments > > getFragmentTokens(std::vector< art::InputTag > const &tags)
Get art tokens for specified input data products.
Utility to dump a artDAQ fragment on screen.
virtual void analyze(art::Event const &event) override
Does nothing, but it is mandatory.
auto begin(FixedBins< T, C > const &) noexcept
Definition: FixedBins.h:573
art::EDAnalyzer::Table< Config > Parameters
details::DumpFragWrap dumpFragment(artdaq::Fragment const &frag)
Dump a artDAQ fragment into an output stream.
auto zip(Iterables &&...iterables)
Range-for loop helper iterating across many collections at the same time.
Definition: zip.h:295
DumpArtDAQfragments(Parameters const &config)