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  static 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,
75  const EleTkIsolFromCands& trkIsoCalc);
76 
77  template <typename T> void setToken(edm::EDGetTokenT<T>& token,edm::InputTag tag){token=consumes<T>(tag);}
78  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));}
79  template <typename T> void setToken(std::vector<edm::EDGetTokenT<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagName){
80  auto tags =iPara.getParameter<std::vector<edm::InputTag> >(tagName);
81  for(auto& tag : tags) {
82  edm::EDGetTokenT<T> token;
83  setToken(token,tag);
84  tokens.push_back(token);
85  }
86  }
87  template <typename T> void setToken(DualToken<T>& token,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
88  if(format.tryAOD()) token.aod=consumes<T>(iPara.getParameter<edm::InputTag>(tagAOD));
89  if(format.tryMiniAOD()) token.miniAOD=consumes<T>(iPara.getParameter<edm::InputTag>(tagMiniAOD));
90  }
91  template <typename T> void setToken(std::vector<DualToken<T> >& tokens,const edm::ParameterSet& iPara,const std::string& tagAOD,const std::string& tagMiniAOD,DataFormat format){
92  auto tagsAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagAOD);
93  auto tagsMiniAOD =iPara.getParameter<std::vector<edm::InputTag> >(tagMiniAOD);
94  size_t maxSize = std::max(tagsAOD.size(),tagsMiniAOD.size());
95  tokens.clear();
96  tokens.resize(maxSize);
97  if(format.tryAOD()){
98  for(size_t tagNr=0;tagNr<tagsAOD.size();tagNr++) {
99  setToken(tokens[tagNr].aod,tagsAOD[tagNr]);
100  }
101  }
102  if(format.tryMiniAOD()){
103  for(size_t tagNr=0;tagNr<tagsMiniAOD.size();tagNr++) {
104  setToken(tokens[tagNr].miniAOD,tagsMiniAOD[tagNr]);
105  }
106  }
107  }
108 
109  template<typename T>
110  static edm::Handle<T> getHandle(const edm::Event& iEvent,
111  const edm::EDGetTokenT<T>& token){
113  iEvent.getByToken(token,handle);
114  return handle;
115  }
116  template<typename T>
117  static edm::Handle<T> getHandle(const edm::Event& iEvent,
118  const DualToken<T>& token){
120  if(!token.aod.isUninitialized()) iEvent.getByToken(token.aod,handle);
121  if(!handle.isValid() && !token.miniAOD.isUninitialized()) iEvent.getByToken(token.miniAOD,handle);
122  return handle;
123  }
124 
125  template<typename T>
126  static std::vector<edm::Handle<T> >
127  getHandles(const edm::Event& iEvent,const std::vector<DualToken<T> >& tokens){
128  std::vector<edm::Handle<T> > handles(tokens.size());
129  if(tokens.empty()) return handles;
130  if(!tokens[0].aod.isUninitialized()) iEvent.getByToken(tokens[0].aod,handles[0]);
131  bool isAOD = handles[0].isValid();
132  if(!isAOD && !tokens[0].miniAOD.isUninitialized() ) iEvent.getByToken(tokens[0].miniAOD,handles[0]);
133 
134  for(size_t tokenNr=1;tokenNr<tokens.size();tokenNr++){
135  auto token = isAOD ? tokens[tokenNr].aod : tokens[tokenNr].miniAOD;
136  if(!token.isUninitialized()) iEvent.getByToken(token,handles[tokenNr]);
137  }
138  return handles;
139  }
140 
141  template<typename T>
142  static bool isEventAOD(const edm::Event& iEvent,const DualToken<T>& token){
144  if(!token.aod.isUninitialized()) iEvent.getByToken(token.aod,handle);
145  if(handle.isValid()) return true;
146  else return false;
147  }
148 
149 
150 
154  std::vector<DualToken<pat::PackedCandidateCollection> >candTokens_;
156 
161  std::vector<EleTkIsolFromCands::PIDVeto> candVetosAOD_;
162  std::vector<EleTkIsolFromCands::PIDVeto> candVetosMiniAOD_;
163 
167 };
168 
172 
173 
174 
176  trkIsoCalc_(iConfig.getParameter<edm::ParameterSet>("trkIsoConfig")),
177  trkIso04Calc_(iConfig.getParameter<edm::ParameterSet>("trkIso04Config")),
178  makeTrkIso04_(iConfig.getParameter<bool>("makeTrkIso04")),
179  dataFormat_(iConfig.getParameter<int>("dataFormat"))
180 {
181  setToken(ebRecHitToken_,iConfig,"ebRecHitsAOD","ebRecHitsMiniAOD",dataFormat_);
182  setToken(eeRecHitToken_,iConfig,"eeRecHitsAOD","eeRecHitsMiniAOD",dataFormat_);
183  setToken(eleToken_,iConfig,"elesAOD","elesMiniAOD",dataFormat_);
184  setToken(candTokens_,iConfig,"candsAOD","candsMiniAOD",dataFormat_);
185  setToken(beamSpotToken_,iConfig,"beamSpot");
186 
187  auto fillVetos=[](const auto& in,auto& out){
188  std::transform(in.begin(),in.end(),std::back_inserter(out),EleTkIsolFromCands::pidVetoFromStr);
189  };
190 
191  fillVetos(iConfig.getParameter<std::vector<std::string> >("candVetosAOD"),candVetosAOD_);
192  if(candVetosAOD_.size()!=iConfig.getParameter<std::vector<edm::InputTag> >("candsAOD").size()){
193  throw cms::Exception("ConfigError") <<" Error candVetosAOD should be the same size as candsAOD "<<std::endl;
194  }
195 
196  fillVetos(iConfig.getParameter<std::vector<std::string> >("candVetosMiniAOD"),candVetosMiniAOD_);
197  if(candVetosMiniAOD_.size()!=iConfig.getParameter<std::vector<edm::InputTag> >("candsMiniAOD").size()){
198  throw cms::Exception("ConfigError") <<" Error candVetosMiniAOD should be the same size as candsMiniAOD "<<std::endl;
199  }
200 
201  produces<edm::ValueMap<float> >(eleTrkPtIsoLabel_);
202  if(makeTrkIso04_) produces<edm::ValueMap<float> >(eleTrkPtIso04Label_);
203  produces<edm::ValueMap<int> >(eleNrSaturateIn5x5Label_);
204 }
205 
207 {
208 
209 }
210 
212 {
213  auto eleHandle = getHandle(iEvent,eleToken_);
214  auto ebRecHitHandle = getHandle(iEvent,ebRecHitToken_);
215  auto eeRecHitHandle = getHandle(iEvent,eeRecHitToken_);
216  auto beamSpotHandle = getHandle(iEvent,beamSpotToken_);
217  auto candHandles = getHandles(iEvent,candTokens_);
218 
219  bool isAOD = isEventAOD(iEvent,eleToken_);
220  const auto& candVetos = isAOD ? candVetosAOD_ : candVetosMiniAOD_;
221 
222  edm::ESHandle<CaloTopology> caloTopoHandle;
223  iSetup.get<CaloTopologyRecord>().get(caloTopoHandle);
224 
225  std::vector<float> eleTrkPtIso;
226  std::vector<float> eleTrkPtIso04;
227  std::vector<int> eleNrSaturateIn5x5;
228  for(auto const& ele : *eleHandle) {
229  eleTrkPtIso.push_back(calTrkIso(ele,*eleHandle,candHandles,candVetos,trkIsoCalc_));
230  if(makeTrkIso04_){
231  eleTrkPtIso04.push_back(calTrkIso(ele,*eleHandle,candHandles,candVetos,trkIso04Calc_));
232  }
233  eleNrSaturateIn5x5.push_back(nrSaturatedCrysIn5x5(ele,ebRecHitHandle,eeRecHitHandle,caloTopoHandle));
234  }
235 
236  writeValueMap(iEvent,eleHandle,eleTrkPtIso,eleTrkPtIsoLabel_);
237  if(makeTrkIso04_) writeValueMap(iEvent,eleHandle,eleTrkPtIso04,eleTrkPtIso04Label_);
238  writeValueMap(iEvent,eleHandle,eleNrSaturateIn5x5,eleNrSaturateIn5x5Label_);
239 }
240 
244  edm::ESHandle<CaloTopology>& caloTopo)
245 {
246  DetId id = ele.superCluster()->seed()->seed();
247  auto recHits = id.subdetId()==EcalBarrel ? ebHits.product() : eeHits.product();
248  return noZS::EcalClusterTools::nrSaturatedCrysIn5x5(id,recHits,caloTopo.product());
249 
250 }
251 
254  const edm::View<reco::GsfElectron>& eles,
255  const std::vector<edm::Handle<pat::PackedCandidateCollection> >& handles,
256  const std::vector<EleTkIsolFromCands::PIDVeto>& pidVetos,
257  const EleTkIsolFromCands& trkIsoCalc)
258 {
259  if(ele.gsfTrack().isNull()) return std::numeric_limits<float>::max();
260  else{
261  float trkIso=0.;
262  for(size_t handleNr=0;handleNr<handles.size();handleNr++){
263  auto& handle = handles[handleNr];
264  if(handle.isValid()){
265  if(handleNr<pidVetos.size()){
266  trkIso+= trkIsoCalc.calIsolPt(*ele.gsfTrack(),*handle,pidVetos[handleNr]);
267  }else{
268  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();
269  }
270  }
271  }
272  return trkIso;
273  }
274 }
275 
276 template<typename T>
279  const std::vector<T> & values,
280  const std::string& label)
281 {
282  std::unique_ptr<edm::ValueMap<T> > valMap(new edm::ValueMap<T>());
283  typename edm::ValueMap<T>::Filler filler(*valMap);
284  filler.insert(handle, values.begin(), values.end());
285  filler.fill();
286  iEvent.put(std::move(valMap),label);
287 }
288 
290 
292  desc.add<edm::InputTag>("beamSpot",edm::InputTag("offlineBeamSpot"));
293  desc.add<edm::InputTag>("ebRecHitsAOD",edm::InputTag("reducedEcalRecHitsEB"));
294  desc.add<edm::InputTag>("eeRecHitsAOD",edm::InputTag("reducedEcalRecHitsEE"));
295  desc.add<std::vector<edm::InputTag> >("candsAOD",{edm::InputTag("packedCandidates")});
296  desc.add<std::vector<std::string> >("candVetosAOD",{"none"});
297  desc.add<edm::InputTag>("elesAOD",edm::InputTag("gedGsfElectrons"));
298 
299  desc.add<edm::InputTag>("ebRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEB"));
300  desc.add<edm::InputTag>("eeRecHitsMiniAOD",edm::InputTag("reducedEcalRecHitsEE"));
301  desc.add<std::vector<edm::InputTag> >("candsMiniAOD",{edm::InputTag("packedCandidates")});
302  desc.add<std::vector<std::string> >("candVetosMiniAOD",{"none"});
303  desc.add<edm::InputTag>("elesMiniAOD",edm::InputTag("gedGsfElectrons"));
304  desc.add<int>("dataFormat",0);
305  desc.add<bool>("makeTrkIso04",false);
306  desc.add("trkIsoConfig",EleTkIsolFromCands::pSetDescript());
307  desc.add("trkIso04Config",EleTkIsolFromCands::pSetDescript());
308 
309  descriptions.addDefault(desc);
310 }
311 
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:186
DualToken< EcalRecHitCollection > ebRecHitToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
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)
char const * label
void setToken(edm::EDGetTokenT< T > &token, const edm::ParameterSet &iPara, const std::string &tag)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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:248
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:74
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)
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.
T get() const
Definition: EventSetup.h:71
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:185
static 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 EleTkIsolFromCands &trkIsoCalc)
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:511