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 
21 // system include files
22 #include <memory>
23 #include <iostream>
24 
25 // user include files
26 #include "CLHEP/Random/RandGauss.h"
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<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap);
62  void endJob() override;
63 
64  // ----------member data ---------------------------
66  const uint32_t m_gainType;
67  const bool m_saveMaps;
68  const std::vector<edm::ParameterSet> m_parameters;
69 
70  std::unique_ptr<TrackerMap> scale_map;
71  std::unique_ptr<TrackerMap> smear_map;
72  std::unique_ptr<TrackerMap> ratio_map;
73  std::unique_ptr<TrackerMap> old_payload_map;
74  std::unique_ptr<TrackerMap> new_payload_map;
75 
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 {
87  //now do what ever initialization is needed
88 
89  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
90 
91  scale_map = std::unique_ptr<TrackerMap>(new TrackerMap("scale"));
92  scale_map->setTitle("Scale factor averaged by module");
93  scale_map->setPalette(1);
94 
95  smear_map =std::unique_ptr<TrackerMap>(new TrackerMap("smear"));
96  smear_map->setTitle("Smear factor averaged by module");
97  smear_map->setPalette(1);
98 
99  ratio_map = std::unique_ptr<TrackerMap>(new TrackerMap("ratio"));
100  ratio_map->setTitle("Average by module of the "+ss_gain+" Gain payload ratio (new/old)");
101  ratio_map->setPalette(1);
102 
103  new_payload_map =std::unique_ptr<TrackerMap>(new TrackerMap("new_payload"));
104  new_payload_map->setTitle("Tracker Map of Modified "+ss_gain+" Gain payload averaged by module");
105  new_payload_map->setPalette(1);
106 
107  old_payload_map =std::unique_ptr<TrackerMap>(new TrackerMap("old_payload"));
108  old_payload_map->setTitle("Tracker Map of Starting "+ss_gain+" Gain Payload averaged by module");
109  old_payload_map->setPalette(1);
110 
111 }
112 
113 
115 {
116 }
117 
118 
119 //
120 // member functions
121 //
122 
123 // ------------ method called for each event ------------
124 void
126 {
127  using namespace edm;
128 
129  edm::ESHandle<TrackerTopology> tTopoHandle;
130  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
131  const auto* const tTopo = tTopoHandle.product();
132 
133  std::vector<std::string> partitions;
134 
135  // fill the list of partitions
136  for(auto& thePSet : m_parameters){
137  const std::string partition(thePSet.getParameter<std::string>("partition"));
138  // only if it is not yet in the list
139  if(std::find(partitions.begin(), partitions.end(), partition) == partitions.end()) {
140  partitions.push_back(partition);
141  }
142  }
143 
144  std::map<sistripsummary::TrackerRegion,SiStripMiscalibrate::Smearings> mapOfSmearings;
145 
146  for(auto& thePSet : m_parameters){
147 
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 
162  edm::ESHandle<SiStripGain> SiStripApvGain_;
163  iSetup.get<SiStripGainRcd>().get(SiStripApvGain_);
164 
165  std::map<std::pair<uint32_t,int>,float> theMap,oldPayloadMap;
166 
167  std::vector<uint32_t> detid;
168  SiStripApvGain_->getDetIds(detid);
169  for (const auto & d : detid) {
170  SiStripApvGain::Range range=SiStripApvGain_->getRange(d,m_gainType);
171  float nAPV=0;
172 
174 
175  // sort by largest to smallest
176  std::sort(regions.rbegin(), regions.rend());
177 
179 
180  for (unsigned int j=0; j<regions.size();j++){
181  bool checkRegion = (mapOfSmearings.count(regions[j]) != 0);
182 
183  if(!checkRegion) {
184  // if the subdetector is not in the list and there's no indication for the whole tracker, just use the default
185  // i.e. no change
186  continue;
187  } else {
188  params = mapOfSmearings[regions[j]];
189  break;
190  }
191  }
192 
193  scale_map->fill(d,params.m_scaleFactor);
194  smear_map->fill(d,params.m_smearFactor);
195 
196  for(int it=0;it<range.second-range.first;it++){
197  nAPV+=1;
198  float Gain=SiStripApvGain_->getApvGain(it,range);
199  std::pair<uint32_t,int> index = std::make_pair(d,nAPV);
200 
201  oldPayloadMap[index]=Gain;
202 
203  if(params.m_doScale){
204  Gain*=params.m_scaleFactor;
205  }
206 
207  if(params.m_doSmear){
208  float smearedGain = CLHEP::RandGauss::shoot(Gain,params.m_smearFactor);
209  Gain=smearedGain;
210  }
211 
212  theMap[index]=Gain;
213 
214  } // loop over APVs
215  } // loop over DetIds
216 
217  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
218 
219  // make the payload ratio map
220  uint32_t cachedId(0);
221  SiStripMiscalibrate::Entry gain_ratio;
224  for(const auto &element : theMap){
225 
226  uint32_t DetId = element.first.first;
227  int nAPV = element.first.second;
228  float new_gain = element.second;
229  float old_gain = oldPayloadMap[std::make_pair(DetId,nAPV)];
230 
231  // flush the counters
232  if(cachedId!=0 && DetId!=cachedId){
233  ratio_map->fill(cachedId,gain_ratio.mean());
234  old_payload_map->fill(cachedId,o_gain.mean());
235  new_payload_map->fill(cachedId,n_gain.mean());
236 
237  //auto test = new_payload_map.get()->smoduleMap;
238 
239  gain_ratio.reset();
240  o_gain.reset();
241  n_gain.reset();
242  }
243 
244  cachedId=DetId;
245  gain_ratio.add(new_gain/old_gain);
246  o_gain.add(old_gain);
247  n_gain.add(new_gain);
248  }
249 
250  // write out the APVGains record
252 
253  if( poolDbService.isAvailable() )
254  poolDbService->writeOne(theAPVGains.get(),poolDbService->currentTime(),m_Record);
255  else
256  throw std::runtime_error("PoolDBService required.");
257 
258 }
259 
260 // ------------ method called once each job just before starting event loop ------------
261 void
263 {
264 }
265 
266 // ------------ method called once each job just after ending the event loop ------------
267 void
269 {
270  if(m_saveMaps){
271 
272  std::string ss_gain = (m_gainType > 0) ? "G2" : "G1";
273 
274  scale_map->save(true,0,0,ss_gain+"_gain_scale_map.pdf");
275  scale_map->save(true,0,0,ss_gain+"_gain_scale_map.png");
276 
277  smear_map->save(true,0,0,ss_gain+"_gain_smear_map.pdf");
278  smear_map->save(true,0,0,ss_gain+"_gain_smear_map.png");
279 
280  ratio_map->save(true,0,0,ss_gain+"_gain_ratio_map.pdf");
281  ratio_map->save(true,0,0,ss_gain+"_gain_ratio_map.png");
282 
284 
285  old_payload_map->save(true,range.first,range.second,"starting_"+ss_gain+"_gain_payload_map.pdf");
286  old_payload_map->save(true,range.first,range.second,"starting_"+ss_gain+"_gain_payload_map.png");
287 
289 
290  new_payload_map->save(true,range.first,range.second,"new_"+ss_gain+"_gain_payload_map.pdf");
291  new_payload_map->save(true,range.first,range.second,"new_"+ss_gain+"_gain_payload_map.png");
292  }
293 }
294 
295 //********************************************************************************//
296 std::unique_ptr<SiStripApvGain>
297 SiStripChannelGainFromDBMiscalibrator::getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap)
298 {
299  std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());
300 
301  std::vector<float> theSiStripVector;
302  uint32_t PreviousDetId = 0;
303  for(const auto &element : theMap){
304  uint32_t DetId = element.first.first;
305  if(DetId != PreviousDetId){
306  if(!theSiStripVector.empty()){
307  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
308  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
309  }
310  theSiStripVector.clear();
311  PreviousDetId = DetId;
312  }
313  theSiStripVector.push_back(element.second);
314 
315  edm::LogInfo("SiStripChannelGainFromDBMiscalibrator")<<" DetId: "<<DetId
316  <<" APV: "<<element.first.second
317  <<" Gain: "<<element.second
318  <<std::endl;
319  }
320 
321  if(!theSiStripVector.empty()){
322  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
323  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
324  }
325 
326  return obj;
327 }
328 
329 
330 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
331 void
334 
335  desc.setComment("Creates rescaled / smeared SiStrip Gain payload. Can be used for both G1 and G2."
336  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
337 
338  edm::ParameterSetDescription descScaler;
339  descScaler.setComment("ParameterSet specifying the Strip tracker partition to be scaled / smeared "
340  "by a given factor.");
341 
342  descScaler.add<std::string>("partition", "Tracker");
343  descScaler.add<bool>("doScale",true);
344  descScaler.add<bool>("doSmear",true);
345  descScaler.add<double>("scaleFactor", 1.0);
346  descScaler.add<double>("smearFactor", 1.0);
347  desc.addVPSet("params", descScaler, std::vector<edm::ParameterSet>(1));
348 
349  desc.addUntracked<std::string>("record","SiStripApvGainRcd");
350  desc.addUntracked<unsigned int>("gainType",1);
351  desc.addUntracked<bool>("saveMaps",true);
352 
353  descriptions.add("scaleAndSmearSiStripGains", desc);
354 
355 }
356 
357 /*--------------------------------------------------------------------*/
358 
359 //define this as a plug-in
361 
362 
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:20
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
void analyze(const edm::Event &, const edm::EventSetup &) override
Definition: DetId.h:18
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:71
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