CMS 3D CMS Logo

PFLinker.cc
Go to the documentation of this file.
6 
7 
9  // vector of InputTag; more than 1 is not for RECO, it is for analysis
10 
11  std::vector<edm::InputTag> tags = iConfig.getParameter<std::vector<edm::InputTag> >("PFCandidate");
12  for (unsigned int i=0;i<tags.size();++i)
13  inputTagPFCandidates_.push_back(consumes<reco::PFCandidateCollection>(tags[i]));
14 
15 
16  inputTagGsfElectrons_=consumes<reco::GsfElectronCollection>(
17  iConfig.getParameter<edm::InputTag>("GsfElectrons"));
18 
19  inputTagPhotons_=consumes<reco::PhotonCollection>(iConfig.getParameter<edm::InputTag>("Photons"));
20 
21 
22  muonTag_ = iConfig.getParameter<edm::InputTag>("Muons");
23  inputTagMuons_=consumes<reco::MuonCollection>(edm::InputTag(muonTag_.label()));
24  inputTagMuonMap_=consumes<reco::MuonToMuonMap>(muonTag_);
25 
27  = iConfig.getParameter<std::string>("OutputPF");
28 
30  = iConfig.getParameter<std::string>("ValueMapElectrons");
31 
33  = iConfig.getParameter<std::string>("ValueMapPhotons");
34 
36  = iConfig.getParameter<bool>("ProducePFCandidates");
37 
39  = iConfig.getParameter<std::string>("ValueMapMerged");
40 
42  = iConfig.getParameter<bool>("FillMuonRefs");
43 
45  = iConfig.getParameter<bool>("forceElectronsInHGCAL");
46 
47  // should not produce PFCandidates and read seve
48  if(producePFCandidates_ && inputTagPFCandidates_.size()>1) {
49  edm::LogError("PFLinker") << " cannot read several collections of PFCandidates and produce a new collection at the same time. " << std::endl;
50  assert(false);
51  }
52  if(producePFCandidates_) {
53  produces<reco::PFCandidateCollection>(nameOutputPF_);
54  }
55  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputElectronsPF_);
56  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputPhotonsPF_);
57  produces<edm::ValueMap<reco::PFCandidatePtr> > (nameOutputMergedPF_);
58  if(fillMuonRefs_) produces<edm::ValueMap<reco::PFCandidatePtr> > (muonTag_.label());
59 
60 }
61 
63 
65 
66  auto pfCandidates_p = std::make_unique<reco::PFCandidateCollection>();
67 
69 
70  std::map<reco::GsfElectronRef,reco::PFCandidatePtr> electronCandidateMap;
71 
72 
73  auto photons = iEvent.getHandle(inputTagPhotons_);
74  std::map<reco::PhotonRef,reco::PFCandidatePtr> photonCandidateMap;
75 
76 
78  if(fillMuonRefs_)
79  muonMap = iEvent.getHandle(inputTagMuonMap_);
80  std::map<reco::MuonRef,reco::PFCandidatePtr> muonCandidateMap;
81 
82  unsigned nColPF=inputTagPFCandidates_.size();
83 
84  for(unsigned icol=0;icol<nColPF;++icol) {
85  auto pfCandidates = iEvent.getHandle(inputTagPFCandidates_[icol]);
86  unsigned ncand=pfCandidates->size();
87 
88  for( unsigned i=0; i<ncand; ++i) {
90  reco::PFCandidate cand(candPtr);
91 
92  bool isphoton = cand.particleId() == reco::PFCandidate::gamma && cand.mva_nothing_gamma()>0.;
93  bool iselectron = cand.particleId() == reco::PFCandidate::e;
94  // PFCandidates may have a valid MuonRef though they are not muons.
95  bool hasNonNullMuonRef = cand.muonRef().isNonnull() && fillMuonRefs_;
96 
97  // if not an electron or a photon or a muon just fill the PFCandidate collection
98  if ( !(isphoton || iselectron || hasNonNullMuonRef)){pfCandidates_p->push_back(cand); continue;}
99 
100 
101  if (hasNonNullMuonRef) {
102  reco::MuonRef muRef = (*muonMap)[cand.muonRef()];
103  cand.setMuonRef(muRef);
104  muonCandidateMap[muRef] = candPtr;
105  }
106 
107 
108  // if it is an electron. Find the GsfElectron with the same GsfTrack
109  if (iselectron) {
110  const reco::GsfTrackRef & gsfTrackRef(cand.gsfTrackRef());
111  auto itcheck = find_if( gsfElectrons->begin(),gsfElectrons->end(),
112  [&gsfTrackRef](const auto& ele) {return (ele.gsfTrack()==gsfTrackRef);} );
113  if(itcheck==gsfElectrons->end()) {
114  if (!forceElectronsInHGCAL_) {
115  std::ostringstream err;
116  err << " Problem in PFLinker: no GsfElectron " << std::endl;
117  edm::LogError("PFLinker") << err.str();
118  } else {
119  LogDebug("PFLinker")
120  << "Forcing an electron pfCandidate at: " << cand.eta()
121  << " in HGCAL" << std::endl;
122  pfCandidates_p->push_back(cand);
123  }
124  continue; // Watch out ! Continue
125  }
126  reco::GsfElectronRef electronRef(gsfElectrons,itcheck-gsfElectrons->begin());
127  cand.setGsfElectronRef(electronRef);
128  cand.setSuperClusterRef(electronRef->superCluster());
129  // update energy information since now it is done post-particleFlowTmp
130  cand.setEcalEnergy(electronRef->superCluster()->rawEnergy(),electronRef->ecalEnergy());
131  cand.setDeltaP(electronRef->p4Error(reco::GsfElectron::P4_COMBINATION));
132  cand.setP4(electronRef->p4(reco::GsfElectron::P4_COMBINATION));
133  electronCandidateMap[electronRef] = candPtr;
134  }
135 
136  // if it is a photon, find the one with the same PF super-cluster
137  if (isphoton) {
138  const reco::SuperClusterRef & scRef(cand.superClusterRef());
139  auto itcheck = find_if( photons->begin(),photons->end(),
140  [&scRef](const auto& photon) {return photon.superCluster() == scRef;} );
141  if(itcheck==photons->end()) {
142  std::ostringstream err;
143  err << " Problem in PFLinker: no Photon " << std::endl;
144  edm::LogError("PFLinker") << err.str();
145  continue; // Watch out ! Continue
146  }
147  reco::PhotonRef photonRef(photons,itcheck-photons->begin());
148  cand.setPhotonRef(photonRef);
149  cand.setSuperClusterRef(photonRef->superCluster());
150  // update energy information since now it is done post-particleFlowTmp
151  cand.setEcalEnergy(photonRef->superCluster()->rawEnergy(),
152  photonRef->getCorrectedEnergy(reco::Photon::regression2));
153  cand.setDeltaP(photonRef->getCorrectedEnergyError(reco::Photon::regression2));
154  cand.setP4(photonRef->p4(reco::Photon::regression2));
155  photonCandidateMap[photonRef] = candPtr;
156  }
157 
158  pfCandidates_p->push_back(cand);
159  }
160  // save the PFCandidates and get a valid handle
161  }
162  const edm::OrphanHandle<reco::PFCandidateCollection> pfCandidateRefProd = (producePFCandidates_) ? iEvent.put(std::move(pfCandidates_p),nameOutputPF_) :
164 
165 
166  // now make the valuemaps
167 
168  edm::ValueMap<reco::PFCandidatePtr> pfMapGsfElectrons = fillValueMap<reco::GsfElectronCollection>(iEvent,
170  gsfElectrons,
171  electronCandidateMap,
172  pfCandidateRefProd);
173 
174  edm::ValueMap<reco::PFCandidatePtr> pfMapPhotons = fillValueMap<reco::PhotonCollection>(iEvent,
176  photons,
177  photonCandidateMap,
178  pfCandidateRefProd);
179 
180 
182 
183  if(fillMuonRefs_){
184  auto muons = iEvent.getHandle(inputTagMuons_);
185 
186  pfMapMuons = fillValueMap<reco::MuonCollection>(iEvent,
187  muonTag_.label(),
188  muons,
189  muonCandidateMap,
190  pfCandidateRefProd);
191  }
192 
193  auto pfMapMerged = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
194  edm::ValueMap<reco::PFCandidatePtr>::Filler pfMapMergedFiller(*pfMapMerged);
195 
196  *pfMapMerged += pfMapGsfElectrons;
197  *pfMapMerged += pfMapPhotons;
198  if(fillMuonRefs_) *pfMapMerged += pfMapMuons;
199 
200  iEvent.put(std::move(pfMapMerged),nameOutputMergedPF_);
201 }
202 
203 
204 
205 template<typename TYPE>
208  edm::Handle<TYPE>& inputObjCollection,
209  const std::map<edm::Ref<TYPE>, reco::PFCandidatePtr> & mapToTheCandidate,
210  const edm::OrphanHandle<reco::PFCandidateCollection> & newPFCandColl) const {
211 
212  auto pfMap_p = std::make_unique<edm::ValueMap<reco::PFCandidatePtr>>();
214 
215  typedef typename std::map<edm::Ref<TYPE>, reco::PFCandidatePtr>::const_iterator MapTYPE_it;
216 
217  unsigned nObj=inputObjCollection->size();
218  std::vector<reco::PFCandidatePtr> values(nObj);
219 
220  for(unsigned iobj=0; iobj < nObj; ++iobj) {
221 
222  edm::Ref<TYPE> objRef(inputObjCollection, iobj);
223  MapTYPE_it itcheck = mapToTheCandidate.find(objRef);
224 
225  reco::PFCandidatePtr candPtr;
226 
227  if(itcheck != mapToTheCandidate.end())
228  candPtr = producePFCandidates_ ? reco::PFCandidatePtr(newPFCandColl,itcheck->second.key()) : itcheck->second;
229 
230  values[iobj] = candPtr;
231  }
232 
233  filler.insert(inputObjCollection,values.begin(),values.end());
234  filler.fill();
235  edm::ValueMap<reco::PFCandidatePtr> returnValue = *pfMap_p;
236  event.put(std::move(pfMap_p),label);
237  return returnValue;
238 }
#define LogDebug(id)
T getParameter(std::string const &) const
edm::InputTag muonTag_
Input Muons.
Definition: PFLinker.h:63
void setDeltaP(double dp)
set uncertainty on momentum
Definition: PFCandidate.h:295
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
bool isNonnull() const
Checks for non-null.
Definition: Ref.h:251
double eta() const final
momentum pseudorapidity
void setGsfElectronRef(const reco::GsfElectronRef &ref)
set GsfElectronRef
Definition: PFCandidate.cc:578
edm::EDGetTokenT< reco::MuonCollection > inputTagMuons_
Definition: PFLinker.h:64
float mva_nothing_gamma() const
mva for gamma detection
Definition: PFCandidate.h:333
void produce(edm::Event &, const edm::EventSetup &) override
Definition: PFLinker.cc:64
bool producePFCandidates_
Flags - if true: References will be towards new collection ; if false to the original one...
Definition: PFLinker.h:79
edm::EDGetTokenT< reco::PhotonCollection > inputTagPhotons_
Input Photons.
Definition: PFLinker.h:60
PFLinker(const edm::ParameterSet &)
Definition: PFLinker.cc:8
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:206
edm::EDGetTokenT< reco::MuonToMuonMap > inputTagMuonMap_
Definition: PFLinker.h:65
char const * label
Handle< PROD > getHandle(EDGetTokenT< PROD > token) const
Definition: Event.h:539
int iEvent
Definition: GenABIO.cc:224
std::string nameOutputElectronsPF_
name of output ValueMap electrons
Definition: PFLinker.h:70
std::string nameOutputPhotonsPF_
name of output ValueMap photons
Definition: PFLinker.h:73
std::string nameOutputPF_
name of output collection of PFCandidate
Definition: PFLinker.h:67
reco::MuonRef muonRef() const
Definition: PFCandidate.cc:459
void setEcalEnergy(float eeRaw, float eeCorr)
set corrected Ecal energy
Definition: PFCandidate.h:218
bool fillMuonRefs_
Set muon refs and produce the value map?
Definition: PFLinker.h:82
void setPhotonRef(const reco::PhotonRef &phRef)
set ref to the corresponding reco::Photon if any
Definition: PFCandidate.cc:613
std::string const & label() const
Definition: InputTag.h:36
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:40
void setMuonRef(const reco::MuonRef &ref)
set muon reference
Definition: PFCandidate.cc:445
bool forceElectronsInHGCAL_
Put Electrons within HGCAL coming from SimPFProducer.
Definition: PFLinker.h:85
reco::GsfTrackRef gsfTrackRef() const
Definition: PFCandidate.cc:480
void setSuperClusterRef(const reco::SuperClusterRef &scRef)
Definition: PFCandidate.cc:629
virtual ParticleType particleId() const
Definition: PFCandidate.h:374
~PFLinker() override
Definition: PFLinker.cc:62
edm::EDGetTokenT< reco::GsfElectronCollection > inputTagGsfElectrons_
Input GsfElectrons.
Definition: PFLinker.h:57
void setP4(const LorentzVector &p4) final
set 4-momentum
def move(src, dest)
Definition: eostools.py:511
std::vector< edm::EDGetTokenT< reco::PFCandidateCollection > > inputTagPFCandidates_
Input PFCandidates.
Definition: PFLinker.h:54
Definition: event.py:1
std::string nameOutputMergedPF_
name of output merged ValueMap
Definition: PFLinker.h:76
reco::SuperClusterRef superClusterRef() const
return a reference to the corresponding SuperCluster if any
Definition: PFCandidate.cc:609