|
| 1 | +// Copyright 2019-2020 CERN and copyright holders of ALICE O2. |
| 2 | +// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders. |
| 3 | +// All rights not expressly granted are reserved. |
| 4 | +// |
| 5 | +// This software is distributed under the terms of the GNU General Public |
| 6 | +// License v3 (GPL Version 3), copied verbatim in the file "COPYING". |
| 7 | +// |
| 8 | +// In applying this license CERN does not waive the privileges and immunities |
| 9 | +// granted to it by virtue of its status as an Intergovernmental Organization |
| 10 | +// or submit itself to any jurisdiction. |
| 11 | + |
| 12 | +/// @author Laurent Aphecetche |
| 13 | + |
| 14 | +#include "DataFormatsMCH/DsChannelId.h" |
| 15 | +#include "MCHGlobalMapping/Mapper.h" |
| 16 | +#include "MCHRawElecMap/DsDetId.h" |
| 17 | +#include "MCHRawElecMap/DsElecId.h" |
| 18 | +#include "MCHRawElecMap/Mapper.h" |
| 19 | +#include "boost/program_options.hpp" |
| 20 | +#include <cstdint> |
| 21 | +#include <fmt/format.h> |
| 22 | +#include <fstream> |
| 23 | +#include <gsl/span> |
| 24 | +#include <iostream> |
| 25 | +#include <iostream> |
| 26 | +#include <iterator> |
| 27 | +#include <limits> |
| 28 | +#include <map> |
| 29 | +#include <optional> |
| 30 | +#include <rapidjson/document.h> |
| 31 | +#include <rapidjson/stringbuffer.h> |
| 32 | +#include <rapidjson/writer.h> |
| 33 | +#include <stdexcept> |
| 34 | +#include <string> |
| 35 | + |
| 36 | +namespace po = boost::program_options; |
| 37 | +using namespace o2::mch::raw; |
| 38 | + |
| 39 | +uint16_t computeDsBinX(int feeId, int linkId, int elinkId) |
| 40 | +{ |
| 41 | + constexpr uint64_t maxLinkId = 12; |
| 42 | + constexpr uint64_t maxElinkId = 40; |
| 43 | + |
| 44 | + int v = feeId * maxLinkId * maxElinkId + |
| 45 | + (linkId % maxLinkId) * maxElinkId + elinkId + 1; |
| 46 | + return static_cast<uint16_t>(v & 0xFFFF); |
| 47 | +} |
| 48 | + |
| 49 | +struct DualSampaInfo { |
| 50 | + uint16_t dsBin; |
| 51 | + uint16_t dsBinX; |
| 52 | + int deId; |
| 53 | + int dsId; |
| 54 | + uint16_t feeId; |
| 55 | + uint8_t linkId; |
| 56 | + uint8_t eLinkId; |
| 57 | + uint16_t solarId; |
| 58 | + uint8_t nch; |
| 59 | + uint32_t firstDsChannelId; |
| 60 | +}; |
| 61 | + |
| 62 | +std::vector<DualSampaInfo> dualSampaInfos; |
| 63 | + |
| 64 | +std::vector<DualSampaInfo> computeDualSampaInfos() |
| 65 | +{ |
| 66 | + uint16_t dsBin{0}; |
| 67 | + |
| 68 | + auto elec2det = createElec2DetMapper<ElectronicMapperGenerated>(); |
| 69 | + auto det2elec = createDet2ElecMapper<ElectronicMapperGenerated>(); |
| 70 | + auto solar2FeeLink = createSolar2FeeLinkMapper<ElectronicMapperGenerated>(); |
| 71 | + |
| 72 | + for (uint16_t dsIndex = 0; dsIndex < o2::mch::NumberOfDualSampas; ++dsIndex) { |
| 73 | + DsDetId det{o2::mch::getDsDetId(dsIndex)}; |
| 74 | + auto elec = det2elec(det); |
| 75 | + if (!elec.has_value()) { |
| 76 | + throw std::runtime_error("mapping is wrong somewhere..."); |
| 77 | + } |
| 78 | + auto eLinkId = elec->elinkId(); |
| 79 | + auto solarId = elec->solarId(); |
| 80 | + auto s2f = solar2FeeLink(solarId); |
| 81 | + if (!s2f.has_value()) { |
| 82 | + throw std::runtime_error("mapping is wrong somewhere..."); |
| 83 | + } |
| 84 | + auto feeId = s2f->feeId(); |
| 85 | + auto linkId = s2f->linkId(); |
| 86 | + |
| 87 | + auto dsBinX = computeDsBinX(feeId, linkId, eLinkId); |
| 88 | + |
| 89 | + uint8_t nch = o2::mch::numberOfDualSampaChannels(dsIndex); |
| 90 | + |
| 91 | + o2::mch::DsChannelId firstDsChannelId(solarId, eLinkId, 0); |
| 92 | + |
| 93 | + auto dsId = det.dsId(); |
| 94 | + auto deId = det.deId(); |
| 95 | + |
| 96 | + dualSampaInfos.emplace_back(DualSampaInfo{dsIndex, dsBinX, |
| 97 | + deId, dsId, |
| 98 | + feeId, linkId, |
| 99 | + eLinkId, solarId, nch, |
| 100 | + firstDsChannelId.value()}); |
| 101 | + } |
| 102 | + return dualSampaInfos; |
| 103 | +} |
| 104 | + |
| 105 | +void solar2json(bool mchview) |
| 106 | +{ |
| 107 | + rapidjson::StringBuffer buffer; |
| 108 | + rapidjson::Writer<rapidjson::StringBuffer> writer(buffer); |
| 109 | + |
| 110 | + auto solar2FeeLink = createSolar2FeeLinkMapper<ElectronicMapperGenerated>(); |
| 111 | + auto dualSampaInfos = computeDualSampaInfos(); |
| 112 | + |
| 113 | + auto outputSolars = [&](uint16_t solarId) { |
| 114 | + auto dualSampas = getDualSampas<ElectronicMapperGenerated>(solarId); |
| 115 | + auto deId = dualSampas.begin()->deId(); |
| 116 | + |
| 117 | + writer.StartObject(); |
| 118 | + writer.Key("solarId"); |
| 119 | + writer.Int(solarId); |
| 120 | + auto feelink = solar2FeeLink(solarId); |
| 121 | + uint16_t feeId{std::numeric_limits<uint16_t>::max()}; |
| 122 | + uint8_t linkId{std::numeric_limits<uint8_t>::max()}; |
| 123 | + if (feelink.has_value()) { |
| 124 | + feeId = feelink->feeId(); |
| 125 | + linkId = feelink->linkId(); |
| 126 | + writer.Key("FeeId"); |
| 127 | + writer.Int(feeId); |
| 128 | + writer.Key("LinkId"); |
| 129 | + writer.Int(linkId); |
| 130 | + } |
| 131 | + writer.Key("deId"); |
| 132 | + writer.Int(deId); |
| 133 | + writer.Key("ds"); |
| 134 | + writer.StartArray(); |
| 135 | + for (const auto& dsi : dualSampaInfos) { |
| 136 | + if (dsi.feeId == feeId && |
| 137 | + dsi.linkId == linkId) { |
| 138 | + writer.StartObject(); |
| 139 | + writer.Key("dsId"); |
| 140 | + writer.Int(dsi.dsId); |
| 141 | + if (mchview) { |
| 142 | + writer.Key("elinkId"); |
| 143 | + } else { |
| 144 | + writer.Key("eLinkId"); |
| 145 | + } |
| 146 | + writer.Int(dsi.eLinkId); |
| 147 | + if (mchview) { |
| 148 | + writer.Key("dsbin"); |
| 149 | + writer.Int(dsi.dsBinX); |
| 150 | + } else { |
| 151 | + writer.Key("binX"); |
| 152 | + writer.Int(dsi.dsBinX); |
| 153 | + writer.Key("bin"); |
| 154 | + writer.Int(dsi.dsBin); |
| 155 | + if (dsi.nch != 64) { |
| 156 | + writer.Key("nch"); |
| 157 | + writer.Int(dsi.nch); |
| 158 | + } |
| 159 | + writer.Key("fdci"); |
| 160 | + writer.Int(dsi.firstDsChannelId); |
| 161 | + } |
| 162 | + writer.EndObject(); |
| 163 | + } |
| 164 | + } |
| 165 | + writer.EndArray(); |
| 166 | + writer.EndObject(); |
| 167 | + }; |
| 168 | + |
| 169 | + auto solarIds = getSolarUIDs<ElectronicMapperGenerated>(); |
| 170 | + writer.StartArray(); |
| 171 | + for (auto s : solarIds) { |
| 172 | + outputSolars(s); |
| 173 | + } |
| 174 | + writer.EndArray(); |
| 175 | + |
| 176 | + std::cout << buffer.GetString() << "\n"; |
| 177 | +} |
| 178 | + |
| 179 | +int main(int argc, char* argv[]) |
| 180 | +{ |
| 181 | + po::variables_map vm; |
| 182 | + po::options_description generic("Generic options"); |
| 183 | + |
| 184 | + // clang-format off |
| 185 | + generic.add_options() |
| 186 | + ("help,h", "produce help message") |
| 187 | + ("solar,s","output solar based file") |
| 188 | + ("mchview,m","output format suitable for mchview") |
| 189 | + ; |
| 190 | + // clang-format on |
| 191 | + |
| 192 | + po::options_description cmdline; |
| 193 | + cmdline.add(generic); |
| 194 | + |
| 195 | + po::store(po::command_line_parser(argc, argv).options(cmdline).run(), vm); |
| 196 | + |
| 197 | + if (vm.count("help")) { |
| 198 | + std::cout << generic << "\n"; |
| 199 | + return 2; |
| 200 | + } |
| 201 | + |
| 202 | + try { |
| 203 | + po::notify(vm); |
| 204 | + } catch (boost::program_options::error& e) { |
| 205 | + std::cout << "Error: " << e.what() << "\n"; |
| 206 | + exit(1); |
| 207 | + } |
| 208 | + |
| 209 | + if (vm.count("solar")) { |
| 210 | + solar2json(vm.count("mchview")); |
| 211 | + } |
| 212 | + |
| 213 | + return 0; |
| 214 | +} |
0 commit comments