CMS 3D CMS Logo

SiStripApvGainRescaler.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiStrip
4 // Class: SiStripApvGainRescaler
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 
38 
42 
43 //
44 // class declaration
45 //
46 
48  public:
51 
52  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
53 
54  private:
55  virtual void beginJob() override;
56  virtual void analyze(const edm::Event&, const edm::EventSetup&) override;
57  std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap);
58  virtual void endJob() override;
59 
60  // ----------member data ---------------------------
62 
63 };
64 
65 //
66 // constructors and destructor
67 //
69  m_Record(iConfig.getParameter<std::string> ("Record"))
70 {
71  //now do what ever initialization is needed
72 }
73 
75 {
76 
77  // do anything here that needs to be done at desctruction time
78  // (e.g. close files, deallocate resources etc.)
79 }
80 
81 
82 //
83 // member functions
84 //
85 
86 // ------------ method called for each event ------------
87 void
89 {
90  using namespace edm;
91 
92  // take G2_old and G1_old from the regular gain handle
93  edm::ESHandle<SiStripGain> g1g2Handle_;
94  iSetup.get<SiStripGainRcd>().get(g1g2Handle_);
95 
96  // take the additional G1_new from the Gain3Rcd (dirty trick)
98  iSetup.get<SiStripApvGain3Rcd>().get(g3Handle_);
99 
100  std::map<std::pair<uint32_t,int>,float> theMap;
101 
102  std::vector<uint32_t> detid;
103  g1g2Handle_->getDetIds(detid);
104  for (const auto & d : detid) {
105 
106  SiStripApvGain::Range rangeG1_old = g1g2Handle_->getRange(d,0);
107  SiStripApvGain::Range rangeG2_old = g1g2Handle_->getRange(d,1);
108  SiStripApvGain::Range rangeG1_new = g3Handle_->getRange(d);
109 
110  int nAPV=0;
111  for(int it=0;it<rangeG1_old.second-rangeG1_old.first;it++){
112  nAPV++;
113 
114  std::pair<uint32_t,int> index = std::make_pair(d,nAPV);
115 
116  float G1_old = g1g2Handle_->getApvGain(it,rangeG1_old);
117  float G2_old = g1g2Handle_->getApvGain(it,rangeG2_old);
118  float G1G2_old = G1_old*G2_old;
119  float G1_new = g3Handle_->getApvGain(it,rangeG1_new);
120 
121  // this is based on G1_old*G2_old = G1_new * G2_new ==> G2_new = (G1_old*G2_old)/G1_new
122 
123  float NewGain = G1G2_old/G1_new;
124 
125  // DO NOT RESCALE APVs set to the default value
126  if(G2_old!=1.){
127  theMap[index]=NewGain;
128  } else {
129  theMap[index]=1.;
130  }
131 
132  } // loop over APVs
133  } // loop over DetIds
134 
135  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
136 
137  // write out the APVGains record
139 
140  if( poolDbService.isAvailable() )
141  poolDbService->writeOne(theAPVGains.get(),poolDbService->currentTime(),m_Record);
142  else
143  throw std::runtime_error("PoolDBService required.");
144 
145 }
146 
147 
148 // ------------ method called once each job just before starting event loop ------------
149 void
151 {
152 }
153 
154 // ------------ method called once each job just after ending the event loop ------------
155 void
157 {
158 }
159 
160 //********************************************************************************//
161 std::unique_ptr<SiStripApvGain>
162 SiStripApvGainRescaler::getNewObject(const std::map<std::pair<uint32_t,int>,float>& theMap)
163 {
164  std::unique_ptr<SiStripApvGain> obj = std::unique_ptr<SiStripApvGain>(new SiStripApvGain());
165 
166  std::vector<float> theSiStripVector;
167  uint32_t PreviousDetId = 0;
168  for(const auto &element : theMap){
169  uint32_t DetId = element.first.first;
170  if(DetId != PreviousDetId){
171  if(!theSiStripVector.empty()){
172  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
173  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
174  }
175  theSiStripVector.clear();
176  PreviousDetId = DetId;
177  }
178  theSiStripVector.push_back(element.second);
179 
180  edm::LogInfo("SiStripApvGainRescaler")<<" DetId: "<<DetId
181  <<" APV: "<<element.first.second
182  <<" Gain: "<<element.second
183  <<std::endl;
184  }
185 
186  if(!theSiStripVector.empty()){
187  SiStripApvGain::Range range(theSiStripVector.begin(),theSiStripVector.end());
188  if ( !obj->put(PreviousDetId,range) ) printf("Bug to put detId = %i\n",PreviousDetId);
189  }
190 
191  return obj;
192 }
193 
194 
195 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
196 void
199 
200  desc.setComment(" Utility class to rescale the values of SiStrip G2 by the ratio of G1_old/G1_new: this is useful in the case in which a Gain2 payload needs to recycled after a G1 update to keep the G1*G2 product constant."
201  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
202 
203  desc.add<std::string>("Record","SiStripApvGainRcd");
204  descriptions.add("rescaleGain2byGain1", desc);
205 }
206 
207 //define this as a plug-in
virtual void endJob() override
std::unique_ptr< SiStripApvGain > getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
virtual void beginJob() override
static float getApvGain(uint16_t apv, const Range &range)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
SiStripApvGainRescaler(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static float getApvGain(const uint16_t &apv, const SiStripApvGain::Range &range)
Definition: SiStripGain.h:73
virtual void analyze(const edm::Event &, const edm::EventSetup &) override
void setComment(std::string const &value)
int iEvent
Definition: GenABIO.cc:230
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)
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
Definition: DetId.h:18
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HLT enums.
T get() const
Definition: EventSetup.h:63
const Range getRange(const uint32_t detID) const
const SiStripApvGain::Range getRange(uint32_t detID) const
Definition: SiStripGain.h:70