CMS 3D CMS Logo

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