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 
116  if (conf.getParameter<bool>("semiDeterministic")) {
117  semiDeterministicRng_.reset(new TRandom2());
119  }
120 
121  if(produceCalibratedObjs_) produces<std::vector<T>>();
122 
123  for(const auto& toStore : valMapsToStore_){
124  produces<edm::ValueMap<float>>(EGEnergySysIndex::name(toStore));
125  }
126 }
127 
128 template<typename T>
130 {
132  desc.add<edm::InputTag>("src",edm::InputTag("gedPhotons"));
134  desc.add<edm::InputTag>("recHitCollectionEB",edm::InputTag("reducedEcalRecHitsEB"));
135  desc.add<edm::InputTag>("recHitCollectionEE",edm::InputTag("reducedEcalRecHitsEE"));
136  desc.add<std::string>("correctionFile",std::string());
137  desc.add<double>("minEtToCalibrate",5.0);
138  desc.add<bool>("produceCalibratedObjs",true);
139  desc.add<bool>("semiDeterministic",true);
140  std::vector<std::string> valMapsProduced;
141  for(auto varToStore : valMapsToStore_) valMapsProduced.push_back(EGEnergySysIndex::name(varToStore));
142  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");
144 }
145 
146 template<typename T>
147 void
149 {
150 
152 
153  edm::Handle<edm::View<T>> inHandle;
154  iEvent.getByToken(electronToken_, inHandle);
155 
156  edm::Handle<EcalRecHitCollection> recHitCollectionEBHandle;
157  edm::Handle<EcalRecHitCollection> recHitCollectionEEHandle;
158 
159  iEvent.getByToken(recHitCollectionEBToken_, recHitCollectionEBHandle);
160  iEvent.getByToken(recHitCollectionEEToken_, recHitCollectionEEHandle);
161 
162  std::unique_ptr<std::vector<T>> out = std::make_unique<std::vector<T>>();
163 
164  size_t nrObj = inHandle->size();
165  std::array<std::vector<float>,EGEnergySysIndex::kNrSysErrs> results;
166  for(auto& res : results) res.reserve(nrObj);
167 
168  const ElectronEnergyCalibrator::EventType evtType = iEvent.isRealData() ?
170 
171 
172  for (const auto& ele : *inHandle) {
173  out->push_back(ele);
174 
175  if(semiDeterministicRng_) setSemiDetRandomSeed(iEvent,ele,nrObj,out->size());
176 
177  const EcalRecHitCollection* recHits = (ele.isEB()) ? recHitCollectionEBHandle.product() : recHitCollectionEEHandle.product();
178  std::array<float,EGEnergySysIndex::kNrSysErrs> uncertainties = energyCorrector_.calibrate(out->back(), iEvent.id().run(), recHits, iEvent.streamID(), evtType);
179 
181  results[index].push_back(uncertainties[index]);
182  }
183  }
184 
185  auto fillAndStore = [&](auto handle){
186  for(const auto& mapToStore : valMapsToStore_){
187  fillAndStoreValueMap(iEvent,handle,results[mapToStore],EGEnergySysIndex::name(mapToStore));
188  }
189  };
190 
192  fillAndStore(iEvent.put(std::move(out)));
193  }else{
194  fillAndStore(inHandle);
195  }
196 
197 }
198 
199 template<typename T>
200 void CalibratedElectronProducerT<T>::setSemiDetRandomSeed(const edm::Event& iEvent,const T& obj,size_t nrObjs,size_t objNr)
201 {
202  if(obj.superCluster().isNonnull()){
203  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromSC(iEvent,obj.superCluster()));
204  }else{
205  semiDeterministicRng_->SetSeed(egamma::getRandomSeedFromObj(iEvent,obj,nrObjs,objNr));
206  }
207 }
208 
211 
213 
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
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