CMS 3D CMS Logo

PFCandidateRecalibrator.cc
Go to the documentation of this file.
1 // - a PF candidate collection (which uses bugged HCAL respcorrs)
3 // - respCorrs values from fixed GT and bugged GT
4 // Produce:
5 // - a new PFCandidate collection containing the recalibrated PFCandidates in HF and where the neutral had pointing to problematic cells are removed
6 // - a second PFCandidate collection with just those discarded hadrons
7 // - a ValueMap<reco::PFCandidateRef> that maps the old to the new, and vice-versa
8 
15 
17 
26 
29 
32 #include <iostream>
33 
34 
35 struct HEChannel {
36  float eta; float phi; float ratio;
37  HEChannel(float eta, float phi, float ratio):
38  eta(eta),
39  phi(phi),
40  ratio(ratio)
41  {}
42 };
43 struct HFChannel {
44  int ieta; int iphi; int depth; float ratio;
45  HFChannel(int ieta, int iphi, int depth, float ratio):
46  ieta(ieta),
47  iphi(iphi),
48  depth(depth),
49  ratio(ratio)
50  {}
51 };
52 
53 
55  public:
57  ~PFCandidateRecalibrator() override {};
58 
59  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
60 
61  private:
62  void beginRun(const edm::Run& iRun, edm::EventSetup const& iSetup) override;
63  void endRun(const edm::Run& iRun, edm::EventSetup const& iSetup) override;
64  void produce(edm::Event&, const edm::EventSetup&) override;
65 
68 
70 
71  std::vector<HEChannel> badChHE_;
72  std::vector<HFChannel> badChHF_;
73 
76 };
77 
79  pfcandidates_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("pfcandidates"))),
80  shortFibreThr_(iConfig.getParameter<double>("shortFibreThr")),
81  longFibreThr_(iConfig.getParameter<double>("longFibreThr"))
82 {
83  produces<reco::PFCandidateCollection>();
84  produces<reco::PFCandidateCollection>("discarded");
85  produces<edm::ValueMap<reco::PFCandidateRef>>();
86 }
87 
89 {
90  if (hcalDbWatcher_.check(iSetup) || hcalRCWatcher_.check(iSetup))
91  {
92  //Get Calib Constants from current GT
94  iSetup.get<HcalDbRecord>().get(gtCond);
95 
96  //Get Calib Constants from bugged tag
98  iSetup.get<HcalRecNumberingRecord>().get(htopo);
99  const HcalTopology* theHBHETopology = htopo.product();
100 
101  edm::ESHandle<HcalRespCorrs> buggedCond;
102  iSetup.get<HcalRespCorrsRcd>().get("bugged", buggedCond);
103  HcalRespCorrs buggedRespCorrs(*buggedCond.product());
104  buggedRespCorrs.setTopo(theHBHETopology);
105 
106  //access calogeometry
108  iSetup.get<CaloGeometryRecord>().get(calogeom);
109  const CaloGeometry* cgeo = calogeom.product();
110  const HcalGeometry* hgeom = static_cast<const HcalGeometry*>(cgeo->getSubdetectorGeometry(DetId::Hcal,HcalForward));
111 
112  //reset the bad channel containers
113  badChHE_.clear();
114  badChHF_.clear();
115 
116  //fill bad cells HE (use eta, phi)
117  const std::vector<DetId>& cellsHE = hgeom->getValidDetIds(DetId::Detector::Hcal, HcalEndcap);
118  for( auto id : cellsHE)
119  {
120  float currentRespCorr = gtCond->getHcalRespCorr(id)->getValue();
121  float buggedRespCorr = buggedRespCorrs.getValues(id)->getValue();
122  if (buggedRespCorr == 0.)
123  continue;
124 
125  float ratio = currentRespCorr/buggedRespCorr;
126  if( std::abs(ratio - 1.f) > 0.001 )
127  {
128  GlobalPoint pos = hgeom->getPosition(id);
129  badChHE_.push_back(HEChannel(pos.eta(),pos.phi(),ratio));
130  }
131  }
132 
133  //fill bad cells HF (use ieta, iphi)
134  auto const& cellsHF = hgeom->getValidDetIds(DetId::Detector::Hcal, HcalForward);
135  for( auto id : cellsHF)
136  {
137  float currentRespCorr = gtCond->getHcalRespCorr(id)->getValue();
138  float buggedRespCorr = buggedRespCorrs.getValues(id)->getValue();
139  if (buggedRespCorr == 0.)
140  continue;
141 
142  float ratio = currentRespCorr/buggedRespCorr;
143  if( std::abs(ratio - 1.f) > 0.001 )
144  {
145  HcalDetId dummyId(id);
146  badChHF_.push_back(HFChannel(dummyId.ieta(), dummyId.iphi(), dummyId.depth(), ratio));
147  }
148  }
149  }
150 }
151 
153 {
154 }
155 
157 {
158  //access calogeometry
160  iSetup.get<CaloGeometryRecord>().get(calogeom);
161  const CaloGeometry* cgeo = calogeom.product();
162  const HcalGeometry* hgeom = static_cast<const HcalGeometry*>(cgeo->getSubdetectorGeometry(DetId::Hcal,HcalForward));
163 
164  //access PFCandidates
166  iEvent.getByToken(pfcandidates_, pfcandidates);
167 
168  int nPfCand = pfcandidates->size();
169  std::unique_ptr<reco::PFCandidateCollection> copy(new reco::PFCandidateCollection());
170  std::unique_ptr<reco::PFCandidateCollection> discarded(new reco::PFCandidateCollection());
171  copy->reserve(nPfCand);
172  std::vector<int> oldToNew(nPfCand), newToOld, badToOld;
173  newToOld.reserve(nPfCand);
174 
175  LogDebug("PFCandidateRecalibrator") << "NEW EV:";
176 
177  //loop over PFCandidates
178  int i = -1;
179  for (const reco::PFCandidate &pf : *pfcandidates)
180  {
181  ++i;
182  float absEta = std::abs(pf.eta());
183 
184  //deal with HE
185  if( pf.particleId() == reco::PFCandidate::ParticleType::h0 &&
186  !badChHE_.empty() && //don't touch if no miscalibration is found
187  absEta > 1.4 && absEta < 3.)
188  {
189  bool toKill = false;
190  for(auto const& badIt: badChHE_)
191  if( reco::deltaR2(pf.eta(), pf.phi(), badIt.eta, badIt.phi) < 0.07 )
192  toKill = true;
193 
194 
195  if(toKill)
196  {
197  discarded->push_back(pf);
198  oldToNew[i] = (-discarded->size());
199  badToOld.push_back(i);
200  continue;
201  }
202  else
203  {
204  copy->push_back(pf);
205  oldToNew[i] = (copy->size());
206  newToOld.push_back(i);
207  }
208  }
209  //deal with HF
210  else if( (pf.particleId() == reco::PFCandidate::ParticleType::h_HF || pf.particleId() == reco::PFCandidate::ParticleType::egamma_HF) &&
211  !badChHF_.empty() && //don't touch if no miscalibration is found
212  absEta >= 3.)
213  {
214  const math::XYZPointF& ecalPoint = pf.positionAtECALEntrance();
215  GlobalPoint ecalGPoint(ecalPoint.X(),ecalPoint.Y(),ecalPoint.Z());
216  HcalDetId closestDetId(hgeom->getClosestCell(ecalGPoint));
217 
218  if(closestDetId.subdet() == HcalForward)
219  {
220  HcalDetId hDetId(closestDetId.subdet(),closestDetId.ieta(),closestDetId.iphi(),1); //depth1
221 
222  //raw*calEnergy() is the same as *calEnergy() - no corrections are done for HF
223  float longE = pf.rawEcalEnergy() + pf.rawHcalEnergy()/2.; //depth1
224  float shortE = pf.rawHcalEnergy()/2.; //depth2
225 
226  float ecalEnergy = pf.rawEcalEnergy();
227  float hcalEnergy = pf.rawHcalEnergy();
228  float totEnergy = ecalEnergy + hcalEnergy;
229  float totEnergyOrig = totEnergy;
230 
231  bool toKill = false;
232 
233  for(auto const& badIt: badChHF_)
234  {
235  if ( hDetId.ieta() == badIt.ieta &&
236  hDetId.iphi() == badIt.iphi )
237  {
238  LogDebug("PFCandidateRecalibrator") << "==> orig en (tot,H,E): "
239  << pf.energy() << " " << pf.rawHcalEnergy() << " " << pf.rawEcalEnergy();
240  if(badIt.depth == 1) //depth1
241  {
242  longE *= badIt.ratio;
243  ecalEnergy = ((longE - shortE) > 0.) ? (longE - shortE) : 0.;
244  totEnergy = ecalEnergy + hcalEnergy;
245  }
246  else //depth2
247  {
248  shortE *= badIt.ratio;
249  hcalEnergy = 2*shortE;
250  ecalEnergy = ((longE - shortE) > 0.) ? (longE - shortE) : 0.;
251  totEnergy = ecalEnergy + hcalEnergy;
252  }
253  //kill candidate if goes below thr
254  if((pf.particleId() == reco::PFCandidate::ParticleType::h_HF && shortE < shortFibreThr_) ||
255  (pf.particleId() == reco::PFCandidate::ParticleType::egamma_HF && longE < longFibreThr_))
256  toKill = true;
257 
258  LogDebug("PFCandidateRecalibrator") << "====> ieta,iphi,depth: "
259  << badIt.ieta << " " << badIt.iphi << " " << badIt.depth << " corr: " << badIt.ratio;
260  LogDebug("PFCandidateRecalibrator") << "====> recal en (tot,H,E): "
261  << totEnergy << " " << hcalEnergy << " " << ecalEnergy;
262 
263  }
264  }
265 
266  if(toKill)
267  {
268  discarded->push_back(pf);
269  oldToNew[i] = (-discarded->size());
270  badToOld.push_back(i);
271 
272  LogDebug("PFCandidateRecalibrator") << "==> KILLED ";
273  }
274  else
275  {
276  copy->push_back(pf);
277  oldToNew[i] = (copy->size());
278  newToOld.push_back(i);
279 
280  copy->back().setHcalEnergy(hcalEnergy, hcalEnergy);
281  copy->back().setEcalEnergy(ecalEnergy, ecalEnergy);
282 
283  float scalingFactor = totEnergy/totEnergyOrig;
284  math::XYZTLorentzVector recalibP4 = pf.p4() * scalingFactor;
285  copy->back().setP4( recalibP4 );
286 
287  LogDebug("PFCandidateRecalibrator") << "====> stored en (tot,H,E): "
288  << copy->back().energy() << " " << copy->back().hcalEnergy() << " " << copy->back().ecalEnergy();
289  }
290  }
291  else
292  {
293  copy->push_back(pf);
294  oldToNew[i] = (copy->size());
295  newToOld.push_back(i);
296  }
297  }
298  else
299  {
300  copy->push_back(pf);
301  oldToNew[i] = (copy->size());
302  newToOld.push_back(i);
303  }
304  }
305 
306  // Now we put things in the event
308  edm::OrphanHandle<reco::PFCandidateCollection> badpf = iEvent.put(std::move(discarded), "discarded");
309 
310  std::unique_ptr<edm::ValueMap<reco::PFCandidateRef>> pf2pf(new edm::ValueMap<reco::PFCandidateRef>());
312  std::vector<reco::PFCandidateRef> refs; refs.reserve(nPfCand);
313 
314  // old to new
315  for (auto iOldToNew : oldToNew){
316  if (iOldToNew > 0) {
317  refs.push_back(reco::PFCandidateRef(newpf, iOldToNew-1));
318  } else {
319  refs.push_back(reco::PFCandidateRef(badpf,-iOldToNew-1));
320  }
321  }
322  filler.insert(pfcandidates, refs.begin(), refs.end());
323  // new good to old
324  refs.clear();
325  for (int i : newToOld) {
326  refs.push_back(reco::PFCandidateRef(pfcandidates,i));
327  }
328  filler.insert(newpf, refs.begin(), refs.end());
329  // new bad to old
330  refs.clear();
331  for (int i : badToOld) {
332  refs.push_back(reco::PFCandidateRef(pfcandidates,i));
333  }
334  filler.insert(badpf, refs.begin(), refs.end());
335  // done
336  filler.fill();
337  iEvent.put(std::move(pf2pf));
338 }
339 
340 void
342 {
344 
345  desc.add<edm::InputTag>("pfcandidates", edm::InputTag("particleFlow"));
346  desc.add<double>("shortFibreThr", 1.4);
347  desc.add<double>("longFibreThr", 1.4);
348 
349  descriptions.add("pfCandidateRecalibrator", desc);
350 
351 }
352 
#define LogDebug(id)
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:45
void produce(edm::Event &, const edm::EventSetup &) override
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:127
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:508
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::ESWatcher< HcalRecNumberingRecord > hcalDbWatcher_
const std::vector< DetId > & getValidDetIds(DetId::Detector det=DetId::Detector(0), int subdet=0) const override
Get a list of valid detector ids (for the given subdetector)
Definition: HcalGeometry.cc:76
Geom::Phi< T > phi() const
Definition: PV3DBase.h:69
void insert(const H &h, I begin, I end)
Definition: ValueMap.h:53
void endRun(const edm::Run &iRun, edm::EventSetup const &iSetup) override
std::vector< HEChannel > badChHE_
ROOT::Math::PositionVector3D< ROOT::Math::Cartesian3D< float > > XYZPointF
point in space with cartesian internal representation
Definition: Point3D.h:10
PFCandidateRecalibrator(const edm::ParameterSet &)
HEChannel(float eta, float phi, float ratio)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
const HcalRespCorr * getHcalRespCorr(const HcalGenericDetId &fId) const
int depth() const
get the tower depth
Definition: HcalDetId.cc:129
int iEvent
Definition: GenABIO.cc:230
int ieta() const
get the cell ieta
Definition: HcalDetId.h:56
def oldToNew(schema)
Definition: lumidbDDL.py:399
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
double f[11][100]
ParameterDescriptionBase * add(U const &iLabel, T const &value)
GlobalPoint getPosition(const DetId &id) const
edm::ESWatcher< HcalRespCorrsRcd > hcalRCWatcher_
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:124
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
const T & get() const
Definition: EventSetup.h:55
std::vector< HFChannel > badChHF_
void add(std::string const &label, ParameterSetDescription const &psetDescription)
HFChannel(int ieta, int iphi, int depth, float ratio)
bool check(const edm::EventSetup &iSetup)
Definition: ESWatcher.h:57
edm::EDGetTokenT< reco::PFCandidateCollection > pfcandidates_
T1 deltaR2(T1 eta1, T2 phi1, T3 eta2, T4 phi2)
Definition: deltaR.h:36
T eta() const
Definition: PV3DBase.h:76
void beginRun(const edm::Run &iRun, edm::EventSetup const &iSetup) override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
fixed size matrix
HLT enums.
float getValue() const
Definition: HcalRespCorr.h:20
def newToOld(schema)
Definition: lumidbDDL.py:413
T const * product() const
Definition: ESHandle.h:86
def move(src, dest)
Definition: eostools.py:510
void setTopo(const HcalTopology *topo)
DetId getClosestCell(const GlobalPoint &r) const override
Definition: Run.h:43