11 #include "art/Framework/Core/EDProducer.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "art/Framework/Services/Registry/ServiceHandle.h"
15 #include "art/Persistency/Common/PtrMaker.h"
16 #include "art/Utilities/ToolMacros.h"
17 #include "cetlib/cpu_timer.h"
18 #include "fhiclcpp/ParameterSet.h"
19 #include "messagefacility/MessageLogger/MessageLogger.h"
28 #include "sbndaq-artdaq-core/Overlays/ICARUS/ICARUSTriggerUDPFragment.hh"
40 #include <string_view>
44 using namespace std::string_literals;
134 virtual void produces(art::ProducesCollector&)
override;
135 virtual void configure(
const fhicl::ParameterSet&)
override;
136 virtual void initializeDataProducts()
override;
137 virtual void process_fragment(
const artdaq::Fragment &fragment)
override;
138 virtual void outputDataProducts(art::Event &event)
override;
163 icarus::ICARUSTriggerUDPFragment makeTriggerFragment
164 (artdaq::Fragment
const& fragment)
const;
167 icarus::ICARUSTriggerInfo parseTriggerString(std::string_view data)
const;
175 static constexpr
double UnknownBeamTime = std::numeric_limits<double>::max();
179 static constexpr
int BNB { 1 };
180 static constexpr
int NuMI { 2 };
181 static constexpr
int OffbeamBNB { 3 };
182 static constexpr
int OffbeamNuMI { 4 };
188 static std::string_view firstLine
189 (std::string
const&
s, std::string
const& endl =
"\0\n\r"s);
193 {
return s * 1000000000ULL + ns; }
196 {
return static_cast<long long int>(
a) - static_cast<long long int>(b); }
201 std::string
const TriggerDecoder::CurrentTriggerInstanceName {};
202 std::string
const TriggerDecoder::PreviousTriggerInstanceName {
"previous" };
205 TriggerDecoder::TriggerDecoder(fhicl::ParameterSet
const &pset)
207 { art::ServiceHandle<detinfo::DetectorClocksService>()->DataForJob() }
209 this->configure(pset);
226 fDebug = pset.get<
bool>(
"Debug",
false);
234 fTrigger = std::make_unique<TriggerCollection>();
236 fRelTrigger = std::make_unique<RelativeTriggerCollection>();
244 (artdaq::Fragment
const& fragment)
const
247 return icarus::ICARUSTriggerUDPFragment { fragment };
249 catch(std::exception
const&
e) {
250 mf::LogSystem(
"TriggerDecoder")
251 <<
"Error while creating trigger fragment from:\n"
253 <<
"\nError message: " << e.what();
257 mf::LogSystem(
"TriggerDecoder")
258 <<
"Unidentified exception while creating trigger fragment from:"
266 (std::string_view data)
const
269 return icarus::parse_ICARUSTriggerString(data.data());
271 catch(std::exception
const&
e) {
272 mf::LogSystem(
"TriggerDecoder")
273 <<
"Error while running standard parser on " << data.length()
274 <<
"-char long trigger string:\n==>|" << data
275 <<
"|<==\nError message: " << e.what();
279 mf::LogSystem(
"TriggerDecoder")
280 <<
"Unidentified exception while running standard parser on "
281 << data.length() <<
"-char long trigger string:\n==>|" << data <<
"|.";
293 uint64_t
const artdaq_ts = fragment.timestamp();
295 std::string data = frag.GetDataString();
296 char *buffer =
const_cast<char*
>(data.c_str());
299 uint64_t
const raw_wr_ts
300 =
makeTimestamp(frag.getWRSeconds(), frag.getWRNanoSeconds());
303 int64_t
const WRtimeToTriggerTime
304 =
static_cast<int64_t
>(artdaq_ts) - raw_wr_ts;
305 auto const correctWRtime = [WRtimeToTriggerTime](uint64_t time)
306 {
return time + WRtimeToTriggerTime; };
307 assert(correctWRtime(raw_wr_ts) == artdaq_ts);
310 int gate_type = datastream_info.gate_type;
311 long delta_gates_bnb [[maybe_unused]] = frag.getDeltaGatesBNB();
312 long delta_gates_numi [[maybe_unused]] = frag.getDeltaGatesOther();
313 long delta_gates_other [[maybe_unused]] = frag.getDeltaGatesNuMI();
314 uint64_t lastTrigger = 0;
320 unsigned int beamgate_count { std::numeric_limits<unsigned int>::max() };
321 std::uint64_t beamgate_ts { artdaq_ts };
328 if (
auto pBeamGateInfo = parsedData.findItem(
"Beam_TS");
329 pBeamGateInfo && (pBeamGateInfo->nValues() == 3)
332 beamgate_count = pBeamGateInfo->getNumber<
unsigned int>(0U);
335 pBeamGateInfo->getNumber<
unsigned int>(1U),
336 pBeamGateInfo->getNumber<
unsigned int>(2U)
341 beamgate_ts += raw_bg_ts - raw_wr_ts;
348 std::cout <<
"Full Timestamp = " << artdaq_ts
349 <<
"\nBeam gate " << beamgate_count <<
" at "
350 << (beamgate_ts/1
'000'000
'000) << "." << std::setfill('0
')
351 << std::setw(9) << (beamgate_ts%1'000
'000'000) << std::setfill(
' ')
353 <<
" ns relative to trigger)" << std::endl;
356 std::string_view
const dataLine =
firstLine(data);
359 std::cout <<
"Parsed data (from " << dataLine.size() <<
" characters): "
360 << parsedData << std::endl;
363 mf::LogError(
"TriggerDecoder")
364 <<
"Error parsing " << dataLine.length()
365 <<
"-char long trigger string:\n==>|" << dataLine
366 <<
"|<==\nError message: " << e.what() << std::endl;
371 std::cout <<
"Trigger packet content:\n" << dataLine
372 <<
"\nFull trigger fragment dump:"
386 default: beamGateBit = sbn::triggerSource::Unknown;
423 lastTrigger = frag.getLastTimestampBNB();
456 mf::LogWarning(
"TriggerDecoder") <<
"Unsupported gate type #" << gate_type;
463 static_cast<unsigned int>(datastream_info.wr_event_no),
465 elecGateStart.value(),
485 (std::string
const&
s, std::string
const& endl )
487 return { s.data(), std::min(s.find_first_of(endl), s.size()) };
Definitions of the trigger bits for SBN.
virtual void produces(art::ProducesCollector &) override
The space point building should output the hit collection for those hits which combine to form space ...
Parser to fill a KeyValuesData structure out of a character buffer.
detinfo::DetectorTimings const fDetTimings
Detector clocks and timings.
bool fDiagnosticOutput
Produces large number of diagnostic messages, use with caution!
IDecoder interface class definiton.
virtual void initializeDataProducts() override
Initialize any data products the tool will output.
This provides an art tool interface definition for tools which "decode" artdaq fragments into LArSoft...
std::unique_ptr< TriggerCollection > TriggerPtr
static constexpr int OffbeamNuMI
static constexpr nanoseconds NuMIgateDuration
std::unique_ptr< sbn::ExtraTriggerInfo > ExtraInfoPtr
virtual void process_fragment(const artdaq::Fragment &fragment) override
Given a set of recob hits, run DBscan to form 3D clusters.
constexpr value_t value() const
Returns the value of the quantity.
static std::string const CurrentTriggerInstanceName
Name of the data product instance for the current trigger.
BeamGateInfoPtr fBeamGateInfo
std::vector< sim::BeamGateInfo > BeamGateInfoCollection
Simple parser for comma-separated text.
std::unique_ptr< BeamGateInfoCollection > BeamGateInfoPtr
static std::string const PreviousTriggerInstanceName
Name of the data product instance for the previous trigger.
Interface to detinfo::DetectorClocks.
ExtraInfoPtr fTriggerExtra
virtual void configure(const fhicl::ParameterSet &) override
Interface for configuring the particular algorithm tool.
icarus::ICARUSTriggerUDPFragment makeTriggerFragment(artdaq::Fragment const &fragment) const
Creates a ICARUSTriggerInfo from a generic fragment.
constexpr mask_t< EnumType > mask(EnumType bit, OtherBits...otherBits)
Returns a mask with all specified bits set.
bool fDebug
Use this for debugging this tool.
static constexpr int NuMI
A value measured in the specified unit.
simulation_time toSimulationTime(FromTime time) const
Converts a time point into simulation time scale.
virtual void outputDataProducts(art::Event &event) override
Output the data products to the event store.
Utility to dump a artDAQ fragment on screen.
electronics_time TriggerTime() const
std::unique_ptr< RelativeTriggerCollection > fRelTrigger
static long long int timestampDiff(std::uint64_t a, std::uint64_t b)
Returns the difference a - b.
static std::uint64_t makeTimestamp(unsigned int s, unsigned int ns)
Combines second and nanosecond counts into a 64-bit timestamp.
details::DumpFragWrap dumpFragment(artdaq::Fragment const &frag)
Dump a artDAQ fragment into an output stream.
then echo File list $list not found else cat $list while read file do echo $file sed s
Dimensioned variables representing space or time quantities.
A class exposing an upgraded interface of detinfo::DetectorClocksData.
std::vector< raw::Trigger > RelativeTriggerCollection
nanosecond_as<> nanosecond
Type of time stored in nanoseconds, in double precision.
std::vector< raw::ExternalTrigger > TriggerCollection
Tool decoding the trigger information from DAQ.
Codes of gate types from the trigger hardware.
triggerSource
Type of beam or beam gate or other trigger source.
static constexpr int OffbeamBNB
static constexpr nanoseconds BNBgateDuration
static std::string_view firstLine(std::string const &s, std::string const &endl="\0\n\r"s)
BEGIN_PROLOG could also be cout
Functions to dump the content of binary data chunks to console.
icarus::ICARUSTriggerInfo parseTriggerString(std::string_view data) const
Parses the trigger data packet with the "standard" parser.