Line data Source code
1 : /**
2 : * @file CTBApplication.cpp
3 : *
4 : * Implementation of CTBApplication's generate_modules dal method
5 : *
6 : * This is part of the DUNE DAQ Software Suite, copyright 2023.
7 : * Licensing/copyright details are in the COPYING file that you should have
8 : * received with this code.
9 : */
10 :
11 : #include "conffwk/Configuration.hpp"
12 : #include "oks/kernel.hpp"
13 : #include "logging/Logging.hpp"
14 :
15 : #include "confmodel/DetectorToDaqConnection.hpp"
16 : #include "confmodel/DetDataSender.hpp"
17 :
18 : #include "ConfigObjectFactory.hpp"
19 : #include "appmodel/appmodelIssues.hpp"
20 : #include "appmodel/CTBApplication.hpp"
21 : #include "appmodel/CTBoardConf.hpp"
22 : #include "appmodel/CTBConf.hpp"
23 : #include "appmodel/CTBModule.hpp"
24 : #include "appmodel/CTBSockets.hpp"
25 : #include "appmodel/CTBTrigger.hpp"
26 : #include "appmodel/CTBMisc.hpp"
27 : #include "appmodel/CTBRandomTrigger.hpp"
28 : #include "appmodel/CTBPulser.hpp"
29 : #include "appmodel/CTBTiming.hpp"
30 : #include "appmodel/CTBHLT.hpp"
31 : #include "appmodel/CTBLLT.hpp"
32 : #include "appmodel/CTBCountLLT.hpp"
33 : #include "appmodel/CTBSubsystem.hpp"
34 : #include "appmodel/CTBCRTSubsystem.hpp"
35 : #include "appmodel/CTBPDSSubsystem.hpp"
36 : #include "appmodel/CTBStatisticsSocket.hpp"
37 : #include "appmodel/CTBSocket.hpp"
38 : #include "appmodel/CTBReceiverSocket.hpp"
39 : #include "appmodel/CTBMonitorSocket.hpp"
40 :
41 :
42 : #include "appmodel/DataHandlerConf.hpp"
43 : #include "appmodel/QueueConnectionRule.hpp"
44 : #include "appmodel/QueueDescriptor.hpp"
45 : #include "appmodel/NetworkConnectionDescriptor.hpp"
46 : #include "appmodel/NetworkConnectionRule.hpp"
47 : #include "appmodel/SourceIDConf.hpp"
48 : #include "appmodel/DataHandlerModule.hpp"
49 :
50 : #include <string>
51 : #include <vector>
52 : #include <bitset>
53 : #include <iostream>
54 : #include <fmt/core.h>
55 : #include <set>
56 :
57 : using namespace dunedaq;
58 : using namespace dunedaq::appmodel;
59 :
60 : std::vector<const confmodel::Resource*>
61 0 : CTBApplication::contained_resources() const {
62 0 : std::vector<const confmodel::Resource*> resources;
63 0 : resources.push_back(dynamic_cast<const confmodel::Resource*>(get_board()));
64 0 : return resources;
65 0 : }
66 :
67 :
68 : void
69 0 : CTBApplication::generate_modules(std::shared_ptr<appmodel::ConfigurationHelper> helper) const
70 : {
71 0 : std::vector<const confmodel::DaqModule*> modules;
72 :
73 0 : ConfigObjectFactory obj_fac(this);
74 :
75 0 : auto dlhConf = get_link_handler();
76 0 : auto dlhClass = dlhConf->get_template_for();
77 :
78 0 : const QueueDescriptor* dlhInputQDesc = nullptr;
79 :
80 0 : for (auto rule : get_queue_rules()) {
81 0 : auto destination_class = rule->get_destination_class();
82 0 : auto data_type = rule->get_descriptor()->get_data_type();
83 0 : if (destination_class == "DataHandlerModule" || destination_class == dlhClass) {
84 0 : dlhInputQDesc = rule->get_descriptor();
85 : }
86 0 : }
87 :
88 0 : const NetworkConnectionDescriptor* dlhReqInputNetDesc = nullptr;
89 0 : const NetworkConnectionDescriptor* tsNetDesc = nullptr;
90 0 : const NetworkConnectionDescriptor* hsiNetDesc = nullptr;
91 :
92 0 : for (auto rule : get_network_rules()) {
93 0 : auto endpoint_class = rule->get_endpoint_class();
94 0 : auto data_type = rule->get_descriptor()->get_data_type();
95 :
96 0 : if (endpoint_class == "DataHandlerModule" || endpoint_class == dlhClass) {
97 0 : if (data_type == "TimeSync") {
98 0 : tsNetDesc = rule->get_descriptor();
99 : }
100 0 : if (data_type == "DataRequest") {
101 0 : dlhReqInputNetDesc = rule->get_descriptor();
102 : }
103 : }
104 0 : if (data_type == "HSIEvent") {
105 0 : hsiNetDesc = rule->get_descriptor();
106 : }
107 0 : }
108 :
109 0 : auto ctb_conf = get_generator();
110 0 : if (ctb_conf ==nullptr) {
111 : throw(BadConf(ERS_HERE, "No CTBModule configuration given"));
112 : }
113 0 : if (dlhInputQDesc == nullptr) {
114 0 : throw(BadConf(ERS_HERE, "No DLH data input queue descriptor given"));
115 : }
116 0 : if (dlhReqInputNetDesc == nullptr) {
117 0 : throw(BadConf(ERS_HERE, "No DLH request input network descriptor given"));
118 : }
119 0 : if (hsiNetDesc == nullptr) {
120 0 : throw(BadConf(ERS_HERE, "No HSIEvent output network descriptor given"));
121 : }
122 :
123 : // Process special Network rules!
124 : // Looking for Fragment rules from DFAppplications in current Session
125 0 : std::vector<conffwk::ConfigObject> fragOutObjs;
126 0 : for (auto [uid, descriptor]:
127 0 : helper->get_netdescriptors("Fragment", "DFApplication")) {
128 0 : fragOutObjs.emplace_back(obj_fac.create_net_obj(descriptor, uid));
129 0 : }
130 :
131 : // start building the list of outputs
132 0 : std::vector<const conffwk::ConfigObject*> fh_output_objs;
133 0 : for (auto& fNet : fragOutObjs) {
134 0 : fh_output_objs.push_back(&fNet);
135 : }
136 :
137 0 : std::vector<conffwk::ConfigObject> ctb_module_outputs;
138 0 : auto sources = get_sources();
139 :
140 0 : for ( const auto & s : sources ) {
141 0 : if (s.second == nullptr) {
142 0 : throw(BadConf(ERS_HERE, "No SourceIDConf given for " + s.first));
143 : }
144 :
145 0 : auto id = s.second->get_sid();
146 : // ----------------------------
147 : // create DLH
148 : // ----------------------------
149 0 : auto det_id = 1; // TODO Eric Flumerfelt <eflumerf@fnal.gov>, 08-Feb-2024: This is a magic number corresponding to kDAQ
150 0 : TLOG() << "creating OKS configuration object for " + s.first + " Data Link Handler class " << dlhClass << ", id " << id;
151 0 : std::string uid("DLH-" + s.first);
152 0 : conffwk::ConfigObject dlhObj = obj_fac.create( dlhClass, uid );
153 0 : dlhObj.set_by_val<uint32_t>("source_id", id);
154 0 : dlhObj.set_by_val<uint32_t>("detector_id", det_id);
155 0 : dlhObj.set_by_val<bool>("post_processing_enabled", false);
156 0 : dlhObj.set_obj("module_configuration", &dlhConf->config_object());
157 :
158 0 : auto net_objc(fh_output_objs);
159 :
160 : // Time Sync network connection
161 0 : if (dlhConf->get_generate_timesync()) {
162 0 : std::string tsStreamUid = tsNetDesc->get_uid_base() + std::to_string(id);
163 0 : conffwk::ConfigObject tsNetObj = obj_fac.create_net_obj(tsNetDesc, tsStreamUid);
164 0 : net_objc.push_back(&tsNetObj);
165 0 : }
166 :
167 0 : dlhObj.set_objs("outputs", net_objc);
168 :
169 : // create Queues from CTB to DLH
170 0 : std::string dataQueueUid(dlhInputQDesc->get_uid_base() + s.first);
171 0 : conffwk::ConfigObject queueObj = obj_fac.create_queue_sid_obj(dlhInputQDesc, id);
172 0 : queueObj.rename(dataQueueUid);
173 :
174 0 : ctb_module_outputs.push_back(queueObj);
175 :
176 : // Create network connections to DLHs
177 0 : conffwk::ConfigObject faNetObj = obj_fac.create_net_obj(dlhReqInputNetDesc, UID() + '_' + s.first);
178 :
179 0 : dlhObj.set_objs("inputs", { &queueObj, &faNetObj });
180 :
181 0 : modules.push_back(obj_fac.get_dal<appmodel::DataHandlerModule>(uid));
182 :
183 0 : } // loop over CTB sources
184 :
185 :
186 0 : conffwk::ConfigObject hsiNetObj = obj_fac.create_net_obj(hsiNetDesc, "");
187 0 : ctb_module_outputs.push_back(hsiNetObj);
188 :
189 0 : auto board = get_board();
190 :
191 0 : conffwk::ConfigObject module_obj = obj_fac.create( "CTBModule", "ctb-module");
192 0 : module_obj.set_obj("configuration", & ctb_conf -> config_object() );
193 0 : module_obj.set_obj("board", & board -> config_object() );
194 :
195 0 : std::vector<const conffwk::ConfigObject*> ctb_module_output_ptrs;
196 0 : for ( const auto & o : ctb_module_outputs ) {
197 0 : ctb_module_output_ptrs.push_back( & o );
198 : }
199 :
200 0 : module_obj.set_objs("outputs", ctb_module_output_ptrs);
201 :
202 0 : auto module = obj_fac.get_dal<appmodel::CTBModule>(module_obj.UID());
203 :
204 0 : modules.push_back(module);
205 :
206 0 : obj_fac.update_modules(modules);
207 0 : }
208 :
209 :
210 :
211 : std::vector<const confmodel::Resource*>
212 0 : CTBoardConf::contained_resources() const {
213 0 : std::vector<const confmodel::Resource*> resources;
214 0 : resources.push_back(get_misc());
215 :
216 0 : auto hlts = get_HLTs();
217 0 : resources.insert(resources.end(), hlts.begin(), hlts.end());
218 :
219 0 : auto crt_llts = get_CRT_LLTs();
220 0 : resources.insert(resources.end(), crt_llts.begin(), crt_llts.end());
221 :
222 0 : auto llts = get_beam_LLTs();
223 0 : resources.insert(resources.end(), llts.begin(), llts.end());
224 :
225 0 : return resources;
226 0 : }
227 :
228 :
229 0 : nlohmann::json CTBoardConf::get_ctb_json(const dunedaq::confmodel::Session& session, std::optional<std::string> socket_host) const {
230 :
231 0 : nlohmann::json json;
232 0 : json["sockets"] = get_sockets() -> get_ctb_json( socket_host );
233 0 : json["misc"] = get_misc()->get_ctb_json(session);
234 :
235 0 : nlohmann::json hlt;
236 :
237 : // constant block that we don't even want to configure
238 0 : auto & mask = hlt["command_mask"];
239 0 : mask["C"]="0x0";
240 0 : mask["D"]="0x0";
241 0 : mask["E"]="0x0";
242 0 : mask["F"]="0x0";
243 :
244 0 : auto hlts = get_HLTs();
245 :
246 0 : std::list<nlohmann::json> json_hlts;
247 0 : for ( const auto & hlt : hlts ) {
248 0 : json_hlts.push_back(hlt->get_ctb_json(session));
249 : }
250 :
251 0 : hlt["trigger"] = nlohmann::json(json_hlts);
252 :
253 0 : json["HLT"] = hlt;
254 :
255 : // --------------------------
256 : // Subsystems
257 : // --------------------------
258 :
259 0 : auto & subsystems = json["subsystems"];
260 :
261 : // ---- Beam ----
262 :
263 0 : auto & beam_block = subsystems["beam"] = get_beam() -> to_json(false, true);
264 0 : std::list<nlohmann::json> json_beam_llts;
265 0 : auto beam_llts = get_beam_LLTs();
266 0 : for ( const auto & llt : beam_llts ) {
267 0 : json_beam_llts.push_back(llt->get_ctb_json(session));
268 : }
269 :
270 0 : beam_block["triggers"] = nlohmann::json(json_beam_llts);
271 :
272 : // ---- CRT ----
273 :
274 0 : auto & crt_block = subsystems["crt"] = get_CRT() -> to_json(false, true);
275 0 : std::list<nlohmann::json> json_crt_llts;
276 0 : auto crt_llts = get_CRT_LLTs();
277 0 : for ( const auto & llt : crt_llts ) {
278 0 : json_crt_llts.push_back(llt->get_ctb_json(session));
279 : }
280 0 : crt_block["triggers"] = nlohmann::json(json_crt_llts);
281 :
282 : // ---- PDS ----
283 :
284 0 : subsystems["pds"] = get_pds() -> to_json(false, true);
285 :
286 0 : nlohmann::json ret;
287 0 : ret["ctb"] = json;
288 :
289 0 : return ret;
290 :
291 0 : }
292 :
293 0 : std::vector<const confmodel::Resource*> CTBMisc::contained_resources() const {
294 0 : return std::vector<const confmodel::Resource*>{ get_randomtrigger_1(), get_randomtrigger_2() };
295 : }
296 :
297 :
298 :
299 0 : nlohmann::json CTBMisc::get_ctb_json(const dunedaq::confmodel::Session& session) const {
300 :
301 0 : nlohmann::json ret;
302 0 : ret["randomtrigger_1"] = get_randomtrigger_1()->get_ctb_json(session);
303 0 : ret["randomtrigger_2"] = get_randomtrigger_2()->get_ctb_json(session);
304 0 : ret["pulser"] = get_pulser() -> to_json(false, true);
305 0 : ret["timing"] = get_timing() -> to_json(false, true);
306 :
307 0 : static std::string ch_status_flag = "ch_status";
308 0 : if ( get_ch_status() ) ret[ch_status_flag] = true;
309 0 : else ret[ch_status_flag] = false;
310 :
311 0 : static std::string standalong_flag = "standalone_enable";
312 0 : ret[standalong_flag] = false;
313 :
314 0 : return ret;
315 :
316 0 : }
317 :
318 :
319 0 : nlohmann::json CTBTrigger::get_ctb_json(const dunedaq::confmodel::Session& session) const {
320 :
321 0 : auto json = this -> to_json(false, true);
322 0 : static std::string enable_tag = "enable";
323 0 : if ( this -> is_disabled(session) ) {
324 0 : json[enable_tag] = false;
325 : }
326 : else {
327 0 : json[enable_tag] = true;
328 : }
329 :
330 0 : json["id"] = this -> UID();
331 :
332 0 : return json;
333 :
334 0 : }
335 :
336 0 : nlohmann::json CTBSockets::get_ctb_json(std::optional<std::string> socket_host) const {
337 :
338 0 : nlohmann::json json;
339 0 : json["receiver"] = get_receiver() -> get_ctb_json(socket_host);
340 0 : json["monitor"] = get_monitor() -> get_ctb_json(socket_host);
341 0 : json["statistics"] = get_statistics() -> to_json(false, true);
342 0 : return json;
343 :
344 0 : }
345 :
346 0 : nlohmann::json CTBSocket::get_ctb_json(std::optional<std::string> socket_host) const {
347 :
348 0 : auto json = to_json(false, true);
349 0 : if ( socket_host ) {
350 0 : json["host"] = socket_host.value();
351 : }
352 0 : return json;
353 :
354 0 : }
355 :
356 :
357 :
|