16#include <boost/assert/source_location.hpp>
18#include <FairRootManager.h>
21#include <TClonesArray.h>
22#include <TCollection.h>
24#include <fmt/format.h>
28#include <unordered_map>
34 template <
typename InputType>
38 using RawDataType = std::remove_const_t<std::remove_cv_t<InputType>>;
40 : branch_name_{ branchName }
51 void init(
bool is_optional =
false,
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
53 auto* ioman = FairRootManager::Instance();
56 throw R3B::runtime_error(fmt::format(
"FairRootManager is nullptr during the initialisation of the "
57 "input data with the branch name \"{}\"",
62 data_ = ioman->InitObjectAs<
const RawDataType*>(branch_name_.c_str());
69 fmt::format(
"The data branch {:?} doesn't exist in the input root file!", branch_name_));
74 fmt::format(
"Initialisation of the input data with the branch name \"{}\" failed!",
81 [[nodiscard]]
auto get(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
const ->
const RawDataType&
87 auto size(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
const
95 auto begin(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
98 return data_->cbegin();
100 auto begin(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
const
103 return data_->cbegin();
105 auto end(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
108 return data_->cend();
110 auto end(
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
const
113 return data_->cend();
117 std::string branch_name_;
119 void check_init(
const boost::source_location& loc)
const
121 if (data_ ==
nullptr)
123 throw R3B::runtime_error(fmt::format(
"Input data with the branch name \"{}\" cannot be "
124 "queried without an initialisation!",
131 template <
typename InputType,
132 typename = std::enable_if_t<std::is_base_of_v<TObject, std::remove_const_t<std::remove_cv_t<InputType>>>>>
136 using RawDataType = std::remove_const_t<std::remove_cv_t<InputType>>;
138 : branch_name_{ branchName }
142 void init(
bool is_optional =
false,
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
144 auto* ioman = FairRootManager::Instance();
145 if (ioman ==
nullptr)
147 throw R3B::runtime_error(fmt::format(
"FairRootManager is nullptr during the initialisation of the "
148 "input data with the branch name \"{}\"",
153 data_ =
dynamic_cast<TClonesArray*
>(ioman->GetObject(branch_name_.c_str()));
154 if (data_ ==
nullptr)
156 const auto msg = fmt::format(
157 "Input TCA data with the branch name {:?} cannot be retrieved from the input file !", branch_name_);
160 R3BLOG(warn, msg.c_str());
166 check_element_type(is_optional, loc);
169 auto read() ->
const std::vector<RawDataType>&
171 output_data_.clear();
172 if (data_ ==
nullptr)
177 output_data_.reserve(data_->GetEntriesFast());
178 for (
auto* element : TRangeDynCast<RawDataType>(data_))
180 output_data_.emplace_back(*element);
185 [[nodiscard]]
auto size() const -> std::
size_t {
return data_ ==
nullptr ? 0 : data_->GetEntriesFast(); }
187 [[nodiscard]]
auto get() const -> const std::vector<
RawDataType>& {
return output_data_; }
190 std::string branch_name_;
191 std::vector<RawDataType> output_data_;
192 TClonesArray* data_ =
nullptr;
194 void check_element_type(
bool is_optional,
const boost::source_location& loc)
196 if (std::string_view{ data_->GetClass()->GetName() } != InputType::Class_Name())
198 const auto msg = fmt::format(
199 "The type of the retrieved data {:?} is not the same as the type given by the connector class {}!",
200 data_->GetClass()->GetName(),
201 InputType::Class_Name());
204 R3BLOG(warn, msg.c_str());
207 throw R3B::runtime_error(msg, loc);
212 template <
typename OutputType>
216 using RawDataType = std::remove_const_t<std::remove_cv_t<OutputType>>;
218 : branch_name_{ branchName }
229 void init(
bool persistance =
true,
const boost::source_location& loc = BOOST_CURRENT_LOCATION)
231 if (
auto* ioman = FairRootManager::Instance(); ioman !=
nullptr)
233 ioman->RegisterAny(branch_name_.c_str(), data_ptr_, persistance);
237 throw R3B::runtime_error(fmt::format(
"FairRootManager is nullptr during the initialisation of the "
238 "output data with the branch name \"{}\"",
249 template <
typename ResetOp>
255 auto size()
const {
return data_.size(); }
258 std::string branch_name_;
263 template <
typename ElementType>
266 template <
typename ElementType>
269 template <
typename KeyType,
typename ValueType>
272 template <
typename KeyType,
typename ValueType>
275 template <
typename KeyType,
typename ValueType>
278 template <
typename KeyType,
typename ValueType>
#define R3BLOG(severity, x)
OutputConnector & operator=(OutputConnector &&)=delete
OutputConnector(OutputConnector &&)=delete
OutputConnector(std::string_view branchName)
std::remove_const_t< std::remove_cv_t< OutputType > > RawDataType
void init(bool persistance=true, const boost::source_location &loc=BOOST_CURRENT_LOCATION)
~OutputConnector()=default
OutputConnector & operator=(const OutputConnector &other)=delete
auto get_constref() const -> const RawDataType &
auto get() -> RawDataType &
OutputConnector(const OutputConnector &)=delete
InputConnector< std::vector< ElementType > > InputVectorConnector
OutputConnector< std::map< KeyType, ValueType > > OutputMapConnector
InputConnector< std::map< KeyType, ValueType > > InputMapConnector
OutputConnector< std::unordered_map< KeyType, ValueType > > OutputHashConnector
InputConnector< std::unordered_map< KeyType, ValueType > > InputHashConnector
OutputConnector< std::vector< ElementType > > OutputVectorConnector