|
| 1 | +/** |
| 2 | + * decoder for data files written by rogue |
| 3 | + */ |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | + |
| 7 | +#include "pflib/Exception.h" |
| 8 | +#include "pflib/logging/Logging.h" |
| 9 | +#include "pflib/packing/Hex.h" |
| 10 | +#include "pflib/packing/MultiSampleECONDEventPacket.h" |
| 11 | +#include "pflib/version/Version.h" |
| 12 | +#include "rogue/GeneralError.h" |
| 13 | +#include "rogue/Helpers.h" |
| 14 | +#include "rogue/interfaces/stream/Frame.h" |
| 15 | +#include "rogue/interfaces/stream/FrameIterator.h" |
| 16 | +#include "rogue/interfaces/stream/Slave.h" |
| 17 | +#include "rogue/utilities/fileio/StreamReader.h" |
| 18 | + |
| 19 | +/** |
| 20 | + * Accept frames from the rogue stream reader, filtering out non-Ecal stuff |
| 21 | + * and then write out the ECOND event packet to the CSV |
| 22 | + */ |
| 23 | +class CaloCSVWriter : public rogue::interfaces::stream::Slave { |
| 24 | + mutable pflib::logging::logger the_log_{pflib::logging::get("CaloCSVWriter")}; |
| 25 | + std::ofstream output_; |
| 26 | + pflib::packing::MultiSampleECONDEventPacket ep_; |
| 27 | + |
| 28 | + public: |
| 29 | + CaloCSVWriter(const std::string& filepath, int nlinks) |
| 30 | + : output_{filepath}, ep_{nlinks} { |
| 31 | + if (not output_) { |
| 32 | + PFEXCEPTION_RAISE("NoOpen", "Unable to open file '" + filepath + "'."); |
| 33 | + } |
| 34 | + |
| 35 | + output_ << pflib::packing::MultiSampleECONDEventPacket::to_csv_header; |
| 36 | + } |
| 37 | + void acceptFrame( |
| 38 | + std::shared_ptr<rogue::interfaces::stream::Frame> frame) override { |
| 39 | + if (frame->getError()) { |
| 40 | + pflib_log(debug) << "Rogue header signaled error present"; |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + if (frame->getChannel() != 0) { |
| 45 | + pflib_log(debug) << "Frame belongs to non-data channel " |
| 46 | + << frame->getChannel(); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + // skip first byte which is meaningless i guess? |
| 51 | + auto frame_it{frame->begin() + 1}; |
| 52 | + // get subsystem id |
| 53 | + uint8_t subsystem_id{0}; |
| 54 | + rogue::interfaces::stream::fromFrame(frame_it, 1, &subsystem_id); |
| 55 | + // get to end of first 32-bit word, skipping contributor_id, burn_count |
| 56 | + frame_it += 2; |
| 57 | + |
| 58 | + if (subsystem_id != 5 and subsystem_id != 7) { |
| 59 | + pflib_log(debug) << "Frame belongs to non-calo subsystem " |
| 60 | + << subsystem_id; |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + int frame_size{frame->end() - frame_it}; |
| 65 | + pflib_log(debug) << "Frame size: " << frame_size; |
| 66 | + if (frame_size % 4 != 0) { |
| 67 | + pflib_log(error) << "Frame size " << frame_size |
| 68 | + << " is not multiple of 4 bytes"; |
| 69 | + } |
| 70 | + |
| 71 | + std::vector<uint32_t> words(frame_size / 4); |
| 72 | + for (int i_word{0}; i_word < words.size(); i_word++) { |
| 73 | + rogue::interfaces::stream::fromFrame(frame_it, 4, &(words[i_word])); |
| 74 | + } |
| 75 | + |
| 76 | + // first three words are not related to ECOND |
| 77 | + // 0: meaningless? |
| 78 | + // 1: timestamp |
| 79 | + // 2: timestamp |
| 80 | + ep_.from(std::span(words.begin() + 3, words.end())); |
| 81 | + ep_.to_csv(output_); |
| 82 | + } |
| 83 | +}; |
| 84 | + |
| 85 | +static void usage() { |
| 86 | + std::cout << "\n" |
| 87 | + " USAGE:\n" |
| 88 | + " rogue-decoder [options] NLINKS input_file.dat\n" |
| 89 | + "\n" |
| 90 | + " OPTIONS:\n" |
| 91 | + " -h,--help : print this help and exit\n" |
| 92 | + " -o,--output : output CSV file to dump samples into " |
| 93 | + "(default is input file with extension changed)\n" |
| 94 | + " -n,--nevents : provide maximum number of events (default is " |
| 95 | + "all events possible)\n" |
| 96 | + " -l,--log : logging level to printout (-1: trace up to 4: " |
| 97 | + "fatal)\n" |
| 98 | + << std::endl; |
| 99 | +} |
| 100 | + |
| 101 | +int main(int argc, char* argv[]) { |
| 102 | + pflib::logging::fixture f; |
| 103 | + |
| 104 | + auto the_log_{pflib::logging::get("rogue-decoder")}; |
| 105 | + |
| 106 | + pflib_log(warn) << "The ECOND decoding is not well tested. Inspect the " |
| 107 | + "results carefully and please contribute!"; |
| 108 | + |
| 109 | + if (argc == 1) { |
| 110 | + // can't do anything without any arguments |
| 111 | + usage(); |
| 112 | + return 1; |
| 113 | + } |
| 114 | + |
| 115 | + int n_links{-1}; |
| 116 | + int nevents{-1}; |
| 117 | + std::string in_file, out_file; |
| 118 | + for (int i_arg{1}; i_arg < argc; i_arg++) { |
| 119 | + std::string arg{argv[i_arg]}; |
| 120 | + if (arg[0] == '-') { |
| 121 | + // option |
| 122 | + if (arg == "-h" or arg == "--help") { |
| 123 | + usage(); |
| 124 | + return 0; |
| 125 | + } else if (arg == "-o" or arg == "--output") { |
| 126 | + if (i_arg + 1 == argc or argv[i_arg + 1][0] == '-') { |
| 127 | + pflib_log(fatal) << "The " << arg |
| 128 | + << " parameter requires an argument after it."; |
| 129 | + return 1; |
| 130 | + } |
| 131 | + i_arg++; |
| 132 | + out_file = argv[i_arg]; |
| 133 | + } else if (arg == "-n" or arg == "--nevents") { |
| 134 | + if (i_arg + 1 == argc or argv[i_arg + 1][0] == '-') { |
| 135 | + pflib_log(fatal) << "The " << arg |
| 136 | + << " parameter requires an argument after it."; |
| 137 | + return 1; |
| 138 | + } |
| 139 | + i_arg++; |
| 140 | + try { |
| 141 | + nevents = std::stoi(argv[i_arg]); |
| 142 | + } catch (const std::invalid_argument& e) { |
| 143 | + pflib_log(fatal) << "The argument to " << arg << " '" << argv[i_arg] |
| 144 | + << "' is not an integer."; |
| 145 | + return 1; |
| 146 | + } |
| 147 | + } else if (arg == "-l" or arg == "--log") { |
| 148 | + if (i_arg + 1 == argc) { |
| 149 | + pflib_log(fatal) << "The " << arg |
| 150 | + << " parameter requires an argument after it."; |
| 151 | + return 1; |
| 152 | + } |
| 153 | + std::string arg_p1{argv[i_arg + 1]}; |
| 154 | + if (arg_p1[0] == '-' and arg_p1 != "-1") { |
| 155 | + pflib_log(fatal) << "The " << arg |
| 156 | + << " parameter requires an argument after it."; |
| 157 | + return 1; |
| 158 | + } |
| 159 | + i_arg++; |
| 160 | + try { |
| 161 | + pflib::logging::set(pflib::logging::convert(std::stoi(argv[i_arg]))); |
| 162 | + } catch (const std::invalid_argument& e) { |
| 163 | + pflib_log(fatal) << "The argument to " << arg << " '" << argv[i_arg] |
| 164 | + << "' is not an integer."; |
| 165 | + return 1; |
| 166 | + } |
| 167 | + } else { |
| 168 | + pflib_log(fatal) << "Unrecognized option " << arg; |
| 169 | + return 1; |
| 170 | + } |
| 171 | + } else { |
| 172 | + if (n_links <= 0) { |
| 173 | + try { |
| 174 | + n_links = std::stoi(arg); |
| 175 | + } catch (const std::invalid_argument& e) { |
| 176 | + pflib_log(fatal) << "The first positional argument needs to be the " |
| 177 | + "number of links, but '" |
| 178 | + << arg << "' is not an integer."; |
| 179 | + return 1; |
| 180 | + } |
| 181 | + } else if (not in_file.empty()) { |
| 182 | + pflib_log(fatal) << "Can only decode one file at a time."; |
| 183 | + return 1; |
| 184 | + } else { |
| 185 | + in_file = arg; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + pflib_log(debug) << pflib::version::debug(); |
| 191 | + |
| 192 | + if (in_file.empty()) { |
| 193 | + pflib_log(fatal) << "Need to provide a file to decode."; |
| 194 | + usage(); |
| 195 | + return 1; |
| 196 | + } |
| 197 | + |
| 198 | + if (out_file.empty()) { |
| 199 | + out_file = in_file.substr(0, in_file.find_last_of(".")) + ".csv"; |
| 200 | + } |
| 201 | + |
| 202 | + try { |
| 203 | + // setup stream |
| 204 | + auto reader = std::make_shared<rogue::utilities::fileio::StreamReader>(); |
| 205 | + auto writer = std::make_shared<CaloCSVWriter>(out_file, n_links); |
| 206 | + reader->addSlave(writer); |
| 207 | + |
| 208 | + // run |
| 209 | + reader->open(in_file); |
| 210 | + reader->closeWait(); |
| 211 | + } catch (const pflib::Exception& e) { |
| 212 | + pflib_log(fatal) << "pflib [" << e.name() << "]: " << e.message(); |
| 213 | + return 1; |
| 214 | + } catch (const rogue::GeneralError& e) { |
| 215 | + pflib_log(fatal) << "Rogue Error: " << e.what(); |
| 216 | + return 1; |
| 217 | + } catch (const std::runtime_error& e) { |
| 218 | + pflib_log(fatal) << e.what(); |
| 219 | + return 1; |
| 220 | + } |
| 221 | + |
| 222 | + return 0; |
| 223 | +} |
0 commit comments