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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
#include "CondFormats/SiStripObjects/interface/SiStripDetVOff.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "CondFormats/SiStripObjects/interface/SiStripDetSummary.h"

#include <algorithm>

void SiStripDetVOff::setBits(uint32_t& enDetId, const int HVoff, const int LVoff) {
  if (LVoff != -1) {
    // LVonMask has all bits equal to 1 apart from the last one.
    if (LVoff == 0)
      enDetId &= LVonMask;
    if (LVoff == 1)
      enDetId |= LVmask;
  }
  if (HVoff != -1) {
    // HVonMask has all bits equal to 1 apart from the next to last one.
    if (HVoff == 0)
      enDetId &= HVonMask;
    if (HVoff == 1)
      enDetId |= HVmask;
  }
}

bool SiStripDetVOff::put(const uint32_t DetId, const int HVoff, const int LVoff) {
  // Shift the DetId number of 2 bits to the left to have it in the final format with
  // the two additional bits used for HV and LV.
  uint32_t enDetId = (DetId << bitShift) & eightBitMask;

  // Binary search to determine if the element is already in the vector
  vOffIterator p = std::lower_bound(v_Voff.begin(), v_Voff.end(), enDetId);
  if (p != v_Voff.end() && (*p >> bitShift) == DetId) {
    // Found a matching entry, insert the HV and LV information.
    setBits(*p, HVoff, LVoff);
    // Check if the detector has all on, in that case remove it from the list.
    if ((~(*p) & allOnMask) == allOnMask)
      v_Voff.erase(p);
  } else {
    // Not found, insert a new entry only if it is not all on
    setBits(enDetId, HVoff, LVoff);
    if ((~enDetId & allOnMask) != allOnMask)
      v_Voff.insert(p, enDetId);
  }
  return true;
}

bool SiStripDetVOff::put(std::vector<uint32_t>& DetId, std::vector<int>& HVoff, std::vector<int>& LVoff) {
  if (DetId.size() == HVoff.size() && DetId.size() == LVoff.size()) {
    constVoffIterator detIdIt = DetId.begin();
    constVoffIterator detIdItEnd = DetId.end();
    constVboolIterator HVoffIt = HVoff.begin();
    constVboolIterator LVoffIt = LVoff.begin();
    for (; detIdIt != detIdItEnd; ++detIdIt, ++HVoffIt, ++LVoffIt) {
      put(*detIdIt, *HVoffIt, *LVoffIt);
    }
  } else {
    std::cout << "Error: inconsistent sizes of vectors:" << std::endl;
    std::cout << "DetId size = " << DetId.size() << ", HVoff size = " << HVoff.size()
              << ", LVoff size = " << LVoff.size() << std::endl;
    return false;
  }
  return true;
}

void SiStripDetVOff::getDetIds(std::vector<uint32_t>& DetIds_) const {
  // returns vector of DetIds in map
  DetIds_.clear();
  // Extract the detId from the bitSet and fill the vector
  constVoffIterator bitSetIt = v_Voff.begin();
  constVoffIterator bitSetItEnd = v_Voff.end();
  for (; bitSetIt != bitSetItEnd; ++bitSetIt) {
    DetIds_.push_back((*bitSetIt) >> bitShift);
  }
}

bool SiStripDetVOff::IsModuleVOff(const uint32_t DetId) const {
  uint32_t enDetId = (DetId << bitShift) & eightBitMask;
  constVoffIterator p = std::lower_bound(v_Voff.begin(), v_Voff.end(), enDetId);
  if (p != v_Voff.end() && (*p >> bitShift) == DetId)
    return true;
  return false;
}

bool SiStripDetVOff::IsModuleLVOff(const uint32_t DetId) const {
  uint32_t enDetId = (DetId << bitShift) & eightBitMask;
  constVoffIterator p = std::lower_bound(v_Voff.begin(), v_Voff.end(), enDetId);
  if (p != v_Voff.end() && (*p >> bitShift) == DetId && (*p & LVmask))
    return true;
  return false;
}

bool SiStripDetVOff::IsModuleHVOff(const uint32_t DetId) const {
  uint32_t enDetId = (DetId << bitShift) & eightBitMask;
  constVoffIterator p = std::lower_bound(v_Voff.begin(), v_Voff.end(), enDetId);
  if (p != v_Voff.end() && (*p >> bitShift) == DetId && (*p & HVmask))
    return true;
  return false;
}

void SiStripDetVOff::printDebug(std::stringstream& ss, const TrackerTopology* /*trackerTopo*/) const {
  std::vector<uint32_t> detIds;
  getDetIds(detIds);
  constVoffIterator it = detIds.begin();
  ss << "DetId    \t HV \t LV" << std::endl;
  for (; it != detIds.end(); ++it) {
    ss << *it << "\t";
    if (IsModuleHVOff(*it))
      ss << "OFF\t";
    else
      ss << "ON \t";
    if (IsModuleLVOff(*it))
      ss << "OFF" << std::endl;
    else
      ss << "ON" << std::endl;
  }
}

int SiStripDetVOff::getLVoffCounts() const {
  std::vector<uint32_t> detIds;
  getDetIds(detIds);
  return std::count_if(std::begin(detIds), std::end(detIds), [this](uint32_t id) -> bool { return IsModuleLVOff(id); });
}

int SiStripDetVOff::getHVoffCounts() const {
  std::vector<uint32_t> detIds;
  getDetIds(detIds);
  return std::count_if(std::begin(detIds), std::end(detIds), [this](uint32_t id) -> bool { return IsModuleHVOff(id); });
}

void SiStripDetVOff::printSummary(std::stringstream& ss, const TrackerTopology* trackerTopo) const {
  SiStripDetSummary summaryHV{trackerTopo};
  SiStripDetSummary summaryLV{trackerTopo};
  std::vector<uint32_t> detIds;
  getDetIds(detIds);
  constVoffIterator it = detIds.begin();
  for (; it != detIds.end(); ++it) {
    if (IsModuleHVOff(*it))
      summaryHV.add(*it);
    if (IsModuleLVOff(*it))
      summaryLV.add(*it);
  }
  ss << "Summary of detectors with HV off:" << std::endl;
  summaryHV.print(ss, false);
  ss << "Summary of detectors with LV off:" << std::endl;
  summaryLV.print(ss, false);
}