CMS 3D CMS Logo

SiStripGainPayloadCopyAndExclude.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: CondTools/SiStrip
4 // Class: SiStripGainPayloadCopyAndExclude
5 //
6 /*
7  *\class SiStripGainPayloadCopyAndExclude SiStripGainPayloadCopyAndExclude.cc CondTools/SiStrip/plugins/SiStripGainPayloadCopyAndExclude.cc
8 
9  Description: This module is meant to copy the content of a SiStrip APV Gain payload (either G1 or G2) from a local sqlite file (that should be feeded to the Event Setup via the SiStripApvGain3Rcd and put in another local sqlite file, excepted for the modules specified in the excludedModules parameter. If the doReverse parameter is true, the opposite action is performed.
10 
11  Implementation: The implemenation takes advantage of the convenience record SiStripApvGain3Rcd in the EventSetup to be able to hold at the same time two instances of the Strip Gains in the same job.
12 
13 */
14 //
15 // Original Author: Marco Musich
16 // Created: Fri, 08 Jun 2018 08:28:01 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"
45 
46 //
47 // class declaration
48 //
49 class SiStripGainPayloadCopyAndExclude : public edm::one::EDAnalyzer<edm::one::SharedResources> {
50 public:
52  ~SiStripGainPayloadCopyAndExclude() override = default;
53 
54  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
55 
56 private:
57  void analyze(const edm::Event&, const edm::EventSetup&) override;
58  std::unique_ptr<SiStripApvGain> getNewObject(const std::map<std::pair<uint32_t, int>, float>& theMap);
59 
60  // ----------member data ---------------------------
63  std::vector<unsigned int> m_excludedMods;
65  const uint32_t m_gainType;
66  const bool m_reverseSelect;
67 };
68 
69 //
70 // constructors and destructor
71 //
73  : m_gainToken{esConsumes()},
74  m_gain3Token{esConsumes()},
75  m_excludedMods{iConfig.getUntrackedParameter<std::vector<unsigned int>>("excludedModules")},
76  m_Record{iConfig.getUntrackedParameter<std::string>("record", "SiStripApvGainRcd")},
77  m_gainType{iConfig.getUntrackedParameter<uint32_t>("gainType", 1)},
78  m_reverseSelect{iConfig.getUntrackedParameter<bool>("reverseSelection", false)} {
80 
81  //now do what ever initialization is needed
82  sort(m_excludedMods.begin(), m_excludedMods.end());
83 
84  edm::LogInfo("ExcludedModules") << "Selected module list";
85  for (std::vector<unsigned int>::const_iterator mod = m_excludedMods.begin(); mod != m_excludedMods.end(); mod++) {
86  edm::LogVerbatim("ExcludedModules") << *mod;
87  }
88 }
89 
90 //
91 // member functions
92 //
93 
94 // ------------ method called for each event ------------
96  using namespace edm;
97 
98  // gain to be validated
101 
102  std::map<std::pair<uint32_t, int>, float> theMap, oldPayloadMap;
103 
104  std::vector<uint32_t> detid;
105  gNew->getDetIds(detid);
106  for (const auto& d : detid) {
107  SiStripApvGain::Range range_new = gNew->getRange(d);
108  SiStripApvGain::Range range_old = gOld->getRange(d, m_gainType);
109  float nAPV = 0;
110 
111  for (int it = 0; it < range_new.second - range_new.first; it++) {
112  nAPV += 1;
113  float Gain = gNew->getApvGain(it, range_new);
114  float patchGain = gOld->getApvGain(it, range_old);
115  std::pair<uint32_t, int> index = std::make_pair(d, nAPV);
116 
117  oldPayloadMap[index] = Gain;
118 
119  bool found(false);
120  for (const auto& mod : m_excludedMods) {
121  if (d == mod) {
122  edm::LogInfo("ModuleFound") << " module " << mod << " found! Excluded... " << std::endl;
123  found = true;
124  break;
125  }
126  }
127 
128  if (m_reverseSelect)
129  found = (!found);
130 
131  if (!found) {
132  theMap[index] = Gain;
133  } else {
134  theMap[index] = patchGain;
135  }
136 
137  } // loop over APVs
138  } // loop over DetIds
139 
140  std::unique_ptr<SiStripApvGain> theAPVGains = this->getNewObject(theMap);
141 
142  // write out the APVGains record
144 
145  if (poolDbService.isAvailable())
146  poolDbService->writeOneIOV(theAPVGains.get(), poolDbService->currentTime(), m_Record);
147  else
148  throw std::runtime_error("PoolDBService required.");
149 }
150 
151 //********************************************************************************//
152 std::unique_ptr<SiStripApvGain> SiStripGainPayloadCopyAndExclude::getNewObject(
153  const std::map<std::pair<uint32_t, int>, float>& theMap) {
154  std::unique_ptr<SiStripApvGain> obj = std::make_unique<SiStripApvGain>();
155 
156  std::vector<float> theSiStripVector;
157  uint32_t PreviousDetId = 0;
158  for (const auto& element : theMap) {
159  uint32_t DetId = element.first.first;
160  if (DetId != PreviousDetId) {
161  if (!theSiStripVector.empty()) {
162  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
163  if (!obj->put(PreviousDetId, range))
164  printf("Bug to put detId = %i\n", PreviousDetId);
165  }
166  theSiStripVector.clear();
167  PreviousDetId = DetId;
168  }
169  theSiStripVector.push_back(element.second);
170 
171  edm::LogInfo("SiStripGainPayloadCopyAndExclude")
172  << " DetId: " << DetId << " APV: " << element.first.second << " Gain: " << element.second << std::endl;
173  }
174 
175  if (!theSiStripVector.empty()) {
176  SiStripApvGain::Range range(theSiStripVector.begin(), theSiStripVector.end());
177  if (!obj->put(PreviousDetId, range))
178  printf("Bug to put detId = %i\n", PreviousDetId);
179  }
180 
181  return obj;
182 }
183 
184 // ------------ method fills 'descriptions' with the allowed parameters for the module ------------
187  desc.addUntracked<std::vector<unsigned int>>("excludedModules", {});
188  desc.addUntracked<std::string>("record", "SiStripApvGainRcd");
189  desc.addUntracked<uint32_t>("gainType", 1);
190  desc.addUntracked<bool>("reverseSelection", false);
191  descriptions.addWithDefaultLabel(desc);
192 }
193 
194 //define this as a plug-in
Log< level::Info, true > LogVerbatim
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
static float getApvGain(uint16_t apv, const Range &range)
~SiStripGainPayloadCopyAndExclude() override=default
const SiStripApvGain::Range getRange(uint32_t detID) const
Definition: SiStripGain.h:75
static float getApvGain(const uint16_t &apv, const SiStripApvGain::Range &range)
Definition: SiStripGain.h:80
int iEvent
Definition: GenABIO.cc:224
void getDetIds(std::vector< uint32_t > &DetIds_) const
std::pair< ContainerIterator, ContainerIterator > Range
Hash writeOneIOV(const T &payload, Time_t time, const std::string &recordName)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const edm::ESGetToken< SiStripGain, SiStripGainRcd > m_gainToken
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:130
d
Definition: ztail.py:151
Log< level::Info, false > LogInfo
Definition: DetId.h:17
std::unique_ptr< SiStripApvGain > getNewObject(const std::map< std::pair< uint32_t, int >, float > &theMap)
SiStripGainPayloadCopyAndExclude(const edm::ParameterSet &)
static const std::string kSharedResource
const Range getRange(const uint32_t detID) const
void analyze(const edm::Event &, const edm::EventSetup &) override
HLT enums.
bool isAvailable() const
Definition: Service.h:40
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T mod(const T &a, const T &b)
Definition: ecalDccMap.h:4
const edm::ESGetToken< SiStripApvGain, SiStripApvGain3Rcd > m_gain3Token