File indexing completed on 2024-04-06 12:12:14
0001 #include <algorithm>
0002 #include <iterator>
0003 #include <ostream>
0004 #include <cctype>
0005
0006 #include "DataFormats/Provenance/interface/BranchDescription.h"
0007 #include "DataFormats/Provenance/interface/ProductRegistry.h"
0008 #include "FWCore/Framework/interface/ProductSelector.h"
0009 #include "FWCore/Framework/interface/ProductSelectorRules.h"
0010 #include "FWCore/Utilities/interface/EDMException.h"
0011 #include "FWCore/Utilities/interface/Algorithms.h"
0012
0013 namespace edm {
0014
0015
0016 typedef std::vector<edm::BranchDescription const*> VCBDP;
0017
0018 ProductSelector::ProductSelector() : productsToSelect_(), initialized_(false) {}
0019
0020 void ProductSelector::initialize(ProductSelectorRules const& rules, VCBDP const& branchDescriptions) {
0021 typedef ProductSelectorRules::BranchSelectState BranchSelectState;
0022
0023
0024
0025 std::vector<BranchSelectState> branchstates;
0026 {
0027 branchstates.reserve(branchDescriptions.size());
0028
0029 VCBDP::const_iterator it = branchDescriptions.begin();
0030 VCBDP::const_iterator end = branchDescriptions.end();
0031 for (; it != end; ++it)
0032 branchstates.emplace_back(*it);
0033 }
0034
0035
0036
0037 rules.applyToAll(branchstates);
0038
0039
0040
0041
0042
0043 {
0044 std::vector<BranchSelectState>::const_iterator it = branchstates.begin();
0045 std::vector<BranchSelectState>::const_iterator end = branchstates.end();
0046 for (; it != end; ++it) {
0047 if (it->selectMe)
0048 productsToSelect_.push_back(it->desc->branchName());
0049 }
0050 sort_all(productsToSelect_);
0051 }
0052 initialized_ = true;
0053 }
0054
0055 bool ProductSelector::selected(BranchDescription const& desc) const {
0056 if (!initialized_) {
0057 throw edm::Exception(edm::errors::LogicError) << "ProductSelector::selected() called prematurely\n"
0058 << "before the product registry has been frozen.\n";
0059 }
0060
0061
0062 return binary_search_all(productsToSelect_, desc.branchName());
0063 }
0064
0065 void ProductSelector::print(std::ostream& os) const {
0066 os << "ProductSelector at: " << static_cast<void const*>(this) << " has " << productsToSelect_.size()
0067 << " products to select:\n";
0068 copy_all(productsToSelect_, std::ostream_iterator<std::string>(os, "\n"));
0069 }
0070
0071 void ProductSelector::checkForDuplicateKeptBranch(
0072 BranchDescription const& desc, std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc) {
0073
0074
0075 if (desc.produced()) {
0076 auto check = [&](BranchID const& branchID) {
0077 auto iter = trueBranchIDToKeptBranchDesc.find(branchID);
0078 if (iter != trueBranchIDToKeptBranchDesc.end()) {
0079 throw edm::Exception(errors::Configuration, "Duplicate Output Selection")
0080 << "Two (or more) equivalent branches have been selected for output.\n"
0081 << "#1: " << BranchKey(desc) << "\n"
0082 << "#2: " << BranchKey(*iter->second) << "\n"
0083 << "Please drop at least one of them.\n";
0084 }
0085 };
0086
0087
0088 BranchID const& trueBranchID = desc.isSwitchAlias() ? desc.switchAliasForBranchID() : desc.originalBranchID();
0089 check(trueBranchID);
0090 trueBranchIDToKeptBranchDesc.insert(std::make_pair(trueBranchID, &desc));
0091 }
0092 }
0093
0094
0095 void ProductSelector::fillDroppedToKept(
0096 ProductRegistry const& preg,
0097 std::map<BranchID, BranchDescription const*> const& trueBranchIDToKeptBranchDesc,
0098 std::map<BranchID::value_type, BranchID::value_type>& droppedBranchIDToKeptBranchID_) {
0099 for (auto const& it : preg.productList()) {
0100 BranchDescription const& desc = it.second;
0101 if (!desc.produced() || desc.isAlias())
0102 continue;
0103 BranchID const& branchID = desc.branchID();
0104 std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(branchID);
0105 if (iter != trueBranchIDToKeptBranchDesc.end()) {
0106
0107 BranchID const& keptBranchID = iter->second->branchID();
0108 if (keptBranchID != branchID) {
0109
0110 droppedBranchIDToKeptBranchID_.insert(std::make_pair(branchID.id(), keptBranchID.id()));
0111 }
0112 }
0113 }
0114 }
0115
0116
0117
0118
0119
0120 std::ostream& operator<<(std::ostream& os, const ProductSelector& gs) {
0121 gs.print(os);
0122 return os;
0123 }
0124
0125 }