artdaq_demo  v3_06_01
ToyDump_module.cc
1 // Class: ToyDump
3 // Module Type: analyzer
4 // File: ToyDump_module.cc
5 // Description: Prints out information about each event.
7 
8 #define TRACE_NAME "ToyDump"
9 
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"
15 
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"
20 
21 #include <algorithm>
22 #include <cassert>
23 #include <cmath>
24 #include <fstream>
25 #include <iomanip>
26 #include <iostream>
27 #include <vector>
28 
29 namespace demo {
30 class ToyDump;
31 }
32 
36 class demo::ToyDump : public art::EDAnalyzer
37 {
38 public:
52  explicit ToyDump( fhicl::ParameterSet const& pset );
53 
57  virtual ~ToyDump();
58 
63  virtual void analyze( art::Event const& evt );
64 
65 private:
66  std::string raw_data_label_;
67  int num_adcs_to_write_;
68  int num_adcs_to_print_;
69  bool binary_mode_;
70  uint32_t columns_to_display_on_screen_;
71  std::string output_file_name_;
72 };
73 
74 demo::ToyDump::ToyDump( fhicl::ParameterSet const& pset )
75  : EDAnalyzer( pset )
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" ) )
82 {}
83 
85 
86 void demo::ToyDump::analyze( art::Event const& evt )
87 {
88  art::EventNumber_t eventNumber = evt.event();
89 
90  // ***********************
91  // *** Toy Fragments ***
92  // ***********************
93 
94  artdaq::Fragments fragments;
95  artdaq::FragmentPtrs containerFragments;
96  std::vector<std::string> fragment_type_labels{"TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2"};
97 
98  for ( auto label : fragment_type_labels )
99  {
100  art::Handle<artdaq::Fragments> fragments_with_label;
101 
102  evt.getByLabel( raw_data_label_, label, fragments_with_label );
103  if ( !fragments_with_label.isValid() ) continue;
104 
105  if ( label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2" )
106  {
107  for ( auto cont : *fragments_with_label )
108  {
109  artdaq::ContainerFragment contf( cont );
110  for ( size_t ii = 0; ii < contf.block_count(); ++ii )
111  {
112  containerFragments.push_back( contf[ ii ] );
113  fragments.push_back( *containerFragments.back() );
114  }
115  }
116  }
117  else
118  {
119  for ( auto frag : *fragments_with_label ) { fragments.emplace_back( frag ); }
120  }
121  }
122 
123  // look for raw Toy data
124  TLOG( TLVL_INFO ) << "Run " << evt.run() << ", subrun " << evt.subRun() << ", event " << eventNumber << " has "
125  << fragments.size() << " fragment(s) of type TOY1 or TOY2";
126 
127  for ( const auto& frag : fragments )
128  {
129  ToyFragment bb( frag );
130 
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() );
136 
137  if ( frag.hasMetadata() )
138  {
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 );
144  }
145 
146  if ( num_adcs_to_write_ >= 0 )
147  {
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() )
152  {
153  TLOG( TLVL_WARNING )
154  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
155  numAdcs = bb.total_adc_values();
156  }
157  if ( binary_mode_ )
158  {
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 ) ); }
162  output.close();
163  }
164  else
165  {
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();
169 
170  for ( uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc )
171  { output << "\t" << std::to_string( *( bb.dataBeginADCs() + i_adc ) ); }
172  output << std::endl;
173  output.close();
174  }
175  }
176 
177  if ( num_adcs_to_print_ >= 0 )
178  {
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() )
183  {
184  TLOG( TLVL_WARNING )
185  << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
186  numAdcs = bb.total_adc_values();
187  }
188 
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 )
193  {
194  std::ostringstream o;
195  o << std::right;
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 )
199  {
200  if ( adc_counter >= numAdcs ) { break; }
201  o << std::setw( 6 ) << std::setfill( ' ' );
202  o << bb.adc_value( adc_counter );
203  ++adc_counter;
204  }
205  TLOG( TLVL_INFO ) << o.str();
206  }
207  }
208  }
209 }
210 
211 DEFINE_ART_MODULE( demo::ToyDump )
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) ...