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 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
#include "DQM/SiStripCommon/interface/TkHistoMap.h"
#include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"

//#define debug_TkHistoMap

TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap) : HistoNumber(35) {
  LogTrace("TkHistoMap") << "TkHistoMap::constructor without parameters";
  load(tkDetMap, "", 0.0f, false, false, false);
}

TkHistoMap::TkHistoMap(
    const TkDetMap* tkDetMap, const std::string& path, const std::string& MapName, float baseline, bool mechanicalView)
    : HistoNumber(35), MapName_(MapName) {
  LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
  load(tkDetMap, path, baseline, mechanicalView, false);
  dqmStore_->meBookerGetter([this, &path, &baseline, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter&) {
    this->createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
  });
}

TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap,
                       const std::string& path,
                       const std::string& MapName,
                       float baseline,
                       bool mechanicalView,
                       bool isTH2F)
    : HistoNumber(35), MapName_(MapName) {
  LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
  load(tkDetMap, path, baseline, mechanicalView, isTH2F);
  dqmStore_->meBookerGetter([this, &path, &baseline, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter&) {
    this->createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
  });
}

TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap,
                       DQMStore::IBooker& ibooker,
                       const std::string& path,
                       const std::string& MapName,
                       float baseline,
                       bool mechanicalView,
                       bool isTH2F)
    : HistoNumber(35), MapName_(MapName) {
  LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
  load(tkDetMap, path, baseline, mechanicalView, isTH2F);
  createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
}

void TkHistoMap::load(const TkDetMap* tkDetMap,
                      const std::string& path,
                      float baseline,
                      bool mechanicalView,
                      bool isTH2F,
                      bool createTkMap) {
  // cannot pass nullptr, otherwise methods making use of TrackerTopology will segfault
  if (tkDetMap == nullptr) {
    throw cms::Exception("LogicError") << " expected pointer to TkDetMap is null!\n";
  }

  cached_detid = 0;
  cached_layer = 0;
  loadServices();
  tkdetmap_ = tkDetMap;
  isTH2F_ = isTH2F;
}

void TkHistoMap::loadServices() {
  if (!edm::Service<DQMStore>().isAvailable()) {
    edm::LogError("TkHistoMap")
        << "\n------------------------------------------"
           "\nUnAvailable Service DQMStore: please insert in the configuration file an instance like"
           "\n\tprocess.load(\"DQMServices.Core.DQMStore_cfg\")"
           "\n------------------------------------------";
  }
  dqmStore_ = edm::Service<DQMStore>().operator->();
}

void TkHistoMap::save(const std::string& filename) {
  // dqmStore_ only for saving
  dqmStore_->save(filename);
}

void TkHistoMap::loadTkHistoMap(const std::string& path, const std::string& MapName, bool mechanicalView) {
  MapName_ = MapName;
  tkHistoMap_.resize(HistoNumber);
  auto loadMap = [this, &path, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
    std::string fullName, folder;
    for (int layer = 1; layer < HistoNumber; ++layer) {
      folder = folderDefinition(ibooker, path, MapName_, layer, mechanicalView, fullName);
#ifdef debug_TkHistoMap
      LogTrace("TkHistoMap") << "[TkHistoMap::loadTkHistoMap] folder " << folder << " histoName " << fullName
                             << " find " << folder.find_last_of("/") << "  length " << folder.length();
#endif
      if (folder.find_last_of('/') != folder.length() - 1)
        folder += "/";
      tkHistoMap_[layer] = igetter.get(folder + fullName);
#ifdef debug_TkHistoMap
      LogTrace("TkHistoMap") << "[TkHistoMap::loadTkHistoMap] folder " << folder << " histoName " << fullName
                             << " layer " << layer << " ptr " << tkHistoMap_[layer] << " find "
                             << folder.find_last_of("/") << "  length " << folder.length();
#endif
    }
  };
  dqmStore_->meBookerGetter(loadMap);
}

void TkHistoMap::createTkHistoMap(DQMStore::IBooker& ibooker,
                                  const std::string& path,
                                  const std::string& MapName,
                                  float baseline,
                                  bool mechanicalView) {
  int nchX;
  int nchY;
  double lowX, highX;
  double lowY, highY;
  std::string fullName, folder;

  tkHistoMap_.resize(HistoNumber);
  const bool bookTH2F = isTH2F_;
  for (int layer = 1; layer < HistoNumber; ++layer) {
    folder = folderDefinition(ibooker, path, MapName, layer, mechanicalView, fullName);
    tkdetmap_->getComponents(layer, nchX, lowX, highX, nchY, lowY, highY);
    MonitorElement* me;
    if (bookTH2F == false) {
      me = ibooker.bookProfile2D(fullName.c_str(), fullName.c_str(), nchX, lowX, highX, nchY, lowY, highY, 0.0, 0.0);
    } else {
      me = ibooker.book2D(fullName.c_str(), fullName.c_str(), nchX, lowX, highX, nchY, lowY, highY);
    }
    //initialize bin content for the not assigned bins
    if (baseline != 0) {
      for (size_t ix = 1; ix <= (unsigned int)nchX; ++ix)
        for (size_t iy = 1; iy <= (unsigned int)nchY; ++iy)
          if (!tkdetmap_->getDetFromBin(layer, ix, iy))
            me->Fill(1. * (lowX + ix - .5), 1. * (lowY + iy - .5), baseline);
    }

    tkHistoMap_[layer] = me;
#ifdef debug_TkHistoMap
    LogTrace("TkHistoMap") << "[TkHistoMap::createTkHistoMap] folder " << folder << " histoName " << fullName
                           << " layer " << layer << " ptr " << tkHistoMap_[layer];
#endif
  }
}

std::string TkHistoMap::folderDefinition(DQMStore::IBooker& ibooker,
                                         std::string folder,
                                         const std::string& MapName,
                                         int layer,
                                         bool mechanicalView,
                                         std::string& fullName) {
  std::string name = MapName + std::string("_");
  fullName = name + TkDetMap::getLayerName(layer);

  if (mechanicalView) {
    std::stringstream ss;

    SiStripFolderOrganizer folderOrg;
    folderOrg.setSiStripFolderName(folder);

    SiStripDetId::SubDetector subDet;
    uint32_t subdetlayer = 0, side = 0;
    TkDetMap::getSubDetLayerSide(layer, subDet, subdetlayer, side);
    folderOrg.getSubDetLayerFolderName(ss, subDet, subdetlayer, side);
    folder = ss.str();
  }
  ibooker.setCurrentFolder(folder);
  return folder;
}

#include <iostream>
void TkHistoMap::fillFromAscii(const std::string& filename) {
  std::ifstream file;
  file.open(filename.c_str());

  if (file.fail()) {
    throw cms::Exception("LogicError") << "failed to open input file" << std::endl;
  }

  float value;
  uint32_t detid;
  while (file.good()) {
    file >> detid >> value;
    fill(detid, value);
  }
  file.close();
}

void TkHistoMap::fill(DetId detid, float value) {
  int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
  if (layer == TkLayerMap::INVALID) {
    edm::LogError("TkHistoMap") << " could not fill for detid " << detid.rawId() << ", as the layer is invalid";
    return;
  }

  TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
#ifdef debug_TkHistoMap
  LogTrace("TkHistoMap") << "[TkHistoMap::fill] Fill detid " << detid.rawId() << " Layer " << layer << " value "
                         << value << " ix,iy " << xybin.ix << " " << xybin.iy << " " << xybin.x << " " << xybin.y << " "
                         << tkHistoMap_[layer]->getTProfile2D()->GetName();
#endif
  if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
    tkHistoMap_[layer]->getTProfile2D()->Fill(xybin.x, xybin.y, value);
  else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
    tkHistoMap_[layer]->getTH2F()->Fill(xybin.x, xybin.y, value);
#ifdef debug_TkHistoMap
  LogTrace("TkHistoMap") << "[TkHistoMap::fill] "
                         << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(xybin.ix, xybin.iy);
  for (size_t ii = 0; ii < 4; ii++)
    for (size_t jj = 0; jj < 11; jj++) {
      if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
        LogTrace("TkHistoMap") << "[TkHistoMap::fill] " << ii << " " << jj << " "
                               << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(ii, jj);
      if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
        LogTrace("TkHistoMap") << "[TkHistoMap::fill] " << ii << " " << jj << " "
                               << tkHistoMap_[layer]->getTH2F()->GetBinContent(ii, jj);
    }
#endif
}

void TkHistoMap::setBinContent(DetId detid, float value) {
  int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
  if (layer == TkLayerMap::INVALID) {
    edm::LogError("TkHistoMap") << " could not setBinContent for detid " << detid.rawId()
                                << ", as the layer is invalid";
    return;
  }
  TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);

  if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D) {
    tkHistoMap_[layer]->getTProfile2D()->SetBinEntries(tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy),
                                                       1);
    tkHistoMap_[layer]->getTProfile2D()->SetBinContent(tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy),
                                                       value);
  } else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F) {
    tkHistoMap_[layer]->setBinContent(xybin.ix, xybin.iy, value);
  }

#ifdef debug_TkHistoMap
  LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent]  setBinContent detid " << detid.rawId() << " Layer " << layer
                         << " value " << value << " ix,iy " << xybin.ix << " " << xybin.iy << " " << xybin.x << " "
                         << xybin.y << " " << tkHistoMap_[layer]->getTProfile2D()->GetName() << " bin "
                         << tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy);

  LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent] "
                         << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(xybin.ix, xybin.iy);
  for (size_t ii = 0; ii < 4; ii++)
    for (size_t jj = 0; jj < 11; jj++) {
      LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent] " << ii << " " << jj << " "
                             << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(ii, jj);
    }
#endif
}

void TkHistoMap::add(DetId detid, float value) {
#ifdef debug_TkHistoMap
  LogTrace("TkHistoMap") << "[TkHistoMap::add]";
#endif
  int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
  if (layer == TkLayerMap::INVALID) {
    edm::LogError("TkHistoMap") << " could not add for detid " << detid.rawId() << ", as the layer is invalid";
    return;
  }
  TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
  if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
    setBinContent(detid,
                  tkHistoMap_[layer]->getTProfile2D()->GetBinContent(
                      tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy)) +
                      value);
  else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
    setBinContent(
        detid,
        tkHistoMap_[layer]->getTH2F()->GetBinContent(tkHistoMap_[layer]->getTH2F()->GetBin(xybin.ix, xybin.iy)) +
            value);
}

float TkHistoMap::getValue(DetId detid) {
  int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
  if (layer == TkLayerMap::INVALID) {
    edm::LogError("TkHistoMap") << " could not getValue for detid " << detid.rawId() << ", as the layer is invalid";
    return -99999.f;
  }

  TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);

  if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
    return tkHistoMap_[layer]->getTH2F()->GetBinContent(tkHistoMap_[layer]->getTH2F()->GetBin(xybin.ix, xybin.iy));
  else
    return tkHistoMap_[layer]->getTProfile2D()->GetBinContent(
        tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy));
}
float TkHistoMap::getEntries(DetId detid) {
  int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
  if (layer == TkLayerMap::INVALID) {
    edm::LogError("TkHistoMap") << " could not getValue for detid " << detid.rawId() << ", as the layer is invalid";
    return -99999.f;
  }

  if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F) {
    return 1;
  } else {
    TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
    return tkHistoMap_[layer]->getTProfile2D()->GetBinEntries(
        tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy));
  }
}

void TkHistoMap::dumpInTkMap(TrackerMap* tkmap, bool dumpEntries) {
  for (int layer = 1; layer < HistoNumber; ++layer) {
    // std::vector<uint32_t> dets;
    // tkdetmap_->getDetsForLayer(layer,dets);
    std::vector<DetId> dets = tkdetmap_->getDetsForLayer(layer);
    for (size_t i = 0; i < dets.size(); ++i) {
      if (dets[i] > 0) {
        if (getEntries(dets[i]) > 0) {
          tkmap->fill(dets[i], dumpEntries ? getEntries(dets[i]) : getValue(dets[i]));
        }
      }
    }
  }
}

#include "TCanvas.h"
#include "TFile.h"
void TkHistoMap::saveAsCanvas(const std::string& filename, const std::string& options, const std::string& mode) {
  //  TCanvas C(MapName_,MapName_,200,10,900,700);
  TCanvas* CTIB =
      new TCanvas(std::string("Canvas_" + MapName_ + "TIB").c_str(), std::string("Canvas_" + MapName_ + "TIB").c_str());
  TCanvas* CTOB =
      new TCanvas(std::string("Canvas_" + MapName_ + "TOB").c_str(), std::string("Canvas_" + MapName_ + "TOB").c_str());
  TCanvas* CTIDP = new TCanvas(std::string("Canvas_" + MapName_ + "TIDP").c_str(),
                               std::string("Canvas_" + MapName_ + "TIDP").c_str());
  TCanvas* CTIDM = new TCanvas(std::string("Canvas_" + MapName_ + "TIDM").c_str(),
                               std::string("Canvas_" + MapName_ + "TIDM").c_str());
  TCanvas* CTECP = new TCanvas(std::string("Canvas_" + MapName_ + "TECP").c_str(),
                               std::string("Canvas_" + MapName_ + "TECP").c_str());
  TCanvas* CTECM = new TCanvas(std::string("Canvas_" + MapName_ + "TECM").c_str(),
                               std::string("Canvas_" + MapName_ + "TECM").c_str());
  CTIB->Divide(2, 2);
  CTOB->Divide(2, 3);
  CTIDP->Divide(1, 3);
  CTIDM->Divide(1, 3);
  CTECP->Divide(3, 3);
  CTECM->Divide(3, 3);

  int i;
  i = 0;
  CTIB->cd(++i);
  tkHistoMap_[TkLayerMap::TIB_L1]->getTProfile2D()->Draw(options.c_str());
  CTIB->cd(++i);
  tkHistoMap_[TkLayerMap::TIB_L2]->getTProfile2D()->Draw(options.c_str());
  CTIB->cd(++i);
  tkHistoMap_[TkLayerMap::TIB_L3]->getTProfile2D()->Draw(options.c_str());
  CTIB->cd(++i);
  tkHistoMap_[TkLayerMap::TIB_L4]->getTProfile2D()->Draw(options.c_str());

  i = 0;
  CTIDP->cd(++i);
  tkHistoMap_[TkLayerMap::TIDP_D1]->getTProfile2D()->Draw(options.c_str());
  CTIDP->cd(++i);
  tkHistoMap_[TkLayerMap::TIDP_D2]->getTProfile2D()->Draw(options.c_str());
  CTIDP->cd(++i);
  tkHistoMap_[TkLayerMap::TIDP_D3]->getTProfile2D()->Draw(options.c_str());

  i = 0;
  CTIDM->cd(++i);
  tkHistoMap_[TkLayerMap::TIDM_D1]->getTProfile2D()->Draw(options.c_str());
  CTIDM->cd(++i);
  tkHistoMap_[TkLayerMap::TIDM_D2]->getTProfile2D()->Draw(options.c_str());
  CTIDM->cd(++i);
  tkHistoMap_[TkLayerMap::TIDM_D3]->getTProfile2D()->Draw(options.c_str());

  i = 0;
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L1]->getTProfile2D()->Draw(options.c_str());
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L2]->getTProfile2D()->Draw(options.c_str());
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L3]->getTProfile2D()->Draw(options.c_str());
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L4]->getTProfile2D()->Draw(options.c_str());
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L5]->getTProfile2D()->Draw(options.c_str());
  CTOB->cd(++i);
  tkHistoMap_[TkLayerMap::TOB_L6]->getTProfile2D()->Draw(options.c_str());

  i = 0;
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W1]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W2]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W3]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W4]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W5]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W6]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W7]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W8]->getTProfile2D()->Draw(options.c_str());
  CTECP->cd(++i);
  tkHistoMap_[TkLayerMap::TECP_W9]->getTProfile2D()->Draw(options.c_str());

  i = 0;
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W1]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W2]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W3]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W4]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W5]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W6]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W7]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W8]->getTProfile2D()->Draw(options.c_str());
  CTECM->cd(++i);
  tkHistoMap_[TkLayerMap::TECM_W9]->getTProfile2D()->Draw(options.c_str());

  TFile* f = new TFile(filename.c_str(), mode.c_str());
  CTIB->Write();
  CTIDP->Write();
  CTIDM->Write();
  CTOB->Write();
  CTECP->Write();
  CTECM->Write();
  f->Close();
  delete f;
}