CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/FWCore/Framework/src/GroupSelector.cc

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