CMS 3D CMS Logo

ProductSelector.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <iterator>
3 #include <ostream>
4 #include <cctype>
5 
12 
13 namespace edm {
14  // The following typedef is used only in this implementation file, in
15  // order to shorten several lines of code.
16  typedef std::vector<edm::BranchDescription const*> VCBDP;
17 
18  ProductSelector::ProductSelector() : productsToSelect_(), initialized_(false) {}
19 
20  void ProductSelector::initialize(ProductSelectorRules const& rules, VCBDP const& branchDescriptions) {
21  typedef ProductSelectorRules::BranchSelectState BranchSelectState;
22 
23  // Get a BranchSelectState for each branch, containing the branch
24  // name, with its 'select bit' set to false.
25  std::vector<BranchSelectState> branchstates;
26  {
27  branchstates.reserve(branchDescriptions.size());
28 
29  VCBDP::const_iterator it = branchDescriptions.begin();
30  VCBDP::const_iterator end = branchDescriptions.end();
31  for (; it != end; ++it)
32  branchstates.emplace_back(*it);
33  }
34 
35  // Now apply the rules to the branchstates, in order. Each rule
36  // can override any previous rule, or all previous rules.
37  rules.applyToAll(branchstates);
38 
39  // For each of the BranchSelectStates that indicates the branch is
40  // to be selected, remember the branch name. The list of branch
41  // names must be sorted, for the implementation of 'selected' to
42  // work.
43  {
44  std::vector<BranchSelectState>::const_iterator it = branchstates.begin();
45  std::vector<BranchSelectState>::const_iterator end = branchstates.end();
46  for (; it != end; ++it) {
47  if (it->selectMe)
48  productsToSelect_.push_back(it->desc->branchName());
49  }
51  }
52  initialized_ = true;
53  }
54 
55  bool ProductSelector::selected(BranchDescription const& desc) const {
56  if (!initialized_) {
57  throw edm::Exception(edm::errors::LogicError) << "ProductSelector::selected() called prematurely\n"
58  << "before the product registry has been frozen.\n";
59  }
60  // We are to select this 'branch' if its name is one of the ones we
61  // have been told to select.
63  }
64 
65  void ProductSelector::print(std::ostream& os) const {
66  os << "ProductSelector at: " << static_cast<void const*>(this) << " has " << productsToSelect_.size()
67  << " products to select:\n";
68  copy_all(productsToSelect_, std::ostream_iterator<std::string>(os, "\n"));
69  }
70 
72  BranchDescription const& desc, std::map<BranchID, BranchDescription const*>& trueBranchIDToKeptBranchDesc) {
73  // Check if an equivalent branch has already been selected due to an EDAlias.
74  // We only need the check for products produced in this process.
75  if (desc.produced()) {
76  auto check = [&](BranchID const& branchID) {
77  auto iter = trueBranchIDToKeptBranchDesc.find(branchID);
78  if (iter != trueBranchIDToKeptBranchDesc.end()) {
79  throw edm::Exception(errors::Configuration, "Duplicate Output Selection")
80  << "Two (or more) equivalent branches have been selected for output.\n"
81  << "#1: " << BranchKey(desc) << "\n"
82  << "#2: " << BranchKey(*iter->second) << "\n"
83  << "Please drop at least one of them.\n";
84  }
85  };
86  BranchID const& trueBranchID = desc.originalBranchID();
87  check(trueBranchID);
88  // In case of SwitchProducer, we have to check also the
89  // aliased-for BranchID for the case that the chosen case is an EDAlias
90  if (desc.isSwitchAlias()) {
92  }
93 
94  trueBranchIDToKeptBranchDesc.insert(std::make_pair(trueBranchID, &desc));
95  }
96  }
97 
98  // Fills in a mapping needed in the case that a branch was dropped while its EDAlias was kept.
100  ProductRegistry const& preg,
101  std::map<BranchID, BranchDescription const*> const& trueBranchIDToKeptBranchDesc,
102  std::map<BranchID::value_type, BranchID::value_type>& droppedBranchIDToKeptBranchID_) {
103  for (auto const& it : preg.productList()) {
104  BranchDescription const& desc = it.second;
105  if (!desc.produced() || desc.isAlias())
106  continue;
107  BranchID const& branchID = desc.branchID();
108  std::map<BranchID, BranchDescription const*>::const_iterator iter = trueBranchIDToKeptBranchDesc.find(branchID);
109  if (iter != trueBranchIDToKeptBranchDesc.end()) {
110  // This branch, produced in this process, or an alias of it, was persisted.
111  BranchID const& keptBranchID = iter->second->branchID();
112  if (keptBranchID != branchID) {
113  // An EDAlias branch was persisted.
114  droppedBranchIDToKeptBranchID_.insert(std::make_pair(branchID.id(), keptBranchID.id()));
115  }
116  }
117  }
118  }
119 
120  //--------------------------------------------------
121  //
122  // Associated free functions
123  //
124  std::ostream& operator<<(std::ostream& os, const ProductSelector& gs) {
125  gs.print(os);
126  return os;
127  }
128 
129 } // namespace edm
std::string const & branchName() const
bool selected(BranchDescription const &desc) const
static void fillDroppedToKept(ProductRegistry const &preg, std::map< BranchID, BranchDescription const * > const &trueBranchIDToKeptBranchDesc, std::map< BranchID::value_type, BranchID::value_type > &droppedBranchIDToKeptBranchID_)
std::vector< std::string > productsToSelect_
void applyToAll(std::vector< BranchSelectState > &branchstates) const
unsigned int id() const
Definition: BranchID.h:23
BranchID const & switchAliasForBranchID() const
ProductList const & productList() const
BranchID const & branchID() const
#define end
Definition: vmac.h:39
void sort_all(RandomAccessSequence &s)
wrappers for std::sort
Definition: Algorithms.h:92
std::vector< edm::BranchDescription const * > VCBDP
HLT enums.
Func copy_all(ForwardSequence &s, Func f)
wrappers for copy
Definition: Algorithms.h:20
void initialize(ProductSelectorRules const &rules, std::vector< BranchDescription const * > const &branchDescriptions)
bool binary_search_all(ForwardSequence const &s, Datum const &d)
wrappers for std::binary_search
Definition: Algorithms.h:58
BranchID const & originalBranchID() const
void print(std::ostream &os) const
Definition: AbsArchive.cc:45
std::ostream & operator<<(std::ostream &ost, const HLTGlobalStatus &hlt)
Formatted printout of trigger tbale.
static void check(T const &p, std::string const &id, SelectedProducts const &iProducts)
static void checkForDuplicateKeptBranch(BranchDescription const &desc, std::map< BranchID, BranchDescription const * > &trueBranchIDToKeptBranchDesc)