Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-22 22:55:33

0001 #include "DQM/SiStripCommon/interface/TkHistoMap.h"
0002 #include "DQM/SiStripCommon/interface/SiStripFolderOrganizer.h"
0003 
0004 //#define debug_TkHistoMap
0005 
0006 TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap) : HistoNumber(35) {
0007   LogTrace("TkHistoMap") << "TkHistoMap::constructor without parameters";
0008   load(tkDetMap, "", 0.0f, false, false, false);
0009 }
0010 
0011 TkHistoMap::TkHistoMap(
0012     const TkDetMap* tkDetMap, const std::string& path, const std::string& MapName, float baseline, bool mechanicalView)
0013     : HistoNumber(35), MapName_(MapName) {
0014   LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
0015   load(tkDetMap, path, baseline, mechanicalView, false);
0016   dqmStore_->meBookerGetter([this, &path, &baseline, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter&) {
0017     this->createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
0018   });
0019 }
0020 
0021 TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap,
0022                        const std::string& path,
0023                        const std::string& MapName,
0024                        float baseline,
0025                        bool mechanicalView,
0026                        bool isTH2F)
0027     : HistoNumber(35), MapName_(MapName) {
0028   LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
0029   load(tkDetMap, path, baseline, mechanicalView, isTH2F);
0030   dqmStore_->meBookerGetter([this, &path, &baseline, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter&) {
0031     this->createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
0032   });
0033 }
0034 
0035 TkHistoMap::TkHistoMap(const TkDetMap* tkDetMap,
0036                        DQMStore::IBooker& ibooker,
0037                        const std::string& path,
0038                        const std::string& MapName,
0039                        float baseline,
0040                        bool mechanicalView,
0041                        bool isTH2F)
0042     : HistoNumber(35), MapName_(MapName) {
0043   LogTrace("TkHistoMap") << "TkHistoMap::constructor with parameters";
0044   load(tkDetMap, path, baseline, mechanicalView, isTH2F);
0045   createTkHistoMap(ibooker, path, MapName_, baseline, mechanicalView);
0046 }
0047 
0048 void TkHistoMap::load(const TkDetMap* tkDetMap,
0049                       const std::string& path,
0050                       float baseline,
0051                       bool mechanicalView,
0052                       bool isTH2F,
0053                       bool createTkMap) {
0054   cached_detid = 0;
0055   cached_layer = 0;
0056   loadServices();
0057   tkdetmap_ = tkDetMap;
0058   isTH2F_ = isTH2F;
0059 }
0060 
0061 void TkHistoMap::loadServices() {
0062   if (!edm::Service<DQMStore>().isAvailable()) {
0063     edm::LogError("TkHistoMap")
0064         << "\n------------------------------------------"
0065            "\nUnAvailable Service DQMStore: please insert in the configuration file an instance like"
0066            "\n\tprocess.load(\"DQMServices.Core.DQMStore_cfg\")"
0067            "\n------------------------------------------";
0068   }
0069   dqmStore_ = edm::Service<DQMStore>().operator->();
0070 }
0071 
0072 void TkHistoMap::save(const std::string& filename) {
0073   // dqmStore_ only for saving
0074   dqmStore_->save(filename);
0075 }
0076 
0077 void TkHistoMap::loadTkHistoMap(const std::string& path, const std::string& MapName, bool mechanicalView) {
0078   MapName_ = MapName;
0079   tkHistoMap_.resize(HistoNumber);
0080   auto loadMap = [this, &path, mechanicalView](DQMStore::IBooker& ibooker, DQMStore::IGetter& igetter) {
0081     std::string fullName, folder;
0082     for (int layer = 1; layer < HistoNumber; ++layer) {
0083       folder = folderDefinition(ibooker, path, MapName_, layer, mechanicalView, fullName);
0084 #ifdef debug_TkHistoMap
0085       LogTrace("TkHistoMap") << "[TkHistoMap::loadTkHistoMap] folder " << folder << " histoName " << fullName
0086                              << " find " << folder.find_last_of("/") << "  length " << folder.length();
0087 #endif
0088       if (folder.find_last_of('/') != folder.length() - 1)
0089         folder += "/";
0090       tkHistoMap_[layer] = igetter.get(folder + fullName);
0091 #ifdef debug_TkHistoMap
0092       LogTrace("TkHistoMap") << "[TkHistoMap::loadTkHistoMap] folder " << folder << " histoName " << fullName
0093                              << " layer " << layer << " ptr " << tkHistoMap_[layer] << " find "
0094                              << folder.find_last_of("/") << "  length " << folder.length();
0095 #endif
0096     }
0097   };
0098   dqmStore_->meBookerGetter(loadMap);
0099 }
0100 
0101 void TkHistoMap::createTkHistoMap(DQMStore::IBooker& ibooker,
0102                                   const std::string& path,
0103                                   const std::string& MapName,
0104                                   float baseline,
0105                                   bool mechanicalView) {
0106   int nchX;
0107   int nchY;
0108   double lowX, highX;
0109   double lowY, highY;
0110   std::string fullName, folder;
0111 
0112   tkHistoMap_.resize(HistoNumber);
0113   const bool bookTH2F = isTH2F_;
0114   for (int layer = 1; layer < HistoNumber; ++layer) {
0115     folder = folderDefinition(ibooker, path, MapName, layer, mechanicalView, fullName);
0116     tkdetmap_->getComponents(layer, nchX, lowX, highX, nchY, lowY, highY);
0117     MonitorElement* me;
0118     if (bookTH2F == false) {
0119       me = ibooker.bookProfile2D(fullName.c_str(), fullName.c_str(), nchX, lowX, highX, nchY, lowY, highY, 0.0, 0.0);
0120     } else {
0121       me = ibooker.book2D(fullName.c_str(), fullName.c_str(), nchX, lowX, highX, nchY, lowY, highY);
0122     }
0123     //initialize bin content for the not assigned bins
0124     if (baseline != 0) {
0125       for (size_t ix = 1; ix <= (unsigned int)nchX; ++ix)
0126         for (size_t iy = 1; iy <= (unsigned int)nchY; ++iy)
0127           if (!tkdetmap_->getDetFromBin(layer, ix, iy))
0128             me->Fill(1. * (lowX + ix - .5), 1. * (lowY + iy - .5), baseline);
0129     }
0130 
0131     tkHistoMap_[layer] = me;
0132 #ifdef debug_TkHistoMap
0133     LogTrace("TkHistoMap") << "[TkHistoMap::createTkHistoMap] folder " << folder << " histoName " << fullName
0134                            << " layer " << layer << " ptr " << tkHistoMap_[layer];
0135 #endif
0136   }
0137 }
0138 
0139 std::string TkHistoMap::folderDefinition(DQMStore::IBooker& ibooker,
0140                                          std::string folder,
0141                                          const std::string& MapName,
0142                                          int layer,
0143                                          bool mechanicalView,
0144                                          std::string& fullName) {
0145   std::string name = MapName + std::string("_");
0146   fullName = name + TkDetMap::getLayerName(layer);
0147 
0148   if (mechanicalView) {
0149     std::stringstream ss;
0150 
0151     SiStripFolderOrganizer folderOrg;
0152     folderOrg.setSiStripFolderName(folder);
0153 
0154     SiStripDetId::SubDetector subDet;
0155     uint32_t subdetlayer = 0, side = 0;
0156     TkDetMap::getSubDetLayerSide(layer, subDet, subdetlayer, side);
0157     folderOrg.getSubDetLayerFolderName(ss, subDet, subdetlayer, side);
0158     folder = ss.str();
0159   }
0160   ibooker.setCurrentFolder(folder);
0161   return folder;
0162 }
0163 
0164 #include <iostream>
0165 void TkHistoMap::fillFromAscii(const std::string& filename) {
0166   std::ifstream file;
0167   file.open(filename.c_str());
0168   float value;
0169   uint32_t detid;
0170   while (file.good()) {
0171     file >> detid >> value;
0172     fill(detid, value);
0173   }
0174   file.close();
0175 }
0176 
0177 void TkHistoMap::fill(DetId detid, float value) {
0178   int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
0179   TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
0180 #ifdef debug_TkHistoMap
0181   LogTrace("TkHistoMap") << "[TkHistoMap::fill] Fill detid " << detid.rawId() << " Layer " << layer << " value "
0182                          << value << " ix,iy " << xybin.ix << " " << xybin.iy << " " << xybin.x << " " << xybin.y << " "
0183                          << tkHistoMap_[layer]->getTProfile2D()->GetName();
0184 #endif
0185   if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
0186     tkHistoMap_[layer]->getTProfile2D()->Fill(xybin.x, xybin.y, value);
0187   else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
0188     tkHistoMap_[layer]->getTH2F()->Fill(xybin.x, xybin.y, value);
0189 
0190 #ifdef debug_TkHistoMap
0191   LogTrace("TkHistoMap") << "[TkHistoMap::fill] "
0192                          << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(xybin.ix, xybin.iy);
0193   for (size_t ii = 0; ii < 4; ii++)
0194     for (size_t jj = 0; jj < 11; jj++) {
0195       if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
0196         LogTrace("TkHistoMap") << "[TkHistoMap::fill] " << ii << " " << jj << " "
0197                                << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(ii, jj);
0198       if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
0199         LogTrace("TkHistoMap") << "[TkHistoMap::fill] " << ii << " " << jj << " "
0200                                << tkHistoMap_[layer]->getTH2F()->GetBinContent(ii, jj);
0201     }
0202 #endif
0203 }
0204 
0205 void TkHistoMap::setBinContent(DetId detid, float value) {
0206   int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
0207   TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
0208   if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D) {
0209     tkHistoMap_[layer]->getTProfile2D()->SetBinEntries(tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy),
0210                                                        1);
0211     tkHistoMap_[layer]->getTProfile2D()->SetBinContent(tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy),
0212                                                        value);
0213   } else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F) {
0214     tkHistoMap_[layer]->setBinContent(xybin.ix, xybin.iy, value);
0215   }
0216 
0217 #ifdef debug_TkHistoMap
0218   LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent]  setBinContent detid " << detid.rawId() << " Layer " << layer
0219                          << " value " << value << " ix,iy " << xybin.ix << " " << xybin.iy << " " << xybin.x << " "
0220                          << xybin.y << " " << tkHistoMap_[layer]->getTProfile2D()->GetName() << " bin "
0221                          << tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy);
0222 
0223   LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent] "
0224                          << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(xybin.ix, xybin.iy);
0225   for (size_t ii = 0; ii < 4; ii++)
0226     for (size_t jj = 0; jj < 11; jj++) {
0227       LogTrace("TkHistoMap") << "[TkHistoMap::setbincontent] " << ii << " " << jj << " "
0228                              << tkHistoMap_[layer]->getTProfile2D()->GetBinContent(ii, jj);
0229     }
0230 #endif
0231 }
0232 
0233 void TkHistoMap::add(DetId detid, float value) {
0234 #ifdef debug_TkHistoMap
0235   LogTrace("TkHistoMap") << "[TkHistoMap::add]";
0236 #endif
0237   int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
0238   TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
0239   if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TPROFILE2D)
0240     setBinContent(detid,
0241                   tkHistoMap_[layer]->getTProfile2D()->GetBinContent(
0242                       tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy)) +
0243                       value);
0244   else if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
0245     setBinContent(
0246         detid,
0247         tkHistoMap_[layer]->getTH2F()->GetBinContent(tkHistoMap_[layer]->getTH2F()->GetBin(xybin.ix, xybin.iy)) +
0248             value);
0249 }
0250 
0251 float TkHistoMap::getValue(DetId detid) {
0252   int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
0253   TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
0254 
0255   if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
0256     return tkHistoMap_[layer]->getTH2F()->GetBinContent(tkHistoMap_[layer]->getTH2F()->GetBin(xybin.ix, xybin.iy));
0257   else
0258     return tkHistoMap_[layer]->getTProfile2D()->GetBinContent(
0259         tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy));
0260 }
0261 float TkHistoMap::getEntries(DetId detid) {
0262   int16_t layer = tkdetmap_->findLayer(detid, cached_detid, cached_layer, cached_XYbin);
0263   TkLayerMap::XYbin xybin = tkdetmap_->getXY(detid, cached_detid, cached_layer, cached_XYbin);
0264   if (tkHistoMap_[layer]->kind() == MonitorElement::Kind::TH2F)
0265     return 1;
0266   else
0267     return tkHistoMap_[layer]->getTProfile2D()->GetBinEntries(
0268         tkHistoMap_[layer]->getTProfile2D()->GetBin(xybin.ix, xybin.iy));
0269 }
0270 
0271 void TkHistoMap::dumpInTkMap(TrackerMap* tkmap, bool dumpEntries) {
0272   for (int layer = 1; layer < HistoNumber; ++layer) {
0273     // std::vector<uint32_t> dets;
0274     // tkdetmap_->getDetsForLayer(layer,dets);
0275     std::vector<DetId> dets = tkdetmap_->getDetsForLayer(layer);
0276     for (size_t i = 0; i < dets.size(); ++i) {
0277       if (dets[i] > 0) {
0278         if (getEntries(dets[i]) > 0) {
0279           tkmap->fill(dets[i], dumpEntries ? getEntries(dets[i]) : getValue(dets[i]));
0280         }
0281       }
0282     }
0283   }
0284 }
0285 
0286 #include "TCanvas.h"
0287 #include "TFile.h"
0288 void TkHistoMap::saveAsCanvas(const std::string& filename, const std::string& options, const std::string& mode) {
0289   //  TCanvas C(MapName_,MapName_,200,10,900,700);
0290   TCanvas* CTIB =
0291       new TCanvas(std::string("Canvas_" + MapName_ + "TIB").c_str(), std::string("Canvas_" + MapName_ + "TIB").c_str());
0292   TCanvas* CTOB =
0293       new TCanvas(std::string("Canvas_" + MapName_ + "TOB").c_str(), std::string("Canvas_" + MapName_ + "TOB").c_str());
0294   TCanvas* CTIDP = new TCanvas(std::string("Canvas_" + MapName_ + "TIDP").c_str(),
0295                                std::string("Canvas_" + MapName_ + "TIDP").c_str());
0296   TCanvas* CTIDM = new TCanvas(std::string("Canvas_" + MapName_ + "TIDM").c_str(),
0297                                std::string("Canvas_" + MapName_ + "TIDM").c_str());
0298   TCanvas* CTECP = new TCanvas(std::string("Canvas_" + MapName_ + "TECP").c_str(),
0299                                std::string("Canvas_" + MapName_ + "TECP").c_str());
0300   TCanvas* CTECM = new TCanvas(std::string("Canvas_" + MapName_ + "TECM").c_str(),
0301                                std::string("Canvas_" + MapName_ + "TECM").c_str());
0302   CTIB->Divide(2, 2);
0303   CTOB->Divide(2, 3);
0304   CTIDP->Divide(1, 3);
0305   CTIDM->Divide(1, 3);
0306   CTECP->Divide(3, 3);
0307   CTECM->Divide(3, 3);
0308 
0309   int i;
0310   i = 0;
0311   CTIB->cd(++i);
0312   tkHistoMap_[TkLayerMap::TIB_L1]->getTProfile2D()->Draw(options.c_str());
0313   CTIB->cd(++i);
0314   tkHistoMap_[TkLayerMap::TIB_L2]->getTProfile2D()->Draw(options.c_str());
0315   CTIB->cd(++i);
0316   tkHistoMap_[TkLayerMap::TIB_L3]->getTProfile2D()->Draw(options.c_str());
0317   CTIB->cd(++i);
0318   tkHistoMap_[TkLayerMap::TIB_L4]->getTProfile2D()->Draw(options.c_str());
0319 
0320   i = 0;
0321   CTIDP->cd(++i);
0322   tkHistoMap_[TkLayerMap::TIDP_D1]->getTProfile2D()->Draw(options.c_str());
0323   CTIDP->cd(++i);
0324   tkHistoMap_[TkLayerMap::TIDP_D2]->getTProfile2D()->Draw(options.c_str());
0325   CTIDP->cd(++i);
0326   tkHistoMap_[TkLayerMap::TIDP_D3]->getTProfile2D()->Draw(options.c_str());
0327 
0328   i = 0;
0329   CTIDM->cd(++i);
0330   tkHistoMap_[TkLayerMap::TIDM_D1]->getTProfile2D()->Draw(options.c_str());
0331   CTIDM->cd(++i);
0332   tkHistoMap_[TkLayerMap::TIDM_D2]->getTProfile2D()->Draw(options.c_str());
0333   CTIDM->cd(++i);
0334   tkHistoMap_[TkLayerMap::TIDM_D3]->getTProfile2D()->Draw(options.c_str());
0335 
0336   i = 0;
0337   CTOB->cd(++i);
0338   tkHistoMap_[TkLayerMap::TOB_L1]->getTProfile2D()->Draw(options.c_str());
0339   CTOB->cd(++i);
0340   tkHistoMap_[TkLayerMap::TOB_L2]->getTProfile2D()->Draw(options.c_str());
0341   CTOB->cd(++i);
0342   tkHistoMap_[TkLayerMap::TOB_L3]->getTProfile2D()->Draw(options.c_str());
0343   CTOB->cd(++i);
0344   tkHistoMap_[TkLayerMap::TOB_L4]->getTProfile2D()->Draw(options.c_str());
0345   CTOB->cd(++i);
0346   tkHistoMap_[TkLayerMap::TOB_L5]->getTProfile2D()->Draw(options.c_str());
0347   CTOB->cd(++i);
0348   tkHistoMap_[TkLayerMap::TOB_L6]->getTProfile2D()->Draw(options.c_str());
0349 
0350   i = 0;
0351   CTECP->cd(++i);
0352   tkHistoMap_[TkLayerMap::TECP_W1]->getTProfile2D()->Draw(options.c_str());
0353   CTECP->cd(++i);
0354   tkHistoMap_[TkLayerMap::TECP_W2]->getTProfile2D()->Draw(options.c_str());
0355   CTECP->cd(++i);
0356   tkHistoMap_[TkLayerMap::TECP_W3]->getTProfile2D()->Draw(options.c_str());
0357   CTECP->cd(++i);
0358   tkHistoMap_[TkLayerMap::TECP_W4]->getTProfile2D()->Draw(options.c_str());
0359   CTECP->cd(++i);
0360   tkHistoMap_[TkLayerMap::TECP_W5]->getTProfile2D()->Draw(options.c_str());
0361   CTECP->cd(++i);
0362   tkHistoMap_[TkLayerMap::TECP_W6]->getTProfile2D()->Draw(options.c_str());
0363   CTECP->cd(++i);
0364   tkHistoMap_[TkLayerMap::TECP_W7]->getTProfile2D()->Draw(options.c_str());
0365   CTECP->cd(++i);
0366   tkHistoMap_[TkLayerMap::TECP_W8]->getTProfile2D()->Draw(options.c_str());
0367   CTECP->cd(++i);
0368   tkHistoMap_[TkLayerMap::TECP_W9]->getTProfile2D()->Draw(options.c_str());
0369 
0370   i = 0;
0371   CTECM->cd(++i);
0372   tkHistoMap_[TkLayerMap::TECM_W1]->getTProfile2D()->Draw(options.c_str());
0373   CTECM->cd(++i);
0374   tkHistoMap_[TkLayerMap::TECM_W2]->getTProfile2D()->Draw(options.c_str());
0375   CTECM->cd(++i);
0376   tkHistoMap_[TkLayerMap::TECM_W3]->getTProfile2D()->Draw(options.c_str());
0377   CTECM->cd(++i);
0378   tkHistoMap_[TkLayerMap::TECM_W4]->getTProfile2D()->Draw(options.c_str());
0379   CTECM->cd(++i);
0380   tkHistoMap_[TkLayerMap::TECM_W5]->getTProfile2D()->Draw(options.c_str());
0381   CTECM->cd(++i);
0382   tkHistoMap_[TkLayerMap::TECM_W6]->getTProfile2D()->Draw(options.c_str());
0383   CTECM->cd(++i);
0384   tkHistoMap_[TkLayerMap::TECM_W7]->getTProfile2D()->Draw(options.c_str());
0385   CTECM->cd(++i);
0386   tkHistoMap_[TkLayerMap::TECM_W8]->getTProfile2D()->Draw(options.c_str());
0387   CTECM->cd(++i);
0388   tkHistoMap_[TkLayerMap::TECM_W9]->getTProfile2D()->Draw(options.c_str());
0389 
0390   TFile* f = new TFile(filename.c_str(), mode.c_str());
0391   CTIB->Write();
0392   CTIDP->Write();
0393   CTIDM->Write();
0394   CTOB->Write();
0395   CTECP->Write();
0396   CTECM->Write();
0397   f->Close();
0398   delete f;
0399 }