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 
17 
19 
28 
31 
34 #include <iostream>
35 
36 struct HEChannel {
37  float eta;
38  float phi;
39  float ratio;
40  HEChannel(float eta, float phi, float ratio) : eta(eta), phi(phi), ratio(ratio) {}
41 };
42 struct HFChannel {
43  int ieta;
44  int iphi;
45  int depth;
46  float ratio;
47  HFChannel(int ieta, int iphi, int depth, float ratio) : ieta(ieta), iphi(iphi), depth(depth), ratio(ratio) {}
48 };
49 
51 public:
54 
55  static void fillDescriptions(edm::ConfigurationDescriptions& descriptions);
56 
57 private:
58  void beginRun(const edm::Run& iRun, edm::EventSetup const& iSetup) override;
59  void endRun(const edm::Run& iRun, edm::EventSetup const& iSetup) override;
60  void produce(edm::Event&, const edm::EventSetup&) override;
61 
64 
66 
72 
73  std::vector<HEChannel> badChHE_;
74  std::vector<HFChannel> badChHF_;
75 
78 };
79 
81  : pfcandidates_(consumes<reco::PFCandidateCollection>(iConfig.getParameter<edm::InputTag>("pfcandidates"))),
82  gtCondToken_(esConsumes<edm::Transition::BeginRun>()),
83  htopoToken_(esConsumes<edm::Transition::BeginRun>()),
84  buggedCondToken_(esConsumes<edm::Transition::BeginRun>(edm::ESInputTag("", "bugged"))),
85  calogeomTokenRun_(esConsumes<edm::Transition::BeginRun>()),
86  calogeomTokenEvent_(esConsumes()),
87  shortFibreThr_(iConfig.getParameter<double>("shortFibreThr")),
88  longFibreThr_(iConfig.getParameter<double>("longFibreThr")) {
89  produces<reco::PFCandidateCollection>();
90  produces<reco::PFCandidateCollection>("discarded");
91  produces<edm::ValueMap<reco::PFCandidateRef>>();
92 }
93 
95  if (hcalDbWatcher_.check(iSetup) || hcalRCWatcher_.check(iSetup)) {
96  //Get Calib Constants from current GT
97  HcalDbService const& gtCond = iSetup.getData(gtCondToken_);
98 
99  //Get Calib Constants from bugged tag
100  const HcalTopology& theHBHETopology = iSetup.getData(htopoToken_);
101 
102  HcalRespCorrs buggedRespCorrs = iSetup.getData(buggedCondToken_);
103  buggedRespCorrs.setTopo(&theHBHETopology);
104 
105  //access calogeometry
106  const CaloGeometry& cgeo = iSetup.getData(calogeomTokenRun_);
107  const HcalGeometry* hgeom = static_cast<const HcalGeometry*>(cgeo.getSubdetectorGeometry(DetId::Hcal, HcalForward));
108 
109  //reset the bad channel containers
110  badChHE_.clear();
111  badChHF_.clear();
112 
113  //fill bad cells HE (use eta, phi)
114  const std::vector<DetId>& cellsHE = hgeom->getValidDetIds(DetId::Detector::Hcal, HcalEndcap);
115  for (auto id : cellsHE) {
116  float currentRespCorr = gtCond.getHcalRespCorr(id)->getValue();
117  float buggedRespCorr = buggedRespCorrs.getValues(id)->getValue();
118  if (buggedRespCorr == 0.)
119  continue;
120 
121  float ratio = currentRespCorr / buggedRespCorr;
122  if (std::abs(ratio - 1.f) > 0.001) {
123  GlobalPoint pos = hgeom->getPosition(id);
124  badChHE_.push_back(HEChannel(pos.eta(), pos.phi(), ratio));
125  }
126  }
127 
128  //fill bad cells HF (use ieta, iphi)
129  auto const& cellsHF = hgeom->getValidDetIds(DetId::Detector::Hcal, HcalForward);
130  for (auto id : cellsHF) {
131  float currentRespCorr = gtCond.getHcalRespCorr(id)->getValue();
132  float buggedRespCorr = buggedRespCorrs.getValues(id)->getValue();
133  if (buggedRespCorr == 0.)
134  continue;
135 
136  float ratio = currentRespCorr / buggedRespCorr;
137  if (std::abs(ratio - 1.f) > 0.001) {
138  HcalDetId dummyId(id);
139  badChHF_.push_back(HFChannel(dummyId.ieta(), dummyId.iphi(), dummyId.depth(), ratio));
140  }
141  }
142  }
143 }
144 
145 void PFCandidateRecalibrator::endRun(const edm::Run& iRun, const edm::EventSetup& iSetup) {}
146 
148  //access calogeometry
149  const CaloGeometry& cgeo = iSetup.getData(calogeomTokenEvent_);
150  const HcalGeometry* hgeom = static_cast<const HcalGeometry*>(cgeo.getSubdetectorGeometry(DetId::Hcal, HcalForward));
151 
152  //access PFCandidates
154  iEvent.getByToken(pfcandidates_, pfcandidates);
155 
156  int nPfCand = pfcandidates->size();
157  std::unique_ptr<reco::PFCandidateCollection> copy(new reco::PFCandidateCollection());
158  std::unique_ptr<reco::PFCandidateCollection> discarded(new reco::PFCandidateCollection());
159  copy->reserve(nPfCand);
160  std::vector<int> oldToNew(nPfCand), newToOld, badToOld;
161  newToOld.reserve(nPfCand);
162 
163  LogDebug("PFCandidateRecalibrator") << "NEW EV:";
164 
165  //loop over PFCandidates
166  int i = -1;
167  for (const reco::PFCandidate& pf : *pfcandidates) {
168  ++i;
169  float absEta = std::abs(pf.eta());
170 
171  //deal with HE
172  if (pf.particleId() == reco::PFCandidate::ParticleType::h0 &&
173  !badChHE_.empty() && //don't touch if no miscalibration is found
174  absEta > 1.4 && absEta < 3.) {
175  bool toKill = false;
176  for (auto const& badIt : badChHE_)
177  if (reco::deltaR2(pf.eta(), pf.phi(), badIt.eta, badIt.phi) < 0.07)
178  toKill = true;
179 
180  if (toKill) {
181  discarded->push_back(pf);
182  oldToNew[i] = (-discarded->size());
183  badToOld.push_back(i);
184  continue;
185  } else {
186  copy->push_back(pf);
187  oldToNew[i] = (copy->size());
188  newToOld.push_back(i);
189  }
190  }
191  //deal with HF
192  else if ((pf.particleId() == reco::PFCandidate::ParticleType::h_HF ||
193  pf.particleId() == reco::PFCandidate::ParticleType::egamma_HF) &&
194  !badChHF_.empty() && //don't touch if no miscalibration is found
195  absEta >= 3.) {
196  const math::XYZPointF& ecalPoint = pf.positionAtECALEntrance();
197  GlobalPoint ecalGPoint(ecalPoint.X(), ecalPoint.Y(), ecalPoint.Z());
198  HcalDetId closestDetId(hgeom->getClosestCell(ecalGPoint));
199 
200  if (closestDetId.subdet() == HcalForward) {
201  HcalDetId hDetId(closestDetId.subdet(), closestDetId.ieta(), closestDetId.iphi(), 1); //depth1
202 
203  //raw*calEnergy() is the same as *calEnergy() - no corrections are done for HF
204  float longE = pf.rawEcalEnergy() + pf.rawHcalEnergy() / 2.; //depth1
205  float shortE = pf.rawHcalEnergy() / 2.; //depth2
206 
207  float ecalEnergy = pf.rawEcalEnergy();
208  float hcalEnergy = pf.rawHcalEnergy();
209  float totEnergy = ecalEnergy + hcalEnergy;
210  float totEnergyOrig = totEnergy;
211 
212  bool toKill = false;
213 
214  for (auto const& badIt : badChHF_) {
215  if (hDetId.ieta() == badIt.ieta && hDetId.iphi() == badIt.iphi) {
216  LogDebug("PFCandidateRecalibrator")
217  << "==> orig en (tot,H,E): " << pf.energy() << " " << pf.rawHcalEnergy() << " " << pf.rawEcalEnergy();
218  if (badIt.depth == 1) //depth1
219  {
220  longE *= badIt.ratio;
221  ecalEnergy = ((longE - shortE) > 0.) ? (longE - shortE) : 0.;
222  totEnergy = ecalEnergy + hcalEnergy;
223  } else //depth2
224  {
225  shortE *= badIt.ratio;
226  hcalEnergy = 2 * shortE;
227  ecalEnergy = ((longE - shortE) > 0.) ? (longE - shortE) : 0.;
228  totEnergy = ecalEnergy + hcalEnergy;
229  }
230  //kill candidate if goes below thr
231  if ((pf.particleId() == reco::PFCandidate::ParticleType::h_HF && shortE < shortFibreThr_) ||
232  (pf.particleId() == reco::PFCandidate::ParticleType::egamma_HF && longE < longFibreThr_))
233  toKill = true;
234 
235  LogDebug("PFCandidateRecalibrator") << "====> ieta,iphi,depth: " << badIt.ieta << " " << badIt.iphi << " "
236  << badIt.depth << " corr: " << badIt.ratio;
237  LogDebug("PFCandidateRecalibrator")
238  << "====> recal en (tot,H,E): " << totEnergy << " " << hcalEnergy << " " << ecalEnergy;
239  }
240  }
241 
242  if (toKill) {
243  discarded->push_back(pf);
244  oldToNew[i] = (-discarded->size());
245  badToOld.push_back(i);
246 
247  LogDebug("PFCandidateRecalibrator") << "==> KILLED ";
248  } else {
249  copy->push_back(pf);
250  oldToNew[i] = (copy->size());
251  newToOld.push_back(i);
252 
253  copy->back().setHcalEnergy(hcalEnergy, hcalEnergy);
254  copy->back().setEcalEnergy(ecalEnergy, ecalEnergy);
255 
256  float scalingFactor = totEnergy / totEnergyOrig;
257  math::XYZTLorentzVector recalibP4 = pf.p4() * scalingFactor;
258  copy->back().setP4(recalibP4);
259 
260  LogDebug("PFCandidateRecalibrator") << "====> stored en (tot,H,E): " << copy->back().energy() << " "
261  << copy->back().hcalEnergy() << " " << copy->back().ecalEnergy();
262  }
263  } else {
264  copy->push_back(pf);
265  oldToNew[i] = (copy->size());
266  newToOld.push_back(i);
267  }
268  } else {
269  copy->push_back(pf);
270  oldToNew[i] = (copy->size());
271  newToOld.push_back(i);
272  }
273  }
274 
275  // Now we put things in the event
277  edm::OrphanHandle<reco::PFCandidateCollection> badpf = iEvent.put(std::move(discarded), "discarded");
278 
279  std::unique_ptr<edm::ValueMap<reco::PFCandidateRef>> pf2pf(new edm::ValueMap<reco::PFCandidateRef>());
281  std::vector<reco::PFCandidateRef> refs;
282  refs.reserve(nPfCand);
283 
284  // old to new
285  for (auto iOldToNew : oldToNew) {
286  if (iOldToNew > 0) {
287  refs.push_back(reco::PFCandidateRef(newpf, iOldToNew - 1));
288  } else {
289  refs.push_back(reco::PFCandidateRef(badpf, -iOldToNew - 1));
290  }
291  }
292  filler.insert(pfcandidates, refs.begin(), refs.end());
293  // new good to old
294  refs.clear();
295  for (int i : newToOld) {
296  refs.push_back(reco::PFCandidateRef(pfcandidates, i));
297  }
298  filler.insert(newpf, refs.begin(), refs.end());
299  // new bad to old
300  refs.clear();
301  for (int i : badToOld) {
302  refs.push_back(reco::PFCandidateRef(pfcandidates, i));
303  }
304  filler.insert(badpf, refs.begin(), refs.end());
305  // done
306  filler.fill();
307  iEvent.put(std::move(pf2pf));
308 }
309 
312 
313  desc.add<edm::InputTag>("pfcandidates", edm::InputTag("particleFlow"));
314  desc.add<double>("shortFibreThr", 1.4);
315  desc.add<double>("longFibreThr", 1.4);
316 
317  descriptions.add("pfCandidateRecalibrator", desc);
318 }
319 
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
void produce(edm::Event &, const edm::EventSetup &) override
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > calogeomTokenRun_
ESGetTokenH3DDVariant esConsumes(std::string const &Record, edm::ConsumesCollector &)
Definition: DeDxTools.cc:283
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
DetId getClosestCell(const GlobalPoint &r) const override
edm::ESWatcher< HcalRecNumberingRecord > hcalDbWatcher_
std::vector< l1t::PFCandidate > PFCandidateCollection
Definition: PFCandidate.h:82
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 &)
const Item * getValues(DetId fId, bool throwOnFail=true) const
HEChannel(float eta, float phi, float ratio)
XYZTLorentzVectorD XYZTLorentzVector
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:29
const edm::ESGetToken< HcalRespCorrs, HcalRespCorrsRcd > buggedCondToken_
int iEvent
Definition: GenABIO.cc:224
const HcalRespCorr * getHcalRespCorr(const HcalGenericDetId &fId) const
constexpr int ieta() const
get the cell ieta
Definition: HcalDetId.h:155
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
Transition
Definition: Transition.h:12
double f[11][100]
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
bool getData(T &iHolder) const
Definition: EventSetup.h:122
const edm::ESGetToken< CaloGeometry, CaloGeometryRecord > calogeomTokenEvent_
edm::ESWatcher< HcalRespCorrsRcd > hcalRCWatcher_
std::vector< reco::PFCandidate > PFCandidateCollection
collection of PFCandidates
constexpr auto deltaR2(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:16
float getValue() const
Definition: HcalRespCorr.h:19
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_
const edm::ESGetToken< HcalTopology, HcalRecNumberingRecord > htopoToken_
void beginRun(const edm::Run &iRun, edm::EventSetup const &iSetup) override
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
fixed size matrix
HLT enums.
GlobalPoint getPosition(const DetId &id) const
const edm::ESGetToken< HcalDbService, HcalDbRecord > gtCondToken_
const CaloSubdetectorGeometry * getSubdetectorGeometry(const DetId &id) const
access the subdetector geometry for the given subdetector directly
Definition: CaloGeometry.cc:34
constexpr int iphi() const
get the cell iphi
Definition: HcalDetId.h:157
def move(src, dest)
Definition: eostools.py:511
void setTopo(const HcalTopology *topo)
Definition: Run.h:45
#define LogDebug(id)
constexpr int depth() const
get the tower depth
Definition: HcalDetId.h:164