otsdaq  v2_05_02_indev
ARTDAQEventBuilderTable_table.cc
1 #include "otsdaq/Macros/TablePluginMacros.h"
2 #include "otsdaq/TablePlugins/ARTDAQEventBuilderTable.h"
3 #include "otsdaq/TablePlugins/XDAQContextTable.h"
4 
5 using namespace ots;
6 
7 // clang-format off
8 
9 #define SLOWCONTROL_PV_FILE_PATH \
10  std::string( \
11  getenv("OTSDAQ_EPICS_DATA")? \
12  (std::string(getenv("OTSDAQ_EPICS_DATA")) + "/" + __ENV__("MU2E_OWNER") + "_otsdaq_artdaqEventBuilder-ai.dbg"): \
13  (EPICS_CONFIG_PATH + "/_otsdaq_artdaqEventBuilder-ai.dbg") )
14 
15 // clang-format on
16 
17 //==============================================================================
18 ARTDAQEventBuilderTable::ARTDAQEventBuilderTable(void)
19  : TableBase("ARTDAQEventBuilderTable")
20  , ARTDAQTableBase("ARTDAQEventBuilderTable")
21  , SlowControlsTableBase("ARTDAQEventBuilderTable")
22 {
24  // WARNING: the names used in C++ MUST match the Table INFO //
26  __COUT__ << "ARTDAQEventBuilderTable Constructed." << __E__;
27 } //end constructor()
28 
29 //==============================================================================
30 ARTDAQEventBuilderTable::~ARTDAQEventBuilderTable(void) {}
31 
32 //==============================================================================
33 void ARTDAQEventBuilderTable::init(ConfigurationManager* configManager)
34 {
35  lastConfigManager_ = configManager;
36 
37  // use isFirstAppInContext to only run once per context, for example to avoid
38  // generating files on local disk multiple times.
39  isFirstAppInContext_ = configManager->isOwnerFirstAppInContext();
40 
41  //__COUTV__(isFirstAppInContext);
42  if(!isFirstAppInContext_)
43  return;
44 
45  // make directory just in case
46  mkdir((ARTDAQTableBase::ARTDAQ_FCL_PATH).c_str(), 0755);
47 
48  // __COUT__ << "*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*&*" << __E__;
49  // __COUT__ << configManager->__SELF_NODE__ << __E__;
50 
51  // handle fcl file generation, wherever the level of this table
52 
53  auto buiders = lastConfigManager_->getNode(ARTDAQTableBase::getTableName()).getChildren(
54  /*default filterMap*/ std::map<std::string /*relative-path*/, std::string /*value*/>(),
55  /*default byPriority*/ false,
56  /*TRUE! onlyStatusTrue*/ true);
57 
58  for(auto& builder : buiders)
59  {
60  ARTDAQTableBase::outputDataReceiverFHICL(builder.second, ARTDAQTableBase::ARTDAQAppType::EventBuilder);
61  ARTDAQTableBase::flattenFHICL(ARTDAQAppType::EventBuilder, builder.second.getValue());
62  }
63 } // end init()
64 
65 //==============================================================================
66 unsigned int ARTDAQEventBuilderTable::slowControlsHandlerConfig(std::stringstream& out,
67  ConfigurationManager* configManager,
68  std::vector<std::pair<std::string /*channelName*/, std::vector<std::string>>>* channelList /*= 0*/
69 ) const
70 {
72  // generate xdaq run parameter file
73 
74  std::string tabStr = "";
75  std::string commentStr = "";
76 
77  // loop through ARTDAQ EventBuilder records starting at ARTDAQSupervisorTable
78  std::vector<std::pair<std::string, ConfigurationTree>> artdaqRecords = configManager->getNode("ARTDAQSupervisorTable").getChildren();
79 
80  unsigned int numberOfEventBuilders = 0;
81  unsigned int numberOfEventBuiderMetricParameters = 0;
82 
83  for(auto& artdaqPair : artdaqRecords) // start main artdaq record loop
84  {
85  if(artdaqPair.second.getNode(colARTDAQSupervisor_.colLinkToEventBuilders_).isDisconnected())
86  continue;
87 
88  std::vector<std::pair<std::string, ConfigurationTree>> eventBuilderRecords =
89  artdaqPair.second.getNode(colARTDAQSupervisor_.colLinkToEventBuilders_).getChildren();
90 
91  for(auto& eventBuilderPair : eventBuilderRecords) // start main eventBuilder record loop
92  {
93  if(!eventBuilderPair.second.status())
94  continue;
95 
96  numberOfEventBuilders++;
97 
98  try
99  {
100  if(eventBuilderPair.second.getNode("daqLink").isDisconnected())
101  continue;
102 
103  auto daqLink = eventBuilderPair.second.getNode("daqLink");
104 
105  if(daqLink.getNode("daqMetricsLink").isDisconnected())
106  continue;
107 
108  auto daqMetricsLinks = daqLink.getNode("daqMetricsLink").getChildren();
109  for(auto& daqMetricsLink : daqMetricsLinks) // start daqMetricsLinks record loop
110  {
111  if(!daqMetricsLink.second.status())
112  continue;
113 
114  if(daqMetricsLink.second.getNode("metricParametersLink").isDisconnected())
115  continue;
116 
117  //ConfigurationTree slowControlsLink = configManager->getNode("ARTDAQMetricAlarmThresholdsTable");
118  ConfigurationTree slowControlsLink = eventBuilderPair.second.getNode("MetricAlarmThresholdsLink");
119 
120  auto metricParametersLinks = daqMetricsLink.second.getNode("metricParametersLink").getChildren();
121  for(auto& metricParametersLink : metricParametersLinks) // start daq MetricParametersLinks record loop
122  {
123  if(!metricParametersLink.second.status())
124  continue;
125 
126  std::string subsystem = metricParametersLink.second.getNode("metricParameterValue")
127  .getValueWithDefault<std::string>(std::string("TDAQ_") + __ENV__("MU2E_OWNER"));
128  if(subsystem.find("Mu2e:") != std::string::npos)
129  subsystem = subsystem.replace(subsystem.find("Mu2e:"), 5, "");
130  while(subsystem.find("\"") != std::string::npos)
131  subsystem = subsystem.replace(subsystem.find("\""), 1, "");
132 
133  numberOfEventBuiderMetricParameters =
134  slowControlsHandler(out, tabStr, commentStr, subsystem, eventBuilderPair.first, slowControlsLink, channelList);
135 
136  __COUT__ << "EventBuilder '" << eventBuilderPair.first
137  << "' number of metrics for slow controls: " << numberOfEventBuiderMetricParameters << __E__;
138  }
139  }
140  }
141  catch(const std::runtime_error& e)
142  {
143  __COUT_ERR__ << "Ignoring EventBuilder error: " << e.what() << __E__;
144  }
145  }
146  }
147 
148  return numberOfEventBuiderMetricParameters;
149 } // end slowControlsHandlerConfig()
150 
151 //==============================================================================
152 // return out file path
153 std::string ARTDAQEventBuilderTable::setFilePath() const { return SLOWCONTROL_PV_FILE_PATH; }
154 
155 DEFINE_OTS_TABLE(ARTDAQEventBuilderTable)