Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-11-02 23:48:46

0001 
0002 #include "DQM/SiStripCommissioningClients/interface/SiStripCommissioningOfflineClient.h"
0003 #include "DataFormats/SiStripCommon/interface/SiStripEnumsAndStrings.h"
0004 #include "DataFormats/SiStripCommon/interface/SiStripHistoTitle.h"
0005 #include "DataFormats/SiStripCommon/interface/SiStripFecKey.h"
0006 #include "DQM/SiStripCommissioningClients/interface/FastFedCablingHistograms.h"
0007 #include "DQM/SiStripCommissioningClients/interface/FedCablingHistograms.h"
0008 #include "DQM/SiStripCommissioningClients/interface/ApvTimingHistograms.h"
0009 #include "DQM/SiStripCommissioningClients/interface/OptoScanHistograms.h"
0010 #include "DQM/SiStripCommissioningClients/interface/VpspScanHistograms.h"
0011 #include "DQM/SiStripCommissioningClients/interface/PedestalsHistograms.h"
0012 #include "DQM/SiStripCommissioningClients/interface/PedsOnlyHistograms.h"
0013 #include "DQM/SiStripCommissioningClients/interface/PedsFullNoiseHistograms.h"
0014 #include "DQM/SiStripCommissioningClients/interface/NoiseHistograms.h"
0015 #include "DQM/SiStripCommissioningClients/interface/SamplingHistograms.h"
0016 #include "DQM/SiStripCommissioningClients/interface/CalibrationHistograms.h"
0017 #include "DQM/SiStripCommissioningClients/interface/DaqScopeModeHistograms.h"
0018 #include "FWCore/ServiceRegistry/interface/Service.h"
0019 #include "DQMServices/Core/interface/DQMStore.h"
0020 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0021 #include "FWCore/ParameterSet/interface/FileInPath.h"
0022 #include <iostream>
0023 #include <iomanip>
0024 #include <fstream>
0025 #include <sstream>
0026 #include <sys/types.h>
0027 #include <dirent.h>
0028 #include <cerrno>
0029 #include "TProfile.h"
0030 #include <cstdint>
0031 
0032 using namespace sistrip;
0033 
0034 // -----------------------------------------------------------------------------
0035 //
0036 SiStripCommissioningOfflineClient::SiStripCommissioningOfflineClient(const edm::ParameterSet& pset)
0037     : bei_(edm::Service<DQMStore>().operator->()),
0038       histos_(nullptr),
0039       //inputFiles_( pset.getUntrackedParameter< std::vector<std::string> >( "InputRootFiles", std::vector<std::string>() ) ),
0040       outputFileName_(pset.getUntrackedParameter<std::string>("OutputRootFile", "")),
0041       collateHistos_(!pset.getUntrackedParameter<bool>("UseClientFile", false)),
0042       analyzeHistos_(pset.getUntrackedParameter<bool>("AnalyzeHistos", true)),
0043       xmlFile_((pset.getUntrackedParameter<edm::FileInPath>("SummaryXmlFile", edm::FileInPath())).fullPath()),
0044       createSummaryPlots_(false),
0045       clientHistos_(false),
0046       uploadToDb_(false),
0047       runType_(sistrip::UNKNOWN_RUN_TYPE),
0048       runNumber_(0),
0049       partitionName_(pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName")
0050                                                                  : ""),
0051       map_(),
0052       plots_(),
0053       parameters_(pset) {
0054   LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0055                          << " Constructing object...";
0056   usesResource("DQMStore");
0057   setInputFiles(inputFiles_,
0058                 pset.getUntrackedParameter<std::string>("FilePath"),
0059                 pset.existsAs<std::string>("PartitionName") ? pset.getParameter<std::string>("PartitionName") : "",
0060                 pset.getUntrackedParameter<uint32_t>("RunNumber"),
0061                 collateHistos_);
0062 }
0063 
0064 // -----------------------------------------------------------------------------
0065 //
0066 SiStripCommissioningOfflineClient::~SiStripCommissioningOfflineClient() {
0067   LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0068                          << " Destructing object...";
0069 }
0070 
0071 // -----------------------------------------------------------------------------
0072 //
0073 void SiStripCommissioningOfflineClient::beginRun(const edm::Run& run, const edm::EventSetup& setup) {
0074   LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0075                          << " Analyzing root file(s)...";
0076 
0077   // Check for null pointer
0078   if (!bei_) {
0079     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0080                                 << " NULL pointer to DQMStore!"
0081                                 << " Aborting...";
0082     return;
0083   }
0084 
0085   // Check if .root file can be opened
0086   std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
0087   for (; ifile != inputFiles_.end(); ifile++) {
0088     std::ifstream root_file;
0089     root_file.open(ifile->c_str());
0090     if (!root_file.is_open()) {
0091       edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0092                                   << " The input root file \"" << *ifile << "\" could not be opened!"
0093                                   << " Please check the path and filename!";
0094     } else {
0095       root_file.close();
0096       std::string::size_type found = ifile->find(sistrip::dqmClientFileName_);
0097       if (found != std::string::npos && clientHistos_) {
0098         edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0099                                     << " The input root files appear to be a mixture"
0100                                     << " of \"Source\" and \"Client\" files!"
0101                                     << " Aborting...";
0102         return;
0103       }
0104       if (found != std::string::npos && inputFiles_.size() != 1) {
0105         edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0106                                     << " There appear to be multiple input \"Client\" root files!"
0107                                     << " Aborting...";
0108         return;
0109       }
0110       if (found != std::string::npos) {
0111         clientHistos_ = true;
0112       }
0113     }
0114   }
0115   if (clientHistos_ && inputFiles_.size() == 1) {
0116     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0117                                    << " Collated histograms found in input root file \"" << inputFiles_[0] << "\"";
0118   }
0119 
0120   // Check if .xml file can be opened
0121   if (!xmlFile_.empty()) {
0122     std::ifstream xml_file;
0123     xml_file.open(xmlFile_.c_str());
0124     if (!xml_file.is_open()) {
0125       edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0126                                   << " The SummaryPlot XML file \"" << xmlFile_ << "\" could not be opened!"
0127                                   << " Please check the path and filename!"
0128                                   << " Aborting...";
0129       return;
0130     } else {
0131       createSummaryPlots_ = true;
0132       xml_file.close();
0133     }
0134   }
0135 
0136   // Open root file(s) and create ME's
0137   if (inputFiles_.empty()) {
0138     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0139                                 << " No input root files specified!";
0140     return;
0141   }
0142 
0143   edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0144                                  << " Opening root files. This may take some time!...";
0145   std::vector<std::string>::const_iterator jfile = inputFiles_.begin();
0146   for (; jfile != inputFiles_.end(); jfile++) {
0147     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0148                            << " Opening root file \"" << *jfile << "\"... (This may take some time.)";
0149     if (clientHistos_) {
0150       bei_->open(*jfile, false, sistrip::collate_, "");
0151     } else {
0152       bei_->open(*jfile, false, "SiStrip", sistrip::collate_);
0153     }
0154     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0155                            << " Opened root file \"" << *jfile << "\"!";
0156   }
0157   edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0158                                  << " Opened " << inputFiles_.size() << " root files!";
0159 
0160   // Retrieve list of histograms
0161   auto allmes = bei_->getAllContents("");
0162   std::vector<std::string> contents;
0163 
0164   // If using client file, remove "source" histograms from list
0165   if (clientHistos_) {
0166     std::set<std::string> temp;
0167     for (auto me : allmes) {
0168       const auto& name = me->getPathname();
0169       if (name.find(sistrip::collate_) != std::string::npos) {
0170         temp.insert(name);
0171       }
0172     }
0173     contents.clear();
0174     for (const auto& s : temp) {
0175       // the old code expects a ":", but does not really need the ME names
0176       contents.push_back(s + ":");
0177     }
0178   }
0179 
0180   // Some debug
0181   LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0182                          << " Found " << contents.size() << " directories containing MonitorElements";
0183 
0184   // Some more debug
0185   if (false) {
0186     std::stringstream ss;
0187     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0188        << " Directories found: " << std::endl;
0189     std::vector<std::string>::iterator istr = contents.begin();
0190     for (; istr != contents.end(); istr++) {
0191       ss << " " << *istr << std::endl;
0192     }
0193     LogTrace(mlDqmClient_) << ss.str();
0194   }
0195 
0196   // Extract run type from contents
0197   runType_ = CommissioningHistograms::runType(bei_, contents);
0198 
0199   // Extract run number from contents
0200   runNumber_ = CommissioningHistograms::runNumber(bei_, contents);
0201 
0202   // Copy custom information to the collated structure
0203   CommissioningHistograms::copyCustomInformation(bei_, contents);
0204 
0205   // Check runType
0206   if (runType_ == sistrip::UNKNOWN_RUN_TYPE) {
0207     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0208                                 << " Unknown commissioning runType: " << SiStripEnumsAndStrings::runType(runType_)
0209                                 << " and run number is " << runNumber_;
0210     return;
0211   } else {
0212     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0213                                    << " Run type is " << SiStripEnumsAndStrings::runType(runType_)
0214                                    << " and run number is " << runNumber_;
0215   }
0216 
0217   // Open and parse "summary plot" xml file
0218   if (createSummaryPlots_) {
0219     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0220                                    << " Parsing summary plot XML file...";
0221     SummaryPlotXmlParser xml_file;
0222     xml_file.parseXML(xmlFile_);
0223     plots_ = xml_file.summaryPlots(runType_);
0224     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0225                                    << " Parsed summary plot XML file and found " << plots_.size() << " plots defined!";
0226   } else {
0227     edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0228                                   << " Null string for SummaryPlotXmlFile!"
0229                                   << " No summary plots will be created!";
0230   }
0231 
0232   // Some debug
0233   std::stringstream ss;
0234   ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]" << std::endl << " Input root files      : ";
0235   if (inputFiles_.empty()) {
0236     ss << "(none)";
0237   } else {
0238     std::vector<std::string>::const_iterator ifile = inputFiles_.begin();
0239     for (; ifile != inputFiles_.end(); ifile++) {
0240       if (ifile != inputFiles_.begin()) {
0241         ss << std::setw(25) << std::setfill(' ') << ": ";
0242       }
0243       ss << "\"" << *ifile << "\"" << std::endl;
0244     }
0245   }
0246   ss << " Run type              : \"" << SiStripEnumsAndStrings::runType(runType_) << "\"" << std::endl
0247      << " Run number            :  " << runNumber_ << std::endl
0248      << " Summary plot XML file : ";
0249   if (xmlFile_.empty()) {
0250     ss << "(none)";
0251   } else {
0252     ss << "\"" << xmlFile_ << "\"";
0253   }
0254   edm::LogVerbatim(mlDqmClient_) << ss.str();
0255 
0256   // Virtual method that creates CommissioningHistogram object
0257   LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0258                          << " Creating CommissioningHistogram object...";
0259   createHistos(parameters_, setup);
0260   if (histos_) {
0261     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0262                            << " Created CommissioningHistogram object!";
0263   } else {
0264     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0265                                 << " NULL pointer to CommissioningHistogram object!"
0266                                 << " Aborting...";
0267     return;
0268   }
0269 
0270   // Perform collation
0271   if (histos_) {
0272     histos_->extractHistograms(contents);
0273   }
0274 
0275   // Perform analysis
0276   if (analyzeHistos_) {
0277     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0278                            << " Analyzing histograms...";
0279     if (histos_) {
0280       histos_->histoAnalysis(true);
0281     }
0282     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0283                            << " Analyzed histograms!";
0284   } else {
0285     edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0286                                   << " No histogram analysis performed!";
0287   }
0288 
0289   // Create summary plots
0290   if (createSummaryPlots_) {
0291     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0292                                    << " Generating summary plots...";
0293     std::vector<SummaryPlot>::const_iterator iplot = plots_.begin();
0294     for (; iplot != plots_.end(); iplot++) {
0295       if (histos_) {
0296         histos_->createSummaryHisto(iplot->monitorable(), iplot->presentation(), iplot->level(), iplot->granularity());
0297       }
0298     }
0299     edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0300                                    << " Generated summary plots!";
0301   } else {
0302     edm::LogWarning(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0303                                   << " No summary plots generated!";
0304   }
0305 
0306   // Save client root file
0307   if (histos_) {
0308     bool save = parameters_.getUntrackedParameter<bool>("SaveClientFile", true);
0309     if (save) {
0310       if (runType_ != sistrip::CALIBRATION_SCAN and runType_ != sistrip::CALIBRATION_SCAN_DECO) {
0311         if (runType_ != sistrip::DAQ_SCOPE_MODE)
0312           histos_->save(outputFileName_, runNumber_);
0313         else
0314           histos_->save(outputFileName_, runNumber_, partitionName_);
0315       } else {
0316         CalibrationHistograms* histo = dynamic_cast<CalibrationHistograms*>(histos_);
0317         histo->save(outputFileName_, runNumber_);
0318       }
0319     } else {
0320       edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0321                                      << " Client file not saved!";
0322     }
0323   }
0324 
0325   // Virtual method to trigger the database upload
0326   uploadToConfigDb();
0327 
0328   // Print analyses
0329   if (histos_) {
0330     histos_->printAnalyses();
0331     histos_->printSummary();
0332   }
0333 
0334   // Remove all ME/CME objects
0335   if (histos_) {
0336     histos_->remove();
0337   }
0338 
0339   edm::LogVerbatim(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0340                                  << " Finished analyzing root file(s)...";
0341 }
0342 
0343 // -----------------------------------------------------------------------------
0344 //
0345 void SiStripCommissioningOfflineClient::analyze(const edm::Event& event, const edm::EventSetup& setup) {
0346   if (!(event.id().event() % 10)) {
0347     LogTrace(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0348                            << " Empty event loop! User can kill job...";
0349   }
0350 }
0351 
0352 // -----------------------------------------------------------------------------
0353 //
0354 void SiStripCommissioningOfflineClient::endJob() {}
0355 
0356 // -----------------------------------------------------------------------------
0357 //
0358 void SiStripCommissioningOfflineClient::createHistos(const edm::ParameterSet& pset, const edm::EventSetup& setup) {
0359   // Check pointer
0360   if (histos_) {
0361     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0362                                 << " CommissioningHistogram object already exists!"
0363                                 << " Aborting...";
0364     return;
0365   }
0366 
0367   // Check pointer to BEI
0368   if (!bei_) {
0369     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0370                                 << " NULL pointer to DQMStore!";
0371     return;
0372   }
0373 
0374   // Create "commissioning histograms" object
0375   if (runType_ == sistrip::FAST_CABLING) {
0376     histos_ = new FastFedCablingHistograms(pset, bei_);
0377   } else if (runType_ == sistrip::FED_CABLING) {
0378     histos_ = new FedCablingHistograms(pset, bei_);
0379   } else if (runType_ == sistrip::APV_TIMING) {
0380     histos_ = new ApvTimingHistograms(pset, bei_);
0381   } else if (runType_ == sistrip::OPTO_SCAN) {
0382     histos_ = new OptoScanHistograms(pset, bei_);
0383   } else if (runType_ == sistrip::VPSP_SCAN) {
0384     histos_ = new VpspScanHistograms(pset, bei_);
0385   } else if (runType_ == sistrip::PEDESTALS) {
0386     histos_ = new PedestalsHistograms(pset, bei_);
0387   } else if (runType_ == sistrip::PEDS_FULL_NOISE) {
0388     histos_ = new PedsFullNoiseHistograms(pset, bei_);
0389   } else if (runType_ == sistrip::PEDS_ONLY) {
0390     histos_ = new PedsOnlyHistograms(pset, bei_);
0391   } else if (runType_ == sistrip::NOISE) {
0392     histos_ = new NoiseHistograms(pset, bei_);
0393   } else if (runType_ == sistrip::APV_LATENCY || runType_ == sistrip::FINE_DELAY) {
0394     histos_ = new SamplingHistograms(pset, bei_, runType_);
0395   } else if (runType_ == sistrip::CALIBRATION || runType_ == sistrip::CALIBRATION_DECO ||
0396              runType_ == sistrip::CALIBRATION_SCAN || runType_ == sistrip::CALIBRATION_SCAN_DECO) {
0397     histos_ = new CalibrationHistograms(pset, bei_, runType_);
0398   } else if (runType_ == sistrip::DAQ_SCOPE_MODE) {
0399     histos_ = new DaqScopeModeHistograms(pset, bei_);
0400   } else if (runType_ == sistrip::UNDEFINED_RUN_TYPE) {
0401     histos_ = nullptr;
0402     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0403                                 << " Undefined run type!";
0404     return;
0405   } else if (runType_ == sistrip::UNKNOWN_RUN_TYPE) {
0406     histos_ = nullptr;
0407     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0408                                 << " Unknown run type!";
0409     return;
0410   }
0411   histos_->configure(pset, setup);
0412 }
0413 
0414 // -----------------------------------------------------------------------------
0415 //
0416 void SiStripCommissioningOfflineClient::setInputFiles(std::vector<std::string>& files,
0417                                                       const std::string path,
0418                                                       const std::string partitionName,
0419                                                       uint32_t run_number,
0420                                                       bool collate_histos) {
0421   std::string runStr;
0422   std::stringstream ss;
0423   ss << std::setfill('0') << std::setw(8) << run_number;
0424   runStr = ss.str();
0425 
0426   std::string nameStr = "";
0427   if (!collate_histos) {
0428     nameStr = "SiStripCommissioningClient_";
0429   } else {
0430     nameStr = "SiStripCommissioningSource_";
0431   }
0432 
0433   LogTrace("TEST") << " runStr " << runStr;
0434 
0435   // Open directory
0436   DIR* dp;
0437   struct dirent* dirp;
0438   if ((dp = opendir(path.c_str())) == nullptr) {
0439     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0440                                 << " Error locating directory \"" << path << "\". No such directory!";
0441     return;
0442   }
0443 
0444   // Find compatible files
0445   while ((dirp = readdir(dp)) != nullptr) {
0446     std::string fileName(dirp->d_name);
0447     bool goodName = (fileName.find(nameStr) != std::string::npos);
0448     bool goodRun = (fileName.find(runStr) != std::string::npos);
0449     bool rootFile = (fileName.find(".root") != std::string::npos);
0450     bool goodPartition = true;
0451     if (not partitionName.empty()) {
0452       goodPartition = (fileName.find(partitionName) != std::string::npos);
0453     }
0454 
0455     //bool rootFile = ( fileName.rfind(".root",5) == fileName.size()-5 );
0456     if (goodName && goodRun && rootFile && goodPartition) {
0457       std::string entry = path;
0458       entry += "/";
0459       entry += fileName;
0460       files.push_back(entry);
0461     }
0462   }
0463   closedir(dp);
0464 
0465   // Some debug
0466   if (!collate_histos && files.size() > 1) {
0467     std::stringstream ss;
0468     ss << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0469        << " Found more than one client file!";
0470     std::vector<std::string>::const_iterator ifile = files.begin();
0471     std::vector<std::string>::const_iterator jfile = files.end();
0472     for (; ifile != jfile; ++ifile) {
0473       ss << std::endl << *ifile;
0474     }
0475     edm::LogError(mlDqmClient_) << ss.str();
0476   } else if (files.empty()) {
0477     edm::LogError(mlDqmClient_) << "[SiStripCommissioningOfflineClient::" << __func__ << "]"
0478                                 << " No input files found!";
0479   }
0480 }