Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 14:14:52

0001 #include "FWCore/Framework/interface/ESProducer.h"
0002 #include "FWCore/Framework/interface/EventSetupRecordIntervalFinder.h"
0003 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0004 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0005 #include "CondFormats/DataRecord/interface/SiStripCondDataRecords.h"
0006 #include "CondFormats/SiStripObjects/interface/SiStripApvSimulationParameters.h"
0007 #include <fstream>
0008 #include <boost/range/adaptor/indexed.hpp>
0009 
0010 class SiStripApvSimulationParametersESSource : public edm::ESProducer, public edm::EventSetupRecordIntervalFinder {
0011 public:
0012   explicit SiStripApvSimulationParametersESSource(const edm::ParameterSet& conf);
0013   ~SiStripApvSimulationParametersESSource() override {}
0014 
0015   void setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0016                       const edm::IOVSyncValue& iov,
0017                       edm::ValidityInterval& iValidity) override;
0018 
0019   std::unique_ptr<SiStripApvSimulationParameters> produce(const SiStripApvSimulationParametersRcd& record);
0020 
0021 private:
0022   std::vector<edm::FileInPath> baselineFiles_TOB_;
0023   std::vector<edm::FileInPath> baselineFiles_TIB_;
0024   std::vector<edm::FileInPath> baselineFiles_TID_;
0025   std::vector<edm::FileInPath> baselineFiles_TEC_;
0026   unsigned int baseline_nBins_;
0027   float baseline_min_;
0028   float baseline_max_;
0029   std::vector<float> puBinEdges_;
0030   std::vector<float> zBinEdges_;
0031   std::vector<float> rBinEdgesTID_;
0032   std::vector<float> rBinEdgesTEC_;
0033 
0034   SiStripApvSimulationParameters::LayerParameters makeLayerParameters(const std::string& apvBaselinesFileName,
0035                                                                       const std::vector<float>& rzBinEdges) const;
0036 };
0037 
0038 SiStripApvSimulationParametersESSource::SiStripApvSimulationParametersESSource(const edm::ParameterSet& conf)
0039     : baseline_nBins_(conf.getUntrackedParameter<unsigned int>("apvBaselines_nBinsPerBaseline")),
0040       baseline_min_(conf.getUntrackedParameter<double>("apvBaselines_minBaseline")),
0041       baseline_max_(conf.getUntrackedParameter<double>("apvBaselines_maxBaseline")) {
0042   setWhatProduced(this);
0043   findingRecord<SiStripApvSimulationParametersRcd>();
0044   for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_puBinEdges")) {
0045     puBinEdges_.push_back(x);
0046   }
0047   for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_zBinEdges")) {
0048     zBinEdges_.push_back(x);
0049   }
0050   for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_rBinEdges_TID")) {
0051     rBinEdgesTID_.push_back(x);
0052   }
0053   for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_rBinEdges_TEC")) {
0054     rBinEdgesTEC_.push_back(x);
0055   }
0056   baselineFiles_TIB_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib1"),
0057                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib2"),
0058                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib3"),
0059                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib4")};
0060   baselineFiles_TOB_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob1"),
0061                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob2"),
0062                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob3"),
0063                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob4"),
0064                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob5"),
0065                         conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob6")};
0066 
0067   if (!rBinEdgesTID_.empty()) {
0068     baselineFiles_TID_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid1"),
0069                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid2"),
0070                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid3")};
0071   }
0072 
0073   if (!rBinEdgesTEC_.empty()) {
0074     baselineFiles_TEC_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec1"),
0075                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec2"),
0076                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec3"),
0077                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec4"),
0078                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec5"),
0079                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec6"),
0080                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec7"),
0081                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec8"),
0082                           conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec9")};
0083   }
0084 }
0085 
0086 void SiStripApvSimulationParametersESSource::setIntervalFor(const edm::eventsetup::EventSetupRecordKey&,
0087                                                             const edm::IOVSyncValue& iov,
0088                                                             edm::ValidityInterval& iValidity) {
0089   iValidity = edm::ValidityInterval{iov.beginOfTime(), iov.endOfTime()};
0090 }
0091 
0092 SiStripApvSimulationParameters::LayerParameters SiStripApvSimulationParametersESSource::makeLayerParameters(
0093     const std::string& apvBaselinesFileName, const std::vector<float>& rzBinEdges) const {
0094   // Prepare histograms
0095   unsigned int nZBins = rzBinEdges.size();
0096   unsigned int nPUBins = puBinEdges_.size();
0097 
0098   if (nPUBins == 0 || nZBins == 0 || baseline_nBins_ == 0) {
0099     throw cms::Exception("MissingInput") << "The parameters for the APV simulation are not correctly configured\n";
0100   }
0101   std::vector<float> baselineBinEdges{};
0102   const auto baseline_binWidth = (baseline_max_ - baseline_min_) / baseline_nBins_;
0103   for (unsigned i{0}; i != baseline_nBins_; ++i) {
0104     baselineBinEdges.push_back(baseline_min_ + i * baseline_binWidth);
0105   }
0106   baselineBinEdges.push_back(baseline_max_);
0107 
0108   SiStripApvSimulationParameters::LayerParameters layerParams{baselineBinEdges, puBinEdges_, rzBinEdges};
0109 
0110   // Read apv baselines from text files
0111   std::vector<double> theAPVBaselines;
0112   std::ifstream apvBaselineFile(apvBaselinesFileName.c_str());
0113   if (!apvBaselineFile.good()) {
0114     throw cms::Exception("FileError") << "Problem opening APV baselines file: " << apvBaselinesFileName;
0115   }
0116   std::string line;
0117   while (std::getline(apvBaselineFile, line)) {
0118     if (!line.empty()) {
0119       std::istringstream lStr{line};
0120       double value;
0121       while (lStr >> value) {
0122         theAPVBaselines.push_back(value);
0123       }
0124     }
0125   }
0126   if (theAPVBaselines.empty()) {
0127     throw cms::Exception("WrongAPVBaselines")
0128         << "Problem reading from APV baselines file " << apvBaselinesFileName << ": no values read in";
0129   }
0130 
0131   if (theAPVBaselines.size() != nZBins * nPUBins * baseline_nBins_) {
0132     throw cms::Exception("WrongAPVBaselines") << "Problem reading from APV baselines file " << apvBaselinesFileName
0133                                               << ": number of baselines read different to that expected i.e. nZBins * "
0134                                                  "nPUBins * apvBaselines_nBinsPerBaseline_";
0135   }
0136 
0137   // Put baselines into histograms
0138   for (auto const& apvBaseline : theAPVBaselines | boost::adaptors::indexed(0)) {
0139     unsigned int binInCurrentHistogram = apvBaseline.index() % baseline_nBins_ + 1;
0140     unsigned int binInZ = int(apvBaseline.index()) / (nPUBins * baseline_nBins_);
0141     unsigned int binInPU = int(apvBaseline.index() - binInZ * (nPUBins)*baseline_nBins_) / baseline_nBins_;
0142 
0143     layerParams.setBinContent(binInCurrentHistogram, binInPU + 1, binInZ + 1, apvBaseline.value());
0144   }
0145 
0146   return layerParams;
0147 }
0148 
0149 std::unique_ptr<SiStripApvSimulationParameters> SiStripApvSimulationParametersESSource::produce(
0150     const SiStripApvSimulationParametersRcd& record) {
0151   auto apvSimParams = std::make_unique<SiStripApvSimulationParameters>(
0152       baselineFiles_TIB_.size(), baselineFiles_TOB_.size(), baselineFiles_TID_.size(), baselineFiles_TEC_.size());
0153   for (unsigned int i{0}; i != baselineFiles_TIB_.size(); ++i) {
0154     if (!apvSimParams->putTIB(i + 1, makeLayerParameters(baselineFiles_TIB_[i].fullPath(), zBinEdges_))) {
0155       throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TIB layer " << (i + 1);
0156     } else {
0157       LogDebug("SiStripApvSimulationParameters") << "Added parameters for TIB layer " << (i + 1);
0158     }
0159   }
0160   for (unsigned int i{0}; i != baselineFiles_TOB_.size(); ++i) {
0161     if (!apvSimParams->putTOB(i + 1, makeLayerParameters(baselineFiles_TOB_[i].fullPath(), zBinEdges_))) {
0162       throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TOB layer " << (i + 1);
0163     } else {
0164       LogDebug("SiStripApvSimulationParameters") << "Added parameters for TOB layer " << (i + 1);
0165     }
0166   }
0167   for (unsigned int i{0}; i != baselineFiles_TID_.size(); ++i) {
0168     if (!apvSimParams->putTID(i + 1, makeLayerParameters(baselineFiles_TID_[i].fullPath(), rBinEdgesTID_))) {
0169       throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TID wheel " << (i + 1);
0170     } else {
0171       LogDebug("SiStripApvSimulationParameters") << "Added parameters for TID wheel " << (i + 1);
0172     }
0173   }
0174   for (unsigned int i{0}; i != baselineFiles_TEC_.size(); ++i) {
0175     if (!apvSimParams->putTEC(i + 1, makeLayerParameters(baselineFiles_TEC_[i].fullPath(), rBinEdgesTEC_))) {
0176       throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TEC wheel " << (i + 1);
0177     } else {
0178       LogDebug("SiStripApvSimulationParameters") << "Added parameters for TEC wheel " << (i + 1);
0179     }
0180   }
0181   return apvSimParams;
0182 }
0183 
0184 #include "FWCore/Framework/interface/SourceFactory.h"
0185 DEFINE_FWK_EVENTSETUP_SOURCE(SiStripApvSimulationParametersESSource);