00001
00002
00003
00004
00005
00007
00008 #define TRACE_NAME "ToyDump"
00009
00010 #include "art/Framework/Core/EDAnalyzer.h"
00011 #include "art/Framework/Core/ModuleMacros.h"
00012 #include "art/Framework/Principal/Event.h"
00013 #include "art/Framework/Principal/Handle.h"
00014 #include "canvas/Utilities/Exception.h"
00015
00016 #include "artdaq-core-demo/Overlays/FragmentType.hh"
00017 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
00018 #include "artdaq-core/Data/ContainerFragment.hh"
00019 #include "artdaq-core/Data/Fragment.hh"
00020
00021 #include <algorithm>
00022 #include <cassert>
00023 #include <cmath>
00024 #include <fstream>
00025 #include <iomanip>
00026 #include <vector>
00027 #include <iostream>
00028
00029 namespace demo
00030 {
00031 class ToyDump;
00032 }
00033
00037 class demo::ToyDump : public art::EDAnalyzer
00038 {
00039 public:
00053 explicit ToyDump(fhicl::ParameterSet const& pset);
00054
00058 virtual ~ToyDump();
00059
00064 virtual void analyze(art::Event const& evt);
00065
00066 private:
00067 std::string raw_data_label_;
00068 int num_adcs_to_write_;
00069 int num_adcs_to_print_;
00070 bool dump_to_file_;
00071 bool dump_to_screen_;
00072 uint32_t columns_to_display_on_screen_;
00073 std::string output_file_name_;
00074 };
00075
00076
00077 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
00078 : EDAnalyzer(pset)
00079 , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
00080 , num_adcs_to_write_(pset.get<int>("num_adcs_to_write", 0))
00081 , num_adcs_to_print_(pset.get<int>("num_adcs_to_print", 10))
00082 , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10))
00083 , output_file_name_(pset.get<std::string>("output_file_name", "out.bin"))
00084 {}
00085
00086 demo::ToyDump::~ToyDump() {}
00087
00088 void demo::ToyDump::analyze(art::Event const& evt)
00089 {
00090 art::EventNumber_t eventNumber = evt.event();
00091
00092
00093
00094
00095
00096 artdaq::Fragments fragments;
00097 artdaq::FragmentPtrs containerFragments;
00098 std::vector<std::string> fragment_type_labels{ "TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2" };
00099
00100 for (auto label : fragment_type_labels)
00101 {
00102 art::Handle<artdaq::Fragments> fragments_with_label;
00103
00104 evt.getByLabel(raw_data_label_, label, fragments_with_label);
00105 if (!fragments_with_label.isValid()) continue;
00106
00107 if (label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2")
00108 {
00109 for (auto cont : *fragments_with_label)
00110 {
00111 artdaq::ContainerFragment contf(cont);
00112 for (size_t ii = 0; ii < contf.block_count(); ++ii)
00113 {
00114 containerFragments.push_back(contf[ii]);
00115 fragments.push_back(*containerFragments.back());
00116 }
00117 }
00118 }
00119 else
00120 {
00121 for (auto frag : *fragments_with_label)
00122 {
00123 fragments.emplace_back(frag);
00124 }
00125 }
00126 }
00127
00128
00129 TLOG(TLVL_INFO) << "Run " << evt.run() << ", subrun " << evt.subRun()
00130 << ", event " << eventNumber << " has " << fragments.size()
00131 << " fragment(s) of type TOY1 or TOY2";
00132
00133 for (const auto& frag : fragments)
00134 {
00135 ToyFragment bb(frag);
00136
00137 TLOG(TLVL_INFO) << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID()
00138 << " w/ seqID " << frag.sequenceID()
00139 << " has total ADC counts = " << bb.total_adc_values();
00140
00141 if (frag.hasMetadata())
00142 {
00143 ToyFragment::Metadata const* md =
00144 frag.metadata<ToyFragment::Metadata>();
00145 TLOG(TLVL_DEBUG) << "Fragment metadata: " << std::showbase << "Board serial number = "
00146 << md->board_serial_number << ", sample bits = "
00147 << md->num_adc_bits
00148 << " -> max ADC value = "
00149 << bb.adc_range((int)md->num_adc_bits);
00150 }
00151
00152 if (num_adcs_to_write_ >= 0)
00153 {
00154 uint32_t numAdcs = num_adcs_to_write_;
00155 if (num_adcs_to_write_ == 0) numAdcs = bb.total_adc_values();
00156 else if (static_cast<uint32_t>(num_adcs_to_write_) > bb.total_adc_values())
00157 {
00158 TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
00159 numAdcs = bb.total_adc_values();
00160 }
00161 std::ofstream output(output_file_name_, std::ios::out | std::ios::app | std::ios::binary);
00162 for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
00163 {
00164 output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
00165 }
00166 output.close();
00167 }
00168
00169 if (num_adcs_to_print_ >= 0)
00170 {
00171 uint32_t numAdcs = num_adcs_to_print_;
00172 if (num_adcs_to_print_ == 0) numAdcs = bb.total_adc_values();
00173 else if (static_cast<uint32_t>(num_adcs_to_print_) > bb.total_adc_values())
00174 {
00175 TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
00176 numAdcs = bb.total_adc_values();
00177 }
00178
00179 TLOG(TLVL_INFO) << "First " << numAdcs << " ADC values in the fragment:";
00180 int rows = 1 + (int)((num_adcs_to_print_ - 1) / columns_to_display_on_screen_);
00181 uint32_t adc_counter = 0;
00182 for (int idx = 0; idx < rows; ++idx)
00183 {
00184 std::ostringstream o;
00185 o << std::right;
00186 o << std::setw(4) << std::setfill('.');
00187 o << (idx * columns_to_display_on_screen_) << ": ";
00188 for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
00189 {
00190 if (adc_counter >= numAdcs) { break; }
00191 o << std::setw(6) << std::setfill(' ');
00192 o << bb.adc_value(adc_counter);
00193 ++adc_counter;
00194 }
00195 TLOG(TLVL_INFO) << o.str();
00196 }
00197 }
00198 }
00199 }
00200
00201 DEFINE_ART_MODULE(demo::ToyDump)