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