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 analyze(const edm::Event&, const edm::EventSetup&) override;
59  SiStripApvGain getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
60  void endJob() override;
61 
62  // ----------member data ---------------------------
63  const uint32_t m_printdebug;
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_printdebug{iConfig.getUntrackedParameter<uint32_t>("printDebug", 1)},
83  m_Record{iConfig.getUntrackedParameter<std::string>("record", "SiStripApvGainRcd")},
84  m_gainType{iConfig.getUntrackedParameter<uint32_t>("gainType", 1)},
85  m_saveMaps{iConfig.getUntrackedParameter<bool>("saveMaps", true)},
86  m_parameters{iConfig.getParameter<std::vector<edm::ParameterSet> >("params")},
87  gainToken_(esConsumes()),
88  tTopoToken_(esConsumes()) {
89  //now do what ever initialization is needed
90 
91  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
92 
93  scale_map = std::make_unique<TrackerMap>("scale");
94  scale_map->setTitle("Scale factor averaged by module");
95  scale_map->setPalette(1);
96 
97  smear_map = std::make_unique<TrackerMap>("smear");
98  smear_map->setTitle("Smear factor averaged by module");
99  smear_map->setPalette(1);
100 
101  ratio_map = std::make_unique<TrackerMap>("ratio");
102  ratio_map->setTitle("Average by module of the " + ss_gain + " Gain payload ratio (new/old)");
103  ratio_map->setPalette(1);
104 
105  new_payload_map = std::make_unique<TrackerMap>("new_payload");
106  new_payload_map->setTitle("Tracker Map of Modified " + ss_gain + " Gain payload averaged by module");
107  new_payload_map->setPalette(1);
108 
109  old_payload_map = std::make_unique<TrackerMap>("old_payload");
110  old_payload_map->setTitle("Tracker Map of Starting " + ss_gain + " Gain Payload averaged by module");
111  old_payload_map->setPalette(1);
112 }
113 
115 
116 //
117 // member functions
118 //
119 
120 // ------------ method called for each event ------------
122  using namespace edm;
123 
124  const auto* const tTopo = &iSetup.getData(tTopoToken_);
125 
126  std::vector<std::string> partitions;
127 
128  // fill the list of partitions
129  for (auto& thePSet : m_parameters) {
130  const std::string partition(thePSet.getParameter<std::string>("partition"));
131  // only if it is not yet in the list
132  if (std::find(partitions.begin(), partitions.end(), partition) == partitions.end()) {
133  partitions.push_back(partition);
134  }
135  }
136 
137  std::map<sistripsummary::TrackerRegion, SiStripMiscalibrate::Smearings> mapOfSmearings;
138 
139  for (auto& thePSet : m_parameters) {
140  const std::string partition(thePSet.getParameter<std::string>("partition"));
142 
143  bool m_doScale(thePSet.getParameter<bool>("doScale"));
144  bool m_doSmear(thePSet.getParameter<bool>("doSmear"));
145  double m_scaleFactor(thePSet.getParameter<double>("scaleFactor"));
146  double m_smearFactor(thePSet.getParameter<double>("smearFactor"));
147 
149  params.setSmearing(m_doScale, m_doSmear, m_scaleFactor, m_smearFactor);
150  mapOfSmearings[region] = params;
151  }
152 
153  const auto& apvGain = iSetup.getData(gainToken_);
154 
155  std::map<std::pair<uint32_t, int>, float> theMap, oldPayloadMap;
156 
157  std::vector<uint32_t> detid;
158  apvGain.getDetIds(detid);
159  for (const auto& d : detid) {
160  SiStripApvGain::Range range = apvGain.getRange(d, m_gainType);
161  float nAPV = 0;
162 
164 
165  // sort by largest to smallest
166  std::sort(regions.rbegin(), regions.rend());
167 
169 
170  for (unsigned int j = 0; j < regions.size(); j++) {
171  bool checkRegion = (mapOfSmearings.count(regions[j]) != 0);
172 
173  if (!checkRegion) {
174  // if the subdetector is not in the list and there's no indication for the whole tracker, just use the default
175  // i.e. no change
176  continue;
177  } else {
178  params = mapOfSmearings[regions[j]];
179  break;
180  }
181  }
182 
183  scale_map->fill(d, params.m_scaleFactor);
184  smear_map->fill(d, params.m_smearFactor);
185 
186  for (int it = 0; it < range.second - range.first; it++) {
187  nAPV += 1;
188  float Gain = apvGain.getApvGain(it, range);
189  std::pair<uint32_t, int> index = std::make_pair(d, nAPV);
190 
191  oldPayloadMap[index] = Gain;
192 
193  if (params.m_doScale) {
194  Gain *= params.m_scaleFactor;
195  }
196 
197  if (params.m_doSmear) {
198  float smearedGain = CLHEP::RandGauss::shoot(Gain, params.m_smearFactor);
199  Gain = smearedGain;
200  }
201 
202  theMap[index] = Gain;
203 
204  } // loop over APVs
205  } // loop over DetIds
206 
207  SiStripApvGain theAPVGains = this->getNewObject(theMap);
208 
209  // make the payload ratio map
210  uint32_t cachedId(0);
211  SiStripMiscalibrate::Entry gain_ratio;
214  unsigned int countDetIds(0); // count DetIds to print
215  for (const auto& element : theMap) {
216  uint32_t DetId = element.first.first;
217  int nAPV = element.first.second;
218  float new_gain = element.second;
219  float old_gain = oldPayloadMap[std::make_pair(DetId, nAPV)];
220 
221  // flush the counters
222  if (cachedId != 0 && DetId != cachedId) {
223  ratio_map->fill(cachedId, gain_ratio.mean());
224  old_payload_map->fill(cachedId, o_gain.mean());
225  new_payload_map->fill(cachedId, n_gain.mean());
226 
227  //auto test = new_payload_map.get()->smoduleMap;
228 
229  gain_ratio.reset();
230  o_gain.reset();
231  n_gain.reset();
232  countDetIds++;
233  }
234 
235  // printout for debug
236  if (countDetIds < m_printdebug) {
237  edm::LogPrint("SiStripChannelGainFromDBMiscalibrator")
238  << "SiStripChannelGainFromDBMiscalibrator"
239  << "::" << __FUNCTION__ << " detid " << DetId << " \t"
240  << " APV " << nAPV << " \t new gain: " << new_gain << " \t old gain: " << old_gain << " \t" << std::endl;
241  }
242 
243  cachedId = DetId;
244  gain_ratio.add(new_gain / old_gain);
245  o_gain.add(old_gain);
246  n_gain.add(new_gain);
247  }
248 
249  // write out the APVGains record
251 
252  if (poolDbService.isAvailable()) {
253  if (poolDbService->isNewTagRequest(m_Record)) {
254  poolDbService->createOneIOV(theAPVGains, poolDbService->currentTime(), m_Record);
255  } else {
256  poolDbService->appendOneIOV(theAPVGains, poolDbService->currentTime(), m_Record);
257  }
258  } else {
259  throw std::runtime_error("PoolDBService required.");
260  }
261 }
262 
263 // ------------ method called once each job just after ending the event loop ------------
265  if (m_saveMaps) {
266  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
267 
268  scale_map->save(true, 0, 0, ss_gain + "_gain_scale_map.pdf");
269  scale_map->save(true, 0, 0, ss_gain + "_gain_scale_map.png");
270 
271  smear_map->save(true, 0, 0, ss_gain + "_gain_smear_map.pdf");
272  smear_map->save(true, 0, 0, ss_gain + "_gain_smear_map.png");
273 
274  ratio_map->save(true, 0, 0, ss_gain + "_gain_ratio_map.pdf");
275  ratio_map->save(true, 0, 0, ss_gain + "_gain_ratio_map.png");
276 
278 
279  old_payload_map->save(true, range.first, range.second, "starting_" + ss_gain + "_gain_payload_map.pdf");
280  old_payload_map->save(true, range.first, range.second, "starting_" + ss_gain + "_gain_payload_map.png");
281 
283 
284  new_payload_map->save(true, range.first, range.second, "new_" + ss_gain + "_gain_payload_map.pdf");
285  new_payload_map->save(true, range.first, range.second, "new_" + ss_gain + "_gain_payload_map.png");
286  }
287 }
288 
289 //********************************************************************************//
291  const std::map<std::pair<uint32_t, int>, float>& theMap) {
293 
294  std::vector<float> theSiStripVector;
295  uint32_t PreviousDetId = 0;
296  for (const auto& element : theMap) {
297  uint32_t DetId = element.first.first;
298  if (DetId != PreviousDetId) {
299  if (!theSiStripVector.empty()) {
300  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
301  if (!obj.put(PreviousDetId, range))
302  printf("Bug to put detId = %i\n", PreviousDetId);
303  }
304  theSiStripVector.clear();
305  PreviousDetId = DetId;
306  }
307  theSiStripVector.push_back(element.second);
308 
309  edm::LogInfo("SiStripChannelGainFromDBMiscalibrator")
310  << " DetId: " << DetId << " APV: " << element.first.second << " Gain: " << element.second << std::endl;
311  }
312 
313  if (!theSiStripVector.empty()) {
314  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
315  if (!obj.put(PreviousDetId, range))
316  printf("Bug to put detId = %i\n", PreviousDetId);
317  }
318 
319  return obj;
320 }
321 
322 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
325 
326  desc.setComment(
327  "Creates rescaled / smeared SiStrip Gain payload. Can be used for both G1 and G2."
328  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
329 
330  edm::ParameterSetDescription descScaler;
331  descScaler.setComment(
332  "ParameterSet specifying the Strip tracker partition to be scaled / smeared "
333  "by a given factor.");
334 
335  descScaler.add<std::string>("partition", "Tracker");
336  descScaler.add<bool>("doScale", true);
337  descScaler.add<bool>("doSmear", true);
338  descScaler.add<double>("scaleFactor", 1.0);
339  descScaler.add<double>("smearFactor", 1.0);
340  desc.addVPSet("params", descScaler, std::vector<edm::ParameterSet>(1));
341 
342  desc.addUntracked<unsigned int>("printDebug", 1);
343  desc.addUntracked<std::string>("record", "SiStripApvGainRcd");
344  desc.addUntracked<unsigned int>("gainType", 1);
345  desc.addUntracked<bool>("saveMaps", true);
346 
347  descriptions.add("scaleAndSmearSiStripGains", desc);
348 }
349 
350 /*--------------------------------------------------------------------*/
351 
352 //define this as a plug-in
const edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > tTopoToken_
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::vector< sistripsummary::TrackerRegion > getRegionsFromDetId(const TrackerTopology *m_trackerTopo, DetId detid)
sistripsummary::TrackerRegion getRegionFromString(std::string region)
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
void createOneIOV(const T &payload, cond::Time_t firstSinceTime, const std::string &recordName)
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)
const edm::ESGetToken< SiStripGain, SiStripGainRcd > gainToken_
std::pair< ContainerIterator, ContainerIterator > Range
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
SiStripApvGain getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Log< level::Warning, true > LogPrint
d
Definition: ztail.py:151
void analyze(const edm::Event &, const edm::EventSetup &) override
Log< level::Info, false > LogInfo
Definition: DetId.h:17
std::pair< float, float > getTruncatedRange(const TrackerMap *theMap)
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const std::vector< edm::ParameterSet > m_parameters
HLT enums.
bool isAvailable() const
Definition: Service.h:40
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)