The Gaudi Framework  master (37c0b60a)
Transformer.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 CERN for the benefit of the LHCb and ATLAS collaborations *
3 * *
4 * This software is distributed under the terms of the Apache version 2 licence, *
5 * copied verbatim in the file "LICENSE". *
6 * *
7 * In applying this licence, CERN does not waive the privileges and immunities *
8 * granted to it by virtue of its status as an Intergovernmental Organization *
9 * or submit itself to any jurisdiction. *
10 \***********************************************************************************/
11 #pragma once
12 
13 #include "details.h"
14 #include "utilities.h"
17 #include <tuple>
18 #include <utility>
19 
20 // Adapt an Algorithm (by default, Gaudi::Algorithm) so that derived classes
21 // a) do not need to access the event store, and have to explicitly
22 // state their data dependencies
23 // b) are encouraged not to have state which depends on the events
24 // (eg. histograms, counters will have to be mutable)
25 
26 namespace Gaudi ::Functional {
27 
28  namespace details {
29 
30  template <typename Signature, typename Traits_, bool isLegacy>
31  struct Transformer;
32 
33  // general N -> 1 algorithms
34  template <typename Out, typename... In, typename Traits_>
35  struct Transformer<Out( const In&... ), Traits_, true>
36  : DataHandleMixin<std::tuple<Out>, filter_evtcontext<In...>, Traits_> {
37  using DataHandleMixin<std::tuple<Out>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
38 
39  // derived classes can NOT implement execute
40  StatusCode execute() override final {
41  try {
42  if constexpr ( sizeof...( In ) == 0 ) {
43  put( std::get<0>( this->m_outputs ), ( *this )() );
44  } else if constexpr ( std::tuple_size_v<filter_evtcontext<In...>> == 0 ) {
45  put( std::get<0>( this->m_outputs ), ( *this )( Gaudi::Hive::currentContext() ) );
46  } else {
47  put( std::get<0>( this->m_outputs ), filter_evtcontext_t<In...>::apply( *this, this->m_inputs ) );
48  }
49  return FilterDecision::PASSED;
50  } catch ( GaudiException& e ) {
51  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
52  return e.code();
53  }
54  }
55 
56  // instead they MUST implement this operator
57  virtual Out operator()( const In&... ) const = 0;
58  };
59 
60  template <typename Out, typename... In, typename Traits_>
61  struct Transformer<Out( const In&... ), Traits_, false>
62  : DataHandleMixin<std::tuple<Out>, filter_evtcontext<In...>, Traits_> {
63  using DataHandleMixin<std::tuple<Out>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
64 
65  // derived classes can NOT implement execute
66  StatusCode execute( const EventContext& ctx ) const override final {
67  try {
68  if constexpr ( sizeof...( In ) == 0 ) {
69  put( std::get<0>( this->m_outputs ), ( *this )() );
70  } else if constexpr ( std::tuple_size_v<filter_evtcontext<In...>> == 0 ) {
71  put( std::get<0>( this->m_outputs ), ( *this )( ctx ) );
72  } else {
73  put( std::get<0>( this->m_outputs ), filter_evtcontext_t<In...>::apply( *this, ctx, this->m_inputs ) );
74  }
75  return FilterDecision::PASSED;
76  } catch ( GaudiException& e ) {
77  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
78  return e.code();
79  }
80  }
81 
82  // instead they MUST implement this operator
83  virtual Out operator()( const In&... ) const = 0;
84  };
85 
86  //
87  // general N -> M algorithms
88  //
89  template <typename Signature, typename Traits_, bool isLegacy>
91 
92  template <typename... Out, typename... In, typename Traits_>
93  struct MultiTransformer<std::tuple<Out...>( const In&... ), Traits_, true>
94  : DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_> {
95  using DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
96 
97  // derived classes can NOT implement execute
98  StatusCode execute() override final {
99  try {
100  std::apply(
101  [this]( auto&... ohandle ) {
103 
104  if constexpr ( sizeof...( In ) == 0 ) {
105  std::apply( [&ohandle...](
106  auto&&... data ) { ( put( ohandle, std::forward<decltype( data )>( data ) ), ... ); },
107  std::as_const( *this )() );
108  } else {
109  std::apply( [&ohandle...](
110  auto&&... data ) { ( put( ohandle, std::forward<decltype( data )>( data ) ), ... ); },
111  filter_evtcontext_t<In...>::apply( std::as_const( *this ), this->m_inputs ) );
112  }
114  },
115  this->m_outputs );
116  return FilterDecision::PASSED;
117  } catch ( GaudiException& e ) {
118  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
119  return e.code();
120  }
121  }
122 
123  // instead they MUST implement this operator
124  virtual std::tuple<Out...> operator()( const In&... ) const = 0;
125  };
126 
127  template <typename... Out, typename... In, typename Traits_>
128  struct MultiTransformer<std::tuple<Out...>( const In&... ), Traits_, false>
129  : DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_> {
130  using DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
131 
132  // derived classes can NOT implement execute
133  StatusCode execute( const EventContext& ctx ) const override final {
134  try {
136  std::apply(
137  [this, &ctx]( auto&... ohandle ) {
138  if constexpr ( sizeof...( In ) == 0 ) {
139  std::apply( [&ohandle...](
140  auto&&... data ) { ( put( ohandle, std::forward<decltype( data )>( data ) ), ... ); },
141  ( *this )() );
142  } else if constexpr ( std::tuple_size_v<filter_evtcontext<In...>> == 0 ) {
143  std::apply( [&ohandle...](
144  auto&&... data ) { ( put( ohandle, std::forward<decltype( data )>( data ) ), ... ); },
145  ( *this )( ctx ) );
146  } else {
147  std::apply( [&ohandle...](
148  auto&&... data ) { ( put( ohandle, std::forward<decltype( data )>( data ) ), ... ); },
149  filter_evtcontext_t<In...>::apply( *this, ctx, this->m_inputs ) );
150  }
151  },
152  this->m_outputs );
154  return FilterDecision::PASSED;
155  } catch ( GaudiException& e ) {
156  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
157  return e.code();
158  }
159  }
160 
161  // instead they MUST implement this operator
162  virtual std::tuple<Out...> operator()( const In&... ) const = 0;
163  };
164 
165  //
166  // general N -> M algorithms with filter functionality
167  //
168  template <typename Signature, typename Traits_, bool isLegacy>
170 
171  template <typename... Out, typename... In, typename Traits_>
172  struct MultiTransformerFilter<std::tuple<Out...>( const In&... ), Traits_, true>
173  : DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_> {
174  using DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
175 
176  // derived classes can NOT implement execute
177  StatusCode execute() override final {
178  try {
179  return std::apply(
180  [&]( auto&... ohandle ) {
182  return std::apply(
183  [&ohandle...]( bool passed, auto&&... data ) {
184  ( put( ohandle, std::forward<decltype( data )>( data ) ), ... );
185  return passed;
186  },
187  filter_evtcontext_t<In...>::apply( *this, this->m_inputs ) );
189  },
190  this->m_outputs )
191  ? FilterDecision::PASSED
192  : FilterDecision::FAILED;
193  } catch ( GaudiException& e ) {
194  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
195  return e.code();
196  }
197  }
198 
199  // instead they MUST implement this operator
200  virtual std::tuple<bool, Out...> operator()( const In&... ) const = 0;
201  };
202 
203  template <typename... Out, typename... In, typename Traits_>
204  struct MultiTransformerFilter<std::tuple<Out...>( const In&... ), Traits_, false>
205  : DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_> {
206  using DataHandleMixin<std::tuple<Out...>, filter_evtcontext<In...>, Traits_>::DataHandleMixin;
207 
208  // derived classes can NOT implement execute
209  StatusCode execute( const EventContext& ctx ) const override final {
210  try {
211  return std::apply(
212  GF_SUPPRESS_SPURIOUS_CLANG_WARNING_BEGIN[&]( auto&... ohandle ) {
213  return std::apply(
214  [&ohandle...]( bool passed, auto&&... data ) {
215  ( put( ohandle, std::forward<decltype( data )>( data ) ), ... );
216  return passed;
217  },
218  filter_evtcontext_t<In...>::apply( *this, ctx, this->m_inputs ) );
219  },
221 
222  this->m_outputs )
223  ? FilterDecision::PASSED
224  : FilterDecision::FAILED;
225  } catch ( GaudiException& e ) {
226  if ( e.code().isFailure() ) this->error() << e.tag() << " : " << e.message() << endmsg;
227  return e.code();
228  }
229  }
230 
231  // instead they MUST implement this operator
232  virtual std::tuple<bool, Out...> operator()( const In&... ) const = 0;
233  };
234  } // namespace details
235 
236  template <typename Signature, typename Traits_ = Traits::useDefaults>
238 
239  template <typename Signature, typename Traits_ = Traits::useDefaults>
241 
242  template <typename Signature, typename Traits_ = Traits::useDefaults>
244 
245 } // namespace Gaudi::Functional
Gaudi ::Functional::details::MultiTransformerFilter< std::tuple< Out... >(const In &...), Traits_, false >::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: Transformer.h:209
Gaudi ::Functional::details::MultiTransformer< std::tuple< Out... >(const In &...), Traits_, true >::execute
StatusCode execute() override final
Definition: Transformer.h:98
Gaudi::Hive::currentContext
GAUDI_API const EventContext & currentContext()
Definition: ThreadLocalContext.cpp:30
Gaudi ::Functional::details::MultiTransformerFilter< std::tuple< Out... >(const In &...), Traits_, false >::operator()
virtual std::tuple< bool, Out... > operator()(const In &...) const =0
GaudiException.h
Gaudi ::Functional::details::MultiTransformerFilter< std::tuple< Out... >(const In &...), Traits_, true >::operator()
virtual std::tuple< bool, Out... > operator()(const In &...) const =0
GaudiException
Definition: GaudiException.h:31
std::tuple
GF_SUPPRESS_SPURIOUS_CLANG_WARNING_BEGIN
#define GF_SUPPRESS_SPURIOUS_CLANG_WARNING_BEGIN
Definition: details.h:39
Gaudi ::Functional::details::MultiTransformerFilter< std::tuple< Out... >(const In &...), Traits_, true >::execute
StatusCode execute() override final
Definition: Transformer.h:177
Gaudi ::Functional::details::Transformer< Out(const In &...), Traits_, true >::operator()
virtual Out operator()(const In &...) const =0
Gaudi ::Functional::details::MultiTransformerFilter
Definition: Transformer.h:169
GaudiException::message
virtual const std::string & message() const
error message to be printed
Definition: GaudiException.h:68
GaudiPython.Pythonizations.ctx
ctx
Definition: Pythonizations.py:578
StatusCode
Definition: StatusCode.h:65
details
Definition: AnyDataWrapper.h:19
Gaudi ::Functional::details::Transformer< Out(const In &...), Traits_, true >::execute
StatusCode execute() override final
Definition: Transformer.h:40
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
GaudiException::tag
virtual const std::string & tag() const
name tag for the exception, or exception type
Definition: GaudiException.h:77
GF_SUPPRESS_SPURIOUS_CLANG_WARNING_END
#define GF_SUPPRESS_SPURIOUS_CLANG_WARNING_END
Definition: details.h:40
Gaudi ::Functional::details::MultiTransformer< std::tuple< Out... >(const In &...), Traits_, false >::operator()
virtual std::tuple< Out... > operator()(const In &...) const =0
utilities.h
Gaudi
This file provides a Grammar for the type Gaudi::Accumulators::Axis It allows to use that type from p...
Definition: __init__.py:1
GaudiException::code
virtual const StatusCode & code() const
StatusCode for Exception.
Definition: GaudiException.h:86
StatusCode::isFailure
bool isFailure() const
Definition: StatusCode.h:129
Gaudi ::Functional::details::MultiTransformer< std::tuple< Out... >(const In &...), Traits_, true >::operator()
virtual std::tuple< Out... > operator()(const In &...) const =0
std
STL namespace.
Gaudi ::Functional::details::MultiTransformer
Definition: Transformer.h:90
EventContext
Definition: EventContext.h:34
Gaudi ::Functional::details::Transformer
Definition: Transformer.h:31
Gaudi ::Functional::details::Transformer< Out(const In &...), Traits_, false >::operator()
virtual Out operator()(const In &...) const =0
details.h
Gaudi ::Functional::details::MultiTransformer< std::tuple< Out... >(const In &...), Traits_, false >::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: Transformer.h:133
Gaudi ::Functional::details::Transformer< Out(const In &...), Traits_, false >::execute
StatusCode execute(const EventContext &ctx) const override final
Definition: Transformer.h:66
FunctionalFilterDecision.h
Gaudi::Functional::details::filter_evtcontext
typename filter_evtcontext_t< In... >::type filter_evtcontext
Definition: details.h:503
Gaudi::Functional::details::put
auto put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
Definition: details.h:168