Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 13:11:31

0001 #include "DQM/SiStripCommissioningClients/interface/CommissioningHistograms.h"
0002 #include "CondFormats/SiStripObjects/interface/CommissioningAnalysis.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripFedKey.h"
0005 #include "DataFormats/SiStripCommon/interface/SiStripDetKey.h"
0006 #include "DQM/SiStripCommissioningSummary/interface/SummaryGenerator.h"
0007 #include "DataFormats/SiStripCommon/interface/SiStripConstants.h"
0008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0009 #include <iomanip>
0010 
0011 using namespace std;
0012 using namespace sistrip;
0013 
0014 // -----------------------------------------------------------------------------
0015 /** */
0016 CommissioningHistograms::CommissioningHistograms(const edm::ParameterSet& pset,
0017                                                  DQMStore* bei,
0018                                                  const sistrip::RunType& task)
0019     : factory_(nullptr),
0020       task_(task),
0021       bei_(bei),
0022       data_(),
0023       histos_(),
0024       pset_(pset),
0025       mask_(pset.existsAs<bool>("vetoModules") ? pset.getParameter<bool>("vetoModules") : true),
0026       fedMaskVector_(pset.existsAs<std::vector<uint32_t> >("fedMaskVector")
0027                          ? pset.getParameter<std::vector<uint32_t> >("fedMaskVector")
0028                          : std::vector<uint32_t>()),
0029       fecMaskVector_(pset.existsAs<std::vector<uint32_t> >("fecMaskVector")
0030                          ? pset.getParameter<std::vector<uint32_t> >("fecMaskVector")
0031                          : std::vector<uint32_t>()),
0032       ringVector_(pset.existsAs<std::vector<uint32_t> >("ringVector")
0033                       ? pset.getParameter<std::vector<uint32_t> >("ringVector")
0034                       : std::vector<uint32_t>()),
0035       ccuVector_(pset.existsAs<std::vector<uint32_t> >("ccuVector")
0036                      ? pset.getParameter<std::vector<uint32_t> >("ccuVector")
0037                      : std::vector<uint32_t>()),
0038       i2cChanVector_(pset.existsAs<std::vector<uint32_t> >("i2cChanVector")
0039                          ? pset.getParameter<std::vector<uint32_t> >("i2cChanVector")
0040                          : std::vector<uint32_t>()),
0041       lldChanVector_(pset.existsAs<std::vector<uint32_t> >("lldChanVector")
0042                          ? pset.getParameter<std::vector<uint32_t> >("lldChanVector")
0043                          : std::vector<uint32_t>()),
0044       dataWithMask_(),
0045       dataWithMaskCached_(false) {
0046   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0047                          << " Constructing object...";
0048 
0049   // DQMStore
0050   if (!bei_) {
0051     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0052                                 << " NULL pointer to DQMStore!";
0053   }
0054 
0055   clearHistosMap();
0056 }
0057 
0058 // -----------------------------------------------------------------------------
0059 /** */
0060 CommissioningHistograms::CommissioningHistograms()
0061     : factory_(nullptr),
0062       task_(sistrip::UNDEFINED_RUN_TYPE),
0063       bei_(nullptr),
0064       data_(),
0065       histos_(),
0066       mask_(true),
0067       dataWithMask_(),
0068       dataWithMaskCached_(false) {
0069   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0070                          << " Constructing object...";
0071 }
0072 
0073 // -----------------------------------------------------------------------------
0074 /** */
0075 CommissioningHistograms::~CommissioningHistograms() {
0076   LogTrace(mlDqmClient_) << "[" << __PRETTY_FUNCTION__ << "]"
0077                          << " Destructing object...";
0078   clearHistosMap();
0079   //@@ do not delete BEI ptrs!
0080 }
0081 
0082 // -----------------------------------------------------------------------------
0083 /** */
0084 void CommissioningHistograms::Histo::print(std::stringstream& ss) const {
0085   ss << " [Histo::" << __func__ << "]" << std::endl
0086      << " Histogram title   : " << title_ << std::endl
0087      << " MonitorElement*   : 0x" << std::hex << std::setw(8) << std::setfill('0') << me_ << std::endl
0088      << std::dec << " CollateME*        : 0x" << std::hex << std::setw(8) << std::setfill('0') << cme_ << std::endl
0089      << std::dec;
0090 }
0091 
0092 // -----------------------------------------------------------------------------
0093 //
0094 uint32_t CommissioningHistograms::runNumber(DQMStore* const bei, const std::vector<std::string>& contents) {
0095   // Check if histograms present
0096   if (contents.empty()) {
0097     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0098                                 << " Found no histograms!";
0099     return 0;
0100   }
0101 
0102   // Iterate through added contents
0103   std::vector<std::string>::const_iterator istr = contents.begin();
0104   while (istr != contents.end()) {
0105     // Extract source directory path
0106     std::string source_dir = istr->substr(0, istr->find(":"));
0107 
0108     // Generate corresponding client path (removing trailing "/")
0109     SiStripFecKey path(source_dir);
0110     std::string client_dir = path.path();
0111     std::string slash = client_dir.substr(client_dir.size() - 1, 1);
0112     if (slash == sistrip::dir_) {
0113       client_dir = client_dir.substr(0, client_dir.size() - 1);
0114     }
0115     client_dir = std::string(sistrip::collate_) + sistrip::dir_ + client_dir;
0116 
0117     // Iterate though MonitorElements from source directory
0118     std::vector<MonitorElement*> me_list = bei->getContents(source_dir);
0119     std::vector<MonitorElement*>::iterator ime = me_list.begin();
0120     for (; ime != me_list.end(); ime++) {
0121       if (!(*ime)) {
0122         edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0123                                     << " NULL pointer to MonitorElement!";
0124         continue;
0125       }
0126 
0127       // Search for run type in string
0128       std::string title = (*ime)->getName();
0129       std::string::size_type pos = title.find(sistrip::runNumber_);
0130 
0131       // Extract run number from string
0132       if (pos != std::string::npos) {
0133         std::string value = title.substr(pos + sizeof(sistrip::runNumber_), std::string::npos);
0134         if (!value.empty()) {
0135           LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0136                                  << " Found string \"" << title.substr(pos, std::string::npos) << "\" with value \""
0137                                  << value << "\"";
0138           if (!(bei->get(client_dir + "/" + title.substr(pos, std::string::npos)))) {
0139             bei->setCurrentFolder(client_dir);
0140             bei->bookString(title.substr(pos, std::string::npos), value);
0141             LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0142                                    << " Booked string \"" << title.substr(pos, std::string::npos)
0143                                    << "\" in directory \"" << client_dir << "\"";
0144           }
0145           uint32_t run;
0146           std::stringstream ss;
0147           ss << value;
0148           ss >> std::dec >> run;
0149           return run;
0150         }
0151       }
0152     }
0153 
0154     istr++;
0155   }
0156   return 0;
0157 }
0158 
0159 // -----------------------------------------------------------------------------
0160 /** Extract run type string from "added contents". */
0161 sistrip::RunType CommissioningHistograms::runType(DQMStore* const bei, const std::vector<std::string>& contents) {
0162   // Check if histograms present
0163   if (contents.empty()) {
0164     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0165                                 << " Found no histograms!";
0166     return sistrip::UNKNOWN_RUN_TYPE;
0167   }
0168 
0169   // Iterate through added contents
0170   std::vector<std::string>::const_iterator istr = contents.begin();
0171   while (istr != contents.end()) {
0172     // Extract source directory path
0173     std::string source_dir = istr->substr(0, istr->find(":"));
0174 
0175     // Generate corresponding client path (removing trailing "/")
0176     SiStripFecKey path(source_dir);
0177     std::string client_dir = path.path();
0178     std::string slash = client_dir.substr(client_dir.size() - 1, 1);
0179     if (slash == sistrip::dir_) {
0180       client_dir = client_dir.substr(0, client_dir.size() - 1);
0181     }
0182     client_dir = std::string(sistrip::collate_) + sistrip::dir_ + client_dir;
0183 
0184     // Iterate though MonitorElements from source directory
0185     std::vector<MonitorElement*> me_list = bei->getContents(source_dir);
0186 
0187     if (me_list.empty()) {
0188       edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0189                                   << " No MonitorElements found in dir " << source_dir;
0190       return sistrip::UNKNOWN_RUN_TYPE;
0191     }
0192 
0193     std::vector<MonitorElement*>::iterator ime = me_list.begin();
0194     for (; ime != me_list.end(); ime++) {
0195       if (!(*ime)) {
0196         edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0197                                     << " NULL pointer to MonitorElement!";
0198         continue;
0199       }
0200 
0201       // Search for run type in string
0202       std::string title = (*ime)->getName();
0203       std::string::size_type pos = title.find(sistrip::taskId_);
0204 
0205       // Extract commissioning task from string
0206       if (pos != std::string::npos) {
0207         std::string value = title.substr(pos + sizeof(sistrip::taskId_), std::string::npos);
0208         if (!value.empty()) {
0209           LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0210                                  << " Found string \"" << title.substr(pos, std::string::npos) << "\" with value \""
0211                                  << value << "\"";
0212           if (!(bei->get(client_dir + sistrip::dir_ + title.substr(pos, std::string::npos)))) {
0213             bei->setCurrentFolder(client_dir);
0214             bei->bookString(title.substr(pos, std::string::npos), value);
0215             LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0216                                    << " Booked string \"" << title.substr(pos, std::string::npos)
0217                                    << "\" in directory \"" << client_dir << "\"";
0218           }
0219           return SiStripEnumsAndStrings::runType(value);
0220         }
0221       }
0222     }
0223 
0224     istr++;
0225   }
0226 
0227   edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0228                               << " Unable to extract RunType!";
0229   return sistrip::UNKNOWN_RUN_TYPE;
0230 }
0231 
0232 // -----------------------------------------------------------------------------
0233 //
0234 void CommissioningHistograms::copyCustomInformation(DQMStore* const bei, const std::vector<std::string>& contents) {
0235   // Check if histograms present
0236   if (contents.empty()) {
0237     edm::LogWarning(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0238                                   << " Found no histograms!";
0239     return;
0240   }
0241 
0242   // Iterate through added contents
0243   std::vector<std::string>::const_iterator istr = contents.begin();
0244   while (istr != contents.end()) {
0245     // Extract source directory path
0246     std::string source_dir = istr->substr(0, istr->find(":"));
0247 
0248     // Generate corresponding client path (removing trailing "/")
0249     SiStripFecKey path(source_dir);
0250     std::string client_dir = path.path();
0251     std::string slash = client_dir.substr(client_dir.size() - 1, 1);
0252     if (slash == sistrip::dir_) {
0253       client_dir = client_dir.substr(0, client_dir.size() - 1);
0254     }
0255 
0256     // Iterate though MonitorElements from source directory
0257     std::vector<MonitorElement*> me_list = bei->getContents(source_dir);
0258     std::vector<MonitorElement*>::iterator ime = me_list.begin();
0259     for (; ime != me_list.end(); ime++) {
0260       if (!(*ime)) {
0261         edm::LogWarning(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0262                                       << " NULL pointer to MonitorElement!";
0263         continue;
0264       }
0265       // Search for calchan, isha or vfs
0266       if ((*ime)->kind() == MonitorElement::Kind::INT) {
0267         std::string title = (*ime)->getName();
0268         std::string::size_type pos = title.find("calchan");
0269         if (pos == std::string::npos)
0270           pos = title.find("isha");
0271         if (pos == std::string::npos)
0272           pos = title.find("vfs");
0273         if (pos != std::string::npos) {
0274           int value = (*ime)->getIntValue();
0275           if (value >= 0) {
0276             edm::LogVerbatim(mlDqmClient_)
0277                 << "[CommissioningHistograms::" << __func__ << "]"
0278                 << " Found \"" << title.substr(pos, std::string::npos) << "\" with value \"" << value << "\"";
0279             if (!(bei->get(client_dir + "/" + title.substr(pos, std::string::npos)))) {
0280               bei->setCurrentFolder(client_dir);
0281               bei->bookInt(title.substr(pos, std::string::npos))->Fill(value);
0282               edm::LogVerbatim(mlDqmClient_)
0283                   << "[CommissioningHistograms::" << __func__ << "]"
0284                   << " Booked \"" << title.substr(pos, std::string::npos) << "\" in directory \"" << client_dir << "\"";
0285             }
0286           }
0287         }
0288       }
0289     }
0290     istr++;
0291   }
0292 }
0293 
0294 // -----------------------------------------------------------------------------
0295 
0296 /** */
0297 void CommissioningHistograms::extractHistograms(const std::vector<std::string>& contents) {
0298   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0299                          << " Extracting available histograms...";
0300 
0301   // Check pointer
0302   if (!bei_) {
0303     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0304                                 << " NULL pointer to DQMStore!";
0305     return;
0306   }
0307 
0308   // Check list of histograms
0309   if (contents.empty()) {
0310     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0311                                 << " Empty contents vector!";
0312     return;
0313   }
0314 
0315   // Iterate through list of histograms
0316   std::vector<std::string>::const_iterator idir;
0317   for (idir = contents.begin(); idir != contents.end(); idir++) {
0318     // Ignore "DQM source" directories if looking in client file
0319     if (idir->find(sistrip::collate_) == std::string::npos) {
0320       continue;
0321     }
0322 
0323     // Extract source directory path
0324     std::string source_dir = idir->substr(0, idir->find(":"));
0325 
0326     // Extract view and create key
0327     sistrip::View view = SiStripEnumsAndStrings::view(source_dir);
0328     SiStripKey path;
0329     if (view == sistrip::CONTROL_VIEW) {
0330       path = SiStripFecKey(source_dir);
0331     } else if (view == sistrip::READOUT_VIEW) {
0332       path = SiStripFedKey(source_dir);
0333     } else if (view == sistrip::DETECTOR_VIEW) {
0334       path = SiStripDetKey(source_dir);
0335     } else {
0336       path = SiStripKey();
0337     }
0338 
0339     // Check path is valid
0340     if (path.granularity() == sistrip::UNKNOWN_GRAN || path.granularity() == sistrip::UNDEFINED_GRAN) {
0341       continue;
0342     }
0343 
0344     // Generate corresponding client path (removing trailing "/")
0345     std::string client_dir(sistrip::undefinedView_);
0346     if (view == sistrip::CONTROL_VIEW) {
0347       client_dir = SiStripFecKey(path.key()).path();
0348     } else if (view == sistrip::READOUT_VIEW) {
0349       client_dir = SiStripFedKey(path.key()).path();
0350     } else if (view == sistrip::DETECTOR_VIEW) {
0351       client_dir = SiStripDetKey(path.key()).path();
0352     } else {
0353       client_dir = SiStripKey(path.key()).path();
0354     }
0355     std::string slash = client_dir.substr(client_dir.size() - 1, 1);
0356     if (slash == sistrip::dir_) {
0357       client_dir = client_dir.substr(0, client_dir.size() - 1);
0358     }
0359     client_dir = std::string(sistrip::collate_) + sistrip::dir_ + client_dir;
0360 
0361     // Retrieve MonitorElements from source directory
0362     std::vector<MonitorElement*> me_list = bei_->getContents(source_dir);
0363 
0364     // Iterate though MonitorElements and create CMEs
0365     std::vector<MonitorElement*>::iterator ime = me_list.begin();
0366     for (; ime != me_list.end(); ime++) {
0367       // Retrieve histogram title
0368       SiStripHistoTitle title((*ime)->getName());
0369 
0370       // Check histogram type
0371       //if ( title.histoType() != sistrip::EXPERT_HISTO ) { continue; }
0372 
0373       // Check granularity
0374       uint16_t channel = sistrip::invalid_;
0375       if (title.granularity() == sistrip::APV) {
0376         channel = SiStripFecKey::lldChan(title.channel());
0377       } else if (title.granularity() == sistrip::UNKNOWN_GRAN || title.granularity() == sistrip::UNDEFINED_GRAN) {
0378         std::stringstream ss;
0379         ss << "[CommissioningHistograms::" << __func__ << "]"
0380            << " Unexpected granularity for histogram title: " << std::endl
0381            << title << " found in path " << std::endl
0382            << path;
0383         edm::LogError(mlDqmClient_) << ss.str();
0384       } else {
0385         channel = title.channel();
0386       }
0387 
0388       // Build key
0389       uint32_t key = sistrip::invalid32_;
0390 
0391       if (view == sistrip::CONTROL_VIEW) {
0392         // for all runs except cabling
0393         SiStripFecKey temp(path.key());
0394         key = SiStripFecKey(temp.fecCrate(), temp.fecSlot(), temp.fecRing(), temp.ccuAddr(), temp.ccuChan(), channel)
0395                   .key();
0396         mapping_[title.keyValue()] = key;
0397 
0398       } else if (view == sistrip::READOUT_VIEW) {
0399         // for cabling run
0400         key = SiStripFedKey(path.key()).key();
0401         uint32_t temp = SiStripFecKey(sistrip::invalid_,
0402                                       sistrip::invalid_,
0403                                       sistrip::invalid_,
0404                                       sistrip::invalid_,
0405                                       sistrip::invalid_,
0406                                       channel)
0407                             .key();  // just record lld channel
0408         mapping_[title.keyValue()] = temp;
0409 
0410       } else if (view == sistrip::DETECTOR_VIEW) {
0411         SiStripDetKey temp(path.key());
0412         key = SiStripDetKey(temp.partition()).key();
0413         mapping_[title.keyValue()] = key;
0414 
0415       } else {
0416         key = SiStripKey(path.key()).key();
0417       }
0418 
0419       // Find CME in histos map
0420       Histo* histo = nullptr;
0421       HistosMap::iterator ihistos = histos_.find(key);
0422       if (ihistos != histos_.end()) {
0423         Histos::iterator ihis = ihistos->second.begin();
0424         while (!histo && ihis < ihistos->second.end()) {
0425           if ((*ime)->getName() == (*ihis)->title_) {
0426             histo = *ihis;
0427           }
0428           ihis++;
0429         }
0430       }
0431 
0432       // Create CollateME if it doesn't exist
0433       if (!histo) {
0434         histos_[key].push_back(new Histo());
0435         histo = histos_[key].back();
0436         histo->title_ = (*ime)->getName();
0437 
0438         // If histogram present in client directory, add to map
0439         if (source_dir.find(sistrip::collate_) != std::string::npos) {
0440           histo->me_ = bei_->get(client_dir + "/" + (*ime)->getName());
0441           if (!histo->me_) {
0442             edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0443                                         << " NULL pointer to MonitorElement!";
0444           }
0445         }
0446       }
0447     }
0448   }
0449 
0450   //printHistosMap();
0451 
0452   edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0453                                  << " Found histograms for " << histos_.size()
0454                                  << " structures in cached histogram map!";
0455 }
0456 
0457 // -----------------------------------------------------------------------------
0458 /** */
0459 void CommissioningHistograms::histoAnalysis(bool debug) {
0460   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0461                          << " (Derived) implementation to come...";
0462 }
0463 
0464 // -----------------------------------------------------------------------------
0465 /** */
0466 void CommissioningHistograms::printAnalyses() {
0467   Analyses::iterator ianal = data().begin();
0468   Analyses::iterator janal = data().end();
0469   for (; ianal != janal; ++ianal) {
0470     if (ianal->second) {
0471       std::stringstream ss;
0472       ianal->second->print(ss);
0473       if (ianal->second->isValid()) {
0474         LogTrace(mlDqmClient_) << ss.str();
0475       } else {
0476         edm::LogWarning(mlDqmClient_) << ss.str();
0477       }
0478     }
0479   }
0480 }
0481 
0482 // -----------------------------------------------------------------------------
0483 /** */
0484 void CommissioningHistograms::printSummary() {
0485   std::stringstream good;
0486   std::stringstream bad;
0487 
0488   Analyses::iterator ianal = data().begin();
0489   Analyses::iterator janal = data().end();
0490   for (; ianal != janal; ++ianal) {
0491     if (ianal->second) {
0492       if (ianal->second->isValid()) {
0493         ianal->second->summary(good);
0494       } else {
0495         ianal->second->summary(bad);
0496       }
0497     }
0498   }
0499 
0500   if (good.str().empty()) {
0501     good << "None found!";
0502   }
0503   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0504                          << " Printing summary of good analyses:"
0505                          << "\n"
0506                          << good.str();
0507 
0508   if (bad.str().empty()) {
0509     return;
0510   }  //@@ bad << "None found!"; }
0511   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0512                          << " Printing summary of bad analyses:"
0513                          << "\n"
0514                          << bad.str();
0515 }
0516 
0517 // -----------------------------------------------------------------------------
0518 /** */
0519 void CommissioningHistograms::printHistosMap() {
0520   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0521                          << " Printing histogram map, which has " << histos_.size() << " entries...";
0522   HistosMap::const_iterator ihistos = histos_.begin();
0523   for (; ihistos != histos_.end(); ihistos++) {
0524     std::stringstream ss;
0525     ss << " Found " << ihistos->second.size() << " histogram(s) for key: " << std::endl
0526        << SiStripFedKey(ihistos->first) << std::endl;
0527     Histos::const_iterator ihisto = ihistos->second.begin();
0528     for (; ihisto != ihistos->second.end(); ihisto++) {
0529       if (*ihisto) {
0530         (*ihisto)->print(ss);
0531       } else {
0532         ss << " NULL pointer to Histo object!";
0533       }
0534     }
0535     LogTrace(mlDqmClient_) << ss.str();
0536   }
0537 }
0538 
0539 // -----------------------------------------------------------------------------
0540 /** */
0541 void CommissioningHistograms::clearHistosMap() {
0542   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0543                          << " Clearing histogram map...";
0544   HistosMap::iterator ihistos = histos_.begin();
0545   for (; ihistos != histos_.end(); ihistos++) {
0546     Histos::iterator ihisto = ihistos->second.begin();
0547     for (; ihisto != ihistos->second.end(); ihisto++) {
0548       if (*ihisto) {
0549         delete *ihisto;
0550       }
0551     }
0552     ihistos->second.clear();
0553   }
0554   histos_.clear();
0555 }
0556 
0557 // -----------------------------------------------------------------------------
0558 /** */
0559 void CommissioningHistograms::createSummaryHisto(const sistrip::Monitorable& mon,
0560                                                  const sistrip::Presentation& pres,
0561                                                  const std::string& dir,
0562                                                  const sistrip::Granularity& gran) {
0563   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]";
0564 
0565   // Check view
0566   sistrip::View view = SiStripEnumsAndStrings::view(dir);
0567   if (view == sistrip::UNKNOWN_VIEW) {
0568     return;
0569   }
0570 
0571   // Analyze histograms
0572   if (data().empty()) {
0573     histoAnalysis(false);
0574   }
0575 
0576   // Check
0577   if (data().empty()) {
0578     edm::LogError(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0579                                 << " No analyses generated!";
0580     return;
0581   }
0582 
0583   // Extract data to be histogrammed
0584   uint32_t xbins = factory()->init(mon, pres, view, dir, gran, data());
0585 
0586   // Only create histograms if entries are found!
0587   if (!xbins) {
0588     return;
0589   }
0590 
0591   // Create summary histogram (if it doesn't already exist)
0592   TH1* summary = nullptr;
0593   if (pres != sistrip::HISTO_1D) {
0594     summary = histogram(mon, pres, view, dir, xbins);
0595   } else {
0596     summary = histogram(mon, pres, view, dir, sistrip::FED_ADC_RANGE, 0., sistrip::FED_ADC_RANGE * 1.);
0597   }
0598 
0599   // Fill histogram with data
0600   factory()->fill(*summary);
0601 }
0602 
0603 // -----------------------------------------------------------------------------
0604 /** */
0605 void CommissioningHistograms::remove(std::string pattern) {
0606   // TODO: remove no longer supported in DQMStore.
0607 }
0608 
0609 // -----------------------------------------------------------------------------
0610 /** */
0611 void CommissioningHistograms::save(std::string& path, uint32_t run_number, std::string partitionName) {
0612   // Construct path and filename
0613   std::stringstream ss;
0614 
0615   if (!path.empty()) {
0616     ss << path;
0617     if (ss.str().find(".root") == std::string::npos) {
0618       ss << ".root";
0619     }
0620 
0621   } else {
0622     // Retrieve SCRATCH directory
0623     std::string scratch = "SCRATCH";
0624     std::string dir = "";
0625     if (std::getenv(scratch.c_str()) != nullptr) {
0626       dir = std::getenv(scratch.c_str());
0627     }
0628 
0629     // Add directory path
0630     if (!dir.empty()) {
0631       ss << dir << "/";
0632     } else {
0633       ss << "/tmp/";
0634     }
0635 
0636     // Add filename with run number and ".root" extension
0637     if (partitionName.empty())
0638       ss << sistrip::dqmClientFileName_ << "_" << std::setfill('0') << std::setw(8) << run_number << ".root";
0639     else
0640       ss << sistrip::dqmClientFileName_ << "_" << partitionName << "_" << std::setfill('0') << std::setw(8)
0641          << run_number << ".root";
0642   }
0643 
0644   // Save file with appropriate filename
0645   LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0646                          << " Saving histograms to root file"
0647                          << " (This may take some time!)";
0648   path = ss.str();
0649   bei_->save(path, sistrip::collate_);
0650   edm::LogVerbatim(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0651                                  << " Saved histograms to root file \"" << ss.str() << "\"!";
0652 }
0653 
0654 // -----------------------------------------------------------------------------
0655 //
0656 TH1* CommissioningHistograms::histogram(const sistrip::Monitorable& mon,
0657                                         const sistrip::Presentation& pres,
0658                                         const sistrip::View& view,
0659                                         const std::string& directory,
0660                                         const uint32_t& xbins,
0661                                         const float& xlow,
0662                                         const float& xhigh) {
0663   // Remember pwd
0664   std::string pwd = bei_->pwd();
0665   bei_->setCurrentFolder(std::string(sistrip::collate_) + sistrip::dir_ + directory);
0666 
0667   // Construct histogram name
0668   std::string name = SummaryGenerator::name(task_, mon, pres, view, directory);
0669 
0670   MonitorElement* me = bei_->get(bei_->pwd() + "/" + name);
0671 
0672   // Create summary plot
0673   float high = static_cast<float>(xbins);
0674   if (pres == sistrip::HISTO_1D) {
0675     if (xlow < 1. * sistrip::valid_ && xhigh < 1. * sistrip::valid_) {
0676       me = bei_->book1D(name, name, xbins, xlow, xhigh);
0677     } else {
0678       me = bei_->book1D(name, name, xbins, 0., high);
0679     }
0680   } else if (pres == sistrip::HISTO_2D_SUM) {
0681     me = bei_->book1D(name, name, xbins, 0., high);
0682   } else if (pres == sistrip::HISTO_2D_SCATTER) {
0683     me = bei_->book2D(name, name, xbins, 0., high, sistrip::FED_ADC_RANGE + 1, 0., sistrip::FED_ADC_RANGE * 1.);
0684   } else if (pres == sistrip::PROFILE_1D) {
0685     me = bei_->bookProfile(name, name, xbins, 0., high, sistrip::FED_ADC_RANGE + 1, 0., sistrip::FED_ADC_RANGE * 1.);
0686   } else {
0687     me = nullptr;
0688     edm::LogWarning(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0689                                   << " Unexpected presentation \"" << SiStripEnumsAndStrings::presentation(pres)
0690                                   << "\" Unable to create summary plot!";
0691   }
0692 
0693   // Check pointer
0694   if (me) {
0695     LogTrace(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0696                            << " Created summary plot with name \"" << me->getName() << "\" in directory \""
0697                            << bei_->pwd() << "\"!";
0698   } else {
0699     edm::LogWarning(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0700                                   << " NULL pointer to MonitorElement!"
0701                                   << " Unable to create summary plot!";
0702   }
0703 
0704   // Extract root object
0705   TH1* summary = ExtractTObject<TH1>().extract(me);
0706   if (!summary) {
0707     edm::LogWarning(mlDqmClient_) << "[CommissioningHistograms::" << __func__ << "]"
0708                                   << " Unable to extract root object!"
0709                                   << " Returning NULL pointer!";
0710   }
0711 
0712   // Return to pwd
0713   bei_->setCurrentFolder(pwd);
0714 
0715   return summary;
0716 }
0717 
0718 CommissioningHistograms::Analyses& CommissioningHistograms::data(bool getMaskedData) {
0719   if (!getMaskedData)
0720     return data_;
0721   else {
0722     if (dataWithMaskCached_)
0723       return dataWithMask_;
0724     else {
0725       Analyses::iterator ianal = data_.begin();
0726       Analyses::iterator janal = data_.end();
0727       for (; ianal != janal; ++ianal) {
0728         CommissioningAnalysis* anal = ianal->second;
0729         SiStripFedKey fedkey = anal->fedKey();
0730         SiStripFecKey feckey = anal->fecKey();
0731         bool maskThisAnal_ = false;
0732         for (std::size_t i = 0; i < fedMaskVector_.size(); i++) {
0733           if (fedkey.fedId() == fedMaskVector_[i])
0734             maskThisAnal_ = true;
0735         }
0736         for (std::size_t i = 0; i < fecMaskVector_.size(); i++) {
0737           if (fecMaskVector_.size() != ringVector_.size() || fecMaskVector_.size() != ccuVector_.size() ||
0738               fecMaskVector_.size() != i2cChanVector_.size() || fecMaskVector_.size() != lldChanVector_.size()) {
0739             continue;
0740           }
0741           if (feckey.fecSlot() == fecMaskVector_[i] && feckey.fecRing() == ringVector_[i] &&
0742               feckey.ccuAddr() == ccuVector_[i] && feckey.ccuChan() == i2cChanVector_[i] &&
0743               feckey.lldChan() == lldChanVector_[i]) {
0744             maskThisAnal_ = true;
0745           }
0746         }
0747         if (mask_ && !maskThisAnal_)
0748           dataWithMask_[ianal->first] = ianal->second;
0749         else if (!mask_ && maskThisAnal_)
0750           dataWithMask_[ianal->first] = ianal->second;
0751       }
0752       dataWithMaskCached_ = true;
0753       return dataWithMask_;
0754     }
0755   }
0756 }