CMS 3D CMS Logo

/afs/cern.ch/work/a/aaltunda/public/www/CMSSW_6_2_7/src/RecoEgamma/Examples/plugins/SimplePhotonAnalyzer.cc

Go to the documentation of this file.
00001 
00008 #include "RecoEgamma/Examples/plugins/SimplePhotonAnalyzer.h"
00009 #include "FWCore/ServiceRegistry/interface/Service.h"
00010 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00011 
00012 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00013 #include "FWCore/Utilities/interface/Exception.h"
00014 #include "DataFormats/Common/interface/Handle.h"
00015 //
00016 #include "DataFormats/EgammaCandidates/interface/Photon.h"
00017 #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
00018 #include "DataFormats/EgammaReco/interface/BasicCluster.h"
00019 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00020 #include "DataFormats/VertexReco/interface/Vertex.h"
00021 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00022 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00023 #include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
00024 #include "RecoEcal/EgammaCoreTools/interface/EcalClusterTools.h"
00025 //
00026 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00027 #include "TFile.h"
00028 
00029 //========================================================================
00030 SimplePhotonAnalyzer::SimplePhotonAnalyzer( const edm::ParameterSet& ps )
00031 //========================================================================
00032 {
00033   photonCollectionProducer_ = ps.getParameter<std::string>("phoProducer");
00034   photonCollection_ = ps.getParameter<std::string>("photonCollection");
00035 
00036   barrelEcalHits_   = ps.getParameter<edm::InputTag>("barrelEcalHits");
00037   endcapEcalHits_   = ps.getParameter<edm::InputTag>("endcapEcalHits");
00038 
00039 
00040 
00041   mcProducer_ = ps.getParameter<std::string>("mcProducer");
00042   //mcCollection_ = ps.getParameter<std::string>("mcCollection");
00043   vertexProducer_ = ps.getParameter<std::string>("primaryVertexProducer");
00044   sample_ = ps.getParameter<int>("sample");
00045 
00046 
00047 }
00048 
00049 
00050 //========================================================================
00051 SimplePhotonAnalyzer::~SimplePhotonAnalyzer()
00052 //========================================================================
00053 {
00054 
00055 
00056 }
00057 
00058 //========================================================================
00059 void
00060 SimplePhotonAnalyzer::beginJob() {
00061 //========================================================================
00062 
00063   edm::Service<TFileService> fs;
00064 
00065   float hiE=0;
00066   float loE=0;
00067   float hiEt=0;
00068   float loEt=0;
00069   float dPhi=0;
00070   float loRes=0;
00071   float hiRes=0;
00072   if ( sample_ ==1 ) {
00073     loE=0.;
00074     hiE=30.;
00075     loEt=0.;
00076     hiEt=30.;
00077     dPhi=0.2;
00078     loRes=0.;
00079     hiRes=1.2;
00080   } else if ( sample_ ==2 ) {
00081     loE=0.;
00082     hiE=200.;
00083     loEt=0.;
00084     hiEt=50.;
00085     dPhi=0.05;
00086     loRes=0.7;
00087     hiRes=1.2;
00088   } else if ( sample_ ==3 ) {
00089     loE=0.;
00090     hiE=500.;
00091     loEt=0.;
00092     hiEt=500.;
00093     dPhi=0.05;
00094     loRes=0.7;
00095     hiRes=1.2;
00096   }  else if (sample_==4) {
00097     loE=0.;
00098     hiE=6000.;
00099     loEt=0.;
00100     hiEt=1200.;
00101     dPhi=0.05;
00102     loRes=0.7;
00103     hiRes=1.2;
00104   }
00105 
00106 
00107   effEta_ = fs->make<TProfile> ("effEta"," Photon reconstruction efficiency",50,-2.5,2.5);
00108   effPhi_ = fs->make<TProfile> ("effPhi"," Photon reconstruction efficiency",80, -3.14, 3.14);
00109 
00110   h1_deltaEta_ = fs->make<TH1F>("deltaEta"," Reco photon Eta minus Generated photon Eta  ",100,-0.2, 0.2);
00111   h1_deltaPhi_ = fs->make<TH1F>("deltaPhi","Reco photon Phi minus Generated photon Phi ",100,-dPhi, dPhi);
00112   h1_pho_Eta_ = fs->make<TH1F>("phoEta","Photon  Eta ",40,-3., 3.);
00113   h1_pho_Phi_ = fs->make<TH1F>("phoPhi","Photon  Phi ",40,-3.14, 3.14);
00114   h1_pho_E_ = fs->make<TH1F>("phoE","Photon Energy ",100,loE,hiE);
00115   h1_pho_Et_ = fs->make<TH1F>("phoEt","Photon Et ",100,loEt,hiEt);
00116 
00117   h1_scEta_ = fs->make<TH1F>("scEta"," SC Eta ",40,-3., 3.);
00118   h1_deltaEtaSC_ = fs->make<TH1F>("deltaEtaSC"," SC Eta minus Generated photon Eta  ",100,-0.02, 0.02);
00119 
00120  //
00121   h1_recEoverTrueEBarrel_ = fs->make<TH1F>("recEoverTrueEBarrel"," Reco photon Energy over Generated photon Energy: Barrel ",100,loRes, hiRes);
00122   h1_recEoverTrueEEndcap_ = fs->make<TH1F>("recEoverTrueEEndcap"," Reco photon Energy over Generated photon Energy: Endcap ",100,loRes, hiRes);
00123 
00124   //
00125 
00126   h1_pho_R9Barrel_ = fs->make<TH1F>("phoR9Barrel","Photon  3x3 energy / SuperCluster energy : Barrel ",100,0.,1.2);
00127   h1_pho_R9Endcap_ = fs->make<TH1F>("phoR9Endcap","Photon  3x3 energy / SuperCluster energy : Endcap ",100,0.,1.2);
00128   h1_pho_sigmaIetaIetaBarrel_ = fs->make<TH1F>("sigmaIetaIetaBarrel",   "sigmaIetaIeta: Barrel",100,0., 0.05) ;
00129   h1_pho_sigmaIetaIetaEndcap_ = fs->make<TH1F>("sigmaIetaIetaEndcap" ,  "sigmaIetaIeta: Endcap",100,0., 0.1) ;
00130   h1_pho_hOverEBarrel_ = fs->make<TH1F>("hOverEBarrel",   "H/E: Barrel",100,0., 0.1) ;
00131   h1_pho_hOverEEndcap_ = fs->make<TH1F>("hOverEEndcap",   "H/E: Endcap",100,0., 0.1) ;
00132   h1_pho_ecalIsoBarrel_ = fs->make<TH1F>("ecalIsolBarrel",   "isolation et sum in Ecal: Barrel",100,0., 100.) ;
00133   h1_pho_ecalIsoEndcap_ = fs->make<TH1F>("ecalIsolEndcap",   "isolation et sum in Ecal: Endcap",100,0., 100.) ;
00134   h1_pho_hcalIsoBarrel_ = fs->make<TH1F>("hcalIsolBarrel",   "isolation et sum in Hcal: Barrel",100,0., 100.) ;
00135   h1_pho_hcalIsoEndcap_ = fs->make<TH1F>("hcalIsolEndcap",   "isolation et sum in Hcal: Endcap",100,0., 100.) ;
00136   h1_pho_trkIsoBarrel_ = fs->make<TH1F>("trkIsolBarrel",   "isolation pt sum in the tracker: Barrel",100,0., 100.) ;
00137   h1_pho_trkIsoEndcap_ = fs->make<TH1F>("trkIsolEndcap",   "isolation pt sum in the tracker: Endcap",100,0., 100.) ;
00138 
00139 
00140 
00141 
00142 
00143 }
00144 
00145 
00146 //========================================================================
00147 void
00148 SimplePhotonAnalyzer::analyze( const edm::Event& evt, const edm::EventSetup& es ) {
00149 //========================================================================
00150 
00151   using namespace edm; // needed for all fwk related classes
00152   edm::LogInfo("PhotonAnalyzer") << "Analyzing event number: " << evt.id() << "\n";
00153 
00154 
00155  // get the  calo topology  from the event setup:
00156   edm::ESHandle<CaloTopology> pTopology;
00157   es.get<CaloTopologyRecord>().get(theCaloTopo_);
00158 
00159 
00160 
00161   // Get the  corrected  photon collection (set in the configuration) which also contains infos about conversions
00162 
00163   Handle<reco::PhotonCollection> photonHandle;
00164   evt.getByLabel(photonCollectionProducer_, photonCollection_ , photonHandle);
00165   const reco::PhotonCollection photonCollection = *(photonHandle.product());
00166 
00167   Handle< HepMCProduct > hepProd ;
00168   evt.getByLabel( mcProducer_.c_str(),  hepProd ) ;
00169   const HepMC::GenEvent * myGenEvent = hepProd->GetEvent();
00170 
00171 
00172   for ( HepMC::GenEvent::particle_const_iterator p = myGenEvent->particles_begin(); p != myGenEvent->particles_end(); ++p ) {
00173     if ( !( (*p)->pdg_id() == 22 && (*p)->status()==1 )  )  continue;
00174 
00175     // single primary photons or photons from Higgs or RS Graviton
00176     HepMC::GenParticle* mother = 0;
00177     if ( (*p)->production_vertex() )  {
00178       if ( (*p)->production_vertex()->particles_begin(HepMC::parents) !=
00179            (*p)->production_vertex()->particles_end(HepMC::parents))
00180         mother = *((*p)->production_vertex()->particles_begin(HepMC::parents));
00181     }
00182     if ( ((mother == 0) || ((mother != 0) && (mother->pdg_id() == 25))
00183           || ((mother != 0) && (mother->pdg_id() == 22)))) {
00184 
00185       float minDelta=10000.;
00186       std::vector<reco::Photon> localPhotons;
00187       int index=0;
00188       int iMatch=-1;
00189 
00190       float phiPho=(*p)->momentum().phi();
00191       float etaPho=(*p)->momentum().eta();
00192       etaPho = etaTransformation(etaPho, (*p)->production_vertex()->position().z()/10. );
00193 
00194       bool matched=false;
00195       // loop  Photon candidates
00196       for( reco::PhotonCollection::const_iterator  iPho = photonCollection.begin(); iPho != photonCollection.end(); iPho++) {
00197         reco::Photon localPho = reco::Photon(*iPho);
00198         localPhotons.push_back(localPho);
00199 
00201         float phiClu=localPho.phi();
00202         float etaClu=localPho.eta();
00203         float deltaPhi = phiClu-phiPho;
00204         float deltaEta = etaClu-etaPho;
00205 
00206         if ( deltaPhi > pi )  deltaPhi -= twopi;
00207         if ( deltaPhi < -pi) deltaPhi += twopi;
00208         deltaPhi=std::pow(deltaPhi,2);
00209         deltaEta=std::pow(deltaEta,2);
00210         float delta = sqrt( deltaPhi+deltaEta);
00211         if ( delta<0.1 && delta < minDelta ) {
00212           minDelta=delta;
00213           iMatch=index;
00214 
00215         }
00216         index++;
00217       } // End loop over photons
00218 
00219       double wt=0.;
00220       if ( iMatch>-1 ) matched = true;
00221 
00223       if (matched ) {
00224         wt=1.;
00225 
00226         effEta_ ->Fill ( etaPho, wt);
00227         effPhi_ ->Fill ( phiPho, wt);
00228         reco::Photon matchingPho = localPhotons[iMatch];
00229 
00230         bool  phoIsInBarrel=false;
00231         if ( fabs(matchingPho.superCluster()->position().eta() ) <  1.479 ) {
00232           phoIsInBarrel=true;
00233         }
00234         edm::Handle<EcalRecHitCollection>   ecalRecHitHandle;
00235 
00236 
00237         h1_scEta_->Fill( matchingPho.superCluster()->position().eta() );
00238         float trueEta=  (*p)->momentum().eta() ;
00239         trueEta = etaTransformation(trueEta, (*p)->production_vertex()->position().z()/10. );
00240         h1_deltaEtaSC_ -> Fill(  localPhotons[iMatch].superCluster()->eta()- trueEta  );
00241 
00242         float photonE = matchingPho.energy();
00243         float photonEt= matchingPho.et() ;
00244         float photonEta= matchingPho.eta() ;
00245         float photonPhi= matchingPho.phi() ;
00246 
00247         float r9 = matchingPho.r9();
00248         float sigmaIetaIeta =  matchingPho.sigmaIetaIeta();
00249         float hOverE = matchingPho.hadronicOverEm();
00250         float ecalIso = matchingPho.ecalRecHitSumEtConeDR04();
00251         float hcalIso = matchingPho.hcalTowerSumEtConeDR04();
00252         float trkIso =  matchingPho.trkSumPtSolidConeDR04();
00253 
00254 
00255         h1_pho_E_->Fill( photonE  );
00256         h1_pho_Et_->Fill( photonEt );
00257         h1_pho_Eta_->Fill( photonEta );
00258         h1_pho_Phi_->Fill( photonPhi );
00259 
00260         h1_deltaEta_ -> Fill(  photonEta - (*p)->momentum().eta()  );
00261         h1_deltaPhi_ -> Fill(  photonPhi - (*p)->momentum().phi()  );
00262 
00263         if ( phoIsInBarrel ) {
00264           h1_recEoverTrueEBarrel_ -> Fill ( photonE / (*p)->momentum().e() );
00265           h1_pho_R9Barrel_->Fill( r9 );
00266           h1_pho_sigmaIetaIetaBarrel_->Fill ( sigmaIetaIeta );
00267           h1_pho_hOverEBarrel_-> Fill ( hOverE );
00268           h1_pho_ecalIsoBarrel_-> Fill ( ecalIso );
00269           h1_pho_hcalIsoBarrel_-> Fill ( hcalIso );
00270           h1_pho_trkIsoBarrel_-> Fill ( trkIso );
00271 
00272         } else {
00273           h1_recEoverTrueEEndcap_ -> Fill ( photonE / (*p)->momentum().e() );
00274           h1_pho_R9Endcap_->Fill( r9 );
00275           h1_pho_sigmaIetaIetaEndcap_->Fill ( sigmaIetaIeta );
00276           h1_pho_hOverEEndcap_-> Fill ( hOverE );
00277           h1_pho_ecalIsoEndcap_-> Fill ( ecalIso );
00278           h1_pho_hcalIsoEndcap_-> Fill ( hcalIso );
00279           h1_pho_trkIsoEndcap_-> Fill ( trkIso );
00280 
00281 
00282         }
00283 
00284 
00285       } //  reco photon matching MC truth 
00286 
00287 
00288 
00289 
00290     } // End loop over MC particles
00291 
00292   }
00293 
00294 
00295 }
00296 
00297 
00298 float SimplePhotonAnalyzer::etaTransformation(  float EtaParticle , float Zvertex)  {
00299 
00300   //---Definitions
00301   const float PI    = 3.1415927;
00302   //UNUSED const float TWOPI = 2.0*PI;
00303 
00304   //---Definitions for ECAL
00305   const float R_ECAL           = 136.5;
00306   const float Z_Endcap         = 328.0;
00307   const float etaBarrelEndcap  = 1.479;
00308 
00309   //---ETA correction
00310 
00311   float Theta = 0.0  ;
00312   float ZEcal = R_ECAL*sinh(EtaParticle)+Zvertex;
00313 
00314   if(ZEcal != 0.0) Theta = atan(R_ECAL/ZEcal);
00315   if(Theta<0.0) Theta = Theta+PI ;
00316   float ETA = - log(tan(0.5*Theta));
00317 
00318   if( fabs(ETA) > etaBarrelEndcap )
00319     {
00320       float Zend = Z_Endcap ;
00321       if(EtaParticle<0.0 )  Zend = -Zend ;
00322       float Zlen = Zend - Zvertex ;
00323       float RR = Zlen/sinh(EtaParticle);
00324       Theta = atan(RR/Zend);
00325       if(Theta<0.0) Theta = Theta+PI ;
00326       ETA = - log(tan(0.5*Theta));
00327     }
00328   //---Return the result
00329   return ETA;
00330   //---end
00331 }
00332 
00333 
00334 
00335 
00336 //========================================================================
00337 void
00338 SimplePhotonAnalyzer::endJob() {
00339 //========================================================================
00340 
00341 
00342 
00343 }