15 #include "boost/lexical_cast.hpp" 16 #include "boost/tokenizer.hpp" 17 #include "range/v3/algorithm/for_each.hpp" 18 #include "range/v3/view/remove_if.hpp" 19 #include "range/v3/view/reverse.hpp" 20 #include "range/v3/view/transform.hpp" 24 #if RANGE_V3_VERSION < 900 26 using namespace ranges::view;
33 struct AlgorithmRepr {
39 if ( a.parent.name() != typ )
s <<
"/" << a.parent.name();
44 struct DataObjIDSorter {
70 m_algorithms = instantiateAndInitializeAlgorithms( m_producers );
74 ranges::for_each( m_algorithms | ranges::views::transform( [](
const auto& entry ) {
return entry.alg; } ) |
75 ranges::views::remove_if( [](
const auto*
alg ) {
return alg->cardinality() == 0; } ),
77 this->warning() <<
"non-reentrant algorithm: " << AlgorithmRepr{*
alg} <<
endmsg;
82 msg <<
"Available DataProducers: ";
84 msg, m_algorithms,
", ",
85 [](
auto& os,
const AlgEntry& e ) -> decltype(
auto ) {
return os << AlgorithmRepr{*e.
alg}; } );
90 m_dependencies = mapProducers( m_algorithms );
100 for (
AlgEntry& algEntry : m_algorithms ) {
101 ss = algEntry.alg->sysStart();
103 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
108 for (
AlgEntry& algEntry : m_cfnodes ) {
109 ss = algEntry.alg->sysStart();
111 error() <<
"Unable to start Algorithm: " << algEntry.alg->name() <<
endmsg;
123 for (
AlgEntry& algEntry : m_algorithms ) {
124 ss = algEntry.alg->sysStop();
126 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
131 for (
AlgEntry& algEntry : m_cfnodes ) {
132 ss = algEntry.alg->sysStop();
134 error() <<
"Unable to stop Algorithm: " << algEntry.alg->name() <<
endmsg;
143 alg->sysFinalize().ignore( );
145 m_algorithms.clear();
155 auto appMgr = service<IAlgManager>(
"ApplicationMgr" );
163 myIAlg = createAlgorithm( *
appMgr, theType, theName );
171 throw GaudiException{
"Failed to create " + boost::lexical_cast<std::string>( item ), __func__,
178 throw GaudiException{
"Failed to initialize " + boost::lexical_cast<std::string>( item ), __func__,
191 debug() <<
"Data Dependencies for Algorithms:";
192 for (
const auto& entry : m_algorithms ) {
193 debug() <<
"\n " << entry.alg->name() <<
" :";
194 for (
const auto*
id : sortedDataObjIDColl( entry.alg->inputDataObjs() ) ) {
195 debug() <<
"\n o INPUT " <<
id->key();
197 for (
const auto*
id : sortedDataObjIDColl( entry.alg->outputDataObjs() ) ) {
198 debug() <<
"\n o OUTPUT " <<
id->key();
207 const auto&
output =
alg.alg->outputDataObjs();
208 if (
output.empty() ) {
continue; }
209 for (
auto id :
output ) {
210 if (
id.key().find(
":" ) != std::string::npos ) {
211 error() <<
" in Alg " << AlgorithmRepr{*
alg.alg} <<
" alternatives are NOT allowed for outputs! id: " <<
id 217 throw GaudiException(
"multiple algorithms declare " +
id.key() +
" as output (" +
alg.alg->name() +
" and " +
218 producers[id]->alg->name() +
" at least). This is not allowed",
225 for (
auto& algEntry : algorithms ) {
226 auto input = sortedDataObjIDColl( algEntry.alg->inputDataObjs() );
229 if (
id.key().find(
":" ) != std::string::npos ) {
230 warning() << AlgorithmRepr{*( algEntry.alg )} <<
" contains alternatives which require resolution...\n";
231 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.
key(), boost::char_separator<char>{
":"}};
234 if ( itok != tokens.end() ) {
235 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
236 id.updateKey( *itok );
237 warning() <<
"Please update input to not require alternatives, and " 238 "instead properly configure the dataloader" 241 error() <<
"failed to find alternate in global output list" 242 <<
" for id: " <<
id <<
" in Alg " << algEntry.alg <<
endmsg;
245 auto iproducer = producers.
find(
id );
246 if ( iproducer != producers.
end() ) {
247 algEntry.dependsOn.insert( iproducer->second );
250 error_message <<
"\nUnknown requested input by " << AlgorithmRepr{*( algEntry.alg )} <<
" : " <<
id.key()
252 error_message <<
"You can set the OutputLevel of HiveDataBrokerSvc to DEBUG to get a list of inputs and " 253 "outputs of every algorithm.\n";
274 for (
const auto& req : requested ) {
276 if (
id.key().find(
":" ) != std::string::npos ) {
277 warning() << req.
key() <<
" contains alternatives which require resolution...\n";
278 auto tokens = boost::tokenizer<boost::char_separator<char>>{
id.key(), boost::char_separator<char>{
":"}};
280 [&](
DataObjID t ) {
return m_dependencies.find( t ) != m_dependencies.end(); } );
281 if ( itok != tokens.end() ) {
282 warning() <<
"found matching output for " << *itok <<
" -- updating info\n";
283 id.updateKey( *itok );
284 warning() <<
"Please update input to not require alternatives, and " 285 "instead properly configure the dataloader" 288 error() <<
"failed to find alternate in global output list" 289 <<
" for id: " <<
id <<
endmsg;
292 auto i = m_dependencies.find(
id );
293 if ( i == m_dependencies.end() )
299 for (
auto current = deps.
begin(); current != deps.
end(); ++current ) {
301 [current](
auto& stopper ) {
return ( *current )->alg->name() == stopper; } ) ) {
304 for (
auto* entry : ( *current )->dependsOn ) {
326 if (
alg !=
end( m_cfnodes ) &&
alg->alg->type() != requested.
type() ) {
327 error() <<
"requested " << requested <<
" but have matching name with different type: " <<
alg->alg->type()
330 if (
alg ==
end( m_cfnodes ) ) {
331 auto av = instantiateAndInitializeAlgorithms( {requested.
type() +
'/' + requested.
name()} );
332 assert( av.size() == 1 );
333 m_cfnodes.push_back(
std::move( av.front() ) );
336 assert(
alg !=
end( m_cfnodes ) );
337 assert(
alg->alg !=
nullptr );
339 [&requested](
auto& stopper ) {
return requested.
name() == stopper; } ) ==
std::end( stoppers ) ) {
340 result = algorithmsRequiredFor(
alg->alg->inputDataObjs(), stoppers );
344 debug() <<
std::endl <<
"requested " << requested <<
" returning " <<
std::endl <<
" ";
346 debug(), result,
",\n ",
347 [](
auto& os,
const Gaudi::Algorithm* a ) -> decltype(
auto ) {
return os << AlgorithmRepr{*a}; } );
Definition of the MsgStream class used to transmit messages.
StatusCode initialize() override
Define general base for Gaudi exception.
StatusCode finalize() override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
StatusCode start() override
::details::reverse_wrapper< T > reverse(T &&iterable)
The IAlgManager is the interface implemented by the Algorithm Factory in the Application Manager to s...
std::map< DataObjID, AlgEntry * > mapProducers(std::vector< AlgEntry > &algorithms) const
const std::string & type() const
StatusCode finalize() override
StatusCode start() override
std::string fullKey() const
virtual StatusCode sysInitialize()=0
Initialization method invoked by the framework.
StatusCode stop() override
#define DECLARE_COMPONENT(type)
Helper class to parse a string of format "type/name".
const std::string & name() const
This class is used for returning status codes from appropriate routines.
StatusCode stop() override
The IAlgorithm is the interface implemented by the Algorithm base class.
Stream & ostream_joiner(Stream &os, Iterator first, Iterator last, Separator sep, OutputElement output=OutputElement{})
std::vector< Gaudi::Algorithm * > algorithmsRequiredFor(const DataObjIDColl &requested, const std::vector< std::string > &stoppers={}) const override
const std::string & key() const
Base class from which all concrete algorithm classes should be derived.
constexpr static const auto FAILURE
const Gaudi::Algorithm & parent
virtual StatusCode createAlgorithm(std::string algtype, std::string algname, IAlgorithm *&alg, bool managed=false, bool checkIfExists=true)=0
Create an instance of a algorithm type that has been declared beforehand and assigns to it a name.
std::vector< AlgEntry > instantiateAndInitializeAlgorithms(const std::vector< std::string > &names) const
decltype(auto) range(Args &&... args)
Zips multiple containers together to form a single range.
StatusCode initialize() override
AttribStringParser::Iterator begin(const AttribStringParser &parser)
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
const std::string & name() const override
The identifying name of the algorithm object.
std::ostream & operator<<(std::ostream &str, const GaudiAlg::ID &id)
Operator overloading for ostream.
T emplace_back(T... args)