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