Project
Loading...
Searching...
No Matches
DsElecId.cxx
Go to the documentation of this file.
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
13#include "Assertions.h"
14#include <fmt/format.h>
15#include <iostream>
16#include <sstream>
17#include <vector>
18#include <string>
19
20namespace o2::mch::raw
21{
22DsElecId::DsElecId(uint16_t solarId, uint8_t elinkGroupId, uint8_t elinkIndex)
23 : mSolarId{solarId}, mElinkGroupId{elinkGroupId}, mElinkIndexInGroup{elinkIndex}
24{
25 impl::assertIsInRange("solarId", solarId, 0, 1023);
26 impl::assertIsInRange("elinkGroupId", mElinkGroupId, 0, 7);
27 impl::assertIsInRange("elinkIndex", mElinkIndexInGroup, 0, 4);
28}
29
30uint32_t encode(const DsElecId& id)
31{
32 return ((id.solarId() & 0xFFFF) << 16) |
33 ((id.elinkGroupId() & 0xFF) << 8) |
34 ((id.elinkIndexInGroup() & 0xFF) << 0);
35}
36
37std::optional<DsElecId> decodeDsElecId(uint32_t code)
38{
39 uint16_t solarId = (code & 0xFFFF0000) >> 16; // 11 (LSB) bits used
40
41 uint8_t groupId = (code & 0xFF00) >> 8; // 3 (LSB) bits used
42
43 uint8_t index = (code & 0xFF) >> 0; // 3 (LSB) bits used
44
45 if (groupId > 7) {
46 return std::nullopt;
47 }
48 if (index > 4) {
49 return std::nullopt;
50 }
51 return DsElecId(solarId, groupId, index);
52}
53
54std::optional<DsElecId> decodeDsElecId(std::string rep)
55{
56 std::istringstream is(rep);
57 std::string line;
58 std::vector<std::string> tokens;
59 while (getline(is, line, '-')) {
60 tokens.emplace_back(line);
61 }
62 if (tokens.size() < 3) {
63 // not a valid representation of a DsElecId
64 return std::nullopt;
65 }
66 if (tokens[0].empty() || tokens[0][0] != 'S') {
67 // token[0] is not a valid representation of a solarId
68 return std::nullopt;
69 }
70 if (tokens[1].empty() || tokens[1][0] != 'J') {
71 // token[1] is not a valid representation of a groupId
72 return std::nullopt;
73 }
74 if (tokens[2].size() < 3 || tokens[2][0] != 'D' || tokens[2][1] != 'S') {
75 // token is not a valid representation of a DS
76 return std::nullopt;
77 }
78 uint16_t solarId = std::atoi(tokens[0].substr(1).c_str());
79 uint8_t groupId = std::atoi(tokens[1].substr(1).c_str());
80 uint8_t index = std::atoi(tokens[2].substr(2).c_str());
81 return DsElecId(solarId, groupId, index);
82}
83
84std::optional<uint8_t> decodeChannelId(std::string rep)
85{
86 auto dsElecId = decodeDsElecId(rep);
87 if (!dsElecId.has_value()) {
88 // must be a valid dsElecId
89 return std::nullopt;
90 }
91 std::istringstream is(rep);
92 std::string line;
93 std::vector<std::string> tokens;
94 while (getline(is, line, '-')) {
95 tokens.emplace_back(line);
96 }
97 if (tokens.size() < 4) {
98 // not a valid representation of a {DsElecId,ChannelId}
99 return std::nullopt;
100 }
101 if (tokens[3].size() < 3 || tokens[3][0] != 'C' || tokens[3][1] != 'H') {
102 // token[3] is not a valid representation of a CH
103 return std::nullopt;
104 }
105 auto chId = std::atoi(tokens[3].substr(2).c_str());
106 if (chId >= 0 && chId <= 63) {
107 return chId;
108 }
109 return std::nullopt;
110}
111
112std::ostream& operator<<(std::ostream& os, const DsElecId& id)
113{
114 std::cout << fmt::format("DsElecId(SOLAR=S{:4d} GROUP=J{:2d} INDEX=DS{:2d}) CODE={:8d}",
115 id.solarId(), id.elinkGroupId(), id.elinkIndexInGroup(), encode(id));
116 return os;
117}
118
119std::string asString(DsElecId dsId)
120{
121 return fmt::format("S{}-J{}-DS{}", dsId.solarId(), dsId.elinkGroupId(), dsId.elinkIndexInGroup());
122}
123
124std::optional<uint8_t> groupFromElinkId(uint8_t elinkId)
125{
126 if (elinkId < 40) {
127 return elinkId / 5;
128 }
129 return std::nullopt;
130}
131
132std::optional<uint8_t> indexFromElinkId(uint8_t elinkId)
133{
134 if (elinkId < 40) {
135 return elinkId - (elinkId / 5) * 5;
136 }
137 return std::nullopt;
138}
139} // namespace o2::mch::raw
constexpr uint16_t solarId() const
solarId is an identifier that uniquely identify a solar board
Definition DsElecId.h:50
DsElecId(uint16_t solarId, uint8_t elinkGroupId, uint8_t elinkIndex)
Definition DsElecId.cxx:22
constexpr uint8_t elinkIndexInGroup() const
Definition DsElecId.h:30
constexpr uint8_t elinkGroupId() const
Definition DsElecId.h:37
GLsizeiptr size
Definition glcorearb.h:659
GLuint index
Definition glcorearb.h:781
int assertIsInRange(std::string what, uint64_t value, uint64_t min, uint64_t max)
Definition Assertions.h:20
std::optional< uint8_t > indexFromElinkId(uint8_t elinkId)
Extracts the index from the elinkId.
Definition DsElecId.cxx:132
std::optional< uint8_t > decodeChannelId(std::string rep)
Definition DsElecId.cxx:84
std::optional< uint8_t > groupFromElinkId(uint8_t elinkId)
Extracts the group from the elinkId.
Definition DsElecId.cxx:124
std::string asString(const SampaCluster &sc)
std::optional< DsElecId > decodeDsElecId(uint32_t code)
Definition DsElecId.cxx:37
uint32_t encode(const DsDetId &id)
Create an integer code for the given id.
Definition DsDetId.cxx:28
std::ostream & operator<<(std::ostream &stream, o2::InteractionRecord const &ir)
void empty(int)