CMS 3D CMS Logo

SiStripFedCabling_PayloadInspector.cc
Go to the documentation of this file.
1 
12 
16 
21 
22 #include <fmt/format.h>
23 #include <memory>
24 #include <sstream>
25 
26 #include "TCanvas.h"
27 #include "TH2D.h"
28 #include "TLatex.h"
29 
30 namespace {
31 
32  using namespace cond::payloadInspector;
33 
34  /************************************************
35  TrackerMap of SiStrip FED Cabling
36  *************************************************/
37  class SiStripFedCabling_TrackerMap : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
38  public:
39  SiStripFedCabling_TrackerMap() : PlotImage<SiStripFedCabling, SINGLE_IOV>("Tracker Map SiStrip Fed Cabling") {}
40 
41  bool fill() override {
42  auto tag = PlotBase::getTag<0>();
43  auto iov = tag.iovs.front();
44  std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
45 
46  std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling");
47  tmap->setPalette(1);
48  std::string titleMap = "TrackerMap of SiStrip Fed Cabling per module, IOV : " + std::to_string(std::get<0>(iov));
49  tmap->setTitle(titleMap);
50 
52  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
53  std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
54 
55  std::vector<uint32_t> activeDetIds;
56  detCabling_->addActiveDetectorsRawIds(activeDetIds);
57 
58  for (const auto& detId : activeDetIds) {
59  int32_t n_conn = 0;
60  for (uint32_t connDet_i = 0; connDet_i < detCabling_->getConnections(detId).size(); connDet_i++) {
61  if (detCabling_->getConnections(detId)[connDet_i] != nullptr &&
62  detCabling_->getConnections(detId)[connDet_i]->isConnected() != 0)
63  n_conn++;
64  }
65  if (n_conn != 0) {
66  tmap->fill(detId, n_conn * 2);
67  }
68  }
69 
70  std::string fileName(m_imageFileName);
71  tmap->save(true, 0., 6., fileName, 4500, 2400); // max 6 APVs per module
72 
73  return true;
74  }
75  };
76 
77  /************************************************
78  TrackerMap of uncabled modules
79  *************************************************/
80  class SiStripUncabledChannels_TrackerMap : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
81  public:
82  SiStripUncabledChannels_TrackerMap()
83  : PlotImage<SiStripFedCabling, SINGLE_IOV>("Tracker Map SiStrip Fed Cabling") {}
84 
85  bool fill() override {
86  auto tag = PlotBase::getTag<0>();
87  auto iov = tag.iovs.front();
88  std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
89 
90  std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling");
91  tmap->setPalette(1);
92  std::string titleMap =
93  "TrackerMap of SiStrip Fraction of uncabled channels per module, IOV : " + std::to_string(std::get<0>(iov));
94  tmap->setTitle(titleMap);
95 
97  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
98  std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
99 
100  std::vector<uint32_t> activeDetIds;
101  detCabling_->addActiveDetectorsRawIds(activeDetIds);
102 
103  const auto detInfo =
105  std::vector<uint32_t> all_detids = detInfo.getAllDetIds();
106 
107  // first add the fully unconnected modules
108  for (const auto& detId : all_detids) {
109  if (!detCabling_->IsConnected(detId)) {
110  tmap->fill(detId, 1);
111  }
112  }
113 
114  // then add the partially unconnected ones
115  for (const auto& detId : activeDetIds) {
116  float frac = calculateConnectedFraction(detCabling_.get(), detId);
117  if (frac != 1.f) {
118  tmap->fill(detId, frac);
119  }
120  }
121 
122  std::string fileName(m_imageFileName);
123  tmap->save(true, 0., 1., fileName, 4500, 2400);
124 
125  return true;
126  }
127 
128  private:
129  // Function to calculate the number of connections for a given detId
130  int32_t calculateConnectedFraction(const SiStripDetCabling* detCabling, const uint32_t detId) {
131  float totAPVs = detCabling->nApvPairs(detId);
132  float n_conn{0};
133  for (uint32_t connDet_i = 0; connDet_i < detCabling->getConnections(detId).size(); connDet_i++) {
134  if (detCabling->getConnections(detId)[connDet_i] != nullptr &&
135  detCabling->getConnections(detId)[connDet_i]->isConnected() != 0) {
136  n_conn++;
137  }
138  }
139  return n_conn / totAPVs;
140  }
141  };
142 
143  /************************************************
144  TrackerMap of SiStrip FED Cabling difference between 2 payloads
145  *************************************************/
146 
147  template <int ntags, IOVMultiplicity nIOVs>
148  class SiStripFedCablingComparisonTrackerMapBase : public PlotImage<SiStripFedCabling, nIOVs, ntags> {
149  public:
150  SiStripFedCablingComparisonTrackerMapBase()
151  : PlotImage<SiStripFedCabling, nIOVs, ntags>("Tracker Map SiStrip Fed Cabling difference") {}
152 
153  bool fill() override {
154  // trick to deal with the multi-ioved tag and two tag case at the same time
155  auto theIOVs = PlotBase::getTag<0>().iovs;
156  auto tagname1 = PlotBase::getTag<0>().name;
157  std::string tagname2 = "";
158  auto firstiov = theIOVs.front();
159  std::tuple<cond::Time_t, cond::Hash> lastiov;
160 
161  // we don't support (yet) comparison with more than 2 tags
162  assert(this->m_plotAnnotations.ntags < 3);
163 
164  if (this->m_plotAnnotations.ntags == 2) {
165  auto tag2iovs = PlotBase::getTag<1>().iovs;
166  tagname2 = PlotBase::getTag<1>().name;
167  lastiov = tag2iovs.front();
168  } else {
169  lastiov = theIOVs.back();
170  }
171 
172  std::shared_ptr<SiStripFedCabling> last_payload = this->fetchPayload(std::get<1>(lastiov));
173  std::shared_ptr<SiStripFedCabling> first_payload = this->fetchPayload(std::get<1>(firstiov));
174 
175  std::string lastIOVsince = std::to_string(std::get<0>(lastiov));
176  std::string firstIOVsince = std::to_string(std::get<0>(firstiov));
177 
178  std::unique_ptr<TrackerMap> tmap = std::make_unique<TrackerMap>("SiStripFedCabling Difference");
179  tmap->setPalette(1);
180 
181  std::string titleMap{};
182  std::string commonPart = "SiStrip Fed Cabling Map: #Delta connections per module";
183  if (this->m_plotAnnotations.ntags == 2) {
184  titleMap = fmt::format("{}: {} - {}", commonPart, tagname2, tagname1);
185  } else {
186  titleMap = fmt::format("{}: IOV : {} - {}", commonPart, std::get<0>(lastiov), std::get<0>(firstiov));
187  }
188  tmap->setTitle(titleMap);
189 
190  const std::string k_TrackerParams =
191  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath();
193 
194  std::unique_ptr<SiStripDetCabling> l_detCabling =
195  std::make_unique<SiStripDetCabling>(*(last_payload.get()), &tTopo);
196  std::unique_ptr<SiStripDetCabling> f_detCabling =
197  std::make_unique<SiStripDetCabling>(*(first_payload.get()), &tTopo);
198 
199  std::vector<uint32_t> f_activeDetIds;
200  f_detCabling->addActiveDetectorsRawIds(f_activeDetIds);
201 
202  std::vector<uint32_t> l_activeDetIds;
203  l_detCabling->addActiveDetectorsRawIds(l_activeDetIds);
204 
205  const auto& setsToPlot = prepareSets(f_activeDetIds, l_activeDetIds);
206 
207  edm::LogPrint("SiStripFedCablingComparisonTrackerMapBase")
208  << "Common Detids: " << setsToPlot.commonElements.size()
209  << " | only in last payload: " << setsToPlot.lExclusiveElements.size()
210  << " | only in first payload: " << setsToPlot.fExclusiveElements.size() << std::endl;
211 
212  // Process common elements
213  for (const auto& detId : setsToPlot.commonElements) {
214  int32_t f_n_conn = calculateConnections(f_detCabling.get(), detId);
215  int32_t l_n_conn = calculateConnections(l_detCabling.get(), detId);
216 
217  if (l_n_conn != f_n_conn) {
218  tmap->fill(detId, (l_n_conn - f_n_conn) * 2); // 2 APVs per channel
219  }
220  }
221 
222  // Process elements only in the last
223  for (const auto& detId : setsToPlot.lExclusiveElements) {
224  int32_t l_n_conn = calculateConnections(l_detCabling.get(), detId);
225 
226  if (l_n_conn != 0) {
227  tmap->fill(detId, l_n_conn * 2); // 2 APVs per channel
228  }
229  }
230 
231  // Process elements only in the first
232  for (const auto& detId : setsToPlot.fExclusiveElements) {
233  int32_t f_n_conn = calculateConnections(f_detCabling.get(), detId);
234 
235  if (f_n_conn != 0) {
236  tmap->fill(detId, -f_n_conn * 2); // 2 APVs per channel
237  }
238  }
239 
240  std::string fileName(this->m_imageFileName);
241  tmap->save(true, -6., 6., fileName, 4500, 2400); // max 6 APVs per module
242 
243  return true;
244  }
245 
246  private:
247  struct setsOfDetids {
248  std::vector<uint32_t> commonElements;
249  std::vector<uint32_t> fExclusiveElements;
250  std::vector<uint32_t> lExclusiveElements;
251  };
252 
253  // Function to calculate the number of connections for a given detId
254  int32_t calculateConnections(const SiStripDetCabling* detCabling, const uint32_t detId) {
255  int32_t n_conn = 0;
256  for (uint32_t connDet_i = 0; connDet_i < detCabling->getConnections(detId).size(); connDet_i++) {
257  if (detCabling->getConnections(detId)[connDet_i] != nullptr &&
258  detCabling->getConnections(detId)[connDet_i]->isConnected() != 0) {
259  n_conn++;
260  }
261  }
262  return n_conn;
263  }
264 
265  // Function to calculate the interesection and exclusive elements out of two vectors of DetIds
266  setsOfDetids prepareSets(std::vector<uint32_t> f_activeDetIds, std::vector<uint32_t> l_activeDetIds) {
267  setsOfDetids output;
268  // Sort the input vectors if they are not already sorted
269  std::sort(f_activeDetIds.begin(), f_activeDetIds.end());
270  std::sort(l_activeDetIds.begin(), l_activeDetIds.end());
271 
272  // Common elements
273  output.commonElements.reserve(std::min(f_activeDetIds.size(), l_activeDetIds.size()));
274  std::set_intersection(f_activeDetIds.begin(),
275  f_activeDetIds.end(),
276  l_activeDetIds.begin(),
277  l_activeDetIds.end(),
278  std::back_inserter(output.commonElements));
279 
280  // Elements only in f_activeDetIds
281  output.fExclusiveElements.reserve(f_activeDetIds.size() - output.commonElements.size());
282  std::set_difference(f_activeDetIds.begin(),
283  f_activeDetIds.end(),
284  l_activeDetIds.begin(),
285  l_activeDetIds.end(),
286  std::back_inserter(output.fExclusiveElements));
287 
288  // Elements only in l_activeDetIds
289  output.lExclusiveElements.reserve(l_activeDetIds.size() - output.commonElements.size());
290  std::set_difference(l_activeDetIds.begin(),
291  l_activeDetIds.end(),
292  f_activeDetIds.begin(),
293  f_activeDetIds.end(),
294  std::back_inserter(output.lExclusiveElements));
295 
296  return output;
297  }
298  };
299 
300  using SiStripFedCablingComparisonTrackerMapSingleTag = SiStripFedCablingComparisonTrackerMapBase<1, MULTI_IOV>;
301  using SiStripFedCablingComparisonTrackerMapTwoTags = SiStripFedCablingComparisonTrackerMapBase<2, SINGLE_IOV>;
302 
303  /************************************************
304  Summary Plot of SiStrip FED Cabling
305  *************************************************/
306  class SiStripFedCabling_Summary : public PlotImage<SiStripFedCabling, SINGLE_IOV> {
307  public:
308  SiStripFedCabling_Summary() : PlotImage<SiStripFedCabling, SINGLE_IOV>("SiStrip Fed Cabling Summary") {}
309 
310  bool fill() override {
311  auto tag = PlotBase::getTag<0>();
312  auto iov = tag.iovs.front();
313  std::shared_ptr<SiStripFedCabling> payload = fetchPayload(std::get<1>(iov));
314  int IOV = std::get<0>(iov);
315  std::vector<uint32_t> activeDetIds;
316 
318  edm::FileInPath("Geometry/TrackerCommonData/data/trackerParameters.xml").fullPath());
319  std::unique_ptr<SiStripDetCabling> detCabling_ = std::make_unique<SiStripDetCabling>(*(payload.get()), &tTopo);
320 
321  detCabling_->addActiveDetectorsRawIds(activeDetIds);
322 
323  containers myCont;
324  containers allCounts;
325 
326  const auto detInfo =
328  for (const auto& it : detInfo.getAllData()) {
329  // check if det id is correct and if it is actually cabled in the detector
330  if (it.first == 0 || it.first == 0xFFFFFFFF) {
331  edm::LogError("DetIdNotGood") << "@SUB=analyze"
332  << "Wrong det id: " << it.first << " ... neglecting!" << std::endl;
333  continue;
334  }
335  updateCounters(it.first, allCounts, tTopo);
336  }
337 
338  for (const auto& detId : activeDetIds) {
339  updateCounters(detId, myCont, tTopo);
340  }
341 
342  TH2D* ME = new TH2D("SummaryOfCabling", "SummaryOfCabling", 6, 0.5, 6.5, 9, 0.5, 9.5);
343  ME->GetXaxis()->SetTitle("Sub Det");
344  ME->GetYaxis()->SetTitle("Layer");
345 
346  ME->SetTitle("");
347 
348  ME->GetXaxis()->SetBinLabel(1, "TIB");
349  ME->GetXaxis()->SetBinLabel(2, "TID F");
350  ME->GetXaxis()->SetBinLabel(3, "TID B");
351  ME->GetXaxis()->SetBinLabel(4, "TOB");
352  ME->GetXaxis()->SetBinLabel(5, "TEC F");
353  ME->GetXaxis()->SetBinLabel(6, "TEC B");
354 
355  for (int i = 0; i < 4; i++) {
356  ME->Fill(1, i + 1, float(myCont.counterTIB[i]) / allCounts.counterTIB[i]);
357  }
358 
359  for (int i = 0; i < 2; i++) {
360  for (int j = 0; j < 3; j++) {
361  ME->Fill(i + 2, j + 1, float(myCont.counterTID[i][j]) / allCounts.counterTID[i][j]);
362  }
363  }
364 
365  for (int i = 0; i < 6; i++) {
366  ME->Fill(4, i + 1, float(myCont.counterTOB[i]) / allCounts.counterTOB[i]);
367  }
368 
369  for (int i = 0; i < 2; i++) {
370  for (int j = 0; j < 9; j++) {
371  ME->Fill(i + 5, j + 1, float(myCont.counterTEC[i][j]) / allCounts.counterTEC[i][j]);
372  }
373  }
374 
375  TCanvas c1("SiStrip FED cabling summary", "SiStrip FED cabling summary", 800, 600);
376  c1.SetTopMargin(0.07);
377  c1.SetBottomMargin(0.10);
378  c1.SetLeftMargin(0.07);
379  c1.SetRightMargin(0.10);
380 
381  ME->Draw("colz");
382  ME->Draw("TEXTsame");
383  ME->SetStats(kFALSE);
384 
385  TLatex t1;
386  t1.SetNDC();
387  t1.SetTextAlign(26);
388  t1.SetTextSize(0.05);
389  t1.DrawLatex(0.5, 0.96, Form("SiStrip FedCabling, IOV %i", IOV));
390 
391  std::string fileName(m_imageFileName);
392  c1.SaveAs(fileName.c_str());
393 
394  return true;
395  }
396 
397  private:
398  struct containers {
399  public:
400  int counterTIB[4] = {0};
401  int counterTID[2][3] = {{0}};
402  int counterTOB[6] = {0};
403  int counterTEC[2][9] = {{0}};
404  };
405 
406  void updateCounters(int detId, containers& cont, const TrackerTopology& tTopo) {
407  StripSubdetector subdet(detId);
408 
409  switch (subdet.subdetId()) {
410  case StripSubdetector::TIB: {
411  int i = tTopo.tibLayer(detId) - 1;
412  cont.counterTIB[i]++;
413  break;
414  }
415  case StripSubdetector::TID: {
416  int j = tTopo.tidWheel(detId) - 1;
417  int side = tTopo.tidSide(detId);
418  if (side == 2) {
419  cont.counterTID[0][j]++;
420  } else if (side == 1) {
421  cont.counterTID[1][j]++;
422  }
423  break;
424  }
425  case StripSubdetector::TOB: {
426  int i = tTopo.tobLayer(detId) - 1;
427  cont.counterTOB[i]++;
428  break;
429  }
430  case StripSubdetector::TEC: {
431  int j = tTopo.tecWheel(detId) - 1;
432  int side = tTopo.tecSide(detId);
433  if (side == 2) {
434  cont.counterTEC[0][j]++;
435  } else if (side == 1) {
436  cont.counterTEC[1][j]++;
437  }
438  break;
439  }
440  }
441  }
442  };
443 
444 } // namespace
445 
447  PAYLOAD_INSPECTOR_CLASS(SiStripFedCabling_TrackerMap);
448  PAYLOAD_INSPECTOR_CLASS(SiStripUncabledChannels_TrackerMap);
449  PAYLOAD_INSPECTOR_CLASS(SiStripFedCablingComparisonTrackerMapSingleTag);
450  PAYLOAD_INSPECTOR_CLASS(SiStripFedCablingComparisonTrackerMapTwoTags);
451  PAYLOAD_INSPECTOR_CLASS(SiStripFedCabling_Summary);
452 }
static constexpr auto TEC
unsigned int tobLayer(const DetId &id) const
const std::vector< const FedChannelConnection * > & getConnections(uint32_t det_id) const
std::string fullPath() const
Definition: FileInPath.cc:161
unsigned int tidSide(const DetId &id) const
unsigned int tidWheel(const DetId &id) const
unsigned int tecWheel(const DetId &id) const
Log< level::Error, false > LogError
assert(be >=bs)
static std::string to_string(const XMLCh *ch)
Definition: ME.h:11
#define PAYLOAD_INSPECTOR_CLASS(CLASS_NAME)
unsigned int tecSide(const DetId &id) const
double f[11][100]
SiStripDetInfo read(std::string filePath)
static constexpr auto TOB
#define PAYLOAD_INSPECTOR_MODULE(PAYLOAD_TYPENAME)
Log< level::Warning, true > LogPrint
static constexpr auto TIB
Contains cabling info at the device level, including DetId, APV pair numbers, hardware addresses...
TrackerTopology fromTrackerParametersXMLFile(const std::string &xmlFileName)
static constexpr char const *const kDefaultFile
Definition: output.py:1
unsigned int tibLayer(const DetId &id) const
static constexpr auto TID
std::vector< std::string > set_difference(std::vector< std::string > const &v1, std::vector< std::string > const &v2)
const uint16_t nApvPairs(uint32_t det_id) const
cont
load Luminosity info ##
Definition: generateEDF.py:620
std::vector< std::string > set_intersection(std::vector< std::string > const &v1, std::vector< std::string > const &v2)