CMS 3D CMS Logo

SinglePionEfficiencyNew.cc

Go to the documentation of this file.
00001 //
00002 //
00003 //
00004 //
00005 // system include files
00006 //
00007 // You should use the TrackerHitAssociator (in SimTracker/TrackerHitAssociation)
00008 // which will give you the PSimHit (or the id of the sim hit). From the 
00009 // PSimHit you can then get track and event id. Of course this
00010 // needs the presence of the corresponding products in your input file.
00011 // Giuseppe Cerati - a person that is developing a lot of code in this respect for studies of the 
00012 // tracking performances + Patrizia.Azzi@cern.ch
00013 // the RecHits are stored in the event in a RangeMap (one  / type of hit) - from this map one can retrieve the 
00014 // hits by DetId.
00015 // Danek's example RecoLocalTracker/SiPixelRecHits/test/ReadPixelRecHit.cc
00016 //  How to commit to head cmssw16x
00017 //  1. sozdanie project area
00018 //  2. cvs co -r jetCorrections_1_6_X_retrofit JetMETCorrections/JetPlusTrack
00019 //  3. <editing>
00020 //     cvs commit JetMETCorrections/JetPlusTrack
00021 //     cvs tag <tagname> JetMETCorrections/JetPlusTrack
00022 
00023 #include <memory>
00024 #include <string>
00025 #include <iostream>
00026 #include <fstream>
00027 #include <vector>
00028 
00029 // user include files
00030 #include "FWCore/Framework/interface/Frameworkfwd.h"
00031 #include "FWCore/Framework/interface/EDAnalyzer.h"
00032 
00033 #include "FWCore/Framework/interface/Event.h"
00034 #include "DataFormats/Common/interface/Handle.h"
00035 #include "FWCore/Framework/interface/MakerMacros.h"
00036 #include "FWCore/Framework/interface/EventSetup.h"
00037 #include "FWCore/Framework/interface/ESHandle.h"
00038 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00039 //
00040 // MC info
00041 #include "CLHEP/Vector/LorentzVector.h"
00042 #include "CLHEP/Units/PhysicalConstants.h"
00043 #include "SimGeneral/HepPDTRecord/interface/ParticleDataTable.h"
00044 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00045 // #include "CLHEP/HepPDT/DefaultConfig.hh"
00046 //
00047 #include "DataFormats/Math/interface/LorentzVector.h"
00048 #include "DataFormats/Math/interface/Vector3D.h"
00049 #include "Math/GenVector/VectorUtil.h"
00050 #include "Math/GenVector/PxPyPzE4D.h"
00051 #include "DataFormats/Math/interface/deltaR.h"
00052 //double dR = deltaR( c1, c2 );
00053 
00054 //
00055 #include "Geometry/Records/interface/IdealGeometryRecord.h"
00056 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00057 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
00058 //jets
00059 #include "DataFormats/JetReco/interface/CaloJet.h"
00060 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
00061 //#include "DataFormats/JetReco/interface/CaloJetfwd.h"
00062 #include "DataFormats/JetReco/interface/GenJet.h"
00063 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00064 //#include "DataFormats/JetReco/interface/GenJetfwd.h"
00065 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
00066 //
00067 // muons and tracks
00068 #include "DataFormats/TrackReco/interface/Track.h"
00069 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00070 #include "DataFormats/TrackingRecHit/interface/TrackingRecHitFwd.h"
00071 #include "DataFormats/TrackingRecHit/interface/TrackingRecHit.h"
00072 #include "DataFormats/TrackReco/interface/HitPattern.h"
00073 // track associator
00074 #include "TrackingTools/TrajectoryState/interface/FreeTrajectoryState.h"
00075 #include "TrackingTools/TrackAssociator/interface/TrackDetectorAssociator.h"
00076 #include "SimTracker/Records/interface/TrackAssociatorRecord.h"
00077 #include "DataFormats/SiStripDetId/interface/TIBDetId.h"
00078 #include "DataFormats/SiStripDetId/interface/TOBDetId.h"
00079 #include "DataFormats/SiStripDetId/interface/TIDDetId.h"
00080 #include "DataFormats/SiStripDetId/interface/TECDetId.h"
00081 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00082 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00083 
00084 // tracker geometry
00085 #include "SimTracker/TrackerHitAssociation/interface/TrackerHitAssociator.h"
00086 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
00087 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
00088 #include "Geometry/CommonDetUnit/interface/GeomDet.h"
00089 #include "Geometry/CommonDetUnit/interface/GeomDetType.h"
00090 #include "Geometry/CommonDetUnit/interface/GeomDetUnit.h"
00091 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetUnit.h"
00092 #include "Geometry/TrackerGeometryBuilder/interface/PixelGeomDetType.h"
00093 #include "DataFormats/SiPixelDetId/interface/PXBDetId.h"
00094 #include "DataFormats/SiPixelDetId/interface/PXFDetId.h"
00095 #include "DataFormats/SiPixelDetId/interface/PixelSubdetector.h"
00096 // pixel rec hits
00097 #include "RecoLocalTracker/SiPixelRecHits/test/ReadPixelRecHit.h"
00098 #include "DataFormats/TrackerRecHit2D/interface/SiPixelRecHitCollection.h"
00099 // vertixes
00100 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00101 #include "DataFormats/VertexReco/interface/Vertex.h"
00102 //add simhit info
00103 //--- for SimHit
00104 #include "SimDataFormats/TrackingHit/interface/PSimHit.h"
00105 #include "SimDataFormats/TrackingHit/interface/PSimHitContainer.h"
00106 //simtrack
00107 #include "SimDataFormats/Track/interface/SimTrack.h"
00108 #include "SimDataFormats/Track/interface/SimTrackContainer.h"
00109 #include "SimDataFormats/Vertex/interface/SimVertex.h"
00110 #include "SimDataFormats/Vertex/interface/SimVertexContainer.h"
00111 
00112 // ecal / hcal
00113 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00114 #include "Geometry/CaloGeometry/interface/CaloCellGeometry.h"
00115 #include "Geometry/CaloGeometry/interface/CaloSubdetectorGeometry.h"
00116 // #include "Geometry/HcalTowerAlgo/interface/CaloTowerGeometry.h"
00117 #include "DataFormats/EcalDetId/interface/EcalSubdetector.h"
00118 #include "DataFormats/EcalDetId/interface/EBDetId.h"
00119 #include "DataFormats/EcalDetId/interface/EEDetId.h"
00120 #include "DataFormats/HcalDetId/interface/HcalDetId.h"
00121 #include "DataFormats/CaloTowers/interface/CaloTowerDetId.h"
00122 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00123 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00124 #include "DataFormats/DetId/interface/DetId.h"
00125 #include "Geometry/CaloEventSetup/interface/CaloTopologyRecord.h"
00126 #include "Geometry/CaloTopology/interface/CaloTowerTopology.h"
00127 #include "Geometry/CaloTopology/interface/CaloSubdetectorTopology.h"
00128 #include "Geometry/CaloTopology/interface/HcalTopology.h"
00129 #include "Geometry/CaloTopology/interface/CaloTopology.h"
00130 #include "RecoCaloTools/Navigation/interface/EcalBarrelNavigator.h"
00131 #include "RecoCaloTools/Navigation/interface/EcalEndcapNavigator.h"
00132 #include "RecoCaloTools/Navigation/interface/EcalPreshowerNavigator.h"
00133 #include "RecoCaloTools/Navigation/interface/CaloTowerNavigator.h"
00134 #include "RecoCaloTools/Navigation/interface/CaloNavigator.h"
00135 
00136 //
00137 //#include "DataFormats/EgammaCandidates/interface/PixelMatchGsfElectron.h"
00138 //#include "DataFormats/EgammaReco/interface/BasicClusterFwd.h"
00139 //#include "DataFormats/EgammaReco/interface/SuperClusterFwd.h"
00140 //#include "DataFormats/EgammaReco/interface/ClusterShapeFwd.h"
00141 //#include "DataFormats/EgammaReco/interface/BasicClusterShapeAssociation.h"
00142 // candidates
00143 #include "DataFormats/Candidate/interface/LeafCandidate.h"
00144 #include "DataFormats/Candidate/interface/Candidate.h"
00145 #include "DataFormats/Candidate/interface/CandidateFwd.h"
00146 //
00147 #include "TFile.h"
00148 #include "TTree.h"
00149 #include "TH1.h"
00150 #include "TH2.h"
00151 
00152 //
00153 #include "DataFormats/Math/interface/LorentzVector.h"
00154 #include "DataFormats/Math/interface/Vector3D.h"
00155 #include "Math/GenVector/VectorUtil.h"
00156 #include "Math/GenVector/PxPyPzE4D.h"
00157 
00158 using namespace std;
00159 using namespace reco;
00160 
00161 //
00162 // class decleration
00163 
00164 class SinglePionEfficiencyNew : public edm::EDAnalyzer {
00165 public:
00166   explicit SinglePionEfficiencyNew(const edm::ParameterSet&);
00167   ~SinglePionEfficiencyNew();
00168 
00169 
00170   double eECALmatrix(CaloNavigator<DetId>& navigator,edm::Handle<EcalRecHitCollection>& hits, int ieta, int iphi);
00171 
00172   double eHCALmatrix(const HcalTopology* topology, const DetId& det,const HBHERecHitCollection& hits, int ieta, int iphi);
00173 
00174   double eHCALneighbours(std::vector<DetId>& vNeighboursDetId, std::vector<DetId>& dets, const HcalTopology* topology, const HBHERecHitCollection& hits);
00175 
00176 private:
00177   virtual void beginJob(const edm::EventSetup&) ;
00178   virtual void analyze(const edm::Event&, const edm::EventSetup&);
00179   virtual void endJob() ;
00180 
00181   // ----------member data ---------------------------
00182   // output tree and root file
00183   string fOutputFileName ;
00184   TFile*      hOutputFile ;
00185   TTree*      t1;
00186   // names of modules, producing object collections
00187   string tracksSrc;
00188   string pxltracksSrc;
00189   string pxlhitsSrc;
00190   string calotowersSrc;
00191 
00192   string ecalHitsProducerSrc;
00193   string ECALbarrelHitCollectionSrc;
00194   string ECALendcapHitCollectionSrc;
00195 
00196   string hbheInputSrc;
00197   string hoInputSrc;  
00198   string hfInputSrc;
00199   string towermakerSrc; 
00200 
00201   // variables to store in ntpl
00202   // simulated/generated tracks
00203   double ptSim1, etaSim1, phiSim1; 
00204   double ptSim2, etaSim2, phiSim2; 
00205   // reconstructed tracks
00206   double ptTrk1, etaTrk1, phiTrk1, drTrk1, purityTrk1;
00207   double ptTrk2, etaTrk2, phiTrk2, drTrk2, purityTrk2;
00208   // track quality and number of valid hits
00209   int trkQuality1, trkQuality2, trkNVhits1, trkNVhits2;
00210   int   idmax1, idmax2;
00211   // reconstructed pixel triplets
00212   double ptPxl1, etaPxl1, phiPxl1, drPxl1, purityPxl1; 
00213   double ptPxl2, etaPxl2, phiPxl2, drPxl2, purityPxl2;
00214   // Et and energy in cone 0.5 around every MC particle
00215   double etCalo1, etCalo2;
00216   double eCalo1, eCalo2;
00217   // Et in 7x7, 11x11 crystal matrix and 3x3, 5x5 HCAL matrix
00218   double e1ECAL7x7, e2ECAL7x7, e1ECAL11x11, e2ECAL11x11; 
00219   double e1HCAL3x3, e2HCAL3x3, e1HCAL5x5, e2HCAL5x5;
00220   // end
00221 
00222   // track hit associator
00223   TrackerHitAssociator* hitAssociator;
00224   // track associator to detector parameters 
00225   TrackAssociatorParameters parameters_;
00226   mutable TrackDetectorAssociator* trackAssociator_;
00227   //
00228   const edm::ParameterSet conf_;
00229   };
00230 //
00231 // constants, enums and typedefs
00232 //
00233 
00234 //
00235 // static data member definitions
00236 //
00237 
00238 //
00239 // constructors and destructor
00240 //
00241 SinglePionEfficiencyNew::SinglePionEfficiencyNew(const edm::ParameterSet& iConfig) : conf_(iConfig)
00242 {
00243    //now do what ever initialization is needed
00244   using namespace edm;
00245 
00246   // get name of output file with histogramms
00247   fOutputFileName = iConfig.getUntrackedParameter<string>("HistOutFile"); 
00248   tracksSrc     = iConfig.getParameter<string>("tracks"); 
00249   pxltracksSrc  = iConfig.getParameter<string>("pxltracks"); 
00250   pxlhitsSrc    = iConfig.getParameter<string>("pxlhits");
00251   calotowersSrc = iConfig.getParameter<std::string>("calotowers");
00252   towermakerSrc = iConfig.getParameter<std::string>("towermaker");
00253 
00254   ecalHitsProducerSrc        = iConfig.getParameter< std::string >("ecalRecHitsProducer");
00255   ECALbarrelHitCollectionSrc = iConfig.getParameter<string>("ECALbarrelHitCollection");
00256   ECALendcapHitCollectionSrc = iConfig.getParameter<string>("ECALendcapHitCollection");
00257 
00258   hbheInputSrc = iConfig.getParameter<string>("hbheInput");
00259   hoInputSrc   = iConfig.getParameter<string>("hoInput");
00260   hfInputSrc   = iConfig.getParameter<string>("hfInput"); 
00261 
00262   edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
00263   parameters_.loadParameters( parameters );
00264   trackAssociator_ =  new TrackDetectorAssociator();
00265   trackAssociator_->useDefaultPropagator();
00266 }
00267 
00268 
00269 SinglePionEfficiencyNew::~SinglePionEfficiencyNew()
00270 {
00271  
00272    // do anything here that needs to be done at desctruction time
00273    // (e.g. close files, deallocate resources etc.)
00274 
00275 }
00276 
00277 /*
00278 double SinglePionEfficiencyNew::deltaPhi(double phi1, double phi2)
00279 {
00280   double pi = 3.1415927;
00281   double dphi = fabs(phi1 - phi2);
00282   if(dphi >= pi) dphi = 2. * pi - dphi; 
00283   return dphi;
00284 }
00285 double SinglePionEfficiencyNew::deltaEta(double eta1, double eta2)
00286 {
00287   double deta = fabs(eta1-eta2);
00288   return deta;
00289 }
00290 double double::deltaR(double eta1, double eta2,
00291                       double phi1, double phi2)
00292 {
00293   double dr = sqrt( deltaEta(eta1, eta2) * deltaEta(eta1, eta2) +
00294                      deltaPhi(phi1, phi2) * deltaPhi(phi1, phi2) );
00295   return dr;
00296 }
00297 */
00298 
00299 //
00300 // member functions
00301 //
00302 
00303 // ------------ method called to for each event  ------------
00304 void
00305 SinglePionEfficiencyNew::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00306 {
00307    using namespace edm;
00308 
00309    hitAssociator = new TrackerHitAssociator::TrackerHitAssociator(iEvent);
00310 
00311    // initialize tree variables
00312    // sim tracks
00313    ptSim1 = 0.; etaSim1 = 0.; phiSim1 = 0.; ptSim2 = 0.; etaSim2 = 0.; phiSim2 = 0.; 
00314    // reconstructed tracks
00315    ptTrk1 = 0.; etaTrk1 = 0.; phiTrk1 = 0.; drTrk1 = 1000.; purityTrk1 = 0.;
00316    ptTrk2 = 0.; etaTrk2 = 0.; phiTrk2 = 0.; drTrk2 = 1000.; purityTrk2 = 0.;
00317    // reco pixel triplets
00318    ptPxl1 = 0.; etaPxl1 = 0.; phiPxl1 = 0.; drPxl1 = 1000.; purityPxl1 = 0.; 
00319    ptPxl2 = 0.; etaPxl2 = 0.; phiPxl2 = 0.; drPxl2 = 1000.; purityPxl2 = 0.; 
00320    // Et of calo towers in cone 0.5 around each MC particle
00321    etCalo1 = 0.; etCalo2 = 0.;
00322    eCalo1 = 0.; eCalo2 = 0.;
00323    // Et in 7x7, 11x11 crystal matrix and 3x3, 5x5 HCAL matrix
00324    e1ECAL7x7 = 0.; e2ECAL7x7 = 0.; e1ECAL11x11 = 0.; e2ECAL11x11 = 0.; 
00325    e1HCAL3x3 = 0.; e2HCAL3x3 = 0.; e1HCAL5x5 = 0.; e2HCAL5x5 = 0.; 
00326    //
00327    trkQuality1 = -1; trkQuality2 = -1; trkNVhits1 = -1; trkNVhits2 = -1;
00328    idmax1 = -1; idmax2 = -1;
00329 
00330    //
00331    // extract tracker geometry
00332    //
00333    edm::ESHandle<TrackerGeometry> theG;
00334    iSetup.get<TrackerDigiGeometryRecord>().get(theG);
00335    //   const TrackerGeometry& theTracker(*theG);
00336 
00337    edm::ESHandle<CaloGeometry> pG;
00338    //   iSetup.get<IdealGeometryRecord>().get(pG);
00339    iSetup.get<CaloGeometryRecord>().get(pG);
00340    const CaloGeometry* geo = pG.product();
00341    const CaloSubdetectorGeometry* gEB = geo->getSubdetectorGeometry(DetId::Ecal,EcalBarrel);
00342    const CaloSubdetectorGeometry* gEE = geo->getSubdetectorGeometry(DetId::Ecal,EcalEndcap);
00343    const CaloSubdetectorGeometry* gHBHE = geo->getSubdetectorGeometry(DetId::Hcal,HcalBarrel);
00344    //   const CaloSubdetectorGeometry* gHE = geo->getSubdetectorGeometry(DetId::Hcal,HcalEndcap);
00345 
00346    edm::ESHandle<CaloTopology> theCaloTopology;
00347    iSetup.get<CaloTopologyRecord>().get(theCaloTopology); 
00348    const CaloSubdetectorTopology* theEBTopology   = theCaloTopology->getSubdetectorTopology(DetId::Ecal,EcalBarrel);
00349    const CaloSubdetectorTopology* theEETopology   = theCaloTopology->getSubdetectorTopology(DetId::Ecal,EcalEndcap);
00350 
00351    // get HcalTopology. should be similar way to get CaloTowerTopology
00352    edm::ESHandle<HcalTopology> htopo;
00353    iSetup.get<IdealGeometryRecord>().get(htopo);
00354    const HcalTopology* theHBHETopology = htopo.product();
00355 
00356    //   const CaloTowerTopology* theCaloTowerTopology;
00357 
00358    // get MC info
00359    edm::Handle<HepMCProduct> EvtHandle ;
00360    iEvent.getByLabel( "source", EvtHandle ) ;
00361    //  iEvent.getByLabel( "VtxSmeared", EvtHandle ) ;
00362 
00363    const HepMC::GenEvent* evt = EvtHandle->GetEvent() ;
00364    ESHandle<ParticleDataTable> pdt;
00365    iSetup.getData( pdt );
00366 
00367    vector<HepLorentzVector> genpions;
00368    genpions.clear();
00369 
00370    for ( HepMC::GenEvent::particle_const_iterator p = evt->particles_begin();
00371          p != evt->particles_end(); ++p ) {
00372      
00373      HepLorentzVector pion((*p)->momentum().px(),(*p)->momentum().py(),(*p)->momentum().pz(),(*p)->momentum().e());
00374      genpions.push_back(pion);
00375      /*
00376      cout <<" status : " << (*p)->status() 
00377           <<" pid = " << (*p)->pdg_id() 
00378           <<" eta = " << (*p)->momentum().eta()
00379           <<" phi = " << (*p)->momentum().phi() 
00380           <<" theta = " << (*p)->momentum().theta() 
00381           <<" pt =  " << (*p)->momentum().perp() 
00382           <<" charge = " << (pdt->particle((*p)->pdg_id()))->charge()
00383           <<" charge3 = " << (pdt->particle((*p)->pdg_id()))->ID().threeCharge() << endl;
00384      */
00385    }
00386    //   edm::Handle<EBRecHitCollection> barrelRecHitsHandle;
00387    //   edm::Handle<EERecHitCollection> endcapRecHitsHandle;
00388    edm::Handle<EcalRecHitCollection> barrelRecHitsHandle;
00389    edm::Handle<EcalRecHitCollection> endcapRecHitsHandle;
00390 
00391    iEvent.getByLabel(ecalHitsProducerSrc,ECALbarrelHitCollectionSrc,barrelRecHitsHandle);
00392    iEvent.getByLabel(ecalHitsProducerSrc,ECALendcapHitCollectionSrc,endcapRecHitsHandle);
00393    // iEvent.getByLabel( edm::InputTag(ecalHitsProducerSrc,ECALendcapHitCollectionSrc,"RECO3"), endcapRecHitsHandle);
00394 
00395    EBRecHitCollection::const_iterator itb;
00396    for (itb=barrelRecHitsHandle->begin(); itb!=barrelRecHitsHandle->end(); itb++) {
00397      GlobalPoint pos = geo->getPosition(itb->detid());
00398      double eta = pos.eta();
00399      //     double the = pos.theta();
00400      double phi = pos.phi();
00401      //     double et  = itb->energy() * sin(the);
00402      double DR1 = deltaR(genpions[0].eta(),genpions[0].phi(),eta,phi);
00403      double DR2 = deltaR(genpions[1].eta(),genpions[1].phi(),eta,phi);
00404      /*
00405      cout <<" ECAL barrel rechit, energy = " << itb->energy()
00406           <<" eta = " << pos.eta()
00407           <<" phi = " << pos.phi() 
00408           <<" theta = " << pos.theta() 
00409           <<" et = " << et << endl;
00410      */
00411      if(DR1 < 0.5) {
00412        eCalo1  = eCalo1  + itb->energy();
00413      }
00414      if(DR2 < 0.5) {
00415        eCalo2  = eCalo2  + itb->energy();
00416      }
00417    }
00418 
00419    for (itb=endcapRecHitsHandle->begin(); itb!=endcapRecHitsHandle->end(); itb++) {
00420      GlobalPoint pos = geo->getPosition(itb->detid());
00421      double eta = pos.eta();
00422      double phi = pos.phi();
00423      //     double the = pos.theta();
00424      //     double et  = itb->energy() * sin(the);
00425      double DR1 = deltaR(genpions[0].eta(),genpions[0].phi(),eta,phi);
00426      double DR2 = deltaR(genpions[1].eta(),genpions[1].phi(),eta,phi);
00427      if(DR1 < 0.5) {
00428        eCalo1  = eCalo1  + itb->energy();
00429      }
00430      if(DR2 < 0.5) {
00431        eCalo2  = eCalo2  + itb->energy();
00432      }
00433      /*
00434      cout <<" ECAL endcap rechit, energy = " << itb->energy()
00435           <<" eta = " << pos.eta()
00436           <<" phi = " << pos.phi() 
00437           <<" theta = " << pos.theta() 
00438           <<" et = " << et << endl;
00439      */
00440    }
00441 
00442    edm::Handle<HBHERecHitCollection> hbhe;
00443    iEvent.getByLabel(hbheInputSrc,hbhe);
00444    const HBHERecHitCollection Hithbhe = *(hbhe.product());
00445 
00446    //   cout <<" ===> ZSP HBHERecHitCollection size = " << Hithbhe.size() << endl;
00447    //   cout <<" ===> NO ZSP HBHERecHitCollection size = " << HithbheR.size() << endl;
00448 
00449    for(HBHERecHitCollection::const_iterator hbheItr=Hithbhe.begin(); hbheItr!=Hithbhe.end(); hbheItr++) {
00450      GlobalPoint pos = geo->getPosition( hbheItr->detid()); 
00451      double eta = pos.eta();
00452      double phi = pos.phi();
00453      //     double the = pos.theta();
00454      //     double et  = hbheItr->energy() * sin(the);
00455      double DR1 = deltaR(genpions[0].eta(),genpions[0].phi(),eta,phi);
00456      double DR2 = deltaR(genpions[1].eta(),genpions[1].phi(),eta,phi);
00457      if(DR1 < 0.5) {
00458        eCalo1  = eCalo1  + hbheItr->energy();
00459      }
00460      if(DR2 < 0.5) {
00461        eCalo2  = eCalo2  + hbheItr->energy();
00462      }
00463      /*
00464      cout <<" HBHE rechit, energy = " << hbheItr->energy()
00465           <<" eta = " << pos.eta()
00466           <<" phi = " << pos.phi() 
00467           <<" theta = " << pos.theta() 
00468           <<" et = " << et << endl;
00469      */
00470    }
00471 
00472    /*
00473   // wrong case for no ZSP
00474    double etCalo1T = 0.;
00475    double eCalo1T = 0.;
00476    double etCalo2T = 0.;
00477    double eCalo2T = 0.;
00478    //
00479    edm::Handle<CaloTowerCollection> towerHandle;
00480    iEvent.getByLabel(towermakerSrc, towerHandle);
00481    const CaloTowerCollection* towers = towerHandle.product();
00482    for(CaloTowerCollection::const_iterator towerItr = towers->begin(); 
00483        towerItr != towers->end(); ++towerItr) {
00484      
00485      double towerEt  = towerItr->et();
00486      double towerE   = towerItr->energy();
00487      double towerEta = towerItr->eta();
00488      double towerPhi = towerItr->phi();
00489 
00490      double etaMC1   = genpions[0].eta();
00491      double phiMC1   = genpions[0].phi();
00492 
00493      double etaMC2   = genpions[1].eta();
00494      double phiMC2   = genpions[1].phi();
00495      
00496      double DR1 = deltaR(genpions[0].eta(),genpions[0].phi(),towerItr->eta(),towerItr->phi());
00497      double DR2 = deltaR(genpions[1].eta(),genpions[1].phi(),towerItr->eta(),towerItr->phi());
00498 
00499      if(DR1 < 0.5) {
00500        eCalo1T = eCalo1T + towerItr->energy();
00501            cout <<" gen eta1 = " << genpions[0].eta()
00502             <<" tower eta = " << towerItr->eta()
00503             <<" gen phi1 = " << genpions[0].phi()
00504             <<" tower phi = " << towerItr->phi() 
00505             <<" DR1 = " << DR1 
00506             <<" tower Et = " << towerItr->et() 
00507             <<" etCalo1 = " << etCalo1 
00508             <<" SimPt1 = " << genpions[0].perp() << endl;
00509          }
00510      if(DR2 < 0.5) {
00511        eCalo2T = eCalo2T + towerItr->energy();
00512            cout <<" gen eta2 = " << genpions[1].eta()
00513             <<" tower eta = " << towerItr->eta()
00514             <<" gen phi2 = " << genpions[1].phi()
00515             <<" tower phi = " << towerItr->phi() 
00516             <<" DR2 = " << DR2
00517             <<" tower Et = " << towerItr->et()
00518             <<" etCalo2 = " << etCalo2 
00519             <<" SimPt2 = " << genpions[1].perp() << endl;
00520          }
00521    }
00522 */
00523    etCalo1 = eCalo1 * sin(genpions[0].theta());
00524    etCalo2 = eCalo2 * sin(genpions[1].theta());
00525 
00526    /*
00527    etCalo1T = eCalo1T * sin(genpions[0].theta());
00528    etCalo2T = eCalo2T * sin(genpions[1].theta());
00529    
00530    cout  <<" eCalo1 = " << eCalo1 <<" eCalo1T = " << eCalo1T
00531          <<" etCalo1 = " << etCalo1 <<" etCalo1T = " << etCalo1T
00532          <<" eCalo2 = " << eCalo2 <<" eCalo2T = " << eCalo2T
00533          <<" etCalo2 = " << etCalo2 <<" etCalo2T = " << etCalo2T << endl;
00534    */
00535 
00536    /*
00537   // wrong case for no ZSP
00538    // get calo towers and collect Et in cone 0.5 around every MC particle 
00539    edm::Handle<CandidateCollection> calotowers;
00540    iEvent.getByLabel(calotowersSrc, calotowers);   
00541    const CandidateCollection* inputCol = calotowers.product();
00542    CandidateCollection::const_iterator candidate;
00543    for( candidate = inputCol->begin(); candidate != inputCol->end(); ++candidate )
00544      {
00545        double phi   = candidate->phi();
00546        double theta = candidate->theta();
00547        double eta   = candidate->eta();
00548        double e     = candidate->energy();
00549        double et    = e*sin(theta);
00550 
00551        cout <<" calo towers: phi = " << phi
00552             <<" eta = " << eta
00553             <<" et = " << et << endl;
00554 
00555        HepLorentzVector tower(candidate->px(),
00556                               candidate->py(),
00557                               candidate->pz(),
00558                               candidate->energy());
00559        double DR1 = genpions[0].deltaR(tower);
00560        double DR2 = genpions[1].deltaR(tower);
00561        if(DR1 < 0.5) {
00562          etCalo1 = etCalo1 + candidate->pt();
00563          eCalo1 = eCalo1 + candidate->energy();
00564        }
00565        if(DR2 < 0.5) {
00566          etCalo2 = etCalo2 + candidate->pt();
00567          eCalo2 = eCalo2 + candidate->energy();
00568        }
00569      }
00570    */
00571 
00572    //get simtrack info
00573    std::vector<SimTrack> theSimTracks;
00574    edm::Handle<SimTrackContainer> SimTk;
00575    iEvent.getByLabel("g4SimHits",SimTk);
00576    theSimTracks.insert(theSimTracks.end(),SimTk->begin(),SimTk->end());
00577    //   cout <<" number of SimTracks = " << theSimTracks.size() << endl;
00578 
00579    edm::Handle<SiPixelRecHitCollection> pxlrecHitColl;
00580    iEvent.getByLabel(pxlhitsSrc, pxlrecHitColl);
00581    //   cout <<"  Size of pxl rec hit collection = " << (pxlrecHitColl.product())->size() << endl;
00582    
00583    // initiate track hit associator
00584    std::vector<PSimHit> matched;
00585    std::vector<unsigned int> SimTrackIds;
00586    // correct
00587    //   hitAssociator->init(iEvent);
00588 
00589    // track collection
00590     //   Handle<TrackCollection> tracks;
00591    Handle<TrackCollection> tracks;
00592    iEvent.getByLabel(tracksSrc, tracks);
00593    //   cout << "====> number of reco tracks "<< tracks->size() << endl;
00594 
00595    // pixel track collection
00596    Handle<TrackCollection> pxltracks;
00597    iEvent.getByLabel(pxltracksSrc, pxltracks);
00598    //   cout << "====> number of pxl tracks "<< pxltracks->size() << endl;
00599 
00600    //   cout <<" Sim Track size = " << theSimTracks.size() << endl;
00601 
00602    for(size_t j = 0; j < theSimTracks.size(); j++){
00603      /*
00604      cout <<" sim track j = " << j
00605           <<" track mom = " << theSimTracks[j].momentum().pt() 
00606           <<" genpartIndex = " << theSimTracks[j].genpartIndex()
00607           <<" type = " << theSimTracks[j].type()
00608           <<" noGenpart = " << theSimTracks[j].noGenpart() 
00609           <<" simTrackId = " << theSimTracks[j].trackId() << endl;
00610      */
00611    }
00612 
00613     int t = 0;
00614 
00615     std::string theTrackQuality = "highPurity";
00616     reco::TrackBase::TrackQuality trackQuality_=reco::TrackBase::qualityByName(theTrackQuality);
00617 
00618     ptSim1     = genpions[0].perp(); 
00619     etaSim1    = genpions[0].eta(); 
00620     phiSim1    = genpions[0].phi();
00621 
00622     ptSim2     = genpions[1].perp(); 
00623     etaSim2    = genpions[1].eta(); 
00624     phiSim2    = genpions[1].phi();
00625 
00626     //    cout <<" Reco track size = " << tracks->size() << endl;
00627 
00628     for(TrackCollection::const_iterator track = tracks->begin();
00629         track != tracks->end(); track++) {
00630       //     const reco::TrackExtraRef & trkExtra = track->extra();
00631 
00632       int trkQuality = track->quality(trackQuality_);
00633       int trkNVhits  = track->numberOfValidHits();
00634       //      cout <<" track quality = " << trkQuality
00635       //           <<" number of valid hits = " << trkNVhits << endl;
00636 
00637       double eECAL7x7i = -1000.; 
00638       double eECAL11x11i = -1000.; 
00639       double eHCAL3x3i = -1000.; 
00640       double eHCAL5x5i = -1000.; 
00641 
00642       const FreeTrajectoryState fts = trackAssociator_->getFreeTrajectoryState(iSetup, *track);
00643       TrackDetMatchInfo info = trackAssociator_->associate(iEvent, iSetup, fts, parameters_);
00644       if( info.isGoodEcal != 0 ) {
00645         const GlobalPoint point(info.trkGlobPosAtEcal.x(),info.trkGlobPosAtEcal.y(),info.trkGlobPosAtEcal.z());
00646         double etaimp = fabs(point.eta());
00647         /*
00648           cout <<" on ECAL x = " << info.trkGlobPosAtEcal.x()
00649           <<" y = " << info.trkGlobPosAtEcal.y()
00650           <<" z = " << info.trkGlobPosAtEcal.z() 
00651           <<" eta = " << etaimp
00652           <<" phi = " << point.phi() << endl;
00653         */
00654         // ECAL barrel or endcap and closest ecal cell
00655         if(etaimp < 1.479) {
00656           const DetId ClosestCell = gEB->getClosestCell(point);
00657           //      EBRecHitCollection::const_iterator itEB = barrelRecHitsHandle->find(ClosestCell);
00658           //      if(itEB != barrelRecHitsHandle->end()) {
00659           //        cout <<" barrel energy of closest hit = " << itEB->energy() << endl;
00660           CaloNavigator<DetId> theNavigator(ClosestCell,theEBTopology);
00661           //        EcalBarrelNavigator theNavigator(ClosestCell,theEBTopology);
00662           eECAL7x7i = eECALmatrix(theNavigator,barrelRecHitsHandle,3,3);
00663           eECAL11x11i = eECALmatrix(theNavigator,barrelRecHitsHandle,5,5);
00664           //      cout <<" barrel energy in 7x7 = " << eECAL7x7i << endl;
00665           //      }
00666         } else {
00667           const DetId ClosestCell = gEE->getClosestCell(point);
00668           //      EERecHitCollection::const_iterator itEE = endcapRecHitsHandle->find(ClosestCell);
00669           //      if(itEE != endcapRecHitsHandle->end()) {
00670           CaloNavigator<DetId> theNavigator(ClosestCell,theEETopology);
00671           //        EcalEndcapNavigator theNavigator(ClosestCell,theEETopology);
00672           eECAL7x7i = eECALmatrix(theNavigator,endcapRecHitsHandle,3,3);
00673           eECAL11x11i = eECALmatrix(theNavigator,endcapRecHitsHandle,5,5);
00674           //      cout <<" endcap energy in 7x7 ECAL cells = " << eECAL7x7i << endl;
00675           //      }
00676         }
00677         // closet HCAL cell
00678         
00679         const DetId ClosestCell = gHBHE->getClosestCell(point);
00680         HcalDetId hcd = ClosestCell;
00681         if(abs(hcd.ieta()) <= 25) {
00682           /*
00683             cout <<" eta inp = " << etaimp
00684             <<" Hcal closest cell ieta = " << hcd.ieta()
00685             <<" iphi = " << hcd.iphi()
00686             <<" depth = " << hcd.depth()
00687             <<" subdet = " << hcd.subdet() << endl;
00688           */
00689           eHCAL3x3i = eHCALmatrix(theHBHETopology, ClosestCell, Hithbhe,1,1);  
00690           eHCAL5x5i = eHCALmatrix(theHBHETopology, ClosestCell, Hithbhe,2,2);  
00691         }
00692         /*
00693           HBHERecHitCollection::const_iterator hbheItr = Hithbhe.find(ClosestCell);
00694           if(hbheItr != Hithbhe.end()) {
00695           cout <<" energy of closest hit = " << hbheItr->energy() << endl;
00696           }
00697           cout <<" before navigator " << endl;
00698           CaloNavigator<DetId> theNavigator(ClosestCell,theHBHETopology);
00699           cout <<" after navigator " << endl;
00700           
00701           cout <<" detector = " << ClosestCell.det()
00702           <<" sub det = " << ClosestCell.subdetId()
00703           <<" idsubdet = " << ClosestCell.subdet() 
00704           <<" ieta = " << ClosestCell.ieta()
00705           <<" iphi = " << ClosestCell.iphi() << endl;
00706           
00707           //    const CaloSubdetectorTopology* theHBHETopology = theCaloTopology->getSubdetectorTopology(ClosestCell);
00708           if(!theHBHETopology) {cout <<" no pointer to HcalTopology " << endl;}
00709           //        std::vector<DetId> vNeighboursDetId = theHBHETopology->north(ClosestCell);
00710           DetId next = theNavigator.north();
00711           cout <<" after north " << endl;
00712         */
00713       }
00714 
00715       //      size_t Nhits = track->recHitsSize();
00716       //      size_t NpxlHits = track->hitPattern().numberOfValidPixelHits();
00717       SimTrackIds.clear();
00718       /*
00719       cout <<"    " << endl;
00720       cout <<" -> track " << t
00721            <<" pt = " << track->pt()
00722            <<" eta = " << track->eta()
00723            <<" phi = " << track->phi()
00724            <<" Nhits = " << track->recHitsSize()
00725            <<" NpxlHits = " << track->hitPattern().numberOfValidPixelHits()
00726           <<" ch2 = " << track->normalizedChi2()
00727            <<" lost hits = " << track->numberOfLostHits()
00728            <<" valid hits = " << track->numberOfValidHits()
00729            <<" outerP = " << track->outerPt()
00730            <<" outer X = " << track->outerPosition().x() << endl;
00731       */
00732       size_t ih = 0;
00733       for(trackingRecHit_iterator rhit = track->recHitsBegin();
00734           rhit != track->recHitsEnd(); ++rhit) {
00735         //       const TrackingRecHitRef rhitRef = trkExtra->recHit(ih);
00736         if((*rhit)->isValid()) {
00737           //try SimHit matching
00738           float mindist = 999999;
00739           float dist;
00740           PSimHit closest;
00741           matched.clear();        
00742           matched = hitAssociator->associateHit((**rhit));
00743           /*
00744           cout <<" " << endl;
00745           cout <<" -----> irhit = " << ih
00746                <<" rhitx = " << (*rhit)->localPosition().x()
00747                <<" rhity = " << (*rhit)->localPosition().y()
00748                <<" rhitz = " << (*rhit)->localPosition().z() << endl;
00749           */
00750           if(!matched.empty()){
00751             //      cout << "  size of matched PSimHit " << matched.size() << endl;
00752             int ish = 0;
00753             for(vector<PSimHit>::const_iterator m = matched.begin(); m < matched.end(); m++) {
00754               if((*m).localPosition().y() < 0.0001) {
00755                 dist = fabs((*rhit)->localPosition().x() - (*m).localPosition().x());
00756               } else {
00757                 float distx = (*rhit)->localPosition().x() - (*m).localPosition().x();
00758                 float disty = (*rhit)->localPosition().y() - (*m).localPosition().y();
00759                 dist = sqrt(distx*distx + disty*disty);
00760               }
00761               /*
00762               cout <<"  --> isimhit = " << ish 
00763                    <<" shitx = " << (*m).localPosition().x()
00764                    <<" shity = " << (*m).localPosition().y()
00765                    <<" shitz = " << (*m).localPosition().z()
00766                    <<" sim track id = " << (*m).trackId() << endl; 
00767               */
00768               ish++;
00769               if(dist < mindist) {
00770                 mindist = dist;
00771                 closest = (*m);
00772               }
00773             }
00774             int trkid = closest.trackId();
00775             //      if(trkid <= (int) theSimTracks.size()) {
00776               //              if(theSimTracks[trkid-1].noGenpart() == 0) {
00777             SimTrackIds.push_back(trkid);
00778               /*
00779               cout <<"          closest simtrack id = " << closest.trackId() 
00780                    <<" min dist = " << mindist << endl;
00781               */
00782               //              }
00783               //            }
00784           } 
00785         }
00786         ih++;
00787       }
00788       // count for sim track with max number of hits belonging to reco track 
00789       int nmax = 0;
00790       int idmax = -1;
00791       for(size_t j=0; j<SimTrackIds.size(); j++){
00792         int n =0;
00793         n = std::count(SimTrackIds.begin(), SimTrackIds.end(), SimTrackIds[j]);
00794         if(n>nmax){
00795           nmax = n;
00796           idmax = SimTrackIds[j];
00797         }
00798       }
00799       // track purity
00800       float purity = 0;
00801       if(SimTrackIds.size() != 0) {
00802         float totsim = nmax;
00803         float tothits = track->recHitsSize();//include pixel as well..
00804         purity = totsim/tothits ;
00805         /*
00806         cout << " ==> Track number # " << t 
00807              << "# of rechits = " << track->recHitsSize() 
00808              << " best matched simtrack id= " << idmax 
00809              << " purity = " << purity << endl;
00810              .*/
00811         //           << " sim track mom = " << theSimTracks[idmax-1].momentum().pt() << endl;
00812       } else {
00813         //      cout <<"  !!!!  no HepMC particles associated with this pixel triplet " << endl;
00814       }
00815       // matching tracks with gen pions
00816       HepLorentzVector tracki(track->px(), track->py(), track->pz(), track->p());
00817       double DR1 = genpions[0].deltaR(tracki);
00818       if(DR1 < drTrk1) {
00819         ptTrk1     = tracki.perp();
00820         etaTrk1    = tracki.eta(); 
00821         phiTrk1    = tracki.phi();
00822         purityTrk1 = purity;
00823         drTrk1     = DR1;
00824         e1ECAL7x7  = eECAL7x7i;
00825         e1ECAL11x11= eECAL11x11i;
00826         e1HCAL3x3  = eHCAL3x3i;
00827         e1HCAL5x5  = eHCAL5x5i;
00828         trkQuality1 = trkQuality; 
00829         trkNVhits1 = trkNVhits;
00830         idmax1 = idmax;
00831       }
00832       double DR2 = genpions[1].deltaR(tracki);
00833       if(DR2 < drTrk2) {
00834         ptTrk2     = tracki.perp();
00835         etaTrk2    = tracki.eta(); 
00836         phiTrk2    = tracki.phi();
00837         purityTrk2 = purity;
00838         drTrk2     = DR2;
00839         e2ECAL7x7  = eECAL7x7i;
00840         e2ECAL11x11= eECAL11x11i;
00841         e2HCAL3x3  = eHCAL3x3i;
00842         e2HCAL5x5  = eHCAL5x5i;
00843         trkQuality2 = trkQuality; 
00844         trkNVhits2 = trkNVhits; 
00845         idmax2 = idmax;
00846       }
00847       t++;
00848     }
00849 
00850     /*
00851     cout <<" " << endl;
00852 
00853     cout <<" best matched track with 1st pion: ptTrk1 = " << ptTrk1
00854          <<" etaTrk1 = " << etaTrk1
00855          <<" phiTrk1 = " << phiTrk1
00856          <<" DR1 = " << drTrk1
00857          <<" purityTrk1 = " << purityTrk1 << endl;
00858 
00859     cout <<" best matched track with 2nd pion: ptTrk2 = " << ptTrk2
00860          <<" etaTrk2 = " << etaTrk2
00861          <<" phiTrk2 = " << phiTrk2
00862          <<" DR2 = " << drTrk2
00863          <<" purityTrk1 = " << purityTrk2 << endl;
00864     */
00865 
00866     // loop over the pixel triplet collection
00867     //    cout <<"   " << endl;
00868     //    cout <<" Loop over pixel triplet collection " << endl;
00869     t = 0;
00870     for(TrackCollection::const_iterator pxltrack = pxltracks->begin();
00871         pxltrack != pxltracks->end(); ++pxltrack) {
00872       SimTrackIds.clear();
00873       //      size_t Nhits = pxltrack->recHitsSize();
00874       //      size_t NpxlHits = pxltrack->hitPattern().numberOfValidPixelHits();
00875       /*
00876       cout <<"  " << endl;
00877       cout <<" pixel track " << t
00878            <<" pt = " << pxltrack->pt()
00879            <<" eta = " << pxltrack->eta()
00880            <<" phi = " << pxltrack->phi()
00881            <<" Nhits = " << Nhits 
00882            <<" NpxlHits = " << NpxlHits 
00883            <<" ch2 = " << pxltrack->normalizedChi2()
00884            <<" lost hits = " << pxltrack->numberOfLostHits()
00885            <<" valid hits = " << pxltrack->numberOfValidHits()
00886            <<" outerP = " << pxltrack->outerPt()
00887            <<" outer X = " << pxltrack->outerPosition().x() << endl;
00888       */
00889       size_t ih = 0;
00890       for(trackingRecHit_iterator rhit = pxltrack->recHitsBegin();
00891           rhit != pxltrack->recHitsEnd(); ++rhit) {
00892         if((*rhit)->isValid()) {
00893           //try SimHit matching
00894           float mindist = 999999;
00895           float dist;
00896           PSimHit closest;
00897           matched.clear();        
00898           matched = hitAssociator->associateHit((**rhit));
00899           /*
00900           cout <<" " << endl;
00901           cout <<" -----> irhit = " << ih
00902                <<" rhitx = " << (*rhit)->localPosition().x()
00903                <<" rhity = " << (*rhit)->localPosition().y() << endl;
00904           */
00905           if(!matched.empty()){
00906             int ish = 0;
00907             for(vector<PSimHit>::const_iterator m = matched.begin(); m < matched.end(); m++) {
00908               float distx = (*rhit)->localPosition().x() - (*m).localPosition().x();
00909               float disty = (*rhit)->localPosition().y() - (*m).localPosition().y();
00910               dist = sqrt(distx*distx + disty*disty);
00911               /*
00912               cout <<"  --> isimhit = " << ish 
00913                    <<" shitx = " << (*m).localPosition().x()
00914                    <<" shity = " << (*m).localPosition().y()
00915                    <<" sim track id = " << (*m).trackId() << endl;
00916               */
00917               ish++;
00918               if(dist < mindist) {
00919                 mindist = dist;
00920                 closest = (*m);
00921               }
00922             }
00923             int trkid = closest.trackId();
00924             if(trkid <= (int) theSimTracks.size()) {
00925               if(theSimTracks[trkid-1].noGenpart() == 0) {
00926                 SimTrackIds.push_back(trkid);
00927                 //              cout <<"          closest simtrack id = " << closest.trackId() 
00928                 //                   <<" min dist = " << mindist << endl;
00929                 cout <<" " << endl;
00930               }
00931             } else {
00932               //              cout <<" track ID > size of SimTracks " << endl;
00933             }
00934           } 
00935         }
00936         ih++;
00937       }
00938       // count for sim track with max number of hits belonging to reco track 
00939       int nmax = 0;
00940       int idmax = -1;
00941       for(size_t j=0; j<SimTrackIds.size(); j++){
00942         int n =0;
00943         n = std::count(SimTrackIds.begin(), SimTrackIds.end(), SimTrackIds[j]);
00944         if(n>nmax){
00945           nmax = n;
00946           idmax = SimTrackIds[j];
00947         }
00948       }
00949       float purity = 0;
00950       if(SimTrackIds.size() != 0) {
00951         float totsim = nmax;
00952         float tothits = pxltrack->recHitsSize();//include pixel as well..
00953         purity = totsim/tothits ;
00954         /*
00955         cout << " Track number # " << t 
00956              << "# of rechits = " << pxltrack->recHitsSize() 
00957              << " matched simtrack id= " << idmax 
00958              << " purity = " << purity 
00959              << " sim track mom = " << theSimTracks[idmax-1].momentum().perp() << endl;
00960         */
00961       } else {
00962         //      cout <<"  !!!!  no HepMC particles associated with this pixel triplet " << endl;
00963       }
00964       // matching tracks with gen pions
00965       HepLorentzVector pxltracki(pxltrack->px(), pxltrack->py(), pxltrack->pz(), pxltrack->p());
00966       double DR1 = genpions[0].deltaR(pxltracki);
00967       if(DR1 < drPxl1) {
00968         ptPxl1     = pxltracki.perp();
00969         etaPxl1    = pxltracki.eta(); 
00970         phiPxl1    = pxltracki.phi();
00971         purityPxl1 = purity;
00972         drPxl1     = DR1;
00973       }
00974       double DR2 = genpions[1].deltaR(pxltracki);
00975       if(DR2 < drPxl2) {
00976         ptPxl2     = pxltracki.perp();
00977         etaPxl2    = pxltracki.eta(); 
00978         phiPxl2    = pxltracki.phi();
00979         purityPxl2 = purity;
00980         drPxl2     = DR2;
00981       }
00982       t++;
00983     }
00984 
00985     /*
00986     cout <<" " << endl;
00987 
00988     cout <<" best matched track with 1st pion: ptPxl1 = " << ptPxl1
00989          <<" etaPxl1 = " << etaPxl1
00990          <<" phiPxl1 = " << phiPxl1
00991          <<" DR1 = " << drPxl1
00992          <<" purityPxl1 = " << purityPxl1 << endl;
00993 
00994     cout <<" best matched track with 2nd pion: ptPxl2 = " << ptPxl2
00995          <<" etaPxl2 = " << etaPxl2
00996          <<" phiPxl2 = " << phiPxl2
00997          <<" DR2 = " << drPxl2
00998          <<" purityPxl1 = " << purityPxl2 << endl;
00999     */
01000 
01001    // fill tree
01002     t1->Fill();
01003     //    cout <<" idmax1 = " << idmax1 <<" idmax2 = " << idmax2 << endl;
01004 
01005     delete hitAssociator;
01006 }
01007 
01008 
01009 // ------------ method called once each job just before starting event loop  ------------
01010 void 
01011 SinglePionEfficiencyNew::beginJob(const edm::EventSetup& iSetup)
01012 {
01013 
01014   using namespace edm;
01015 
01016   // TrackerHitAssociator::TrackerHitAssociator(const edm::Event&, const edm::ParameterSet&)
01017   // TrackerHitAssociator::TrackerHitAssociator(const edm::Event&)
01018 
01019   // create tree
01020   hOutputFile   = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
01021   t1 = new TTree("t1","analysis tree");
01022   // sim tracks
01023   t1->Branch("ptSim1",&ptSim1,"ptSim1/D");
01024   t1->Branch("etaSim1",&etaSim1,"etaSim1/D");
01025   t1->Branch("phiSim1",&phiSim1,"phiSim1/D");
01026   t1->Branch("ptSim2",&ptSim2,"ptSim2/D");
01027   t1->Branch("etaSim2",&etaSim2,"etaSim2/D");
01028   t1->Branch("phiSim2",&phiSim2,"phiSim2/D");
01029   // reco tracks
01030   t1->Branch("ptTrk1",&ptTrk1,"ptTrk1/D");
01031   t1->Branch("etaTrk1",&etaTrk1,"etaTrk1/D");
01032   t1->Branch("phiTrk1",&phiTrk1,"phiTrk1/D");
01033   t1->Branch("drTrk1",&drTrk1,"drTrk1/D");
01034   t1->Branch("purityTrk1",&purityTrk1,"purityTrk1/D");
01035   t1->Branch("ptTrk2",&ptTrk2,"ptTrk2/D");
01036   t1->Branch("etaTrk2",&etaTrk2,"etaTrk2/D");
01037   t1->Branch("phiTrk2",&phiTrk2,"phiTrk2/D");
01038   t1->Branch("drTrk2",&drTrk2,"drTrk2/D");
01039   t1->Branch("purityTrk2",&purityTrk2,"purityTrk2/D");
01040   // pxl tracks
01041   t1->Branch("ptPxl1",&ptPxl1,"ptPxl1/D");
01042   t1->Branch("etaPxl1",&etaPxl1,"etaPxl1/D");
01043   t1->Branch("phiPxl1",&phiPxl1,"phiPxl1/D");
01044   t1->Branch("drPxl1",&drPxl1,"drPxl1/D");
01045   t1->Branch("purityPxl1",&purityPxl1,"purityPxl1/D");
01046   t1->Branch("ptPxl2",&ptPxl2,"ptPxl2/D");
01047   t1->Branch("etaPxl2",&etaPxl2,"etaPxl2/D");
01048   t1->Branch("phiPxl2",&phiPxl2,"phiPxl2/D");
01049   t1->Branch("drPxl2",&drPxl2,"drPxl2/D");
01050   t1->Branch("purityPxl2",&purityPxl2,"purityPxl2/D");
01051   // Et in calo cones around MC jets
01052   t1->Branch("etCalo1",&etCalo1,"etCalo1/D");
01053   t1->Branch("etCalo2",&etCalo2,"etCalo2/D");
01054   t1->Branch("eCalo1",&eCalo1,"eCalo1/D");
01055   t1->Branch("eCalo2",&eCalo2,"eCalo2/D");
01056   // Et in 7x7, 11x11 ECAL crystals and 3x3, 5x5 HCAL towers
01057   t1->Branch("e1ECAL7x7",&e1ECAL7x7,"e1ECAL7x7/D");
01058   t1->Branch("e1ECAL11x11",&e1ECAL11x11,"e1ECAL11x11/D");
01059   t1->Branch("e2ECAL7x7",&e2ECAL7x7,"e2ECAL7x7/D");
01060   t1->Branch("e2ECAL11x11",&e2ECAL11x11,"e2ECAL11x11/D");
01061   t1->Branch("e1HCAL3x3",&e1HCAL3x3,"e1HCAL3x3/D");
01062   t1->Branch("e1HCAL5x5",&e1HCAL5x5,"e1HCAL5x5/D");
01063   t1->Branch("e2HCAL3x3",&e2HCAL3x3,"e2HCAL3x3/D");
01064   t1->Branch("e2HCAL5x5",&e2HCAL5x5,"e2HCAL5x5/D");
01065   // tracker quality and number of hits
01066   t1->Branch("trkQuality1",&trkQuality1,"trkQuality1/I");
01067   t1->Branch("trkQuality2",&trkQuality2,"trkQuality2/I");
01068   t1->Branch("trkNVhits1",&trkNVhits1,"trkNVhits1/I");
01069   t1->Branch("trkNVhits2",&trkNVhits2,"trkNVhits2/I");
01070   t1->Branch("idmax1",&idmax1,"idmax1/I");
01071   t1->Branch("idmax2",&idmax2,"idmax2/I");
01072   //
01073   return ;
01074 }
01075 
01076 // ------------ method called once each job just after ending the event loop  ------------
01077 void 
01078 SinglePionEfficiencyNew::endJob() {
01079 
01080   //  delete hitAssociator;
01081   hOutputFile->Write() ;
01082   hOutputFile->Close() ;
01083   return ;
01084 
01085 }
01086 
01087 double SinglePionEfficiencyNew::eECALmatrix(CaloNavigator<DetId>& navigator,edm::Handle<EcalRecHitCollection>& hits, int ieta, int iphi)
01088 {
01089   DetId thisDet;
01090   std::vector<DetId> dets;
01091   dets.clear();
01092   EcalRecHitCollection::const_iterator hit;
01093   double energySum = 0.0;
01094   
01095   for (int dx = -ieta; dx < ieta+1; ++dx)
01096     {
01097       for (int dy = -iphi; dy < iphi+1; ++dy)
01098         {
01099           //std::cout << "dx, dy " << dx << ", " << dy << std::endl;
01100           thisDet = navigator.offsetBy(dx, dy);
01101           navigator.home();
01102           
01103           if (thisDet != DetId(0))
01104             {
01105               hit = hits->find(thisDet);
01106               if (hit != hits->end()) 
01107                 {
01108                   dets.push_back(thisDet);
01109                   energySum += hit->energy();
01110                 }
01111             }
01112         }
01113     }
01114   return energySum;
01115 }
01116 
01117 double SinglePionEfficiencyNew::eHCALmatrix(const HcalTopology* topology, const DetId& det, const HBHERecHitCollection& hits, int ieta, int iphi)
01118 {
01119   double energy = 0;
01120 
01121   if(ieta > 2) {
01122     cout <<" no matrix > 5x5 is coded ! " << endl;
01123     return energy;
01124   }
01125   // used dets during building the matrix
01126   std::vector<DetId> dets;
01127   dets.clear();
01128 
01129   //central tower
01130   HBHERecHitCollection::const_iterator hbheItr = hits.find(det);
01131   if(hbheItr != hits.end()) {
01132     energy = hbheItr->energy();
01133     dets.push_back(det);
01134   }
01135   HcalDetId depth = det;
01136   // max. three depths can be in endcap and we go to 2nd and 3rd from 1st where we are now
01137   for(int idepth = 0; idepth < 2; idepth++) {
01138     std::vector<DetId> vUpDetId = topology->up(depth);
01139     if(vUpDetId.size() != 0) {
01140       int n = std::count(dets.begin(),dets.end(),vUpDetId[0]);
01141       if(n == 0) {
01142         dets.push_back(vUpDetId[0]);
01143         HBHERecHitCollection::const_iterator hbheItrUp = hits.find(vUpDetId[0]);
01144         if(hbheItrUp != hits.end()) {
01145           energy = energy + hbheItrUp->energy();
01146         }
01147       }
01148       depth = vUpDetId[0];
01149     }
01150   }
01151 
01152   // go to east from central tower 
01153   std::vector<DetId> vNeighboursDetId = topology->east(det);
01154   energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01155   if(ieta == 2) {
01156     for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01157       std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
01158       energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01159     }
01160   }
01161   vNeighboursDetId.clear();
01162   // go to west from central tower 
01163   vNeighboursDetId = topology->west(det);
01164   energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01165   if(ieta == 2) {
01166     for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01167       std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
01168       energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01169     }
01170   }
01171   vNeighboursDetId.clear();
01172 
01173 
01174   // do steps in phi to north
01175   DetId detnorth = det;
01176   for (int inorth = 0; inorth < iphi; inorth++) {
01177     std::vector<DetId> NorthDetId = topology->north(detnorth);
01178     energy = energy + eHCALneighbours(NorthDetId, dets, topology, hits);
01179 
01180     // go to east  
01181     vNeighboursDetId = topology->east(NorthDetId[0]);
01182     energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01183     if(ieta == 2) {
01184       for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01185         std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
01186         energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01187       }
01188     }
01189     vNeighboursDetId.clear();
01190     // go to west 
01191     vNeighboursDetId = topology->west(NorthDetId[0]);
01192     energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01193     if(ieta == 2) {
01194       for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01195         std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
01196         energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01197       }
01198     }
01199     detnorth = NorthDetId[0];
01200     vNeighboursDetId.clear();
01201   }
01202 
01203   // do steps in phi to south
01204   DetId detsouth = det;
01205   for (int isouth = 0; isouth < iphi; isouth++) {
01206     std::vector<DetId> SouthDetId = topology->south(detsouth);
01207     energy = energy + eHCALneighbours(SouthDetId, dets, topology, hits);
01208 
01209     // go to east  
01210     vNeighboursDetId = topology->east(SouthDetId[0]);
01211     energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01212     if(ieta == 2) {
01213       for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01214         std::vector<DetId> vNeighboursDetIdc = topology->east(vNeighboursDetId[ii]);
01215         energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01216       }
01217     }
01218     vNeighboursDetId.clear();
01219     // go to west 
01220     vNeighboursDetId = topology->west(SouthDetId[0]);
01221     energy = energy + eHCALneighbours(vNeighboursDetId, dets, topology, hits);
01222     if(ieta == 2) {
01223       for (int ii = 0; ii < (int) vNeighboursDetId.size(); ii++) {
01224         std::vector<DetId> vNeighboursDetIdc = topology->west(vNeighboursDetId[ii]);
01225         energy = energy + eHCALneighbours(vNeighboursDetIdc, dets, topology, hits);
01226       }
01227     }
01228     detsouth = SouthDetId[0];
01229     vNeighboursDetId.clear();
01230   }
01231 
01232   // done
01233   /*
01234   HcalDetId cht = dets[0];
01235   if(abs(cht.ieta()) == 15 && cht.subdet() == 2) {
01236     cout <<" ==> Tower used " << endl; 
01237     double energy = 0.;
01238     for(int i = 0; i < dets.size(); i++) {
01239       HcalDetId hdet = dets[i];
01240       double hitenergy = 0.;
01241       HBHERecHitCollection::const_iterator hbheItr = hits.find(dets[i]);
01242       if(hbheItr != hits.end()) {
01243         hitenergy = hbheItr->energy();
01244         energy = energy + hitenergy; 
01245       }
01246       cout <<" subdet = " << hdet.subdet() 
01247            <<" ieta = " << hdet.ieta()
01248            <<" phi = " <<  hdet.iphi() 
01249            <<" idepth = " << hdet.depth() 
01250            <<" hitsenergy = " << hitenergy 
01251            <<" energy = " << energy << endl;
01252     }
01253   }
01254   */
01255   return energy;
01256 }
01257 
01258 double SinglePionEfficiencyNew::eHCALneighbours(std::vector<DetId>& vNeighboursDetId, std::vector<DetId>& dets, 
01259                                                 const HcalTopology* topology, const HBHERecHitCollection& hits)
01260 {
01261   double eHCALneighbour = 0.;
01262   for(int i = 0; i < (int) vNeighboursDetId.size(); i++) {
01263     int n = std::count(dets.begin(),dets.end(),vNeighboursDetId[i]);
01264     if(n != 0) continue; 
01265     dets.push_back(vNeighboursDetId[i]);
01266     HBHERecHitCollection::const_iterator hbheItr = hits.find(vNeighboursDetId[i]);
01267     if(hbheItr != hits.end()) {
01268       eHCALneighbour = eHCALneighbour + hbheItr->energy();
01269     }
01270     // go into depth (for endcap)
01271 
01272     HcalDetId depth = vNeighboursDetId[i];
01273     // max. three depths can be in endcap and we go to 2nd and 3rd from 1st where we are now
01274     for(int idepth = 0; idepth < 2; idepth++) {
01275       /*
01276       cout <<" idepth = " << idepth
01277            <<" ieta = " << depth.ieta()
01278            <<" iphi = " << depth.iphi()
01279            <<" depth = " << depth.depth() << endl;
01280       */
01281       std::vector<DetId> vUpDetId = topology->up(depth);
01282       if(vUpDetId.size() != 0) {
01283         int n = std::count(dets.begin(),dets.end(),vUpDetId[0]);
01284         if(n == 0) {
01285           dets.push_back(vUpDetId[0]);
01286           HBHERecHitCollection::const_iterator hbheItrUp = hits.find(vUpDetId[0]);
01287           if(hbheItrUp != hits.end()) {
01288             eHCALneighbour = eHCALneighbour + hbheItrUp->energy();
01289           }
01290         }
01291         depth = vUpDetId[0];
01292       }
01293     }
01294 
01295   }
01296   return eHCALneighbour;
01297 }
01298 
01299 //define this as a plug-in
01300 DEFINE_FWK_MODULE(SinglePionEfficiencyNew);

Generated on Tue Jun 9 17:39:33 2009 for CMSSW by  doxygen 1.5.4