SND@LHC Software
Loading...
Searching...
No Matches
boardMappingParser.cxx
Go to the documentation of this file.
1// This is the C++ version of shipLHC/rawData/boardMappingParser.py
2#include <iostream>
3#include <sstream>
4#include <fstream>
5#include <string>
6#include <map>
7#include <tuple>
8#include "nlohmann/json.hpp"
9#include "TString.h" // to use Form
10
11using namespace std;
12using json = nlohmann::json;
13
14map<string, map<string, map<string, int>> > boardMaps{};
15map<string, map<string, map<string, string>> > boardMapsMu{};
16
17using json = nlohmann::json;
18
19namespace quicktype {
20 struct Info
21 {
22 string Class;
23 string type;
24 int board;
25 vector<string> slots;
26 };
28 {
29 string Class;
30 string type;
31 vector<int> boards;
32 };
33
34 inline json get_untyped(const json &j, const char *property) {
35 if (j.find(property) != j.end()) {
36 return j.at(property).get<json>();
37 }
38 return json();
39 }
40}
41
42namespace nlohmann {
43
44 inline void from_json(const json& _j, struct quicktype::Info& _x) {
45 _x.Class = _j.at("class").get<string>();
46 _x.type = _j.at("type").get<string>();
47 _x.board = _j.at("board").get<int>();
48 _x.slots = _j.at("slots").get<vector<string>>();
49 }
50
51 inline void to_json(json& _j, const struct quicktype::Info& _x) {
52 _j = json{{"class", _x.Class}, {"type", _x.type}, {"board", _x.board}, {"slots", _x.slots}};
53 }
54 inline void from_json(const json& _j, struct quicktype::Info_scifi& _x) {
55 _x.Class = _j.at("class").get<string>();
56 _x.type = _j.at("type").get<string>();
57 _x.boards = _j.at("boards").get<vector<int>>();
58 }
59
60 inline void to_json(json& _j, const struct quicktype::Info_scifi& _x) {
61 _j = json{{"class", _x.Class}, {"type", _x.type}, {"boards", _x.boards}};
62 }
63}
64
65tuple<map<string, map<string, map<string, int>> >, map<string, map<string, map<string, string>> > > getBoardMapping(json j)
66{
67 /*
68 the mapping is structured as: {<subsystem>: {<plane>: {<settings>...}, ...}, ...}
69 subsystem is veto, scifi, us, ds
70 plane depends on subsystem (e.g. '1x', '1y'... for Scifi)
71 settings depends on sybsystem as well
72 more info here: https://gitlab.cern.ch/snd-scifi/software/-/wikis/Board-Mapping
73 */
74
75 // Seperate structure for scifi than other subsystems
76 quicktype::Info info;
77 quicktype::Info_scifi info_scifi;
78
79 string bString;
80 char c;
81
82 for (auto& el : j.items())
83 {
84 // Loop over the planes in the subsystem
85 for (auto& subel : el.value().items())
86 {
87 string jsonStr = subel.value().dump();
88
89 // Each subsystem is treated separately
90 if (el.key() =="scifi")
91 {
92 info_scifi = nlohmann::json::parse(jsonStr);
93 // sanity check that the settings are correct
94 if (info_scifi.Class != "multiboard" || info_scifi.type != "snd_scifi")
95 {
96 cout << "Wrong class/type: " << info_scifi.Class << "/" << info_scifi.type << endl;
97 break;
98 }
99 // Loop over the boards (for SciFi)
100 for (int i = 0; i < info_scifi.boards.size(); i++)
101 {
102 bString = Form("board_%i", info_scifi.boards.at(i));
103 c =::toupper(subel.key()[1]);
104 boardMaps["Scifi"][bString][Form("M%c%c", subel.key()[0], c )] = i;
105 }
106 }
107 else if (el.key() =="veto")
108 {
109 info = nlohmann::json::parse(jsonStr);
110 // sanity check that the settings are correct
111 if (info.Class != "multislot" || info.type != "snd_veto")
112 {
113 cout << "Wrong class/type: " << info.Class << "/" << info.type << endl;
114 break;
115 }
116 // Add the "board_XX' entry in the dictionary if not already there
117 bString = Form("board_%i", info.board);
118 if (boardMapsMu["MuFilter"].find(bString.c_str()) == boardMapsMu["MuFilter"].end())
119 {
120 boardMapsMu["MuFilter"][bString] = {};
121 }
122 // Loop over the slots (the first is always left, the second always right)
123 for (int i = 0; i < info.slots.size(); i++)
124 {
125 bString = Form("board_%i", info.board);
126 if (stoi(subel.key())<3){
127 if (i==0) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("Veto_%iLeft", stoi(subel.key()));
128 else if (i==1) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("Veto_%iRight", stoi(subel.key()));
129 }
130 if (stoi(subel.key())==3) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("Veto_%iVert", stoi(subel.key()));
131 }
132 }
133 else if (el.key() =="us")
134 {
135 info = nlohmann::json::parse(jsonStr);
136 // sanity check that the settings are correct
137 if (info.Class != "multislot" || info.type != "snd_us")
138 {
139 cout << "Wrong class/type: " << info.Class << "/" << info.type << endl;
140 break;
141 }
142 // Add the "board_XX' entry in the dictionary if not already there
143 bString = Form("board_%i", info.board);
144 if (boardMapsMu["MuFilter"].find(bString.c_str()) == boardMapsMu["MuFilter"].end())
145 {
146 boardMapsMu["MuFilter"][bString] = {};
147 }
148 // Loop over the slots (the first is always left, the second always right)
149 for (int i = 0; i < info.slots.size(); i++)
150 {
151 bString = Form("board_%i", info.board);
152 if (i==0) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("US_%iLeft", stoi(subel.key()));
153 else if (i==1) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("US_%iRight", stoi(subel.key()));
154 }
155 }
156 else if (el.key() =="ds")
157 {
158 info = nlohmann::json::parse(jsonStr);
159 // sanity check that the settings are correct
160 if (info.Class != "multislot" || (info.type != "snd_dsh" && info.type != "snd_dsv"))
161 {
162 cout << "Wrong class/type: " << info.Class << "/" << info.type << endl;
163 break;
164 }
165 // Add the "board_XX' entry in the dictionary if not already there
166 bString = Form("board_%i", info.board);
167 if (boardMapsMu["MuFilter"].find(bString.c_str()) == boardMapsMu["MuFilter"].end())
168 {
169 boardMapsMu["MuFilter"][bString] = {};
170 }
171 // for DS we have the additional complication of vertival planes,
172 // but they are two different plane types (snd_dsh and snd_dsv)
173 if(info.type == "snd_dsh")
174 {
175 // Loop over the slots (the first is always left, the second always right)
176 for (int i = 0; i < info.slots.size(); i++)
177 {
178 bString = Form("board_%i", info.board);
179 if (i==0) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("DS_%iLeft", stoi(subel.key()));
180 else if (i==1) boardMapsMu["MuFilter"][bString][info.slots.at(i)] = Form("DS_%iRight", stoi(subel.key()));
181 }
182 }
183 else
184 {
185 boardMapsMu["MuFilter"][bString][info.slots.at(0)] = Form("DS_%iVert", stoi(subel.key()));
186 }
187 }
188 else
189 {
190 cout << "Unknown subsystem " << el.key() <<endl;
191 break;
192 }
193 }
194 }
195 return make_tuple(boardMaps, boardMapsMu);
196}
nlohmann::json json
map< string, map< string, map< string, int > > > boardMaps
tuple< map< string, map< string, map< string, int > > >, map< string, map< string, map< string, string > > > > getBoardMapping(json j)
map< string, map< string, map< string, string > > > boardMapsMu
void to_json(json &_j, const struct quicktype::Info &_x)
void from_json(const json &_j, struct quicktype::Info &_x)
json get_untyped(const json &j, const char *property)
vector< string > slots