CMS 3D CMS Logo

ElectronHEEPIDValueMapProducer.cc
Go to the documentation of this file.
3 
7 
9 
13 
19 
22 
25 
26 #include <memory>
27 #include <vector>
28 
29 //Heavily inspired from ElectronIDValueMapProducer
30 
31 
33 private:
34  //helper classes to handle AOD vs MiniAOD
35  template<typename T>
36  struct DualToken {
39  };
40  class DataFormat {
41  public:
42  enum Format{AUTO=0,AOD=1,MINIAOD=2};
43  private:
44  int data_;
45  public:
46  DataFormat(int val):data_(val){}
47  bool tryAOD()const{return data_==AUTO || data_==AOD;}
48  bool tryMiniAOD()const{return data_==AUTO || data_==MINIAOD;}
49  int operator()()const{return data_;}
50  };
51 public:
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 private:
58  virtual void produce(edm::Event&, const edm::EventSetup&) override;
59 
60  template<typename T>
61  static void writeValueMap(edm::Event &iEvent,
63  const std::vector<T> & values,
64  const std::string& label);
65 
66  static int nrSaturatedCrysIn5x5(const reco::GsfElectron& ele,
69  edm::ESHandle<CaloTopology>& caloTopo);
70 
71  float calTrkIso(const reco::GsfElectron& ele,
72  const edm::View<reco::GsfElectron>& eles,
73  const std::vector<edm::Handle<pat::PackedCandidateCollection> >& handles);
74 
75  template <typename T> void setToken(edm::EDGetTokenT<T>& token,edm::InputTag tag){token=consumes<T>(tag);}
76  template <typename T> void setToken(edm::EDGetTokenT<T>& token,const edm::ParameterSet& iPara,const std::string& tag){token=consumes<T>(iPara.getParameter<edm::InputTag>(tag));}
77  template <typename T> void setToken(std::vector<edm::EDGetTokenT<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagName){
78  auto tags =iPara.getParameter<std::vector<edm::InputTag> >(tagName);
79  for(auto& tag : tags) {
80  edm::EDGetTokenT<T> token;
81  setToken(token,tag);
82  tokens.push_back(token);
83  }
84  }
85  template <typename T> void setToken(DualToken<T>& token,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
86  if(format.tryAOD()) token.aod=consumes<T>(iPara.getParameter<edm::InputTag>(tagAOD));
87  if(format.tryMiniAOD()) token.miniAOD=consumes<T>(iPara.getParameter<edm::InputTag>(tagMiniAOD));
88  }
89  template <typename T> void setToken(std::vector<DualToken<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
90  auto tagsAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagAOD);
91  auto tagsMiniAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagMiniAOD);
92  size_t maxSize = std::max(tagsAOD.size(),tagsMiniAOD.size());
93  tokens.clear();
94  tokens.resize(maxSize);
95  if(format.tryAOD()){
96  for(size_t tagNr=0;tagNr<tagsAOD.size();tagNr++) {
97  setToken(tokens[tagNr].aod,tagsAOD[tagNr]);
98  }
99  }
100  if(format.tryMiniAOD()){
101  for(size_t tagNr=0;tagNr<tagsMiniAOD.size();tagNr++) {
102  setToken(tokens[tagNr].miniAOD,tagsMiniAOD[tagNr]);
103  }
104  }
105  }
106 
107  template<typename T> edm::Handle<T> getHandle(const edm::Event& iEvent,const edm::EDGetTokenT<T>& token){
109  iEvent.getByToken(token,handle);
110  return handle;
111  }
112  template<typename T> edm::Handle<T> getHandle(const edm::Event& iEvent,const DualToken<T>& token){
114  if(!token.aod.isUninitialized()) iEvent.getByToken(token.aod,handle);
115  if(!handle.isValid() && !token.miniAOD.isUninitialized()) iEvent.getByToken(token.miniAOD,handle);
116  return handle;
117  }
118 
119  template<typename T> std::vector<edm::Handle<T> > getHandles(const edm::Event& iEvent,const std::vector<DualToken<T> >& tokens){
120  std::vector<edm::Handle<T> > handles(tokens.size());
121  if(tokens.empty()) return handles;
122  if(!tokens[0].aod.isUninitialized()) iEvent.getByToken(tokens[0].aod,handles[0]);
123  bool isAOD = handles[0].isValid();
124  if(!isAOD && !tokens[0].miniAOD.isUninitialized() ) iEvent.getByToken(tokens[0].miniAOD,handles[0]);
125 
126  for(size_t tokenNr=1;tokenNr<tokens.size();tokenNr++){
127  auto token = isAOD ? tokens[tokenNr].aod : tokens[tokenNr].miniAOD;
128  if(!token.isUninitialized()) iEvent.getByToken(token,handles[tokenNr]);
129  }
130  return handles;
131  }
132 
133 
134 
138  std::vector<DualToken<pat::PackedCandidateCollection> >candTokens_;
140 
143 
146 };
147 
150 
151 
152 
154  trkIsoCalc_(iConfig.getParameter<edm::ParameterSet>("trkIsoConfig")),
155  dataFormat_(iConfig.getParameter<int>("dataFormat"))
156 {
157  setToken(ebRecHitToken_,iConfig,"ebRecHitsAOD","ebRecHitsMiniAOD",dataFormat_);
158  setToken(eeRecHitToken_,iConfig,"eeRecHitsAOD","eeRecHitsMiniAOD",dataFormat_);
159  setToken(eleToken_,iConfig,"elesAOD","elesMiniAOD",dataFormat_);
160  setToken(candTokens_,iConfig,"candsAOD","candsMiniAOD",dataFormat_);
161  setToken(beamSpotToken_,iConfig,"beamSpot");
162 
163  produces<edm::ValueMap<float> >(eleTrkPtIsoLabel_);
164  produces<edm::ValueMap<int> >(eleNrSaturateIn5x5Label_);
165 }
166 
168 {
169 
170 }
171 
173 {
174  auto eleHandle = getHandle(iEvent,eleToken_);
175  auto ebRecHitHandle = getHandle(iEvent,ebRecHitToken_);
176  auto eeRecHitHandle = getHandle(iEvent,eeRecHitToken_);
177  auto beamSpotHandle = getHandle(iEvent,beamSpotToken_);
178  auto candHandles = getHandles(iEvent,candTokens_);
179 
180 
181  edm::ESHandle<CaloTopology> caloTopoHandle;
182  iSetup.get<CaloTopologyRecord>().get(caloTopoHandle);
183 
184  std::vector<float> eleTrkPtIso;
185  std::vector<int> eleNrSaturateIn5x5;
186  for(size_t eleNr=0;eleNr<eleHandle->size();eleNr++){
187  auto elePtr = eleHandle->ptrAt(eleNr);
188  eleTrkPtIso.push_back(calTrkIso(*elePtr,*eleHandle,candHandles));
189  eleNrSaturateIn5x5.push_back(nrSaturatedCrysIn5x5(*elePtr,ebRecHitHandle,eeRecHitHandle,caloTopoHandle));
190  }
191 
192  writeValueMap(iEvent,eleHandle,eleTrkPtIso,eleTrkPtIsoLabel_);
193  writeValueMap(iEvent,eleHandle,eleNrSaturateIn5x5,eleNrSaturateIn5x5Label_);
194 }
195 
199  edm::ESHandle<CaloTopology>& caloTopo)
200 {
201  DetId id = ele.superCluster()->seed()->seed();
202  auto recHits = id.subdetId()==EcalBarrel ? ebHits.product() : eeHits.product();
203  return noZS::EcalClusterTools::nrSaturatedCrysIn5x5(id,recHits,caloTopo.product());
204 
205 }
206 
208  const edm::View<reco::GsfElectron>& eles,
209  const std::vector<edm::Handle<pat::PackedCandidateCollection> >& handles)
210 {
211  if(ele.gsfTrack().isNull()) return std::numeric_limits<float>::max();
212  else{
213  float trkIso=0.;
214  for(auto& handle: handles){
215  if(handle.isValid()){
216  trkIso+= trkIsoCalc_.calIsolPt(*ele.gsfTrack(),*handle,eles);
217  }
218  }
219  return trkIso;
220  }
221 }
222 
223 template<typename T>
226  const std::vector<T> & values,
227  const std::string& label)
228 {
229  std::unique_ptr<edm::ValueMap<T> > valMap(new edm::ValueMap<T>());
230  typename edm::ValueMap<T>::Filler filler(*valMap);
231  filler.insert(handle, values.begin(), values.end());
232  filler.fill();
233  iEvent.put(std::move(valMap),label);
234 }
235 
237 
239  desc.add<edm::InputTag>("beamSpot",edm::InputTag("offlineBeamSpot"));
240  desc.add<edm::InputTag>("ebRecHitsAOD",edm::InputTag("reducedEcalRecHitsEB"));
241  desc.add<edm::InputTag>("eeRecHitsAOD",edm::InputTag("reducedEcalRecHitsEE"));
242  desc.add<std::vector<edm::InputTag> >("candsAOD",{edm::InputTag("packedCandidates")});
243  desc.add<edm::InputTag>("elesAOD",edm::InputTag("gedGsfElectrons"));
244 
245  desc.add<edm::InputTag>("ebRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEB"));
246  desc.add<edm::InputTag>("eeRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEE"));
247  desc.add<std::vector<edm::InputTag> >("candsMiniAOD",{edm::InputTag("packedCandidates")});
248  desc.add<edm::InputTag>("elesMiniAOD",edm::InputTag("gedGsfElectrons"));
249  desc.add<int>("dataFormat",0);
250 
251  desc.add("trkIsoConfig",EleTkIsolFromCands::pSetDescript());
252 
253  descriptions.addDefault(desc);
254 }
255 
static int nrSaturatedCrysIn5x5(const DetId &id, const EcalRecHitCollection *recHits, const CaloTopology *topology)
T getParameter(std::string const &) const
DualToken< EcalRecHitCollection > ebRecHitToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
static void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< reco::GsfElectron > > &handle, const std::vector< T > &values, const std::string &label)
virtual void produce(edm::Event &, const edm::EventSetup &) override
static int nrSaturatedCrysIn5x5(const reco::GsfElectron &ele, edm::Handle< EcalRecHitCollection > &ebHits, edm::Handle< EcalRecHitCollection > &eeHits, edm::ESHandle< CaloTopology > &caloTopo)
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:52
std::vector< DualToken< pat::PackedCandidateCollection > > candTokens_
double calIsolPt(const reco::TrackBase &trk, const pat::PackedCandidateCollection &cands, const edm::View< reco::GsfElectron > &eles)
std::vector< edm::Handle< T > > getHandles(const edm::Event &iEvent, const std::vector< DualToken< T > > &tokens)
void setToken(edm::EDGetTokenT< T > &token, const edm::ParameterSet &iPara, const std::string &tag)
int iEvent
Definition: GenABIO.cc:230
void addDefault(ParameterSetDescription const &psetDescription)
virtual SuperClusterRef superCluster() const
reference to a SuperCluster
Definition: GsfElectron.h:184
DualToken< edm::View< reco::GsfElectron > > eleToken_
void setToken(DualToken< T > &token, const edm::ParameterSet &iPara, const std::string &tagAOD, const std::string &tagMiniAOD, DataFormat format)
static edm::ParameterSetDescription pSetDescript()
DualToken< EcalRecHitCollection > eeRecHitToken_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:75
bool isNull() const
Checks for null.
Definition: Ref.h:249
Definition: DetId.h:18
void setToken(std::vector< DualToken< T > > &tokens, const edm::ParameterSet &iPara, const std::string &tagAOD, const std::string &tagMiniAOD, DataFormat format)
T const * product() const
Definition: Handle.h:81
float calTrkIso(const reco::GsfElectron &ele, const edm::View< reco::GsfElectron > &eles, const std::vector< edm::Handle< pat::PackedCandidateCollection > > &handles)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const T & get() const
Definition: EventSetup.h:56
void setToken(std::vector< edm::EDGetTokenT< T > > &tokens, const edm::ParameterSet &iPara, const std::string &tagName)
ElectronHEEPIDValueMapProducer(const edm::ParameterSet &)
HLT enums.
edm::Handle< T > getHandle(const edm::Event &iEvent, const edm::EDGetTokenT< T > &token)
edm::Handle< T > getHandle(const edm::Event &iEvent, const DualToken< T > &token)
void setToken(edm::EDGetTokenT< T > &token, edm::InputTag tag)
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510
virtual GsfTrackRef gsfTrack() const
reference to a GsfTrack
Definition: GsfElectron.h:185