CMS 3D CMS Logo

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