Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:08:43

0001 #include "DQM/SiStripMonitorClient/interface/SiStripSummaryCreator.h"
0002 #include "DQM/SiStripMonitorClient/interface/SiStripConfigParser.h"
0003 #include "DQMServices/Core/interface/DQMStore.h"
0004 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0005 #include "FWCore/ParameterSet/interface/FileInPath.h"
0006 
0007 #include <iostream>
0008 
0009 SiStripSummaryCreator::SiStripSummaryCreator() {
0010   edm::LogInfo("SiStripSummaryCreator") << " Creating SiStripSummaryCreator "
0011                                         << "\n";
0012 }
0013 
0014 SiStripSummaryCreator::~SiStripSummaryCreator() {
0015   edm::LogInfo("SiStripSummaryCreator") << " Deleting SiStripSummaryCreator "
0016                                         << "\n";
0017 }
0018 
0019 bool SiStripSummaryCreator::readConfiguration(std::string const& file_path) {
0020   summaryMEs_.clear();
0021   SiStripConfigParser config_parser;
0022   config_parser.getDocument(edm::FileInPath(file_path).fullPath());
0023   if (!config_parser.getFrequencyForSummary(summaryFrequency_)) {
0024     std::cout << "SiStripSummaryCreator::readConfiguration: Failed to read Summary configuration parameters!! ";
0025     summaryFrequency_ = -1;
0026     return false;
0027   }
0028   if (!config_parser.getMENamesForSummary(summaryMEs_)) {
0029     std::cout << "SiStripSummaryCreator::readConfiguration: Failed to read Summary configuration parameters!! ";
0030     return false;
0031   }
0032   return true;
0033 }
0034 
0035 void SiStripSummaryCreator::setSummaryMENames(std::map<std::string, std::string>& me_names) {
0036   summaryMEs_.clear();
0037   for (std::map<std::string, std::string>::const_iterator isum = me_names.begin(); isum != me_names.end(); isum++) {
0038     summaryMEs_.insert(std::pair<std::string, std::string>(isum->first, isum->second));
0039   }
0040 }
0041 
0042 //
0043 // -- Browse through the Folder Structure
0044 //
0045 void SiStripSummaryCreator::createSummary(DQMStore& dqm_store) {
0046   if (summaryMEs_.empty())
0047     return;
0048   std::string currDir = dqm_store.pwd();
0049   std::vector<std::string> subdirs = dqm_store.getSubdirs();
0050   int nmod = 0;
0051   for (std::vector<std::string>::const_iterator it = subdirs.begin(); it != subdirs.end(); it++) {
0052     if ((*it).find("module_") == std::string::npos)
0053       continue;
0054     nmod++;
0055   }
0056   if (nmod > 0) {
0057     fillSummaryHistos(dqm_store);
0058   } else {
0059     for (std::vector<std::string>::const_iterator it = subdirs.begin(); it != subdirs.end(); it++) {
0060       dqm_store.cd(*it);
0061       createSummary(dqm_store);
0062       dqm_store.goUp();
0063     }
0064     fillGrandSummaryHistos(dqm_store);
0065   }
0066 }
0067 //
0068 // -- Create and Fill Summary Histograms at the lowest level of the structure
0069 //
0070 void SiStripSummaryCreator::fillSummaryHistos(DQMStore& dqm_store) {
0071   std::string currDir = dqm_store.pwd();
0072   std::map<std::string, MonitorElement*> MEMap;
0073   std::vector<std::string> subdirs = dqm_store.getSubdirs();
0074   if (subdirs.empty())
0075     return;
0076 
0077   for (std::map<std::string, std::string>::const_iterator isum = summaryMEs_.begin(); isum != summaryMEs_.end();
0078        isum++) {
0079     std::string name = isum->first;
0080     int iBinStep = 0;
0081     int ndet = 0;
0082     std::string htype = isum->second;
0083     for (std::vector<std::string>::const_iterator it = subdirs.begin(); it != subdirs.end(); it++) {
0084       if ((*it).find("module_") == std::string::npos)
0085         continue;
0086       dqm_store.cd(*it);
0087       ndet++;
0088       std::vector<MonitorElement*> contents = dqm_store.getContents(dqm_store.pwd());
0089       dqm_store.goUp();
0090       for (std::vector<MonitorElement*>::const_iterator im = contents.begin(); im != contents.end(); im++) {
0091         MonitorElement* me_i = (*im);
0092         if (!me_i)
0093           continue;
0094         std::string name_i = me_i->getName();
0095         if (name_i.find(name) == std::string::npos)
0096           continue;
0097         std::map<std::string, MonitorElement*>::iterator iPos = MEMap.find(name);
0098         MonitorElement* me;
0099         // Get the Summary ME
0100         if (iPos == MEMap.end()) {
0101           me = getSummaryME(dqm_store, name, htype);
0102           MEMap.insert(std::pair<std::string, MonitorElement*>(name, me));
0103         } else
0104           me = iPos->second;
0105         // Fill it now
0106         fillHistos(ndet, iBinStep, htype, me_i, me);
0107         iBinStep += me_i->getNbinsX();
0108         break;
0109       }
0110     }
0111   }
0112 }
0113 
0114 //
0115 //  -- Fill Summary Histogram at higher level
0116 //
0117 void SiStripSummaryCreator::fillGrandSummaryHistos(DQMStore& dqm_store) {
0118   std::map<std::string, MonitorElement*> MEMap;
0119   std::string currDir = dqm_store.pwd();
0120   std::string dir_name = currDir.substr(currDir.find_last_of('/') + 1);
0121   if ((dir_name.find("SiStrip") == 0) || (dir_name.find("Collector") == 0) || (dir_name.find("MechanicalView") == 0) ||
0122       (dir_name.find("FU") == 0))
0123     return;
0124   std::vector<std::string> subdirs = dqm_store.getSubdirs();
0125   if (subdirs.empty())
0126     return;
0127   ;
0128   for (std::map<std::string, std::string>::const_iterator isum = summaryMEs_.begin(); isum != summaryMEs_.end();
0129        isum++) {
0130     std::string name, summary_name;
0131     name = isum->first;
0132     if (isum->second == "sum" || isum->second == "sum")
0133       summary_name = "Summary_" + isum->first;
0134     else
0135       summary_name = "Summary_Mean" + isum->first;
0136     std::string htype = isum->second;
0137     int ibinStep = 0;
0138     for (std::vector<std::string>::const_iterator it = subdirs.begin(); it != subdirs.end(); it++) {
0139       dqm_store.cd(*it);
0140       std::vector<MonitorElement*> contents = dqm_store.getContents(dqm_store.pwd());
0141       dqm_store.goUp();
0142       for (std::vector<MonitorElement*>::const_iterator im = contents.begin(); im != contents.end(); im++) {
0143         MonitorElement* me_i = (*im);
0144         if (!me_i)
0145           continue;
0146         std::string name_i = me_i->getName();
0147         if (name_i.find((summary_name)) != std::string::npos) {
0148           std::map<std::string, MonitorElement*>::iterator iPos = MEMap.find(name);
0149           MonitorElement* me;
0150           if (iPos == MEMap.end()) {
0151             if (htype == "sum" || htype == "Sum") {
0152               me = getSummaryME(dqm_store, name, htype);
0153             } else {
0154               me = getSummaryME(dqm_store, name, "bin-by-bin");
0155             }
0156             MEMap.insert(std::pair<std::string, MonitorElement*>(name, me));
0157           } else
0158             me = iPos->second;
0159           if (htype == "sum" || htype == "Sum") {
0160             fillHistos(0, ibinStep, htype, me_i, me);
0161           } else {
0162             fillHistos(0, ibinStep, "bin-by-bin", me_i, me);
0163           }
0164           ibinStep += me_i->getNbinsX();
0165           break;
0166         }
0167       }
0168     }
0169   }
0170 }
0171 
0172 SiStripSummaryCreator::MonitorElement* SiStripSummaryCreator::getSummaryME(DQMStore& dqm_store,
0173                                                                            std::string& name,
0174                                                                            std::string htype) {
0175   MonitorElement* me = nullptr;
0176   std::string currDir = dqm_store.pwd();
0177   std::string sum_name, tag_name;
0178 
0179   std::string dname = currDir.substr(currDir.find_last_of('/') + 1);
0180   if (dname.find('_') != std::string::npos)
0181     dname.insert(dname.find('_'), "_");
0182   if (htype == "sum" && htype == "Sum") {
0183     sum_name = "Summary" + name + "__" + dname;
0184     tag_name = "Summary" + name;
0185   } else {
0186     sum_name = "Summary_Mean" + name + "__" + dname;
0187     tag_name = "Summary_Mean" + name;
0188   }
0189   // If already booked
0190   std::vector<MonitorElement*> contents = dqm_store.getContents(currDir);
0191   for (std::vector<MonitorElement*>::const_iterator im = contents.begin(); im != contents.end(); im++) {
0192     MonitorElement* me = (*im);
0193     if (!me)
0194       continue;
0195     std::string me_name = me->getName();
0196     if (me_name.find(sum_name) == 0) {
0197       if (me->kind() == MonitorElement::Kind::TH1F || me->kind() == MonitorElement::Kind::TH2F ||
0198           me->kind() == MonitorElement::Kind::TPROFILE) {
0199         TH1* hist1 = me->getTH1();
0200         if (hist1) {
0201           hist1->Reset();
0202           return me;
0203         }
0204       }
0205     }
0206   }
0207   std::map<int, std::string> tags;
0208   if (!me) {
0209     int nBins = 0;
0210     std::vector<std::string> subdirs = dqm_store.getSubdirs();
0211     // set # of bins of the histogram
0212     if (htype == "mean" || htype == "Mean") {
0213       nBins = subdirs.size();
0214       me = dqm_store.book1D(sum_name, sum_name, nBins, 0.5, nBins + 0.5);
0215       int ibin = 0;
0216       for (auto const& subdir : subdirs) {
0217         std::string subdir_name = subdir.substr(subdir.find_last_of('/') + 1);
0218         ++ibin;
0219         tags.emplace(ibin, subdir_name);
0220       }
0221     } else if (htype == "bin-by-bin" || htype == "Bin-by-Bin") {
0222       for (auto const& subdir : subdirs) {
0223         dqm_store.cd(subdir);
0224         std::string subdir_name = subdir.substr(subdir.find_last_of('/') + 1);
0225         auto const& s_contents = dqm_store.getContents(dqm_store.pwd());
0226         for (auto const* s_me : s_contents) {
0227           if (!s_me)
0228             continue;
0229           std::string s_me_name = s_me->getName();
0230           if (s_me_name.find(name) == 0 || s_me_name.find(tag_name) == 0) {
0231             int ibin = s_me->getNbinsX();
0232             nBins += ibin;
0233             tags.emplace(nBins - ibin / 2, subdir_name);
0234             break;
0235           }
0236         }
0237         dqm_store.goUp();
0238       }
0239       me = dqm_store.book1D(sum_name, sum_name, nBins, 0.5, nBins + 0.5);
0240     } else if (htype == "sum" || htype == "Sum") {
0241       for (auto const& subdir : subdirs) {
0242         dqm_store.cd(subdir);
0243         auto const& s_contents = dqm_store.getContents(dqm_store.pwd());
0244         dqm_store.goUp();
0245         for (auto* s_me : s_contents) {
0246           if (!s_me)
0247             continue;
0248           std::string s_me_name = s_me->getName();
0249           if (s_me_name.find(name) == std::string::npos)
0250             continue;
0251           if (s_me->kind() == MonitorElement::Kind::TH1F) {
0252             TH1F* hist1 = s_me->getTH1F();
0253             if (hist1) {
0254               nBins = s_me->getNbinsX();
0255               me = dqm_store.book1D(
0256                   sum_name, sum_name, nBins, hist1->GetXaxis()->GetXmin(), hist1->GetXaxis()->GetXmax());
0257               break;
0258             }
0259           }
0260         }
0261       }
0262     }
0263   }
0264   // Set the axis title
0265   if (me && me->kind() == MonitorElement::Kind::TH1F && (htype != "sum" || htype != "Sum")) {
0266     TH1F* hist = me->getTH1F();
0267     if (hist) {
0268       if (name.find("NoisyStrips") != std::string::npos)
0269         hist->GetYaxis()->SetTitle("Noisy Strips (%)");
0270       else
0271         hist->GetYaxis()->SetTitle(name.c_str());
0272 
0273       for (auto const& [bin, label] : tags) {
0274         hist->GetXaxis()->SetBinLabel(bin, label.c_str());
0275       }
0276       hist->LabelsOption("uv");
0277     }
0278   }
0279   return me;
0280 }
0281 
0282 void SiStripSummaryCreator::fillHistos(
0283     int const ival, int const istep, std::string const htype, MonitorElement* me_src, MonitorElement* me) {
0284   if (me->getTH1()) {
0285     TH1F* hist1 = nullptr;
0286     TH2F* hist2 = nullptr;
0287     if (me->kind() == MonitorElement::Kind::TH1F)
0288       hist1 = me->getTH1F();
0289     if (me->kind() == MonitorElement::Kind::TH2F)
0290       hist2 = me->getTH2F();
0291 
0292     int nbins = me_src->getNbinsX();
0293     const std::string& name = me_src->getName();
0294     if (htype == "mean" || htype == "Mean") {
0295       if (hist2 && name.find("NoisyStrips") != std::string::npos) {
0296         float bad = 0.0;
0297         float entries = me_src->getEntries();
0298         if (entries > 0.0) {
0299           float binEntry = entries / nbins;
0300           for (int k = 1; k < nbins + 1; k++) {
0301             float noisy = me_src->getBinContent(k, 3) + me_src->getBinContent(k, 5);
0302             float dead = me_src->getBinContent(k, 2) + me_src->getBinContent(k, 4);
0303             if (noisy >= binEntry * 0.5 || dead >= binEntry * 0.5)
0304               bad++;
0305           }
0306           bad = bad * 100.0 / nbins;
0307           me->Fill(ival, bad);
0308         }
0309       } else
0310         me->Fill(ival, me_src->getMean());
0311     } else if (htype == "bin-by-bin" || htype == "Bin-by-Bin") {
0312       for (int k = 1; k < nbins + 1; k++) {
0313         me->setBinContent(istep + k, me_src->getBinContent(k));
0314       }
0315     } else if (htype == "sum" || htype == "Sum") {
0316       if (hist1) {
0317         for (int k = 1; k < nbins + 1; k++) {
0318           float val = me_src->getBinContent(k) + me->getBinContent(k);
0319           me->setBinContent(k, val);
0320         }
0321       }
0322     }
0323   }
0324 }