CMS 3D CMS Logo

EcalDeadCellDeltaRFilter.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: EcalDeadCellDeltaRFilter
4 // Class: EcalDeadCellDeltaRFilter
5 //
11 //
12 // Original Author: Hongxuan Liu
13 //
14 
15 // system include files
16 #include <memory>
17 
18 // user include files
21 
22 // XXX: Must BEFORE Frameworkfwd.h
25 
29 
32 
34 
40 
43 
47 
51 
55 
56 // Geometry
61 
66 
69 
71 
77 
81 
83 
84 // HCAL
89 
91 
92 #include "TFile.h"
93 #include "TTree.h"
94 #include "TH1.h"
95 
97 public:
99  ~EcalDeadCellDeltaRFilter() override;
100 
101 private:
102  bool filter(edm::Event&, const edm::EventSetup&) override;
103  void beginJob() override;
104  void endJob() override;
105  void beginRun(const edm::Run&, const edm::EventSetup&) override;
106  void endRun(const edm::Run&, const edm::EventSetup&) override;
108  const edm::EventSetup&) override;
109  virtual void envSet(const edm::EventSetup&);
110 
111  // ----------member data ---------------------------
114 // jet selection cut: pt, eta
115 // default (pt=-1, eta= 9999) means no cut
116  const std::vector<double> jetSelCuts_;
117 
120 
121  const bool debug_, printSkimInfo_;
122 
124 
125  void loadEventInfo(const edm::Event& iEvent, const edm::EventSetup& iSetup);
126  void loadJets(const edm::Event& iEvent, const edm::EventSetup& iSetup);
127  void loadMET(const edm::Event& iEvent, const edm::EventSetup& iSetup);
128 
132  bool isdata;
133 
135 
136 // Channel status related
137  edm::ESHandle<EcalChannelStatus> ecalStatus; // these come from EventSetup
140 
142 
145 
146 // XXX: All the following can be built at the beginning of a run
147 // Store DetId <==> std::vector<double> (eta, phi, theta)
148  std::map<DetId, std::vector<double> > EcalAllDeadChannelsValMap;
149 // Store EB: DetId <==> std::vector<int> (subdet, ieta, iphi, status)
150 // Store EE: DetId <==> std::vector<int> (subdet, ix, iy, iz, status)
151  std::map<DetId, std::vector<int> > EcalAllDeadChannelsBitMap;
152 
153 // Store DetId <==> EcalTrigTowerDetId
154  std::map<DetId, EcalTrigTowerDetId> EcalAllDeadChannelsTTMap;
155 
156  int getChannelStatusMaps();
157 
160 
161  const bool makeProfileRoot_;
163  TFile *profFile;
164  TH1F *h1_dummy;
165 
166  const bool isProd_;
167  const int verbose_;
168 
169  const bool doCracks_;
170 // Cracks definition
171  const std::vector<double> cracksHBHEdef_, cracksHEHFdef_;
172 
173 // Simple dR filter
174  const std::vector<double> EcalDeadCellDeltaRFilterInput_;
175 
176  int dPhiToMETfunc(const std::vector<reco::Jet> &jetTVec, const double &dPhiCutVal, std::vector<reco::Jet> &closeToMETjetsVec);
177  int dRtoMaskedChnsEvtFilterFunc(const std::vector<reco::Jet> &jetTVec, const int &chnStatus, const double &dRCutVal);
178 
179  int etaToBoundary(const std::vector<reco::Jet> &jetTVec);
180 
181  int isCloseToBadEcalChannel(const reco::Jet &jet, const double &deltaRCut, const int &chnStatus, std::map<double, DetId> &deltaRdetIdMap);
182 
183  const bool taggingMode_;
184 };
185 
187 
188  iEvent.getByToken(metToken_, met);
189 
190 }
191 
193  run = iEvent.id().run();
194  event = iEvent.id().event();
195  ls = iEvent.luminosityBlock();
196  isdata = iEvent.isRealData();
197 
198  if( !isPrintedOnce ){
199  if( debug_ ){
200  if( isdata ) std::cout<<"\nInput dataset is DATA"<<std::endl<<std::endl;
201  else std::cout<<"\nInput dataset is MC"<<std::endl<<std::endl;
202  }
203  isPrintedOnce = true;
204  }
205 
206 }
207 
209 
210  iEvent.getByToken(jetToken_, jets);
211 
212 }
213 
214 //
215 // static data member definitions
216 //
217 
218 //
219 // constructors and destructor
220 //
222  : jetToken_ (consumes<edm::View<reco::Jet> >(iConfig.getParameter<edm::InputTag>("jetInputTag")))
223  , jetSelCuts_ (iConfig.getParameter<std::vector<double> >("jetSelCuts"))
224 
225  , metToken_ (consumes<edm::View<reco::MET> >(iConfig.getParameter<edm::InputTag>("metInputTag")))
226 
227  , debug_ (iConfig.getUntrackedParameter<bool>("debug",false))
228  , printSkimInfo_ (iConfig.getUntrackedParameter<bool>("printSkimInfo",false))
229 
230  , maskedEcalChannelStatusThreshold_ (iConfig.getParameter<int>("maskedEcalChannelStatusThreshold"))
231  , chnStatusToBeEvaluated_ (iConfig.getParameter<int>("chnStatusToBeEvaluated"))
232 
233  , makeProfileRoot_ (iConfig.getUntrackedParameter<bool>("makeProfileRoot", true))
234  , profileRootName_ (iConfig.getUntrackedParameter<std::string>("profileRootName", "EcalDeadCellDeltaRFilter.root"))
235 
236  , isProd_ (iConfig.getUntrackedParameter<bool>("isProd"))
237  , verbose_ (iConfig.getParameter<int>("verbose"))
238 
239  , doCracks_ (iConfig.getUntrackedParameter<bool>("doCracks"))
240  , cracksHBHEdef_ (iConfig.getParameter<std::vector<double> > ("cracksHBHEdef"))
241  , cracksHEHFdef_ (iConfig.getParameter<std::vector<double> > ("cracksHEHFdef"))
242 
243  , EcalDeadCellDeltaRFilterInput_ (iConfig.getParameter<std::vector<double> >("EcalDeadCellDeltaRFilterInput"))
244 
245  , taggingMode_ (iConfig.getParameter<bool>("taggingMode"))
246 {
247  produces<int> ("deadCellStatus"); produces<int> ("boundaryStatus");
248  produces<bool>();
249 
250  if( makeProfileRoot_ ){
251  profFile = new TFile(profileRootName_.c_str(), "RECREATE");
252  h1_dummy = new TH1F("dummy", "dummy", 500, 0, 500);
253  }
254 }
255 
257  if( makeProfileRoot_ ){
258  profFile->cd();
259 
260  h1_dummy->Write();
261 
262  profFile->Close();
263  delete profFile;
264  }
265 }
266 
268 
269  if (debug_) std::cout << "***envSet***" << std::endl;
270 
271  iSetup.get<IdealGeometryRecord>().get(ttMap_);
272 
273  iSetup.get<EcalChannelStatusRcd> ().get(ecalStatus);
274  iSetup.get<CaloGeometryRecord> ().get(geometry);
275 
276  if( !ecalStatus.isValid() ) throw "Failed to get ECAL channel status!";
277  if( !geometry.isValid() ) throw "Failed to get the geometry!";
278 
279 }
280 
281 // ------------ method called on each new Event ------------
283 
284  loadEventInfo(iEvent, iSetup);
285  loadJets(iEvent, iSetup);
286  loadMET(iEvent, iSetup);
287 
288 // XXX: In the following, never assign pass to true again
289 // Currently, always true
290  bool pass = true;
291 
292  using namespace edm;
293 
294  std::vector<reco::Jet> seledJets;
295 
296  for( edm::View<reco::Jet>::const_iterator ij = jets->begin(); ij != jets->end(); ij++){
297  if( ij->pt() > jetSelCuts_[0] && std::abs(ij->eta()) < jetSelCuts_[1] ){
298  seledJets.push_back(reco::Jet(*ij));
299  }
300  }
301 
302  if( seledJets.empty() ) return pass;
303 
304  double dPhiToMET = EcalDeadCellDeltaRFilterInput_[0], dRtoDeadCell = EcalDeadCellDeltaRFilterInput_[1];
305 
306  std::vector<reco::Jet> closeToMETjetsVec;
307 
308  int dPhiToMETstatus = dPhiToMETfunc(seledJets, dPhiToMET, closeToMETjetsVec);
309 
310 // Get event filter for simple dR cut
311  int deadCellStatus = dRtoMaskedChnsEvtFilterFunc(closeToMETjetsVec, chnStatusToBeEvaluated_, dRtoDeadCell);
312 
313  int boundaryStatus = etaToBoundary(closeToMETjetsVec);
314 
315  if(debug_ ){
316  printf("\nrun : %8u event : %12llu ls : %8u dPhiToMETstatus : %d deadCellStatus : %d boundaryStatus : %d\n", run, event, ls, dPhiToMETstatus, deadCellStatus, boundaryStatus);
317  printf("met : %6.2f metphi : % 6.3f dPhiToMET : %5.3f dRtoDeadCell : %5.3f\n", (*met)[0].pt(), (*met)[0].phi(), dPhiToMET, dRtoDeadCell);
318  }
319 
320  if( makeProfileRoot_ ){
321 // h1_dummy->Fill(xxx);
322  }
323 
324  iEvent.put(std::make_unique<int>(deadCellStatus), "deadCellStatus");
325  iEvent.put(std::make_unique<int>(boundaryStatus), "boundaryStatus");
326 
327  if( deadCellStatus || (doCracks_ && boundaryStatus) ) pass = false;
328 
329  iEvent.put(std::make_unique<bool>(pass));
330 
331  return taggingMode_ || pass;
332 }
333 
334 // ------------ method called once each job just before starting event loop ------------
336  if (debug_) std::cout << "beginJob" << std::endl;
337 }
338 
339 // ------------ method called once each job just after ending the event loop ------------
341  if (debug_) std::cout << "endJob" << std::endl;
342 }
343 
344 // ------------ method called once each run just before starting event loop ------------
346  if (debug_) std::cout << "beginRun" << std::endl;
347 // Channel status might change for each run (data)
348 // Event setup
349  envSet(iSetup);
351  if( debug_) std::cout<< "EcalAllDeadChannelsValMap.size() : "<<EcalAllDeadChannelsValMap.size()<<" EcalAllDeadChannelsBitMap.size() : "<<EcalAllDeadChannelsBitMap.size()<<std::endl;
352  return;
353 }
354 
355 // ------------ method called once each run just after starting event loop ------------
357  if (debug_) std::cout << "endRun" << std::endl;
358  return;
359 }
360 
361 // ------------ method called at lumi block start
363 
364  // needs per-LS access, if used at all
365  iSetup.get<HcalChannelQualityRcd>().get("withTopo",hcalStatus);
366  if( !hcalStatus.isValid() ) throw "Failed to get HCAL channel status!";
367  return;
368 }
369 
370 int EcalDeadCellDeltaRFilter::etaToBoundary(const std::vector<reco::Jet> &jetTVec){
371 
372  int isClose = 0;
373 
374  int cntOrder10 = 0;
375  for(unsigned int ij=0; ij<jetTVec.size(); ij++){
376 
377  double recoJetEta = jetTVec[ij].eta();
378 
379  if( std::abs(recoJetEta)>cracksHBHEdef_[0] && std::abs(recoJetEta)<cracksHBHEdef_[1] ) isClose += (cntOrder10*10 + 1);
380  if( std::abs(recoJetEta)>cracksHEHFdef_[0] && std::abs(recoJetEta)<cracksHEHFdef_[1] ) isClose += (cntOrder10*10 + 2);
381 
382  if( isClose/pow(10, cntOrder10) >=3 ) cntOrder10 = isClose/10 + 1;
383  }
384 
385  return isClose;
386 
387 }
388 
389 
390 // Cache all jets that are close to the MET within a dphi of dPhiCutVal
391 int EcalDeadCellDeltaRFilter::dPhiToMETfunc(const std::vector<reco::Jet> &jetTVec, const double &dPhiCutVal, std::vector<reco::Jet> &closeToMETjetsVec){
392 
393  closeToMETjetsVec.clear();
394 
395  double minDphi = 999.0;
396  int minIdx = -1;
397  for(unsigned int ii=0; ii<jetTVec.size(); ii++){
398 
399  const reco::Jet& jet = jetTVec[ii];
400 
401  double deltaPhi = std::abs(reco::deltaPhi( jet.phi(), (*met)[0].phi() ) );
402  if( deltaPhi > dPhiCutVal ) continue;
403 
404  closeToMETjetsVec.push_back(jetTVec[ii]);
405 
406  if( deltaPhi < minDphi ){
407  minDphi = deltaPhi;
408  minIdx = ii;
409  }
410  }
411 
412  if( minIdx == -1 ){} // removing a stupid compiling WARNING that minIdx NOT used.
413 // if( minIdx == -1 ) return 0;
414 // closeToMETjetsVec.push_back(jetTVec[minIdx]);
415 
416  return (int)closeToMETjetsVec.size();
417 }
418 
419 
420 int EcalDeadCellDeltaRFilter::dRtoMaskedChnsEvtFilterFunc(const std::vector<reco::Jet> &jetTVec, const int &chnStatus, const double &dRCutVal){
421 
422  int isClose = 0;
423 
424  for(unsigned int ii=0; ii<jetTVec.size(); ii++){
425 
426  const reco::Jet& jet = jetTVec[ii];
427 
428  std::map<double, DetId> dummy;
429  int isPerJetClose = isCloseToBadEcalChannel(jet, dRCutVal, chnStatus, dummy);
430 // if( isPerJetClose ){ isClose = 1; break; }
431  if( isPerJetClose ){ isClose ++; }
432  }
433 
434  return isClose;
435 
436 }
437 
438 
439 int EcalDeadCellDeltaRFilter::isCloseToBadEcalChannel(const reco::Jet &jet, const double &deltaRCut, const int &chnStatus, std::map<double, DetId> &deltaRdetIdMap){
440 
441  double jetEta = jet.eta(), jetPhi = jet.phi();
442 
443  deltaRdetIdMap.clear();
444 
445  double min_dist = 999;
446  DetId min_detId;
447 
448  std::map<DetId, std::vector<int> >::iterator bitItor;
449  for(bitItor = EcalAllDeadChannelsBitMap.begin(); bitItor != EcalAllDeadChannelsBitMap.end(); bitItor++){
450 
451  DetId maskedDetId = bitItor->first;
452 // int subdet = bitItor->second.front();
453  int status = bitItor->second.back();
454 
455  if( chnStatus >0 && status != chnStatus ) continue;
456  if( chnStatus <0 && status < abs(chnStatus) ) continue;
457 
458  std::map<DetId, std::vector<double> >::iterator valItor = EcalAllDeadChannelsValMap.find(maskedDetId);
459  if( valItor == EcalAllDeadChannelsValMap.end() ){ std::cout<<"Error cannot find maskedDetId in EcalAllDeadChannelsValMap ?!"<<std::endl; continue; }
460 
461  double eta = (valItor->second)[0], phi = (valItor->second)[1];
462 
463  double dist = reco::deltaR(eta, phi, jetEta, jetPhi);
464 
465  if( min_dist > dist ){ min_dist = dist; min_detId = maskedDetId; }
466  }
467 
468  if( min_dist > deltaRCut && deltaRCut >0 ) return 0;
469 
470  deltaRdetIdMap.insert( std::make_pair(min_dist, min_detId) );
471 
472  return 1;
473 }
474 
475 
477 
479 
480 // Loop over EB ...
481  for( int ieta=-85; ieta<=85; ieta++ ){
482  for( int iphi=0; iphi<=360; iphi++ ){
483  if(! EBDetId::validDetId( ieta, iphi ) ) continue;
484 
485  const EBDetId detid = EBDetId( ieta, iphi, EBDetId::ETAPHIMODE );
486  EcalChannelStatus::const_iterator chit = ecalStatus->find( detid );
487 // refer https://twiki.cern.ch/twiki/bin/viewauth/CMS/EcalChannelStatus
488  int status = ( chit != ecalStatus->end() ) ? chit->getStatusCode() & 0x1F : -1;
489 
490  const CaloSubdetectorGeometry* subGeom = geometry->getSubdetectorGeometry (detid);
491  auto cellGeom = subGeom->getGeometry (detid);
492  double eta = cellGeom->getPosition ().eta ();
493  double phi = cellGeom->getPosition ().phi ();
494  double theta = cellGeom->getPosition().theta();
495 
496  if(status >= maskedEcalChannelStatusThreshold_){
497  std::vector<double> valVec; std::vector<int> bitVec;
498  valVec.push_back(eta); valVec.push_back(phi); valVec.push_back(theta);
499  bitVec.push_back(1); bitVec.push_back(ieta); bitVec.push_back(iphi); bitVec.push_back(status);
500  EcalAllDeadChannelsValMap.insert( std::make_pair(detid, valVec) );
501  EcalAllDeadChannelsBitMap.insert( std::make_pair(detid, bitVec) );
502  }
503  } // end loop iphi
504  } // end loop ieta
505 
506 // Loop over EE detid
507  for( int ix=0; ix<=100; ix++ ){
508  for( int iy=0; iy<=100; iy++ ){
509  for( int iz=-1; iz<=1; iz++ ){
510  if(iz==0) continue;
511  if(! EEDetId::validDetId( ix, iy, iz ) ) continue;
512 
513  const EEDetId detid = EEDetId( ix, iy, iz, EEDetId::XYMODE );
514  EcalChannelStatus::const_iterator chit = ecalStatus->find( detid );
515  int status = ( chit != ecalStatus->end() ) ? chit->getStatusCode() & 0x1F : -1;
516 
517  const CaloSubdetectorGeometry* subGeom = geometry->getSubdetectorGeometry (detid);
518  auto cellGeom = subGeom->getGeometry (detid);
519  double eta = cellGeom->getPosition ().eta () ;
520  double phi = cellGeom->getPosition ().phi () ;
521  double theta = cellGeom->getPosition().theta();
522 
523  if(status >= maskedEcalChannelStatusThreshold_){
524  std::vector<double> valVec; std::vector<int> bitVec;
525  valVec.push_back(eta); valVec.push_back(phi); valVec.push_back(theta);
526  bitVec.push_back(2); bitVec.push_back(ix); bitVec.push_back(iy); bitVec.push_back(iz); bitVec.push_back(status);
527  EcalAllDeadChannelsValMap.insert( std::make_pair(detid, valVec) );
528  EcalAllDeadChannelsBitMap.insert( std::make_pair(detid, bitVec) );
529  }
530  } // end loop iz
531  } // end loop iy
532  } // end loop ix
533 
534  EcalAllDeadChannelsTTMap.clear();
535  std::map<DetId, std::vector<int> >::iterator bitItor;
536  for(bitItor = EcalAllDeadChannelsBitMap.begin(); bitItor != EcalAllDeadChannelsBitMap.end(); bitItor++){
537  const DetId id = bitItor->first;
538  EcalTrigTowerDetId ttDetId = ttMap_->towerOf(id);
539  EcalAllDeadChannelsTTMap.insert(std::make_pair(id, ttDetId) );
540  }
541 
542  return 1;
543 }
544 
545 //define this as a plug-in
RunNumber_t run() const
Definition: EventID.h:39
constexpr double deltaPhi(double phi1, double phi2)
Definition: deltaPhi.h:22
EventNumber_t event() const
Definition: EventID.h:41
EcalDeadCellDeltaRFilter(const edm::ParameterSet &)
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:125
double eta() const final
momentum pseudorapidity
edm::EDGetTokenT< edm::View< reco::Jet > > jetToken_
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:517
const std::vector< double > cracksHBHEdef_
Base class for all types of Jets.
Definition: Jet.h:20
static const int XYMODE
Definition: EEDetId.h:335
std::map< DetId, EcalTrigTowerDetId > EcalAllDeadChannelsTTMap
Geom::Theta< T > theta() const
unsigned long long EventNumber_t
edm::LuminosityBlockNumber_t luminosityBlock() const
Definition: EventBase.h:61
int dPhiToMETfunc(const std::vector< reco::Jet > &jetTVec, const double &dPhiCutVal, std::vector< reco::Jet > &closeToMETjetsVec)
bool isRealData() const
Definition: EventBase.h:62
unsigned int LuminosityBlockNumber_t
void beginRun(const edm::Run &, const edm::EventSetup &) override
EcalTrigTowerDetId towerOf(const DetId &id) const
Get the tower id for this det id (or null if not known)
static bool validDetId(int i, int j)
check if a valid index combination
Definition: EBDetId.h:118
EDGetTokenT< ProductType > consumes(edm::InputTag const &tag)
bool filter(edm::Event &, const edm::EventSetup &) override
int dRtoMaskedChnsEvtFilterFunc(const std::vector< reco::Jet > &jetTVec, const int &chnStatus, const double &dRCutVal)
int isCloseToBadEcalChannel(const reco::Jet &jet, const double &deltaRCut, const int &chnStatus, std::map< double, DetId > &deltaRdetIdMap)
int iEvent
Definition: GenABIO.cc:224
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
void loadMET(const edm::Event &iEvent, const edm::EventSetup &iSetup)
Definition: Jet.py:1
edm::Handle< edm::View< reco::MET > > met
edm::LuminosityBlockNumber_t ls
void loadEventInfo(const edm::Event &iEvent, const edm::EventSetup &iSetup)
const std::vector< double > cracksHEHFdef_
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
edm::EDGetTokenT< edm::View< reco::MET > > metToken_
void endRun(const edm::Run &, const edm::EventSetup &) override
virtual void envSet(const edm::EventSetup &)
edm::ESHandle< EcalTrigTowerConstituentsMap > ttMap_
edm::ESHandle< CaloGeometry > geometry
static const int ETAPHIMODE
Definition: EBDetId.h:158
std::map< DetId, std::vector< double > > EcalAllDeadChannelsValMap
ii
Definition: cuy.py:590
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:28
int etaToBoundary(const std::vector< reco::Jet > &jetTVec)
Definition: DetId.h:18
virtual std::shared_ptr< const CaloCellGeometry > getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
static bool validDetId(int crystal_ix, int crystal_iy, int iz)
Definition: EEDetId.h:248
std::vector< Item >::const_iterator const_iterator
void beginLuminosityBlock(const edm::LuminosityBlock &, const edm::EventSetup &) override
void loadJets(const edm::Event &iEvent, const edm::EventSetup &iSetup)
const std::vector< double > jetSelCuts_
edm::EventID id() const
Definition: EventBase.h:59
std::map< DetId, std::vector< int > > EcalAllDeadChannelsBitMap
fixed size matrix
HLT enums.
boost::indirect_iterator< typename seq_t::const_iterator > const_iterator
Definition: View.h:86
T get() const
Definition: EventSetup.h:71
edm::ESHandle< EcalChannelStatus > ecalStatus
unsigned int RunNumber_t
edm::Handle< edm::View< reco::Jet > > jets
bool isValid() const
Definition: ESHandle.h:44
const std::vector< double > EcalDeadCellDeltaRFilterInput_
edm::ESHandle< HcalChannelQuality > hcalStatus
double phi() const final
momentum azimuthal angle
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
Definition: event.py:1
Definition: Run.h:45