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 
19 
24 
31 
33 
34 #include "TRandom2.h"
35 
36 #include <vector>
37 
38 
39 template<typename T>
41 {
42 public:
45  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
46  void produce( edm::Event &, const edm::EventSetup & ) override ;
47 
48 private:
49  void setSemiDetRandomSeed(const edm::Event& iEvent,const T& obj,size_t nrObjs,size_t objNr);
50 
52 
55  std::unique_ptr<TRandom> semiDeterministicRng_;
59  static const std::vector<int> valMapsToStore_;
60 
61 };
62 
63 template<typename T>
64 const std::vector<int> CalibratedElectronProducerT<T>::valMapsToStore_ = {
90 };
91 
92 namespace{
93  template<typename HandleType,typename ValType>
94  void fillAndStoreValueMap(edm::Event& iEvent,HandleType objHandle,
95  const std::vector<ValType>& vals,const std::string& name)
96  {
97  auto valMap = std::make_unique<edm::ValueMap<ValType> >();
98  typename edm::ValueMap<ValType>::Filler filler(*valMap);
99  filler.insert(objHandle,vals.begin(),vals.end());
100  filler.fill();
101  iEvent.put(std::move(valMap),name);
102  }
103 }
104 
105 template<typename T>
107  electronToken_(consumes<edm::View<T>>(conf.getParameter<edm::InputTag>("src"))),
108  epCombinationTool_(conf.getParameter<edm::ParameterSet>("epCombConfig")),
109  energyCorrector_(epCombinationTool_, conf.getParameter<std::string>("correctionFile")),
110  recHitCollectionEBToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEB"))),
111  recHitCollectionEEToken_(consumes<EcalRecHitCollection>(conf.getParameter<edm::InputTag>("recHitCollectionEE"))),
112  produceCalibratedObjs_(conf.getParameter<bool>("produceCalibratedObjs"))
113 {
114  energyCorrector_.setMinEt(conf.getParameter<double>("minEtToCalibrate"));
115  energyCorrector_.setUseSmearCorrEcalEnergyErrInComb(conf.getParameter<bool>("useSmearCorrEcalEnergyErrInComb"));
116 
117  if (conf.getParameter<bool>("semiDeterministic")) {
118  semiDeterministicRng_.reset(new TRandom2());
120  }
121 
122  if(produceCalibratedObjs_) produces<std::vector<T>>();
123 
124  for(const auto& toStore : valMapsToStore_){
125  produces<edm::ValueMap<float>>(EGEnergySysIndex::name(toStore));
126  }
127 }
128 
129 template<typename T>
131 {
133  desc.add<edm::InputTag>("src",edm::InputTag("gedPhotons"));
135  desc.add<edm::InputTag>("recHitCollectionEB",edm::InputTag("reducedEcalRecHitsEB"));
136  desc.add<edm::InputTag>("recHitCollectionEE",edm::InputTag("reducedEcalRecHitsEE"));
137  desc.add<std::string>("correctionFile",std::string());
138  desc.add<double>("minEtToCalibrate",5.0);
139  desc.add<bool>("produceCalibratedObjs",true);
140  desc.add<bool>("semiDeterministic",true);
141  desc.add<bool>("useSmearCorrEcalEnergyErrInComb",false);
142  std::vector<std::string> valMapsProduced;
143  for(auto varToStore : valMapsToStore_) valMapsProduced.push_back(EGEnergySysIndex::name(varToStore));
144  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");
146 }
147 
148 template<typename T>
149 void
151 {
152 
154 
155  edm::Handle<edm::View<T>> inHandle;
156  iEvent.getByToken(electronToken_, inHandle);
157 
158  edm::Handle<EcalRecHitCollection> recHitCollectionEBHandle;
159  edm::Handle<EcalRecHitCollection> recHitCollectionEEHandle;
160 
161  iEvent.getByToken(recHitCollectionEBToken_, recHitCollectionEBHandle);
162  iEvent.getByToken(recHitCollectionEEToken_, recHitCollectionEEHandle);
163 
164  std::unique_ptr<std::vector<T>> out = std::make_unique<std::vector<T>>();
165 
166  size_t nrObj = inHandle->size();
167  std::array<std::vector<float>,EGEnergySysIndex::kNrSysErrs> results;
168  for(auto& res : results) res.reserve(nrObj);
169 
170  const ElectronEnergyCalibrator::EventType evtType = iEvent.isRealData() ?
172 
173 
174  for (const auto& ele : *inHandle) {
175  out->push_back(ele);
176 
177  if(semiDeterministicRng_) setSemiDetRandomSeed(iEvent,ele,nrObj,out->size());
178 
179  const EcalRecHitCollection* recHits = (ele.isEB()) ? recHitCollectionEBHandle.product() : recHitCollectionEEHandle.product();
180  std::array<float,EGEnergySysIndex::kNrSysErrs> uncertainties = energyCorrector_.calibrate(out->back(), iEvent.id().run(), recHits, iEvent.streamID(), evtType);
181 
183  results[index].push_back(uncertainties[index]);
184  }
185  }
186 
187  auto fillAndStore = [&](auto handle){
188  for(const auto& mapToStore : valMapsToStore_){
189  fillAndStoreValueMap(iEvent,handle,results[mapToStore],EGEnergySysIndex::name(mapToStore));
190  }
191  };
192 
194  fillAndStore(iEvent.put(std::move(out)));
195  }else{
196  fillAndStore(inHandle);
197  }
198 
199 }
200 
201 template<typename T>
202 void CalibratedElectronProducerT<T>::setSemiDetRandomSeed(const edm::Event& iEvent,const T& obj,size_t nrObjs,size_t objNr)
203 {
204  if(obj.superCluster().isNonnull()){
205  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromSC(iEvent,obj.superCluster()));
206  }else{
207  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromObj(iEvent,obj,nrObjs,objNr));
208  }
209 }
210 
213 
215 
RunNumber_t run() const
Definition: EventID.h:39
T getParameter(std::string const &) const
std::string defaultModuleLabel()
CalibratedElectronProducerT(const edm::ParameterSet &)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
size_t size() const
Definition: Event.cc:249
void setUseSmearCorrEcalEnergyErrInComb(bool val)
static edm::ParameterSetDescription makePSetDescription()
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
static const std::string & name(size_t index)
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
ElectronEnergyCalibrator energyCorrector_
void calibrate(SimpleElectron &electron, edm::StreamID const &)
bool isRealData() const
Definition: EventBase.h:64
Definition: Electron.h:4
edm::EDGetTokenT< edm::View< T > > electronToken_
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEBToken_
int iEvent
Definition: GenABIO.cc:230
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
ParameterDescriptionBase * add(U const &iLabel, T const &value)
void setSemiDetRandomSeed(const edm::Event &iEvent, const T &obj, size_t nrObjs, size_t objNr)
void setEventContent(const edm::EventSetup &iSetup)
T const * product() const
Definition: Handle.h:81
void add(std::string const &label, ParameterSetDescription const &psetDescription)
std::unique_ptr< TRandom > semiDeterministicRng_
edm::EDGetTokenT< EcalRecHitCollection > recHitCollectionEEToken_
edm::EventID id() const
Definition: EventBase.h:60
uint32_t getRandomSeedFromSC(const edm::Event &iEvent, const reco::SuperClusterRef scRef)
HLT enums.
static const std::vector< int > valMapsToStore_
StreamID streamID() const
Definition: Event.h:86
long double T
def move(src, dest)
Definition: eostools.py:510