CMS 3D CMS Logo

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