Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
#include "CommonTools/UtilAlgos/interface/DetIdSelector.h"
#include "DataFormats/DetId/interface/DetId.h"
#include "FWCore/ParameterSet/interface/ParameterSet.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"

DetIdSelector::DetIdSelector() : m_selections(), m_masks() {}

DetIdSelector::DetIdSelector(const std::string& selstring) : m_selections(), m_masks() { addSelection(selstring); }

DetIdSelector::DetIdSelector(const std::vector<std::string>& selstrings) : m_selections(), m_masks() {
  addSelection(selstrings);
}

DetIdSelector::DetIdSelector(const edm::ParameterSet& selconfig) : m_selections(), m_masks() {
  const std::vector<std::string> selstrings = selconfig.getUntrackedParameter<std::vector<std::string> >("selection");
  addSelection(selstrings);
}

void DetIdSelector::addSelection(const std::string& selstring) {
  unsigned int selection;
  unsigned int mask;

  if (selstring.substr(0, 2) == "0x") {
    sscanf(selstring.c_str(), "%x-%x", &mask, &selection);
  } else {
    sscanf(selstring.c_str(), "%u-%u", &mask, &selection);
  }

  m_selections.push_back(selection);
  m_masks.push_back(mask);

  LogDebug("Selection added") << "Selection " << selection << " with mask " << mask << " added";
}

void DetIdSelector::addSelection(const std::vector<std::string>& selstrings) {
  for (std::vector<std::string>::const_iterator selstring = selstrings.begin(); selstring != selstrings.end();
       ++selstring) {
    addSelection(*selstring);
  }
}

bool DetIdSelector::isSelected(const unsigned int& rawid) const {
  for (unsigned int i = 0; i < m_selections.size(); ++i) {
    if ((m_masks[i] & rawid) == m_selections[i])
      return true;
  }

  return false;
}

bool DetIdSelector::isSelected(const DetId& detid) const { return isSelected(detid.rawId()); }

bool DetIdSelector::operator()(const DetId& detid) const { return isSelected(detid.rawId()); }

bool DetIdSelector::operator()(const unsigned int& rawid) const { return isSelected(rawid); }