CMS 3D CMS Logo

CalibratedPhotonProducers.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 photon
4 // it will write out all the calibration info in the event, such as scale correction value,
5 // smearing correction value, random nr used, energy post calibration, energy pre calibration
6 // can optionally write out a new collection of photon with the energy corrected by default
7 // a port of EgammaAnalysis/ElectronTools/CalibratedPhotonProducerRun2
8 
18 
25 
26 #include "TRandom2.h"
27 
28 #include <vector>
29 #include <random>
30 
31 template <typename T>
33 public:
36  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
37  void produce(edm::Event&, const edm::EventSetup&) override;
38 
39 private:
40  void setSemiDetRandomSeed(const edm::Event& iEvent, const T& obj, size_t nrObjs, size_t objNr);
41 
44  std::unique_ptr<TRandom> semiDeterministicRng_;
48 
49  static const std::vector<int> valMapsToStore_;
50 };
51 
52 template <typename T>
53 const std::vector<int> CalibratedPhotonProducerT<T>::valMapsToStore_ = {
61 
62 namespace {
63  template <typename HandleType, typename ValType>
64  void fillAndStoreValueMap(edm::Event& iEvent,
65  HandleType objHandle,
66  const std::vector<ValType>& vals,
67  const std::string& name) {
68  auto valMap = std::make_unique<edm::ValueMap<ValType>>();
69  typename edm::ValueMap<ValType>::Filler filler(*valMap);
70  filler.insert(objHandle, vals.begin(), vals.end());
71  filler.fill();
72  iEvent.put(std::move(valMap), name);
73  }
74 } // namespace
75 
76 template <typename T>
78  : photonToken_(consumes<edm::View<T>>(conf.getParameter<edm::InputTag>("src"))),
79  energyCorrector_(conf.getParameter<std::string>("correctionFile")),
80  recHitCollectionEBToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEB"))),
81  recHitCollectionEEToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEE"))),
82  produceCalibratedObjs_(conf.getParameter<bool>("produceCalibratedObjs")) {
83  energyCorrector_.setMinEt(conf.getParameter<double>("minEtToCalibrate"));
84 
85  if (conf.getParameter<bool>("semiDeterministic")) {
86  semiDeterministicRng_.reset(new TRandom2());
88  }
89 
91  produces<std::vector<T>>();
92 
93  for (const auto& toStore : valMapsToStore_) {
94  produces<edm::ValueMap<float>>(EGEnergySysIndex::name(toStore));
95  }
96 }
97 
98 template <typename T>
101  desc.add<edm::InputTag>("src", edm::InputTag("gedGsfElectrons"));
102  desc.add<edm::InputTag>("recHitCollectionEB", edm::InputTag("reducedEcalRecHitsEB"));
103  desc.add<edm::InputTag>("recHitCollectionEE", edm::InputTag("reducedEcalRecHitsEE"));
104  desc.add<std::string>("correctionFile", std::string());
105  desc.add<double>("minEtToCalibrate", 5.0);
106  desc.add<bool>("produceCalibratedObjs", true);
107  desc.add<bool>("semiDeterministic", true);
108  std::vector<std::string> valMapsProduced;
109  for (auto varToStore : valMapsToStore_)
110  valMapsProduced.push_back(EGEnergySysIndex::name(varToStore));
111  desc.add<std::vector<std::string>>("valueMapsStored", valMapsProduced)
112  ->setComment(
113  "provides to python configs the list of valuemaps stored, can not be overriden in the python config");
114  descriptions.addWithDefaultLabel(desc);
115 }
116 
117 template <typename T>
119  edm::Handle<edm::View<T>> inHandle;
120  iEvent.getByToken(photonToken_, inHandle);
121 
122  edm::Handle<EcalRecHitCollection> recHitCollectionEBHandle;
123  edm::Handle<EcalRecHitCollection> recHitCollectionEEHandle;
124 
125  iEvent.getByToken(recHitCollectionEBToken_, recHitCollectionEBHandle);
126  iEvent.getByToken(recHitCollectionEEToken_, recHitCollectionEEHandle);
127 
128  std::unique_ptr<std::vector<T>> out = std::make_unique<std::vector<T>>();
129 
130  size_t nrObj = inHandle->size();
131  std::array<std::vector<float>, EGEnergySysIndex::kNrSysErrs> results;
132  for (auto& res : results)
133  res.reserve(nrObj);
134 
135  const PhotonEnergyCalibrator::EventType evtType =
137 
138  for (const auto& pho : *inHandle) {
139  out->emplace_back(pho);
140 
141  if (semiDeterministicRng_)
142  setSemiDetRandomSeed(iEvent, pho, nrObj, out->size());
143 
145  (pho.isEB()) ? recHitCollectionEBHandle.product() : recHitCollectionEEHandle.product();
146  std::array<float, EGEnergySysIndex::kNrSysErrs> uncertainties =
147  energyCorrector_.calibrate(out->back(), iEvent.id().run(), recHits, iEvent.streamID(), evtType);
148 
149  for (size_t index = 0; index < EGEnergySysIndex::kNrSysErrs; index++) {
150  results[index].push_back(uncertainties[index]);
151  }
152  }
153 
154  auto fillAndStore = [&](auto handle) {
155  for (const auto& mapToStore : valMapsToStore_) {
156  fillAndStoreValueMap(iEvent, handle, results[mapToStore], EGEnergySysIndex::name(mapToStore));
157  }
158  };
159 
160  if (produceCalibratedObjs_) {
161  fillAndStore(iEvent.put(std::move(out)));
162  } else {
163  fillAndStore(inHandle);
164  }
165 }
166 
167 //needs to be synced to CalibratedElectronProducers, want the same seed for a given SC
168 template <typename T>
170  const T& obj,
171  size_t nrObjs,
172  size_t objNr) {
173  if (obj.superCluster().isNonnull()) {
174  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromSC(iEvent, obj.superCluster()));
175  } else {
176  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromObj(iEvent, obj, nrObjs, objNr));
177  }
178 }
179 
182 
184 
ConfigurationDescriptions.h
PhotonEnergyCalibrator::setMinEt
void setMinEt(float val)
Definition: PhotonEnergyCalibrator.h:36
CalibratedPhotonProducerT::photonToken_
edm::EDGetTokenT< edm::View< T > > photonToken_
Definition: CalibratedPhotonProducers.cc:42
Handle.h
electrons_cff.bool
bool
Definition: electrons_cff.py:372
CalibratedPhotonProducerT::semiDeterministicRng_
std::unique_ptr< TRandom > semiDeterministicRng_
Definition: CalibratedPhotonProducers.cc:44
edm::ParameterSetDescription::add
ParameterDescriptionBase * add(U const &iLabel, T const &value)
Definition: ParameterSetDescription.h:95
EGEnergySysIndex.h
PhotonEnergyCalibrator::EventType
EventType
Definition: PhotonEnergyCalibrator.h:22
edm::Handle::product
T const * product() const
Definition: Handle.h:70
ESHandle.h
PhotonEnergyCalibrator
Definition: PhotonEnergyCalibrator.h:20
sistrip::View
View
Definition: ConstantsForView.h:26
patZpeak.handle
handle
Definition: patZpeak.py:23
edm::EDGetTokenT
Definition: EDGetToken.h:33
edm
HLT enums.
Definition: AlignableModifier.h:19
CalibratedPhotonProducerT::produce
void produce(edm::Event &, const edm::EventSetup &) override
Definition: CalibratedPhotonProducers.cc:118
Photon.h
edm::ParameterSetDescription
Definition: ParameterSetDescription.h:52
EGEnergySysIndex::kSmearRhoDown
Definition: EGEnergySysIndex.h:21
EDProducer.h
EGEnergySysIndex::kScaleGainUp
Definition: EGEnergySysIndex.h:18
edm::SortedCollection< EcalRecHit >
bookConverter.results
results
Definition: bookConverter.py:144
EGEnergySysIndex::kEcalErrPostCorr
Definition: EGEnergySysIndex.h:34
PhotonFwd.h
EGEnergySysIndex::kSmearRhoUp
Definition: EGEnergySysIndex.h:20
CalibratedPhotonProducerT::fillDescriptions
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
Definition: CalibratedPhotonProducers.cc:99
edm::Handle
Definition: AssociativeIterator.h:50
EGEnergySysIndex::kScaleValue
Definition: EGEnergySysIndex.h:28
PhotonEnergyCalibrator::initPrivateRng
void initPrivateRng(TRandom *rnd)
Definition: PhotonEnergyCalibrator.cc:15
EGEnergySysIndex::kScaleStatDown
Definition: EGEnergySysIndex.h:15
EGEnergySysIndex::kNrSysErrs
static constexpr size_t kNrSysErrs
Definition: EGEnergySysIndex.h:40
PhotonEnergyCalibrator.h
EGEnergySysIndex::kSmearNrSigma
Definition: EGEnergySysIndex.h:30
EGEnergySysIndex::kScaleGainDown
Definition: EGEnergySysIndex.h:19
MakerMacros.h
Photon.h
EGEnergySysIndex::kScaleUp
Definition: EGEnergySysIndex.h:24
CalibratedPhotonProducerT::CalibratedPhotonProducerT
CalibratedPhotonProducerT(const edm::ParameterSet &)
Definition: CalibratedPhotonProducers.cc:77
DEFINE_FWK_MODULE
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
egamma::getRandomSeedFromObj
uint32_t getRandomSeedFromObj(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
Definition: EgammaRandomSeeds.h:21
egamma::getRandomSeedFromSC
uint32_t getRandomSeedFromSC(const edm::Event &iEvent, const reco::SuperClusterRef scRef)
Definition: EgammaRandomSeeds.cc:5
PhotonEnergyCalibrator::EventType::MC
EGEnergySysIndex::kEcalErrPreCorr
Definition: EGEnergySysIndex.h:32
ParameterSetDescription.h
CalibratedPhotonProducerT::produceCalibratedObjs_
bool produceCalibratedObjs_
Definition: CalibratedPhotonProducers.cc:47
EDGetToken.h
CalibratedPhotonProducerT::energyCorrector_
PhotonEnergyCalibrator energyCorrector_
Definition: CalibratedPhotonProducers.cc:43
getGTfromDQMFile.obj
obj
Definition: getGTfromDQMFile.py:32
EGEnergySysIndex::kSmearDown
Definition: EGEnergySysIndex.h:27
EGEnergySysIndex::name
static const std::string & name(size_t index)
Definition: EGEnergySysIndex.h:42
edm::ConfigurationDescriptions
Definition: ConfigurationDescriptions.h:28
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
FastTrackerRecHitMaskProducer_cfi.recHits
recHits
Definition: FastTrackerRecHitMaskProducer_cfi.py:8
HLT_2018_cff.InputTag
InputTag
Definition: HLT_2018_cff.py:79016
edm::ParameterSet
Definition: ParameterSet.h:36
CalibratedPhotonProducerT::setSemiDetRandomSeed
void setSemiDetRandomSeed(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
Definition: CalibratedPhotonProducers.cc:169
Event.h
trigObjTnPSource_cfi.filler
filler
Definition: trigObjTnPSource_cfi.py:21
iEvent
int iEvent
Definition: GenABIO.cc:224
edm::stream::EDProducer
Definition: EDProducer.h:38
EGEnergySysIndex::kScaleDown
Definition: EGEnergySysIndex.h:25
edm::EventSetup
Definition: EventSetup.h:57
res
Definition: Electron.h:6
EGEnergySysIndex::kEcalPostCorr
Definition: EGEnergySysIndex.h:33
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
eostools.move
def move(src, dest)
Definition: eostools.py:511
std
Definition: JetResolutionObject.h:76
EGEnergySysIndex::kEcalPreCorr
Definition: EGEnergySysIndex.h:31
EGEnergySysIndex::kSmearValue
Definition: EGEnergySysIndex.h:29
CalibratedPhotonProducerT::recHitCollectionEEToken_
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEEToken_
Definition: CalibratedPhotonProducers.cc:46
T
long double T
Definition: Basic3DVectorLD.h:48
EGEnergySysIndex::kSmearUp
Definition: EGEnergySysIndex.h:26
EGEnergySysIndex::kSmearPhiDown
Definition: EGEnergySysIndex.h:23
PhotonEnergyCalibrator::EventType::DATA
EgammaRandomSeeds.h
EGEnergySysIndex::kScaleSystUp
Definition: EGEnergySysIndex.h:16
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
EventSetup.h
EGEnergySysIndex::kScaleSystDown
Definition: EGEnergySysIndex.h:17
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
EGEnergySysIndex::kScaleStatUp
Definition: EGEnergySysIndex.h:14
MillePedeFileConverter_cfg.out
out
Definition: MillePedeFileConverter_cfg.py:31
edm::helper::Filler
Definition: ValueMap.h:22
ParameterSet.h
edm::Event
Definition: Event.h:73
EGEnergySysIndex::kSmearPhiUp
Definition: EGEnergySysIndex.h:22
CalibratedPhotonProducerT::valMapsToStore_
static const std::vector< int > valMapsToStore_
Definition: CalibratedPhotonProducers.cc:49
CalibratedPhotonProducerT
Definition: CalibratedPhotonProducers.cc:32
edm::InputTag
Definition: InputTag.h:15
CalibratedPhotonProducerT::~CalibratedPhotonProducerT
~CalibratedPhotonProducerT() override
Definition: CalibratedPhotonProducers.cc:35
edm::ConfigurationDescriptions::addWithDefaultLabel
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
Definition: ConfigurationDescriptions.cc:87
CalibratedPhotonProducerT::recHitCollectionEBToken_
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEBToken_
Definition: CalibratedPhotonProducers.cc:45