CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ElectronHEEPIDValueMapProducer.cc
Go to the documentation of this file.
3 
7 
10 
14 
20 
23 
26 
27 #include <memory>
28 #include <vector>
29 
30 using IsolationCalculators = std::vector<std::unique_ptr<EleTkIsolFromCands>>;
31 
32 //Heavily inspired from ElectronIDValueMapProducer
34 private:
35  //helper classes to handle AOD vs MiniAOD
36  template <typename T>
37  struct DualToken {
40  };
41  class DataFormat {
42  public:
43  enum Format { AUTO = 0, AOD = 1, MINIAOD = 2 };
44 
45  private:
46  int data_;
47 
48  public:
49  DataFormat(int val) : data_(val) {}
50  bool tryAOD() const { return data_ == AUTO || data_ == AOD; }
51  bool tryMiniAOD() const { return data_ == AUTO || data_ == MINIAOD; }
52  int operator()() const { return data_; }
53  };
54 
55 public:
58 
59  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
60 
61 private:
62  void produce(edm::Event&, const edm::EventSetup&) override;
63 
64  template <typename T>
65  static void writeValueMap(edm::Event& iEvent,
67  const std::vector<T>& values,
68  const std::string& label);
69 
70  static int nrSaturatedCrysIn5x5(const reco::GsfElectron& ele,
73  edm::ESHandle<CaloTopology>& caloTopo);
74 
75  static float calTrkIso(const reco::GsfElectron& ele, IsolationCalculators const& isolationCalculators);
76 
77  template <typename T>
79  token = consumes<T>(tag);
80  }
81  template <typename T>
83  token = consumes<T>(iPara.getParameter<edm::InputTag>(tag));
84  }
85  template <typename T>
86  void setToken(std::vector<edm::EDGetTokenT<T>>& tokens, const edm::ParameterSet& iPara, const std::string& tagName) {
87  auto tags = iPara.getParameter<std::vector<edm::InputTag>>(tagName);
88  for (auto& tag : tags) {
90  setToken(token, tag);
91  tokens.push_back(token);
92  }
93  }
94  template <typename T>
96  const edm::ParameterSet& iPara,
97  const std::string& tagAOD,
98  const std::string& tagMiniAOD,
100  if (format.tryAOD())
101  token.aod = consumes<T>(iPara.getParameter<edm::InputTag>(tagAOD));
102  if (format.tryMiniAOD())
103  token.miniAOD = consumes<T>(iPara.getParameter<edm::InputTag>(tagMiniAOD));
104  }
105  template <typename T>
107  const edm::ParameterSet& iPara,
108  const std::string& tagAOD,
109  const std::string& tagMiniAOD,
110  DataFormat format) {
111  auto tagsAOD = iPara.getParameter<std::vector<edm::InputTag>>(tagAOD);
112  auto tagsMiniAOD = iPara.getParameter<std::vector<edm::InputTag>>(tagMiniAOD);
113  size_t maxSize = std::max(tagsAOD.size(), tagsMiniAOD.size());
114  tokens.clear();
115  tokens.resize(maxSize);
116  if (format.tryAOD()) {
117  for (size_t tagNr = 0; tagNr < tagsAOD.size(); tagNr++) {
118  setToken(tokens[tagNr].aod, tagsAOD[tagNr]);
119  }
120  }
121  if (format.tryMiniAOD()) {
122  for (size_t tagNr = 0; tagNr < tagsMiniAOD.size(); tagNr++) {
123  setToken(tokens[tagNr].miniAOD, tagsMiniAOD[tagNr]);
124  }
125  }
126  }
127 
128  template <typename T>
131  if (!token.aod.isUninitialized())
132  iEvent.getByToken(token.aod, handle);
133  if (!handle.isValid() && !token.miniAOD.isUninitialized())
134  iEvent.getByToken(token.miniAOD, handle);
135  return handle;
136  }
137 
138  template <typename T>
139  static std::vector<edm::Handle<T>> getHandles(const edm::Event& iEvent, const std::vector<DualToken<T>>& tokens) {
140  std::vector<edm::Handle<T>> handles(tokens.size());
141  if (tokens.empty())
142  return handles;
143  if (!tokens[0].aod.isUninitialized())
144  iEvent.getByToken(tokens[0].aod, handles[0]);
145  bool isAOD = handles[0].isValid();
146  if (!isAOD && !tokens[0].miniAOD.isUninitialized())
147  iEvent.getByToken(tokens[0].miniAOD, handles[0]);
148 
149  for (size_t tokenNr = 1; tokenNr < tokens.size(); tokenNr++) {
150  auto token = isAOD ? tokens[tokenNr].aod : tokens[tokenNr].miniAOD;
151  if (!token.isUninitialized())
152  iEvent.getByToken(token, handles[tokenNr]);
153  }
154  return handles;
155  }
156 
157  template <typename T>
158  static bool isEventAOD(const edm::Event& iEvent, const DualToken<T>& token) {
160  if (!token.aod.isUninitialized())
161  iEvent.getByToken(token.aod, handle);
162  if (handle.isValid())
163  return true;
164  else
165  return false;
166  }
167 
171  std::vector<DualToken<pat::PackedCandidateCollection>> candTokens_;
174 
179  std::vector<EleTkIsolFromCands::PIDVeto> candVetosAOD_;
180  std::vector<EleTkIsolFromCands::PIDVeto> candVetosMiniAOD_;
181 
185 };
186 
190 
192  : trkIsoCalcCfg_(iConfig.getParameter<edm::ParameterSet>("trkIsoConfig")),
193  trkIso04CalcCfg_(iConfig.getParameter<edm::ParameterSet>("trkIso04Config")),
194  makeTrkIso04_(iConfig.getParameter<bool>("makeTrkIso04")),
195  dataFormat_(iConfig.getParameter<int>("dataFormat")) {
196  setToken(ebRecHitToken_, iConfig, "ebRecHitsAOD", "ebRecHitsMiniAOD", dataFormat_);
197  setToken(eeRecHitToken_, iConfig, "eeRecHitsAOD", "eeRecHitsMiniAOD", dataFormat_);
198  setToken(eleToken_, iConfig, "elesAOD", "elesMiniAOD", dataFormat_);
199  setToken(candTokens_, iConfig, "candsAOD", "candsMiniAOD", dataFormat_);
200  setToken(beamSpotToken_, iConfig, "beamSpot");
202 
203  auto fillVetos = [](const auto& in, auto& out) {
204  std::transform(in.begin(), in.end(), std::back_inserter(out), EleTkIsolFromCands::pidVetoFromStr);
205  };
206 
207  fillVetos(iConfig.getParameter<std::vector<std::string>>("candVetosAOD"), candVetosAOD_);
208  if (candVetosAOD_.size() != iConfig.getParameter<std::vector<edm::InputTag>>("candsAOD").size()) {
209  throw cms::Exception("ConfigError") << " Error candVetosAOD should be the same size as candsAOD " << std::endl;
210  }
211 
212  fillVetos(iConfig.getParameter<std::vector<std::string>>("candVetosMiniAOD"), candVetosMiniAOD_);
213  if (candVetosMiniAOD_.size() != iConfig.getParameter<std::vector<edm::InputTag>>("candsMiniAOD").size()) {
214  throw cms::Exception("ConfigError") << " Error candVetosMiniAOD should be the same size as candsMiniAOD "
215  << std::endl;
216  }
217 
218  produces<edm::ValueMap<float>>(eleTrkPtIsoLabel_);
219  if (makeTrkIso04_)
220  produces<edm::ValueMap<float>>(eleTrkPtIso04Label_);
221  produces<edm::ValueMap<int>>(eleNrSaturateIn5x5Label_);
222 }
223 
225 
227  auto eleHandle = getHandle(iEvent, eleToken_);
228  auto ebRecHitHandle = getHandle(iEvent, ebRecHitToken_);
229  auto eeRecHitHandle = getHandle(iEvent, eeRecHitToken_);
230  auto beamSpotHandle = iEvent.getHandle(beamSpotToken_);
231 
232  bool isAOD = isEventAOD(iEvent, eleToken_);
233  const auto& candVetos = isAOD ? candVetosAOD_ : candVetosMiniAOD_;
234 
235  IsolationCalculators trkIsoCalcs;
236  IsolationCalculators trkIso04Calcs;
237 
238  {
239  int handleNr = 0;
240  for (auto const& handle : getHandles(iEvent, candTokens_)) {
241  if (handle.isValid()) {
242  trkIsoCalcs.emplace_back(std::make_unique<EleTkIsolFromCands>(trkIsoCalcCfg_, *handle, candVetos[handleNr]));
243  if (makeTrkIso04_) {
244  trkIso04Calcs.emplace_back(
245  std::make_unique<EleTkIsolFromCands>(trkIso04CalcCfg_, *handle, candVetos[handleNr]));
246  }
247  }
248  ++handleNr;
249  }
250  }
251 
252  edm::ESHandle<CaloTopology> caloTopoHandle = iSetup.getHandle(caloTopoToken_);
253 
254  std::vector<float> eleTrkPtIso;
255  std::vector<float> eleTrkPtIso04;
256  std::vector<int> eleNrSaturateIn5x5;
257  for (auto const& ele : *eleHandle) {
258  eleTrkPtIso.push_back(calTrkIso(ele, trkIsoCalcs));
259  if (makeTrkIso04_) {
260  eleTrkPtIso04.push_back(calTrkIso(ele, trkIso04Calcs));
261  }
262  eleNrSaturateIn5x5.push_back(nrSaturatedCrysIn5x5(ele, ebRecHitHandle, eeRecHitHandle, caloTopoHandle));
263  }
264 
265  writeValueMap(iEvent, eleHandle, eleTrkPtIso, eleTrkPtIsoLabel_);
266  if (makeTrkIso04_)
267  writeValueMap(iEvent, eleHandle, eleTrkPtIso04, eleTrkPtIso04Label_);
268  writeValueMap(iEvent, eleHandle, eleNrSaturateIn5x5, eleNrSaturateIn5x5Label_);
269 }
270 
274  edm::ESHandle<CaloTopology>& caloTopo) {
275  DetId id = ele.superCluster()->seed()->seed();
276  auto recHits = id.subdetId() == EcalBarrel ? ebHits.product() : eeHits.product();
278 }
279 
281  IsolationCalculators const& isolationCalculators) {
282  if (ele.gsfTrack().isNull()) {
284  }
285 
286  float trkIso = 0.;
287  for (auto& calculator : isolationCalculators) {
288  trkIso += (*calculator)(*ele.gsfTrack()).ptSum;
289  }
290  return trkIso;
291 }
292 
293 template <typename T>
296  const std::vector<T>& values,
297  const std::string& label) {
298  std::unique_ptr<edm::ValueMap<T>> valMap(new edm::ValueMap<T>());
299  typename edm::ValueMap<T>::Filler filler(*valMap);
300  filler.insert(handle, values.begin(), values.end());
301  filler.fill();
302  iEvent.put(std::move(valMap), label);
303 }
304 
307  desc.add<edm::InputTag>("beamSpot", edm::InputTag("offlineBeamSpot"));
308  desc.add<edm::InputTag>("ebRecHitsAOD", edm::InputTag("reducedEcalRecHitsEB"));
309  desc.add<edm::InputTag>("eeRecHitsAOD", edm::InputTag("reducedEcalRecHitsEE"));
310  desc.add<std::vector<edm::InputTag>>("candsAOD", {edm::InputTag("packedCandidates")});
311  desc.add<std::vector<std::string>>("candVetosAOD", {"none"});
312  desc.add<edm::InputTag>("elesAOD", edm::InputTag("gedGsfElectrons"));
313 
314  desc.add<edm::InputTag>("ebRecHitsMiniAOD", edm::InputTag("reducedEcalRecHitsEB"));
315  desc.add<edm::InputTag>("eeRecHitsMiniAOD", edm::InputTag("reducedEcalRecHitsEE"));
316  desc.add<std::vector<edm::InputTag>>("candsMiniAOD", {edm::InputTag("packedCandidates")});
317  desc.add<std::vector<std::string>>("candVetosMiniAOD", {"none"});
318  desc.add<edm::InputTag>("elesMiniAOD", edm::InputTag("gedGsfElectrons"));
319  desc.add<int>("dataFormat", 0);
320  desc.add<bool>("makeTrkIso04", false);
321  desc.add("trkIsoConfig", EleTkIsolFromCands::pSetDescript());
322  desc.add("trkIso04Config", EleTkIsolFromCands::pSetDescript());
323 
324  descriptions.addDefault(desc);
325 }
326 
static int nrSaturatedCrysIn5x5(const DetId &id, const EcalRecHitCollection *recHits, const CaloTopology *topology)
DualToken< EcalRecHitCollection > ebRecHitToken_
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:133
edm::EDGetTokenT< reco::BeamSpot > beamSpotToken_
tuple miniAOD
Definition: PAT_cff.py:15
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:539
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
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< std::unique_ptr< EleTkIsolFromCands >> IsolationCalculators
std::vector< DualToken< pat::PackedCandidateCollection > > candTokens_
void setToken(std::vector< edm::EDGetTokenT< T >> &tokens, const edm::ParameterSet &iPara, const std::string &tagName)
static edm::Handle< T > getHandle(const edm::Event &iEvent, const DualToken< T > &token)
static float calTrkIso(const reco::GsfElectron &ele, IsolationCalculators const &isolationCalculators)
char const * label
GsfTrackRef gsfTrack() const override
reference to a GsfTrack
Definition: GsfElectron.h:156
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:563
void setToken(edm::EDGetTokenT< T > &token, const edm::ParameterSet &iPara, const std::string &tag)
int iEvent
Definition: GenABIO.cc:224
void addDefault(ParameterSetDescription const &psetDescription)
std::vector< EleTkIsolFromCands::PIDVeto > candVetosMiniAOD_
edm::ESGetToken< CaloTopology, CaloTopologyRecord > caloTopoToken_
std::vector< BaseVolumeHandle * > handles
tuple maxSize
&#39;/store/data/Commissioning08/BeamHalo/RECO/StuffAlmostToP5_v1/000/061/642/10A0FE34-A67D-DD11-AD05-000...
static std::vector< edm::Handle< T > > getHandles(const edm::Event &iEvent, const std::vector< DualToken< T >> &tokens)
DualToken< edm::View< reco::GsfElectron > > eleToken_
def move
Definition: eostools.py:511
void setToken(DualToken< T > &token, const edm::ParameterSet &iPara, const std::string &tagAOD, const std::string &tagMiniAOD, DataFormat format)
tuple handle
Definition: patZpeak.py:23
static edm::ParameterSetDescription pSetDescript()
DualToken< EcalRecHitCollection > eeRecHitToken_
static void writeValueMap(edm::Event &iEvent, const edm::Handle< edm::View< reco::GsfElectron >> &handle, const std::vector< T > &values, const std::string &label)
ParameterDescriptionBase * add(U const &iLabel, T const &value)
bool isValid() const
Definition: HandleBase.h:70
bool isNull() const
Checks for null.
Definition: Ref.h:235
Definition: DetId.h:17
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:70
static PIDVeto pidVetoFromStr(const std::string &vetoStr)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
T const * product() const
Definition: ESHandle.h:86
T getParameter(std::string const &) const
Definition: ParameterSet.h:303
std::vector< EleTkIsolFromCands::PIDVeto > candVetosAOD_
EleTkIsolFromCands::Configuration trkIso04CalcCfg_
ElectronHEEPIDValueMapProducer(const edm::ParameterSet &)
ESHandle< T > getHandle(const ESGetToken< T, R > &iToken) const
Definition: EventSetup.h:157
void setToken(edm::EDGetTokenT< T > &token, edm::InputTag tag)
static bool isEventAOD(const edm::Event &iEvent, const DualToken< T > &token)
ESGetTokenH3DDVariant esConsumes(std::string const &Reccord, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
tuple size
Write out results.
SuperClusterRef superCluster() const override
reference to a SuperCluster
Definition: GsfElectron.h:155
EleTkIsolFromCands::Configuration trkIsoCalcCfg_
unsigned transform(const HcalDetId &id, unsigned transformCode)