Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:04:59

0001 #include "DataFormats/PatCandidates/interface/VIDCutFlowResult.h"
0002 #include "FWCore/Utilities/interface/Exception.h"
0003 
0004 namespace {
0005   const std::string empty_str("");
0006 }
0007 
0008 namespace vid {
0009 
0010   CutFlowResult::CutFlowResult(const std::string& name,
0011                                const std::string& hash,
0012                                const std::map<std::string, unsigned>& n2idx,
0013                                const std::vector<double>& values,
0014                                unsigned bitmap,
0015                                unsigned mask)
0016       : name_(name), hash_(hash), bitmap_(bitmap), mask_(mask), values_(values) {
0017     for (const auto& val : n2idx) {
0018       names_.push_back(val.first);
0019       indices_.push_back(val.second);
0020     }
0021   }
0022 
0023   const std::string& CutFlowResult::getNameAtIndex(const unsigned idx) const {
0024     unsigned internal_idx = 0;
0025     for (const auto& value : indices_) {
0026       if (value == idx)
0027         return names_[internal_idx];
0028       ++internal_idx;
0029     }
0030     throw cms::Exception("IndexNotFound") << "index = " << idx << " has no corresponding cut name!";
0031     return empty_str;
0032   }
0033 
0034   bool CutFlowResult::getCutResultByIndex(const unsigned idx) const {
0035     if (idx >= indices_.size()) {
0036       throw cms::Exception("OutOfBounds") << idx << " is out of bounds for this cut flow!";
0037     }
0038     return getCutBit(idx);
0039   }
0040 
0041   bool CutFlowResult::getCutResultByName(const std::string& name) const {
0042     auto found_name = std::lower_bound(names_.begin(), names_.end(), name);
0043     if (found_name == names_.end() || *found_name != name) {
0044       throw cms::Exception("UnknownName") << "Cut name: " << name << " is not known for this cutflow!";
0045     }
0046     return getCutBit(indices_[std::distance(names_.begin(), found_name)]);
0047   }
0048 
0049   bool CutFlowResult::isCutMasked(const unsigned idx) const {
0050     if (idx >= indices_.size()) {
0051       throw cms::Exception("OutOfBounds") << idx << " is out of bounds for this cut flow!";
0052     }
0053     return getMaskBit(idx);
0054   }
0055 
0056   bool CutFlowResult::isCutMasked(const std::string& name) const {
0057     auto found_name = std::lower_bound(names_.begin(), names_.end(), name);
0058     if (found_name == names_.end() || *found_name != name) {
0059       throw cms::Exception("UnknownName") << "Cut name: " << name << " is not known for this cutflow!";
0060     }
0061     return getMaskBit(indices_[std::distance(names_.begin(), found_name)]);
0062   }
0063 
0064   double CutFlowResult::getValueCutUpon(const unsigned idx) const {
0065     if (idx >= indices_.size()) {
0066       throw cms::Exception("OutOfBounds") << idx << " is out of bounds for this cut flow!";
0067     }
0068     return getCutValue(idx);
0069   }
0070 
0071   double CutFlowResult::getValueCutUpon(const std::string& name) const {
0072     auto found_name = std::lower_bound(names_.begin(), names_.end(), name);
0073     if (found_name == names_.end() || *found_name != name) {
0074       throw cms::Exception("UnknownName") << "Cut name: " << name << " is not known for this cutflow!";
0075     }
0076     return getCutValue(indices_[std::distance(names_.begin(), found_name)]);
0077   }
0078 
0079   CutFlowResult CutFlowResult::getCutFlowResultMasking(const std::vector<unsigned>& idxs) const {
0080     unsigned bitmap = bitmap_;
0081     unsigned mask = mask_;
0082     for (const unsigned idx : idxs) {
0083       if (idx >= indices_.size()) {
0084         throw cms::Exception("OutOfBounds") << idx << " is out of bounds for this cut flow!";
0085       }
0086       mask = mask | 1 << idx;
0087     }
0088     bitmap = bitmap | mask;
0089     return CutFlowResult(name_, empty_str, names_, indices_, values_, bitmap, mask);
0090   }
0091 
0092   CutFlowResult CutFlowResult::getCutFlowResultMasking(const std::vector<std::string>& names) const {
0093     unsigned bitmap = bitmap_;
0094     unsigned mask = mask_;
0095     for (const std::string& name : names) {
0096       auto found_name = std::lower_bound(names_.begin(), names_.end(), name);
0097       if (found_name == names_.end() || *found_name != name) {
0098         throw cms::Exception("UnknownName") << "Cut name: " << name << " is not known for this cutflow!";
0099       }
0100       mask = mask | 1 << indices_[std::distance(names_.begin(), found_name)];
0101     }
0102     bitmap = bitmap | mask;
0103     return CutFlowResult(name_, empty_str, names_, indices_, values_, bitmap, mask);
0104   }
0105 
0106   CutFlowResult CutFlowResult::getCutFlowResultMasking(const unsigned idx) const {
0107     unsigned bitmap = bitmap_;
0108     unsigned mask = mask_;
0109     if (idx >= indices_.size()) {
0110       throw cms::Exception("OutOfBounds") << idx << " is out of bounds for this cut flow!";
0111     }
0112     mask = mask | 1 << idx;
0113     bitmap = bitmap | mask;
0114     return CutFlowResult(name_, empty_str, names_, indices_, values_, bitmap, mask);
0115   }
0116 
0117   CutFlowResult CutFlowResult::getCutFlowResultMasking(const std::string& name) const {
0118     unsigned bitmap = bitmap_;
0119     unsigned mask = mask_;
0120     auto found_name = std::lower_bound(names_.begin(), names_.end(), name);
0121     if (found_name == names_.end() || *found_name != name) {
0122       throw cms::Exception("UnknownName") << "Cut name: " << name << " is not known for this cutflow!";
0123     }
0124     mask = mask | 1 << indices_[std::distance(names_.begin(), found_name)];
0125     bitmap = bitmap | mask;
0126     return CutFlowResult(name_, empty_str, names_, indices_, values_, bitmap, mask);
0127   }
0128 }  // namespace vid