00001 #include "otsdaq-core/ConfigurationPluginDataFormats/ARTDAQBoardReaderConfiguration.h"
00002 #include "otsdaq-core/Macros/ConfigurationPluginMacros.h"
00003 #include "otsdaq-core/ConfigurationInterface/ConfigurationManager.h"
00004 #include "otsdaq-core/ConfigurationPluginDataFormats/XDAQContextConfiguration.h"
00005
00006 #include <iostream>
00007 #include <fstream>
00008 #include <stdio.h>
00009 #include <sys/stat.h>
00010
00011 using namespace ots;
00012
00013
00014 #define ARTDAQ_FCL_PATH std::string(getenv("USER_DATA")) + "/"+ "ARTDAQConfigurations/"
00015 #define ARTDAQ_FILE_PREAMBLE "boardReader"
00016
00017
00018 #define OUT out << tabStr << commentStr
00019 #define PUSHTAB tabStr += "\t"
00020 #define POPTAB tabStr.resize(tabStr.size()-1)
00021 #define PUSHCOMMENT commentStr += "# "
00022 #define POPCOMMENT commentStr.resize(commentStr.size()-2)
00023
00024
00025
00026 ARTDAQBoardReaderConfiguration::ARTDAQBoardReaderConfiguration(void)
00027 : ConfigurationBase("ARTDAQBoardReaderConfiguration")
00028 {
00030
00032
00033 }
00034
00035
00036 ARTDAQBoardReaderConfiguration::~ARTDAQBoardReaderConfiguration(void)
00037 {}
00038
00039
00040 void ARTDAQBoardReaderConfiguration::init(ConfigurationManager* configManager)
00041 {
00042
00043 mkdir((ARTDAQ_FCL_PATH).c_str(), 0755);
00044
00045 __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << std::endl;
00046 __COUT__ << configManager->__SELF_NODE__ << std::endl;
00047
00048 const XDAQContextConfiguration *contextConfig = configManager->__GET_CONFIG__(XDAQContextConfiguration);
00049
00050 std::vector<const XDAQContextConfiguration::XDAQContext *> readerContexts =
00051 contextConfig->getBoardReaderContexts();
00052
00053
00054
00055
00056
00057 for (auto &readerContext : readerContexts)
00058 {
00059 try
00060 {
00061 ConfigurationTree readerConfigNode = contextConfig->getSupervisorConfigNode(configManager,
00062 readerContext->contextUID_, readerContext->applications_[0].applicationUID_);
00063
00064 __COUT__ << "Path for this reader config is " <<
00065 readerContext->contextUID_ << "/" <<
00066 readerContext->applications_[0].applicationUID_ << "/" <<
00067 readerConfigNode.getValueAsString() <<
00068 std::endl;
00069
00070 __COUT__ << "Checking that this reader supervisor node is DataManager-like." << std::endl;
00071
00072 readerConfigNode.getNode("LinkToDataManagerConfiguration").getChildren();
00073
00074 }
00075 catch (const std::runtime_error& e)
00076 {
00077 __SS__ << "artdaq Board Readers must be instantiated as a Consumer within a DataManager configuration. Error found while checking for LinkToDataManagerConfiguration: " <<
00078 e.what() << std::endl;
00079 __COUT_ERR__ << ss.str();
00080 __COUT__ << "Path for this reader config is " <<
00081 readerContext->contextUID_ << "/" <<
00082 readerContext->applications_[0].applicationUID_ << "/X" << std::endl;
00083 __COUT_ERR__ << "This board reader will likely not get instantiated properly! Proceeding anyway with fcl generation." << std::endl;
00084
00085
00086
00087 }
00088
00089
00090
00091
00092
00093 }
00094
00095
00096
00097 auto childrenMap = configManager->__SELF_NODE__.getChildren();
00098 std::string appUID, buffUID, consumerUID;
00099 for (auto &child : childrenMap)
00100 {
00101 const XDAQContextConfiguration::XDAQContext * thisContext = nullptr;
00102 for (auto& readerContext : readerContexts) {
00103 ConfigurationTree readerConfigNode = contextConfig->getSupervisorConfigNode(configManager,
00104 readerContext->contextUID_, readerContext->applications_[0].applicationUID_);
00105 auto dataManagerConfMap = readerConfigNode.getNode("LinkToDataManagerConfiguration").getChildren();
00106 for (auto dmc : dataManagerConfMap) {
00107 auto dataBufferConfMap = dmc.second.getNode("LinkToDataBufferConfiguration").getChildren();
00108 for (auto dbc : dataBufferConfMap) {
00109 auto processorConfUID = dbc.second.getNode("LinkToProcessorUID").getUIDAsString();
00110 if (processorConfUID == child.second.getValue()) {
00111 __COUT__ << "Found match for context UID: " << readerContext->contextUID_ << std::endl;
00112 thisContext = readerContext;
00113 }
00114 }
00115 }
00116 }
00117
00118 if (thisContext == nullptr) {
00119 __COUT_ERR__ << "Could not find matching context for this configuration!" << std::endl;
00120 }
00121 else {
00122 if (child.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00123 {
00124 outputFHICL(configManager, child.second,
00125 contextConfig->getARTDAQAppRank(thisContext->contextUID_),
00126 contextConfig->getContextAddress(thisContext->contextUID_),
00127 contextConfig->getARTDAQDataPort(configManager,thisContext->contextUID_),
00128 contextConfig);
00129 }
00130 }
00131 }
00132 }
00133
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 std::string ARTDAQBoardReaderConfiguration::getFHICLFilename(const ConfigurationTree &boardReaderNode)
00145 {
00146 __COUT__ << "ARTDAQ BoardReader UID: " << boardReaderNode.getValue() << std::endl;
00147 std::string filename = ARTDAQ_FCL_PATH + ARTDAQ_FILE_PREAMBLE + "-";
00148 std::string uid = boardReaderNode.getValue();
00149 for (unsigned int i = 0; i < uid.size(); ++i)
00150 if ((uid[i] >= 'a' && uid[i] <= 'z') ||
00151 (uid[i] >= 'A' && uid[i] <= 'Z') ||
00152 (uid[i] >= '0' && uid[i] <= '9'))
00153 filename += uid[i];
00154
00155 filename += ".fcl";
00156
00157 __COUT__ << "fcl: " << filename << std::endl;
00158
00159 return filename;
00160 }
00161
00162
00163 void ARTDAQBoardReaderConfiguration::outputFHICL(ConfigurationManager* configManager,
00164 const ConfigurationTree &boardReaderNode, unsigned int selfRank, std::string selfHost, unsigned int selfPort,
00165 const XDAQContextConfiguration *contextConfig)
00166 {
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260 std::string filename = getFHICLFilename(boardReaderNode);
00261
00263
00264 std::fstream out;
00265
00266 std::string tabStr = "";
00267 std::string commentStr = "";
00268
00269 out.open(filename, std::fstream::out | std::fstream::trunc);
00270 if (out.fail())
00271 {
00272 __SS__ << "Failed to open ARTDAQ Builder fcl file: " << filename << std::endl;
00273 throw std::runtime_error(ss.str());
00274 }
00275
00276
00277 try
00278 {
00279 if (boardReaderNode.isDisconnected())
00280 {
00281
00282 OUT << "{}\n\n";
00283 out.close();
00284 return;
00285 }
00286 }
00287 catch (const std::runtime_error)
00288 {
00289 __COUT__ << "Ignoring error, assume this a valid UID node." << std::endl;
00290
00291
00292 }
00293
00294
00295
00296 OUT << "daq: {\n";
00297
00298
00299 PUSHTAB;
00300 OUT << "fragment_receiver: {\n";
00301
00302 PUSHTAB;
00303 {
00304
00305 OUT << "generator" <<
00306 ": " <<
00307 boardReaderNode.getNode("daqGeneratorPluginType").getValue() <<
00308 ("\t #daq generator plug-in type") <<
00309 "\n";
00310 OUT << "fragment_type" <<
00311 ": " <<
00312 boardReaderNode.getNode("daqGeneratorFragmentType").getValue() <<
00313 ("\t #generator data fragment type") <<
00314 "\n\n";
00315
00316
00317 auto parametersLink = boardReaderNode.getNode("daqParametersLink");
00318 if (!parametersLink.isDisconnected())
00319 {
00320
00321 auto parameters = parametersLink.getChildren();
00322 for (auto ¶meter : parameters)
00323 {
00324 if (!parameter.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00325 PUSHCOMMENT;
00326
00327
00328
00329
00330
00331
00332
00333 auto comment = parameter.second.getNode("CommentDescription");
00334 OUT << parameter.second.getNode("daqParameterKey").getValue() <<
00335 ": " <<
00336 parameter.second.getNode("daqParameterValue").getValue()
00337 <<
00338 (comment.isDefaultValue() ? "" : ("\t # " + comment.getValue())) <<
00339 "\n";
00340
00341 if (!parameter.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00342 POPCOMMENT;
00343 }
00344 }
00345 OUT << "\n";
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373 OUT << "destinations: {\n";
00374
00375 PUSHTAB;
00376 auto destinationsGroup = boardReaderNode.getNode("daqDestinationsLink");
00377 if (!destinationsGroup.isDisconnected())
00378 {
00379 try
00380 {
00381 auto destinations = destinationsGroup.getChildren();
00382 for (auto &destination : destinations)
00383 {
00384 auto destinationContextUID = destination.second.getNode("destinationARTDAQContextLink").getValueAsString();
00385
00386 unsigned int destinationRank = contextConfig->getARTDAQAppRank(destinationContextUID);
00387 std::string host = contextConfig->getContextAddress(destinationContextUID);
00388 unsigned int port = contextConfig->getARTDAQDataPort(configManager,destinationContextUID);
00389
00390 OUT << destination.second.getNode("destinationKey").getValue() <<
00391 ": {" <<
00392 " transferPluginType: " <<
00393 destination.second.getNode("transferPluginType").getValue() <<
00394 " destination_rank: " <<
00395 destinationRank <<
00396 " max_fragment_size_words: " <<
00397 destination.second.getNode("ARTDAQGlobalConfigurationLink/maxFragmentSizeWords").getValue<unsigned int>() <<
00398 " host_map: [{rank: " << destinationRank << " host: \"" << host << "\" portOffset: " << std::to_string(port) << "}, " <<
00399 "{rank: " << selfRank << " host: \"" << selfHost << "\" portOffset: " << std::to_string(selfPort) << "}]" <<
00400 "}\n";
00401 }
00402 }
00403 catch (const std::runtime_error& e)
00404 {
00405 __SS__ << "Are the DAQ destinations valid? Error occurred looking for Board Reader DAQ sources for UID '" <<
00406 boardReaderNode.getValue() << "': " << e.what() << std::endl;
00407 __COUT_ERR__ << ss.str() << std::endl;
00408 throw std::runtime_error(ss.str());
00409 }
00410 }
00411 POPTAB;
00412 OUT << "}\n\n";
00413
00414 POPTAB;
00415 OUT << "}\n\n";
00416
00417
00418 OUT << "metrics: {\n";
00419
00420 PUSHTAB;
00421 auto metricsGroup = boardReaderNode.getNode("daqMetricsLink");
00422 if (!metricsGroup.isDisconnected())
00423 {
00424 auto metrics = metricsGroup.getChildren();
00425
00426 for (auto &metric : metrics)
00427 {
00428 if (!metric.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00429 PUSHCOMMENT;
00430
00431 OUT << metric.second.getNode("metricKey").getValue() <<
00432 ": {\n";
00433 PUSHTAB;
00434
00435 OUT << "metricPluginType: " <<
00436 metric.second.getNode("metricPluginType").getValue()
00437 << "\n";
00438 OUT << "level: " <<
00439 metric.second.getNode("metricLevel").getValue()
00440 << "\n";
00441
00442 auto metricParametersGroup = metric.second.getNode("metricParametersLink");
00443 if (!metricParametersGroup.isDisconnected())
00444 {
00445 auto metricParameters = metricParametersGroup.getChildren();
00446 for (auto &metricParameter : metricParameters)
00447 {
00448 if (!metricParameter.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00449 PUSHCOMMENT;
00450
00451 OUT << metricParameter.second.getNode("metricParameterKey").getValue() <<
00452 ": " <<
00453 metricParameter.second.getNode("metricParameterValue").getValue()
00454 << "\n";
00455
00456 if (!metricParameter.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00457 POPCOMMENT;
00458
00459 }
00460 }
00461 POPTAB;
00462 OUT << "}\n\n";
00463
00464 if (!metric.second.getNode(ViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
00465 POPCOMMENT;
00466 }
00467 }
00468 POPTAB;
00469 OUT << "}\n\n";
00470
00471 POPTAB;
00472 OUT << "}\n\n";
00473
00474
00475 out.close();
00476 }
00477
00478 DEFINE_OTS_CONFIGURATION(ARTDAQBoardReaderConfiguration)