CMS 3D CMS Logo

SiStripNoisesFromDBMiscalibrator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiStrip
4 // Class: SiStripNoisesFromDBMiscalibrator
5 //
14 //
15 // Original Author: Marco Musich
16 // Created: Tue, 03 Oct 2017 12:57:34 GMT
17 //
18 //
19 
20 // system include files
21 #include <memory>
22 #include <iostream>
23 #include <fstream>
24 
25 // user include files
26 #include "CLHEP/Random/RandGauss.h"
46 
47 //
48 // class declaration
49 //
50 
52 public:
54  ~SiStripNoisesFromDBMiscalibrator() override = default;
55 
56  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
57 
58 private:
59  void analyze(const edm::Event&, const edm::EventSetup&) override;
60  SiStripNoises getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
61  SiStripNoises getNewObject_withDefaults(const std::map<std::pair<uint32_t, int>, float>& theMap,
62  const float theDefault);
63  void endJob() override;
64 
65  // ----------member data ---------------------------
66  const uint32_t m_printdebug;
67  const bool m_perDetIDdebug;
68  const bool m_fillDefaults;
69  const bool m_saveMaps;
70  const std::vector<edm::ParameterSet> m_parameters;
74 
75  std::unique_ptr<TrackerMap> scale_map;
76  std::unique_ptr<TrackerMap> smear_map;
77  std::unique_ptr<TrackerMap> ratio_map;
78  std::unique_ptr<TrackerMap> old_payload_map;
79  std::unique_ptr<TrackerMap> new_payload_map;
80  std::unique_ptr<TrackerMap> missing_map;
81 };
82 
83 //
84 // constructors and destructor
85 //
87  : m_printdebug{iConfig.getUntrackedParameter<uint32_t>("printDebug", 10)},
88  m_perDetIDdebug{iConfig.getUntrackedParameter<bool>("perDetIDdebug", false)},
89  m_fillDefaults{iConfig.getUntrackedParameter<bool>("fillDefaults", false)},
90  m_saveMaps{iConfig.getUntrackedParameter<bool>("saveMaps", true)},
91  m_parameters{iConfig.getParameter<std::vector<edm::ParameterSet> >("params")},
92  fp_{iConfig.getUntrackedParameter<edm::FileInPath>("file",
94  m_tTopoToken(esConsumes()),
95  m_noiseToken(esConsumes()) {
96  //now do what ever initialization is needed
97 
98  scale_map = std::make_unique<TrackerMap>("scale");
99  scale_map->setTitle("Tracker Map of Scale factor averaged by module");
100  scale_map->setPalette(1);
101 
102  smear_map = std::make_unique<TrackerMap>("smear");
103  smear_map->setTitle("Tracker Map of Smear factor averaged by module");
104  smear_map->setPalette(1);
105 
106  old_payload_map = std::make_unique<TrackerMap>("old_payload");
107  old_payload_map->setTitle("Tracker Map of Starting Noise Payload averaged by module");
108  old_payload_map->setPalette(1);
109 
110  new_payload_map = std::make_unique<TrackerMap>("new_payload");
111  new_payload_map->setTitle("Tracker Map of Modified Noise Payload averaged by module");
112  new_payload_map->setPalette(1);
113 
114  ratio_map = std::make_unique<TrackerMap>("ratio");
115  ratio_map->setTitle("Tracker Map of Average by module of the payload ratio (new/old)");
116  ratio_map->setPalette(1);
117 
118  if (m_fillDefaults) {
119  missing_map = std::make_unique<TrackerMap>("uncabled");
120  missing_map->setTitle("Tracker Map of uncabled modules");
121  missing_map->setPalette(1);
122  }
123 }
124 
125 //
126 // member functions
127 //
128 
129 // ------------ method called for each event ------------
131  using namespace edm;
132 
133  const auto tTopo = &iSetup.getData(m_tTopoToken);
134 
135  std::vector<std::string> partitions;
136 
137  // fill the list of partitions
138  for (auto& thePSet : m_parameters) {
139  const std::string partition(thePSet.getParameter<std::string>("partition"));
140  // only if it is not yet in the list
141  if (std::find(partitions.begin(), partitions.end(), partition) == partitions.end()) {
142  partitions.push_back(partition);
143  }
144  }
145 
146  std::map<sistripsummary::TrackerRegion, SiStripMiscalibrate::Smearings> mapOfSmearings;
147 
148  for (auto& thePSet : m_parameters) {
149  const std::string partition(thePSet.getParameter<std::string>("partition"));
151 
152  bool m_doScale(thePSet.getParameter<bool>("doScale"));
153  bool m_doSmear(thePSet.getParameter<bool>("doSmear"));
154  double m_scaleFactor(thePSet.getParameter<double>("scaleFactor"));
155  double m_smearFactor(thePSet.getParameter<double>("smearFactor"));
156 
158  params.setSmearing(m_doScale, m_doSmear, m_scaleFactor, m_smearFactor);
159  mapOfSmearings[region] = params;
160  }
161 
162  const auto& stripNoises = iSetup.getData(m_noiseToken);
163 
164  std::map<std::pair<uint32_t, int>, float> theMap, oldPayloadMap;
165 
166  std::vector<uint32_t> detid;
167  stripNoises.getDetIds(detid);
168  for (const auto& d : detid) {
169  SiStripNoises::Range range = stripNoises.getRange(d);
170 
172 
173  // sort by largest to smallest
174  std::sort(regions.rbegin(), regions.rend());
175 
177 
178  for (unsigned int j = 0; j < regions.size(); j++) {
179  bool checkRegion = (mapOfSmearings.count(regions[j]) != 0);
180 
181  if (!checkRegion) {
182  // if the subdetector is not in the list and there's no indication for the whole tracker, just use the default
183  // i.e. no change
184  continue;
185  } else {
186  params = mapOfSmearings[regions[j]];
187  break;
188  }
189  }
190 
191  scale_map->fill(d, params.m_scaleFactor);
192  smear_map->fill(d, params.m_smearFactor);
193 
194  int nStrips = 0;
195  for (int it = 0; it < (range.second - range.first) * 8 / 9; ++it) {
196  auto noise = stripNoises.getNoise(it, range);
197  std::pair<uint32_t, int> index = std::make_pair(d, nStrips);
198 
199  oldPayloadMap[index] = noise;
200 
201  if (params.m_doScale) {
202  noise *= params.m_scaleFactor;
203  }
204 
205  if (params.m_doSmear) {
206  float smearedNoise = CLHEP::RandGauss::shoot(noise, params.m_smearFactor);
207  noise = smearedNoise;
208  }
209 
210  theMap[index] = noise;
211 
212  nStrips += 1;
213 
214  } // loop over APVs
215  } // loop over DetIds
216 
217  SiStripNoises theSiStripNoises{};
218  if (!m_fillDefaults) {
219  theSiStripNoises = this->getNewObject(theMap);
220  } else {
221  theSiStripNoises = this->getNewObject_withDefaults(theMap, -1.);
222  }
223 
224  // make the payload ratio map
225  uint32_t cachedId(0);
226  SiStripMiscalibrate::Entry noise_ratio;
229  uint countDetIds(0); // count DetIds to print
230  uint countStrips(0);
231  for (const auto& element : theMap) {
232  countStrips++;
233  uint32_t DetId = element.first.first;
234  int nstrip = element.first.second;
235  float new_noise = element.second;
236  float old_noise = oldPayloadMap[std::make_pair(DetId, nstrip)];
237 
238  // flush the counters
239  if (cachedId != 0 && DetId != cachedId) {
240  ratio_map->fill(cachedId, noise_ratio.mean());
241  old_payload_map->fill(cachedId, o_noise.mean());
242  new_payload_map->fill(cachedId, n_noise.mean());
243 
244  //auto test = new_payload_map.get()->smoduleMap;
245 
246  noise_ratio.reset();
247  o_noise.reset();
248  n_noise.reset();
249  countDetIds++;
250 
251  if (m_perDetIDdebug && (countDetIds < m_printdebug)) {
252  edm::LogPrint("SiStripNoisesFromDBMiscalibrator")
253  << "SiStripNoisesFromDBMiscalibrator"
254  << "::" << __FUNCTION__ << " detid " << DetId << " \t"
255  << " strip " << nstrip << " \t new <noise>: " << std::setw(5) << std::setprecision(2) << n_noise.mean()
256  << " \t old <noise>: " << o_noise.mean() << " \t" << std::endl;
257  }
258  }
259 
260  // printout for debug
261  if ((countStrips < m_printdebug) && !m_perDetIDdebug) {
262  edm::LogPrint("SiStripNoisesFromDBMiscalibrator")
263  << "SiStripNoisesFromDBMiscalibrator"
264  << "::" << __FUNCTION__ << " detid " << DetId << " \t"
265  << " strip " << nstrip << " \t new noise: " << std::setw(5) << std::setprecision(2) << new_noise
266  << " \t old noise: " << old_noise << " \t" << std::endl;
267  }
268 
269  cachedId = DetId;
270  noise_ratio.add(new_noise / old_noise);
271  o_noise.add(old_noise);
272  n_noise.add(new_noise);
273  }
274 
275  // write out the SiStripNoises record
277 
278  if (poolDbService.isAvailable()) {
279  if (poolDbService->isNewTagRequest("SiStripNoisesRcd")) {
280  poolDbService->createOneIOV(theSiStripNoises, poolDbService->currentTime(), "SiStripNoisesRcd");
281  } else {
282  poolDbService->appendOneIOV(theSiStripNoises, poolDbService->currentTime(), "SiStripNoisesRcd");
283  }
284  } else {
285  throw std::runtime_error("PoolDBService required.");
286  }
287 }
288 
289 // ------------ method called once each job just after ending the event loop ------------
291  if (m_saveMaps) {
292  scale_map->save(true, 0, 0, "noise_scale_map.pdf");
293  scale_map->save(true, 0, 0, "noise_scale_map.png");
294 
295  smear_map->save(true, 0, 0, "noise_smear_map.pdf");
296  smear_map->save(true, 0, 0, "noise_smear_map.png");
297 
298  ratio_map->save(true, 0, 0, "noise_ratio_map.pdf");
299  ratio_map->save(true, 0, 0, "noise_ratio_map.png");
300 
302 
303  old_payload_map->save(true, range.first, range.second, "starting_noise_payload_map.pdf");
304  old_payload_map->save(true, range.first, range.second, "starting_noise_payload_map.png");
305 
307 
308  new_payload_map->save(true, range.first, range.second, "new_noise_payload_map.pdf");
309  new_payload_map->save(true, range.first, range.second, "new_noise_payload_map.png");
310 
311  if (m_fillDefaults) {
312  missing_map->save(true, 0, 0, "missing_map.pdf");
313  missing_map->save(true, 0, 0, "missing_map.png");
314  }
315  }
316 }
317 
318 //********************************************************************************//
320  const std::map<std::pair<uint32_t, int>, float>& theMap, const float theDefault) {
321  SiStripNoises obj{};
322 
323  std::vector<uint32_t> missingDetIds;
324 
326  const auto& DetInfos = reader.getAllData();
327 
328  for (const auto& it : DetInfos) {
329  const auto& nAPVs = it.second.nApvs;
330  //Generate Noise for det detid
331  bool isMissing(false);
332  SiStripNoises::InputVector theSiStripVector;
333  for (int t_strip = 0; t_strip < 128 * nAPVs; ++t_strip) {
334  std::pair<uint32_t, int> index = std::make_pair(it.first, t_strip);
335 
336  if (theMap.find(index) == theMap.end()) {
337  LogDebug("SiStripNoisesFromDBMiscalibrator") << "detid " << it.first << " \t"
338  << " strip " << t_strip << " \t"
339  << " not found" << std::endl;
340 
341  isMissing = true;
342  obj.setData(theDefault, theSiStripVector);
343 
344  } else {
345  float noise = theMap.at(index);
346  obj.setData(noise, theSiStripVector);
347  }
348  }
349 
350  if (isMissing)
351  missingDetIds.push_back(it.first);
352 
353  if (!obj.put(it.first, theSiStripVector)) {
354  edm::LogError("SiStripNoisesFromDBMiscalibrator")
355  << "[SiStripNoisesFromDBMiscalibrator::analyze] detid already exists" << std::endl;
356  }
357  }
358 
359  if (!missingDetIds.empty()) {
360  // open output file
361  std::stringstream name;
362  name << "missing_modules.txt";
363  std::ofstream* ofile = new std::ofstream(name.str(), std::ofstream::trunc);
364  if (!ofile->is_open())
365  throw "cannot open output file!";
366  for (const auto& missing : missingDetIds) {
367  edm::LogVerbatim("SiStripNoisesFromDBMiscalibrator") << missing << " " << 1 << std::endl;
368  (*ofile) << missing << " " << 1 << std::endl;
369  missing_map->fill(missing, 1);
370  }
371 
372  ofile->close();
373  delete ofile;
374  }
375 
376  return obj;
377 }
378 
379 //********************************************************************************//
380 SiStripNoises SiStripNoisesFromDBMiscalibrator::getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap) {
381  SiStripNoises obj{};
382 
383  uint32_t PreviousDetId = 0;
384  SiStripNoises::InputVector theSiStripVector;
385  for (const auto& element : theMap) {
386  uint32_t DetId = element.first.first;
387  float noise = element.second;
388 
389  if (DetId != PreviousDetId) {
390  if (!theSiStripVector.empty()) {
391  if (!obj.put(PreviousDetId, theSiStripVector)) {
392  edm::LogError("SiStripNoisesFromDBMiscalibrator")
393  << "[SiStripNoisesFromDBMiscalibrator::analyze] detid already exists" << std::endl;
394  }
395  }
396 
397  theSiStripVector.clear();
398  PreviousDetId = DetId;
399  }
400  obj.setData(noise, theSiStripVector);
401  }
402  return obj;
403 }
404 
405 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
408 
409  desc.setComment(
410  "Creates rescaled / smeared SiStrip Noise payload."
411  "PoolDBOutputService must be set up for 'SiSiStripNoisesRcd'.");
412 
413  edm::ParameterSetDescription descScaler;
414  descScaler.setComment(
415  "ParameterSet specifying the Strip tracker partition to be scaled / smeared "
416  "by a given factor.");
417 
418  descScaler.add<std::string>("partition", "Tracker");
419  descScaler.add<bool>("doScale", true);
420  descScaler.add<bool>("doSmear", true);
421  descScaler.add<double>("scaleFactor", 1.0);
422  descScaler.add<double>("smearFactor", 1.0);
423  desc.addVPSet("params", descScaler, std::vector<edm::ParameterSet>(1));
424 
425  desc.addUntracked<unsigned int>("printDebug", 10);
426  desc.addUntracked<bool>("perDetIDdebug", false);
427  desc.addUntracked<bool>("fillDefaults", false);
428  desc.addUntracked<bool>("saveMaps", true);
429 
430  descriptions.add("scaleAndSmearSiStripNoises", desc);
431 }
432 
433 //define this as a plug-in
Log< level::Info, true > LogVerbatim
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
T const & getData(const ESGetToken< T, R > &iToken) const noexcept(false)
Definition: EventSetup.h:119
std::string fullPath() const
Definition: FileInPath.cc:161
std::vector< sistripsummary::TrackerRegion > getRegionsFromDetId(const TrackerTopology *m_trackerTopo, DetId detid)
sistripsummary::TrackerRegion getRegionFromString(std::string region)
std::vector< uint16_t > InputVector
Definition: SiStripNoises.h:50
reader
Definition: DQM.py:105
Log< level::Error, false > LogError
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
missing
Definition: combine.py:5
nStrips
1.2 is to make the matching window safely the two nearest strips 0.35 is the size of an ME0 chamber i...
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
SiStripNoises getNewObject_withDefaults(const std::map< std::pair< uint32_t, int >, float > &theMap, const float theDefault)
T getUntrackedParameter(std::string const &, T const &) const
void appendOneIOV(const T &payload, cond::Time_t sinceTime, const std::string &recordName)
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:224
bool isNewTagRequest(const std::string &recordName)
~SiStripNoisesFromDBMiscalibrator() override=default
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void analyze(const edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
SiStripDetInfo read(std::string filePath)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Log< level::Warning, true > LogPrint
d
Definition: ztail.py:151
Definition: DetId.h:17
std::pair< float, float > getTruncatedRange(const TrackerMap *theMap)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
const std::vector< edm::ParameterSet > m_parameters
SiStripNoises getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
SiStripNoisesFromDBMiscalibrator(const edm::ParameterSet &)
static constexpr char const *const kDefaultFile
bool isAvailable() const
Definition: Service.h:40
const edm::ESGetToken< SiStripNoises, SiStripNoisesRcd > m_noiseToken
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripNoises.h:47
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > m_tTopoToken
#define LogDebug(id)