CMS 3D CMS Logo

PFLinker.cc
Go to the documentation of this file.
1 
20 
21 namespace edm {
22  class EventSetup;
23 } // namespace edm
24 
26 public:
27  explicit PFLinker(const edm::ParameterSet&);
28 
29  ~PFLinker() override;
30 
31  void produce(edm::Event&, const edm::EventSetup&) override;
32 
33 private:
34  template <typename TYPE>
38  edm::Handle<TYPE>& inputObjCollection,
39  const std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>& mapToTheCandidate,
40  const edm::OrphanHandle<reco::PFCandidateCollection>& newPFCandColl) const;
41 
42 private:
44  std::vector<edm::EDGetTokenT<reco::PFCandidateCollection>> inputTagPFCandidates_;
45 
48 
51 
58 
61 
64 
67 
70 
73 
76 };
77 
79 
81  // vector of InputTag; more than 1 is not for RECO, it is for analysis
82 
83  std::vector<edm::InputTag> tags = iConfig.getParameter<std::vector<edm::InputTag>>("PFCandidate");
84  for (unsigned int i = 0; i < tags.size(); ++i)
85  inputTagPFCandidates_.push_back(consumes<reco::PFCandidateCollection>(tags[i]));
86 
87  inputTagGsfElectrons_ = consumes<reco::GsfElectronCollection>(iConfig.getParameter<edm::InputTag>("GsfElectrons"));
88 
89  inputTagPhotons_ = consumes<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("Photons"));
90 
91  muonTag_ = iConfig.getParameter<edm::InputTag>("Muons");
92  inputTagMuons_ = consumes<reco::MuonCollection>(edm::InputTag(muonTag_.label()));
93  inputTagMuonMap_ = consumes<reco::MuonToMuonMap>(muonTag_);
94 
95  nameOutputPF_ = iConfig.getParameter<std::string>("OutputPF");
96 
97  nameOutputElectronsPF_ = iConfig.getParameter<std::string>("ValueMapElectrons");
98 
99  nameOutputPhotonsPF_ = iConfig.getParameter<std::string>("ValueMapPhotons");
100 
101  producePFCandidates_ = iConfig.getParameter<bool>("ProducePFCandidates");
102 
103  nameOutputMergedPF_ = iConfig.getParameter<std::string>("ValueMapMerged");
104 
105  fillMuonRefs_ = iConfig.getParameter<bool>("FillMuonRefs");
106 
107  forceElectronsInHGCAL_ = iConfig.getParameter<bool>("forceElectronsInHGCAL");
108 
109  // should not produce PFCandidates and read seve
110  if (producePFCandidates_ && inputTagPFCandidates_.size() > 1) {
111  edm::LogError("PFLinker")
112  << " cannot read several collections of PFCandidates and produce a new collection at the same time. "
113  << std::endl;
114  assert(false);
115  }
116  if (producePFCandidates_) {
117  produces<reco::PFCandidateCollection>(nameOutputPF_);
118  }
119  produces<edm::ValueMap<reco::PFCandidatePtr>>(nameOutputElectronsPF_);
120  produces<edm::ValueMap<reco::PFCandidatePtr>>(nameOutputPhotonsPF_);
121  produces<edm::ValueMap<reco::PFCandidatePtr>>(nameOutputMergedPF_);
122  if (fillMuonRefs_)
123  produces<edm::ValueMap<reco::PFCandidatePtr>>(muonTag_.label());
124 }
125 
127 
129  auto pfCandidates_p = std::make_unique<reco::PFCandidateCollection>();
130 
131  auto gsfElectrons = iEvent.getHandle(inputTagGsfElectrons_);
132 
133  std::map<reco::GsfElectronRef, reco::PFCandidatePtr> electronCandidateMap;
134 
135  auto photons = iEvent.getHandle(inputTagPhotons_);
136  std::map<reco::PhotonRef, reco::PFCandidatePtr> photonCandidateMap;
137 
139  if (fillMuonRefs_)
140  muonMap = iEvent.getHandle(inputTagMuonMap_);
141  std::map<reco::MuonRef, reco::PFCandidatePtr> muonCandidateMap;
142 
143  unsigned nColPF = inputTagPFCandidates_.size();
144 
145  for (unsigned icol = 0; icol < nColPF; ++icol) {
146  auto pfCandidates = iEvent.getHandle(inputTagPFCandidates_[icol]);
147  unsigned ncand = pfCandidates->size();
148 
149  for (unsigned i = 0; i < ncand; ++i) {
151  reco::PFCandidate cand(candPtr);
152 
153  bool isphoton = cand.particleId() == reco::PFCandidate::gamma && cand.mva_nothing_gamma() > 0.;
154  bool iselectron = cand.particleId() == reco::PFCandidate::e;
155  // PFCandidates may have a valid MuonRef though they are not muons.
156  bool hasNonNullMuonRef = cand.muonRef().isNonnull() && fillMuonRefs_;
157 
158  // if not an electron or a photon or a muon just fill the PFCandidate collection
159  if (!(isphoton || iselectron || hasNonNullMuonRef)) {
160  pfCandidates_p->push_back(cand);
161  continue;
162  }
163 
164  if (hasNonNullMuonRef) {
165  reco::MuonRef muRef = (*muonMap)[cand.muonRef()];
166  cand.setMuonRef(muRef);
167  muonCandidateMap[muRef] = candPtr;
168  }
169 
170  // if it is an electron. Find the GsfElectron with the same GsfTrack
171  if (iselectron) {
172  const reco::GsfTrackRef& gsfTrackRef(cand.gsfTrackRef());
173  auto itcheck = find_if(gsfElectrons->begin(), gsfElectrons->end(), [&gsfTrackRef](const auto& ele) {
174  return (ele.gsfTrack() == gsfTrackRef);
175  });
176  if (itcheck == gsfElectrons->end()) {
177  if (!forceElectronsInHGCAL_) {
178  std::ostringstream err;
179  err << " Problem in PFLinker: no GsfElectron " << std::endl;
180  edm::LogError("PFLinker") << err.str();
181  } else {
182  LogDebug("PFLinker") << "Forcing an electron pfCandidate at: " << cand.eta() << " in HGCAL" << std::endl;
183  pfCandidates_p->push_back(cand);
184  }
185  continue; // Watch out ! Continue
186  }
187  reco::GsfElectronRef electronRef(gsfElectrons, itcheck - gsfElectrons->begin());
188  cand.setGsfElectronRef(electronRef);
189  cand.setSuperClusterRef(electronRef->superCluster());
190  // update energy information since now it is done post-particleFlowTmp
191  cand.setEcalEnergy(electronRef->superCluster()->rawEnergy(), electronRef->ecalEnergy());
192  cand.setDeltaP(electronRef->p4Error(reco::GsfElectron::P4_COMBINATION));
193  cand.setP4(electronRef->p4(reco::GsfElectron::P4_COMBINATION));
194  electronCandidateMap[electronRef] = candPtr;
195  }
196 
197  // if it is a photon, find the one with the same PF super-cluster
198  if (isphoton) {
199  const reco::SuperClusterRef& scRef(cand.superClusterRef());
200  auto itcheck = find_if(
201  photons->begin(), photons->end(), [&scRef](const auto& photon) { return photon.superCluster() == scRef; });
202  if (itcheck == photons->end()) {
203  std::ostringstream err;
204  err << " Problem in PFLinker: no Photon " << std::endl;
205  edm::LogError("PFLinker") << err.str();
206  continue; // Watch out ! Continue
207  }
208  reco::PhotonRef photonRef(photons, itcheck - photons->begin());
209  cand.setPhotonRef(photonRef);
210  cand.setSuperClusterRef(photonRef->superCluster());
211  // update energy information since now it is done post-particleFlowTmp
212  cand.setEcalEnergy(photonRef->superCluster()->rawEnergy(),
213  photonRef->getCorrectedEnergy(reco::Photon::regression2));
214  cand.setDeltaP(photonRef->getCorrectedEnergyError(reco::Photon::regression2));
215  cand.setP4(photonRef->p4(reco::Photon::regression2));
216  photonCandidateMap[photonRef] = candPtr;
217  }
218 
219  pfCandidates_p->push_back(cand);
220  }
221  // save the PFCandidates and get a valid handle
222  }
223  const edm::OrphanHandle<reco::PFCandidateCollection> pfCandidateRefProd =
224  (producePFCandidates_) ? iEvent.put(std::move(pfCandidates_p), nameOutputPF_)
226 
227  // now make the valuemaps
228 
229  edm::ValueMap<reco::PFCandidatePtr> pfMapGsfElectrons = fillValueMap<reco::GsfElectronCollection>(
230  iEvent, nameOutputElectronsPF_, gsfElectrons, electronCandidateMap, pfCandidateRefProd);
231 
232  edm::ValueMap<reco::PFCandidatePtr> pfMapPhotons = fillValueMap<reco::PhotonCollection>(
233  iEvent, nameOutputPhotonsPF_, photons, photonCandidateMap, pfCandidateRefProd);
234 
236 
237  if (fillMuonRefs_) {
238  auto muons = iEvent.getHandle(inputTagMuons_);
239 
240  pfMapMuons =
241  fillValueMap<reco::MuonCollection>(iEvent, muonTag_.label(), muons, muonCandidateMap, pfCandidateRefProd);
242  }
243 
244  auto pfMapMerged = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
245  edm::ValueMap<reco::PFCandidatePtr>::Filler pfMapMergedFiller(*pfMapMerged);
246 
247  *pfMapMerged += pfMapGsfElectrons;
248  *pfMapMerged += pfMapPhotons;
249  if (fillMuonRefs_)
250  *pfMapMerged += pfMapMuons;
251 
252  iEvent.put(std::move(pfMapMerged), nameOutputMergedPF_);
253 }
254 
255 template <typename TYPE>
257  edm::Event& event,
259  edm::Handle<TYPE>& inputObjCollection,
260  const std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>& mapToTheCandidate,
261  const edm::OrphanHandle<reco::PFCandidateCollection>& newPFCandColl) const {
262  auto pfMap_p = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
264 
265  typedef typename std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>::const_iterator MapTYPE_it;
266 
267  unsigned nObj = inputObjCollection->size();
268  std::vector<reco::PFCandidatePtr> values(nObj);
269 
270  for (unsigned iobj = 0; iobj < nObj; ++iobj) {
271  edm::Ref<TYPE> objRef(inputObjCollection, iobj);
272  MapTYPE_it itcheck = mapToTheCandidate.find(objRef);
273 
274  reco::PFCandidatePtr candPtr;
275 
276  if (itcheck != mapToTheCandidate.end())
277  candPtr = producePFCandidates_ ? reco::PFCandidatePtr(newPFCandColl, itcheck->second.key()) : itcheck->second;
278 
279  values[iobj] = candPtr;
280  }
281 
282  filler.insert(inputObjCollection, values.begin(), values.end());
283  filler.fill();
284  edm::ValueMap<reco::PFCandidatePtr> returnValue = *pfMap_p;
285  event.put(std::move(pfMap_p), label);
286  return returnValue;
287 }
edm::InputTag muonTag_
Input Muons.
Definition: PFLinker.cc:53
T getParameter(std::string const &) const
Definition: ParameterSet.h:307
edm::EDGetTokenT< reco::MuonCollection > inputTagMuons_
Definition: PFLinker.cc:54
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFLinker.cc:128
bool producePFCandidates_
Flags - if true: References will be towards new collection ; if false to the original one...
Definition: PFLinker.cc:69
std::string const & label() const
Definition: InputTag.h:36
Log< level::Error, false > LogError
edm::EDGetTokenT< reco::PhotonCollection > inputTagPhotons_
Input Photons.
Definition: PFLinker.cc:50
assert(be >=bs)
PFLinker(const edm::ParameterSet &)
Definition: PFLinker.cc:80
edm::EDGetTokenT< reco::MuonToMuonMap > inputTagMuonMap_
Definition: PFLinker.cc:55
char const * label
int iEvent
Definition: GenABIO.cc:224
std::string nameOutputElectronsPF_
name of output ValueMap electrons
Definition: PFLinker.cc:60
std::string nameOutputPhotonsPF_
name of output ValueMap photons
Definition: PFLinker.cc:63
std::string nameOutputPF_
name of output collection of PFCandidate
Definition: PFLinker.cc:57
bool fillMuonRefs_
Set muon refs and produce the value map?
Definition: PFLinker.cc:72
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
HLT enums.
bool forceElectronsInHGCAL_
Put Electrons within HGCAL coming from SimPFProducer.
Definition: PFLinker.cc:75
~PFLinker() override
Definition: PFLinker.cc:126
edm::EDGetTokenT< reco::GsfElectronCollection > inputTagGsfElectrons_
Input GsfElectrons.
Definition: PFLinker.cc:47
def move(src, dest)
Definition: eostools.py:511
std::vector< edm::EDGetTokenT< reco::PFCandidateCollection > > inputTagPFCandidates_
Input PFCandidates.
Definition: PFLinker.cc:44
Definition: event.py:1
std::string nameOutputMergedPF_
name of output merged ValueMap
Definition: PFLinker.cc:66
#define LogDebug(id)
edm::ValueMap< reco::PFCandidatePtr > fillValueMap(edm::Event &event, std::string label, edm::Handle< TYPE > &inputObjCollection, const std::map< edm::Ref< TYPE >, reco::PFCandidatePtr > &mapToTheCandidate, const edm::OrphanHandle< reco::PFCandidateCollection > &newPFCandColl) const
Definition: PFLinker.cc:256