CMS 3D CMS Logo

SiStripTkMaps.cc
Go to the documentation of this file.
1 // CMSSW includes
9 
10 // ROOT includes
11 #include "TArrow.h"
12 #include "TPaletteAxis.h"
13 #include "TGaxis.h"
14 #include "TCanvas.h"
15 #include "TColor.h"
16 #include "TGraph.h"
17 #include "TLatex.h"
18 #include "TH2Poly.h"
19 #include "TStyle.h"
20 
21 // STL includes
22 #include <array>
23 #include <fstream>
24 #include <iostream>
25 #include <map>
26 #include <string>
27 #include <vector>
28 
29 // boost includes
30 #include <boost/tokenizer.hpp>
31 #include <boost/range/adaptor/indexed.hpp>
32 
34 
35 //============================================================================
36 void SiStripTkMaps::bookMap(const std::string mapTitle, const std::string zAxisTitle) {
37  double minx = 0xFFFFFF, maxx = -0xFFFFFF, miny = 0xFFFFFF, maxy = -0xFFFFFFF;
38  readVertices(minx, maxx, miny, maxy);
39 
40  // set the titles
41  m_zAxisTitle = zAxisTitle;
42  m_mapTitle = mapTitle;
43 
44  TGaxis::SetMaxDigits(2);
45 
46  // margin of the box
47  static constexpr int margin = 5;
48  m_trackerMap =
49  new TH2Poly("Tracker Map", m_mapTitle.c_str(), minx - margin, maxx + margin, miny - margin, maxy + margin);
50  m_trackerMap->SetFloat();
51  m_trackerMap->SetOption(m_option);
52  m_trackerMap->SetStats(false);
53  m_trackerMap->GetZaxis()->SetLabelSize(0.03);
54  m_trackerMap->GetZaxis()->SetTitleOffset(0.5);
55  m_trackerMap->GetZaxis()->SetTitleSize(0.05);
56  m_trackerMap->GetZaxis()->SetTitle(m_zAxisTitle.c_str());
57  m_trackerMap->GetZaxis()->CenterTitle();
58 
59  // Add all bins
60  for (const auto& pair : m_bins) {
61  m_trackerMap->AddBin(pair.second->Clone());
62  }
63 
64  // Initialize all bins with zero values
65  for (const auto& pair : m_bins) {
66  m_trackerMap->Fill(pair.first, 0.0);
67  }
68 }
69 
70 //============================================================================
71 void SiStripTkMaps::fill(long rawid, double val) {
72  m_trackerMap->Fill(TString::Format("%ld", rawid), val);
73  m_values.push_back(val);
74 }
75 
76 //============================================================================
78  // margins
79  static constexpr float tmargin_ = 0.08;
80  static constexpr float bmargin_ = 0.02;
81  static constexpr float lmargin_ = 0.02;
82  static constexpr float rmargin_ = 0.08;
83 
84  // window size
85  static constexpr int wH_ = 3000;
86  static constexpr int hH_ = 700;
87 
88  // Define a special value for "empty" bins
89  static constexpr double emptyBinValue = -9999;
90 
91  // Ensure all bins are drawn, including empty ones
92  for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
93  if (m_trackerMap->GetBinContent(i) == 0) {
94  m_trackerMap->SetBinContent(i, emptyBinValue);
95  }
96  }
97 
98  // Adjust the color palette
99  if (!m_values.empty()) {
100  double minValue = *std::min_element(m_values.begin(), m_values.end());
101  double maxValue = *std::max_element(m_values.begin(), m_values.end());
102 
103  // Setting a palette that skips the color for the emptyBinValue
104  m_trackerMap->SetMinimum(minValue); // Set min to the smallest valid value
105  m_trackerMap->SetMaximum(maxValue); // Set max to the largest valid value
106  }
107 
108  canvas.cd();
109  adjustCanvasMargins(canvas.cd(), tmargin_, bmargin_, lmargin_, rmargin_);
110  canvas.Update();
111 
112  m_trackerMap->SetTitle("");
113  if (!option.empty()) {
114  m_trackerMap->Draw(option.c_str());
115  } else {
116  m_trackerMap->Draw();
117  }
118 
119  // Set the "empty" bins color to white
120  for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
121  if (m_trackerMap->GetBinContent(i) == emptyBinValue) {
122  m_trackerMap->SetBinContent(i, emptyBinValue);
123  m_trackerMap->SetMarkerColor(kWhite);
124  }
125  }
126 
127  canvas.SetFrameLineColor(0);
128  gPad->Update();
129  TPaletteAxis* palette = (TPaletteAxis*)m_trackerMap->GetListOfFunctions()->FindObject("palette");
130  if (palette != nullptr) {
131  palette->SetLabelSize(0.02);
132  palette->SetX1NDC(1 - rmargin_);
133  palette->SetX2NDC(1 - rmargin_ + lmargin_);
134  }
135 
136  // if not right size, and not drawn in same mode
137  if (canvas.GetWindowHeight() != hH_ && canvas.GetWindowWidth() != wH_ && option.find("same") == std::string::npos) {
138  canvas.SetWindowSize(wH_, hH_);
139  }
140 
141  // call the map dressing
142  dressMap(canvas);
143 }
144 
145 //============================================================================
146 void SiStripTkMaps::dressMap(TCanvas& canv) {
147  std::array<std::string, 12> barrelNames = {
148  {"TIB L2", "TIB L1", "TIB L4", "TIB L3", "TOB L2", "TOB L1", "TOB L4", " TOB L3", "TOB L6", "TOB L5"}};
149  std::array<std::string, 4> endcapNames = {{"TID", "TEC", "TID", "TEC"}};
150  std::array<std::string, 24> disknumbering = {{"+1", "+2", "+3", "+1", "+2", "+3", "+4", "+5",
151  "+6", "+7", "+8", "+9", "-1", "-2", "-3", "-1",
152  "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9"}};
153 
154  static constexpr std::array<float, 12> b_coordx = {
155  {0.1, 0.1, 0.26, 0.26, 0.41, 0.41, 0.56, 0.56, 0.725, 0.725, 0.05, 0.17}};
156  static constexpr std::array<float, 12> b_coordy = {
157  {0.70, 0.45, 0.70, 0.45, 0.70, 0.46, 0.70, 0.46, 0.70, 0.46, 0.85, 0.85}};
158 
159  static constexpr std::array<float, 4> e_coordx = {{0.01, 0.21, 0.01, 0.21}};
160  static constexpr std::array<float, 4> e_coordy = {{0.89, 0.89, 0.17, 0.17}};
161 
162  static constexpr std::array<float, 24> n_coordx = {{0.01, 0.087, 0.165, 0.227, 0.305, 0.383, 0.461, 0.539,
163  0.616, 0.694, 0.772, 0.850, 0.01, 0.087, 0.165, 0.227,
164  0.305, 0.383, 0.461, 0.539, 0.617, 0.695, 0.773, 0.851}};
165 
166  static constexpr std::array<float, 24> n_coordy = {{0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
167  0.85, 0.85, 0.85, 0.85, 0.13, 0.13, 0.13, 0.13,
168  0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13}};
169 
170  canv.cd();
171  for (const auto& name : barrelNames | boost::adaptors::indexed(0)) {
172  auto ltx = TLatex();
173  ltx.SetTextFont(62);
174  ltx.SetTextSize(0.035);
175  ltx.SetTextAlign(11);
176  ltx.DrawLatexNDC(b_coordx[name.index()], b_coordy[name.index()], name.value().c_str());
177  }
178 
179  for (const auto& name : endcapNames | boost::adaptors::indexed(0)) {
180  auto ltx = TLatex();
181  ltx.SetTextFont(62);
182  ltx.SetTextSize(0.05);
183  ltx.SetTextAlign(11);
184  ltx.DrawLatexNDC(e_coordx[name.index()], e_coordy[name.index()], name.value().c_str());
185  }
186 
187  for (const auto& name : disknumbering | boost::adaptors::indexed(0)) {
188  auto ltx = TLatex();
189  ltx.SetTextFont(62);
190  ltx.SetTextSize(0.035);
191  ltx.SetTextAlign(11);
192  ltx.DrawLatexNDC(n_coordx[name.index()], n_coordy[name.index()], name.value().c_str());
193  }
194 
195  auto ltx = TLatex();
196  ltx.SetTextFont(62);
197  ltx.SetTextSize(0.045);
198  ltx.SetTextAlign(11);
199  ltx.DrawLatexNDC(gPad->GetLeftMargin(), 1 - gPad->GetTopMargin() + 0.03, m_mapTitle.c_str());
200 
201  // barrel axes
202  drawArrows(0.09, 0.23, 0.24, 0.45, "#phi", "z");
203  // endcap axes
204  drawArrows(0.85, 0.89, 0.83, 0.95, "x", "y");
205 
206  canv.Modified();
207  canv.Update(); // make sure it's really (re)drawn
208 }
209 
210 //============================================================================
212  const float x_X1, const float x_X2, const float x_Y1, const float y_Y2, const char* x_label, const char* y_label) {
213  auto arrow_X = TArrow();
214  arrow_X.SetLineColor(kBlue);
215  arrow_X.SetLineWidth(2);
216  arrow_X.SetOption("|>");
217  arrow_X.SetArrowSize(10);
218  arrow_X.DrawLineNDC(x_X1, x_Y1, x_X2, x_Y1);
219 
220  auto arrow_Y = TArrow();
221  arrow_Y.SetLineColor(kBlue);
222  arrow_Y.SetLineWidth(2);
223  arrow_Y.SetOption("|>");
224  arrow_Y.SetArrowSize(10);
225  arrow_Y.DrawLineNDC(x_X2, x_Y1, x_X2, y_Y2);
226 
227  auto text_X = TLatex();
228  text_X.SetTextSize(0.04);
229  text_X.SetTextAlign(11);
230  text_X.SetTextColor(kBlue);
231  text_X.DrawLatexNDC(x_X1, x_Y1 - 0.03, x_label);
232 
233  auto text_Y = TLatex();
234  text_Y.SetTextSize(0.04);
235  text_Y.SetTextAlign(11);
236  text_Y.SetTextColor(kBlue);
237  text_Y.DrawLatexNDC(x_X2 + 0.005, y_Y2 - 0.01, y_label);
238 }
239 
240 //============================================================================
242  TVirtualPad* pad, const float top, const float bottom, const float left, const float right) {
243  if (top > 0) {
244  pad->SetTopMargin(top);
245  }
246  if (bottom > 0) {
247  pad->SetBottomMargin(bottom);
248  }
249  if (left > 0) {
250  pad->SetLeftMargin(left);
251  }
252  if (right > 0) {
253  pad->SetRightMargin(right);
254  }
255 }
256 
257 //============================================================================
258 void SiStripTkMaps::readVertices(double& minx, double& maxx, double& miny, double& maxy) {
259  std::ifstream in;
260 
261  // TPolyline vertices stored at https://github.com/cms-data/DQM-SiStripMonitorClient
262  in.open(edm::FileInPath("DQM/SiStripMonitorClient/data/Geometry/tracker_map_bare").fullPath().c_str());
263 
264  if (!in.good()) {
265  throw cms::Exception("FileError") << "SiStripTkMaps: problem opening vertices file!!" << std::endl;
266  return;
267  }
268 
269  while (in.good()) {
270  long detid = 0;
271  double x[5], y[5];
272 
274  std::getline(in, line);
275  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
276  boost::char_separator<char> sep{" "};
277  tokenizer tok{line, sep};
278 
279  int ix{0}, iy{0};
280  bool isPixel{false};
281  for (const auto& t : tok | boost::adaptors::indexed(0)) {
282  int i = t.index();
283  if (i == 0) {
284  detid = atoll((t.value()).c_str());
285 
286  // Drop Pixel Data
287  DetId detId(detid);
288  if (detId.subdetId() == PixelSubdetector::PixelBarrel || detId.subdetId() == PixelSubdetector::PixelEndcap) {
289  isPixel = true;
290  break;
291  }
292  } else {
293  if (i % 2 == 0) {
294  x[ix] = atof((t.value()).c_str());
295  if (x[ix] < minx) {
296  minx = x[ix];
297  }
298  if (x[ix] > maxx) {
299  maxx = x[ix];
300  }
301  ++ix;
302  } else {
303  y[iy] = atof((t.value()).c_str());
304  if (y[iy] < miny) {
305  miny = y[iy];
306  }
307  if (y[iy] > maxy) {
308  maxy = y[iy];
309  }
310  ++iy;
311  } // else
312  } // else
313  } // loop on entries
314 
315  if (isPixel) {
316  continue;
317  }
318 
319  m_bins[detid] = std::make_shared<TGraph>(ix, x, y);
320  m_bins[detid]->SetName(TString::Format("%ld", detid));
321  m_bins[detid]->SetTitle(TString::Format("Module ID=%ld", detid));
322  m_detIdVector.push_back(detid);
323  }
324 }
void readVertices(double &minx, double &maxx, double &miny, double &maxy)
Option_t * m_option
Definition: SiStripTkMaps.h:73
void bookMap(const std::string mapTitle, const std::string zAxisTitle)
void dressMap(TCanvas &canv)
void drawArrows(const float x_X1, const float x_X2, const float x_Y1, const float y_Y2, const char *x_label, const char *y_label)
std::string m_zAxisTitle
Definition: SiStripTkMaps.h:75
void adjustCanvasMargins(TVirtualPad *pad, const float top, const float bottom, const float left, const float right)
Double_t margin
std::string m_mapTitle
Definition: SiStripTkMaps.h:74
ALPAKA_FN_ACC ALPAKA_FN_INLINE uint32_t ix(uint32_t id)
Definition: DetId.h:17
std::vector< unsigned int > m_detIdVector
Definition: SiStripTkMaps.h:78
void fill(long rawid, double val)
std::vector< double > m_values
Definition: SiStripTkMaps.h:79
bool isPixel(HitType hitType)
def canvas(sub, attr)
Definition: svgfig.py:482
void drawMap(TCanvas &canvas, std::string option="")
ALPAKA_FN_ACC ALPAKA_FN_INLINE uint32_t iy(uint32_t id)
TH2Poly * m_trackerMap
Definition: SiStripTkMaps.h:81
std::map< long, std::shared_ptr< TGraph > > m_bins
Definition: SiStripTkMaps.h:77