CMS 3D CMS Logo

SiStripApvSimulationParametersESProducer.cc
Go to the documentation of this file.
7 #include <fstream>
8 #include <boost/range/adaptor/indexed.hpp>
9 
11 public:
14 
16  const edm::IOVSyncValue& iov,
17  edm::ValidityInterval& iValidity) override;
18 
19  std::unique_ptr<SiStripApvSimulationParameters> produce(const SiStripApvSimulationParametersRcd& record);
20 
21 private:
22  std::vector<edm::FileInPath> baselineFiles_TOB_;
23  std::vector<edm::FileInPath> baselineFiles_TIB_;
24  std::vector<edm::FileInPath> baselineFiles_TID_;
25  std::vector<edm::FileInPath> baselineFiles_TEC_;
26  unsigned int baseline_nBins_;
29  std::vector<float> puBinEdges_;
30  std::vector<float> zBinEdges_;
31  std::vector<float> rBinEdgesTID_;
32  std::vector<float> rBinEdgesTEC_;
33 
35  const std::vector<float>& rzBinEdges) const;
36 };
37 
39  : baseline_nBins_(conf.getUntrackedParameter<unsigned int>("apvBaselines_nBinsPerBaseline")),
40  baseline_min_(conf.getUntrackedParameter<double>("apvBaselines_minBaseline")),
41  baseline_max_(conf.getUntrackedParameter<double>("apvBaselines_maxBaseline")) {
42  setWhatProduced(this);
43  findingRecord<SiStripApvSimulationParametersRcd>();
44  for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_puBinEdges")) {
45  puBinEdges_.push_back(x);
46  }
47  for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_zBinEdges")) {
48  zBinEdges_.push_back(x);
49  }
50  for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_rBinEdges_TID")) {
51  rBinEdgesTID_.push_back(x);
52  }
53  for (const auto x : conf.getUntrackedParameter<std::vector<double>>("apvBaselines_rBinEdges_TEC")) {
54  rBinEdgesTEC_.push_back(x);
55  }
56  baselineFiles_TIB_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib1"),
57  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib2"),
58  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib3"),
59  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tib4")};
60  baselineFiles_TOB_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob1"),
61  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob2"),
62  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob3"),
63  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob4"),
64  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob5"),
65  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tob6")};
66 
67  if (!rBinEdgesTID_.empty()) {
68  baselineFiles_TID_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid1"),
69  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid2"),
70  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tid3")};
71  }
72 
73  if (!rBinEdgesTEC_.empty()) {
74  baselineFiles_TEC_ = {conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec1"),
75  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec2"),
76  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec3"),
77  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec4"),
78  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec5"),
79  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec6"),
80  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec7"),
81  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec8"),
82  conf.getUntrackedParameter<edm::FileInPath>("apvBaselinesFile_tec9")};
83  }
84 }
85 
87  const edm::IOVSyncValue& iov,
88  edm::ValidityInterval& iValidity) {
89  iValidity = edm::ValidityInterval{iov.beginOfTime(), iov.endOfTime()};
90 }
91 
93  const std::string& apvBaselinesFileName, const std::vector<float>& rzBinEdges) const {
94  // Prepare histograms
95  unsigned int nZBins = rzBinEdges.size();
96  unsigned int nPUBins = puBinEdges_.size();
97 
98  if (nPUBins == 0 || nZBins == 0 || baseline_nBins_ == 0) {
99  throw cms::Exception("MissingInput") << "The parameters for the APV simulation are not correctly configured\n";
100  }
101  std::vector<float> baselineBinEdges{};
102  const auto baseline_binWidth = (baseline_max_ - baseline_min_) / baseline_nBins_;
103  for (unsigned i{0}; i != baseline_nBins_; ++i) {
104  baselineBinEdges.push_back(baseline_min_ + i * baseline_binWidth);
105  }
106  baselineBinEdges.push_back(baseline_max_);
107 
108  SiStripApvSimulationParameters::LayerParameters layerParams{baselineBinEdges, puBinEdges_, rzBinEdges};
109 
110  // Read apv baselines from text files
111  std::vector<double> theAPVBaselines;
112  std::ifstream apvBaselineFile(apvBaselinesFileName.c_str());
113  if (!apvBaselineFile.good()) {
114  throw cms::Exception("FileError") << "Problem opening APV baselines file: " << apvBaselinesFileName;
115  }
117  while (std::getline(apvBaselineFile, line)) {
118  if (!line.empty()) {
119  std::istringstream lStr{line};
120  double value;
121  while (lStr >> value) {
122  theAPVBaselines.push_back(value);
123  }
124  }
125  }
126  if (theAPVBaselines.empty()) {
127  throw cms::Exception("WrongAPVBaselines")
128  << "Problem reading from APV baselines file " << apvBaselinesFileName << ": no values read in";
129  }
130 
131  if (theAPVBaselines.size() != nZBins * nPUBins * baseline_nBins_) {
132  throw cms::Exception("WrongAPVBaselines") << "Problem reading from APV baselines file " << apvBaselinesFileName
133  << ": number of baselines read different to that expected i.e. nZBins * "
134  "nPUBins * apvBaselines_nBinsPerBaseline_";
135  }
136 
137  // Put baselines into histograms
138  for (auto const& apvBaseline : theAPVBaselines | boost::adaptors::indexed(0)) {
139  unsigned int binInCurrentHistogram = apvBaseline.index() % baseline_nBins_ + 1;
140  unsigned int binInZ = int(apvBaseline.index()) / (nPUBins * baseline_nBins_);
141  unsigned int binInPU = int(apvBaseline.index() - binInZ * (nPUBins)*baseline_nBins_) / baseline_nBins_;
142 
143  layerParams.setBinContent(binInCurrentHistogram, binInPU + 1, binInZ + 1, apvBaseline.value());
144  }
145 
146  return layerParams;
147 }
148 
149 std::unique_ptr<SiStripApvSimulationParameters> SiStripApvSimulationParametersESSource::produce(
151  auto apvSimParams = std::make_unique<SiStripApvSimulationParameters>(
153  for (unsigned int i{0}; i != baselineFiles_TIB_.size(); ++i) {
154  if (!apvSimParams->putTIB(i + 1, makeLayerParameters(baselineFiles_TIB_[i].fullPath(), zBinEdges_))) {
155  throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TIB layer " << (i + 1);
156  } else {
157  LogDebug("SiStripApvSimulationParameters") << "Added parameters for TIB layer " << (i + 1);
158  }
159  }
160  for (unsigned int i{0}; i != baselineFiles_TOB_.size(); ++i) {
161  if (!apvSimParams->putTOB(i + 1, makeLayerParameters(baselineFiles_TOB_[i].fullPath(), zBinEdges_))) {
162  throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TOB layer " << (i + 1);
163  } else {
164  LogDebug("SiStripApvSimulationParameters") << "Added parameters for TOB layer " << (i + 1);
165  }
166  }
167  for (unsigned int i{0}; i != baselineFiles_TID_.size(); ++i) {
168  if (!apvSimParams->putTID(i + 1, makeLayerParameters(baselineFiles_TID_[i].fullPath(), rBinEdgesTID_))) {
169  throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TID wheel " << (i + 1);
170  } else {
171  LogDebug("SiStripApvSimulationParameters") << "Added parameters for TID wheel " << (i + 1);
172  }
173  }
174  for (unsigned int i{0}; i != baselineFiles_TEC_.size(); ++i) {
175  if (!apvSimParams->putTEC(i + 1, makeLayerParameters(baselineFiles_TEC_[i].fullPath(), rBinEdgesTEC_))) {
176  throw cms::Exception("SiStripApvSimulationParameters") << "Could not add parameters for TEC wheel " << (i + 1);
177  } else {
178  LogDebug("SiStripApvSimulationParameters") << "Added parameters for TEC wheel " << (i + 1);
179  }
180  }
181  return apvSimParams;
182 }
183 
auto setWhatProduced(T *iThis, const es::Label &iLabel={})
Definition: ESProducer.h:163
static const IOVSyncValue & endOfTime()
Definition: IOVSyncValue.cc:82
std::unique_ptr< SiStripApvSimulationParameters > produce(const SiStripApvSimulationParametersRcd &record)
T getUntrackedParameter(std::string const &, T const &) const
static const IOVSyncValue & beginOfTime()
Definition: IOVSyncValue.cc:88
Definition: value.py:1
#define DEFINE_FWK_EVENTSETUP_SOURCE(type)
Definition: SourceFactory.h:92
void setIntervalFor(const edm::eventsetup::EventSetupRecordKey &, const edm::IOVSyncValue &iov, edm::ValidityInterval &iValidity) override
SiStripApvSimulationParameters::LayerParameters makeLayerParameters(const std::string &apvBaselinesFileName, const std::vector< float > &rzBinEdges) const
#define LogDebug(id)