CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_6_1_2_SLHC4_patch1/src/RecoEgamma/Examples/plugins/ElectronSeedAnalyzer.cc

Go to the documentation of this file.
00001 
00002 // user include files
00003 #include "FWCore/Framework/interface/ESHandle.h"
00004 #include "FWCore/Framework/interface/Frameworkfwd.h"
00005 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00006 #include "FWCore/Framework/interface/EDAnalyzer.h"
00007 #include "FWCore/Framework/interface/Event.h"
00008 #include "FWCore/Framework/interface/MakerMacros.h"
00009 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00010 
00011 #include "DataFormats/EgammaReco/interface/ElectronSeedFwd.h"
00012 #include "DataFormats/EgammaReco/interface/ElectronSeed.h"
00013 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
00014 #include "DataFormats/EgammaReco/interface/SuperCluster.h"
00015 #include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00016 #include "DataFormats/Common/interface/OwnVector.h"
00017 #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
00018 #include "DataFormats/TrajectoryState/interface/PTrajectoryStateOnDet.h"
00019 
00020 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00021 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00022 
00023 #include "TrackingTools/DetLayers/interface/BarrelDetLayer.h"
00024 #include "TrackingTools/DetLayers/interface/ForwardDetLayer.h"
00025 #include "RecoTracker/TkDetLayers/interface/GeometricSearchTracker.h"
00026 #include "RecoEgamma/Examples/plugins/ElectronSeedAnalyzer.h"
00027 
00028 #include "RecoEgamma/EgammaElectronAlgos/interface/FTSFromVertexToPointFactory.h"
00029 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00030 #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
00031 #include "RecoEgamma/EgammaElectronAlgos/interface/BarrelMeasurementEstimator.h"
00032 #include "RecoEgamma/EgammaElectronAlgos/interface/ForwardMeasurementEstimator.h"
00033 #include "DataFormats/GeometryCommonDetAlgo/interface/PerpendicularBoundPlaneBuilder.h"
00034 #include "TrackingTools/MaterialEffects/interface/PropagatorWithMaterial.h"
00035 
00036 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00037 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00038 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00039 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00040 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00041 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00042 
00043 #include "MagneticField/Engine/interface/MagneticField.h"
00044 #include "MagneticField/Records/interface/IdealMagneticFieldRecord.h"
00045 
00046 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
00047 
00048 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
00049 #include "HepMC/GenParticle.h"
00050 #include "HepMC/SimpleVector.h"
00051 #include "CLHEP/Units/GlobalPhysicalConstants.h"
00052 
00053 #include <iostream>
00054 #include "TFile.h"
00055 #include "TH1F.h"
00056 #include "TH1I.h"
00057 #include "TTree.h"
00058 
00059 using namespace std;
00060 using namespace reco;
00061 
00062 ElectronSeedAnalyzer::ElectronSeedAnalyzer(const edm::ParameterSet& conf) :
00063   beamSpot_(conf.getParameter<edm::InputTag>("beamSpot"))
00064 {
00065   inputCollection_=conf.getParameter<edm::InputTag>("inputCollection") ;
00066   histfile_ = new TFile("electronpixelseeds.root","RECREATE");
00067 }
00068 
00069 void ElectronSeedAnalyzer::beginJob()
00070 {
00071   histfile_->cd();
00072   tree_ = new TTree("ElectronSeeds","ElectronSeed validation ntuple");
00073   tree_->Branch("mcEnergy",mcEnergy,"mcEnergy[10]/F");
00074   tree_->Branch("mcEta",mcEta,"mcEta[10]/F");
00075   tree_->Branch("mcPhi",mcPhi,"mcPhi[10]/F");
00076   tree_->Branch("mcPt",mcPt,"mcPt[10]/F");
00077   tree_->Branch("mcQ",mcQ,"mcQ[10]/F");
00078   tree_->Branch("superclusterEnergy",superclusterEnergy,"superclusterEnergy[10]/F");
00079   tree_->Branch("superclusterEta",superclusterEta,"superclusterEta[10]/F");
00080   tree_->Branch("superclusterPhi",superclusterPhi,"superclusterPhi[10]/F");
00081   tree_->Branch("superclusterEt",superclusterEt,"superclusterEt[10]/F");
00082   tree_->Branch("seedMomentum",seedMomentum,"seedMomentum[10]/F");
00083   tree_->Branch("seedEta",seedEta,"seedEta[10]/F");
00084   tree_->Branch("seedPhi",seedPhi,"seedPhi[10]/F");
00085   tree_->Branch("seedPt",seedPt,"seedPt[10]/F");
00086   tree_->Branch("seedQ",seedQ,"seedQ[10]/F");
00087   tree_->Branch("seedSubdet1",seedSubdet1,"seedSubdet1[10]/I");
00088   tree_->Branch("seedLayer1",seedLayer1,"seedLayer1[10]/I");
00089   tree_->Branch("seedSide1",seedSide1,"seedSide1[10]/I");
00090   tree_->Branch("seedPhi1",seedPhi1,"seedPhi1[10]/F");
00091   tree_->Branch("seedDphi1",seedDphi1,"seedDphi1[10]/F");
00092   tree_->Branch("seedDrz1",seedDrz1,"seedDrz1[10]/F");
00093   tree_->Branch("seedRz1",seedRz1,"seedRz1[10]/F");
00094   tree_->Branch("seedSubdet2",seedSubdet2,"seedSubdet2[10]/I");
00095   tree_->Branch("seedLayer2",seedLayer2,"seedLayer2[10]/I");
00096   tree_->Branch("seedSide2",seedSide2,"seedSide2[10]/I");
00097   tree_->Branch("seedPhi2",seedPhi2,"seedPhi2[10]/F");
00098   tree_->Branch("seedDphi2",seedDphi2,"seedDphi2[10]/F");
00099   tree_->Branch("seedRz2",seedRz2,"seedRz2[10]/F");
00100   tree_->Branch("seedDrz2",seedDrz2,"seedDrz2[10]/F");
00101   histeMC_ = new TH1F("eMC","MC particle energy",100,0.,100.);
00102   histeMCmatched_ = new TH1F("eMCmatched","matched MC particle energy",100,0.,100.);
00103   histecaldriveneMCmatched_ = new TH1F("ecaldriveneMCmatched","matched MC particle energy, ecal driven",100,0.,100.);
00104   histtrackerdriveneMCmatched_ = new TH1F("trackerdriveneMCmatched","matched MC particle energy, tracker driven",100,0.,100.);
00105   histp_ = new TH1F("p","seed p",100,0.,100.);
00106   histeclu_ = new TH1F("clus energy","supercluster energy",100,0.,100.);
00107   histpt_ = new TH1F("pt","seed pt",100,0.,100.);
00108   histptMC_ = new TH1F("ptMC","MC particle pt",100,0.,100.);
00109   histptMCmatched_ = new TH1F("ptMCmatched","matched MC particle pt",100,0.,100.);
00110   histecaldrivenptMCmatched_ = new TH1F("ecaldrivenptMCmatched","matched MC particle pt, ecal driven",100,0.,100.);
00111   histtrackerdrivenptMCmatched_ = new TH1F("trackerdrivenptMCmatched","matched MC particle pt, tracker driven",100,0.,100.);
00112   histetclu_ = new TH1F("Et","supercluster Et",100,0.,100.);
00113   histeffpt_ = new TH1F("pt eff","seed effciency vs pt",100,0.,100.);
00114   histeta_ = new TH1F("seed eta","seed eta",100,-2.5,2.5);
00115   histetaMC_ = new TH1F("etaMC","MC particle eta",100,-2.5,2.5);
00116   histetaMCmatched_ = new TH1F("etaMCmatched","matched MC particle eta",100,-2.5,2.5);
00117   histecaldrivenetaMCmatched_ = new TH1F("ecaldrivenetaMCmatched","matched MC particle eta, ecal driven",100,-2.5,2.5);
00118   histtrackerdrivenetaMCmatched_ = new TH1F("trackerdrivenetaMCmatched","matched MC particle eta, tracker driven",100,-2.5,2.5);
00119   histetaclu_ = new TH1F("clus eta","supercluster eta",100,-2.5,2.5);
00120   histeffeta_ = new TH1F("eta eff","seed effciency vs eta",100,-2.5,2.5);
00121   histq_ = new TH1F("q","seed charge",100,-2.5,2.5);
00122   histeoverp_ = new TH1F("E/p","seed E/p",100,0.,10.);
00123   histnbseeds_ = new TH1I("nrs","Nr of seeds ",50,0.,25.);
00124   histnbclus_ = new TH1I("nrclus","Nr of superclusters ",50,0.,25.);
00125   histnrseeds_ = new TH1I("ns","Nr of seeds if clusters",50,0.,25.);
00126 }
00127 
00128 void ElectronSeedAnalyzer::endJob()
00129 {
00130   histfile_->cd();
00131   tree_->Print();
00132   tree_->Write();
00133 
00134   // efficiency vs eta
00135   TH1F *histetaEff = (TH1F*)histetaMCmatched_->Clone("histetaEff");
00136   histetaEff->Reset();
00137   histetaEff->Divide(histetaMCmatched_,histeta_,1,1,"b");
00138   histetaEff->Print();
00139   histetaEff->GetXaxis()->SetTitle("#eta");
00140   histetaEff->GetYaxis()->SetTitle("Efficiency");
00141 
00142   // efficiency vs pt
00143   TH1F *histptEff = (TH1F*)histptMCmatched_->Clone("histotEff");
00144   histptEff->Reset();
00145   histptEff->Divide(histptMCmatched_,histpt_,1,1,"b");
00146   histptEff->Print();
00147   histptEff->GetXaxis()->SetTitle("p_{T}");
00148   histptEff->GetYaxis()->SetTitle("Efficiency");
00149 
00150   histeMCmatched_->Write();
00151   histecaldriveneMCmatched_->Write();
00152   histtrackerdriveneMCmatched_->Write();
00153   histeMC_->Write();
00154   histp_->Write();
00155   histeclu_->Write();
00156   histpt_->Write();
00157   histptMCmatched_->Write();
00158   histecaldrivenptMCmatched_->Write();
00159   histtrackerdrivenptMCmatched_->Write();
00160   histptMC_->Write();
00161   histetclu_->Write();
00162   histeffpt_->Write();
00163   histeta_->Write();
00164   histetaMCmatched_->Write();
00165   histecaldrivenetaMCmatched_->Write();
00166   histtrackerdrivenetaMCmatched_->Write();
00167   histetaMC_->Write();
00168   histetaclu_->Write();
00169   histeffeta_->Write();
00170   histq_->Write();
00171   histeoverp_->Write();
00172   histnbseeds_->Write();
00173   histnbclus_->Write();
00174   histnrseeds_->Write();
00175 }
00176 
00177 ElectronSeedAnalyzer::~ElectronSeedAnalyzer()
00178 {
00179   // do anything here that needs to be done at desctruction time
00180   // (e.g. close files, deallocate resources etc.)
00181   //tree_->Print();
00182   histfile_->Write();
00183   histeMC_->Write();
00184   histfile_->Close();
00185 }
00186 
00187 void ElectronSeedAnalyzer::analyze( const edm::Event& e, const edm::EventSetup& iSetup)
00188 {
00189 
00190   edm::ESHandle<TrackerGeometry> pDD ;
00191   edm::ESHandle<MagneticField> theMagField ;
00192   iSetup.get<TrackerDigiGeometryRecord> ().get(pDD);
00193   iSetup.get<IdealMagneticFieldRecord>().get(theMagField);
00194 
00195   // rereads the seeds for test purposes
00196   typedef edm::OwnVector<TrackingRecHit> recHitContainer;
00197   typedef recHitContainer::const_iterator const_iterator;
00198   typedef std::pair<const_iterator,const_iterator> range;
00199 
00200 
00201   // get beam spot
00202   edm::Handle<reco::BeamSpot> theBeamSpot;
00203   e.getByLabel(beamSpot_, theBeamSpot);
00204 
00205   // get seeds
00206 
00207   edm::Handle<ElectronSeedCollection> elSeeds;
00208   e.getByLabel(inputCollection_,elSeeds);
00209   edm::LogInfo("")<<"\n\n =================> Treating event "<<e.id()<<" Number of seeds "<<elSeeds.product()->size();
00210   int is=0;
00211 
00212   FTSFromVertexToPointFactory   myFTS;
00213   float mass=.000511; // electron propagation
00214   PropagatorWithMaterial* prop1stLayer = new PropagatorWithMaterial(oppositeToMomentum,mass,&(*theMagField));
00215   PropagatorWithMaterial* prop2ndLayer = new PropagatorWithMaterial(alongMomentum,mass,&(*theMagField));
00216 
00217   float dphi1=0., dphi2=0., drz1=0., drz2=0.;
00218   float phi1=0., phi2=0., rz1=0., rz2=0.;
00219 
00220   for( ElectronSeedCollection::const_iterator MyS= (*elSeeds).begin(); MyS != (*elSeeds).end(); ++MyS) {
00221 
00222     LogDebug("") <<"\nSeed nr "<<is<<": ";
00223     range r=(*MyS).recHits();
00224      LogDebug("")<<" Number of RecHits= "<<(*MyS).nHits();
00225     const GeomDet *det1=0;const GeomDet *det2=0;
00226 
00227     TrajectorySeed::const_iterator it=r.first;
00228     DetId id1 = (*it).geographicalId();
00229     det1 = pDD->idToDet(id1);
00230     LogDebug("") <<" First hit local x,y,z "<<(*it).localPosition()<<" det "<<id1.det()<<" subdet "<<id1.subdetId();
00231     LogDebug("") <<" First hit global  "<<det1->toGlobal((*it).localPosition());
00232     //std::cout <<" First hit local x,y,z "<<(*it).localPosition()<<" det "<<id1.det()<<" subdet "<<id1.subdetId()<< std::endl;
00233     //std::cout <<" First hit global  "<<det1->toGlobal((*it).localPosition())<< std::endl;
00234     it++;
00235     DetId id2 = (*it).geographicalId();
00236     det2 = pDD->idToDet(id2);
00237     LogDebug("") <<" Second hit local x,y,z "<<(*it).localPosition()<<" det "<<id2.det()<<" subdet "<<id2.subdetId();
00238     LogDebug("") <<" Second hit global  "<<det2->toGlobal((*it).localPosition());
00239     //std::cout <<" Second hit local x,y,z "<<(*it).localPosition()<<" det "<<id2.det()<<" subdet "<<id2.subdetId()<< std::endl;
00240     //std::cout <<" Second hit global  "<<det2->toGlobal((*it).localPosition()) << std::endl;
00241 
00242     // state on last det
00243     const GeomDet *det=0;
00244     for (TrackingRecHitCollection::const_iterator rhits=r.first; rhits!=r.second; rhits++) det = pDD->idToDet(((*rhits)).geographicalId());
00245     TrajectoryStateOnSurface t=  trajectoryStateTransform::transientState((*MyS).startingState(), &(det->surface()), &(*theMagField));
00246 
00247     // debug
00248 
00249     LogDebug("")<<" ElectronSeed outermost state position: "<<t.globalPosition();
00250     LogDebug("")<<" ElectronSeed outermost state momentum: "<<t.globalMomentum();
00251     edm::RefToBase<CaloCluster> caloCluster = (*MyS).caloCluster() ;
00252     if (caloCluster.isNull()) continue;
00253     edm::Ref<SuperClusterCollection> theClus = caloCluster.castTo<SuperClusterRef>() ;
00254     LogDebug("")<<" ElectronSeed superCluster energy: "<<theClus->energy()<<", position: "<<theClus->position();
00255     LogDebug("")<<" ElectronSeed outermost state Pt: "<<t.globalMomentum().perp();
00256     LogDebug("")<<" ElectronSeed supercluster Et: "<<theClus->energy()*sin(2.*atan(exp(-theClus->position().eta())));
00257     LogDebug("")<<" ElectronSeed outermost momentum direction eta: "<<t.globalMomentum().eta();
00258     LogDebug("")<<" ElectronSeed supercluster eta: "<<theClus->position().eta();
00259     LogDebug("")<<" ElectronSeed seed charge: "<<(*MyS).getCharge();
00260     LogDebug("")<<" ElectronSeed E/p: "<<theClus->energy()/t.globalMomentum().mag();
00261 
00262     // retreive SC and compute distances between hit position and prediction the same
00263     // way as in the PixelHitMatcher
00264     
00265     // inputs are charge, cluster position, vertex position, cluster energy and B field
00266     int charge = int((*MyS).getCharge());
00267     GlobalPoint xmeas(theClus->position().x(),theClus->position().y(),theClus->position().z());
00268     GlobalPoint vprim(theBeamSpot->position().x(),theBeamSpot->position().y(),theBeamSpot->position().z());
00269     float energy = theClus->energy();
00270 
00271     FreeTrajectoryState fts = myFTS(&(*theMagField),xmeas, vprim,
00272                                  energy, charge);
00273     //std::cout << "[PixelHitMatcher::compatibleSeeds] fts position, momentum " <<
00274     // fts.parameters().position() << " " << fts.parameters().momentum() << std::endl;
00275 
00276     PerpendicularBoundPlaneBuilder bpb;
00277     TrajectoryStateOnSurface tsos(fts, *bpb(fts.position(), fts.momentum()));
00278 
00279     //      TrajectorySeed::range r=(*seeds.product())[i].recHits();
00280    // TrajectorySeed::range r=(*seeds)[i].recHits();
00281 
00282     // first Hit
00283     it=r.first;
00284     DetId id=(*it).geographicalId();
00285     const GeomDet *geomdet=pDD->idToDet((*it).geographicalId());
00286     LocalPoint lp=(*it).localPosition();
00287     GlobalPoint hitPos=geomdet->surface().toGlobal(lp);
00288 
00289     TrajectoryStateOnSurface tsos1;
00290     tsos1 = prop1stLayer->propagate(tsos,geomdet->surface()) ;
00291 
00292     if (tsos1.isValid()) {
00293 
00294       std::pair<bool,double> est;
00295 
00296       //UB add test on phidiff
00297       float SCl_phi = xmeas.phi();
00298       float localDphi = SCl_phi-hitPos.phi();
00299       if(localDphi>CLHEP::pi)localDphi-=(2*CLHEP::pi);
00300       if(localDphi<-CLHEP::pi)localDphi+=(2*CLHEP::pi);
00301       if(std::abs(localDphi)>2.5)continue;
00302 
00303       phi1 = hitPos.phi();
00304       dphi1 = hitPos.phi() - tsos1.globalPosition().phi();
00305       rz1 = hitPos.perp();
00306       drz1 = hitPos.perp() - tsos1.globalPosition().perp();
00307       if (id.subdetId()%2==1) {
00308         drz1 = hitPos.z() - tsos1.globalPosition().z();
00309         rz1 = hitPos.z();
00310       }
00311 
00312       // now second Hit
00313       it++;
00314       DetId id2=(*it).geographicalId();
00315       const GeomDet *geomdet2=pDD->idToDet((*it).geographicalId());
00316       TrajectoryStateOnSurface tsos2;
00317 
00318       // compute the z vertex from the cluster point and the found pixel hit
00319       double pxHit1z = hitPos.z();
00320       double pxHit1x = hitPos.x();
00321       double pxHit1y = hitPos.y();
00322       double r1diff = (pxHit1x-vprim.x())*(pxHit1x-vprim.x()) + (pxHit1y-vprim.y())*(pxHit1y-vprim.y());
00323       r1diff=sqrt(r1diff);
00324       double r2diff = (xmeas.x()-pxHit1x)*(xmeas.x()-pxHit1x) + (xmeas.y()-pxHit1y)*(xmeas.y()-pxHit1y);
00325       r2diff=sqrt(r2diff);
00326       double zVertexPred = pxHit1z - r1diff*(xmeas.z()-pxHit1z)/r2diff;
00327 
00328       GlobalPoint vertexPred(vprim.x(),vprim.y(),zVertexPred);
00329 
00330       FreeTrajectoryState fts2 = myFTS(&(*theMagField),hitPos,vertexPred,energy, charge);
00331       tsos2 = prop2ndLayer->propagate(fts2,geomdet2->surface()) ;
00332 
00333       if (tsos2.isValid()) {
00334         LocalPoint lp2=(*it).localPosition();
00335         GlobalPoint hitPos2=geomdet2->surface().toGlobal(lp2);
00336         phi2 = hitPos2.phi();
00337         dphi2 = hitPos2.phi() - tsos2.globalPosition().phi();
00338         rz2 = hitPos2.perp();
00339         drz2 = hitPos2.perp() - tsos2.globalPosition().perp();
00340         if (id2.subdetId()%2==1) {
00341           rz2 = hitPos2.z();
00342           drz2 = hitPos2.z() - tsos2.globalPosition().z();
00343         }
00344       }
00345 
00346     }
00347 
00348     // fill the tree and histos
00349 
00350     histpt_->Fill(t.globalMomentum().perp());
00351     histetclu_->Fill(theClus->energy()*sin(2.*atan(exp(-theClus->position().eta()))));
00352     histeta_->Fill(t.globalMomentum().eta());
00353     histetaclu_->Fill(theClus->position().eta());
00354     histq_->Fill((*MyS).getCharge());
00355     histeoverp_->Fill(theClus->energy()/t.globalMomentum().mag());
00356 
00357     if (is<10) {
00358       superclusterEnergy[is] = theClus->energy();
00359       superclusterEta[is] = theClus->position().eta();
00360       superclusterPhi[is] = theClus->position().phi();
00361       superclusterEt[is] = theClus->energy()*sin(2.*atan(exp(-theClus->position().eta())));
00362       seedMomentum[is] = t.globalMomentum().mag();
00363       seedEta[is] = t.globalMomentum().eta();
00364       seedPhi[is] = t.globalMomentum().phi();
00365       seedPt[is] = t.globalMomentum().perp();
00366       seedQ[is] = (*MyS).getCharge();
00367       seedSubdet1[is] = id1.subdetId();
00368       switch (seedSubdet1[is]) {
00369         case 1:
00370           {
00371           PXBDetId pxbid1( id1.rawId() );
00372           seedLayer1[is] = pxbid1.layer();
00373           seedSide1[is] = 0;
00374           break;
00375           }
00376         case 2:
00377         {
00378           PXFDetId pxfid1( id1.rawId() );
00379           seedLayer1[is] = pxfid1.disk();
00380           seedSide1[is] = pxfid1.side();
00381           break;
00382           }
00383         case 3:
00384           {
00385           TIBDetId tibid1( id1.rawId() );
00386           seedLayer1[is] = tibid1.layer();
00387           seedSide1[is] = 0;
00388           break;
00389           }
00390         case 4:
00391           {
00392           TIDDetId tidid1( id1.rawId() );
00393           seedLayer1[is] = tidid1.wheel();
00394           seedSide1[is] = tidid1.side();
00395           break;
00396           }
00397         case 5:
00398           {
00399           TOBDetId tobid1( id1.rawId() );
00400           seedLayer1[is] = tobid1.layer();
00401           seedSide1[is] = 0;
00402           break;
00403           }
00404         case 6:
00405           {
00406           TECDetId tecid1( id1.rawId() );
00407           seedLayer1[is] = tecid1.wheel();
00408           seedSide1[is] = tecid1.side();
00409           break;
00410           }
00411       }
00412       seedPhi1[is] = phi1;
00413       seedRz1[is] = rz1;
00414       seedDphi1[is] = dphi1;
00415       seedDrz1[is] = drz1;
00416       seedSubdet2[is] = id2.subdetId();
00417       switch (seedSubdet2[is]) {
00418         case 1:
00419           {
00420           PXBDetId pxbid2( id2.rawId() );
00421           seedLayer2[is] = pxbid2.layer();
00422           seedSide2[is] = 0;
00423           break;
00424           }
00425         case 2:
00426           {
00427           PXFDetId pxfid2( id2.rawId() );
00428           seedLayer2[is] = pxfid2.disk();
00429           seedSide2[is] = pxfid2.side();
00430           break;
00431           }
00432         case 3:
00433           {
00434           TIBDetId tibid2( id2.rawId() );
00435           seedLayer2[is] = tibid2.layer();
00436           seedSide2[is] = 0;
00437           break;
00438           }
00439         case 4:
00440           {
00441           TIDDetId tidid2( id2.rawId() );
00442           seedLayer2[is] = tidid2.wheel();
00443           seedSide2[is] = tidid2.side();
00444           break;
00445           }
00446         case 5:
00447           {
00448           TOBDetId tobid2( id2.rawId() );
00449           seedLayer2[is] = tobid2.layer();
00450           seedSide2[is] = 0;
00451           break;
00452           }
00453         case 6:
00454           {
00455           TECDetId tecid2( id2.rawId() );
00456           seedLayer2[is] = tecid2.wheel();
00457           seedSide2[is] = tecid2.side();
00458           break;
00459           }
00460       }
00461       seedDphi2[is] = dphi2;
00462       seedDrz2[is] = drz2;
00463       seedPhi2[is] = phi2;
00464       seedRz2[is] = rz2;
00465     }
00466 
00467     is++;
00468 
00469   }
00470 
00471   histnbseeds_->Fill(elSeeds.product()->size());
00472 
00473   // get input clusters
00474 
00475   edm::Handle<SuperClusterCollection> clusters;
00476   //CC to be changed according to supercluster input
00477   e.getByLabel("correctedHybridSuperClusters", clusters);
00478   histnbclus_->Fill(clusters.product()->size());
00479   if (clusters.product()->size()>0) histnrseeds_->Fill(elSeeds.product()->size());
00480   // get MC information
00481 
00482   edm::Handle<edm::HepMCProduct> HepMCEvt;
00483   // this one is empty branch in current test files
00484   //e.getByLabel("VtxSmeared", "", HepMCEvt);
00485   //e.getByLabel("source", "", HepMCEvt);
00486   e.getByLabel("generator", "", HepMCEvt);
00487 
00488   const HepMC::GenEvent* MCEvt = HepMCEvt->GetEvent();
00489   HepMC::GenParticle* genPc=0;
00490   HepMC::FourVector pAssSim;
00491   int ip=0;
00492   for (HepMC::GenEvent::particle_const_iterator partIter = MCEvt->particles_begin();
00493    partIter != MCEvt->particles_end(); ++partIter) {
00494 
00495     for (HepMC::GenEvent::vertex_const_iterator vertIter = MCEvt->vertices_begin();
00496      vertIter != MCEvt->vertices_end(); ++vertIter) {
00497 
00498       //      CLHEP::HepLorentzVector creation = (*partIter)->CreationVertex();
00499       HepMC::GenVertex * creation = (*partIter)->production_vertex();
00500       //      CLHEP::HepLorentzVector momentum = (*partIter)->Momentum();
00501       HepMC::FourVector momentum = (*partIter)->momentum();
00502       //      HepPDT::ParticleID id = (*partIter)->particleID();  // electrons and positrons are 11 and -11
00503        int id = (*partIter)->pdg_id();  // electrons and positrons are 11 and -11
00504      LogDebug("")  << "MC particle id " << id << ", creationVertex " << (*creation) << " cm, initialMomentum " << momentum.rho() << " GeV/c" << std::endl;
00505 
00506       if (id == 11 || id == -11) {
00507 
00508       // single primary electrons or electrons from Zs or Ws
00509       HepMC::GenParticle* mother = 0;
00510       if ( (*partIter)->production_vertex() )  {
00511        if ( (*partIter)->production_vertex()->particles_begin(HepMC::parents) !=
00512            (*partIter)->production_vertex()->particles_end(HepMC::parents))
00513             mother = *((*partIter)->production_vertex()->particles_begin(HepMC::parents));
00514       }
00515       if ( ((mother == 0) || ((mother != 0) && (mother->pdg_id() == 23))
00516                           || ((mother != 0) && (mother->pdg_id() == 32))
00517                           || ((mother != 0) && (std::abs(mother->pdg_id()) == 24)))) {
00518       genPc=(*partIter);
00519       pAssSim = genPc->momentum();
00520 
00521       // EWK fiducial
00522       //if (pAssSim.perp()> 100. || std::abs(pAssSim.eta())> 2.5) continue;     
00523       //if (pAssSim.perp()< 20. || (std::abs(pAssSim.eta())> 1.4442 && std::abs(pAssSim.eta())< 1.56) || std::abs(pAssSim.eta())> 2.5) continue;
00524       // reconstruction fiducial
00525       //if (pAssSim.perp()< 5. || std::abs(pAssSim.eta())> 2.5) continue;
00526       if (std::abs(pAssSim.eta())> 2.5) continue;
00527 
00528       histptMC_->Fill(pAssSim.perp());
00529       histetaMC_->Fill(pAssSim.eta());
00530       histeMC_->Fill(pAssSim.rho());
00531 
00532      // looking for the best matching gsf electron
00533       bool okSeedFound = false;
00534       double seedOkRatio = 999999.;
00535 
00536       // find best matched seed
00537       reco::ElectronSeed bestElectronSeed;
00538       for( ElectronSeedCollection::const_iterator gsfIter= (*elSeeds).begin(); gsfIter != (*elSeeds).end(); ++gsfIter) {
00539 
00540         range r=gsfIter->recHits();
00541         const GeomDet *det=0;
00542         for (TrackingRecHitCollection::const_iterator rhits=r.first; rhits!=r.second; rhits++) det = pDD->idToDet(((*rhits)).geographicalId());
00543          TrajectoryStateOnSurface t= trajectoryStateTransform::transientState(gsfIter->startingState(), &(det->surface()), &(*theMagField));
00544 
00545         float eta = t.globalMomentum().eta();
00546         float phi = t.globalMomentum().phi();
00547         float p = t.globalMomentum().mag();
00548         double dphi = phi-pAssSim.phi();
00549         if (std::abs(dphi)>CLHEP::pi)
00550          dphi = dphi < 0? (CLHEP::twopi) + dphi : dphi - CLHEP::twopi;
00551         double deltaR = sqrt(std::pow((eta-pAssSim.eta()),2) + std::pow(dphi,2));
00552         if ( deltaR < 0.15 ){
00553 //      if ( deltaR < 0.3 ){
00554         //if ( (genPc->pdg_id() == 11) && (gsfIter->charge() < 0.) || (genPc->pdg_id() == -11) &&
00555         //(gsfIter->charge() > 0.) ){
00556           double tmpSeedRatio = p/pAssSim.t();
00557           if ( std::abs(tmpSeedRatio-1) < std::abs(seedOkRatio-1) ) {
00558             seedOkRatio = tmpSeedRatio;
00559             bestElectronSeed=*gsfIter;
00560             okSeedFound = true;
00561           }
00562         //}
00563         }
00564       } // loop over rec ele to look for the best one
00565 
00566       // analysis when the mc track is found
00567      if (okSeedFound){
00568 
00569         histptMCmatched_->Fill(pAssSim.perp());
00570         histetaMCmatched_->Fill(pAssSim.eta());
00571         histeMCmatched_->Fill(pAssSim.rho());
00572         if (ip<10) {
00573           mcEnergy[ip] = pAssSim.rho();
00574           mcEta[ip] = pAssSim.eta();
00575           mcPhi[ip] = pAssSim.phi();
00576           mcPt[ip] = pAssSim.perp();
00577           mcQ[ip] = ((id == 11) ? -1.: +1.);
00578         }
00579       }
00580       
00581      // efficiency for ecal driven only
00582       okSeedFound = false;
00583       seedOkRatio = 999999.;
00584 
00585       // find best matched seed
00586       for( ElectronSeedCollection::const_iterator gsfIter= (*elSeeds).begin(); gsfIter != (*elSeeds).end(); ++gsfIter) {
00587 
00588         range r=gsfIter->recHits();
00589         const GeomDet *det=0;
00590         for (TrackingRecHitCollection::const_iterator rhits=r.first; rhits!=r.second; rhits++) det = pDD->idToDet(((*rhits)).geographicalId());
00591          TrajectoryStateOnSurface t= trajectoryStateTransform::transientState(gsfIter->startingState(), &(det->surface()), &(*theMagField));
00592 
00593         float eta = t.globalMomentum().eta();
00594         float phi = t.globalMomentum().phi();
00595         float p = t.globalMomentum().mag();
00596         double dphi = phi-pAssSim.phi();
00597         if (std::abs(dphi)>CLHEP::pi)
00598          dphi = dphi < 0? (CLHEP::twopi) + dphi : dphi - CLHEP::twopi;
00599         double deltaR = sqrt(std::pow((eta-pAssSim.eta()),2) + std::pow(dphi,2));
00600         if (gsfIter->isEcalDriven()) {
00601         if ( deltaR < 0.15 ){
00602 //      if ( deltaR < 0.3 ){
00603         //if ( (genPc->pdg_id() == 11) && (gsfIter->charge() < 0.) || (genPc->pdg_id() == -11) &&
00604         //(gsfIter->charge() > 0.) ){
00605           double tmpSeedRatio = p/pAssSim.t();
00606           if ( std::abs(tmpSeedRatio-1) < std::abs(seedOkRatio-1) ) {
00607             seedOkRatio = tmpSeedRatio;
00608             bestElectronSeed=*gsfIter;
00609             okSeedFound = true;
00610           }
00611         //}
00612         }
00613         } // end if ecal driven
00614       } // loop over rec ele to look for the best one
00615 
00616       // analysis when the mc track is found
00617      if (okSeedFound){
00618 
00619         histecaldrivenptMCmatched_->Fill(pAssSim.perp());
00620         histecaldrivenetaMCmatched_->Fill(pAssSim.eta());
00621         histecaldriveneMCmatched_->Fill(pAssSim.rho());
00622       }
00623 
00624       // efficiency for tracker driven only
00625       okSeedFound = false;
00626       seedOkRatio = 999999.;
00627 
00628       // find best matched seed
00629       for( ElectronSeedCollection::const_iterator gsfIter= (*elSeeds).begin(); gsfIter != (*elSeeds).end(); ++gsfIter) {
00630 
00631         range r=gsfIter->recHits();
00632         const GeomDet *det=0;
00633         for (TrackingRecHitCollection::const_iterator rhits=r.first; rhits!=r.second; rhits++) det = pDD->idToDet(((*rhits)).geographicalId());
00634          TrajectoryStateOnSurface t= trajectoryStateTransform::transientState(gsfIter->startingState(), &(det->surface()), &(*theMagField));
00635 
00636         float eta = t.globalMomentum().eta();
00637         float phi = t.globalMomentum().phi();
00638         float p = t.globalMomentum().mag();
00639         double dphi = phi-pAssSim.phi();
00640         if (std::abs(dphi)>CLHEP::pi)
00641          dphi = dphi < 0? (CLHEP::twopi) + dphi : dphi - CLHEP::twopi;
00642         double deltaR = sqrt(std::pow((eta-pAssSim.eta()),2) + std::pow(dphi,2));
00643         if (gsfIter->isTrackerDriven()) {
00644         if ( deltaR < 0.15 ){
00645 //      if ( deltaR < 0.3 ){
00646         //if ( (genPc->pdg_id() == 11) && (gsfIter->charge() < 0.) || (genPc->pdg_id() == -11) &&
00647         //(gsfIter->charge() > 0.) ){
00648           double tmpSeedRatio = p/pAssSim.t();
00649           if ( std::abs(tmpSeedRatio-1) < std::abs(seedOkRatio-1) ) {
00650             seedOkRatio = tmpSeedRatio;
00651             bestElectronSeed=*gsfIter;
00652             okSeedFound = true;
00653           }
00654         //}
00655         }
00656         } // end if ecal driven
00657       } // loop over rec ele to look for the best one
00658 
00659       // analysis when the mc track is found
00660      if (okSeedFound){
00661 
00662         histtrackerdrivenptMCmatched_->Fill(pAssSim.perp());
00663         histtrackerdrivenetaMCmatched_->Fill(pAssSim.eta());
00664         histtrackerdriveneMCmatched_->Fill(pAssSim.rho());
00665       }
00666 
00667      } // end if mother W or Z
00668 
00669     } // end if gen part is electron
00670 
00671     } // end loop on vertices
00672 
00673     ip++;
00674 
00675   } // end loop on gen particles
00676 
00677   //tree_->Fill();
00678 
00679 }
00680 
00681