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:
00054 explicit ToyDump(fhicl::ParameterSet const& pset);
00055
00059 virtual ~ToyDump();
00060
00065 virtual void analyze(art::Event const& evt);
00066
00067 private:
00068 std::string raw_data_label_;
00069 int num_adcs_to_write_;
00070 int num_adcs_to_print_;
00071 bool binary_mode_;
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 , binary_mode_(pset.get<bool>("binary_mode", true))
00083 , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10))
00084 , output_file_name_(pset.get<std::string>("output_file_name", "out.bin"))
00085 {}
00086
00087 demo::ToyDump::~ToyDump() {}
00088
00089 void demo::ToyDump::analyze(art::Event const& evt)
00090 {
00091 art::EventNumber_t eventNumber = evt.event();
00092
00093
00094
00095
00096
00097 artdaq::Fragments fragments;
00098 artdaq::FragmentPtrs containerFragments;
00099 std::vector<std::string> fragment_type_labels{ "TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2" };
00100
00101 for (auto label : fragment_type_labels)
00102 {
00103 art::Handle<artdaq::Fragments> fragments_with_label;
00104
00105 evt.getByLabel(raw_data_label_, label, fragments_with_label);
00106 if (!fragments_with_label.isValid()) continue;
00107
00108 if (label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2")
00109 {
00110 for (auto cont : *fragments_with_label)
00111 {
00112 artdaq::ContainerFragment contf(cont);
00113 for (size_t ii = 0; ii < contf.block_count(); ++ii)
00114 {
00115 containerFragments.push_back(contf[ii]);
00116 fragments.push_back(*containerFragments.back());
00117 }
00118 }
00119 }
00120 else
00121 {
00122 for (auto frag : *fragments_with_label)
00123 {
00124 fragments.emplace_back(frag);
00125 }
00126 }
00127 }
00128
00129
00130 TLOG(TLVL_INFO) << "Run " << evt.run() << ", subrun " << evt.subRun()
00131 << ", event " << eventNumber << " has " << fragments.size()
00132 << " fragment(s) of type TOY1 or TOY2";
00133
00134 for (const auto& frag : fragments)
00135 {
00136 ToyFragment bb(frag);
00137
00138 TLOG(TLVL_INFO) << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID()
00139 << " w/ seqID " << frag.sequenceID()
00140 << " has total ADC counts = " << bb.total_adc_values();
00141
00142 if (frag.hasMetadata())
00143 {
00144 ToyFragment::Metadata const* md =
00145 frag.metadata<ToyFragment::Metadata>();
00146 TLOG(TLVL_DEBUG) << "Fragment metadata: " << std::showbase << "Board serial number = "
00147 << md->board_serial_number << ", sample bits = "
00148 << md->num_adc_bits
00149 << " -> max ADC value = "
00150 << bb.adc_range((int)md->num_adc_bits);
00151 }
00152
00153 if (num_adcs_to_write_ >= 0)
00154 {
00155 uint32_t numAdcs = num_adcs_to_write_;
00156 if (num_adcs_to_write_ == 0) numAdcs = bb.total_adc_values();
00157 else if (static_cast<uint32_t>(num_adcs_to_write_) > bb.total_adc_values())
00158 {
00159 TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
00160 numAdcs = bb.total_adc_values();
00161 }
00162 if (binary_mode_) {
00163 std::ofstream output(output_file_name_, std::ios::out | std::ios::app | std::ios::binary);
00164 for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
00165 {
00166 output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
00167 }
00168 output.close();
00169 }
00170 else {
00171 std::ofstream output(output_file_name_, std::ios::out | std::ios::app);
00172 output << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << "\t" << frag.fragmentID();
00173
00174 for (uint32_t i_adc = 0; i_adc < numAdcs; ++i_adc)
00175 {
00176 output << "\t" << std::to_string(*(bb.dataBeginADCs() + i_adc));
00177 }
00178 output << std::endl;
00179 output.close();
00180
00181 }
00182 }
00183
00184 if (num_adcs_to_print_ >= 0)
00185 {
00186 uint32_t numAdcs = num_adcs_to_print_;
00187 if (num_adcs_to_print_ == 0) numAdcs = bb.total_adc_values();
00188 else if (static_cast<uint32_t>(num_adcs_to_print_) > bb.total_adc_values())
00189 {
00190 TLOG(TLVL_WARNING) << "Asked for more ADC values to file than are in Fragment. Only writing what's here...";
00191 numAdcs = bb.total_adc_values();
00192 }
00193
00194 TLOG(TLVL_INFO) << "First " << numAdcs << " ADC values in the fragment:";
00195 int rows = 1 + (int)((num_adcs_to_print_ - 1) / columns_to_display_on_screen_);
00196 uint32_t adc_counter = 0;
00197 for (int idx = 0; idx < rows; ++idx)
00198 {
00199 std::ostringstream o;
00200 o << std::right;
00201 o << std::setw(4) << std::setfill('.');
00202 o << (idx * columns_to_display_on_screen_) << ": ";
00203 for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
00204 {
00205 if (adc_counter >= numAdcs) { break; }
00206 o << std::setw(6) << std::setfill(' ');
00207 o << bb.adc_value(adc_counter);
00208 ++adc_counter;
00209 }
00210 TLOG(TLVL_INFO) << o.str();
00211 }
00212 }
00213 }
00214 }
00215
00216 DEFINE_ART_MODULE(demo::ToyDump)