Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
// CMSSW includes
#include "CalibTracker/StandaloneTrackerTopology/interface/StandaloneTrackerTopology.h"
#include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
#include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
#include "DataFormats/TrackerCommon/interface/PixelBarrelName.h"
#include "DataFormats/TrackerCommon/interface/PixelEndcapName.h"
#include "FWCore/MessageLogger/interface/MessageLogger.h"
#include "FWCore/ParameterSet/interface/FileInPath.h"

// ROOT includes
#include "TArrow.h"
#include "TPaletteAxis.h"
#include "TGaxis.h"
#include "TCanvas.h"
#include "TColor.h"
#include "TGraph.h"
#include "TLatex.h"
#include "TH2Poly.h"
#include "TStyle.h"

// STL includes
#include <array>
#include <fstream>
#include <iostream>
#include <map>
#include <string>
#include <vector>

// boost includes
#include <boost/tokenizer.hpp>
#include <boost/range/adaptor/indexed.hpp>

#include "DQM/TrackerRemapper/interface/SiStripTkMaps.h"

//============================================================================
void SiStripTkMaps::bookMap(const std::string mapTitle, const std::string zAxisTitle) {
  double minx = 0xFFFFFF, maxx = -0xFFFFFF, miny = 0xFFFFFF, maxy = -0xFFFFFFF;
  readVertices(minx, maxx, miny, maxy);

  // set the titles
  m_zAxisTitle = zAxisTitle;
  m_mapTitle = mapTitle;

  TGaxis::SetMaxDigits(2);

  // margin of the box
  static constexpr int margin = 5;
  m_trackerMap =
      new TH2Poly("Tracker Map", m_mapTitle.c_str(), minx - margin, maxx + margin, miny - margin, maxy + margin);
  m_trackerMap->SetFloat();
  m_trackerMap->SetOption(m_option);
  m_trackerMap->SetStats(false);
  m_trackerMap->GetZaxis()->SetLabelSize(0.03);
  m_trackerMap->GetZaxis()->SetTitleOffset(0.5);
  m_trackerMap->GetZaxis()->SetTitleSize(0.05);
  m_trackerMap->GetZaxis()->SetTitle(m_zAxisTitle.c_str());
  m_trackerMap->GetZaxis()->CenterTitle();

  // Add all bins
  for (const auto& pair : m_bins) {
    m_trackerMap->AddBin(pair.second->Clone());
  }

  // Initialize all bins with zero values
  for (const auto& pair : m_bins) {
    m_trackerMap->Fill(pair.first, 0.0);
  }
}

//============================================================================
void SiStripTkMaps::fill(long rawid, double val) {
  m_trackerMap->Fill(TString::Format("%ld", rawid), val);
  m_values.push_back(val);
}

//============================================================================
void SiStripTkMaps::drawMap(TCanvas& canvas, std::string option) {
  // margins
  static constexpr float tmargin_ = 0.08;
  static constexpr float bmargin_ = 0.02;
  static constexpr float lmargin_ = 0.02;
  static constexpr float rmargin_ = 0.08;

  // window size
  static constexpr int wH_ = 3000;
  static constexpr int hH_ = 700;

  // Define a special value for "empty" bins
  static constexpr double emptyBinValue = -9999;

  // Ensure all bins are drawn, including empty ones
  for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
    if (m_trackerMap->GetBinContent(i) == 0) {
      m_trackerMap->SetBinContent(i, emptyBinValue);
    }
  }

  // Adjust the color palette
  if (!m_values.empty()) {
    double minValue = *std::min_element(m_values.begin(), m_values.end());
    double maxValue = *std::max_element(m_values.begin(), m_values.end());

    // Setting a palette that skips the color for the emptyBinValue
    m_trackerMap->SetMinimum(minValue);  // Set min to the smallest valid value
    m_trackerMap->SetMaximum(maxValue);  // Set max to the largest valid value
  }

  canvas.cd();
  adjustCanvasMargins(canvas.cd(), tmargin_, bmargin_, lmargin_, rmargin_);
  canvas.Update();

  m_trackerMap->SetTitle("");
  if (!option.empty()) {
    m_trackerMap->Draw(option.c_str());
  } else {
    m_trackerMap->Draw();
  }

  // Set the "empty" bins color to white
  for (int i = 1; i <= m_trackerMap->GetNumberOfBins(); ++i) {
    if (m_trackerMap->GetBinContent(i) == emptyBinValue) {
      m_trackerMap->SetBinContent(i, emptyBinValue);
      m_trackerMap->SetMarkerColor(kWhite);
    }
  }

  canvas.SetFrameLineColor(0);
  gPad->Update();
  TPaletteAxis* palette = (TPaletteAxis*)m_trackerMap->GetListOfFunctions()->FindObject("palette");
  if (palette != nullptr) {
    palette->SetLabelSize(0.02);
    palette->SetX1NDC(1 - rmargin_);
    palette->SetX2NDC(1 - rmargin_ + lmargin_);
  }

  // if not right size, and not drawn in same mode
  if (canvas.GetWindowHeight() != hH_ && canvas.GetWindowWidth() != wH_ && option.find("same") == std::string::npos) {
    canvas.SetWindowSize(wH_, hH_);
  }

  // call the map dressing
  dressMap(canvas);
}

//============================================================================
void SiStripTkMaps::dressMap(TCanvas& canv) {
  std::array<std::string, 12> barrelNames = {
      {"TIB L2", "TIB L1", "TIB L4", "TIB L3", "TOB L2", "TOB L1", "TOB L4", " TOB L3", "TOB L6", "TOB L5"}};
  std::array<std::string, 4> endcapNames = {{"TID", "TEC", "TID", "TEC"}};
  std::array<std::string, 24> disknumbering = {{"+1", "+2", "+3", "+1", "+2", "+3", "+4", "+5",
                                                "+6", "+7", "+8", "+9", "-1", "-2", "-3", "-1",
                                                "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9"}};

  static constexpr std::array<float, 12> b_coordx = {
      {0.1, 0.1, 0.26, 0.26, 0.41, 0.41, 0.56, 0.56, 0.725, 0.725, 0.05, 0.17}};
  static constexpr std::array<float, 12> b_coordy = {
      {0.70, 0.45, 0.70, 0.45, 0.70, 0.46, 0.70, 0.46, 0.70, 0.46, 0.85, 0.85}};

  static constexpr std::array<float, 4> e_coordx = {{0.01, 0.21, 0.01, 0.21}};
  static constexpr std::array<float, 4> e_coordy = {{0.89, 0.89, 0.17, 0.17}};

  static constexpr std::array<float, 24> n_coordx = {{0.01,  0.087, 0.165, 0.227, 0.305, 0.383, 0.461, 0.539,
                                                      0.616, 0.694, 0.772, 0.850, 0.01,  0.087, 0.165, 0.227,
                                                      0.305, 0.383, 0.461, 0.539, 0.617, 0.695, 0.773, 0.851}};

  static constexpr std::array<float, 24> n_coordy = {{0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85, 0.85,
                                                      0.85, 0.85, 0.85, 0.85, 0.13, 0.13, 0.13, 0.13,
                                                      0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13, 0.13}};

  canv.cd();
  for (const auto& name : barrelNames | boost::adaptors::indexed(0)) {
    auto ltx = TLatex();
    ltx.SetTextFont(62);
    ltx.SetTextSize(0.035);
    ltx.SetTextAlign(11);
    ltx.DrawLatexNDC(b_coordx[name.index()], b_coordy[name.index()], name.value().c_str());
  }

  for (const auto& name : endcapNames | boost::adaptors::indexed(0)) {
    auto ltx = TLatex();
    ltx.SetTextFont(62);
    ltx.SetTextSize(0.05);
    ltx.SetTextAlign(11);
    ltx.DrawLatexNDC(e_coordx[name.index()], e_coordy[name.index()], name.value().c_str());
  }

  for (const auto& name : disknumbering | boost::adaptors::indexed(0)) {
    auto ltx = TLatex();
    ltx.SetTextFont(62);
    ltx.SetTextSize(0.035);
    ltx.SetTextAlign(11);
    ltx.DrawLatexNDC(n_coordx[name.index()], n_coordy[name.index()], name.value().c_str());
  }

  auto ltx = TLatex();
  ltx.SetTextFont(62);
  ltx.SetTextSize(0.045);
  ltx.SetTextAlign(11);
  ltx.DrawLatexNDC(gPad->GetLeftMargin(), 1 - gPad->GetTopMargin() + 0.03, m_mapTitle.c_str());

  // barrel axes
  drawArrows(0.09, 0.23, 0.24, 0.45, "#phi", "z");
  // endcap axes
  drawArrows(0.85, 0.89, 0.83, 0.95, "x", "y");

  canv.Modified();
  canv.Update();  // make sure it's really (re)drawn
}

//============================================================================
void SiStripTkMaps::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) {
  auto arrow_X = TArrow();
  arrow_X.SetLineColor(kBlue);
  arrow_X.SetLineWidth(2);
  arrow_X.SetOption("|>");
  arrow_X.SetArrowSize(10);
  arrow_X.DrawLineNDC(x_X1, x_Y1, x_X2, x_Y1);

  auto arrow_Y = TArrow();
  arrow_Y.SetLineColor(kBlue);
  arrow_Y.SetLineWidth(2);
  arrow_Y.SetOption("|>");
  arrow_Y.SetArrowSize(10);
  arrow_Y.DrawLineNDC(x_X2, x_Y1, x_X2, y_Y2);

  auto text_X = TLatex();
  text_X.SetTextSize(0.04);
  text_X.SetTextAlign(11);
  text_X.SetTextColor(kBlue);
  text_X.DrawLatexNDC(x_X1, x_Y1 - 0.03, x_label);

  auto text_Y = TLatex();
  text_Y.SetTextSize(0.04);
  text_Y.SetTextAlign(11);
  text_Y.SetTextColor(kBlue);
  text_Y.DrawLatexNDC(x_X2 + 0.005, y_Y2 - 0.01, y_label);
}

//============================================================================
void SiStripTkMaps::adjustCanvasMargins(
    TVirtualPad* pad, const float top, const float bottom, const float left, const float right) {
  if (top > 0) {
    pad->SetTopMargin(top);
  }
  if (bottom > 0) {
    pad->SetBottomMargin(bottom);
  }
  if (left > 0) {
    pad->SetLeftMargin(left);
  }
  if (right > 0) {
    pad->SetRightMargin(right);
  }
}

//============================================================================
void SiStripTkMaps::readVertices(double& minx, double& maxx, double& miny, double& maxy) {
  std::ifstream in;

  // TPolyline vertices stored at https://github.com/cms-data/DQM-SiStripMonitorClient
  in.open(edm::FileInPath("DQM/SiStripMonitorClient/data/Geometry/tracker_map_bare").fullPath().c_str());

  if (!in.good()) {
    throw cms::Exception("FileError") << "SiStripTkMaps: problem opening vertices file!!" << std::endl;
    return;
  }

  while (in.good()) {
    long detid = 0;
    double x[5], y[5];

    std::string line;
    std::getline(in, line);
    typedef boost::tokenizer<boost::char_separator<char>> tokenizer;
    boost::char_separator<char> sep{" "};
    tokenizer tok{line, sep};

    int ix{0}, iy{0};
    bool isPixel{false};
    for (const auto& t : tok | boost::adaptors::indexed(0)) {
      int i = t.index();
      if (i == 0) {
        detid = atoll((t.value()).c_str());

        // Drop Pixel Data
        DetId detId(detid);
        if (detId.subdetId() == PixelSubdetector::PixelBarrel || detId.subdetId() == PixelSubdetector::PixelEndcap) {
          isPixel = true;
          break;
        }
      } else {
        if (i % 2 == 0) {
          x[ix] = atof((t.value()).c_str());
          if (x[ix] < minx) {
            minx = x[ix];
          }
          if (x[ix] > maxx) {
            maxx = x[ix];
          }
          ++ix;
        } else {
          y[iy] = atof((t.value()).c_str());
          if (y[iy] < miny) {
            miny = y[iy];
          }
          if (y[iy] > maxy) {
            maxy = y[iy];
          }
          ++iy;
        }  // else
      }  // else
    }  // loop on entries

    if (isPixel) {
      continue;
    }

    m_bins[detid] = std::make_shared<TGraph>(ix, x, y);
    m_bins[detid]->SetName(TString::Format("%ld", detid));
    m_bins[detid]->SetTitle(TString::Format("Module ID=%ld", detid));
    m_detIdVector.push_back(detid);
  }
}