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