CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_4_1_8_patch12/src/RecoEgamma/Examples/plugins/MCPizeroAnalyzer.cc

Go to the documentation of this file.
00001 #include <iostream>
00002 //
00003 #include "RecoEgamma/Examples/plugins/MCPizeroAnalyzer.h"
00004 #include "RecoEgamma/EgammaMCTools/interface/PizeroMCTruthFinder.h"
00005 #include "RecoEgamma/EgammaMCTools/interface/PizeroMCTruth.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 "SimDataFormats/CrossingFrame/interface/CrossingFrame.h"
00015 #include "SimDataFormats/CrossingFrame/interface/MixCollection.h"
00016 #include "SimDataFormats/TrackingAnalysis/interface/TrackingParticleFwd.h"
00017 #include "SimDataFormats/TrackingAnalysis/interface/TrackingVertexContainer.h"
00018 //
00019 #include "DataFormats/Common/interface/Handle.h"
00020 //
00021 #include "FWCore/Framework/interface/Event.h"
00022 #include "FWCore/Framework/interface/EventSetup.h"
00023 #include "FWCore/Framework/interface/ESHandle.h"
00024 #include "FWCore/Framework/interface/MakerMacros.h"
00025 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00026 #include "FWCore/Utilities/interface/Exception.h"
00027 //
00028 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00029 #include "MagneticField/Engine/interface/MagneticField.h"
00030 
00031 //
00032 #include "TFile.h"
00033 #include "TH1.h"
00034 #include "TH2.h"
00035 #include "TTree.h"
00036 #include "TVector3.h"
00037 #include "TProfile.h"
00038 //
00039 
00040 
00041 
00042 using namespace std;
00043 
00044 
00045 MCPizeroAnalyzer::MCPizeroAnalyzer( const edm::ParameterSet& pset )
00046    : fOutputFileName_( pset.getUntrackedParameter<string>("HistOutFile",std::string("TestConversions.root")) ),
00047      fOutputFile_(0)
00048 {
00049 
00050 
00051 }
00052 
00053 
00054 
00055 MCPizeroAnalyzer::~MCPizeroAnalyzer() {
00056 
00057 
00058   delete thePizeroMCTruthFinder_;
00059 
00060 }
00061 
00062 
00063 void MCPizeroAnalyzer::beginJob()
00064 {
00065 
00066 
00067   nEvt_=0;
00068 
00069   thePizeroMCTruthFinder_ = new PizeroMCTruthFinder();
00070 
00071   fOutputFile_   = new TFile( fOutputFileName_.c_str(), "RECREATE" ) ;
00072 
00074   h_MCPizE_ = new TH1F("MCPizE","MC piz energy",100,0.,200.);
00075   h_MCPizPhi_ = new TH1F("MCPizPhi","MC piz phi",40,-3.14, 3.14);
00076   h_MCPizEta_ = new TH1F("MCPizEta","MC piz eta",40,-3., 3.);
00077   h_MCPizUnEta_ = new TH1F("MCPizUnEta","MC un piz eta",40,-3., 3.);
00078   h_MCPiz1ConEta_ = new TH1F("MCPiz1ConEta","MC con piz eta: at least one converted photon",40,-3., 3.);
00079   h_MCPiz2ConEta_ = new TH1F("MCPiz2ConEta","MC con piz eta: two converted photons",40,-3., 3.);
00080   h_MCPizMass1_= new TH1F("MCPizMass1","Piz mass unconverted ",100, 0., 200);
00081   h_MCPizMass2_= new TH1F("MCPizMass2","Piz mass converted ",100, 0., 200);
00082 
00083   // All Photons from Pizeros
00084   h_MCPhoE_ = new TH1F("MCPhoE","MC photon energy",100,0.,200.);
00085   h_MCPhoPhi_ = new TH1F("MCPhoPhi","MC photon phi",40,-3.14, 3.14);
00086   h_MCPhoEta_ = new TH1F("MCPhoEta","MC photon eta",40,-3., 3.);
00087 
00088  // Converted photons
00089   h_MCConvPhoE_ = new TH1F("MCConvPhoE","MC converted photon energy",100,0.,200.);
00090   h_MCConvPhoPhi_ = new TH1F("MCConvPhoPhi","MC converted photon phi",40,-3.14, 3.14);
00091   h_MCConvPhoEta_ = new TH1F("MCConvPhoEta","MC converted photon eta",40,-3., 3.);
00092   h_MCConvPhoR_ = new TH1F("MCConvPhoR","MC converted photon R",120,0.,120.);
00093   // Electrons from converted photons
00094   h_MCEleE_ = new TH1F("MCEleE","MC ele energy",100,0.,200.);
00095   h_MCElePhi_ = new TH1F("MCElePhi","MC ele phi",40,-3.14, 3.14);
00096   h_MCEleEta_ = new TH1F("MCEleEta","MC ele eta",40,-3., 3.);
00097   h_BremFrac_ = new TH1F("bremFrac","brem frac ", 50, 0., 1.);
00098   h_BremEnergy_ = new TH1F("bremE","Brem energy",100,0.,200.);
00099 
00100 
00101   h_EleEvsPhoE_ = new TH2F ("eleEvsPhoE","eleEvsPhoE",100,0.,200.,100,0.,200.);
00102 
00103 
00104   return ;
00105 }
00106 
00107 
00108 float MCPizeroAnalyzer::etaTransformation(  float EtaParticle , float Zvertex)  {
00109 
00110 //---Definitions
00111         const float PI    = 3.1415927;
00112         //UNUSED const float TWOPI = 2.0*PI;
00113 
00114 //---Definitions for ECAL
00115         const float R_ECAL           = 136.5;
00116         const float Z_Endcap         = 328.0;
00117         const float etaBarrelEndcap  = 1.479;
00118 
00119 //---ETA correction
00120 
00121         float Theta = 0.0  ;
00122         float ZEcal = R_ECAL*sinh(EtaParticle)+Zvertex;
00123 
00124         if(ZEcal != 0.0) Theta = atan(R_ECAL/ZEcal);
00125         if(Theta<0.0) Theta = Theta+PI ;
00126         float ETA = - log(tan(0.5*Theta));
00127 
00128         if( fabs(ETA) > etaBarrelEndcap )
00129           {
00130            float Zend = Z_Endcap ;
00131            if(EtaParticle<0.0 )  Zend = -Zend ;
00132            float Zlen = Zend - Zvertex ;
00133            float RR = Zlen/sinh(EtaParticle);
00134            Theta = atan(RR/Zend);
00135            if(Theta<0.0) Theta = Theta+PI ;
00136            ETA = - log(tan(0.5*Theta));
00137           }
00138 //---Return the result
00139         return ETA;
00140 //---end
00141 }
00142 
00143 float MCPizeroAnalyzer::phiNormalization(float & phi)
00144 {
00145 //---Definitions
00146  const float PI    = 3.1415927;
00147  const float TWOPI = 2.0*PI;
00148 
00149 
00150  if(phi >  PI) {phi = phi - TWOPI;}
00151  if(phi < -PI) {phi = phi + TWOPI;}
00152 
00153  //  cout << " Float_t PHInormalization out " << PHI << endl;
00154  return phi;
00155 
00156 }
00157 
00158 
00159 
00160 
00161 
00162 void MCPizeroAnalyzer::analyze( const edm::Event& e, const edm::EventSetup& )
00163 {
00164 
00165 
00166   using namespace edm;
00167   //UNUSED const float etaPhiDistance=0.01;
00168   // Fiducial region
00169   //UNUSED const float TRK_BARL =0.9;
00170   //UNUSED const float BARL = 1.4442; // DAQ TDR p.290
00171   //UNUSED const float END_LO = 1.566;
00172   //UNUSED const float END_HI = 2.5;
00173  // Electron mass
00174   //UNUSED const Float_t mElec= 0.000511;
00175 
00176 
00177   nEvt_++;
00178   LogInfo("MCPizeroAnalyzer") << "MCPizeroAnalyzer Analyzing event number: " << e.id() << " Global Counter " << nEvt_ <<"\n";
00179   //  LogDebug("MCPizeroAnalyzer") << "MCPizeroAnalyzer Analyzing event number: "  << e.id() << " Global Counter " << nEvt_ <<"\n";
00180   std::cout << "MCPizeroAnalyzer Analyzing event number: "  << e.id() << " Global Counter " << nEvt_ <<"\n";
00181 
00183   std::cout  << " MCPizeroAnalyzer Looking for MC truth " << "\n";
00184 
00185   //get simtrack info
00186   std::vector<SimTrack> theSimTracks;
00187   std::vector<SimVertex> theSimVertices;
00188 
00189   edm::Handle<SimTrackContainer> SimTk;
00190   edm::Handle<SimVertexContainer> SimVtx;
00191   e.getByLabel("g4SimHits",SimTk);
00192   e.getByLabel("g4SimHits",SimVtx);
00193 
00194   theSimTracks.insert(theSimTracks.end(),SimTk->begin(),SimTk->end());
00195   theSimVertices.insert(theSimVertices.end(),SimVtx->begin(),SimVtx->end());
00196   std::cout << " MCPizeroAnalyzer This Event has " <<  theSimTracks.size() << " sim tracks " << std::endl;
00197   std::cout << " MCPizeroAnalyzer This Event has " <<  theSimVertices.size() << " sim vertices " << std::endl;
00198   if (  ! theSimTracks.size() ) std::cout << " Event number " << e.id() << " has NO sim tracks " << std::endl;
00199 
00200 
00201   std::vector<PizeroMCTruth> MCPizeroeros=thePizeroMCTruthFinder_->find (theSimTracks,  theSimVertices);
00202   std::cout << " MCPizeroAnalyzer MCPizeroeros size " <<  MCPizeroeros.size() << std::endl;
00203 
00204   for ( std::vector<PizeroMCTruth>::const_iterator iPiz=MCPizeroeros.begin(); iPiz !=MCPizeroeros.end(); ++iPiz ){
00205 
00206     h_MCPizE_->Fill  ( (*iPiz).fourMomentum().e() );
00207     h_MCPizEta_->Fill  ( (*iPiz).fourMomentum().pseudoRapidity() );
00208     h_MCPizPhi_->Fill  ( (*iPiz).fourMomentum().phi() );
00209 
00210     std::vector<PhotonMCTruth> mcPhotons=(*iPiz).photons();
00211     std::cout << " MCPizeroAnalyzer mcPhotons size " << mcPhotons.size() << std::endl;
00212 
00213     float px = mcPhotons[0].fourMomentum().x() + mcPhotons[1].fourMomentum().x();
00214     float py = mcPhotons[0].fourMomentum().y() + mcPhotons[1].fourMomentum().y();
00215     float pz = mcPhotons[0].fourMomentum().z() + mcPhotons[1].fourMomentum().z();
00216     float e  = mcPhotons[0].fourMomentum().e() + mcPhotons[1].fourMomentum().e();
00217     float invM =  sqrt( e*e - px*px -py*py - pz*pz)*1000;
00218     h_MCPizMass1_ ->Fill (invM);
00219 
00220     int converted=0;
00221     for ( std::vector<PhotonMCTruth>::const_iterator iPho=mcPhotons.begin(); iPho !=mcPhotons.end(); ++iPho ){
00222       h_MCPhoE_->Fill  ( (*iPho).fourMomentum().e() );
00223       h_MCPhoEta_->Fill  ( (*iPho).fourMomentum().pseudoRapidity() );
00224       h_MCPhoPhi_->Fill  ( (*iPho).fourMomentum().phi() );
00225       if (  (*iPho).isAConversion() ) {
00226 
00227         converted++;
00228 
00229         h_MCConvPhoE_->Fill  ( (*iPho).fourMomentum().e() );
00230         h_MCConvPhoEta_->Fill  ( (*iPho).fourMomentum().pseudoRapidity() );
00231         h_MCConvPhoPhi_->Fill  ( (*iPho).fourMomentum().phi() );
00232         h_MCConvPhoR_->Fill  ( (*iPho).vertex().perp() );
00233 
00234         std::vector<ElectronMCTruth> mcElectrons=(*iPho).electrons();
00235         std::cout << " MCPizeroAnalyzer mcElectrons size " << mcElectrons.size() << std::endl;
00236 
00237         for ( std::vector<ElectronMCTruth>::const_iterator iEl=mcElectrons.begin(); iEl !=mcElectrons.end(); ++iEl ){
00238 
00239           if ( (*iEl).fourMomentum().e()  < 30 ) continue;
00240           h_MCEleE_->Fill  ( (*iEl).fourMomentum().e() );
00241           h_MCEleEta_->Fill  ( (*iEl).fourMomentum().pseudoRapidity() );
00242           h_MCElePhi_->Fill  ( (*iEl).fourMomentum().phi() );
00243 
00244 
00245           h_EleEvsPhoE_->Fill ( (*iPho).fourMomentum().e(), (*iEl).fourMomentum().e() );
00246 
00247           float totBrem=0;
00248           for ( unsigned int iBrem=0; iBrem < (*iEl).bremVertices().size(); ++iBrem )
00249             totBrem +=  (*iEl).bremMomentum()[iBrem].e();
00250 
00251           h_BremFrac_->Fill( totBrem/(*iEl).fourMomentum().e() );
00252           h_BremEnergy_->Fill (  totBrem  );
00253         }
00254 
00255       }
00256 
00257     }
00258 
00259 
00260       if ( converted > 0 ) {
00261         h_MCPiz1ConEta_->Fill  ( (*iPiz).fourMomentum().pseudoRapidity() );
00262         if ( converted==2) h_MCPiz2ConEta_->Fill  ( (*iPiz).fourMomentum().pseudoRapidity() );
00263       } else {
00264         h_MCPizUnEta_->Fill  ( (*iPiz).fourMomentum().pseudoRapidity() );
00265       }
00266 
00267 
00268 
00269   }
00270 
00271 
00272 
00273 
00274 }
00275 
00276 
00277 
00278 
00279 void MCPizeroAnalyzer::endJob()
00280 {
00281 
00282 
00283 
00284 
00285    fOutputFile_->Write() ;
00286    fOutputFile_->Close() ;
00287 
00288    edm::LogInfo("MCPizeroAnalyzer") << "Analyzed " << nEvt_  << "\n";
00289    std::cout  << "MCPizeroAnalyzer::endJob Analyzed " << nEvt_ << " events " << "\n";
00290 
00291    return ;
00292 }
00293