8 #define TRACE_NAME "ToyDump"
10 #include "art/Framework/Core/EDAnalyzer.h"
11 #include "art/Framework/Core/ModuleMacros.h"
12 #include "art/Framework/Principal/Event.h"
13 #include "art/Framework/Principal/Handle.h"
14 #include "canvas/Utilities/Exception.h"
16 #include "artdaq-core-demo/Overlays/FragmentType.hh"
17 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
18 #include "artdaq-core/Data/ContainerFragment.hh"
19 #include "artdaq-core/Data/Fragment.hh"
52 explicit ToyDump( fhicl::ParameterSet
const& pset );
63 virtual void analyze( art::Event
const& evt );
66 std::string raw_data_label_;
67 int num_adcs_to_write_;
68 int num_adcs_to_print_;
70 uint32_t columns_to_display_on_screen_;
71 std::string output_file_name_;
76 , raw_data_label_( pset.get<std::string>(
"raw_data_label",
"daq" ) )
77 , num_adcs_to_write_( pset.get<int>(
"num_adcs_to_write", 0 ) )
78 , num_adcs_to_print_( pset.get<int>(
"num_adcs_to_print", 10 ) )
79 , binary_mode_( pset.get<bool>(
"binary_mode", true ) )
80 , columns_to_display_on_screen_( pset.get<uint32_t>(
"columns_to_display_on_screen", 10 ) )
81 , output_file_name_( pset.get<std::string>(
"output_file_name",
"out.bin" ) )
88 art::EventNumber_t eventNumber = evt.event();
94 artdaq::Fragments fragments;
95 artdaq::FragmentPtrs containerFragments;
96 std::vector<std::string> fragment_type_labels{
"TOY1",
"TOY2",
"ContainerTOY1",
"ContainerTOY2"};
98 for (
auto label : fragment_type_labels )
100 art::Handle<artdaq::Fragments> fragments_with_label;
102 evt.getByLabel( raw_data_label_, label, fragments_with_label );
103 if ( !fragments_with_label.isValid() )
continue;
105 if ( label ==
"Container" || label ==
"ContainerTOY1" || label ==
"ContainerTOY2" )
107 for (
auto cont : *fragments_with_label )
109 artdaq::ContainerFragment contf( cont );
110 for (
size_t ii = 0; ii < contf.block_count(); ++ii )
112 containerFragments.push_back( contf[ ii ] );
113 fragments.push_back( *containerFragments.back() );
119 for (
auto frag : *fragments_with_label ) { fragments.emplace_back( frag ); }
124 TLOG( TLVL_INFO ) <<
"Run " << evt.run() <<
", subrun " << evt.subRun() <<
", event " << eventNumber <<
" has "
125 << fragments.size() <<
" fragment(s) of type TOY1 or TOY2";
127 for (
const auto& frag : fragments )
129 ToyFragment bb( frag );
131 TLOG( TLVL_INFO ) << fragmentTypeToString( static_cast<demo::detail::FragmentType>( frag.type() ) )
132 <<
" fragment " << frag.fragmentID() <<
" w/ seqID " << frag.sequenceID() <<
" and timestamp "
133 << frag.timestamp() <<
" has total ADC counts = " << bb.total_adc_values()
134 <<
", trig # = " << bb.hdr_trigger_number()
135 <<
", dist_type = " <<
static_cast<int>( bb.hdr_distribution_type() );
137 if ( frag.hasMetadata() )
139 ToyFragment::Metadata
const* md = frag.metadata<ToyFragment::Metadata>();
140 TLOG( TLVL_DEBUG ) <<
"Fragment metadata: " << std::showbase
141 <<
"Board serial number = " << md->board_serial_number
142 <<
", sample bits = " << md->num_adc_bits
143 <<
" -> max ADC value = " << bb.adc_range( (
int)md->num_adc_bits );
146 if ( num_adcs_to_write_ >= 0 )
148 uint32_t numAdcs = num_adcs_to_write_;
149 if ( num_adcs_to_write_ == 0 )
150 numAdcs = bb.total_adc_values();
151 else if ( static_cast<uint32_t>( num_adcs_to_write_ ) > bb.total_adc_values() )
154 <<
"Asked for more ADC values to file than are in Fragment. Only writing what's here...";
155 numAdcs = bb.total_adc_values();
159 std::ofstream output( output_file_name_, std::ios::out | std::ios::app | std::ios::binary );
160 for ( uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc )
161 { output.write( (
char*)( bb.dataBeginADCs() + i_adc ),
sizeof( ToyFragment::adc_t ) ); }
166 std::ofstream output( output_file_name_, std::ios::out | std::ios::app );
167 output << fragmentTypeToString( static_cast<demo::detail::FragmentType>( frag.type() ) ) <<
"\t"
168 << frag.fragmentID();
170 for ( uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc )
171 { output <<
"\t" << std::to_string( *( bb.dataBeginADCs() + i_adc ) ); }
177 if ( num_adcs_to_print_ >= 0 )
179 uint32_t numAdcs = num_adcs_to_print_;
180 if ( num_adcs_to_print_ == 0 )
181 numAdcs = bb.total_adc_values();
182 else if ( static_cast<uint32_t>( num_adcs_to_print_ ) > bb.total_adc_values() )
185 <<
"Asked for more ADC values to file than are in Fragment. Only writing what's here...";
186 numAdcs = bb.total_adc_values();
189 TLOG( TLVL_INFO ) <<
"First " << numAdcs <<
" ADC values in the fragment:";
190 int rows = 1 + (int)( ( num_adcs_to_print_ - 1 ) / columns_to_display_on_screen_ );
191 uint32_t adc_counter = 0;
192 for (
int idx = 0; idx < rows; ++idx )
194 std::ostringstream o;
196 o << std::setw( 4 ) << std::setfill(
'.' );
197 o << ( idx * columns_to_display_on_screen_ ) <<
": ";
198 for ( uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx )
200 if ( adc_counter >= numAdcs ) {
break; }
201 o << std::setw( 6 ) << std::setfill(
' ' );
202 o << bb.adc_value( adc_counter );
205 TLOG( TLVL_INFO ) << o.str();
ToyDump(fhicl::ParameterSet const &pset)
ToyDump Constructor.
An art::EDAnalyzer module designed to display the data from demo::ToyFragment objects.
virtual ~ToyDump()
ToyDump Destructor.
virtual void analyze(art::Event const &evt)
Analyze an event. Called by art for each event in run (based on command line options) ...