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  for (const auto& pair : m_bins) {
60  m_trackerMap->AddBin(pair.second->Clone());
61  }
62 }
63 
64 //============================================================================
65 void SiStripTkMaps::fill(long rawid, double val) {
66  m_trackerMap->Fill(TString::Format("%ld", rawid), val);
67  m_values.push_back(val);
68 }
69 
70 //============================================================================
72  // margins
73  static constexpr float tmargin_ = 0.08;
74  static constexpr float bmargin_ = 0.02;
75  static constexpr float lmargin_ = 0.02;
76  static constexpr float rmargin_ = 0.08;
77 
78  // window size
79  static constexpr int wH_ = 3000;
80  static constexpr int hH_ = 700;
81 
82  canvas.cd();
83  adjustCanvasMargins(canvas.cd(), tmargin_, bmargin_, lmargin_, rmargin_);
84  canvas.Update();
85 
86  m_trackerMap->SetTitle("");
87  if (!option.empty()) {
88  m_trackerMap->Draw(option.c_str());
89  } else {
90  m_trackerMap->Draw();
91  }
92 
93  canvas.SetFrameLineColor(0);
94  gPad->Update();
95  TPaletteAxis* palette = (TPaletteAxis*)m_trackerMap->GetListOfFunctions()->FindObject("palette");
96  if (palette != nullptr) {
97  palette->SetLabelSize(0.02);
98  palette->SetX1NDC(1 - rmargin_);
99  palette->SetX2NDC(1 - rmargin_ + lmargin_);
100  }
101 
102  // if not right size, and not drawn in same mode
103  if (canvas.GetWindowHeight() != hH_ && canvas.GetWindowWidth() != wH_ && option.find("same") == std::string::npos) {
104  canvas.SetWindowSize(wH_, hH_);
105  }
106 
107  // call the map dressing
108  dressMap(canvas);
109 }
110 
111 //============================================================================
112 void SiStripTkMaps::dressMap(TCanvas& canv) {
113  std::array<std::string, 12> barrelNames = {
114  {"TIB L2", "TIB L1", "TIB L4", "TIB L3", "TOB L2", "TOB L1", "TOB L4", " TOB L3", "TOB L6", "TOB L5"}};
115  std::array<std::string, 4> endcapNames = {{"TID", "TEC", "TID", "TEC"}};
116  std::array<std::string, 24> disknumbering = {{"+1", "+2", "+3", "+1", "+2", "+3", "+4", "+5",
117  "+6", "+7", "+8", "+9", "-1", "-2", "-3", "-1",
118  "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9"}};
119 
120  static constexpr std::array<float, 12> b_coordx = {
121  {0.1, 0.1, 0.26, 0.26, 0.41, 0.41, 0.56, 0.56, 0.725, 0.725, 0.05, 0.17}};
122  static constexpr std::array<float, 12> b_coordy = {
123  {0.70, 0.45, 0.70, 0.45, 0.70, 0.46, 0.70, 0.46, 0.70, 0.46, 0.85, 0.85}};
124 
125  static constexpr std::array<float, 4> e_coordx = {{0.01, 0.21, 0.01, 0.21}};
126  static constexpr std::array<float, 4> e_coordy = {{0.89, 0.89, 0.17, 0.17}};
127 
128  static constexpr std::array<float, 24> n_coordx = {{0.01, 0.087, 0.165, 0.227, 0.305, 0.383, 0.461, 0.539,
129  0.616, 0.694, 0.772, 0.850, 0.01, 0.087, 0.165, 0.227,
130  0.305, 0.383, 0.461, 0.539, 0.617, 0.695, 0.773, 0.851}};
131 
132  static constexpr std::array<float, 24> n_coordy = {{0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
133  0.85, 0.85, 0.85, 0.85, 0.13, 0.13, 0.13, 0.13,
134  0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13}};
135 
136  canv.cd();
137  for (const auto& name : barrelNames | boost::adaptors::indexed(0)) {
138  auto ltx = TLatex();
139  ltx.SetTextFont(62);
140  ltx.SetTextSize(0.035);
141  ltx.SetTextAlign(11);
142  ltx.DrawLatexNDC(b_coordx[name.index()], b_coordy[name.index()], name.value().c_str());
143  }
144 
145  for (const auto& name : endcapNames | boost::adaptors::indexed(0)) {
146  auto ltx = TLatex();
147  ltx.SetTextFont(62);
148  ltx.SetTextSize(0.05);
149  ltx.SetTextAlign(11);
150  ltx.DrawLatexNDC(e_coordx[name.index()], e_coordy[name.index()], name.value().c_str());
151  }
152 
153  for (const auto& name : disknumbering | boost::adaptors::indexed(0)) {
154  auto ltx = TLatex();
155  ltx.SetTextFont(62);
156  ltx.SetTextSize(0.035);
157  ltx.SetTextAlign(11);
158  ltx.DrawLatexNDC(n_coordx[name.index()], n_coordy[name.index()], name.value().c_str());
159  }
160 
161  auto ltx = TLatex();
162  ltx.SetTextFont(62);
163  ltx.SetTextSize(0.045);
164  ltx.SetTextAlign(11);
165  ltx.DrawLatexNDC(gPad->GetLeftMargin(), 1 - gPad->GetTopMargin() + 0.03, m_mapTitle.c_str());
166 
167  // barrel axes
168  drawArrows(0.09, 0.23, 0.24, 0.45, "#phi", "z");
169  // endcap axes
170  drawArrows(0.85, 0.89, 0.83, 0.95, "x", "y");
171 
172  canv.Modified();
173  canv.Update(); // make sure it's really (re)drawn
174 }
175 
176 //============================================================================
178  const float x_X1, const float x_X2, const float x_Y1, const float y_Y2, const char* x_label, const char* y_label) {
179  auto arrow_X = TArrow();
180  arrow_X.SetLineColor(kBlue);
181  arrow_X.SetLineWidth(2);
182  arrow_X.SetOption("|>");
183  arrow_X.SetArrowSize(10);
184  arrow_X.DrawLineNDC(x_X1, x_Y1, x_X2, x_Y1);
185 
186  auto arrow_Y = TArrow();
187  arrow_Y.SetLineColor(kBlue);
188  arrow_Y.SetLineWidth(2);
189  arrow_Y.SetOption("|>");
190  arrow_Y.SetArrowSize(10);
191  arrow_Y.DrawLineNDC(x_X2, x_Y1, x_X2, y_Y2);
192 
193  auto text_X = TLatex();
194  text_X.SetTextSize(0.04);
195  text_X.SetTextAlign(11);
196  text_X.SetTextColor(kBlue);
197  text_X.DrawLatexNDC(x_X1, x_Y1 - 0.03, x_label);
198 
199  auto text_Y = TLatex();
200  text_Y.SetTextSize(0.04);
201  text_Y.SetTextAlign(11);
202  text_Y.SetTextColor(kBlue);
203  text_Y.DrawLatexNDC(x_X2 + 0.005, y_Y2 - 0.01, y_label);
204 }
205 
206 //============================================================================
208  TVirtualPad* pad, const float top, const float bottom, const float left, const float right) {
209  if (top > 0) {
210  pad->SetTopMargin(top);
211  }
212  if (bottom > 0) {
213  pad->SetBottomMargin(bottom);
214  }
215  if (left > 0) {
216  pad->SetLeftMargin(left);
217  }
218  if (right > 0) {
219  pad->SetRightMargin(right);
220  }
221 }
222 
223 //============================================================================
224 void SiStripTkMaps::readVertices(double& minx, double& maxx, double& miny, double& maxy) {
225  std::ifstream in;
226 
227  // TPolyline vertices stored at https://github.com/cms-data/DQM-SiStripMonitorClient
228  in.open(edm::FileInPath("DQM/SiStripMonitorClient/data/Geometry/tracker_map_bare").fullPath().c_str());
229 
230  if (!in.good()) {
231  throw cms::Exception("FileError") << "SiStripTkMaps: problem opening vertices file!!" << std::endl;
232  return;
233  }
234 
235  while (in.good()) {
236  long detid = 0;
237  double x[5], y[5];
238 
240  std::getline(in, line);
241  typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
242  boost::char_separator<char> sep{" "};
243  tokenizer tok{line, sep};
244 
245  int ix{0}, iy{0};
246  bool isPixel{false};
247  for (const auto& t : tok | boost::adaptors::indexed(0)) {
248  int i = t.index();
249  if (i == 0) {
250  detid = atoll((t.value()).c_str());
251 
252  // Drop Pixel Data
253  DetId detId(detid);
254  if (detId.subdetId() == PixelSubdetector::PixelBarrel || detId.subdetId() == PixelSubdetector::PixelEndcap) {
255  isPixel = true;
256  break;
257  }
258  } else {
259  if (i % 2 == 0) {
260  x[ix] = atof((t.value()).c_str());
261  if (x[ix] < minx) {
262  minx = x[ix];
263  }
264  if (x[ix] > maxx) {
265  maxx = x[ix];
266  }
267  ++ix;
268  } else {
269  y[iy] = atof((t.value()).c_str());
270  if (y[iy] < miny) {
271  miny = y[iy];
272  }
273  if (y[iy] > maxy) {
274  maxy = y[iy];
275  }
276  ++iy;
277  } // else
278  } // else
279  } // loop on entries
280 
281  if (isPixel) {
282  continue;
283  }
284 
285  m_bins[detid] = std::make_shared<TGraph>(ix, x, y);
286  m_bins[detid]->SetName(TString::Format("%ld", detid));
287  m_bins[detid]->SetTitle(TString::Format("Module ID=%ld", detid));
288  m_detIdVector.push_back(detid);
289  }
290 }
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