artdaq_demo  v2_10_03
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator
NthEvent_transfer.cc
1 #include "artdaq/TransferPlugins/TransferInterface.hh"
2 #include "artdaq/TransferPlugins/MakeTransferPlugin.hh"
3 #include "artdaq-core/Data/Fragment.hh"
4 #include "artdaq-core/Utilities/ExceptionHandler.hh"
5 #include "cetlib/BasicPluginFactory.h"
6 
7 #include "messagefacility/MessageLogger/MessageLogger.h"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 #include <boost/tokenizer.hpp>
11 
12 #include <sys/shm.h>
13 #include <memory>
14 #include <iostream>
15 #include <string>
16 #include <limits>
17 #include <sstream>
18 
22 namespace artdaq
23 {
28  class NthEventTransfer : public TransferInterface
29  {
30  public:
38  NthEventTransfer(fhicl::ParameterSet const& ps, artdaq::TransferInterface::Role role);
39 
46  TransferInterface::CopyStatus
47  copyFragment(artdaq::Fragment& fragment,
48  size_t send_timeout_usec = std::numeric_limits<size_t>::max()) override;
49 
56  TransferInterface::CopyStatus
57  moveFragment(artdaq::Fragment&& fragment,
58  size_t send_timeout_usec = std::numeric_limits<size_t>::max()) override;
59 
66  int receiveFragment(artdaq::Fragment& fragment,
67  size_t receiveTimeout) override
68  {
69  // nth-event discarding is done at the send side. Pass receive calls through to underlying transfer
70  return physical_transfer_->receiveFragment(fragment, receiveTimeout);
71  }
72 
77  int source_rank() const { return physical_transfer_->source_rank(); }
78 
83  int destination_rank() const { return physical_transfer_->destination_rank(); }
84 
85 
86  private:
87 
88  bool pass(const artdaq::Fragment& ) const;
89 
90  std::unique_ptr<TransferInterface> physical_transfer_;
91  size_t nth_;
92  size_t offset_;
93  };
94 
95  NthEventTransfer::NthEventTransfer(fhicl::ParameterSet const& pset, artdaq::TransferInterface::Role role) :
96  TransferInterface(pset, role)
97  , nth_(pset.get<size_t>("nth")),
98  offset_(pset.get<size_t>("offset",0))
99  {
100  if (pset.has_key("source_rank") || pset.has_key("destination_rank")) {
101  throw cet::exception("NthEvent") << "The parameters \"source_rank\" and \"destination_rank\" must be explicitly defined in the body of the physical_transfer_plugin table, and not outside of it";
102  }
103 
104 
105  if (offset_ >= nth_) {
106  throw cet::exception("NthEvent") << "Offset value of " << offset_ <<
107  " must not be larger than the modulus value of " << nth_;
108  }
109 
110  if(nth_ == 0)
111  {
112  mf::LogWarning("NthEventTransfer") << "0 was passed as the nth parameter to NthEventTransfer. Will change to 1 (0 is undefined behavior)";
113  nth_ = 1;
114  }
115  // Instantiate the TransferInterface plugin used to effect transfers
116  physical_transfer_ = MakeTransferPlugin(pset, "physical_transfer_plugin", role);
117  }
118 
119 
120  TransferInterface::CopyStatus
121  NthEventTransfer::copyFragment(artdaq::Fragment& fragment,
122  size_t send_timeout_usec)
123  {
124 
125  if (!pass(fragment))
126  {
127  // Do not transfer but return success. Fragment is discarded
128  return TransferInterface::CopyStatus::kSuccess;
129  }
130 
131  // This is the nth Fragment, transfer
132  return physical_transfer_->copyFragment(fragment, send_timeout_usec);
133  }
134 
135  TransferInterface::CopyStatus
136  NthEventTransfer::moveFragment(artdaq::Fragment&& fragment,
137  size_t send_timeout_usec)
138  {
139  if (!pass(fragment))
140  {
141  // Do not transfer but return success. Fragment is discarded
142  return TransferInterface::CopyStatus::kSuccess;
143  }
144 
145  // This is the nth Fragment, transfer
146  return physical_transfer_->moveFragment(std::move(fragment), send_timeout_usec);
147  }
148 
149  bool
150  NthEventTransfer::pass(const artdaq::Fragment& fragment) const
151  {
152  bool passed = false;
153 
154  if (fragment.type() == artdaq::Fragment::DataFragmentType) {
155  passed = (fragment.sequenceID() + nth_ - offset_) % nth_ == 0 ? true: false;
156  } else {
157  passed = true;
158  }
159 
160  return passed;
161  }
162 }
163 
164 DEFINE_ARTDAQ_TRANSFER(artdaq::NthEventTransfer)
165 
166 // Local Variables:
167 // mode: c++
168 // End:
Demonstration TransferInterface plugin showing how to discard events Intended for use in the transfer...
int receiveFragment(artdaq::Fragment &fragment, size_t receiveTimeout) override
Receive a fragment from the transfer plugin.
int destination_rank() const
Get the destination rank from the physical transfer.
NthEventTransfer(fhicl::ParameterSet const &ps, artdaq::TransferInterface::Role role)
NthEventTransfer Constructor.
TransferInterface::CopyStatus copyFragment(artdaq::Fragment &fragment, size_t send_timeout_usec=std::numeric_limits< size_t >::max()) override
Copy a fragment, using the non-reliable channel.
int source_rank() const
Get the source rank from the physical transfer.
TransferInterface::CopyStatus moveFragment(artdaq::Fragment &&fragment, size_t send_timeout_usec=std::numeric_limits< size_t >::max()) override
Copy a fragment, using the reliable channel. moveFragment assumes ownership of the fragment...