CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_3_1/src/Alignment/OfflineValidation/plugins/EopTreeWriter.cc

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 //
00003 // Package:    EopTreeWriter
00004 // Class:      EopTreeWriter
00005 // 
00013 //
00014 // Original Author:  Holger Enderle
00015 //         Created:  Thu Dec  4 11:22:48 CET 2008
00016 // $Id: EopTreeWriter.cc,v 1.3 2012/03/02 07:46:48 mussgill Exp $
00017 //
00018 //
00019 
00020 // framework include files
00021 #include "FWCore/Framework/interface/Frameworkfwd.h"
00022 #include "FWCore/Framework/interface/EDAnalyzer.h"
00023 #include "FWCore/Framework/interface/EventSetup.h"
00024 #include "FWCore/Framework/interface/Event.h"
00025 #include "FWCore/Framework/interface/MakerMacros.h"
00026 #include "FWCore/Framework/interface/ESHandle.h"
00027 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00028 #include "FWCore/MessageLogger/interface/MessageLogger.h"
00029 
00030 #include "FWCore/ServiceRegistry/interface/Service.h"
00031 
00032 // user include files
00033 #include <DataFormats/TrackReco/interface/Track.h>
00034 #include "DataFormats/TrackReco/interface/TrackFwd.h"
00035 #include <TMath.h>
00036 #include <TH1.h>
00037 #include "TTree.h"
00038 
00039 #include "CommonTools/UtilAlgos/interface/TFileService.h"
00040 #include "CommonTools/Utils/interface/TFileDirectory.h"
00041 
00042 #include "TrackingTools/TrackAssociator/interface/TrackDetectorAssociator.h"
00043 #include "TrackingTools/TrackAssociator/interface/TrackAssociatorParameters.h"
00044 #include "TrackingTools/GeomPropagators/interface/PropagationExceptions.h"
00045 #include "TrackPropagation/SteppingHelixPropagator/interface/SteppingHelixPropagator.h"
00046 
00047 #include "DataFormats/Common/interface/Handle.h"
00048 #include "DataFormats/JetReco/interface/CaloJet.h"
00049 #include "DataFormats/CaloTowers/interface/CaloTowerFwd.h"
00050 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
00051 
00052 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00053 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00054 
00055 #include "DataFormats/HcalIsolatedTrack/interface/IsolatedPixelTrackCandidateFwd.h"
00056 #include "DataFormats/HcalIsolatedTrack/interface/IsolatedPixelTrackCandidate.h"
00057 #include "DataFormats/RecoCandidate/interface/RecoCaloTowerCandidate.h"
00058 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00059 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00060 #include "DataFormats/DTRecHit/interface/DTRecHitCollection.h"
00061 
00062 #include "Alignment/OfflineValidation/interface/EopVariables.h"
00063 
00064 //
00065 // class decleration
00066 //
00067 
00068 class EopTreeWriter : public edm::EDAnalyzer {
00069    public:
00070       explicit EopTreeWriter(const edm::ParameterSet&);
00071       ~EopTreeWriter();
00072 
00073 
00074    private:
00075       virtual void beginJob() ;
00076       virtual void analyze(const edm::Event&, const edm::EventSetup&);
00077       virtual void endJob() ;
00078 
00079       double getDistInCM(double eta1, double phi1, double eta2, double phi2);
00080 
00081       // ----------member data ---------------------------
00082       edm::InputTag src_;
00083 
00084       edm::Service<TFileService> fs_;
00085       TTree *tree_;
00086       EopVariables *treeMemPtr_;
00087       TrackDetectorAssociator trackAssociator_;
00088       TrackAssociatorParameters parameters_;
00089 };
00090 
00091 //
00092 // constants, enums and typedefs
00093 //
00094 
00095 //
00096 // static data member definitions
00097 //
00098 
00099 //
00100 // constructors and destructor
00101 //
00102 EopTreeWriter::EopTreeWriter(const edm::ParameterSet& iConfig) :
00103   src_(iConfig.getParameter<edm::InputTag>("src"))
00104 {
00105    //now do what ever initialization is needed
00106 
00107    // TrackAssociator parameters
00108    edm::ParameterSet parameters = iConfig.getParameter<edm::ParameterSet>("TrackAssociatorParameters");
00109    parameters_.loadParameters( parameters );
00110 
00111    tree_ = fs_->make<TTree>("EopTree","EopTree");
00112    treeMemPtr_ = new EopVariables;
00113    tree_->Branch("EopVariables", &treeMemPtr_); // address of pointer!
00114 }
00115 
00116 
00117 EopTreeWriter::~EopTreeWriter()
00118 {
00119  
00120    // do anything here that needs to be done at destruction time
00121    // (e.g. close files, deallocate resources etc.)
00122 
00123 }
00124 
00125 
00126 //
00127 // member functions
00128 //
00129 
00130 // ------------ method called to for each event  ------------
00131 void
00132 EopTreeWriter::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
00133 {
00134    using namespace edm;
00135 
00136    // get geometry
00137    edm::ESHandle<CaloGeometry> geometry;
00138    iSetup.get<CaloGeometryRecord>().get(geometry);
00139    const CaloGeometry* geo = geometry.product();
00140 //    const CaloSubdetectorGeometry* towerGeometry = 
00141 //      geo->getSubdetectorGeometry(DetId::Calo, CaloTowerDetId::SubdetId);
00142 
00143    // temporary collection of EB+EE recHits
00144    std::auto_ptr<EcalRecHitCollection> tmpEcalRecHitCollection(new EcalRecHitCollection);
00145    std::vector<edm::InputTag> ecalLabels_;
00146 
00147    edm::Handle<EcalRecHitCollection> tmpEc;
00148    bool ecalInAlca = iEvent.getByLabel(edm::InputTag("IsoProd","IsoTrackEcalRecHitCollection"),tmpEc);
00149    bool ecalInReco = iEvent.getByLabel(edm::InputTag("ecalRecHit","EcalRecHitsEB"),tmpEc)&& 
00150                      iEvent.getByLabel(edm::InputTag("ecalRecHit","EcalRecHitsEE"),tmpEc);
00151    if(ecalInAlca)ecalLabels_.push_back(edm::InputTag("IsoProd","IsoTrackEcalRecHitCollection"));
00152    else if(ecalInReco){
00153      ecalLabels_.push_back(edm::InputTag("ecalRecHit","EcalRecHitsEB"));
00154      ecalLabels_.push_back(edm::InputTag("ecalRecHit","EcalRecHitsEE"));
00155    }
00156    else throw cms::Exception("MissingProduct","can not find EcalRecHits");
00157 
00158    std::vector<edm::InputTag>::const_iterator i;
00159    for (i=ecalLabels_.begin(); i!=ecalLabels_.end(); i++) 
00160      {
00161        edm::Handle<EcalRecHitCollection> ec;
00162        iEvent.getByLabel(*i,ec);
00163        for(EcalRecHitCollection::const_iterator recHit = (*ec).begin(); recHit != (*ec).end(); ++recHit)
00164          {
00165            tmpEcalRecHitCollection->push_back(*recHit);
00166          }
00167      }      
00168    
00169    edm::Handle<reco::TrackCollection> tracks;
00170    iEvent.getByLabel(src_, tracks);
00171 
00172    edm::Handle<reco::IsolatedPixelTrackCandidateCollection> isoPixelTracks;
00173    edm::Handle<reco::IsolatedPixelTrackCandidateCollection> tmpPix;
00174    bool pixelInAlca = iEvent.getByLabel(edm::InputTag("IsoProd","HcalIsolatedTrackCollection"),tmpPix);
00175    if(pixelInAlca)iEvent.getByLabel(edm::InputTag("IsoProd","HcalIsolatedTrackCollection"),isoPixelTracks);
00176 
00177    Double_t trackemc1;
00178    Double_t trackemc3;
00179    Double_t trackemc5;
00180    Double_t trackhac1;
00181    Double_t trackhac3;
00182    Double_t trackhac5;
00183    Double_t maxPNearby;
00184    Double_t dist;
00185    Double_t EnergyIn;
00186    Double_t EnergyOut;
00187 
00188    parameters_.useMuon = false;
00189 
00190    if(pixelInAlca)
00191      if(isoPixelTracks->size()==0) return;
00192 
00193    for(reco::TrackCollection::const_iterator track = tracks->begin();track!=tracks->end();++track){
00194 
00195      bool noChargedTracks = true;
00196 
00197      if(track->p()<9.) continue;
00198 
00199      trackAssociator_.useDefaultPropagator();
00200      TrackDetMatchInfo info = trackAssociator_.associate(iEvent, iSetup, trackAssociator_.getFreeTrajectoryState(iSetup, *track), parameters_);
00201 
00202      trackemc1 = 0;
00203      trackemc3 = 0;
00204      trackemc5 = 0;
00205      trackhac1 = 0;
00206      trackhac3 = 0;
00207      trackhac5 = 0;
00208      
00209      trackemc1 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 0);
00210      trackemc3 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 1);
00211      trackemc5 = info.nXnEnergy(TrackDetMatchInfo::EcalRecHits, 2);
00212      trackhac1 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 0);
00213      trackhac3 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 1);
00214      trackhac5 = info.nXnEnergy(TrackDetMatchInfo::HcalRecHits, 2);
00215 
00216      if(trackhac3<5.) continue;
00217 
00218      double etaecal=info.trkGlobPosAtEcal.eta();
00219      double phiecal=info.trkGlobPosAtEcal.phi();
00220        
00221      maxPNearby=-10;
00222      dist=50;
00223      for (reco::TrackCollection::const_iterator track1 = tracks->begin(); track1!=tracks->end(); track1++)
00224        {
00225          if (track == track1) continue;
00226          TrackDetMatchInfo info1 = trackAssociator_.associate(iEvent, iSetup, *track1, parameters_);
00227          double etaecal1=info1.trkGlobPosAtEcal.eta();
00228          double phiecal1=info1.trkGlobPosAtEcal.phi();
00229 
00230          if (etaecal1==0&&phiecal1==0) continue;        
00231 
00232          double ecDist=getDistInCM(etaecal,phiecal,etaecal1,phiecal1);
00233 
00234          if( ecDist <  40. ) 
00235            {
00236              //calculate maximum P and sum P near seed track
00237              if (track1->p()>maxPNearby)
00238                {
00239                  maxPNearby=track1->p();
00240                  dist = ecDist;
00241                }
00242              
00243              //apply loose isolation criteria
00244              if (track1->p()>5.) 
00245                {
00246                  noChargedTracks = false;
00247                  break;
00248                }
00249            }
00250        }
00251      EnergyIn=0;
00252      EnergyOut=0;
00253      if(noChargedTracks){
00254        for (std::vector<EcalRecHit>::const_iterator ehit=tmpEcalRecHitCollection->begin(); ehit!=tmpEcalRecHitCollection->end(); ehit++) 
00255          {
00257            // R-scheme of ECAL CLUSTERIZATION
00258            GlobalPoint posH = geo->getPosition((*ehit).detid());
00259            double phihit = posH.phi();
00260            double etahit = posH.eta();
00261            
00262            double dHitCM=getDistInCM(etaecal,phiecal,etahit,phihit);
00263            
00264            if (dHitCM<9.0)
00265              {
00266                EnergyIn+=ehit->energy();
00267              }
00268            if (dHitCM>15.0&&dHitCM<35.0)
00269              {
00270                EnergyOut+=ehit->energy();
00271              }
00272            
00273          }
00274 
00275        treeMemPtr_->fillVariables(track->charge(), track->innerOk(), track->outerRadius(),
00276                                   track->numberOfValidHits(), track->numberOfLostHits(),
00277                                   track->chi2(), track->normalizedChi2(),
00278                                   track->p(), track->pt(), track->ptError(), 
00279                                   track->theta(), track->eta(), track->phi(),
00280                                   trackemc1, trackemc3, trackemc5,
00281                                   trackhac1, trackhac3, trackhac5,
00282                                   maxPNearby, dist, EnergyIn, EnergyOut);
00283        
00284        tree_->Fill();
00285        }
00286    }
00287 }
00288 
00289 
00290 // ------------ method called once each job just before starting event loop  ------------
00291 void 
00292 EopTreeWriter::beginJob()
00293 {
00294 }
00295 
00296 // ------------ method called once each job just after ending the event loop  ------------
00297 void 
00298 EopTreeWriter::endJob() {
00299 
00300   delete treeMemPtr_; treeMemPtr_ = 0;
00301 
00302 }
00303 
00304 double 
00305 EopTreeWriter::getDistInCM(double eta1, double phi1, double eta2, double phi2)
00306 {
00307   double deltaPhi=phi1-phi2;
00308   while(deltaPhi >   TMath::Pi())deltaPhi-=2*TMath::Pi();
00309   while(deltaPhi <= -TMath::Pi())deltaPhi+=2*TMath::Pi();
00310   double dR;
00311   // double Rec;
00312   double theta1=2*atan(exp(-eta1));
00313   double theta2=2*atan(exp(-eta2));
00314   double cotantheta1;
00315   if(cos(theta1)==0)cotantheta1=0;
00316   else cotantheta1=1/tan(theta1);
00317   double cotantheta2;
00318   if(cos(theta2)==0)cotantheta2=0;
00319   else cotantheta2=1/tan(theta2);
00320   // if (fabs(eta1)<1.479) Rec=129; //radius of ECAL barrel
00321   // else Rec=317; //distance from IP to ECAL endcap
00322   //|vect| times tg of acos(scalar product)
00323   // dR=fabs((Rec/sin(theta1))*tan(acos(sin(theta1)*sin(theta2)*(sin(phi1)*sin(phi2)+cos(phi1)*cos(phi2))+cos(theta1)*cos(theta2))));
00324   if(fabs(eta1)<1.479)dR=129*sqrt((cotantheta1-cotantheta2)*(cotantheta1-cotantheta2)+deltaPhi*deltaPhi);
00325   else dR=317*sqrt(tan(theta1)*tan(theta1)+tan(theta2)*tan(theta2)-2*tan(theta1)*tan(theta2)*cos(deltaPhi));
00326   return dR;
00327 }
00328 
00329 //define this as a plug-in
00330 DEFINE_FWK_MODULE(EopTreeWriter);