Pandora
Pandora source code navigator
Loading...
Searching...
No Matches
AlgorithmManager.cc
Go to the documentation of this file.
1
9#include "Pandora/Algorithm.h"
11
12#include "Persistency/EventReadingAlgorithm.h"
13#include "Persistency/EventWritingAlgorithm.h"
14
16
17#include "Xml/tinyxml.h"
18
19#include <iomanip>
20#include <iostream>
21#include <sstream>
22
23namespace pandora
24{
25
27 m_pPandora(pPandora)
28{
29 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, RegisterAlgorithmFactory("EventReading", new EventReadingAlgorithm::Factory));
30 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, RegisterAlgorithmFactory("EventWriting", new EventWritingAlgorithm::Factory));
31}
32
33//------------------------------------------------------------------------------------------------------------------------------------------
34
36{
37 for (AlgorithmMap::value_type &mapEntry : m_algorithmMap)
38 delete mapEntry.second;
39
40 for (AlgorithmFactoryMap::value_type &mapEntry : m_algorithmFactoryMap)
41 delete mapEntry.second;
42
43 for (AlgorithmTool *const pAlgorithmTool : m_algorithmToolVector)
44 delete pAlgorithmTool;
45
46 for (AlgorithmToolFactoryMap::value_type &mapEntry : m_algorithmToolFactoryMap)
47 delete mapEntry.second;
48
49 m_algorithmMap.clear();
53}
54
55//------------------------------------------------------------------------------------------------------------------------------------------
56
57StatusCode AlgorithmManager::RegisterAlgorithmFactory(const std::string &algorithmType, AlgorithmFactory *const pAlgorithmFactory)
58{
59 if (!m_algorithmFactoryMap.insert(AlgorithmFactoryMap::value_type(algorithmType, pAlgorithmFactory)).second)
60 return STATUS_CODE_ALREADY_PRESENT;
61
62 return STATUS_CODE_SUCCESS;
63}
64
65//------------------------------------------------------------------------------------------------------------------------------------------
66
67StatusCode AlgorithmManager::RegisterAlgorithmToolFactory(const std::string &algorithmToolType, AlgorithmToolFactory *const pAlgorithmToolFactory)
68{
69 if (!m_algorithmToolFactoryMap.insert(AlgorithmToolFactoryMap::value_type(algorithmToolType, pAlgorithmToolFactory)).second)
70 return STATUS_CODE_ALREADY_PRESENT;
71
72 return STATUS_CODE_SUCCESS;
73}
74
75//------------------------------------------------------------------------------------------------------------------------------------------
76
78{
79 for (TiXmlElement *pXmlElement = pXmlHandle->FirstChild("algorithm").Element(); nullptr != pXmlElement;
80 pXmlElement = pXmlElement->NextSiblingElement("algorithm"))
81 {
82 std::string algorithmName;
83 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, CreateAlgorithm(pXmlElement, algorithmName));
84 m_pandoraAlgorithms.push_back(algorithmName);
85 }
86
87 return STATUS_CODE_SUCCESS;
88}
89
90//------------------------------------------------------------------------------------------------------------------------------------------
91
92StatusCode AlgorithmManager::CreateAlgorithm(TiXmlElement *const pXmlElement, std::string &algorithmName)
93{
94 const char *const pAttribute(pXmlElement->Attribute("type"));
95
96 if (!pAttribute)
97 {
98 std::cout << "Algorithm encountered in xml without defined type." << std::endl;
99 return STATUS_CODE_NOT_FOUND;
100 }
101
102 std::string xmlInstanceLabel;
103 const StatusCode instanceStatusCode(FindSpecificAlgorithmInstance(pXmlElement, algorithmName, xmlInstanceLabel));
104
105 if (STATUS_CODE_NOT_FOUND != instanceStatusCode)
106 return instanceStatusCode;
107
108 AlgorithmFactoryMap::const_iterator iter = m_algorithmFactoryMap.find(pXmlElement->Attribute("type"));
109
110 if (m_algorithmFactoryMap.end() == iter)
111 {
112 std::cout << "Algorithm type '" << pXmlElement->Attribute("type") << "' not registered with pandora algorithm manager." << std::endl;
113 return STATUS_CODE_NOT_FOUND;
114 }
115
116 Algorithm *const pLocalAlgorithm = iter->second->CreateAlgorithm();
117
118 try
119 {
120 std::stringstream ss;
121 ss << std::setw(4) << std::setfill('0') << (1 + m_algorithmMap.size());
122 algorithmName = "Alg" + ss.str();
123
124 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithm->RegisterDetails(m_pPandora, iter->first, algorithmName));
125
126 if (!m_algorithmMap.insert(AlgorithmMap::value_type(algorithmName, pLocalAlgorithm)).second)
127 throw StatusCodeException(STATUS_CODE_FAILURE);
128
129 if (!xmlInstanceLabel.empty() && !m_specificAlgorithmInstanceMap.insert(SpecificAlgorithmInstanceMap::value_type(xmlInstanceLabel, algorithmName)).second)
130 {
131 std::cout << "Failure attempting to reuse algorithm with xml instance label " << xmlInstanceLabel << std::endl;
132 throw StatusCodeException(STATUS_CODE_FAILURE);
133 }
134
135 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithm->ReadSettings(TiXmlHandle(pXmlElement)));
136 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithm->Initialize());
137 }
138 catch (StatusCodeException &statusCodeException)
139 {
140 m_algorithmMap.erase(algorithmName);
141 delete pLocalAlgorithm;
142 throw statusCodeException;
143 }
144
145 return STATUS_CODE_SUCCESS;
146}
147
148//------------------------------------------------------------------------------------------------------------------------------------------
149
151{
152 const char *const pAttribute(pXmlElement->Attribute("type"));
153
154 if (!pAttribute)
155 {
156 std::cout << "Algorithm tool encountered in xml without defined type." << std::endl;
157 return STATUS_CODE_NOT_FOUND;
158 }
159
160 AlgorithmToolFactoryMap::const_iterator iter = m_algorithmToolFactoryMap.find(pXmlElement->Attribute("type"));
161
162 if (m_algorithmToolFactoryMap.end() == iter)
163 {
164 std::cout << "Algorithm tool type '" << pXmlElement->Attribute("type") << "' not registered with pandora algorithm manager." << std::endl;
165 return STATUS_CODE_NOT_FOUND;
166 }
167
168 AlgorithmTool *const pLocalAlgorithmTool = iter->second->CreateAlgorithmTool();
169
170 try
171 {
172 std::stringstream ss;
173 ss << std::setw(4) << std::setfill('0') << (1 + m_algorithmToolVector.size());
174 const std::string toolInstanceName("Tool" + ss.str());
175
176 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithmTool->RegisterDetails(m_pPandora, iter->first, toolInstanceName));
177 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithmTool->ReadSettings(TiXmlHandle(pXmlElement)));
178 PANDORA_THROW_RESULT_IF(STATUS_CODE_SUCCESS, !=, pLocalAlgorithmTool->Initialize());
179
180 m_algorithmToolVector.push_back(pLocalAlgorithmTool);
181 pAlgorithmTool = pLocalAlgorithmTool;
182 }
183 catch (StatusCodeException &statusCodeException)
184 {
185 delete pLocalAlgorithmTool;
186 throw statusCodeException;
187 }
188
189 return STATUS_CODE_SUCCESS;
190}
191
192//------------------------------------------------------------------------------------------------------------------------------------------
193
194StatusCode AlgorithmManager::FindSpecificAlgorithmInstance(TiXmlElement *const pXmlElement, std::string &algorithmName, std::string &xmlInstanceLabel) const
195{
196 try
197 {
198 const char *const pAttribute(pXmlElement->Attribute("instance"));
199
200 if (!pAttribute)
201 return STATUS_CODE_NOT_FOUND;
202
203 xmlInstanceLabel = std::string(pAttribute);
204 SpecificAlgorithmInstanceMap::const_iterator iter = m_specificAlgorithmInstanceMap.find(xmlInstanceLabel);
205
206 if (m_specificAlgorithmInstanceMap.end() == iter)
207 return STATUS_CODE_NOT_FOUND;
208
209 algorithmName = iter->second;
210 AlgorithmMap::const_iterator targetIter = m_algorithmMap.find(algorithmName);
211
212 if ((m_algorithmMap.end() == targetIter) || (targetIter->second->GetType() != std::string(pXmlElement->Attribute("type"))))
213 return STATUS_CODE_FAILURE;
214
215 return STATUS_CODE_SUCCESS;
216 }
217 catch (...)
218 {
219 return STATUS_CODE_NOT_FOUND;
220 }
221}
222
223//------------------------------------------------------------------------------------------------------------------------------------------
224
226{
227 for (AlgorithmMap::value_type &mapEntry : m_algorithmMap)
228 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, mapEntry.second->Reset());
229
230 for (AlgorithmTool *const pAlgorithmTool : m_algorithmToolVector)
231 PANDORA_RETURN_RESULT_IF(STATUS_CODE_SUCCESS, !=, pAlgorithmTool->Reset());
232
233 return STATUS_CODE_SUCCESS;
234}
235
236} // namespace pandora
Header file for the algorithm class.
Header file for the algorithm manager class.
Header file for the algorithm tool class.
#define PANDORA_THROW_RESULT_IF(StatusCode1, Operator, Command)
Definition StatusCodes.h:43
#define PANDORA_RETURN_RESULT_IF(StatusCode1, Operator, Command)
Definition StatusCodes.h:19
Factory class for instantiating algorithm.
Factory class for instantiating algorithm.
Factory class for instantiating algorithms.
Definition Algorithm.h:39
Algorithm class. Algorithm addresses are held only by the algorithm manager. They have a fully define...
Definition Algorithm.h:21
StatusCode CreateAlgorithmTool(TiXmlElement *const pXmlElement, AlgorithmTool *&pAlgorithmTool)
Create an algorithm tool, via one of the algorithm tool factories registered with pandora.
StatusCode RegisterAlgorithmToolFactory(const std::string &algorithmToolType, AlgorithmToolFactory *const pAlgorithmToolFactory)
Register an algorithm tool factory.
const Pandora *const m_pPandora
The pandora instance that will run the algorithms.
StatusCode InitializeAlgorithms(const TiXmlHandle *const pXmlHandle)
Initialize algorithms.
AlgorithmToolVector m_algorithmToolVector
The algorithm tool vector.
StatusCode ResetForNextEvent()
Call the reset callback in all managed algorithms and algorithm tools.
StatusCode RegisterAlgorithmFactory(const std::string &algorithmType, AlgorithmFactory *const pAlgorithmFactory)
Register an algorithm factory.
SpecificAlgorithmInstanceMap m_specificAlgorithmInstanceMap
The specific algorithm instance map.
AlgorithmMap m_algorithmMap
The algorithm map.
AlgorithmManager(const Pandora *const pPandora)
Constructor.
StatusCode FindSpecificAlgorithmInstance(TiXmlElement *const pXmlElement, std::string &algorithmName, std::string &xmlInstanceLabel) const
Find the name of a specific algorithm instance, so that it can be re-used.
StatusCode CreateAlgorithm(TiXmlElement *const pXmlElement, std::string &algorithmName)
Create an algorithm, via one of the algorithm factories registered with pandora.
AlgorithmFactoryMap m_algorithmFactoryMap
The algorithm factory map.
AlgorithmToolFactoryMap m_algorithmToolFactoryMap
The algorithm tool factory map.
StringVector m_pandoraAlgorithms
The ordered list of names of top-level algorithms, to be run by pandora.
Factory class for instantiating algorithm tools.
AlgorithmTool class. Algorithm tools will tend to be tailored for specific parent algorithms,...
Pandora class.
Definition Pandora.h:40
virtual StatusCode Initialize()
Perform any operations that must occur after reading settings, but before running the process.
Definition Process.h:126
virtual StatusCode ReadSettings(const TiXmlHandle xmlHandle)=0
Read the algorithm settings.
StatusCode RegisterDetails(const Pandora *const pPandora, const std::string &type, const std::string &instanceName)
Register i) the pandora instance that will run the process and ii) the process type.
Definition Process.h:146
StatusCodeException class.
const char * Attribute(const char *name) const
Definition tinyxml.cc:580
TiXmlElement * Element() const
Definition tinyxml.h:1710
TiXmlHandle FirstChild() const
Return a handle to the first child node.
Definition tinyxml.cc:1635
StatusCode
The StatusCode enum.