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:
34  explicit CalibratedPhotonProducerT( const edm::ParameterSet & ) ;
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 
53 template<typename T>
54 const std::vector<int> CalibratedPhotonProducerT<T>::valMapsToStore_ = {
76 };
77 
78 namespace{
79  template<typename HandleType,typename ValType>
80  void fillAndStoreValueMap(edm::Event& iEvent,HandleType objHandle,
81  const std::vector<ValType>& vals,const std::string& name)
82  {
83  auto valMap = std::make_unique<edm::ValueMap<ValType> >();
84  typename edm::ValueMap<ValType>::Filler filler(*valMap);
85  filler.insert(objHandle,vals.begin(),vals.end());
86  filler.fill();
87  iEvent.put(std::move(valMap),name);
88  }
89 }
90 
91 template<typename T>
93  photonToken_(consumes<edm::View<T> >(conf.getParameter<edm::InputTag>("src"))),
94  energyCorrector_(conf.getParameter<std::string >("correctionFile")),
95  recHitCollectionEBToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEB"))),
96  recHitCollectionEEToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEE"))),
97  produceCalibratedObjs_(conf.getParameter<bool>("produceCalibratedObjs"))
98 {
99 
100  energyCorrector_.setMinEt(conf.getParameter<double>("minEtToCalibrate"));
101 
102  if (conf.getParameter<bool>("semiDeterministic")) {
103  semiDeterministicRng_.reset(new TRandom2());
105  }
106 
107  if(produceCalibratedObjs_) produces<std::vector<T>>();
108 
109  for(const auto& toStore : valMapsToStore_){
110  produces<edm::ValueMap<float>>(EGEnergySysIndex::name(toStore));
111  }
112 }
113 
114 template<typename T>
116 {
118  desc.add<edm::InputTag>("src",edm::InputTag("gedGsfElectrons"));
119  desc.add<edm::InputTag>("recHitCollectionEB",edm::InputTag("reducedEcalRecHitsEB"));
120  desc.add<edm::InputTag>("recHitCollectionEE",edm::InputTag("reducedEcalRecHitsEE"));
121  desc.add<std::string>("correctionFile",std::string());
122  desc.add<double>("minEtToCalibrate",5.0);
123  desc.add<bool>("produceCalibratedObjs",true);
124  desc.add<bool>("semiDeterministic",true);
125  std::vector<std::string> valMapsProduced;
126  for(auto varToStore : valMapsToStore_) valMapsProduced.push_back(EGEnergySysIndex::name(varToStore));
127  desc.add<std::vector<std::string> >("valueMapsStored",valMapsProduced)->setComment("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>
132 void
134 
135  edm::Handle<edm::View<T>> inHandle;
136  iEvent.getByToken(photonToken_, inHandle);
137 
138  edm::Handle<EcalRecHitCollection> recHitCollectionEBHandle;
139  edm::Handle<EcalRecHitCollection> recHitCollectionEEHandle;
140 
141  iEvent.getByToken(recHitCollectionEBToken_, recHitCollectionEBHandle);
142  iEvent.getByToken(recHitCollectionEEToken_, recHitCollectionEEHandle);
143 
144  std::unique_ptr<std::vector<T>> out = std::make_unique<std::vector<T>>();
145 
146  size_t nrObj = inHandle->size();
147  std::array<std::vector<float>,EGEnergySysIndex::kNrSysErrs> results;
148  for(auto& res : results) res.reserve(nrObj);
149 
150  const PhotonEnergyCalibrator::EventType evtType = iEvent.isRealData() ?
152 
153  for (const auto& pho : *inHandle) {
154  out->emplace_back(pho);
155 
156  if(semiDeterministicRng_) setSemiDetRandomSeed(iEvent,pho,nrObj,out->size());
157 
158  const EcalRecHitCollection* recHits = (pho.isEB()) ? recHitCollectionEBHandle.product() : recHitCollectionEEHandle.product();
159  std::array<float,EGEnergySysIndex::kNrSysErrs> uncertainties = energyCorrector_.calibrate(out->back(), iEvent.id().run(), recHits, iEvent.streamID(), evtType);
160 
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 
173  fillAndStore(iEvent.put(std::move(out)));
174  }else{
175  fillAndStore(inHandle);
176  }
177 
178 }
179 
180 //needs to be synced to CalibratedElectronProducers, want the same seed for a given SC
181 template<typename T>
182 void CalibratedPhotonProducerT<T>::setSemiDetRandomSeed(const edm::Event& iEvent,const T& obj,size_t nrObjs,size_t objNr)
183 {
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 
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
void addWithDefaultLabel(ParameterSetDescription const &psetDescription)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
size_t size() const
Definition: Event.cc:278
std::unique_ptr< TRandom > semiDeterministicRng_
void initPrivateRng(TRandom *rnd)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
static const std::string & name(size_t index)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
void produce(edm::Event &, const edm::EventSetup &) override
bool isRealData() const
Definition: EventBase.h:64
Definition: Electron.h:6
int iEvent
Definition: GenABIO.cc:230
PhotonEnergyCalibrator energyCorrector_
void setSemiDetRandomSeed(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
CalibratedPhotonProducerT(const edm::ParameterSet &)
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEEToken_
static constexpr size_t kNrSysErrs
uint32_t getRandomSeedFromObj(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEBToken_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
T const * product() const
Definition: Handle.h:81
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
edm::EDGetTokenT< edm::View< T > > photonToken_
edm::EventID id() const
Definition: EventBase.h:60
uint32_t getRandomSeedFromSC(const edm::Event &iEvent, const reco::SuperClusterRef scRef)
HLT enums.
StreamID streamID() const
Definition: Event.h:95
static const std::vector< int > valMapsToStore_
long double T
def move(src, dest)
Definition: eostools.py:510
std::array< float, EGEnergySysIndex::kNrSysErrs > calibrate(reco::Photon &photon, const unsigned int runNumber, const EcalRecHitCollection *recHits, edm::StreamID const &id, const EventType eventType) const