tdaq-develop-2025-02-12
VisualDataManager.cc
1 #include "otsdaq/RootUtilities/VisualDataManager.h"
2 #include "otsdaq/ConfigurationInterface/ConfigurationManager.h"
3 #include "otsdaq/DataManager/DQMHistosConsumerBase.h"
4 #include "otsdaq/DataManager/DataManager.h"
5 #include "otsdaq/DataManager/DataProcessor.h"
6 #include "otsdaq/Macros/CoutMacros.h"
7 
8 #include <TFile.h>
9 #include <cassert>
10 #include <chrono> // std::chrono::seconds
11 #include <iostream>
12 #include <sstream>
13 #include <thread> // std::this_thread::sleep_for
14 
15 using namespace ots;
16 #undef __MF_SUBJECT__
17 #define __MF_SUBJECT__ "VisualDataManager"
18 #define mfSubject_ (std::string("VisualDataManager"))
19 
20 //==============================================================================
22  const std::string& supervisorConfigurationPath)
23  : DataManager(theXDAQContextConfigTree, supervisorConfigurationPath)
24  , doNotStop_(false)
25  , ready_(false)
26  , theLiveDQMHistos_(false)
30 {
31 }
32 
33 //==============================================================================
34 VisualDataManager::~VisualDataManager(void) {}
35 
36 //==============================================================================
38 {
39  fileMap_.clear();
40  theLiveDQMs_.clear();
42  auto buffers = theXDAQContextConfigTree_
43  .getNode(theConfigurationPath_ + "/LinkToDataBufferTable")
44  .getChildren();
45 
46  __CFG_COUT__ << "Buffer count " << buffers.size() << __E__;
47 
48  for(const auto& buffer : buffers)
49  {
50  __CFG_COUT__ << "Data Buffer Name: " << buffer.first << std::endl;
51  if(buffer.second.getNode(TableViewColumnInfo::COL_NAME_STATUS).getValue<bool>())
52  {
53  std::vector<std::string> producers;
54  std::vector<std::string> consumers;
55  auto bufferConfigurationMap =
56  buffer.second.getNode("LinkToDataProcessorTable").getChildren();
57  for(const auto& bufferConfiguration : bufferConfigurationMap)
58  {
59  __CFG_COUT__ << "Processor id: " << bufferConfiguration.first
60  << std::endl;
61  if(bufferConfiguration.second
62  .getNode(TableViewColumnInfo::COL_NAME_STATUS)
63  .getValue<bool>() &&
64  (bufferConfiguration.second.getNode("ProcessorType")
65  .getValue<std::string>() == "Consumer"))
66  {
67  __CFG_COUT__
68  << "Consumer Plugin Type = "
69  << bufferConfiguration.second.getNode("ProcessorPluginName")
70  << __E__;
71 
72  auto bufferIt = buffers_.at(buffer.first);
73  for(const auto& consumer : bufferIt.consumers_)
74  {
75  __CFG_COUT__
76  << "CONSUMER PROCESSOR: " << consumer->getProcessorID()
77  << std::endl;
78  if(consumer->getProcessorID() ==
79  bufferConfiguration.second.getNode("ProcessorUID")
80  .getValue<std::string>())
81  {
82  __CFG_COUT__ << "CONSUMER: " << consumer->getProcessorID()
83  << std::endl;
84 
85  try
86  {
87  __CFG_COUT__ << "Trying for DQMHistosConsumerBase."
88  << __E__;
89  // theLiveDQMHistos_ =
90  // dynamic_cast<DQMHistosConsumerBase*>(consumer);
91  theLiveDQMs_.emplace_back(
92  dynamic_cast<DQMHistosBase*>(consumer));
93  dynamic_cast<DQMHistosBase*>(consumer)->setDataManager(
94  this);
95 
96  // __CFG_COUT__ << "Did we succeed? " << theLiveDQMHistos_
97  // << __E__;
98  }
99  catch(...)
100  {
101  } // ignore failures
102 
103  if(theLiveDQMs_.size() == 0)
104  {
105  __CFG_SS__
106  << "There are no configured visualizer consumer! "
107  "There must be at least one consumer if you want "
108  "to use the visualizer."
109  << __E__;
110  __CFG_SS_THROW__;
111  }
112  }
113  }
114  }
115  }
116  }
117  }
118 }
119 
120 //==============================================================================
121 void VisualDataManager::halt(void)
122 {
123  ready_ = false;
124  theLiveDQMHistos_ = false;
125  while(doNotStop_) {};
126  DataManager::halt();
127 }
128 
129 //==============================================================================
130 void VisualDataManager::pause(void)
131 {
132  __CFG_COUT__ << "Pausing..." << std::endl;
133  DataManager::pause();
134 }
135 
136 //==============================================================================
137 void VisualDataManager::resume(void) { DataManager::resume(); }
138 
139 //==============================================================================
140 void VisualDataManager::start(std::string runNumber)
141 {
142  __CFG_COUT__ << "Start!" << __E__;
143 
144  ready_ = false;
145 
146  DataManager::start(runNumber);
147  theLiveDQMHistos_ = true;
148 
149  ready_ = true;
150 }
151 
152 //==============================================================================
153 void VisualDataManager::stop(void)
154 {
155  ready_ = false;
156  theLiveDQMHistos_ = false;
157  while(doNotStop_) {};
158  DataManager::stop();
159 }
160 
161 //==============================================================================
162 void VisualDataManager::load(std::string fileName, std::string type)
163 {
164  if(type == "Histograms")
165  theFileDQMHistos_.load(fileName);
166  // else if(type == "Monicelli")
167  // theMonicelliEventAnalyzer_.load(fileName);
168  // else if(type == "Geometry")
169  // theMonicelliGeometryConverter_.loadGeometry(fileName);
170 }
171 
172 //==============================================================================
173 bool VisualDataManager::getLiveDQMHistos(void) { return theLiveDQMHistos_; }
174 
175 //==============================================================================
176 DQMHistosBase& VisualDataManager::getFileDQMHistos(void) { return theFileDQMHistos_; }
177 
178 //==============================================================================
179 TFile* VisualDataManager::openFile(std::string fileName)
180 {
181  auto tmpFile = fileMap_.find(fileName);
182  if(tmpFile == fileMap_.end() || !tmpFile->second->IsOpen())
183  fileMap_[fileName] = TFile::Open(fileName.c_str(), "RECREATE");
184 
185  if(fileMap_[fileName] == nullptr || !fileMap_[fileName]->IsOpen())
186  {
187  __GEN_SS__ << "Can't open file: " << fileName
188  << ". It is likely that the directory where the file should be stored "
189  "does not exist or cannot be written."
190  << __E__;
191  __GEN_COUT_ERR__ << "\n" << ss.str();
192  __GEN_SS_THROW__;
193  }
194  return fileMap_[fileName];
195 }
196 
199 //{
200 // return theMonicelliEventAnalyzer_.getEvents();
201 //}
202 //
205 //{
206 // return theMonicelliGeometryConverter_.getGeometry();
207 //}
ConfigurationTree getNode(const std::string &nodeName, bool doNotThrowOnBrokenUIDLinks=false) const
navigating between nodes
std::vector< std::pair< std::string, ConfigurationTree > > getChildren(std::map< std::string, std::string > filterMap=std::map< std::string, std::string >(), bool byPriority=false, bool onlyStatusTrue=false) const
virtual void configure(void)
State Machine Methods.
Definition: DataManager.cc:65
DQMHistosBase & getFileDQMHistos(void)
TO BE DELETED.
void configure(void) override
State Machine Methods.
VisualDataManager(const ConfigurationTree &theXDAQContextConfigTree, const std::string &supervisorConfigurationPath)