otsdaq-mu2e-stm  5.02.01
operation_manager.hh
1 #ifndef OPERATION_MANAGER_hh_
2 #define OPERATION_MANAGER_hh_
3 
4 #include <unordered_map>
5 #include <map>
6 
7 // Async Logger code
8 #include "Mu2e-STMDAQ/utils/async_logger.hh"
9 // Signal handler header
10 #include "Mu2e-STMDAQ/utils/signal_handler.hh"
11 // UDP header
12 #include "Mu2e-STMDAQ/processing/udp.hh"
13 // Check data header
14 #include "Mu2e-STMDAQ/processing/check_data.hh"
15 // Form events header
16 #include "Mu2e-STMDAQ/processing/form_events.hh"
17 // Baseline header
18 #include "Mu2e-STMDAQ/processing/baseline.hh"
19 // Zero suppression header
20 #include "Mu2e-STMDAQ/processing/zero_suppress.hh"
21 // Noise header
22 #include "Mu2e-STMDAQ/processing/noise.hh"
23 // MWD header
24 #include "Mu2e-STMDAQ/processing/mwd.hh"
25 // Pulse height header
26 #include "Mu2e-STMDAQ/processing/pulse_height.hh"
27 // Prescale header
28 #include "Mu2e-STMDAQ/processing/prescale.hh"
29 // Template module header
30 #include "Mu2e-STMDAQ/processing/template_module.hh"
31 // Write manager header
32 #include "Mu2e-STMDAQ/processing/write_manager.hh"
33 // UDP header
34 #include "Mu2e-STMDAQ/processing/tcp.hh"
35 // Write manager header
36 #include "Mu2e-STMDAQ/processing/dqm_manager.hh"
37 // UDP header
38 #include "Mu2e-STMDAQ/debug/test_funcs.hh"
39 // SHM header
40 #include "Mu2e-STMDAQ/utils/shm_manager.hh"
41 
42 // Standard thread functions
44 
45 private:
46 
47  // Store reference to the Config instance
48  Config& cfg;
49 
50  // Async Logger
51  const std::shared_ptr<AsyncLogger>& logger;
52 
53  // STM data info
54  const std::shared_ptr<STMdata>& stm;
55 
56  // Signal Handler
57  const std::shared_ptr<SignalHandler>& signal;
58 
59  // Stores instances of operation classes
60  std::unordered_map<std::string, std::shared_ptr<OperationBase>> classes;
61  // Seleceted operations to use
62  std::vector<std::pair<std::string, op_any>> useOps;
63  // Selected operations flag
64  std::vector<std::pair<std::string,std::string>> useOpsFlag;
65 
66  // Define a struct to hold both the class constructor and operation names
67  struct ClassEntry {
68  std::function<std::shared_ptr<OperationBase>()> constructor;
69  std::vector<std::string> operations;
70  };
71 
72  // Define class map for creating class instances dynamically
73  const std::unordered_map<std::string, ClassEntry> class_map = {
74  {"UDP",
75  { [this]() { return std::make_shared<UDP>(logger, stm, signal, false); },
76  {"receive_data"} }},
77  {"CheckData",
78  { [this]() { return std::make_shared<CheckData>(logger,stm); },
79  {"check_packets"} }},
80  {"FormEvents",
81  { [this]() { return std::make_shared<FormEvents>(logger,stm); },
82  {"get_events"} }},
83  {"Baseline",
84  { [this]() { return std::make_shared<Baseline>(cfg,logger,stm); },
85  {"calc_baseline"} }},
86  {"MWD",
87  { [this]() { return std::make_shared<MWD>(logger, stm);},
88  {"subtract_baseline", "deconv", "diff", "averaging", "find_peaks"} }},
89  {"ZeroSuppress",
90  { [this]() { return std::make_shared<ZeroSuppress>(cfg,logger,stm, signal);},
91  {"prep_data", "find_peaks", "suppress_data"} }},
92  {"PulseHeight",
93  { [this]() { return std::make_shared<PulseHeight>(cfg, logger, stm);},
94  {"detectPulseCandidates", "processPulseCandidates"} }},
95  {"Noise",
96  { [this]() { return std::make_shared<Noise>(cfg, logger, stm);},
97  {"get_noise_data", "check_noise", "noise_fft"} }},
98  {"Prescale",
99  { [this]() { return std::make_shared<Prescale>(logger,stm);},
100  {"prescale_data"} }},
101  {"TemplateClass",
102  { [this]() { return std::make_shared<TemplateClass>(logger, stm);},
103  {"temp_func_1", "temp_func_2"} }},
104  {"WriteManager",
105  { [this]() { return std::make_shared<WriteManager>(logger, stm, signal);},
106  {"write_events", "write_raw", "write_zs", "write_ph"} }},
107  {"TCP",
108  { [this]() { return std::make_shared<TCP>(logger, stm, signal); },
109  {"send_data"} }},
110  {"DQM",
111  { [this]() { return std::make_shared<DQM>(cfg,logger, stm, signal, this); },
112  {"update_dqm"} }},
113  {"TestFuncs",
114  { [this]() { return std::make_shared<TestFuncs>(signal); },
115  {"mod_prev_buffer","read_mod_buffer","check_form_events", "do_nothing"} }}
116  };
117 
118 public:
119 
120  // Constructor to initialize threads
121  explicit OperationManager(Config& cfg_,
122  const std::shared_ptr<AsyncLogger>& logger,
123  const std::shared_ptr<STMdata>& stm_,
124  const std::shared_ptr<SignalHandler>& signal_);
125 
126  // Return the selected subset of operations
127  std::vector<std::pair<std::string, op_any>> getUseOps() const;
128 
129  // Return how many classes have been selected
130  std::size_t class_num() const { return classes.size(); }
131 
132  // Function to check if a class name exists
133  bool isValidClass(std::string_view class_) {
134  for (const auto& [className, map] : class_map) {
135  if (className == class_) return true;
136  }
137  return false;
138  }
139 
140  // Function to check if a operation name exists within class
141  bool isValidOp(std::string_view class_, std::string_view op_) {
142  for (const auto& [className, map] : class_map) {
143  if (className == class_) {
144  for (const auto& op : map.operations) {
145  if (op == op_) {
146  return true;
147  }
148  }
149  }
150  }
151  return false;
152  }
153 
154  // Get the pointer to a class in the operation base
155  std::shared_ptr<OperationBase>& get_class(std::string name){
156  if (classes.find(name) == classes.end()){
157  if (name == "Baseline"){
158  static std::shared_ptr<OperationBase> null_ptr = nullptr;
159  return null_ptr;
160  }
161  else{
162  logger->log("ERROR in operation_manager.hh. Cannot find class named: " + name + ". Exiting...",0);
163  }
164  }
165  return classes[name];
166  }
167 
168  // Check whether a class is in use
169  bool check_class(std::string name){
170  // Find the class name in the name
171  auto it = classes.find(name);
172  // If class is within the map
173  if (it != classes.end()) {
174  // Return true
175  return true;
176  }
177  // Return false
178  return false;
179  }
180 
181  // Destructor for logging
182  ~OperationManager() {
183  //std::cout << "Operation Manager destructor called.\n";
184  if (logger) logger->log("Operation Manager destructor called.",1);
185  }
186 
187 };
188 
189 #endif
Definition: config.hh:19