CMS 3D CMS Logo

Phase1PixelSummaryMap.cc
Go to the documentation of this file.
1 #include "TArrow.h"
2 #include "TCanvas.h"
3 #include "TGraph.h"
4 #include "TH1.h"
5 #include "TH2.h"
6 #include "TH2Poly.h"
7 #include "TLatex.h"
8 #include "TStyle.h"
9 
10 #include <fmt/printf.h>
11 #include <fstream>
12 #include <memory>
13 #include <boost/tokenizer.hpp>
14 #include <boost/range/adaptor/indexed.hpp>
15 
20 
21 // set option, but only if not already set
22 //============================================================================
24  if (m_option != nullptr && !m_option[0]) {
25  m_option = option;
26  } else {
27  edm::LogError("Phase1PixelSummaryMap")
28  << "Option has already been set to " << m_option << ". It's not possible to reset it.";
29  }
30 }
31 
32 //============================================================================
34  m_BaseTrackerMap = std::make_shared<TH2Poly>("Summary", "", -10, 160, -70, 70);
35  m_BaseTrackerMap->SetFloat(true);
36  m_BaseTrackerMap->GetXaxis()->SetTitle("");
37  m_BaseTrackerMap->GetYaxis()->SetTitle("");
38  m_BaseTrackerMap->GetZaxis()->SetTitle(m_zAxisTitle.c_str());
39  m_BaseTrackerMap->GetZaxis()->CenterTitle();
40  m_BaseTrackerMap->GetZaxis()->SetTitleOffset(1.2);
41  m_BaseTrackerMap->SetOption("COLZ L");
42  m_BaseTrackerMap->SetStats(false);
43 
44  //BARREL FIRST
45  for (unsigned int i = 0; i < maxPxBarrel; i++) {
46  LOGINFO("Phase1PixelSummaryMap") << "barrel, shift: " << i << " corner: " << i << std::endl;
47  LOGINFO("Phase1PixelSummaryMap") << "translate x: " << 0 << std::endl;
48  LOGINFO("Phase1PixelSummaryMap") << "translate y: " << barrelLadderShift[i] << std::endl;
49 
50  int currBarrelTranslateX = 0;
51  int currBarrelTranslateY = barrelLadderShift[i];
52  addNamedBins(m_cornersBPIX[i], currBarrelTranslateX, currBarrelTranslateY, 1, 1, true);
53  }
54 
55  //MINUS FORWARD
56  for (int j : {-3, -2, -1}) {
57  LOGINFO("Phase1PixelSummaryMap") << "negative fwd, shift: " << -j - 1 << " corner: " << maxPxForward + j
58  << std::endl;
59  LOGINFO("Phase1PixelSummaryMap") << "translate x: " << forwardDiskXShift[-j - 1] << std::endl;
60  LOGINFO("Phase1PixelSummaryMap") << "translate y: " << -forwardDiskYShift << std::endl;
61 
62  int currForwardTranslateX = forwardDiskXShift[-j - 1];
63  int currForwardTranslateY = -forwardDiskYShift;
64  addNamedBins(m_cornersFPIX[maxPxForward + j], currForwardTranslateX, currForwardTranslateY, 1, 1);
65  }
66 
67  //PLUS FORWARD
68  for (int k : {1, 2, 3}) {
69  LOGINFO("Phase1PixelSummaryMap") << "positive fwd, shift: " << k << " corner: " << maxPxForward + k - 1
70  << std::endl;
71  LOGINFO("Phase1PixelSummaryMap") << "translate x: " << forwardDiskXShift[k - 1] << std::endl;
72  LOGINFO("Phase1PixelSummaryMap") << "translate y: " << forwardDiskYShift << std::endl;
73 
74  int currForwardTranslateX = forwardDiskXShift[k - 1];
75  int currForwardTranslateY = forwardDiskYShift;
76  addNamedBins(m_cornersFPIX[maxPxForward + k - 1], currForwardTranslateX, currForwardTranslateY, 1, 1);
77  }
78 
79  edm::LogPrint("Phase1PixelSummaryMap") << "Base Tracker Map: constructed" << std::endl;
80  return;
81 }
82 
83 //============================================================================
84 void Phase1PixelSummaryMap::printTrackerMap(TCanvas& canvas, const float topMargin, int index) {
85  //canvas = TCanvas("c1","c1",plotWidth,plotHeight);
86  if (index != 0)
87  canvas.cd(index);
88  else
89  canvas.cd();
90 
91  if (index == 0) {
92  canvas.SetTopMargin(topMargin);
93  canvas.SetBottomMargin(0.02);
94  canvas.SetLeftMargin(0.02);
95  canvas.SetRightMargin(0.14);
96  } else {
97  m_BaseTrackerMap->GetZaxis()->SetTitleOffset(1.5);
98  canvas.cd(index)->SetTopMargin(topMargin);
99  canvas.cd(index)->SetBottomMargin(0.02);
100  canvas.cd(index)->SetLeftMargin(0.02);
101  canvas.cd(index)->SetRightMargin(0.14);
102  }
103 
104  m_BaseTrackerMap->Draw("AL");
105  m_BaseTrackerMap->Draw("AC COLZ0 L SAME");
106 
107  //### z arrow
108  arrow = TArrow(0.05, 27.0, 0.05, -30.0, 0.02, "|>");
109  arrow.SetLineWidth(4);
110  arrow.Draw();
111  arrow.SetAngle(30);
112  //### phi arrow
113  phiArrow = TArrow(0.0, 27.0, 30.0, 27.0, 0.02, "|>");
114  phiArrow.SetLineWidth(4);
115  phiArrow.Draw();
116  phiArrow.SetAngle(30);
117  //### x arrow
118  xArrow = TArrow(25.0, 44.5, 50.0, 44.5, 0.02, "|>");
119  xArrow.SetLineWidth(4);
120  xArrow.Draw();
121  xArrow.SetAngle(30);
122  //### y arrow
123  yArrow = TArrow(25.0, 44.5, 25.0, 69.5, 0.02, "|>");
124  yArrow.SetLineWidth(4);
125  yArrow.Draw();
126  yArrow.SetAngle(30);
127 
128  //###################################################
129  //# add some captions
130  auto txt = TLatex();
131  txt.SetNDC();
132  txt.SetTextFont(1);
133  txt.SetTextColor(1);
134  txt.SetTextAlign(22);
135  txt.SetTextAngle(0);
136 
137  //# draw new-style title
138  txt.SetTextSize((topMargin == 0.02) ? 0.05 : 0.03);
139  txt.DrawLatex(0.5, ((index == 0) ? 0.95 : 0.93), (fmt::sprintf("Pixel Tracker Map: %s", m_title)).c_str());
140  txt.SetTextSize(0.03);
141 
142  txt.DrawLatex(0.55, 0.125, "-DISK");
143  txt.DrawLatex(0.55, 0.875, "+DISK");
144 
145  txt.DrawLatex(0.08, 0.28, "+z");
146  txt.DrawLatex(0.25, 0.70, "+phi");
147  txt.DrawLatex((index == 0) ? 0.31 : 0.33, 0.78, "+x");
148  txt.DrawLatex((index == 0) ? 0.21 : 0.22, ((index == 0) ? 0.96 : 0.94), "+y");
149 
150  txt.SetTextAngle(90);
151  txt.DrawLatex(0.04, 0.5, "BARREL");
152 
153  edm::LogPrint("Phase1PixelSummaryMap") << "Base Tracker Map: printed" << std::endl;
154 }
155 
156 //============================================================================
157 bool Phase1PixelSummaryMap::fillTrackerMap(unsigned int id, double value) {
158  auto detid = DetId(id);
159  if (detid.subdetId() != PixelSubdetector::PixelBarrel && detid.subdetId() != PixelSubdetector::PixelEndcap) {
160  edm::LogError("Phase1PixelSummaryMap")
161  << __func__ << " The following detid " << id << " is not Pixel!" << std::endl;
162  return false;
163  } else {
164  m_BaseTrackerMap->Fill(TString::Format("%u", id), value);
165  return true;
166  }
167 }
168 
169 //============================================================================
170 const std::pair<float, float> Phase1PixelSummaryMap::getZAxisRange() const {
171  return std::make_pair(m_BaseTrackerMap->GetMinimum(), m_BaseTrackerMap->GetMaximum());
172 }
173 
174 //============================================================================
175 void Phase1PixelSummaryMap::setZAxisRange(const double min, const double max) {
176  m_BaseTrackerMap->GetZaxis()->SetRangeUser(min, max);
177 }
178 
179 //============================================================================
181  edm::FileInPath geoFile, int tX, int tY, int sX, int sY, bool applyModuleRotation) {
182  auto cornerFileName = geoFile.fullPath();
183  std::ifstream cornerFile(cornerFileName.c_str());
184  if (!cornerFile.good()) {
185  throw cms::Exception("FileError") << "Problem opening corner file: " << cornerFileName;
186  }
188  while (std::getline(cornerFile, line)) {
189  if (!line.empty()) {
190  std::istringstream iss(line);
191 
192  auto tokens = Ph1PMapSummaryHelper::tokenize(line, '"');
193  // Printing the token vector
194  for (unsigned int i = 0; i < tokens.size(); i++)
195  LOGDEBUG("Phase1PixelSummaryMap") << tokens[i] << '\n';
196 
197  auto detInfo = Ph1PMapSummaryHelper::tokenize(tokens[0], ' ');
198  unsigned int detId = stoi(detInfo[0]);
199  std::string detIdName = detInfo[1];
200  auto xy = Ph1PMapSummaryHelper::tokenize(tokens[1], ' ');
201  unsigned int verNum = 1;
202  std::vector<float> xP, yP;
203  for (const auto& coord : xy) {
204  auto coordSpl = Ph1PMapSummaryHelper::tokenize(coord, ',');
205  if (applyModuleRotation) {
206  xP.push_back(-(std::stof(coordSpl[0]) * sX + tX));
207  yP.push_back(((std::stof(coordSpl[1]) * sY + tY)));
208  } else {
209  xP.push_back(std::stof(coordSpl[0]) * sX + tX);
210  yP.push_back(std::stof(coordSpl[1]) * sY + tY);
211  }
212  verNum++;
213  }
214  //close the polygon
215  xP.push_back(xP[0]);
216  yP.push_back(yP[0]);
217 
218  LOGDEBUG("Phase1PixelSummaryMap") << detId << "[";
219  for (const auto& p : xP) {
220  LOGDEBUG("Phase1PixelSummaryMap") << p << ",";
221  }
222  LOGDEBUG("Phase1PixelSummaryMap") << "] [ ";
223  for (const auto& q : yP) {
224  LOGDEBUG("Phase1PixelSummaryMap") << q << ",";
225  }
226  LOGDEBUG("Phase1PixelSummaryMap") << "]" << std::endl;
227 
228  const unsigned int N = verNum;
229  if (applyModuleRotation) {
230  bins[detId] = std::make_shared<TGraph>(N, &yP[0], &xP[0]);
231  } else {
232  bins[detId] = std::make_shared<TGraph>(N, &xP[0], &yP[0]);
233  //bins[detId] = std::make_shared<TGraph>(N, &yP[0], &xP[0]); // rotation by 90 deg (so that it had the same layout as for the strips)
234  }
235 
236  bins[detId]->SetName(detInfo[0].c_str());
237  m_BaseTrackerMap->AddBin(bins[detId]->Clone());
238  }
239  }
240  return;
241 }
const std::string m_zAxisTitle
void resetOption(const char *option)
static const unsigned int maxPxBarrel
const std::array< int, maxPxBarrel > barrelLadderShift
std::map< uint32_t, std::shared_ptr< TGraph > > bins
const std::array< int, maxPxForward > forwardDiskXShift
std::string fullPath() const
Definition: FileInPath.cc:161
#define LOGINFO(x)
std::vector< std::string > tokenize(std::string line, char delimiter)
const std::pair< float, float > getZAxisRange() const
static const unsigned int maxPxForward
Log< level::Error, false > LogError
std::shared_ptr< TH2Poly > m_BaseTrackerMap
#define LOGDEBUG(x)
std::vector< edm::FileInPath > m_cornersBPIX
void setZAxisRange(const double min, const double max)
Definition: value.py:1
Log< level::Warning, true > LogPrint
Definition: DetId.h:17
#define N
Definition: blowfish.cc:9
void addNamedBins(edm::FileInPath geoFile, int tX, int tY, int sX, int sY, bool applyModuleRotation=false)
std::vector< edm::FileInPath > m_cornersFPIX
void printTrackerMap(TCanvas &canvas, const float topMargin=0.02, int index=0)
bool fillTrackerMap(unsigned int id, double value)
def canvas(sub, attr)
Definition: svgfig.py:482