CMS 3D CMS Logo

SiStripChannelGainFromDBMiscalibrator.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiStrip
4 // Class: SiStripChannelGainFromDBMiscalibrator
5 //
13 //
14 // Original Author: Marco Musich
15 // Created: Tue, 03 Oct 2017 12:57:34 GMT
16 //
17 //
18 
19 
20 // system include files
21 #include <memory>
22 #include <iostream>
23 
24 // user include files
27 
30 
33 
40 
47 
48 #include "TRandom3.h"
49 
50 //
51 // class declaration
52 //
53 
54 namespace ApvGain {
55  struct GainSmearings{
57  m_doScale = false;
58  m_doSmear = false;
59  m_scaleFactor = 1.;
60  m_smearFactor = 0.;
61  }
63 
64  void setSmearing(bool doScale,bool doSmear,double the_scaleFactor,double the_smearFactor){
65  m_doScale = doScale;
66  m_doSmear = doSmear;
67  m_scaleFactor = the_scaleFactor;
68  m_smearFactor = the_smearFactor;
69  }
70 
71  bool m_doScale;
72  bool m_doSmear;
73  double m_scaleFactor;
74  double m_smearFactor;
75  };
76 
77 }
78 
80  public:
83 
84  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
85 
86  private:
87  virtual void beginJob() override;
88  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
89  std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap);
90  sistripsummary::TrackerRegion getRegionFromString(std::string region);
91  std::vector<sistripsummary::TrackerRegion> getRegionsFromDetId(const TrackerTopology* tTopo,DetId detid);
92  virtual void endJob() override;
93 
94  // ----------member data ---------------------------
96  const uint32_t m_gainType;
97  const std::vector<edm::ParameterSet> m_parameters;
98 };
99 
100 //
101 // constructors and destructor
102 //
104  m_Record{iConfig.getUntrackedParameter<std::string> ("record" , "SiStripApvGainRcd")},
105  m_gainType{iConfig.getUntrackedParameter<uint32_t>("gainType",1)},
106  m_parameters{iConfig.getParameter<std::vector<edm::ParameterSet> >("params")}
107 {
108  //now do what ever initialization is needed
109 }
110 
111 
113 {
114 }
115 
116 
117 //
118 // member functions
119 //
120 
121 // ------------ method called for each event ------------
122 void
124 {
125  using namespace edm;
126 
127  edm::ESHandle<TrackerTopology> tTopoHandle;
128  iSetup.get<TrackerTopologyRcd>().get(tTopoHandle);
129  const auto* const tTopo = tTopoHandle.product();
130 
131  std::vector<std::string> partitions;
132 
133  // fill the list of partitions
134  for(auto& thePSet : m_parameters){
135  const std::string partition(thePSet.getParameter<std::string>("partition"));
136  // only if it is not yet in the list
137  if(std::find(partitions.begin(), partitions.end(), partition) == partitions.end()) {
138  partitions.push_back(partition);
139  }
140  }
141 
142  std::map<sistripsummary::TrackerRegion,ApvGain::GainSmearings> mapOfSmearings;
143 
144  for(auto& thePSet : m_parameters){
145 
146  const std::string partition(thePSet.getParameter<std::string>("partition"));
147  sistripsummary::TrackerRegion region = this->getRegionFromString(partition);
148 
149  bool m_doScale(thePSet.getParameter<bool>("doScale"));
150  bool m_doSmear(thePSet.getParameter<bool>("doSmear"));
151  double m_scaleFactor(thePSet.getParameter<double>("scaleFactor"));
152  double m_smearFactor(thePSet.getParameter<double>("smearFactor"));
153 
155  params.setSmearing(m_doScale,m_doSmear,m_scaleFactor,m_smearFactor);
156  mapOfSmearings[region]=params;
157  }
158 
159 
160  edm::ESHandle<SiStripGain> SiStripApvGain_;
161  iSetup.get<SiStripGainRcd>().get(SiStripApvGain_);
162 
163  std::map<std::pair<uint32_t,int>,float> theMap;
164  std::shared_ptr<TRandom3> random(new TRandom3(1));
165 
166  std::vector<uint32_t> detid;
167  SiStripApvGain_->getDetIds(detid);
168  for (const auto & d : detid) {
169  SiStripApvGain::Range range=SiStripApvGain_->getRange(d,m_gainType);
170  float nAPV=0;
171 
172  auto regions = getRegionsFromDetId(tTopo,d);
173 
174  // sort by largest to smallest
175  std::sort(regions.rbegin(), regions.rend());
176 
178 
179  for (unsigned int j=0; j<regions.size();j++){
180  bool checkRegion = (mapOfSmearings.count(regions[j]) != 0);
181 
182  if(!checkRegion) {
183  // if the subdetector is not in the list and there's no indication for the whole tracker, just use the default
184  // i.e. no change
185  continue;
186  } else {
187  params = mapOfSmearings[regions[j]];
188  break;
189  }
190  }
191 
192  for(int it=0;it<range.second-range.first;it++){
193  nAPV+=1;
194  float Gain=SiStripApvGain_->getApvGain(it,range);
195  std::pair<uint32_t,int> index = std::make_pair(d,nAPV);
196 
197  if(params.m_doScale){
198  Gain*=params.m_scaleFactor;
199  }
200 
201  if(params.m_doSmear){
202  float smearedGain = random->Gaus(Gain,params.m_smearFactor);
203  Gain=smearedGain;
204  }
205 
206  theMap[index]=Gain;
207 
208  } // loop over APVs
209  } // loop over DetIds
210 
211  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
212 
213  // write out the APVGains record
215 
216  if( poolDbService.isAvailable() )
217  poolDbService->writeOne(theAPVGains.get(),poolDbService->currentTime(),m_Record);
218  else
219  throw std::runtime_error("PoolDBService required.");
220 
221 }
222 
223 
224 // ------------ method called once each job just before starting event loop ------------
225 void
227 {
228 }
229 
230 // ------------ method called once each job just after ending the event loop ------------
231 void
233 {
234 }
235 
236 //********************************************************************************//
237 std::unique_ptr<SiStripApvGain>
238 SiStripChannelGainFromDBMiscalibrator::getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap)
239 {
240  std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());
241 
242  std::vector<float> theSiStripVector;
243  uint32_t PreviousDetId = 0;
244  for(const auto &element : theMap){
245  uint32_t DetId = element.first.first;
246  if(DetId != PreviousDetId){
247  if(!theSiStripVector.empty()){
248  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
249  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
250  }
251  theSiStripVector.clear();
252  PreviousDetId = DetId;
253  }
254  theSiStripVector.push_back(element.second);
255 
256  edm::LogInfo("SiStripChannelGainFromDBMiscalibrator")<<" DetId: "<<DetId
257  <<" APV: "<<element.first.second
258  <<" Gain: "<<element.second
259  <<std::endl;
260  }
261 
262  if(!theSiStripVector.empty()){
263  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
264  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
265  }
266 
267  return obj;
268 }
269 
270 
271 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
272 void
275 
276  desc.setComment("Creates rescaled / smeared SiStrip Gain payload. Can be used for both G1 and G2."
277  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
278 
279  edm::ParameterSetDescription descScaler;
280  descScaler.setComment("ParameterSet specifying the Strip tracker partition to be scaled / smeared "
281  "by a given factor.");
282 
283  descScaler.add<std::string>("partition", "Tracker");
284  descScaler.add<bool>("doScale",true);
285  descScaler.add<bool>("doSmear",true);
286  descScaler.add<double>("scaleFactor", 1.0);
287  descScaler.add<double>("smearFactor", 1.0);
288  desc.addVPSet("params", descScaler, std::vector<edm::ParameterSet>(1));
289 
290  desc.addUntracked<std::string>("record","SiStripApvGainRcd");
291  desc.addUntracked<unsigned int>("gainType",1);
292 
293  descriptions.add("scaleAndSmearSiStripGains", desc);
294 
295 }
296 
297 /*--------------------------------------------------------------------*/
299 /*--------------------------------------------------------------------*/
300 {
301  if(region.find("Tracker")!=std::string::npos){
302  return sistripsummary::TRACKER ;
303  } else if(region.find("TIB")!=std::string::npos){
304  if (region=="TIB_1") return sistripsummary::TIB_1;
305  else if (region=="TIB_2") return sistripsummary::TIB_2;
306  else if (region=="TIB_3") return sistripsummary::TIB_3;
307  else if (region=="TIB_4") return sistripsummary::TIB_4;
308  else return sistripsummary::TIB ;
309  } else if(region.find("TOB")!=std::string::npos){
310  if (region=="TOB_1") return sistripsummary::TOB_1;
311  else if (region=="TOB_2") return sistripsummary::TOB_2;
312  else if (region=="TOB_3") return sistripsummary::TOB_3;
313  else if (region=="TOB_4") return sistripsummary::TOB_4;
314  else if (region=="TOB_5") return sistripsummary::TOB_5;
315  else if (region=="TOB_6") return sistripsummary::TOB_6;
316  else return sistripsummary::TOB ;
317  } else if(region.find("TID")!=std::string::npos){
318  if(region.find("TIDM")!=std::string::npos){
319  if (region=="TIDM_1") return sistripsummary::TIDM_1;
320  else if (region=="TIDM_2") return sistripsummary::TIDM_2;
321  else if (region=="TIDM_3") return sistripsummary::TIDM_3;
322  else return sistripsummary::TIDM;
323  } else if(region.find("TIDP")!=std::string::npos){
324  if (region=="TIDP_1") return sistripsummary::TIDP_1;
325  else if (region=="TIDP_2") return sistripsummary::TIDP_2;
326  else if (region=="TIDP_3") return sistripsummary::TIDP_3;
327  else return sistripsummary::TIDP;
328  } else return sistripsummary::TID ;
329  } else if(region.find("TEC")!=std::string::npos) {
330  if(region.find("TECM")!=std::string::npos){
331  if (region=="TECM_1") return sistripsummary::TECM_1;
332  else if (region=="TECM_2") return sistripsummary::TECM_2;
333  else if (region=="TECM_3") return sistripsummary::TECM_3;
334  else if (region=="TECM_4") return sistripsummary::TECM_4;
335  else if (region=="TECM_5") return sistripsummary::TECM_5;
336  else if (region=="TECM_6") return sistripsummary::TECM_6;
337  else if (region=="TECM_7") return sistripsummary::TECM_7;
338  else if (region=="TECM_8") return sistripsummary::TECM_8;
339  else if (region=="TECM_9") return sistripsummary::TECM_9;
340  else return sistripsummary::TECM;
341  } else if(region.find("TECP")!=std::string::npos){
342  if (region=="TECP_1") return sistripsummary::TECP_1;
343  else if (region=="TECP_2") return sistripsummary::TECP_2;
344  else if (region=="TECP_3") return sistripsummary::TECP_3;
345  else if (region=="TECP_4") return sistripsummary::TECP_4;
346  else if (region=="TECP_5") return sistripsummary::TECP_5;
347  else if (region=="TECP_6") return sistripsummary::TECP_6;
348  else if (region=="TECP_7") return sistripsummary::TECP_7;
349  else if (region=="TECP_8") return sistripsummary::TECP_8;
350  else if (region=="TECP_9") return sistripsummary::TECP_9;
351  else return sistripsummary::TECP;
352  } else return sistripsummary::TEC ;
353  } else {
354  edm::LogError("LogicError") << "Unknown partition: " << region;
355  throw cms::Exception("Invalid Partition passed");
356  }
357 }
358 
359 /*--------------------------------------------------------------------*/
360 std::vector<sistripsummary::TrackerRegion> SiStripChannelGainFromDBMiscalibrator::getRegionsFromDetId(const TrackerTopology* m_trackerTopo,DetId detid)
361 /*--------------------------------------------------------------------*/
362 {
363  int layer = 0;
364  int side = 0;
365  int subdet = 0;
366  int detCode = 0;
367 
368  std::vector<sistripsummary::TrackerRegion> ret;
369 
370  switch (detid.subdetId()) {
372  layer = m_trackerTopo->tibLayer(detid);
373  subdet = 1;
374  break;
376  layer = m_trackerTopo->tobLayer(detid);
377  subdet = 2;
378  break;
380  // is this module in TID+ or TID-?
381  layer = m_trackerTopo->tidWheel(detid);
382  side = m_trackerTopo->tidSide(detid);
383  subdet = 3*10+side;
384  break;
386  // is this module in TEC+ or TEC-?
387  layer = m_trackerTopo->tecWheel(detid);
388  side = m_trackerTopo->tecSide(detid);
389  subdet = 4*10+side;
390  break;
391  }
392 
393  detCode = (subdet*10)+layer;
394 
395  ret.push_back(static_cast<sistripsummary::TrackerRegion>(detCode));
396 
397  if(subdet/10 > 0) {
398  ret.push_back(static_cast<sistripsummary::TrackerRegion>(subdet/10));
399  }
400 
401  ret.push_back(static_cast<sistripsummary::TrackerRegion>(subdet));
402  ret.push_back(sistripsummary::TRACKER);
403 
404  return ret;
405 }
406 
407 
408 //define this as a plug-in
410 
411 
T getUntrackedParameter(std::string const &, T const &) const
ParameterDescriptionBase * addVPSet(U const &iLabel, ParameterSetDescription const &validator, std::vector< ParameterSet > const &defaults)
unsigned int tibLayer(const DetId &id) const
ParameterDescriptionBase * addUntracked(U const &iLabel, T const &value)
virtual example_stream void analyze(const edm::Event &, const edm::EventSetup &) override
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
unsigned int tidWheel(const DetId &id) const
TRandom random
Definition: MVATrainer.cc:138
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:20
void beginJob()
Definition: Breakpoints.cc:15
static float getApvGain(const uint16_t &apv, const SiStripApvGain::Range &range)
Definition: SiStripGain.h:73
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:230
unsigned int tidSide(const DetId &id) const
void setSmearing(bool doScale, bool doSmear, double the_scaleFactor, double the_smearFactor)
std::vector< sistripsummary::TrackerRegion > getRegionsFromDetId(const TrackerTopology *tTopo, DetId detid)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool isAvailable() const
Definition: Service.h:46
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)
int subdetId() const
get the contents of the subdetector field (not cast into any detector&#39;s numbering enum) ...
Definition: DetId.h:38
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:108
virtual 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)
const T & get() const
Definition: EventSetup.h:59
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const std::vector< edm::ParameterSet > m_parameters
HLT enums.
sistripsummary::TrackerRegion getRegionFromString(std::string region)
unsigned int tecWheel(const DetId &id) const
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T const * product() const
Definition: ESHandle.h:86
unsigned int tobLayer(const DetId &id) const
unsigned int tecSide(const DetId &id) const
const SiStripApvGain::Range getRange(uint32_t detID) const
Definition: SiStripGain.h:70