CMS 3D CMS Logo

CalibratedElectronProducers.cc
Go to the documentation of this file.
1 //author: Alan Smithee
2 //description:
3 // this class allows the residual scale and smearing to be applied to electrons
4 // the scale and smearing is on the ecal part of the energy
5 // hence the E/p combination needs to be re-don, hence the E/p Combination Tools
6 // it re-applies the regression with the new corrected ecal energy
7 // returns a vector of calibrated energies and correction data, indexed by EGEnergySysIndex
8 // a port of EgammaAnalysis/ElectronTools/CalibratedElectronProducerRun2
9 
20 
25 
32 
33 #include "TRandom2.h"
34 
35 #include <memory>
36 
37 #include <vector>
38 
39 template <typename T>
41 public:
44  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
45  void produce(edm::Event&, const edm::EventSetup&) override;
46 
47 private:
48  void setSemiDetRandomSeed(const edm::Event& iEvent, const T& obj, size_t nrObjs, size_t objNr);
49 
51 
54  std::unique_ptr<TRandom> semiDeterministicRng_;
58  static const std::vector<int> valMapsToStore_;
59 };
60 
61 template <typename T>
62 const std::vector<int> CalibratedElectronProducerT<T>::valMapsToStore_ = {
72 
73 namespace {
74  template <typename HandleType, typename ValType>
75  void fillAndStoreValueMap(edm::Event& iEvent,
76  HandleType objHandle,
77  const std::vector<ValType>& vals,
78  const std::string& name) {
79  auto valMap = std::make_unique<edm::ValueMap<ValType>>();
80  typename edm::ValueMap<ValType>::Filler filler(*valMap);
81  filler.insert(objHandle, vals.begin(), vals.end());
82  filler.fill();
83  iEvent.put(std::move(valMap), name);
84  }
85 } // namespace
86 
87 template <typename T>
89  : electronToken_(consumes(conf.getParameter<edm::InputTag>("src"))),
90  epCombinationTool_{conf.getParameterSet("epCombConfig"), consumesCollector()},
91  energyCorrector_(epCombinationTool_, conf.getParameter<std::string>("correctionFile")),
92  recHitCollectionEBToken_(consumes(conf.getParameter<edm::InputTag>("recHitCollectionEB"))),
93  recHitCollectionEEToken_(consumes(conf.getParameter<edm::InputTag>("recHitCollectionEE"))),
94  produceCalibratedObjs_(conf.getParameter<bool>("produceCalibratedObjs")) {
95  energyCorrector_.setMinEt(conf.getParameter<double>("minEtToCalibrate"));
96 
97  if (conf.getParameter<bool>("semiDeterministic")) {
98  semiDeterministicRng_ = std::make_unique<TRandom2>();
99  energyCorrector_.initPrivateRng(semiDeterministicRng_.get());
100  }
101 
102  if (produceCalibratedObjs_)
103  produces<std::vector<T>>();
104 
105  for (const auto& toStore : valMapsToStore_) {
106  produces<edm::ValueMap<float>>(EGEnergySysIndex::name(toStore));
107  }
108 }
109 
110 template <typename T>
113  desc.add<edm::InputTag>("src", edm::InputTag("gedPhotons"));
115  desc.add<edm::InputTag>("recHitCollectionEB", edm::InputTag("reducedEcalRecHitsEB"));
116  desc.add<edm::InputTag>("recHitCollectionEE", edm::InputTag("reducedEcalRecHitsEE"));
117  desc.add<std::string>("correctionFile", std::string());
118  desc.add<double>("minEtToCalibrate", 5.0);
119  desc.add<bool>("produceCalibratedObjs", true);
120  desc.add<bool>("semiDeterministic", true);
121  std::vector<std::string> valMapsProduced;
122  valMapsProduced.reserve(valMapsToStore_.size());
123  for (auto varToStore : valMapsToStore_)
124  valMapsProduced.push_back(EGEnergySysIndex::name(varToStore));
125  desc.add<std::vector<std::string>>("valueMapsStored", valMapsProduced)
126  ->setComment(
127  "provides to python configs the list of valuemaps stored, can not be overriden in the python config");
128  descriptions.addWithDefaultLabel(desc);
129 }
130 
131 template <typename T>
133  epCombinationTool_.setEventContent(iSetup);
134 
135  auto inHandle = iEvent.getHandle(electronToken_);
136 
137  auto recHitCollectionEBHandle = iEvent.getHandle(recHitCollectionEBToken_);
138  auto recHitCollectionEEHandle = iEvent.getHandle(recHitCollectionEEToken_);
139 
140  std::unique_ptr<std::vector<T>> out = std::make_unique<std::vector<T>>();
141 
142  size_t nrObj = inHandle->size();
143  std::array<std::vector<float>, EGEnergySysIndex::kNrSysErrs> results;
144  for (auto& res : results)
145  res.reserve(nrObj);
146 
149 
150  for (const auto& ele : *inHandle) {
151  out->push_back(ele);
152 
153  if (semiDeterministicRng_)
154  setSemiDetRandomSeed(iEvent, ele, nrObj, out->size());
155 
157  (ele.isEB()) ? recHitCollectionEBHandle.product() : recHitCollectionEEHandle.product();
158  std::array<float, EGEnergySysIndex::kNrSysErrs> uncertainties =
159  energyCorrector_.calibrate(out->back(), iEvent.id().run(), recHits, iEvent.streamID(), evtType);
160 
161  for (size_t index = 0; index < EGEnergySysIndex::kNrSysErrs; index++) {
162  results[index].push_back(uncertainties[index]);
163  }
164  }
165 
166  auto fillAndStore = [&](auto handle) {
167  for (const auto& mapToStore : valMapsToStore_) {
168  fillAndStoreValueMap(iEvent, handle, results[mapToStore], EGEnergySysIndex::name(mapToStore));
169  }
170  };
171 
172  if (produceCalibratedObjs_) {
173  fillAndStore(iEvent.put(std::move(out)));
174  } else {
175  fillAndStore(inHandle);
176  }
177 }
178 
179 template <typename T>
181  const T& obj,
182  size_t nrObjs,
183  size_t objNr) {
184  if (obj.superCluster().isNonnull()) {
185  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromSC(iEvent, obj.superCluster()));
186  } else {
187  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromObj(iEvent, obj, nrObjs, objNr));
188  }
189 }
190 
193 
195 
CalibratedElectronProducerT(const edm::ParameterSet &)
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
static edm::ParameterSetDescription makePSetDescription()
static const std::string & name(size_t index)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
ElectronEnergyCalibrator energyCorrector_
ParameterSet const & getParameterSet(std::string const &) const
Definition: Electron.h:6
edm::EDGetTokenT< edm::View< T > > electronToken_
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEBToken_
int iEvent
Definition: GenABIO.cc:224
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static constexpr size_t kNrSysErrs
uint32_t getRandomSeedFromObj(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
void produce(edm::Event &, const edm::EventSetup &) override
void setSemiDetRandomSeed(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
std::unique_ptr< TRandom > semiDeterministicRng_
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEEToken_
uint32_t getRandomSeedFromSC(const edm::Event &iEvent, const reco::SuperClusterRef scRef)
HLT enums.
static const std::vector< int > valMapsToStore_
long double T
def move(src, dest)
Definition: eostools.py:511