Back to home page

Project CMSSW displayed by LXR

 
 

    


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

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