![]() |
![]() |
00001 // $Id: GroupSelector.cc,v 1.27 2008/06/05 23:17:05 wmtan Exp $ 00002 00003 #include <algorithm> 00004 #include <iterator> 00005 #include <ostream> 00006 #include <cctype> 00007 00008 #include "boost/algorithm/string.hpp" 00009 00010 #include "DataFormats/Provenance/interface/BranchDescription.h" 00011 #include "FWCore/Framework/interface/GroupSelector.h" 00012 #include "FWCore/Framework/interface/GroupSelectorRules.h" 00013 #include "FWCore/ParameterSet/interface/ParameterSet.h" 00014 #include "FWCore/Utilities/interface/EDMException.h" 00015 #include "FWCore/Utilities/interface/Algorithms.h" 00016 00017 00018 namespace edm { 00019 // The following typedef is used only in this implementation file, in 00020 // order to shorten several lines of code. 00021 typedef std::vector<edm::BranchDescription const*> VCBDP; 00022 00023 GroupSelector::GroupSelector() : groupsToSelect_(), initialized_(false) {} 00024 00025 void 00026 GroupSelector::initialize(GroupSelectorRules const& rules, VCBDP const& branchDescriptions) { 00027 typedef GroupSelectorRules::BranchSelectState BranchSelectState; 00028 00029 // Get a BranchSelectState for each branch, containing the branch 00030 // name, with its 'select bit' set to false. 00031 std::vector<BranchSelectState> branchstates; 00032 { 00033 branchstates.reserve(branchDescriptions.size()); 00034 00035 VCBDP::const_iterator it = branchDescriptions.begin(); 00036 VCBDP::const_iterator end = branchDescriptions.end(); 00037 for (; it != end; ++it) branchstates.push_back(BranchSelectState(*it)); 00038 } 00039 00040 // Now apply the rules to the branchstates, in order. Each rule 00041 // can override any previous rule, or all previous rules. 00042 rules.applyToAll(branchstates); 00043 00044 // For each of the BranchSelectStates that indicates the branch is 00045 // to be selected, remember the branch name. The list of branch 00046 // names must be sorted, for the implementation of 'selected' to 00047 // work. 00048 { 00049 std::vector<BranchSelectState>::const_iterator it = branchstates.begin(); 00050 std::vector<BranchSelectState>::const_iterator end = branchstates.end(); 00051 for (; it != end; ++it) { 00052 if (it->selectMe) groupsToSelect_.push_back(it->desc->branchName()); 00053 } 00054 sort_all(groupsToSelect_); 00055 } 00056 initialized_ = true; 00057 } 00058 00059 bool GroupSelector::selected(BranchDescription const& desc) const { 00060 if (!initialized_) { 00061 throw edm::Exception(edm::errors::LogicError) 00062 << "GroupSelector::selected() called prematurely\n" 00063 << "before the product registry has been frozen.\n"; 00064 } 00065 // We are to select this 'branch' if its name is one of the ones we 00066 // have been told to select. 00067 return binary_search_all(groupsToSelect_, desc.branchName()); 00068 } 00069 00070 void 00071 GroupSelector::print(std::ostream& os) const { 00072 os << "GroupSelector at: " 00073 << static_cast<void const*>(this) 00074 << " has " 00075 << groupsToSelect_.size() 00076 << " groups to select:\n"; 00077 copy_all(groupsToSelect_, std::ostream_iterator<std::string>(os, "\n")); 00078 } 00079 00080 00081 //-------------------------------------------------- 00082 // 00083 // Associated free functions 00084 // 00085 std::ostream& 00086 operator<< (std::ostream& os, const GroupSelector& gs) 00087 { 00088 gs.print(os); 00089 return os; 00090 } 00091 00092 }