CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch9/src/RecoEgamma/Examples/plugins/SimpleConvertedPhotonAnalyzer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 //
00003 #include "RecoEgamma/Examples/plugins/SimpleConvertedPhotonAnalyzer.h"
00004 #include "RecoEgamma/EgammaMCTools/interface/PhotonMCTruthFinder.h"
00005 #include "RecoEgamma/EgammaMCTools/interface/PhotonMCTruth.h"
00006 #include "RecoEgamma/EgammaMCTools/interface/ElectronMCTruth.h"
00007 //
00008 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00009 #include "SimDataFormats/Track/interface/SimTrack.h"
00010 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00011 #include "SimDataFormats/Vertex/interface/SimVertex.h"
00012 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00013 //
00014 #include "FWCore/Framework/interface/Event.h"
00015 #include "FWCore/Framework/interface/EventSetup.h"
00016 #include "FWCore/Framework/interface/ESHandle.h"
00017 #include "FWCore/Framework/interface/MakerMacros.h"
00018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00019 #include "FWCore/Utilities/interface/Exception.h"
00020 //
00021 #include "DataFormats/Common/interface/Handle.h"
00022 #include "DataFormats/TrackReco/interface/Track.h"
00023 #include "DataFormats/TrackReco/interface/TrackExtra.h"
00024 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00025 #include "DataFormats/EgammaCandidates/interface/Conversion.h"
00026 #include "DataFormats/EgammaCandidates/interface/ConversionFwd.h"
00027 //#include "DataFormats/EgammaReco/interface/SuperCluster.h"
00028 #include "DataFormats/CaloRecHit/interface/CaloCluster.h"
00029 //
00030 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00031 #include "MagneticField/Engine/interface/MagneticField.h"
00032 
00033 //
00034 #include "TFile.h"
00035 #include "TH1.h"
00036 #include "TH2.h"
00037 #include "TTree.h"
00038 #include "TVector3.h"
00039 #include "TProfile.h"
00040 //
00041 
00042 
00043 
00044 using namespace std;
00045 
00046 
00047 SimpleConvertedPhotonAnalyzer::SimpleConvertedPhotonAnalyzer( const edm::ParameterSet& pset )
00048    : fOutputFileName_( pset.getUntrackedParameter<string>("HistOutFile",std::string("TestConversions.root")) ),
00049      fOutputFile_(0)
00050 {
00051 
00052   convertedPhotonCollectionProducer_ = pset.getParameter<std::string>("phoProducer");
00053   convertedPhotonCollection_ = pset.getParameter<std::string>("convertedPhotonCollection");
00054   //
00055 
00056 
00057 }
00058 
00059 
00060 
00061 SimpleConvertedPhotonAnalyzer::~SimpleConvertedPhotonAnalyzer() {
00062 
00063 
00064   delete thePhotonMCTruthFinder_;
00065 
00066 }
00067 
00068 
00069 void SimpleConvertedPhotonAnalyzer::beginJob()
00070 {
00071 
00072 
00073   nEvt_=0;
00074   nMCPho_=0;
00075   nMatched_=0;
00076 
00077 
00078   thePhotonMCTruthFinder_ = new PhotonMCTruthFinder();
00079 
00080   fOutputFile_   = new TFile( fOutputFileName_.c_str(), "RECREATE" ) ;
00081 
00083   h_ErecoEMC_  = new TH1F("deltaE","    delta(reco-mc) energy",100,0.,2.);
00084   h_deltaPhi_ = new TH1F("deltaPhi","  delta(reco-mc) phi",100,-0.1, 0.1);
00085   h_deltaEta_ = new TH1F("deltaEta","  delta(reco-mc) eta",100,-0.05, 0.05);
00086 
00088   h_MCphoE_ = new TH1F("MCphoE","MC photon energy",100,0.,100.);
00089   h_MCphoPhi_ = new TH1F("MCphoPhi","MC photon phi",40,-3.14, 3.14);
00090   h_MCphoEta_ = new TH1F("MCphoEta","MC photon eta",40,-3., 3.);
00091 
00092 
00094   h_MCConvE_ = new TH1F("MCConvE","MC converted photon energy",100,0.,100.);
00095   h_MCConvPt_ = new TH1F("MCConvPt","MC converted photon pt",100,0.,100.);
00096   h_MCConvEta_ = new TH1F("MCConvEta","MC converted photon eta",50, 0., 2.5);
00097 
00099   h_scE_ = new TH1F("scE","Uncorrected converted photons : SC Energy ",100,0., 200.);
00100   h_scEta_ = new TH1F("scEta","Uncorrected converted photons:  SC Eta ",40,-3., 3.);
00101   h_scPhi_ = new TH1F("scPhi","Uncorrected converted photons: SC Phi ",40, -3.14, 3.14);
00102   //
00103   h_phoE_ = new TH1F("phoE","Uncorrected converted photons :  Energy ",100,0., 200.);
00104   h_phoEta_ = new TH1F("phoEta","Uncorrected converted photons:   Eta ",40,-3., 3.);
00105   h_phoPhi_ = new TH1F("phoPhi","Uncorrected converted photons:  Phi ",40,  -3.14, 3.14);
00106 
00107   // Recontructed tracks from converted photon candidates
00108   h2_tk_nHitsVsR_ = new TH2F("tknHitsVsR","Tracks Hits vs R  ", 12,0.,120.,20,0.5, 20.5);
00109   h2_tk_inPtVsR_ = new TH2F("tkInPtvsR","Tracks inner Pt vs R  ", 12,0.,120.,100,0., 100.);
00110 
00111 
00112   return ;
00113 }
00114 
00115 
00116 float SimpleConvertedPhotonAnalyzer::etaTransformation(  float EtaParticle , float Zvertex)  {
00117 
00118 //---Definitions
00119         const float PI    = 3.1415927;
00120         //UNUSED const float TWOPI = 2.0*PI;
00121 
00122 //---Definitions for ECAL
00123         const float R_ECAL           = 136.5;
00124         const float Z_Endcap         = 328.0;
00125         const float etaBarrelEndcap  = 1.479;
00126 
00127 //---ETA correction
00128 
00129         float Theta = 0.0  ;
00130         float ZEcal = R_ECAL*sinh(EtaParticle)+Zvertex;
00131 
00132         if(ZEcal != 0.0) Theta = atan(R_ECAL/ZEcal);
00133         if(Theta<0.0) Theta = Theta+PI ;
00134         float ETA = - log(tan(0.5*Theta));
00135 
00136         if( fabs(ETA) > etaBarrelEndcap )
00137           {
00138            float Zend = Z_Endcap ;
00139            if(EtaParticle<0.0 )  Zend = -Zend ;
00140            float Zlen = Zend - Zvertex ;
00141            float RR = Zlen/sinh(EtaParticle);
00142            Theta = atan(RR/Zend);
00143            if(Theta<0.0) Theta = Theta+PI ;
00144            ETA = - log(tan(0.5*Theta));
00145           }
00146 //---Return the result
00147         return ETA;
00148 //---end
00149 }
00150 
00151 
00152 
00153 
00154 
00155 
00156 void SimpleConvertedPhotonAnalyzer::analyze( const edm::Event& e, const edm::EventSetup& )
00157 {
00158 
00159 
00160   using namespace edm;
00161   const float etaPhiDistance=0.01;
00162   // Fiducial region
00163   //UNUSED const float TRK_BARL =0.9;
00164   const float BARL = 1.4442; // DAQ TDR p.290
00165   const float END_LO = 1.566;
00166   const float END_HI = 2.5;
00167  // Electron mass
00168   //UNUSED const Float_t mElec= 0.000511;
00169 
00170 
00171   nEvt_++;
00172   LogInfo("ConvertedPhotonAnalyzer") << "ConvertedPhotonAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ <<"\n";
00173   //  LogDebug("ConvertedPhotonAnalyzer") << "ConvertedPhotonAnalyzer Analyzing event number: "  << e.id() << " Global Counter " << nEvt_ <<"\n";
00174   std::cout << "ConvertedPhotonAnalyzer Analyzing event number: "  << e.id() << " Global Counter " << nEvt_ <<"\n";
00175 
00176 
00178   Handle<reco::ConversionCollection> convertedPhotonHandle;
00179   e.getByLabel(convertedPhotonCollectionProducer_, convertedPhotonCollection_ , convertedPhotonHandle);
00180   const reco::ConversionCollection phoCollection = *(convertedPhotonHandle.product());
00181   std::cout  << "ConvertedPhotonAnalyzer  Converted photon collection size " << phoCollection.size() << "\n";
00182 
00183 
00185   std::cout  << " ConvertedPhotonAnalyzer Looking for MC truth " << "\n";
00186 
00187   //get simtrack info
00188   std::vector<SimTrack> theSimTracks;
00189   std::vector<SimVertex> theSimVertices;
00190 
00191   edm::Handle<SimTrackContainer> SimTk;
00192   edm::Handle<SimVertexContainer> SimVtx;
00193   e.getByLabel("g4SimHits",SimTk);
00194   e.getByLabel("g4SimHits",SimVtx);
00195 
00196   theSimTracks.insert(theSimTracks.end(),SimTk->begin(),SimTk->end());
00197   theSimVertices.insert(theSimVertices.end(),SimVtx->begin(),SimVtx->end());
00198 
00199   std::vector<PhotonMCTruth> mcPhotons=thePhotonMCTruthFinder_->find (theSimTracks,  theSimVertices);
00200   std::cout << " ConvertedPhotonAnalyzer mcPhotons size " <<  mcPhotons.size() << std::endl;
00201 
00202 
00203 
00204   // Loop over simulated photons
00205   //UNUSED int iDet=0;
00206   //UNUSED int iRadius=-1;
00207   //UNUSED int indPho=0;
00208 
00209   for ( std::vector<PhotonMCTruth>::const_iterator mcPho=mcPhotons.begin(); mcPho !=mcPhotons.end(); mcPho++) {
00210     float mcPhi= (*mcPho).fourMomentum().phi();
00211     float mcEta= (*mcPho).fourMomentum().pseudoRapidity();
00212     mcEta = etaTransformation(mcEta, (*mcPho).primaryVertex().z() );
00213 
00214     if ( ! (  fabs(mcEta) <= BARL || ( fabs(mcEta) >= END_LO && fabs(mcEta) <=END_HI ) ) ) {
00215           continue;
00216     } // all ecal fiducial region
00217 
00218 
00219     std::cout << " ConvertedPhotonAnalyzer MC Photons before matching  " <<  std::endl;
00220     std::cout << " ConvertedPhotonAnalyzer Photons isAconversion " << (*mcPho).isAConversion() << " mcMatchingPhoton energy " << (*mcPho).fourMomentum().e()  << " conversion vertex R " << (*mcPho).vertex().perp() << " Z " <<  (*mcPho).vertex().z() <<  " x " << (*mcPho).vertex().x() << " y " <<(*mcPho).vertex().y()  << " z " << (*mcPho).vertex().z() <<  std::endl;
00221     std::cout << " ConvertedPhotonAnalyzer mcEta " << mcEta<< " mcPhi " << mcPhi <<  std::endl;
00222 
00223 
00224     h_MCphoE_->Fill(  (*mcPho).fourMomentum().e());
00225     h_MCphoEta_->Fill( (*mcPho).fourMomentum().eta());
00226     h_MCphoPhi_->Fill(  (*mcPho).fourMomentum().phi());
00227 
00228 
00229 
00230     // keep only visible conversions
00231     if (  (*mcPho).isAConversion() == 0 ) continue;
00232 
00233 
00234     nMCPho_++;
00235 
00236     h_MCConvEta_ ->Fill ( fabs( (*mcPho).fourMomentum().pseudoRapidity()) - 0.001);
00237 
00238 
00239     bool REJECTED;
00240 
00241 
00243     std::cout   << " ConvertedPhotonAnalyzer  Starting loop over photon candidates " << "\n";
00244     for( reco::ConversionCollection::const_iterator  iPho = phoCollection.begin(); iPho != phoCollection.end(); iPho++) {
00245       REJECTED=false;
00246 
00247       std::cout  << " ConvertedPhotonAnalyzer Reco SC energy " << (*iPho).caloCluster()[0]->energy() <<  "\n";
00248 
00249 
00250 
00251       float phiClu=(*iPho).caloCluster()[0]->phi();
00252       float etaClu=(*iPho).caloCluster()[0]->eta();
00253       float deltaPhi = phiClu-mcPhi;
00254       float deltaEta = etaClu-mcEta;
00255 
00256 
00257       if ( deltaPhi > Geom::pi()  ) deltaPhi -= Geom::twoPi();
00258       if ( deltaPhi < -Geom::pi() ) deltaPhi += Geom::twoPi();
00259       deltaPhi=std::pow(deltaPhi,2);
00260       deltaEta=std::pow(deltaEta,2);
00261       float delta =  deltaPhi+deltaEta ;
00262       if (  delta >= etaPhiDistance  )  REJECTED=true;
00263 
00264 
00265       //      if ( ! (  fabs(etaClu) <= BARL || ( fabs(etaClu) >= END_LO && fabs(etaClu) <=END_HI ) ) ) REJECTED=true;
00266 
00267       if ( REJECTED ) continue;
00268       std::cout << " MATCHED " << std::endl;
00269       nMatched_++;
00270 
00271 
00272       std::cout << " ConvertedPhotonAnalyzer Matching candidate " << std::endl;
00273 
00274       std::cout << " ConvertedPhotonAnalyzer Photons isAconversion " << (*mcPho).isAConversion() << " mcMatchingPhoton energy " <<  (*mcPho).fourMomentum().e()  << " ConvertedPhotonAnalyzer conversion vertex R " <<  (*mcPho).vertex().perp() << " Z " <<  (*mcPho).vertex().z() <<  std::endl;
00275 
00276 
00277       h_ErecoEMC_->Fill(   (*iPho).caloCluster()[0]->energy()/(*mcPho).fourMomentum().e());
00278       h_deltaPhi_-> Fill ( (*iPho).caloCluster()[0]->position().phi()- mcPhi);
00279       h_deltaEta_-> Fill ( (*iPho).caloCluster()[0]->position().eta()- mcEta);
00280 
00281       h_scE_->Fill( (*iPho).caloCluster()[0]->energy() );
00282       h_scEta_->Fill( (*iPho).caloCluster()[0]->position().eta() );
00283       h_scPhi_->Fill( (*iPho).caloCluster()[0]->position().phi() );
00284 
00285 
00286       for (unsigned int i=0; i<(*iPho).tracks().size(); i++) {
00287         std::cout  << " ConvertedPhotonAnalyzer Reco Track charge " <<  (*iPho).tracks()[i]->charge() << "  Num of RecHits " << (*iPho).tracks()[i]->recHitsSize() << " inner momentum " <<  sqrt (  (*iPho).tracks()[i]->innerMomentum().Mag2() )  <<  "\n";
00288 
00289 
00290         h2_tk_nHitsVsR_ -> Fill (  (*mcPho).vertex().perp(), (*iPho).tracks()[i]->recHitsSize()   );
00291         h2_tk_inPtVsR_->Fill  (  (*mcPho).vertex().perp(),  sqrt( (*iPho).tracks()[i]->innerMomentum().Mag2() ) );
00292 
00293 
00294       }
00295 
00296 
00297 
00298 
00299 
00300 
00301 
00302 
00303 
00304     }   
00305 
00306   }
00307 
00308 
00309 }
00310 
00311 
00312 
00313 
00314 void SimpleConvertedPhotonAnalyzer::endJob()
00315 {
00316 
00317 
00318 
00319 
00320    fOutputFile_->Write() ;
00321    fOutputFile_->Close() ;
00322 
00323    edm::LogInfo("ConvertedPhotonAnalyzer") << "Analyzed " << nEvt_  << "\n";
00324    // std::cout  << "::endJob Analyzed " << nEvt_ << " events " << " with total " << nPho_ << " Photons " << "\n";
00325    std::cout  << "ConvertedPhotonAnalyzer::endJob Analyzed " << nEvt_ << " events " << "\n";
00326 
00327    return ;
00328 }
00329 
00330 
00331