CMS 3D CMS Logo

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