CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_5/src/RecoParticleFlow/PFProducer/plugins/PFCandidateChecker.cc

Go to the documentation of this file.
00001 #include "RecoParticleFlow/PFProducer/plugins/PFCandidateChecker.h"
00002 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
00003 #include "DataFormats/ParticleFlowReco/interface/PFBlock.h"
00004 #include "DataFormats/JetReco/interface/PFJet.h"
00005 
00006 #include "FWCore/Framework/interface/ESHandle.h"
00007 
00008 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00009 #include "FWCore/Utilities/interface/Exception.h"
00010 #include "FWCore/Framework/interface/EventSetup.h"
00011 
00012 
00013 using namespace std;
00014 using namespace edm;
00015 using namespace reco;
00016 
00017 PFCandidateChecker::PFCandidateChecker(const edm::ParameterSet& iConfig) {
00018   
00019 
00020 
00021   inputTagPFCandidatesReco_ 
00022     = iConfig.getParameter<InputTag>("pfCandidatesReco");
00023 
00024   inputTagPFCandidatesReReco_ 
00025     = iConfig.getParameter<InputTag>("pfCandidatesReReco");
00026 
00027   inputTagPFJetsReco_ 
00028     = iConfig.getParameter<InputTag>("pfJetsReco");
00029 
00030   inputTagPFJetsReReco_ 
00031     = iConfig.getParameter<InputTag>("pfJetsReReco");
00032 
00033   deltaEMax_ 
00034     = iConfig.getParameter<double>("deltaEMax");
00035 
00036   deltaEtaMax_ 
00037     = iConfig.getParameter<double>("deltaEtaMax");
00038 
00039   deltaPhiMax_ 
00040     = iConfig.getParameter<double>("deltaPhiMax");
00041 
00042   verbose_ = 
00043     iConfig.getUntrackedParameter<bool>("verbose",false);
00044 
00045   printBlocks_ = 
00046     iConfig.getUntrackedParameter<bool>("printBlocks",false);
00047 
00048   rankByPt_ = 
00049     iConfig.getUntrackedParameter<bool>("rankByPt",false);
00050 
00051   entry_ = 0;
00052 
00053 
00054   LogDebug("PFCandidateChecker")
00055     <<" input collections : "<<inputTagPFCandidatesReco_<<" "<<inputTagPFCandidatesReReco_;
00056    
00057 }
00058 
00059 
00060 
00061 PFCandidateChecker::~PFCandidateChecker() { }
00062 
00063 
00064 
00065 void 
00066 PFCandidateChecker::beginRun(const edm::Run& run, 
00067                               const edm::EventSetup & es) { }
00068 
00069 
00070 void 
00071 PFCandidateChecker::analyze(const Event& iEvent, 
00072                              const EventSetup& iSetup) {
00073   
00074   LogDebug("PFCandidateChecker")<<"START event: "<<iEvent.id().event()
00075                          <<" in run "<<iEvent.id().run()<<endl;
00076   
00077   
00078   
00079   // get PFCandidates
00080 
00081   Handle<PFCandidateCollection> pfCandidatesReco;
00082   iEvent.getByLabel(inputTagPFCandidatesReco_, pfCandidatesReco);
00083 
00084   Handle<PFCandidateCollection> pfCandidatesReReco;
00085   iEvent.getByLabel(inputTagPFCandidatesReReco_, pfCandidatesReReco);
00086 
00087   Handle<PFJetCollection> pfJetsReco;
00088   iEvent.getByLabel(inputTagPFJetsReco_, pfJetsReco);
00089 
00090   Handle<PFJetCollection> pfJetsReReco;
00091   iEvent.getByLabel(inputTagPFJetsReReco_, pfJetsReReco);
00092 
00093   reco::PFCandidateCollection pfReco, pfReReco;  
00094 
00095   // to sort, one needs to copy
00096   if(rankByPt_)
00097     {
00098       pfReco=*pfCandidatesReco;
00099       pfReReco=*pfCandidatesReReco;
00100       sort(pfReco.begin(),pfReco.end(),greaterPt);
00101       sort(pfReReco.begin(),pfReReco.end(),greaterPt);
00102     }
00103   
00104   unsigned minSize = pfReco.size() < pfReReco.size() ? pfReco.size() : pfReReco.size();
00105   bool differentCand = false;
00106   bool differentSize = pfReco.size() != pfReReco.size();
00107   if ( differentSize ) 
00108     std::cout << "+++WARNING+++ PFCandidate size changed for entry " 
00109               << entry_ << " !" << endl
00110               << " - RECO    size : " << pfReco.size() << endl 
00111               << " - Re-RECO size : " << pfReReco.size() << endl;
00112 
00113   unsigned npr = 0;
00114   for( unsigned i=0; i<minSize; i++ ) {
00115     
00116     const reco::PFCandidate & candReco = (rankByPt_) ? pfReco[i] : (*pfCandidatesReco)[i];
00117     const reco::PFCandidate & candReReco = (rankByPt_) ? pfReReco[i] : (*pfCandidatesReReco)[i];
00118     
00119     double deltaE = (candReReco.energy()-candReco.energy())/(candReReco.energy()+candReco.energy());
00120     double deltaEta = candReReco.eta()-candReco.eta();
00121     double deltaPhi = candReReco.phi()-candReco.phi();
00122     if ( fabs(deltaE) > deltaEMax_ ||
00123          fabs(deltaEta) > deltaEtaMax_ ||
00124          fabs(deltaPhi) > deltaPhiMax_ ) { 
00125       differentCand = true;
00126       std::cout << "+++WARNING+++ PFCandidate " << i 
00127                 << " changed  for entry " << entry_ << " ! " << std::endl 
00128                 << " - RECO     : " << candReco << std::endl
00129                 << " - Re-RECO  : " << candReReco << std::endl
00130                 << " DeltaE   = : " << deltaE << std::endl
00131                 << " DeltaEta = : " << deltaEta << std::endl
00132                 << " DeltaPhi = : " << deltaPhi << std::endl << std::endl;
00133       if (printBlocks_) {
00134         std::cout << "Elements in Block for RECO: " <<std::endl;
00135         printElementsInBlocks(candReco);
00136         std::cout << "Elements in Block for Re-RECO: " <<std::endl;
00137         printElementsInBlocks(candReReco);
00138       }
00139       if ( ++npr == 5 ) break;
00140     }
00141   }
00142   
00143   if ( differentSize || differentCand ) { 
00144     printJets(*pfJetsReco, *pfJetsReReco);
00145     printMet(pfReco, pfReReco);
00146   }
00147 
00148   ++entry_;
00149   LogDebug("PFCandidateChecker")<<"STOP event: "<<iEvent.id().event()
00150                          <<" in run "<<iEvent.id().run()<<std::endl;
00151 }
00152 
00153 
00154 void PFCandidateChecker::printMet(const PFCandidateCollection& pfReco,
00155                                   const PFCandidateCollection& pfReReco) const { 
00156 
00157   double metX = 0.;
00158   double metY = 0.;
00159   for( unsigned i=0; i<pfReco.size(); i++ ) {
00160     metX += pfReco[i].px();
00161     metY += pfReco[i].py();
00162   }
00163   double met = std::sqrt(metX*metX + metY*metY);
00164   std::cout << "MET RECO    = " << metX << " " << metY << " " << met << std::endl;
00165 
00166   metX = 0.;
00167   metY = 0.;
00168   for( unsigned i=0; i<pfReReco.size(); i++ ) {
00169     metX += pfReReco[i].px();
00170     metY += pfReReco[i].py();
00171   }
00172   met = std::sqrt(metX*metX + metY*metY);
00173   std::cout << "MET Re-RECO = " << metX << " " << metY << " " << met << std::endl;
00174 
00175 }
00176 
00177 void PFCandidateChecker::printJets(const PFJetCollection& pfJetsReco,
00178                                    const PFJetCollection& pfJetsReReco) const { 
00179 
00180   bool differentSize = pfJetsReco.size() != pfJetsReReco.size();
00181   if ( differentSize ) 
00182     std::cout << "+++WARNING+++ PFJet size changed for entry " 
00183               << entry_ << " !" << endl
00184               << " - RECO    size : " << pfJetsReco.size() << endl 
00185               << " - Re-RECO size : " << pfJetsReReco.size() << endl;
00186   unsigned minSize = pfJetsReco.size() < pfJetsReReco.size() ? pfJetsReco.size() : pfJetsReReco.size(); 
00187   unsigned npr = 0;
00188   for ( unsigned i = 0; i < minSize; ++i) {
00189     const reco::PFJet & candReco = pfJetsReco[i];
00190     const reco::PFJet & candReReco = pfJetsReReco[i];
00191     if ( candReco.et() < 20. && candReReco.et() < 20. ) break;
00192     double deltaE = (candReReco.et()-candReco.et())/(candReReco.et()+candReco.et());
00193     double deltaEta = candReReco.eta()-candReco.eta();
00194     double deltaPhi = candReReco.phi()-candReco.phi();
00195     if ( fabs(deltaE) > deltaEMax_ ||
00196          fabs(deltaEta) > deltaEtaMax_ ||
00197          fabs(deltaPhi) > deltaPhiMax_ ) { 
00198       std::cout << "+++WARNING+++ PFJet " << i 
00199                 << " changed  for entry " << entry_ << " ! " << std::endl 
00200                 << " - RECO     : " << candReco.et() << " " << candReco.eta() << " " << candReco.phi() << std::endl
00201                 << " - Re-RECO  : " << candReReco.et() << " " << candReReco.eta() << " " << candReReco.phi() << std::endl
00202                 << " DeltaE   = : " << deltaE << std::endl
00203                 << " DeltaEta = : " << deltaEta << std::endl
00204                 << " DeltaPhi = : " << deltaPhi << std::endl << std::endl;
00205       if ( ++npr == 5 ) break;
00206     } else { 
00207       std::cout << "Jet " << i << " " << candReco.et() << std::endl;
00208     }
00209   }
00210 
00211 }
00212 
00213 
00214 void PFCandidateChecker::printElementsInBlocks(const PFCandidate& cand,
00215                                                 ostream& out) const {
00216   if(!out) return;
00217 
00218   PFBlockRef firstRef;
00219 
00220   assert(!cand.elementsInBlocks().empty() );
00221   for(unsigned i=0; i<cand.elementsInBlocks().size(); i++) {
00222     PFBlockRef blockRef = cand.elementsInBlocks()[i].first;
00223 
00224     if(blockRef.isNull()) {
00225       cerr<<"ERROR! no block ref!";
00226       continue;
00227     }
00228 
00229     if(!i) {
00230       out<<(*blockRef);
00231       firstRef = blockRef;
00232     }
00233     else if( blockRef!=firstRef) {
00234       cerr<<"WARNING! This PFCandidate is not made from a single block"<<endl;
00235     }
00236  
00237     out<<"\t"<<cand.elementsInBlocks()[i].second<<endl;
00238   }
00239 }