CMS 3D CMS Logo

FastSiPixelFEDChannelContainerFromQuality.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondFormats/SiPixelObjects
4 // Class: FastSiPixelFEDChannelContainerFromQuality
5 //
9 //
10 // Original Author: Marco Musich
11 // Created: Tue, 25 Apr 2019 12:04:36 GMT
12 //
13 //
14 
34 
35 #include <iomanip> // std::setw
36 #include <iostream>
37 #include <fstream>
38 #include <sstream>
39 
40 #include <TROOT.h>
41 #include <TSystem.h>
42 #include <TCanvas.h>
43 #include <TFile.h>
44 #include <TLegend.h>
45 #include <TGraph.h>
46 #include <TH1.h>
47 
49  std::pair<unsigned int, unsigned int> unpack(cond::Time_t since) {
50  auto kLowMask = 0XFFFFFFFF;
51  auto run = (since >> 32);
52  auto lumi = (since & kLowMask);
53  return std::make_pair(run, lumi);
54  }
55 } // namespace SiPixelFEDChannelUtils
56 
58 public:
61  void analyze(const edm::Event& evt, const edm::EventSetup& evtSetup) override;
62  void endJob() override;
63  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
65  const SiPixelQuality& theQuality,
66  const SiPixelFedCablingMap& theFedCabling,
67  const SiPixelFedCablingTree& theCablingTree);
68 
69 private:
76 
77  // Specify output text file name. Leave empty if do not want to dump in a file
79 
80  // Manually specify the start/end time.
81  unsigned long long m_startTime;
82  unsigned long long m_endTime;
83 
84  const bool printdebug_;
85  const bool isMC_;
87 
88  std::unique_ptr<SiPixelFEDChannelContainer> myQualities;
89 
90  inline unsigned int closest_from_above(std::vector<unsigned int> const& vec, unsigned int value) {
91  auto const it = std::lower_bound(vec.begin(), vec.end(), value);
92  return vec.at(it - vec.begin() - 1);
93  }
94 
95  inline unsigned int closest_from_below(std::vector<unsigned int> const& vec, unsigned int value) {
96  auto const it = std::upper_bound(vec.begin(), vec.end(), value);
97  return vec.at(it - vec.begin() - 1);
98  }
99 };
100 
102  : m_connectionPool(),
103  m_condDbQuality(iConfig.getParameter<std::string>("condDBQuality")),
104  m_condDbCabling(iConfig.getParameter<std::string>("condDBCabling")),
105  m_QualityTagName(iConfig.getParameter<std::string>("qualityTagName")),
106  m_CablingTagName(iConfig.getParameter<std::string>("cablingMapTagName")),
107  m_record(iConfig.getParameter<std::string>("record")),
108  m_output(iConfig.getParameter<std::string>("output")),
109  m_startTime(iConfig.getParameter<unsigned long long>("startIOV")),
110  m_endTime(iConfig.getParameter<unsigned long long>("endIOV")),
111  printdebug_(iConfig.getUntrackedParameter<bool>("printDebug", false)),
112  isMC_(iConfig.getUntrackedParameter<bool>("isMC", true)),
113  removeEmptyPayloads_(iConfig.getUntrackedParameter<bool>("removeEmptyPayloads", false)) {
116 
117  //now do what ever initialization is needed
118  myQualities = std::make_unique<SiPixelFEDChannelContainer>();
119 }
120 
122 
124  std::stringstream ss;
125 
126  cond::Time_t startIov = m_startTime;
127  cond::Time_t endIov = m_endTime;
128  if (startIov > endIov)
129  throw cms::Exception("endTime must be greater than startTime!");
130  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
131  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
132  << "Set start time " << startIov << "\n ... Set end time " << endIov;
133 
134  // open db session for the quality
135  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
136  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
137  << "Query the condition database " << m_condDbQuality;
138 
140  condDbSession.transaction().start(true);
141 
142  // query the database
143  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
144  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
145  << "Reading IOVs from tag " << m_QualityTagName;
146 
147  // open db session for the cabling map
148  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
149  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
150  << "Query the condition database " << m_condDbCabling;
151 
153  condDbSession2.transaction().start(true);
154 
155  // query the database
156  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
157  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
158  << "Reading IOVs from tag " << m_CablingTagName;
159 
160  // get the list of payloads for the SiPixelQuality tag
161  std::vector<std::tuple<cond::Time_t, cond::Hash> > m_iovs;
162  condDbSession.readIov(m_QualityTagName).selectRange(startIov, endIov, m_iovs);
163 
166 
167  // get the list of payloads for the Cabling Map
168  std::vector<std::tuple<cond::Time_t, cond::Hash> > m_cabling_iovs;
169  condDbSession2.readIov(m_CablingTagName).selectRange(MIN_VAL, MAX_VAL, m_cabling_iovs);
170 
171  // create here the unpacked list of IOVs (run numbers)
172  std::vector<unsigned int> listOfIOVs;
173  std::transform(m_iovs.begin(),
174  m_iovs.end(),
175  std::back_inserter(listOfIOVs),
176  [](std::tuple<cond::Time_t, cond::Hash> myIOV) -> unsigned int {
177  return SiPixelFEDChannelUtils::unpack(std::get<0>(myIOV)).first;
178  });
179 
180  std::vector<unsigned int> listOfCablingIOVs;
181  std::transform(m_cabling_iovs.begin(),
182  m_cabling_iovs.end(),
183  std::back_inserter(listOfCablingIOVs),
184  [](std::tuple<cond::Time_t, cond::Hash> myIOV2) -> unsigned int { return std::get<0>(myIOV2); });
185 
186  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
187  << " Number of SiPixelQuality paloyads to analyze: " << listOfIOVs.size()
188  << " Number of SiPixelFedCablngMap payloads: " << listOfCablingIOVs.size() << std::endl;
189 
190  if (listOfCablingIOVs.size() > 1) {
191  if (closest_from_below(listOfCablingIOVs, listOfIOVs.front()) !=
192  closest_from_above(listOfCablingIOVs, listOfIOVs.back())) {
193  throw cms::Exception("") << " The Pixel FED Cabling map does not cover all the requested SiPixelQuality IOVs in "
194  "the same interval of validity \n";
195  }
196  } else {
197  if (listOfIOVs.front() < listOfCablingIOVs.front()) {
198  throw cms::Exception("") << " The Pixel FED Cabling map does not cover all the requested IOVs \n";
199  }
200  }
201 
202  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
203  << " First run covered by SiPixelQuality tag: " << listOfIOVs.front()
204  << " / last run covered by SiPixelQuality tag: " << listOfIOVs.back() << std::endl;
205 
206  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << " SiPixel Cabling Map IOVs in the interval: ";
207  for (const auto& cb : m_cabling_iovs) {
208  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
209  << " " << std::setw(6) << std::get<0>(cb) << " : " << std::get<1>(cb);
210  }
211  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << std::endl;
212 
213  if (printdebug_) {
214  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
215  << " closest_from_above(listOfCablingIOVs,listOfIOVs.back()): "
216  << closest_from_above(listOfCablingIOVs, listOfIOVs.back()) << std::endl;
217  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
218  << " closest_from_below(listOfCablingIOVs,listOfIOVs.front()): "
219  << closest_from_below(listOfCablingIOVs, listOfIOVs.front()) << std::endl;
220  }
221 
222  auto it = std::find(
223  listOfCablingIOVs.begin(), listOfCablingIOVs.end(), closest_from_below(listOfCablingIOVs, listOfIOVs.front()));
224  int index = std::distance(listOfCablingIOVs.begin(), it);
225 
226  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
227  << " using the SiPixelFedCablingMap with hash: " << std::get<1>(m_cabling_iovs.at(index)) << std::endl;
228 
229  auto theCablingMapPayload = condDbSession2.fetchPayload<SiPixelFedCablingMap>(std::get<1>(m_cabling_iovs.at(index)));
230  auto theCablingTree = (*theCablingMapPayload).cablingTree();
231 
232  printf(
233  "Progressing Bar :0%% 20%% 40%% 60%% 80%% 100%%\n");
234  printf("Translating into SiPixelFEDChannelCollection :");
235  int step = (m_iovs.size() >= 50) ? m_iovs.size() / 50 : 1;
236  int niov = 0;
237  for (const auto& myIOV : m_iovs) {
238  if (niov % step == 0) {
239  printf(".");
240  fflush(stdout);
241  }
242 
243  auto payload = condDbSession.fetchPayload<SiPixelQuality>(std::get<1>(myIOV));
244  auto runLS = SiPixelFEDChannelUtils::unpack(std::get<0>(myIOV));
245 
246  // print IOVs summary
247  ss << runLS.first << "," << runLS.second << " (" << std::get<0>(myIOV) << ")"
248  << " [hash: " << std::get<1>(myIOV) << "] \n";
249 
250  std::string scenario = std::to_string(runLS.first) + "_" + std::to_string(runLS.second);
251 
252  if (printdebug_) {
253  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
254  << "Found IOV:" << runLS.first << "(" << runLS.second << ")" << std::endl;
255  }
256 
257  auto theSiPixelFEDChannelCollection =
258  this->createFromSiPixelQuality(*payload, *theCablingMapPayload, *theCablingTree);
259 
260  if (removeEmptyPayloads_ && theSiPixelFEDChannelCollection.empty())
261  return;
262 
263  myQualities->setScenario(scenario, theSiPixelFEDChannelCollection);
264 
265  ++niov;
266  }
267  //carriage return for tests
268  printf("\n\n");
269 
270  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
271  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] "
272  << "Read " << niov << " IOVs from tag " << m_QualityTagName
273  << " corresponding to the specified time interval.\n\n"
274  << ss.str();
275 
276  if (printdebug_) {
277  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
278  << "[FastSiPixelFEDChannelContainerFromQuality::" << __func__ << "] " << ss.str();
279  }
280 
281  condDbSession.transaction().commit();
282  condDbSession2.transaction().commit();
283 
284  if (!m_output.empty()) {
285  std::ofstream fout;
286  fout.open(m_output);
287  fout << ss.str();
288  fout.close();
289  }
290 }
291 
292 // ------------ method called once each job just before starting event loop ------------
295  const SiPixelFedCablingMap& theFedCabling,
296  const SiPixelFedCablingTree& theCablingTree) {
297  auto fedid_ = theFedCabling.det2fedMap();
298 
300 
301  auto theDisabledModules = theQuality.getBadComponentList();
302  for (const auto& mod : theDisabledModules) {
303  //mod.DetID, mod.errorType,mod.BadRocs
304 
305  int coded_badRocs = mod.BadRocs;
306  std::vector<PixelFEDChannel> disabledChannelsDetSet;
307  std::vector<sipixelobjects::CablingPathToDetUnit> path = theFedCabling.pathToDetUnit(mod.DetID);
308  unsigned int nrocs_inLink(0);
309  if (!path.empty()) {
310  const sipixelobjects::PixelFEDCabling* aFed = theCablingTree.fed(path.at(0).fed);
311  const sipixelobjects::PixelFEDLink* link = aFed->link(path.at(0).link);
312  nrocs_inLink = link->numberOfROCs();
313  } else {
314  throw cms::Exception("Inconsistent data") << "could not find CablingPathToDetUnit for detId:" << mod.DetID;
315  }
316 
317  std::bitset<16> bad_rocs(coded_badRocs);
318  unsigned int n_ch = bad_rocs.size() / nrocs_inLink;
319 
320  for (unsigned int i_roc = 0; i_roc < n_ch; ++i_roc) {
321  unsigned int first_idx = nrocs_inLink * i_roc;
322  unsigned int sec_idx = nrocs_inLink * (i_roc + 1) - 1;
323  unsigned int mask = pow(2, nrocs_inLink) - 1;
324  unsigned int n_setbits = (coded_badRocs >> (i_roc * nrocs_inLink)) & mask;
325 
326  if (n_setbits == 0) {
327  continue;
328  }
329 
330  if (n_setbits != mask) {
331  if (printdebug_) {
332  edm::LogWarning("FastSiPixelFEDChannelContainerFromQuality")
333  << "Mismatch! DetId: " << mod.DetID << " " << n_setbits << " " << mask << std::endl;
334  }
335  continue;
336  }
337 
338  if (printdebug_) {
339  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality") << "passed" << std::endl;
340  }
341 
342  unsigned int link_id = 99999;
343  unsigned int fed_id = 99999;
344 
345  for (auto const& p : path) {
346  const sipixelobjects::PixelFEDCabling* aFed = theCablingTree.fed(p.fed);
347  const sipixelobjects::PixelFEDLink* link = aFed->link(p.link);
348  const sipixelobjects::PixelROC* roc = link->roc(p.roc);
349  unsigned int first_roc = roc->idInDetUnit();
350 
351  if (first_roc == first_idx) {
352  link_id = p.link;
353  fed_id = p.fed;
354  break;
355  }
356  }
357 
358  if (printdebug_) {
359  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
360  << " " << fed_id << " " << link_id << " " << first_idx << " " << sec_idx << std::endl;
361  }
362 
363  PixelFEDChannel ch = {fed_id, link_id, first_idx, sec_idx};
364  disabledChannelsDetSet.push_back(ch);
365 
366  if (printdebug_) {
367  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
368  << i_roc << " " << coded_badRocs << " " << first_idx << " " << sec_idx << std::endl;
369  edm::LogVerbatim("FastSiPixelFEDChannelContainerFromQuality")
370  << "=======================================" << std::endl;
371  }
372  }
373 
374  if (!disabledChannelsDetSet.empty()) {
375  theBadChannelCollection[mod.DetID] = disabledChannelsDetSet;
376  }
377  }
378  return theBadChannelCollection;
379 }
380 
383  desc.setComment("Writes payloads of type SiPixelFEDChannelContainer");
384  desc.addUntracked<bool>("printDebug", false);
385  desc.addUntracked<bool>("removeEmptyPayloads", false);
386  desc.add<std::string>("record", "SiPixelStatusScenariosRcd");
387  desc.add<std::string>("condDBQuality", "frontier://FrontierPrep/CMS_CONDITIONS");
388  desc.add<std::string>("qualityTagName", "SiPixelQualityOffline_2017_threshold1percent_stuckTBM");
389  desc.add<std::string>("condDBCabling", "frontier://FrontierProd/CMS_CONDITIONS");
390  desc.add<std::string>("cablingMapTagName", "SiPixelFedCablingMap_v1");
391  desc.add<unsigned long long>("startIOV", 1310841198608821);
392  desc.add<unsigned long long>("endIOV", 1312696624480350);
393  desc.add<std::string>("output", "summary.txt");
394  desc.add<std::string>("connect", "");
395 
396  edm::ParameterSetDescription descDBParameters;
397  descDBParameters.addUntracked<std::string>("authenticationPath", "");
398  descDBParameters.addUntracked<int>("authenticationSystem", 0);
399  descDBParameters.addUntracked<std::string>("security", "");
400  descDBParameters.addUntracked<int>("messageLevel", 0);
401 
402  desc.add<edm::ParameterSetDescription>("DBParameters", descDBParameters);
403  descriptions.add("FastSiPixelFEDChannelContainerFromQuality", desc);
404 }
405 
407  // edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")<<"Analyzed "<<IOVcount_<<" IOVs"<<std::endl;
408  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality")
409  << "Size of SiPixelFEDChannelContainer object " << myQualities->size() << std::endl
410  << std::endl;
411 
412  if (printdebug_) {
413  edm::LogInfo("FastSiPixelFEDChannelContainerFromQuality") << "Content of SiPixelFEDChannelContainer " << std::endl;
414 
415  // use built-in method in the CondFormat
416  myQualities->printAll();
417  }
418 
419  // Form the data here
421  if (poolDbService.isAvailable()) {
422  cond::Time_t valid_time = poolDbService->currentTime();
423  // this writes the payload to begin in current run defined in cfg
424  if (!isMC_) {
425  poolDbService->writeOneIOV(*myQualities, valid_time, m_record);
426  } else {
427  // for MC IOV since=1
428  poolDbService->writeOneIOV(*myQualities, 1, m_record);
429  }
430  }
431 }
432 
const TimeTypeSpecs timeTypeSpecs[]
Definition: Time.cc:16
Log< level::Info, true > LogVerbatim
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
const PixelFEDLink * link(unsigned int id) const
return link identified by id. Link id&#39;s are ranged [1, numberOfLinks]
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
Time_t beginValue
Definition: Time.h:41
void start(bool readOnly=true)
Definition: Session.cc:18
const Time_t MIN_VAL(0)
const Time_t kLowMask(0xFFFFFFFF)
std::string to_string(const V &value)
Definition: OMSAccess.h:71
std::unique_ptr< T > fetchPayload(const cond::Hash &payloadHash)
Definition: Session.h:213
std::vector< sipixelobjects::CablingPathToDetUnit > pathToDetUnit(uint32_t rawDetId) const final
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
Transaction & transaction()
Definition: Session.cc:52
scenario
Definition: constants.h:173
constexpr uint32_t mask
Definition: gpuClustering.h:24
void setParameters(const edm::ParameterSet &connectionPset)
const std::vector< disabledModuleType > getBadComponentList() const
unsigned long long Time_t
Definition: Time.h:14
SiPixelFEDChannelContainer::SiPixelFEDChannelCollection createFromSiPixelQuality(const SiPixelQuality &theQuality, const SiPixelFedCablingMap &theFedCabling, const SiPixelFedCablingTree &theCablingTree)
unsigned int closest_from_below(std::vector< unsigned int > const &vec, unsigned int value)
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
Session createSession(const std::string &connectionString, bool writeCapable=false)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
Definition: value.py:1
void analyze(const edm::Event &evt, const edm::EventSetup &evtSetup) override
IOVProxy readIov(const std::string &tag)
Definition: Session.cc:63
Log< level::Info, false > LogInfo
const PixelFEDCabling * fed(unsigned int idFed) const
get fed identified by its id
std::pair< unsigned int, unsigned int > unpack(cond::Time_t since)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
IOVArray selectRange(const cond::Time_t &begin, const cond::Time_t &end)
Definition: IOVProxy.cc:197
std::unordered_map< uint32_t, unsigned int > det2fedMap() const final
std::unique_ptr< SiPixelFEDChannelContainer > myQualities
bool isAvailable() const
Definition: Service.h:40
step
Definition: StallMonitor.cc:98
Log< level::Warning, false > LogWarning
Time_t endValue
Definition: Time.h:42
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
const Time_t MAX_VAL(std::numeric_limits< Time_t >::max())
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:29
unsigned int closest_from_above(std::vector< unsigned int > const &vec, unsigned int value)
std::map< DetId, std::vector< PixelFEDChannel > > SiPixelFEDChannelCollection
unsigned transform(const HcalDetId &id, unsigned transformCode)