File indexing completed on 2024-04-06 12:12:03
0001 #ifndef FWCore_Framework_GetterOfProducts_h
0002 #define FWCore_Framework_GetterOfProducts_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098 #include "DataFormats/Common/interface/Handle.h"
0099 #include "DataFormats/Provenance/interface/BranchDescription.h"
0100 #include "FWCore/Framework/interface/Event.h"
0101 #include "FWCore/Framework/interface/EventForOutput.h"
0102 #include "FWCore/Framework/interface/LuminosityBlock.h"
0103 #include "FWCore/Framework/interface/LuminosityBlockForOutput.h"
0104 #include "FWCore/Framework/interface/ProcessBlock.h"
0105 #include "FWCore/Framework/interface/ProcessBlockForOutput.h"
0106 #include "FWCore/Framework/interface/Run.h"
0107 #include "FWCore/Framework/interface/RunForOutput.h"
0108 #include "FWCore/Framework/interface/WillGetIfMatch.h"
0109 #include "FWCore/Utilities/interface/BranchType.h"
0110 #include "FWCore/Utilities/interface/EDGetToken.h"
0111 #include "FWCore/Utilities/interface/TypeID.h"
0112
0113 #include <functional>
0114 #include <memory>
0115 #include <string>
0116 #include <vector>
0117
0118 namespace edm {
0119
0120 template <typename U>
0121 struct BranchTypeForContainerType {
0122 static constexpr BranchType branchType = InEvent;
0123 };
0124 template <>
0125 struct BranchTypeForContainerType<LuminosityBlock> {
0126 static constexpr BranchType branchType = InLumi;
0127 };
0128 template <>
0129 struct BranchTypeForContainerType<LuminosityBlockForOutput> {
0130 static constexpr BranchType branchType = InLumi;
0131 };
0132 template <>
0133 struct BranchTypeForContainerType<Run> {
0134 static constexpr BranchType branchType = InRun;
0135 };
0136 template <>
0137 struct BranchTypeForContainerType<RunForOutput> {
0138 static constexpr BranchType branchType = InRun;
0139 };
0140 template <>
0141 struct BranchTypeForContainerType<ProcessBlock> {
0142 static constexpr BranchType branchType = InProcess;
0143 };
0144 template <>
0145 struct BranchTypeForContainerType<ProcessBlockForOutput> {
0146 static constexpr BranchType branchType = InProcess;
0147 };
0148
0149 template <typename T>
0150 class GetterOfProducts {
0151 public:
0152 GetterOfProducts() : branchType_(edm::InEvent) {}
0153
0154 template <typename U, typename M>
0155 GetterOfProducts(U const& match, M* module, edm::BranchType branchType = edm::InEvent)
0156 : matcher_(WillGetIfMatch<T>(match, module)),
0157 tokens_(new std::vector<edm::EDGetTokenT<T>>),
0158 branchType_(branchType) {}
0159
0160 void operator()(edm::BranchDescription const& branchDescription) {
0161 if (branchDescription.dropped())
0162 return;
0163 if (branchDescription.branchType() == branchType_ &&
0164 branchDescription.unwrappedTypeID() == edm::TypeID(typeid(T))) {
0165 auto const& token = matcher_(branchDescription);
0166 if (not token.isUninitialized()) {
0167 tokens_->push_back(token);
0168 }
0169 }
0170 }
0171
0172 template <typename ProductContainer>
0173 void fillHandles(ProductContainer const& productContainer, std::vector<edm::Handle<T>>& handles) const {
0174 handles.clear();
0175 if (branchType_ == BranchTypeForContainerType<ProductContainer>::branchType) {
0176 handles.reserve(tokens_->size());
0177 for (auto const& token : *tokens_) {
0178 if (auto handle = productContainer.getHandle(token)) {
0179 handles.push_back(handle);
0180 }
0181 }
0182 }
0183 }
0184
0185 std::vector<edm::EDGetTokenT<T>> const& tokens() const { return *tokens_; }
0186 edm::BranchType branchType() const { return branchType_; }
0187
0188 private:
0189 std::function<EDGetTokenT<T>(BranchDescription const&)> matcher_;
0190
0191
0192 std::shared_ptr<std::vector<edm::EDGetTokenT<T>>> tokens_;
0193 edm::BranchType branchType_;
0194 };
0195 }
0196 #endif