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  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  const std::vector<EleTkIsolFromCands::PIDVeto>& pidVetos)const;
75 
76  template <typename T> void setToken(edm::EDGetTokenT<T>& token,edm::InputTag tag){token=consumes<T>(tag);}
77  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));}
78  template <typename T> void setToken(std::vector<edm::EDGetTokenT<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagName){
79  auto tags =iPara.getParameter<std::vector<edm::InputTag> >(tagName);
80  for(auto& tag : tags) {
81  edm::EDGetTokenT<T> token;
82  setToken(token,tag);
83  tokens.push_back(token);
84  }
85  }
86  template <typename T> void setToken(DualToken<T>& token,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
87  if(format.tryAOD()) token.aod=consumes<T>(iPara.getParameter<edm::InputTag>(tagAOD));
88  if(format.tryMiniAOD()) token.miniAOD=consumes<T>(iPara.getParameter<edm::InputTag>(tagMiniAOD));
89  }
90  template <typename T> void setToken(std::vector<DualToken<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
91  auto tagsAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagAOD);
92  auto tagsMiniAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagMiniAOD);
93  size_t maxSize = std::max(tagsAOD.size(),tagsMiniAOD.size());
94  tokens.clear();
95  tokens.resize(maxSize);
96  if(format.tryAOD()){
97  for(size_t tagNr=0;tagNr<tagsAOD.size();tagNr++) {
98  setToken(tokens[tagNr].aod,tagsAOD[tagNr]);
99  }
100  }
101  if(format.tryMiniAOD()){
102  for(size_t tagNr=0;tagNr<tagsMiniAOD.size();tagNr++) {
103  setToken(tokens[tagNr].miniAOD,tagsMiniAOD[tagNr]);
104  }
105  }
106  }
107 
108  template<typename T>
109  static edm::Handle<T> getHandle(const edm::Event& iEvent,
110  const edm::EDGetTokenT<T>& token){
112  iEvent.getByToken(token,handle);
113  return handle;
114  }
115  template<typename T>
116  static edm::Handle<T> getHandle(const edm::Event& iEvent,
117  const DualToken<T>& token){
119  if(!token.aod.isUninitialized()) iEvent.getByToken(token.aod,handle);
120  if(!handle.isValid() && !token.miniAOD.isUninitialized()) iEvent.getByToken(token.miniAOD,handle);
121  return handle;
122  }
123 
124  template<typename T>
125  static std::vector<edm::Handle<T> >
126  getHandles(const edm::Event& iEvent,const std::vector<DualToken<T> >& tokens){
127  std::vector<edm::Handle<T> > handles(tokens.size());
128  if(tokens.empty()) return handles;
129  if(!tokens[0].aod.isUninitialized()) iEvent.getByToken(tokens[0].aod,handles[0]);
130  bool isAOD = handles[0].isValid();
131  if(!isAOD && !tokens[0].miniAOD.isUninitialized() ) iEvent.getByToken(tokens[0].miniAOD,handles[0]);
132 
133  for(size_t tokenNr=1;tokenNr<tokens.size();tokenNr++){
134  auto token = isAOD ? tokens[tokenNr].aod : tokens[tokenNr].miniAOD;
135  if(!token.isUninitialized()) iEvent.getByToken(token,handles[tokenNr]);
136  }
137  return handles;
138  }
139 
140  template<typename T>
141  static bool isEventAOD(const edm::Event& iEvent,const DualToken<T>& token){
143  if(!token.aod.isUninitialized()) iEvent.getByToken(token.aod,handle);
144  if(handle.isValid()) return true;
145  else return false;
146  }
147 
148 
149 
153  std::vector<DualToken<pat::PackedCandidateCollection> >candTokens_;
155 
158  std::vector<EleTkIsolFromCands::PIDVeto> candVetosAOD_;
159  std::vector<EleTkIsolFromCands::PIDVeto> candVetosMiniAOD_;
160 
163 };
164 
167 
168 
169 
171  trkIsoCalc_(iConfig.getParameter<edm::ParameterSet>("trkIsoConfig")),
172  dataFormat_(iConfig.getParameter<int>("dataFormat"))
173 {
174  setToken(ebRecHitToken_,iConfig,"ebRecHitsAOD","ebRecHitsMiniAOD",dataFormat_);
175  setToken(eeRecHitToken_,iConfig,"eeRecHitsAOD","eeRecHitsMiniAOD",dataFormat_);
176  setToken(eleToken_,iConfig,"elesAOD","elesMiniAOD",dataFormat_);
177  setToken(candTokens_,iConfig,"candsAOD","candsMiniAOD",dataFormat_);
178  setToken(beamSpotToken_,iConfig,"beamSpot");
179 
180  auto fillVetos=[](const auto& in,auto& out){
181  std::transform(in.begin(),in.end(),std::back_inserter(out),EleTkIsolFromCands::pidVetoFromStr);
182  };
183 
184  fillVetos(iConfig.getParameter<std::vector<std::string> >("candVetosAOD"),candVetosAOD_);
185  if(candVetosAOD_.size()!=iConfig.getParameter<std::vector<edm::InputTag> >("candsAOD").size()){
186  throw cms::Exception("ConfigError") <<" Error candVetosAOD should be the same size as candsAOD "<<std::endl;
187  }
188 
189  fillVetos(iConfig.getParameter<std::vector<std::string> >("candVetosMiniAOD"),candVetosMiniAOD_);
190  if(candVetosMiniAOD_.size()!=iConfig.getParameter<std::vector<edm::InputTag> >("candsMiniAOD").size()){
191  throw cms::Exception("ConfigError") <<" Error candVetosMiniAOD should be the same size as candsMiniAOD "<<std::endl;
192  }
193 
194  produces<edm::ValueMap<float> >(eleTrkPtIsoLabel_);
195  produces<edm::ValueMap<int> >(eleNrSaturateIn5x5Label_);
196 }
197 
199 {
200 
201 }
202 
204 {
205  auto eleHandle = getHandle(iEvent,eleToken_);
206  auto ebRecHitHandle = getHandle(iEvent,ebRecHitToken_);
207  auto eeRecHitHandle = getHandle(iEvent,eeRecHitToken_);
208  auto beamSpotHandle = getHandle(iEvent,beamSpotToken_);
209  auto candHandles = getHandles(iEvent,candTokens_);
210 
211  bool isAOD = isEventAOD(iEvent,eleToken_);
212  const auto& candVetos = isAOD ? candVetosAOD_ : candVetosMiniAOD_;
213 
214  edm::ESHandle<CaloTopology> caloTopoHandle;
215  iSetup.get<CaloTopologyRecord>().get(caloTopoHandle);
216 
217  std::vector<float> eleTrkPtIso;
218  std::vector<int> eleNrSaturateIn5x5;
219  for(size_t eleNr=0;eleNr<eleHandle->size();eleNr++){
220  auto elePtr = eleHandle->ptrAt(eleNr);
221  eleTrkPtIso.push_back(calTrkIso(*elePtr,*eleHandle,candHandles,candVetos));
222  eleNrSaturateIn5x5.push_back(nrSaturatedCrysIn5x5(*elePtr,ebRecHitHandle,eeRecHitHandle,caloTopoHandle));
223  }
224 
225  writeValueMap(iEvent,eleHandle,eleTrkPtIso,eleTrkPtIsoLabel_);
226  writeValueMap(iEvent,eleHandle,eleNrSaturateIn5x5,eleNrSaturateIn5x5Label_);
227 }
228 
232  edm::ESHandle<CaloTopology>& caloTopo)
233 {
234  DetId id = ele.superCluster()->seed()->seed();
235  auto recHits = id.subdetId()==EcalBarrel ? ebHits.product() : eeHits.product();
236  return noZS::EcalClusterTools::nrSaturatedCrysIn5x5(id,recHits,caloTopo.product());
237 
238 }
239 
242  const edm::View<reco::GsfElectron>& eles,
243  const std::vector<edm::Handle<pat::PackedCandidateCollection> >& handles,
244  const std::vector<EleTkIsolFromCands::PIDVeto>& pidVetos)const
245 {
246  if(ele.gsfTrack().isNull()) return std::numeric_limits<float>::max();
247  else{
248  float trkIso=0.;
249  for(size_t handleNr=0;handleNr<handles.size();handleNr++){
250  auto& handle = handles[handleNr];
251  if(handle.isValid()){
252  if(handleNr<pidVetos.size()){
253  trkIso+= trkIsoCalc_.calIsolPt(*ele.gsfTrack(),*handle,pidVetos[handleNr]);
254  }else{
255  throw cms::Exception("LogicError") <<" somehow the pidVetos and handles do not much, given this is checked at construction time, something has gone wrong in the code handle nr "<<handleNr<<" size of vetos "<<pidVetos.size();
256  }
257  }
258  }
259  return trkIso;
260  }
261 }
262 
263 template<typename T>
266  const std::vector<T> & values,
267  const std::string& label)
268 {
269  std::unique_ptr<edm::ValueMap<T> > valMap(new edm::ValueMap<T>());
270  typename edm::ValueMap<T>::Filler filler(*valMap);
271  filler.insert(handle, values.begin(), values.end());
272  filler.fill();
273  iEvent.put(std::move(valMap),label);
274 }
275 
277 
279  desc.add<edm::InputTag>("beamSpot",edm::InputTag("offlineBeamSpot"));
280  desc.add<edm::InputTag>("ebRecHitsAOD",edm::InputTag("reducedEcalRecHitsEB"));
281  desc.add<edm::InputTag>("eeRecHitsAOD",edm::InputTag("reducedEcalRecHitsEE"));
282  desc.add<std::vector<edm::InputTag> >("candsAOD",{edm::InputTag("packedCandidates")});
283  desc.add<std::vector<std::string> >("candVetosAOD",{"none"});
284  desc.add<edm::InputTag>("elesAOD",edm::InputTag("gedGsfElectrons"));
285 
286  desc.add<edm::InputTag>("ebRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEB"));
287  desc.add<edm::InputTag>("eeRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEE"));
288  desc.add<std::vector<edm::InputTag> >("candsMiniAOD",{edm::InputTag("packedCandidates")});
289  desc.add<std::vector<std::string> >("candVetosMiniAOD",{"none"});
290  desc.add<edm::InputTag>("elesMiniAOD",edm::InputTag("gedGsfElectrons"));
291  desc.add<int>("dataFormat",0);
292 
293  desc.add("trkIsoConfig",EleTkIsolFromCands::pSetDescript());
294 
295  descriptions.addDefault(desc);
296 }
297 
size
Write out results.
static int nrSaturatedCrysIn5x5(const DetId &id, const EcalRecHitCollection *recHits, const CaloTopology *topology)
T getParameter(std::string const &) const
GsfTrackRef gsfTrack() const override
reference to a GsfTrack
Definition: GsfElectron.h:185
DualToken< EcalRecHitCollection > ebRecHitToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:136
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:519
#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)
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:53
std::vector< DualToken< pat::PackedCandidateCollection > > candTokens_
static edm::Handle< T > getHandle(const edm::Event &iEvent, const DualToken< T > &token)
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)
std::vector< EleTkIsolFromCands::PIDVeto > candVetosMiniAOD_
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_
double calIsolPt(Args &&...args) const
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:74
bool isNull() const
Checks for null.
Definition: Ref.h:250
Definition: DetId.h:18
static edm::Handle< T > getHandle(const edm::Event &iEvent, const edm::EDGetTokenT< T > &token)
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
static PIDVeto pidVetoFromStr(const std::string &vetoStr)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
static std::vector< edm::Handle< T > > getHandles(const edm::Event &iEvent, const std::vector< DualToken< T > > &tokens)
const T & get() const
Definition: EventSetup.h:59
float calTrkIso(const reco::GsfElectron &ele, const edm::View< reco::GsfElectron > &eles, const std::vector< edm::Handle< pat::PackedCandidateCollection > > &handles, const std::vector< EleTkIsolFromCands::PIDVeto > &pidVetos) const
std::vector< EleTkIsolFromCands::PIDVeto > candVetosAOD_
void setToken(std::vector< edm::EDGetTokenT< T > > &tokens, const edm::ParameterSet &iPara, const std::string &tagName)
ElectronHEEPIDValueMapProducer(const edm::ParameterSet &)
HLT enums.
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:184
void setToken(edm::EDGetTokenT< T > &token, edm::InputTag tag)
T const * product() const
Definition: ESHandle.h:86
static bool isEventAOD(const edm::Event &iEvent, const DualToken< T > &token)
def move(src, dest)
Definition: eostools.py:510