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 // system include files
20 #include <memory>
21 #include <iostream>
22 
23 // user include files
26 
29 
31 
36 
40 
41 //
42 // class declaration
43 //
44 
46 public:
48  ~SiStripApvGainRescaler() override;
49 
50  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
51 
52 private:
53  void analyze(const edm::Event&, const edm::EventSetup&) override;
54  std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
55 
56  // ----------member data ---------------------------
57  const uint32_t m_printdebug;
59 
60  // take G2_old and G1_old from the regular gain handle
62  // take the additional G1_new from the Gain3Rcd (dirty trick)
64 };
65 
66 //
67 // constructors and destructor
68 //
70  : m_printdebug{iConfig.getUntrackedParameter<uint32_t>("printDebug", 1)},
71  m_Record(iConfig.getParameter<std::string>("Record")),
72  g1g2Token_(esConsumes()),
73  g3Token_(esConsumes()) {}
74 
76 //
77 // member functions
78 //
79 
80 // ------------ method called for each event ------------
82  using namespace edm;
83 
84  const auto& g1g2 = iSetup.getData(g1g2Token_);
85  const auto& g3 = iSetup.getData(g3Token_);
86 
87  std::map<std::pair<uint32_t, int>, float> theMap;
88 
89  std::vector<uint32_t> detid;
90  g1g2.getDetIds(detid);
91  for (const auto& d : detid) {
92  SiStripApvGain::Range rangeG1_old = g1g2.getRange(d, 0);
93  SiStripApvGain::Range rangeG2_old = g1g2.getRange(d, 1);
94  SiStripApvGain::Range rangeG1_new = g3.getRange(d);
95 
96  int nAPV = 0;
97  for (int it = 0; it < rangeG1_old.second - rangeG1_old.first; it++) {
98  nAPV++;
99 
100  std::pair<uint32_t, int> index = std::make_pair(d, nAPV);
101 
102  float G1_old = g1g2.getApvGain(it, rangeG1_old);
103  float G2_old = g1g2.getApvGain(it, rangeG2_old);
104  float G1G2_old = G1_old * G2_old;
105  float G1_new = g3.getApvGain(it, rangeG1_new);
106 
107  // this is based on G1_old*G2_old = G1_new * G2_new ==> G2_new = (G1_old*G2_old)/G1_new
108 
109  float NewGain = G1G2_old / G1_new;
110 
111  // DO NOT RESCALE APVs set to the default value
112  if (G2_old != 1.) {
113  theMap[index] = NewGain;
114  } else {
115  theMap[index] = 1.;
116  }
117 
118  } // loop over APVs
119  } // loop over DetIds
120 
121  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
122 
123  // write out the APVGains record
125 
126  if (poolDbService.isAvailable())
127  poolDbService->writeOneIOV(*theAPVGains, poolDbService->currentTime(), m_Record);
128  else
129  throw std::runtime_error("PoolDBService required.");
130 }
131 
132 //********************************************************************************//
133 std::unique_ptr<SiStripApvGain> SiStripApvGainRescaler::getNewObject(
134  const std::map<std::pair<uint32_t, int>, float>& theMap) {
135  std::unique_ptr<SiStripApvGain> obj = std::make_unique<SiStripApvGain>();
136 
137  std::vector<float> theSiStripVector;
138  uint32_t PreviousDetId = 0;
139  unsigned int countDetIds(0); // count DetIds to print
140  for (const auto& element : theMap) {
141  uint32_t DetId = element.first.first;
142  if (DetId != PreviousDetId) {
143  if (!theSiStripVector.empty()) {
144  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
145  if (!obj->put(PreviousDetId, range))
146  edm::LogError("SiStripApvGainRescaler") << "Bug to put detId = " << PreviousDetId << "\n";
147  }
148  theSiStripVector.clear();
149  PreviousDetId = DetId;
150  countDetIds++;
151  }
152  theSiStripVector.push_back(element.second);
153 
154  if (countDetIds <= m_printdebug) {
155  edm::LogInfo("SiStripApvGainRescaler")
156  << __FUNCTION__ << " DetId: " << DetId << " APV: " << element.first.second << " Gain: " << element.second;
157  }
158  }
159 
160  if (!theSiStripVector.empty()) {
161  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
162  if (!obj->put(PreviousDetId, range))
163  edm::LogError("SiStripApvGainRescaler") << "Bug to put detId = " << PreviousDetId << "\n";
164  }
165 
166  return obj;
167 }
168 
169 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
172 
173  desc.setComment(
174  " Utility class to rescale the values of SiStrip G2 by the ratio of G1_old/G1_new: this is useful in the case in "
175  "which a Gain2 payload needs to recycled after a G1 update to keep the G1*G2 product constant."
176  "PoolDBOutputService must be set up for 'SiStripApvGainRcd'.");
177 
178  desc.add<std::string>("Record", "SiStripApvGainRcd");
179  desc.addUntracked<unsigned int>("printDebug", 1);
180  descriptions.add("rescaleGain2byGain1", desc);
181 }
182 
183 //define this as a plug-in
std::unique_ptr< SiStripApvGain > getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
SiStripApvGainRescaler(const edm::ParameterSet &)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void analyze(const edm::Event &, const edm::EventSetup &) override
T getUntrackedParameter(std::string const &, T const &) const
int iEvent
Definition: GenABIO.cc:224
std::pair< ContainerIterator, ContainerIterator > Range
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
bool getData(T &iHolder) const
Definition: EventSetup.h:122
d
Definition: ztail.py:151
Log< level::Info, false > LogInfo
Definition: DetId.h:17
~SiStripApvGainRescaler() override
void add(std::string const &label, ParameterSetDescription const &psetDescription)
const edm::ESGetToken< SiStripApvGain, SiStripApvGain3Rcd > g3Token_
HLT enums.
bool isAvailable() const
Definition: Service.h:40
const edm::ESGetToken< SiStripGain, SiStripGainRcd > g1g2Token_