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