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 artdaq::FragmentPtrs containerFragments;
00093 std::vector<std::string> fragment_type_labels{"TOY1", "TOY2", "ContainerTOY1", "ContainerTOY2"};
00094
00095 for (auto label : fragment_type_labels)
00096 {
00097 art::Handle<artdaq::Fragments> fragments_with_label;
00098
00099 evt.getByLabel(raw_data_label_, label, fragments_with_label);
00100 if (!fragments_with_label.isValid()) continue;
00101
00102
00103
00104
00105
00106 if (label == "Container" || label == "ContainerTOY1" || label == "ContainerTOY2")
00107 {
00108 for (auto cont : *fragments_with_label)
00109 {
00110 artdaq::ContainerFragment contf(cont);
00111 for (size_t ii = 0; ii < contf.block_count(); ++ii)
00112 {
00113 containerFragments.push_back(contf[ii]);
00114 fragments.push_back(*containerFragments.back());
00115 }
00116 }
00117 }
00118 else
00119 {
00120 for (auto frag : *fragments_with_label)
00121 {
00122 fragments.emplace_back(frag);
00123 }
00124 }
00125 }
00126
00127
00128
00129 std::cout << "######################################################################" << std::endl;
00130 std::cout << std::endl;
00131 std::cout << "Run " << evt.run() << ", subrun " << evt.subRun()
00132 << ", event " << eventNumber << " has " << fragments.size()
00133 << " fragment(s) of type TOY1 or TOY2" << std::endl;
00134
00135 for (const auto& frag : fragments)
00136 {
00137 ToyFragment bb(frag);
00138
00139 std::cout << std::endl;
00140 std::cout << fragmentTypeToString(static_cast<demo::detail::FragmentType>(frag.type())) << " fragment " << frag.fragmentID() << " w/ seqID " << frag.sequenceID() << " has total ADC counts = "
00141 << bb.total_adc_values() << std::endl;
00142
00143
00144 if (frag.hasMetadata())
00145 {
00146 std::cout << std::endl;
00147 std::cout << "Fragment metadata: " << std::endl;
00148 ToyFragment::Metadata const* md =
00149 frag.metadata<ToyFragment::Metadata>();
00150 std::cout << std::showbase << "Board serial number = "
00151 << ((int)md->board_serial_number) << ", sample bits = "
00152 << ((int)md->num_adc_bits)
00153 << " -> max ADC value = "
00154 << bb.adc_range((int)md->num_adc_bits)
00155 << std::endl;
00156
00157 }
00158
00159 if (num_adcs_to_show_ == 0)
00160 {
00161 num_adcs_to_show_ = bb.total_adc_values();
00162 }
00163
00164 if (num_adcs_to_show_ > 0)
00165 {
00166 if (num_adcs_to_show_ > bb.total_adc_values())
00167 {
00168 throw cet::exception("num_adcs_to_show is larger than total number of adcs in fragment");
00169 }
00170 else
00171 {
00172 std::cout << std::endl;
00173 std::cout << "First " << num_adcs_to_show_
00174 << " ADC values in the fragment: "
00175 << std::endl;
00176 }
00177
00178 if (dump_to_file_)
00179 {
00180 std::ofstream output("out.bin", std::ios::out | std::ios::app | std::ios::binary);
00181 for (uint32_t i_adc = 0; i_adc < num_adcs_to_show_; ++i_adc)
00182 {
00183 output.write((char*)(bb.dataBeginADCs() + i_adc), sizeof(ToyFragment::adc_t));
00184 }
00185 output.close();
00186 }
00187
00188 if (dump_to_screen_)
00189 {
00190 std::cout << std::right;
00191 int rows = 1 + (int)((num_adcs_to_show_ - 1) / columns_to_display_on_screen_);
00192 uint32_t adc_counter = 0;
00193 for (int idx = 0; idx < rows; ++idx)
00194 {
00195 std::cout << std::setw(4) << std::setfill('.');
00196 std::cout << (idx * columns_to_display_on_screen_) << ": ";
00197 for (uint32_t jdx = 0; jdx < columns_to_display_on_screen_; ++jdx)
00198 {
00199 if (adc_counter >= num_adcs_to_show_) { break; }
00200 std::cout << std::setw(6) << std::setfill(' ');
00201 std::cout << bb.adc_value(adc_counter);
00202 ++adc_counter;
00203 }
00204 std::cout << std::endl;
00205 }
00206 }
00207
00208 std::cout << std::endl;
00209 }
00210 }
00211 std::cout << std::endl;
00212 }
00213
00214 DEFINE_ART_MODULE(demo::ToyDump)