CMS 3D CMS Logo

SiStripChannelGainFromDBMiscalibrator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiStrip
4 // Class: SiStripChannelGainFromDBMiscalibrator
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 
24 // user include files
25 #include "CLHEP/Random/RandGauss.h"
46 //
47 // class declaration
48 //
49 
51 public:
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 private:
58  void beginJob() override;
59  void analyze(const edm::Event&, const edm::EventSetup&) override;
60  std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
61  void endJob() override;
62 
63  // ----------member data ---------------------------
65  const uint32_t m_gainType;
66  const bool m_saveMaps;
67  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 };
77 
78 //
79 // constructors and destructor
80 //
82  : m_Record{iConfig.getUntrackedParameter<std::string>("record", "SiStripApvGainRcd")},
83  m_gainType{iConfig.getUntrackedParameter<uint32_t>("gainType", 1)},
84  m_saveMaps{iConfig.getUntrackedParameter<bool>("saveMaps", true)},
85  m_parameters{iConfig.getParameter<std::vector<edm::ParameterSet> >("params")},
86  gainToken_(esConsumes()),
87  tTopoToken_(esConsumes()) {
88  //now do what ever initialization is needed
89 
90  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
91 
92  scale_map = std::make_unique<TrackerMap>("scale");
93  scale_map->setTitle("Scale factor averaged by module");
94  scale_map->setPalette(1);
95 
96  smear_map = std::make_unique<TrackerMap>("smear");
97  smear_map->setTitle("Smear factor averaged by module");
98  smear_map->setPalette(1);
99 
100  ratio_map = std::make_unique<TrackerMap>("ratio");
101  ratio_map->setTitle("Average by module of the " + ss_gain + " Gain payload ratio (new/old)");
102  ratio_map->setPalette(1);
103 
104  new_payload_map = std::make_unique<TrackerMap>("new_payload");
105  new_payload_map->setTitle("Tracker Map of Modified " + ss_gain + " Gain payload averaged by module");
106  new_payload_map->setPalette(1);
107 
108  old_payload_map = std::make_unique<TrackerMap>("old_payload");
109  old_payload_map->setTitle("Tracker Map of Starting " + ss_gain + " Gain Payload averaged by module");
110  old_payload_map->setPalette(1);
111 }
112 
114 
115 //
116 // member functions
117 //
118 
119 // ------------ method called for each event ------------
121  using namespace edm;
122 
123  const auto* const tTopo = &iSetup.getData(tTopoToken_);
124 
125  std::vector<std::string> partitions;
126 
127  // fill the list of partitions
128  for (auto& thePSet : m_parameters) {
129  const std::string partition(thePSet.getParameter<std::string>("partition"));
130  // only if it is not yet in the list
131  if (std::find(partitions.begin(), partitions.end(), partition) == partitions.end()) {
132  partitions.push_back(partition);
133  }
134  }
135 
136  std::map<sistripsummary::TrackerRegion, SiStripMiscalibrate::Smearings> mapOfSmearings;
137 
138  for (auto& thePSet : m_parameters) {
139  const std::string partition(thePSet.getParameter<std::string>("partition"));
141 
142  bool m_doScale(thePSet.getParameter<bool>("doScale"));
143  bool m_doSmear(thePSet.getParameter<bool>("doSmear"));
144  double m_scaleFactor(thePSet.getParameter<double>("scaleFactor"));
145  double m_smearFactor(thePSet.getParameter<double>("smearFactor"));
146 
148  params.setSmearing(m_doScale, m_doSmear, m_scaleFactor, m_smearFactor);
149  mapOfSmearings[region] = params;
150  }
151 
152  const auto& apvGain = iSetup.getData(gainToken_);
153 
154  std::map<std::pair<uint32_t, int>, float> theMap, oldPayloadMap;
155 
156  std::vector<uint32_t> detid;
157  apvGain.getDetIds(detid);
158  for (const auto& d : detid) {
159  SiStripApvGain::Range range = apvGain.getRange(d, m_gainType);
160  float nAPV = 0;
161 
163 
164  // sort by largest to smallest
165  std::sort(regions.rbegin(), regions.rend());
166 
168 
169  for (unsigned int j = 0; j < regions.size(); j++) {
170  bool checkRegion = (mapOfSmearings.count(regions[j]) != 0);
171 
172  if (!checkRegion) {
173  // if the subdetector is not in the list and there's no indication for the whole tracker, just use the default
174  // i.e. no change
175  continue;
176  } else {
177  params = mapOfSmearings[regions[j]];
178  break;
179  }
180  }
181 
182  scale_map->fill(d, params.m_scaleFactor);
183  smear_map->fill(d, params.m_smearFactor);
184 
185  for (int it = 0; it < range.second - range.first; it++) {
186  nAPV += 1;
187  float Gain = apvGain.getApvGain(it, range);
188  std::pair<uint32_t, int> index = std::make_pair(d, nAPV);
189 
190  oldPayloadMap[index] = Gain;
191 
192  if (params.m_doScale) {
193  Gain *= params.m_scaleFactor;
194  }
195 
196  if (params.m_doSmear) {
197  float smearedGain = CLHEP::RandGauss::shoot(Gain, params.m_smearFactor);
198  Gain = smearedGain;
199  }
200 
201  theMap[index] = Gain;
202 
203  } // loop over APVs
204  } // loop over DetIds
205 
206  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
207 
208  // make the payload ratio map
209  uint32_t cachedId(0);
210  SiStripMiscalibrate::Entry gain_ratio;
213  for (const auto& element : theMap) {
214  uint32_t DetId = element.first.first;
215  int nAPV = element.first.second;
216  float new_gain = element.second;
217  float old_gain = oldPayloadMap[std::make_pair(DetId, nAPV)];
218 
219  // flush the counters
220  if (cachedId != 0 && DetId != cachedId) {
221  ratio_map->fill(cachedId, gain_ratio.mean());
222  old_payload_map->fill(cachedId, o_gain.mean());
223  new_payload_map->fill(cachedId, n_gain.mean());
224 
225  //auto test = new_payload_map.get()->smoduleMap;
226 
227  gain_ratio.reset();
228  o_gain.reset();
229  n_gain.reset();
230  }
231 
232  cachedId = DetId;
233  gain_ratio.add(new_gain / old_gain);
234  o_gain.add(old_gain);
235  n_gain.add(new_gain);
236  }
237 
238  // write out the APVGains record
240 
241  if (poolDbService.isAvailable())
242  poolDbService->writeOne(theAPVGains.get(), poolDbService->currentTime(), m_Record);
243  else
244  throw std::runtime_error("PoolDBService required.");
245 }
246 
247 // ------------ method called once each job just before starting event loop ------------
249 
250 // ------------ method called once each job just after ending the event loop ------------
252  if (m_saveMaps) {
253  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
254 
255  scale_map->save(true, 0, 0, ss_gain + "_gain_scale_map.pdf");
256  scale_map->save(true, 0, 0, ss_gain + "_gain_scale_map.png");
257 
258  smear_map->save(true, 0, 0, ss_gain + "_gain_smear_map.pdf");
259  smear_map->save(true, 0, 0, ss_gain + "_gain_smear_map.png");
260 
261  ratio_map->save(true, 0, 0, ss_gain + "_gain_ratio_map.pdf");
262  ratio_map->save(true, 0, 0, ss_gain + "_gain_ratio_map.png");
263 
265 
266  old_payload_map->save(true, range.first, range.second, "starting_" + ss_gain + "_gain_payload_map.pdf");
267  old_payload_map->save(true, range.first, range.second, "starting_" + ss_gain + "_gain_payload_map.png");
268 
270 
271  new_payload_map->save(true, range.first, range.second, "new_" + ss_gain + "_gain_payload_map.pdf");
272  new_payload_map->save(true, range.first, range.second, "new_" + ss_gain + "_gain_payload_map.png");
273  }
274 }
275 
276 //********************************************************************************//
278  const std::map<std::pair<uint32_t, int>, float>& theMap) {
279  std::unique_ptr<SiStripApvGain> obj = std::make_unique<SiStripApvGain>();
280 
281  std::vector<float> theSiStripVector;
282  uint32_t PreviousDetId = 0;
283  for (const auto& element : theMap) {
284  uint32_t DetId = element.first.first;
285  if (DetId != PreviousDetId) {
286  if (!theSiStripVector.empty()) {
287  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
288  if (!obj->put(PreviousDetId, range))
289  printf("Bug to put detId = %i\n", PreviousDetId);
290  }
291  theSiStripVector.clear();
292  PreviousDetId = DetId;
293  }
294  theSiStripVector.push_back(element.second);
295 
296  edm::LogInfo("SiStripChannelGainFromDBMiscalibrator")
297  << " DetId: " << DetId << " APV: " << element.first.second << " Gain: " << element.second << std::endl;
298  }
299 
300  if (!theSiStripVector.empty()) {
301  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
302  if (!obj->put(PreviousDetId, range))
303  printf("Bug to put detId = %i\n", PreviousDetId);
304  }
305 
306  return obj;
307 }
308 
309 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
312 
313  desc.setComment(
314  "Creates rescaled / smeared SiStrip Gain payload. Can be used for both G1 and G2."
315  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
316 
317  edm::ParameterSetDescription descScaler;
318  descScaler.setComment(
319  "ParameterSet specifying the Strip tracker partition to be scaled / smeared "
320  "by a given factor.");
321 
322  descScaler.add<std::string>("partition", "Tracker");
323  descScaler.add<bool>("doScale", true);
324  descScaler.add<bool>("doSmear", true);
325  descScaler.add<double>("scaleFactor", 1.0);
326  descScaler.add<double>("smearFactor", 1.0);
327  desc.addVPSet("params", descScaler, std::vector<edm::ParameterSet>(1));
328 
329  desc.addUntracked<std::string>("record", "SiStripApvGainRcd");
330  desc.addUntracked<unsigned int>("gainType", 1);
331  desc.addUntracked<bool>("saveMaps", true);
332 
333  descriptions.add("scaleAndSmearSiStripGains", desc);
334 }
335 
336 /*--------------------------------------------------------------------*/
337 
338 //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
SiStripChannelGainFromDBMiscalibrator::tTopoToken_
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
Definition: SiStripChannelGainFromDBMiscalibrator.cc:69
ESHandle.h
SiStripChannelGainFromDBMiscalibrator::analyze
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: SiStripChannelGainFromDBMiscalibrator.cc:120
SiStripGain.h
SiStripChannelGainFromDBMiscalibrator::m_gainType
const uint32_t m_gainType
Definition: SiStripChannelGainFromDBMiscalibrator.cc:65
CalibrationSummaryClient_cfi.params
params
Definition: CalibrationSummaryClient_cfi.py:14
edm
HLT enums.
Definition: AlignableModifier.h:19
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
SiStripMiscalibrate::Smearings
Definition: SiStripMiscalibrateHelper.h:45
SiStripGainRcd.h
SiStripDetId.h
IntegrityClient_cfi.Gain
Gain
Definition: IntegrityClient_cfi.py:16
SiStripMiscalibrate::Entry::add
void add(double val)
Definition: SiStripMiscalibrateHelper.h:25
SiStripApvGainRcd.h
edm::ParameterSet::getUntrackedParameter
T getUntrackedParameter(std::string const &, T const &) const
AlignmentPI::partitions
partitions
Definition: AlignmentPayloadInspectorHelper.h:48
edm::LogInfo
Log< level::Info, false > LogInfo
Definition: MessageLogger.h:125
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
edm::Service::isAvailable
bool isAvailable() const
Definition: Service.h:40
SiStripChannelGainFromDBMiscalibrator::gainToken_
edm::ESGetToken< SiStripGain, SiStripGainRcd > gainToken_
Definition: SiStripChannelGainFromDBMiscalibrator.cc:68
SiStripChannelGainFromDBMiscalibrator::m_Record
const std::string m_Record
Definition: SiStripChannelGainFromDBMiscalibrator.cc:64
DetId
Definition: DetId.h:17
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
sistripsummary::TrackerRegion
TrackerRegion
Definition: SiStripSummary.h:19
Service.h
SiStripChannelGainFromDBMiscalibrator::m_saveMaps
const bool m_saveMaps
Definition: SiStripChannelGainFromDBMiscalibrator.cc:66
SiStripChannelGainFromDBMiscalibrator::new_payload_map
std::unique_ptr< TrackerMap > new_payload_map
Definition: SiStripChannelGainFromDBMiscalibrator.cc:75
SiStripChannelGainFromDBMiscalibrator::getNewObject
std::unique_ptr< SiStripApvGain > getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
Definition: SiStripChannelGainFromDBMiscalibrator.cc:277
SiStripApvGain.h
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
SiStripChannelGainFromDBMiscalibrator::m_parameters
const std::vector< edm::ParameterSet > m_parameters
Definition: SiStripChannelGainFromDBMiscalibrator.cc:67
SiStripChannelGainFromDBMiscalibrator::~SiStripChannelGainFromDBMiscalibrator
~SiStripChannelGainFromDBMiscalibrator() override
Definition: SiStripChannelGainFromDBMiscalibrator.cc:113
SiStripChannelGainFromDBMiscalibrator::ratio_map
std::unique_ptr< TrackerMap > ratio_map
Definition: SiStripChannelGainFromDBMiscalibrator.cc:73
edm::ParameterSet
Definition: ParameterSet.h:47
edm::ParameterSetDescription::setComment
void setComment(std::string const &value)
Definition: ParameterSetDescription.cc:33
Event.h
SiStripChannelGainFromDBMiscalibrator::SiStripChannelGainFromDBMiscalibrator
SiStripChannelGainFromDBMiscalibrator(const edm::ParameterSet &)
Definition: SiStripChannelGainFromDBMiscalibrator.cc:81
SiStripMiscalibrate::getTruncatedRange
std::pair< float, float > getTruncatedRange(const TrackerMap *theMap)
Definition: SiStripMiscalibrateHelper.cc:87
SiStripChannelGainFromDBMiscalibrator::endJob
void endJob() override
Definition: SiStripChannelGainFromDBMiscalibrator.cc:251
SiStripSummary.h
HLT_FULL_cff.region
region
Definition: HLT_FULL_cff.py:88272
edm::Service< cond::service::PoolDBOutputService >
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::EventSetup
Definition: EventSetup.h:57
SiStripMiscalibrate::Entry::reset
void reset()
Definition: SiStripMiscalibrateHelper.h:31
SiStripMiscalibrateHelper.h
edm::ESGetToken< SiStripGain, SiStripGainRcd >
TrackerMap.h
edm::EventSetup::getData
bool getData(T &iHolder) const
Definition: EventSetup.h:120
SiStripChannelGainFromDBMiscalibrator::beginJob
void beginJob() override
Definition: SiStripChannelGainFromDBMiscalibrator.cc:248
submitPVResolutionJobs.desc
string desc
Definition: submitPVResolutionJobs.py:251
SiStripChannelGainFromDBMiscalibrator::smear_map
std::unique_ptr< TrackerMap > smear_map
Definition: SiStripChannelGainFromDBMiscalibrator.cc:72
SiStripChannelGainFromDBMiscalibrator::old_payload_map
std::unique_ptr< TrackerMap > old_payload_map
Definition: SiStripChannelGainFromDBMiscalibrator.cc:74
SiStripChannelGainFromDBMiscalibrator::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: SiStripChannelGainFromDBMiscalibrator.cc:310
Frameworkfwd.h
SiStripApvGain::Range
std::pair< ContainerIterator, ContainerIterator > Range
Definition: SiStripApvGain.h:28
cond::service::PoolDBOutputService::writeOne
Hash writeOne(const T *payload, Time_t time, const std::string &recordName)
Definition: PoolDBOutputService.h:63
EventSetup.h
SiStripChannelGainFromDBMiscalibrator::scale_map
std::unique_ptr< TrackerMap > scale_map
Definition: SiStripChannelGainFromDBMiscalibrator.cc:71
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
SiStripChannelGainFromDBMiscalibrator
Definition: SiStripChannelGainFromDBMiscalibrator.cc:50
ztail.d
d
Definition: ztail.py:151
AlignmentPI::regions
regions
Definition: AlignmentPayloadInspectorHelper.h:76
SiStripMiscalibrate::getRegionFromString
sistripsummary::TrackerRegion getRegionFromString(std::string region)
Definition: SiStripMiscalibrateHelper.cc:8
genParticles_cff.map
map
Definition: genParticles_cff.py:11
ParameterSet.h
SiStripMiscalibrate::Entry::mean
double mean()
Definition: SiStripMiscalibrateHelper.h:18
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
edm::Event
Definition: Event.h:73
StripSubdetector.h
cond::service::PoolDBOutputService::currentTime
cond::Time_t currentTime() const
Definition: PoolDBOutputService.cc:217
SiStripMiscalibrate::Entry
Definition: SiStripMiscalibrateHelper.h:14
SiStripMiscalibrate::getRegionsFromDetId
std::vector< sistripsummary::TrackerRegion > getRegionsFromDetId(const TrackerTopology *m_trackerTopo, DetId detid)
Definition: SiStripMiscalibrateHelper.cc:38