CMS 3D CMS Logo

SiStripBadChannelPatcher.cc
Go to the documentation of this file.
1 // system include files
2 #include <vector>
3 #include <iostream>
4 #include <fstream>
5 #include <sstream>
6 
7 // user include files
24 
25 namespace sistrip {
26  static const uint16_t NOT_A_FEDID = static_cast<uint16_t>(FEDNumbering::NOT_A_FEDID);
27 }
28 
30 public:
32  : record_(iConfig.getParameter<std::string>("Record")),
33  printDebug_(iConfig.getParameter<bool>("printDebug")),
34  detIdsToExclude_(iConfig.getParameter<std::vector<unsigned int>>("detIdsToExclude")),
35  detIdsToInclude_(iConfig.getParameter<std::vector<unsigned int>>("detIdsToInclude")),
36  fedsToExclude_(iConfig.getParameter<std::vector<unsigned int>>("FEDsToExclude")),
37  fedsToInclude_(iConfig.getParameter<std::vector<unsigned int>>("FEDsToInclude")),
40 
41  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
42  ~SiStripBadChannelPatcher() override = default;
43 
44 private:
45  void analyze(const edm::Event&, const edm::EventSetup&) override;
46  std::unique_ptr<SiStripBadStrip> getNewObject(const edm::EventSetup& iSetup);
47  unsigned int fedFromDetId(const uint32_t& detid);
48  void addDetIdsFromExcludedFEDs(const std::vector<uint32_t>& allDetIds);
49 
50  // member data
53  const bool printDebug_;
54  std::vector<uint32_t> detIdsToExclude_;
55  std::vector<uint32_t> detIdsToInclude_;
56  std::vector<unsigned int> fedsToExclude_;
57  std::vector<unsigned int> fedsToInclude_;
60 };
61 
62 //-----------------------------------------------------------------------------------------------//
65  desc.setComment(
66  "create a SiStripBadStrip payload starting from one in DB and excluding or adding entire modules from a list");
67  desc.add<std::string>("Record", "SiStripBadStrip")->setComment("Record to match in the PoolDBOutputService");
68  desc.add<bool>("printDebug", false)->setComment("full debug printout");
69  desc.add<std::vector<unsigned int>>("detIdsToExclude", {})->setComment("list of detIds to exclude");
70  desc.add<std::vector<unsigned int>>("detIdsToInclude", {})->setComment("list of detIds to include");
71  desc.add<std::vector<unsigned int>>("FEDsToExclude", {})->setComment("list of FEDs to exclude");
72  desc.add<std::vector<unsigned int>>("FEDsToInclude", {})->setComment("list of FEDs to include");
73  descriptions.addWithDefaultLabel(desc);
74 }
75 
76 //-----------------------------------------------------------------------------------------------//
77 unsigned int SiStripBadChannelPatcher::fedFromDetId(const uint32_t& detid) {
78  // For the cabled det_id retrieve the FEDid
79  const std::vector<const FedChannelConnection*>& conns = detCabling_->getConnections(detid);
80  if (!(conns.size())) {
81  edm::LogWarning("SiStripBadChannelPatcher")
82  << " DetId " << detid << " appears to be uncabled, returning NOT_A_FEDID !";
83  return sistrip::NOT_A_FEDID;
84  }
85  unsigned int lFedId = sistrip::NOT_A_FEDID;
86  for (uint32_t ch = 0; ch < conns.size(); ch++) {
87  if (conns[ch] && conns[ch]->isConnected()) {
88  lFedId = conns[ch]->fedId();
89  LogDebug("SiStripBadChannelPatcher") << "obtained FED id " << ch << " " << lFedId;
90  if (lFedId < sistrip::FED_ID_MIN || lFedId > sistrip::FED_ID_MAX) {
91  edm::LogWarning("SiStripBadChannelPatcher") << lFedId << " for detid " << detid << " connection " << ch;
92  continue;
93  } else {
94  break;
95  }
96  }
97  }
98  return lFedId;
99 }
100 
101 //-----------------------------------------------------------------------------------------------//
102 void SiStripBadChannelPatcher::addDetIdsFromExcludedFEDs(const std::vector<uint32_t>& allDetIds) {
103  for (const auto& detid : allDetIds) {
104  const auto& currentFED = this->fedFromDetId(detid);
105  if (std::count(fedsToInclude_.begin(), fedsToInclude_.end(), currentFED)) {
106  detIdsToInclude_.push_back(detid);
107  }
108  }
109 }
110 
111 //-----------------------------------------------------------------------------------------------//
113  using namespace edm;
114 
115  // create the patched bad strips payload
116  std::unique_ptr<SiStripBadStrip> theBadStrips = this->getNewObject(iSetup);
117 
118  // write out the BadStrip record
120 
121  if (poolDbService.isAvailable()) {
122  poolDbService->writeOneIOV(*theBadStrips, poolDbService->currentTime(), record_);
123  } else {
124  throw std::runtime_error("PoolDBService required.");
125  }
126 }
127 
128 //-----------------------------------------------------------------------------------------------//
129 std::unique_ptr<SiStripBadStrip> SiStripBadChannelPatcher::getNewObject(const edm::EventSetup& iSetup) {
130  edm::LogInfo("SiStripBadChannelPatcher") << "... creating dummy SiStripBadStrip Data";
131 
132  // this is the output object
133  auto obj = std::make_unique<SiStripBadStrip>();
134 
135  // get the cabling object
136  detCabling_ = &iSetup.getData(cablingToken_);
137 
138  // get the strips to add
139  std::vector<uint32_t> detids;
140  const auto& payload = iSetup.getData(badStripToken_);
141  payload.getDetIds(detids);
142 
143  // copy the exisiting channels (excluding the ones to remove)
144  for (const auto& id : detids) {
145  // check on the detids to exclude
146  if (std::count(detIdsToExclude_.begin(), detIdsToExclude_.end(), id)) {
147  edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO EXCLUDE DETID: " << id;
148  continue;
149  } else {
150  LogDebug("SiStripBadChannelPatcher") << "I AM GOING TO KEEP DETID: " << id;
151  }
152 
153  // check on the FEDs to exclude
154  const auto& currentFED = this->fedFromDetId(id);
155  if (std::count(fedsToExclude_.begin(), fedsToExclude_.end(), currentFED)) {
156  edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO EXCLUDE DETID: " << id;
157  continue;
158  } else {
159  LogDebug("SiStripBadChannelPatcher") << "I AM GOING TO KEEP DETID: " << id;
160  }
161 
162  SiStripBadStrip::Range range = payload.getRange(id);
163  std::vector<unsigned int> theSiStripVector;
164  unsigned int theBadStripRange;
165  for (std::vector<unsigned int>::const_iterator badStrip = range.first; badStrip != range.second; ++badStrip) {
166  unsigned short firstBadStrip = payload.decode(*badStrip).firstStrip;
167  unsigned short nConsecutiveBadStrips = payload.decode(*badStrip).range;
168  theBadStripRange = obj->encode(firstBadStrip, nConsecutiveBadStrips);
169  theSiStripVector.push_back(theBadStripRange);
170  }
171  SiStripBadStrip::Range outRange(theSiStripVector.begin(), theSiStripVector.end());
172  if (!obj->put(id, outRange))
173  edm::LogError("SiStripBadChannelPatcher") << "[SiStripBadChannelPatcher::analyze] detid already exists";
174  } // loop on the detids of the original payload
175 
176  const auto& detInfo =
178 
179  // add to the list of DetIds to include also the one from the list of FEDs
180  const std::vector<uint32_t>& allDetIds = detInfo.getAllDetIds();
181  this->addDetIdsFromExcludedFEDs(allDetIds);
182 
183  // add more full bad detids
184  if (!detIdsToInclude_.empty()) {
185  edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO ADD MORE DETIDS";
186 
187  std::stringstream ss;
188  for (const auto& detid : detIdsToInclude_) {
189  edm::LogInfo("SiStripBadChannelPatcher") << "I AM GOING TO ADD DETID: " << detid;
190  const auto& nAPVs = detInfo.getNumberOfApvsAndStripLength(detid).first;
191 
192  std::vector<unsigned int> theSiStripVector;
193  unsigned short firstBadStrip{0}, nConsecutiveBadStrips{0};
194  unsigned int theBadStripRange;
195 
196  for (unsigned int n = 0; n < nAPVs; n++) {
197  firstBadStrip = n * sistrip::STRIPS_PER_APV;
198  nConsecutiveBadStrips = sistrip::STRIPS_PER_APV;
199  theBadStripRange = obj->encode(firstBadStrip, nConsecutiveBadStrips);
200 
201  if (printDebug_) {
202  ss << "detid " << detid << " \t"
203  << " firstBadStrip " << firstBadStrip << "\t "
204  << " nConsecutiveBadStrips " << nConsecutiveBadStrips << "\t "
205  << " packed integer " << std::hex << theBadStripRange << std::dec << std::endl;
206  }
207 
208  theSiStripVector.push_back(theBadStripRange);
209  }
210 
211  SiStripBadStrip::Range outRange(theSiStripVector.begin(), theSiStripVector.end());
212  if (!obj->put(detid, outRange))
213  edm::LogError("SiStripBadChannelPatcher") << "[SiStripBadChannelPatcher::analyze] detid already exists";
214  } // loop on detids to include
215 
216  if (printDebug_) {
217  // print the added strips
218  edm::LogInfo("SiStripBadChannelPatcher") << ss.str();
219  }
220 
221  } // if there is any new channel to append
222 
223  return obj;
224 }
225 
228 
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
void addDetIdsFromExcludedFEDs(const std::vector< uint32_t > &allDetIds)
const std::vector< const FedChannelConnection * > & getConnections(uint32_t det_id) const
const edm::ESGetToken< SiStripBadStrip, SiStripBadStripRcd > badStripToken_
unsigned int fedFromDetId(const uint32_t &detid)
std::vector< unsigned int > fedsToExclude_
sistrip classes
int iEvent
Definition: GenABIO.cc:224
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
SiStripDetInfo read(std::string filePath)
bool getData(T &iHolder) const
Definition: EventSetup.h:122
Log< level::Info, false > LogInfo
~SiStripBadChannelPatcher() override=default
Constants and enumerated types for FED/FEC systems.
static const uint16_t NOT_A_FEDID
HLT enums.
std::vector< uint32_t > detIdsToExclude_
SiStripBadChannelPatcher(const edm::ParameterSet &iConfig)
std::unique_ptr< SiStripBadStrip > getNewObject(const edm::EventSetup &iSetup)
static const uint16_t STRIPS_PER_APV
std::pair< ContainerIterator, ContainerIterator > Range
static constexpr char const *const kDefaultFile
bool isAvailable() const
Definition: Service.h:40
static const uint16_t FED_ID_MAX
Log< level::Warning, false > LogWarning
std::vector< unsigned int > fedsToInclude_
std::vector< uint32_t > detIdsToInclude_
const std::vector< uint32_t > & getAllDetIds() const noexcept
const edm::ESGetToken< SiStripDetCabling, SiStripDetCablingRcd > cablingToken_
void analyze(const edm::Event &, const edm::EventSetup &) override
const SiStripDetCabling * detCabling_
#define LogDebug(id)