00001 #include "JetMETCorrections/JetPlusTrack/plugins/JetPlusTrackAnalysis.h"
00002
00003 #include <vector>
00004
00005 #include "FWCore/Framework/interface/Event.h"
00006 #include "DataFormats/Common/interface/Handle.h"
00007 #include "FWCore/ParameterSet/interface/ParameterSet.h"
00008 #include "FWCore/Framework/interface/MakerMacros.h"
00009 #include "FWCore/Framework/interface/ESHandle.h"
00010
00011 #include "Geometry/Records/interface/CaloGeometryRecord.h"
00012
00013 #include "DataFormats/VertexReco/interface/VertexFwd.h"
00014 #include "DataFormats/VertexReco/interface/Vertex.h"
00015 #include "DataFormats/JetReco/interface/CaloJetCollection.h"
00016 #include "DataFormats/JetReco/interface/CaloJet.h"
00017 #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
00018 #include "DataFormats/JetReco/interface/GenJet.h"
00019 #include "DataFormats/JetReco/interface/GenJetCollection.h"
00020 #include "SimDataFormats/HepMCProduct/interface/HepMCProduct.h"
00021 #include "DataFormats/HcalRecHit/interface/HcalRecHitCollections.h"
00022 #include "DataFormats/EcalRecHit/interface/EcalRecHitCollections.h"
00023 #include "DataFormats/Provenance/interface/Provenance.h"
00024
00025 using namespace std;
00026 namespace cms
00027 {
00028
00029 JetPlusTrackAnalysis::JetPlusTrackAnalysis(const edm::ParameterSet& iConfig)
00030 {
00031 cout<<" Start JetPlusTrackAnalysis now"<<endl;
00032 mCone = iConfig.getParameter<double>("Cone");
00033 cout<<" Start JetPlusTrackAnalysis Point 0"<<endl;
00034 mInputJetsCaloTower = iConfig.getParameter<edm::InputTag>("src1");
00035 cout<<" Start JetPlusTrackAnalysis Point 1"<<endl;
00036 mInputJetsGen = iConfig.getParameter<edm::InputTag>("src2");
00037 cout<<" Start JetPlusTrackAnalysis Point 2"<<endl;
00038 mInputJetsCorrected = iConfig.getParameter<edm::InputTag>("src3");
00039
00040 m_inputTrackLabel = iConfig.getUntrackedParameter<std::string>("inputTrackLabel");
00041
00042 hbhelabel_ = iConfig.getParameter<edm::InputTag>("HBHERecHitCollectionLabel");
00043 cout<<" Start JetPlusTrackAnalysis Point 5"<<endl;
00044 holabel_ = iConfig.getParameter<edm::InputTag>("HORecHitCollectionLabel");
00045 cout<<" Start JetPlusTrackAnalysis Point 6"<<endl;
00046 ecalLabels_=iConfig.getParameter<std::vector<edm::InputTag> >("ecalInputs");
00047 cout<<" Start JetPlusTrackAnalysis Point 7"<<endl;
00048 fOutputFileName = iConfig.getUntrackedParameter<string>("HistOutFile");
00049 cout<<" Start JetPlusTrackAnalysis Point 8"<<endl;
00050 allowMissingInputs_=iConfig.getUntrackedParameter<bool>("AllowMissingInputs",false);
00051 cout<<" JetPlusTrackAnalysis constructor "<<endl;
00052 }
00053
00054 JetPlusTrackAnalysis::~JetPlusTrackAnalysis()
00055 {
00056 cout<<" JetPlusTrack destructor "<<endl;
00057 }
00058
00059 void JetPlusTrackAnalysis::beginJob( const edm::EventSetup& iSetup)
00060 {
00061
00062 cout<<" Begin job "<<endl;
00063
00064 hOutputFile = new TFile( fOutputFileName.c_str(), "RECREATE" ) ;
00065 myTree = new TTree("JetPlusTrack","JetPlusTrack Tree");
00066 myTree->Branch("run", &run, "run/I");
00067 myTree->Branch("event", &event, "event/I");
00068
00069 NumRecoJetsCaloTower = 0;
00070 NumRecoJetsCorrected = 0;
00071 NumRecoJetsRecHit = 0;
00072 NumGenJets = 0;
00073 NumPart = 0;
00074 NumRecoTrack = 0;
00075
00076
00077 myTree->Branch("NumRecoJetsCaloTower", &NumRecoJetsCaloTower, "NumRecoJetsCaloTower/I");
00078 myTree->Branch("JetRecoEtCaloTower", JetRecoEtCaloTower, "JetRecoEtCaloTower[100]/F");
00079 myTree->Branch("JetRecoEtaCaloTower", JetRecoEtaCaloTower, "JetRecoEtaCaloTower[100]/F");
00080 myTree->Branch("JetRecoPhiCaloTower", JetRecoPhiCaloTower, "JetRecoPhiCaloTower[100]/F");
00081 myTree->Branch("JetRecoEtRecHit", JetRecoEtRecHit, "JetRecoEtRecHit[100]/F");
00082 myTree->Branch("JetRecoGenRecType", JetRecoGenRecType, "JetRecoGenRecType[100]/F");
00083 myTree->Branch("JetRecoGenPartonType", JetRecoGenPartonType , "JetRecoGenPartonType[100]/F");
00084 myTree->Branch("EcalEmpty", EcalEmpty , "EcalEmpty[100]/F");
00085 myTree->Branch("HcalEmpty", HcalEmpty , "HcalEmpty[100]/F");
00086
00087 myTree->Branch("NumRecoJetsCorrected", &NumRecoJetsCorrected, "NumRecoJetsCorrected/I");
00088 myTree->Branch("JetRecoEtCorrected", JetRecoEtCorrected, "JetRecoEtCorrected[100]/F");
00089 myTree->Branch("JetRecoEtCorrectedZS", JetRecoEtCorrectedZS, "JetRecoEtCorrectedZS[100]/F");
00090 myTree->Branch("JetRecoEtaCorrected", JetRecoEtaCorrected, "JetRecoEtaCorrected[100]/F");
00091 myTree->Branch("JetRecoPhiCorrected", JetRecoPhiCorrected, "JetRecoPhiCorrected[100]/F");
00092
00093 myTree->Branch("NumGenJets", &NumGenJets, "NumGenJets/I");
00094 myTree->Branch("JetGenEt", JetGenEt, "JetGenEt[10]/F");
00095 myTree->Branch("JetGenEta", JetGenEta, "JetGenEta[10]/F");
00096 myTree->Branch("JetGenPhi", JetGenPhi, "JetGenPhi[10]/F");
00097 myTree->Branch("JetGenCode", JetGenCode, "JetGenCode[10]/I");
00098
00099 myTree->Branch("NumPart", &NumPart, "NumPart/I");
00100 myTree->Branch("Code", Code, "Code[4000]/I");
00101 myTree->Branch("Charge", Charge, "Charge[4000]/I");
00102 myTree->Branch("partpx", partpx, "partpx[4000]/F");
00103 myTree->Branch("partpy", partpy, "partpy[4000]/F");
00104 myTree->Branch("partpz", partpz, "partpz[4000]/F");
00105 myTree->Branch("parte", parte, "parte[4000]/F");
00106 myTree->Branch("partm", partm, "partm[4000]/F");
00107
00108 myTree->Branch("NumRecoTrack", &NumRecoTrack, "NumRecoTrack/I");
00109 myTree->Branch("TrackRecoEt", TrackRecoEt, "TrackRecoEt[NumRecoTrack]/F");
00110 myTree->Branch("TrackRecoEta", TrackRecoEta, "TrackRecoEta[NumRecoTrack]/F");
00111 myTree->Branch("TrackRecoPhi", TrackRecoPhi, "TrackRecoPhi[NumRecoTrack]/F");
00112
00113
00114 edm::ESHandle<CaloGeometry> pG;
00115 iSetup.get<CaloGeometryRecord>().get(pG);
00116 geo = pG.product();
00117
00118
00119 }
00120 void JetPlusTrackAnalysis::endJob()
00121 {
00122
00123 cout << "===== Start writing user histograms =====" << endl;
00124 hOutputFile->SetCompressionLevel(2);
00125 hOutputFile->cd();
00126 myTree->Write();
00127 hOutputFile->Close() ;
00128 cout << "===== End writing user histograms =======" << endl;
00129
00130 }
00131
00132 void JetPlusTrackAnalysis::analyze(
00133 const edm::Event& iEvent,
00134 const edm::EventSetup& theEventSetup)
00135 {
00136 cout<<" JetPlusTrack analyze "<<endl;
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 run = iEvent.id().run();
00150 event = iEvent.id().event();
00151
00152
00153
00154 float pt[2],eta[2],phi[2];
00155 int parton[2];
00156
00157 cout<<" Try to take HepMCProduct "<<endl;
00158 edm::Handle< edm::HepMCProduct > EvtHandles ;
00159 iEvent.getByType( EvtHandles ) ;
00160
00161
00162
00163 if (!EvtHandles.isValid()) {
00164
00165 if (!allowMissingInputs_) {cout<<" GenParticles are missed "<<endl;}
00166 *EvtHandles;
00167 } else {
00168 const HepMC::GenEvent* Evt = EvtHandles->GetEvent() ;
00169
00170 int ihep = 0;
00171
00172 for (HepMC::GenEvent::particle_const_iterator
00173 Part = Evt->particles_begin() ; Part!=Evt->particles_end(); Part++ )
00174 {
00175 if(ihep == 6 || ihep == 7)
00176 {
00177 cout<<" parton "<<(*Part)->pdg_id()<<" "<<(*Part)->status()<<" "<<((*Part)->momentum()).perp()<<endl;
00178 pt[ihep-6] = ((*Part)->momentum()).perp();
00179 eta[ihep-6] = ((*Part)->momentum()).eta();
00180 phi[ihep-6] = ((*Part)->momentum()).phi();
00181 parton[ihep-6] = (*Part)->pdg_id();
00182 }
00183
00184
00185
00186
00187
00188
00189 ihep++;
00190
00191 }
00192 }
00193
00194 NumGenJets = 0;
00195 int icode = -1;
00196 {
00197 edm::Handle<reco::GenJetCollection> jets;
00198 iEvent.getByLabel(mInputJetsGen, jets);
00199 if (!jets.isValid()) {
00200
00201 if (!allowMissingInputs_) {
00202 *jets;
00203 }
00204 } else {
00205 reco::GenJetCollection::const_iterator jet = jets->begin ();
00206 if(jets->size() > 0 )
00207 {
00208 for (; jet != jets->end (); jet++)
00209 {
00210 if( NumGenJets < 4 )
00211 {
00212
00213 double dphi1 = fabs((*jet).phi()-phi[0]);
00214 if(dphi1 > 4.*atan(1.)) dphi1 = 8.*atan(1.) - dphi1;
00215 double dphi2 = fabs((*jet).phi()-phi[1]);
00216 if(dphi2 > 4.*atan(1.)) dphi2 = 8.*atan(1.) - dphi2;
00217 double deta1 = (*jet).eta()-eta[0];
00218 double deta2 = (*jet).eta()-eta[1];
00219 double dr1 = sqrt(dphi1*dphi1+deta1*deta1);
00220 double dr2 = sqrt(dphi2*dphi2+deta2*deta2);
00221 if(dr1 < 0.5 || dr2 < 0.5) {
00222 JetGenEt[NumGenJets] = (*jet).et();
00223 JetGenEta[NumGenJets] = (*jet).eta();
00224 JetGenPhi[NumGenJets] = (*jet).phi();
00225 cout<<" Associated jet: Phi, eta gen "<< JetGenPhi[NumGenJets]<<" "<< JetGenEta[NumGenJets]<<endl;
00226
00227 if(dr1 < 0.5) icode = 0;
00228 if(dr2 < 0.5) icode = 1;
00229 JetGenCode[NumGenJets] = icode;
00230 cout<<" Gen jet "<<NumGenJets<<" "<<JetGenEt[NumGenJets]<<" "<<JetGenEta[NumGenJets]<<" "<<JetGenPhi[NumGenJets]<<" "<<JetGenCode[NumGenJets]<<endl;
00231 NumGenJets++;
00232 }
00233 }
00234 }
00235 }
00236 }
00237 }
00238
00239
00240
00241 NumRecoJetsCaloTower = 0;
00242 {
00243 edm::Handle<reco::CaloJetCollection> jets;
00244 iEvent.getByLabel(mInputJetsCaloTower, jets);
00245 if (!jets.isValid()) {
00246
00247 if (!allowMissingInputs_) {cout<<"CaloTowers are missed "<<endl;
00248 *jets;
00249 }
00250 } else {
00251 reco::CaloJetCollection::const_iterator jet = jets->begin ();
00252
00253 cout<<" Size of jets "<<jets->size()<<endl;
00254
00255 if(jets->size() > 0 )
00256 {
00257 for (; jet != jets->end (); jet++)
00258 {
00259
00260 if( NumRecoJetsCaloTower < 100 )
00261 {
00262
00263
00264
00265 JetRecoEtCaloTower[NumRecoJetsCaloTower] = (*jet).et();
00266 JetRecoEtaCaloTower[NumRecoJetsCaloTower] = (*jet).eta();
00267 JetRecoPhiCaloTower[NumRecoJetsCaloTower] = (*jet).phi();
00268
00269 JetRecoGenRecType[NumRecoJetsCaloTower] = -1;
00270 JetRecoGenPartonType[NumRecoJetsCaloTower] = -1;
00271 NumRecoJetsCaloTower++;
00272
00273 }
00274 }
00275 }
00276 }
00277 }
00278
00279 if( NumRecoJetsCaloTower > 0 && NumGenJets > 0 )
00280 {
00281 for(int iii=0; iii<NumRecoJetsCaloTower; iii++)
00282 {
00283 for(int jjj=0; jjj<NumGenJets; jjj++)
00284 {
00285 double dphi1 = fabs(JetRecoPhiCaloTower[iii]-JetGenPhi[jjj]);
00286 if(dphi1 > 4.*atan(1.)) dphi1 = 8.*atan(1.) - dphi1;
00287 double deta1 = JetRecoEtaCaloTower[iii]-JetGenEta[jjj];
00288 double dr1 = sqrt(dphi1*dphi1+deta1*deta1);
00289 if(dr1 < 0.3) {JetRecoGenRecType[iii] = jjj;JetRecoGenPartonType[iii] = JetGenCode[jjj];
00290 cout<<" Associated jet "<< iii<<" "<<JetRecoGenRecType[iii]<<" "<<JetRecoGenPartonType[iii]<<endl;
00291 cout<<" Etcalo "<<JetRecoEtCaloTower[iii]<<" ETgen "<<JetGenEt[jjj]<<endl;
00292 }
00293 }
00294 }
00295 }
00296
00297
00298 NumRecoJetsCorrected = 0;
00299 {
00300 edm::Handle<reco::CaloJetCollection> jets;
00301 iEvent.getByLabel(mInputJetsCorrected, jets);
00302 if (!jets.isValid()) {
00303
00304 if (!allowMissingInputs_) {cout<<"JetPlusTrack CaloTowers are missed "<<endl;
00305 *jets;
00306 }
00307 } else {
00308 reco::CaloJetCollection::const_iterator jet = jets->begin ();
00309
00310 cout<<" Size of jets "<<jets->size()<<endl;
00311 if(jets->size() > 0 )
00312 {
00313 for (; jet != jets->end (); jet++)
00314 {
00315 if( NumRecoJetsCorrected < 100 )
00316 {
00317 JetRecoEtCorrected[NumRecoJetsCorrected] = (*jet).et();
00318 JetRecoEtaCorrected[NumRecoJetsCorrected] = (*jet).eta();
00319 JetRecoPhiCorrected[NumRecoJetsCorrected] = (*jet).phi();
00320
00321 if( JetRecoGenRecType[NumRecoJetsCorrected] > -1 )cout<<" Calo jet "<<JetRecoEtCaloTower[NumRecoJetsCorrected]<<" Cor "<<JetRecoEtCorrected[NumRecoJetsCorrected]<<" Gen "<<JetGenEt[(int)(JetRecoGenRecType[NumRecoJetsCorrected])]<<endl;
00322
00323 NumRecoJetsCorrected++;
00324 }
00325 }
00326 }
00327 }
00328 }
00329
00330
00331
00332
00333
00334 std::vector<edm::InputTag>::const_iterator i;
00335 int iecal = 0;
00336 double empty_jet_energy_ecal = 0.;
00337
00338
00339 for(int jjj=0; jjj<NumRecoJetsCaloTower; jjj++)
00340 {
00341 JetRecoEtRecHit[jjj] = 0.;
00342
00343 for (i=ecalLabels_.begin(); i!=ecalLabels_.end(); i++) {
00344 {
00345 edm::Handle<EcalRecHitCollection> ec;
00346 iEvent.getByLabel(*i,ec);
00347 if (!ec.isValid()) {
00348
00349 if (!allowMissingInputs_) {cout<<" Ecal rechits are missed "<<endl;
00350 *ec;
00351 }
00352 } else {
00353
00354 for(EcalRecHitCollection::const_iterator recHit = (*ec).begin();
00355 recHit != (*ec).end(); ++recHit)
00356 {
00357
00358 GlobalPoint pos = geo->getPosition(recHit->detid());
00359 double deta = pos.eta() - JetRecoEtaCaloTower[jjj];
00360 double dphi = fabs(pos.phi() - JetRecoPhiCaloTower[jjj]);
00361 if(dphi > 4.*atan(1.)) dphi = 8.*atan(1.) - dphi;
00362 double dr = sqrt(dphi*dphi + deta*deta);
00363 double dphi_empty = fabs(pos.phi()+4.*atan(1.) - JetRecoPhiCaloTower[jjj]);
00364 if(dphi_empty > 4.*atan(1.)) dphi_empty = 8.*atan(1.) - dphi_empty;
00365 double dr_empty = sqrt(dphi_empty*dphi_empty + deta*deta);
00366
00367
00368 if(dr<mCone)
00369 {
00370
00371
00372 JetRecoEtRecHit[jjj] = JetRecoEtRecHit[jjj] + (*recHit).energy();
00373
00374 }
00375 if(dr_empty<mCone)
00376 {
00377 empty_jet_energy_ecal = empty_jet_energy_ecal + (*recHit).energy();
00378 }
00379 }
00380 }
00381 }
00382 iecal++;
00383 }
00384
00385 }
00386
00387 double empty_jet_energy_hcal = 0.;
00388 {
00389 edm::Handle<HBHERecHitCollection> hbhe;
00390 iEvent.getByLabel(hbhelabel_,hbhe);
00391 if (!hbhe.isValid()) {
00392
00393 cout<<" Exception in hbhe "<<endl;
00394 if (!allowMissingInputs_) {
00395 *hbhe;
00396 }
00397 } else {
00398 for(int jjj=0; jjj<NumRecoJetsCaloTower; jjj++)
00399 {
00400 for(HBHERecHitCollection::const_iterator hbheItr = (*hbhe).begin();
00401 hbheItr != (*hbhe).end(); ++hbheItr)
00402 {
00403 DetId id = (hbheItr)->detid();
00404 GlobalPoint pos = geo->getPosition(hbheItr->detid());
00405 double deta = pos.eta() - JetRecoEtaCaloTower[jjj];
00406 double dphi = fabs(pos.phi() - JetRecoPhiCaloTower[jjj]);
00407 if(dphi > 4.*atan(1.)) dphi = 8.*atan(1.) - dphi;
00408 double dr = sqrt(dphi*dphi + deta*deta);
00409 double dphi_empty = fabs(pos.phi()+4.*atan(1.) - JetRecoPhiCaloTower[jjj]);
00410 if(dphi_empty > 4.*atan(1.)) dphi_empty = 8.*atan(1.) - dphi_empty;
00411 double dr_empty = sqrt(dphi_empty*dphi_empty + deta*deta);
00412
00413 if(dr<mCone)
00414 {
00415
00416 JetRecoEtRecHit[jjj] = JetRecoEtRecHit[jjj] + (*hbheItr).energy();
00417 }
00418 if(dr_empty<mCone)
00419 {
00420 empty_jet_energy_hcal = empty_jet_energy_hcal + (*hbheItr).energy();
00421 }
00422 }
00423
00424 }
00425 }
00426 }
00427
00428
00429 EcalEmpty[0] = empty_jet_energy_ecal;
00430 HcalEmpty[0] = empty_jet_energy_hcal;
00431
00432
00433
00434 edm::Handle<reco::TrackCollection> tracks;
00435 iEvent.getByLabel(m_inputTrackLabel, tracks);
00436
00437 reco::TrackCollection::const_iterator trk;
00438 int iTracks = 0;
00439 for ( trk = tracks->begin(); trk != tracks->end(); ++trk){
00440 TrackRecoEt[iTracks] = trk->pt();
00441 TrackRecoEta[iTracks] = trk->eta();
00442 TrackRecoPhi[iTracks] = trk->phi();
00443 iTracks++;
00444 }
00445 NumRecoTrack = iTracks;
00446 cout<<" Number of tracks "<<NumRecoTrack<<endl;
00447
00448 for(int jjj = 0; jjj<NumRecoJetsCaloTower; jjj++)
00449 {
00450 if(JetRecoGenPartonType[jjj] > -1){
00451 cout<<" Calo energy "<<JetRecoEtCaloTower[jjj]<<" RecHit energy "<<JetRecoEtRecHit[jjj]<<" Empty cone ECAL "<<empty_jet_energy_ecal<<
00452 " Empty cone HCAL "<<empty_jet_energy_hcal <<" association with gen jet "<<JetRecoGenRecType[jjj]
00453 <<" Association with parton "<<JetRecoGenPartonType[jjj]<<endl;
00454 int i1 = (int) JetRecoGenPartonType[jjj];
00455 int i2 = (int) JetRecoGenPartonType[jjj];
00456 cout<<" Parton energy "<<pt[i1]<<" type "<<parton[i2]<<endl;
00457 }
00458 }
00459 myTree->Fill();
00460
00461 }
00462 }
00463