00001
00002
00003
00004
00005
00007
00008 #include "art/Framework/Core/EDAnalyzer.h"
00009 #include "art/Framework/Core/ModuleMacros.h"
00010 #include "art/Framework/Principal/Event.h"
00011 #include "art/Framework/Principal/Handle.h"
00012 #include "canvas/Utilities/Exception.h"
00013
00014 #include "artdaq-core-demo/Overlays/FragmentType.hh"
00015 #include "artdaq-core-demo/Overlays/ToyFragment.hh"
00016 #include "artdaq-core/Data/ContainerFragment.hh"
00017 #include "artdaq-core/Data/Fragment.hh"
00018
00019 #include <algorithm>
00020 #include <cassert>
00021 #include <cmath>
00022 #include <fstream>
00023 #include <iomanip>
00024 #include <vector>
00025 #include <iostream>
00026
00027 namespace demo
00028 {
00029 class ToyDump;
00030 }
00031
00035 class demo::ToyDump : public art::EDAnalyzer
00036 {
00037 public:
00051 explicit ToyDump(fhicl::ParameterSet const& pset);
00052
00056 virtual ~ToyDump();
00057
00062 virtual void analyze(art::Event const& evt);
00063
00064 private:
00065 std::string raw_data_label_;
00066 uint32_t num_adcs_to_show_;
00067 bool dump_to_file_;
00068 bool dump_to_screen_;
00069 uint32_t columns_to_display_on_screen_;
00070 };
00071
00072
00073 demo::ToyDump::ToyDump(fhicl::ParameterSet const& pset)
00074 : EDAnalyzer(pset)
00075 , raw_data_label_(pset.get<std::string>("raw_data_label", "daq"))
00076 , num_adcs_to_show_(pset.get<uint32_t>("num_adcs_to_show", 0))
00077 , dump_to_file_(pset.get<bool>("dump_to_file", true))
00078 , dump_to_screen_(pset.get<bool>("dump_to_screen", false))
00079 , columns_to_display_on_screen_(pset.get<uint32_t>("columns_to_display_on_screen", 10)) {}
00080
00081 demo::ToyDump::~ToyDump() {}
00082
00083 void demo::ToyDump::analyze(art::Event const& evt)
00084 {
00085 art::EventNumber_t eventNumber = evt.event();
00086
00087
00088
00089
00090
00091 artdaq::Fragments fragments;
00092 std::vector<std::string> fragment_type_labels{"TOY1", "TOY2", "Container"};
00093
00094 for (auto label : fragment_type_labels)
00095 {
00096 art::Handle<artdaq::Fragments> fragments_with_label;
00097
00098 evt.getByLabel(raw_data_label_, label, fragments_with_label);
00099 if (!fragments_with_label.isValid()) continue;
00100
00101
00102
00103
00104
00105 if (label == "Container")
00106 {
00107 for (auto cont : *fragments_with_label)
00108 {
00109 artdaq::ContainerFragment contf(cont);
00110 for (size_t ii = 0; ii < contf.block_count(); ++ii)
00111 {
00112 size_t fragSize = contf.fragSize(ii);
00113 artdaq::Fragment thisfrag;
00114 thisfrag.resizeBytes(fragSize);
00115
00116
00117 memcpy(thisfrag.headerAddress(), contf.at(ii), fragSize);
00118
00119
00120 fragments.push_back(thisfrag);
00121 }
00122 }
00123 }
00124 else
00125 {
00126 for (auto frag : *fragments_with_label)
00127 {
00128 fragments.emplace_back(frag);
00129 }
00130 }
00131 }
00132
00133
00134
00135 std::cout << "######################################################################" << std::endl;
00136 std::cout << std::endl;
00137 std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
00138 << ", event " << eventNumber << " has " << fragments.size()
00139 << " fragment(s) of type TOY1 or TOY2" << std::endl;
00140
00141 for (const auto& frag : fragments)
00142 {
00143 ToyFragment bb(frag);
00144
00145 std::cout << std::endl;
00146 std::cout << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " has total ADC counts = "
00147 << bb.total_adc_values() << std::endl;
00148
00149
00150 if (frag.hasMetadata())
00151 {
00152 std::cout << std::endl;
00153 std::cout << "Fragment metadata: " << std::endl;
00154 ToyFragment::Metadata const* md =
00155 frag.metadata<ToyFragment::Metadata>();
00156 std::cout << std::showbase << "Board serial number = "
00157 << ((int)md->board_serial_number) << ", sample bits = "
00158 << ((int)md->num_adc_bits)
00159 << " -> max ADC value = "
00160 << bb.adc_range((int)md->num_adc_bits)
00161 << std::endl;
00162
00163 }
00164
00165 if (num_adcs_to_show_ == 0)
00166 {
00167 num_adcs_to_show_ = bb.total_adc_values();
00168 }
00169
00170 if (num_adcs_to_show_ > 0)
00171 {
00172 if (num_adcs_to_show_ > bb.total_adc_values())
00173 {
00174 throw cet::exception("num_adcs_to_show is larger than total number of adcs in fragment");
00175 }
00176 else
00177 {
00178 std::cout << std::endl;
00179 std::cout << "First " << num_adcs_to_show_
00180 << " ADC values in the fragment: "
00181 << std::endl;
00182 }
00183
00184 if (dump_to_file_)
00185 {
00186 std::ofstream output("out.bin", std::ios::out | std::ios::app | std::ios::binary);
00187 for (uint32_t i_adc = 0; i_adc < num_adcs_to_show_; ++i_adc)
00188 {
00189 output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
00190 }
00191 output.close();
00192 }
00193
00194 if (dump_to_screen_)
00195 {
00196 std::cout << std::right;
00197 int rows = 1 + (int)((num_adcs_to_show_ - 1) / columns_to_display_on_screen_);
00198 uint32_t adc_counter = 0;
00199 for (int idx = 0; idx < rows; ++idx)
00200 {
00201 std::cout << std::setw(4) << std::setfill('.');
00202 std::cout << (idx * columns_to_display_on_screen_) << ": ";
00203 for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
00204 {
00205 if (adc_counter >= num_adcs_to_show_) { break; }
00206 std::cout << std::setw(6) << std::setfill(' ');
00207 std::cout << bb.adc_value(adc_counter);
00208 ++adc_counter;
00209 }
00210 std::cout << std::endl;
00211 }
00212 }
00213
00214 std::cout << std::endl;
00215 }
00216 }
00217 std::cout << std::endl;
00218 }
00219
00220 DEFINE_ART_MODULE(demo::ToyDump)