ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MPIAnalyzer/src/MPIntuple.cc
Revision: 1.30
Committed: Wed Mar 23 13:46:39 2011 UTC (14 years, 1 month ago) by naodell
Content type: text/plain
Branch: MAIN
Changes since 1.29: +554 -422 lines
Log Message:
Prototype of Jet-Parton associater. JES uncertainties.

File Contents

# User Rev Content
1 naodell 1.1 // Original Author: "Nathaniel Odell"
2     // Secondary Author: Steven Won
3 andrey 1.2 // With contributions from: Andrey Pozdnyakov
4 naodell 1.1 // Created: Thurs April 22 2010
5 naodell 1.30 // $Id: MPIntuple.cc,v 1.29 2011/02/28 16:46:30 naodell Exp $
6 naodell 1.1
7     // system include files
8     #include <memory>
9     #include <string>
10    
11     // user include files
12     #include "FWCore/Framework/interface/Frameworkfwd.h"
13     #include "FWCore/Framework/interface/EDAnalyzer.h"
14     #include "FWCore/Framework/interface/ESHandle.h"
15     #include "FWCore/Framework/interface/EventSetup.h"
16     #include "FWCore/Framework/interface/Event.h"
17     #include "FWCore/Framework/interface/MakerMacros.h"
18 naodell 1.26 #include "FWCore/Framework/interface/LuminosityBlock.h"
19 naodell 1.1 #include "FWCore/ParameterSet/interface/ParameterSet.h"
20 naodell 1.26
21     #include "Geometry/Records/interface/CaloGeometryRecord.h"
22 naodell 1.1 #include "DataFormats/GeometryVector/interface/GlobalPoint.h"
23 naodell 1.26 #include "DataFormats/GeometryVector/interface/GlobalVector.h"
24     #include "DataFormats/GeometryVector/interface/LocalPoint.h"
25     #include "DataFormats/GeometryVector/interface/LocalVector.h"
26     #include "DataFormats/Math/interface/Point3D.h"
27     #include "DataFormats/Math/interface/Vector3D.h"
28     #include "DataFormats/Math/interface/LorentzVector.h"
29 naodell 1.10 #include "DataFormats/Math/interface/deltaPhi.h"
30 naodell 1.1
31 naodell 1.15 // Libraries for objects
32 naodell 1.26 #include "DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
33     #include "DataFormats/ParticleFlowCandidate/interface/PFCandidateFwd.h"
34     #include "DataFormats/HepMCCandidate/interface/GenParticle.h"
35 naodell 1.1 #include "DataFormats/JetReco/interface/CaloJet.h"
36     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
37 naodell 1.26 #include "DataFormats/JetReco/interface/GenJet.h"
38     #include "DataFormats/JetReco/interface/GenJetCollection.h"
39 naodell 1.1 #include "DataFormats/JetReco/interface/PFJet.h"
40     #include "DataFormats/JetReco/interface/PFJetCollection.h"
41 naodell 1.27 #include "DataFormats/METReco/interface/PFMET.h"
42     #include "DataFormats/METReco/interface/PFMETCollection.h"
43     #include "DataFormats/METReco/interface/PFMETFwd.h"
44     #include "DataFormats/METReco/interface/MET.h"
45     #include "DataFormats/METReco/interface/METCollection.h"
46     #include "DataFormats/METReco/interface/METFwd.h"
47 naodell 1.26 #include "DataFormats/MuonReco/interface/Muon.h"
48     #include "DataFormats/MuonReco/interface/MuonFwd.h"
49     #include "DataFormats/MuonReco/interface/MuonSelectors.h"
50     #include "DataFormats/EgammaCandidates/interface/GsfElectron.h"
51     #include "DataFormats/EgammaCandidates/interface/GsfElectronFwd.h"
52     #include "DataFormats/EgammaCandidates/interface/Photon.h"
53     #include "DataFormats/EgammaCandidates/interface/PhotonFwd.h"
54     #include "DataFormats/TrackReco/interface/Track.h"
55     #include "DataFormats/TrackReco/interface/TrackFwd.h"
56     #include "DataFormats/GsfTrackReco/interface/GsfTrack.h"
57 naodell 1.1 #include "DataFormats/VertexReco/interface/Vertex.h"
58     #include "DataFormats/VertexReco/interface/VertexFwd.h"
59 naodell 1.7 #include "DataFormats/BTauReco/interface/JetTag.h"
60 naodell 1.16 #include "DataFormats/BeamSpot/interface/BeamSpot.h"
61 naodell 1.26 #include "DataFormats/Luminosity/interface/LumiSummary.h"
62     #include "DataFormats/Common/interface/ValueMap.h"
63 naodell 1.30 #include "SimDataFormats/GeneratorProducts/interface/HepMCProduct.h"
64 naodell 1.18 #include "SimDataFormats/GeneratorProducts/interface/GenEventInfoProduct.h"
65 naodell 1.24 #include "SimDataFormats/GeneratorProducts/interface/GenRunInfoProduct.h"
66 naodell 1.26 #include "SimDataFormats/PileupSummaryInfo/interface/PileupSummaryInfo.h"
67 naodell 1.18
68 devildog 1.11 //#include "RecoVertex/PrimaryVertexProducer/interface/VertexHigherPtSquared.h"
69 naodell 1.7
70 naodell 1.30 // JEC
71 naodell 1.4 #include "JetMETCorrections/Objects/interface/JetCorrector.h"
72 naodell 1.30 #include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
73 naodell 1.4
74     // ntuple storage classes
75     #include "TCJet.h"
76 naodell 1.27 #include "TCMET.h"
77 naodell 1.4 #include "TCPrimaryVtx.h"
78 naodell 1.25 #include "TCGenJet.h"
79 naodell 1.26 #include "TCMuon.h"
80     #include "TCElectron.h"
81 naodell 1.4
82 andrey 1.2 // Need for HLT trigger info:
83     #include "FWCore/Common/interface/TriggerNames.h"
84     #include "DataFormats/Common/interface/TriggerResults.h"
85     #include "HLTrigger/HLTcore/interface/HLTConfigProvider.h"
86    
87 naodell 1.4 //Root stuff
88 naodell 1.1 #include "TROOT.h"
89 naodell 1.15 #include "TH1.h"
90 naodell 1.16 #include "TH2.h"
91 naodell 1.15 #include "TProfile.h"
92 naodell 1.1 #include "TFile.h"
93     #include "TTree.h"
94     #include "TString.h"
95     #include "TObject.h"
96     #include "TObjArray.h"
97     #include "TClonesArray.h"
98     #include "TRefArray.h"
99     #include "TLorentzVector.h"
100     #include "TVector3.h"
101    
102 naodell 1.30 //MC stuff
103     //#include "HepMC/GenEvent"
104    
105 naodell 1.1 using namespace edm;
106     using namespace std;
107     using namespace reco;
108    
109     //
110     // class declaration
111     //
112    
113     class MPIntuple : public edm::EDAnalyzer {
114 naodell 1.30 public:
115     explicit MPIntuple(const edm::ParameterSet&);
116     ~MPIntuple();
117    
118    
119     private:
120     virtual void beginJob() ;
121     virtual void beginRun(const edm::Run&, const edm::EventSetup&) ;
122     virtual void analyze(const edm::Event&, const edm::EventSetup&);
123     virtual void endLuminosityBlock(const edm::LuminosityBlock&,const edm::EventSetup&);
124     virtual void endRun(const edm::Run&, const edm::Event&, const edm::EventSetup&);
125     virtual void endJob() ;
126    
127     bool genParticleMatch(const HepMC::GenParticle& genParticle, const reco::Candidate& motherParticle);
128     bool triggerDecision(edm::Handle<edm::TriggerResults>& hltR, int iTrigger);
129     float sumPtSquared(const Vertex& v);
130    
131     // ----------member data ---------------------------
132    
133    
134     int eventNumber, runNumber, lumiSection, bunchCross;
135     float ptHat, qScale, crossSection, evtWeight;
136     float lumiDeadCount, lumiLiveFrac, intDelLumi;
137    
138     TTree* sTree;
139     TFile* ntupleFile;
140     edm::InputTag recoJetTag_;
141     edm::InputTag recoMETTag_;
142     edm::InputTag genJetTag_;
143     edm::InputTag muonTag_;
144     edm::InputTag electronTag_;
145     edm::InputTag primaryVtxTag_;
146     edm::InputTag triggerResultsTag_;
147     edm::InputTag electronIDMap_;
148     bool doGenJets_;
149     bool doPFJets_;
150     bool triggerHLT_;
151     bool isRealData;
152     int nJets_;
153     string rootfilename;
154    
155     TClonesArray* recoJets;
156     TClonesArray* recoMET;
157     TClonesArray* genJets;
158     TClonesArray* primaryVtx;
159     TClonesArray* recoMuons;
160     TClonesArray* recoElectrons;
161    
162     //Triggers
163     string hlTriggerResults_, hltProcess_, triggerName_;
164     TriggerNames triggerNames;
165     HLTConfigProvider hltConfig_;
166     vector<string> hlNames;
167     vector<string> mpiTriggers_;
168     unsigned int triggerStatus;
169     unsigned int hltPrescale[32];
170    
171    
172     //Histograms
173     TH1D * h1_ptHat;
174     TH1D * h1_fracAssociatedTracks;
175     TH1D * h1_meanJetTrackZ;
176     TH1D * h1_trackDxy;
177     TH1D * h1_jetVertexZ;
178     TH1D * h1_associatedSumPt;
179     TH1D * h1_associatedVertexIndex;
180     TH1D * h1_partonJetMatch;
181     TH2F * h2_nAssTracksVsJetPt;
182     TProfile * p1_nVtcs;
183 naodell 1.1 };
184    
185 naodell 1.18 MPIntuple::MPIntuple(const edm::ParameterSet& iConfig)
186 naodell 1.1 {
187 naodell 1.30 recoJetTag_ = iConfig.getUntrackedParameter<edm::InputTag>("RecoJetTag");
188     recoMETTag_ = iConfig.getUntrackedParameter<edm::InputTag>("RecoMETTag");
189     muonTag_ = iConfig.getUntrackedParameter<edm::InputTag>("MuonTag");
190     electronTag_ = iConfig.getUntrackedParameter<edm::InputTag>("ElectronTag");
191     genJetTag_ = iConfig.getUntrackedParameter<edm::InputTag>("GenJetTag");
192     primaryVtxTag_ = iConfig.getUntrackedParameter<edm::InputTag>("PrimaryVtxTag");
193     electronIDMap_ = iConfig.getParameter<edm::InputTag>("electronIDMap");
194     nJets_ = iConfig.getUntrackedParameter<int>("nJets");
195     doGenJets_ = iConfig.getUntrackedParameter<bool>("doGenJets");
196     doPFJets_ = iConfig.getUntrackedParameter<bool>("doPFJets");
197     triggerHLT_ = iConfig.getUntrackedParameter<bool>("triggerHLT");
198     hlTriggerResults_ = iConfig.getUntrackedParameter<string>("HLTriggerResults","TriggerResults");
199     hltProcess_ = iConfig.getUntrackedParameter<string>("hltName");
200     mpiTriggers_ = iConfig.getUntrackedParameter<vector<string> >("triggers");
201     rootfilename = iConfig.getUntrackedParameter<string>("rootfilename");
202 naodell 1.1 }
203    
204     MPIntuple::~MPIntuple()
205     {
206    
207     }
208    
209     //
210     // member functions
211     //
212    
213     // ------------ method called to for each event ------------
214     void MPIntuple::analyze(const edm::Event& iEvent, const edm::EventSetup& iSetup)
215     {
216    
217 naodell 1.30 eventNumber = iEvent.id().event();
218     runNumber = iEvent.id().run();
219     lumiSection = (unsigned int)iEvent.getLuminosityBlock().luminosityBlock();
220     bunchCross = (unsigned int)iEvent.bunchCrossing();
221     isRealData = iEvent.isRealData();
222    
223     edm::Handle<reco::BeamSpot> beamSpotHandle;
224     iEvent.getByLabel("offlineBeamSpot", beamSpotHandle);
225     reco::BeamSpot vertexBeamSpot = *beamSpotHandle;
226    
227     int pfCount = 0;
228     int genCount = 0;
229     int vtxCount = 0;
230     float primaryVertexZ = -999;
231    
232     //////////////////////////
233     //Get vertex information//
234     //////////////////////////
235    
236     Handle<reco::VertexCollection> primaryVtcs;
237     iEvent.getByLabel(primaryVtxTag_, primaryVtcs);
238    
239     for(VertexCollection::const_iterator vtx_iter = primaryVtcs->begin(); vtx_iter!= primaryVtcs->end(); ++vtx_iter){
240     reco::Vertex myVtx = reco::Vertex(*vtx_iter);
241     if(!myVtx.isValid() || myVtx.isFake()) continue;
242     TCPrimaryVtx* vtxCon = new ((*primaryVtx)[vtxCount]) TCPrimaryVtx;
243     vtxCon->SetPosition(myVtx.x(), myVtx.y(), myVtx.z());
244     vtxCon->SetNDof(myVtx.ndof());
245     vtxCon->SetChi2(myVtx.chi2());
246     vtxCon->SetNTrks(myVtx.tracksSize());
247     vtxCon->SetSumPt2Trks(sumPtSquared(myVtx));
248     if(vtxCount == 0) primaryVertexZ = myVtx.z();
249     ++vtxCount;
250     }
251 naodell 1.16
252 naodell 1.30 p1_nVtcs->Fill(runNumber, vtxCount);
253 naodell 1.16
254 naodell 1.30 ///////////////////////
255     //get jet information//
256     ///////////////////////
257    
258     if(doPFJets_){
259    
260     edm::Handle<reco::JetTagCollection> bTagHandle1;
261     iEvent.getByLabel("trackCountingHighEffBJetTags", bTagHandle1);
262     const reco::JetTagCollection & bTags1 = *(bTagHandle1.product());
263     reco::JetTagCollection::const_iterator jet_it_1;
264    
265     edm::Handle<reco::JetTagCollection> bTagHandle2;
266     iEvent.getByLabel("trackCountingHighPurBJetTags", bTagHandle2);
267     const reco::JetTagCollection & bTags2 = *(bTagHandle2.product());
268     reco::JetTagCollection::const_iterator jet_it_2;
269    
270     edm::FileInPath fip("CondFormats/JetMETObjects/data/Spring10_Uncertainty_AK5PF.txt");
271     //JetCorrectionUncertainty *jecUnc = new JetCorrectionUncertainty(fip.fullPath());
272    
273     const JetCorrector* correctorL2 = JetCorrector::getJetCorrector("ak5PFL2Relative",iSetup);
274     const JetCorrector* correctorL3 = JetCorrector::getJetCorrector("ak5PFL3Absolute",iSetup);
275     const JetCorrector* correctorRes = JetCorrector::getJetCorrector("ak5PFResidual", iSetup);
276    
277     Handle<reco::PFJetCollection> PFJets;
278     iEvent.getByLabel(recoJetTag_, PFJets);
279    
280     for (PFJetCollection::const_iterator jet_iter = PFJets->begin(); jet_iter!= PFJets->end(); ++jet_iter) {
281    
282     reco::PFJet myJet = reco::PFJet(*jet_iter);
283     reco::PFJet corJet = reco::PFJet(*jet_iter);
284    
285     float scale2 = correctorL2->correction(corJet);
286     corJet.scaleEnergy(scale2);
287     float scale3 = correctorL3->correction(corJet);
288     corJet.scaleEnergy(scale3);
289    
290     if (corJet.pt() > 5) {
291    
292     for (jet_it_1 = bTags1.begin(); jet_it_1 != bTags1.end(); jet_it_1++) {
293     if ((fabs(jet_it_1->first->eta() - myJet.eta()) < .005) && (deltaPhi(jet_it_1->first->phi(),myJet.phi()) < .005)) break;
294     }
295    
296     for (jet_it_2 = bTags2.begin(); jet_it_2 != bTags2.end(); jet_it_2++) {
297     if ((fabs(jet_it_2->first->eta() - myJet.eta()) < .005) && (deltaPhi(jet_it_2->first->phi(),myJet.phi()) < .005)) break;
298     }
299    
300     TCJet* jetCon = new ((*recoJets)[pfCount]) TCJet;
301    
302     jetCon->SetP4(myJet.px(), myJet.py(), myJet.pz(), myJet.energy());
303     jetCon->SetVtx(-999.0, -999.0, -999.0);
304     jetCon->SetChHadFrac(myJet.chargedHadronEnergyFraction());
305     jetCon->SetNeuHadFrac(myJet.neutralHadronEnergyFraction());
306     jetCon->SetChEmFrac(myJet.chargedEmEnergyFraction());
307     jetCon->SetNeuEmFrac(myJet.neutralEmEnergyFraction());
308     jetCon->SetNumConstit(myJet.chargedMultiplicity() + myJet.neutralMultiplicity());
309     jetCon->SetNumChPart(myJet.chargedMultiplicity());
310    
311     if(jet_it_2 != bTags2.end()) jetCon->SetBDiscrTrkCountHiPure(jet_it_2->second);
312     if(jet_it_1 != bTags1.end()) jetCon->SetBDiscrTrkCountHiEff(jet_it_1->second);
313    
314     //more corrections?
315    
316     jetCon->SetJetCorr(2, scale2);
317     jetCon->SetJetCorr(3, scale3);
318    
319     if (isRealData) {
320     float scaleRes = correctorRes->correction(corJet);
321     jetCon->SetJetCorr(4, scaleRes);
322    
323     //jecUnc->setJetEta(corJet.eta());
324     //jecUnc->setJetPt(corJet.pt());
325     //jetCon->SetUncertaintyJES(jecUnc->getUncertainty(true));
326     }
327    
328     /////////////////////////
329     //get associated tracks//
330     /////////////////////////
331    
332     const reco::TrackRefVector &tracks = myJet.getTrackRefs();
333    
334     vector<TVector3> vtxPositionCollection;
335     vector<float> associatedTrackSumPt;
336     vector<unsigned int> jetTrackAddresses;
337     float sumTrackX, sumTrackY, sumTrackZ, sumTrackIP, sumTrackPt;
338     int nJetTracks, nVertexTracks, nAssociatedTracks, vertexIndex;
339     int vCount = 0;
340    
341     nJetTracks = nVertexTracks = nAssociatedTracks = 0;
342     sumTrackX = sumTrackY = sumTrackZ = sumTrackIP = sumTrackPt = 0;
343    
344     if(fabs(myJet.eta()) < 2.5){
345    
346     for (TrackRefVector::const_iterator iTrack = tracks.begin(); iTrack != tracks.end(); ++iTrack) {
347     const reco::Track &myJetTrack = **iTrack;
348    
349     sumTrackPt += myJetTrack.pt();
350     sumTrackX += myJetTrack.vx();
351     sumTrackY += myJetTrack.vy();
352     sumTrackZ += myJetTrack.vz();
353     sumTrackIP += myJetTrack.dxy(vertexBeamSpot.position());
354     jetTrackAddresses.push_back((unsigned int)&myJetTrack);
355     ++nJetTracks;
356     }
357    
358     if (nJetTracks > 0) {
359     jetCon->SetVtx(sumTrackX/nJetTracks, sumTrackY/nJetTracks, sumTrackZ/nJetTracks);
360     h1_meanJetTrackZ->Fill(sumTrackZ/nJetTracks);
361     h1_trackDxy->Fill(sumTrackIP/nJetTracks);
362     }
363     if(jetTrackAddresses.size() > 0){
364    
365     for (VertexCollection::const_iterator vtx_iter = primaryVtcs->begin(); vtx_iter!= primaryVtcs->end(); ++vtx_iter) {
366     reco::Vertex myVtx = reco::Vertex(*vtx_iter);
367     if(!myVtx.isValid() || myVtx.isFake()) continue;
368     TVector3 *iVtxPosition = new TVector3(myVtx.x(), myVtx.y(), myVtx.z());
369     vtxPositionCollection.push_back(*iVtxPosition);
370     associatedTrackSumPt.push_back(0);
371    
372     for(Vertex::trackRef_iterator iTrackRef = myVtx.tracks_begin(); iTrackRef != myVtx.tracks_end(); ++iTrackRef){
373     const edm::RefToBase<reco::Track> &myTrackRef = *iTrackRef;
374     if(myTrackRef.isAvailable()){
375     const reco::Track &myVertexTrack = *myTrackRef.get();
376    
377     for(vector<unsigned int>::const_iterator iTrackAddress = jetTrackAddresses.begin(); iTrackAddress != jetTrackAddresses.end(); ++iTrackAddress){
378     if (*iTrackAddress == (unsigned int)&myVertexTrack) {
379     associatedTrackSumPt.at(vCount) += myVertexTrack.pt()/sumTrackPt;
380     ++nAssociatedTracks;
381     }
382     }
383     }
384     }
385     ++vCount;
386     }
387    
388     float maxSumPtFraction = 0;
389     vCount = vertexIndex = 0;
390    
391     for (vector<float>::const_iterator iTrackSumPt = associatedTrackSumPt.begin(); iTrackSumPt != associatedTrackSumPt.end(); ++iTrackSumPt) {
392     if (*iTrackSumPt > maxSumPtFraction) {
393     maxSumPtFraction = *iTrackSumPt;
394     vertexIndex = vCount + 1;
395     }
396     ++vCount;
397     }
398     if (vertexIndex > 0) {
399     h1_jetVertexZ->Fill(vtxPositionCollection[vertexIndex-1].z());
400     }
401    
402     jetCon->SetVtxSumPtFrac(maxSumPtFraction);
403     jetCon->SetVtxSumPt(sumTrackPt);
404     jetCon->SetVtxTrackFrac((float)nAssociatedTracks/(float)nJetTracks);
405     jetCon->SetVtxNTracks(nJetTracks);
406     jetCon->SetVtxIndex(vertexIndex);
407     }
408     }
409     ++pfCount;
410     }
411     }
412     }
413 naodell 1.16
414 naodell 1.26
415 naodell 1.30 //////////////////
416     // Get MET info //
417     //////////////////
418    
419    
420     Handle<PFMETCollection> MET;
421     iEvent.getByLabel(recoMETTag_, MET);
422    
423     int metCount = 0;
424     for (PFMETCollection::const_iterator iMET = MET->begin(); iMET != MET->end(); ++iMET) {
425     TCMET* metCon = new ((*recoMET)[metCount]) TCMET;
426     metCon->SetSumEt(iMET->sumEt());
427     metCon->SetMet(iMET->et());
428     metCon->SetPhi(iMET->phi());
429     metCon->SetPhotonEtFraction(iMET->photonEtFraction());
430     metCon->SetElectronEtFraction(iMET->electronEtFraction());
431     metCon->SetMuonEtFraction(iMET->muonEtFraction());
432     metCon->SetNeutralHadronEtFraction(iMET->neutralHadronEtFraction());
433     metCon->SetChargedHadronEtFraction(iMET->chargedHadronEtFraction());
434     metCon->SetHFHadronEtFraction(iMET->HFHadronEtFraction());
435     metCon->SetHFEMEtFraction(iMET->HFEMEtFraction());
436     }
437 naodell 1.26
438 naodell 1.30 ///////////////////
439     // Get muon info //
440     ///////////////////
441    
442    
443     Handle<MuonCollection> muons;
444     iEvent.getByLabel(muonTag_, muons);
445    
446     int muCount = 0;
447     for (MuonCollection::const_iterator mu = muons->begin(); mu != muons->end(); ++mu) {
448     if (mu->isGlobalMuon() && mu->pt() > 8){
449     TCMuon* lepCon = new ((*recoMuons)[muCount]) TCMuon;
450     lepCon->Setp4(mu->px(), mu->py(), mu->pz(), mu->p());
451     lepCon->SetVtx(mu->globalTrack()->vx(),mu->globalTrack()->vy(),mu->globalTrack()->vz());
452     lepCon->SetEta(mu->eta());
453     lepCon->SetPhi(mu->phi());
454     lepCon->SetCharge(mu->charge());
455     lepCon->SetisGLB(mu->isGlobalMuon());
456     lepCon->SetisTRK(mu->isTrackerMuon());
457     lepCon->Setdxy(mu->globalTrack()->dxy(vertexBeamSpot.position()));
458     lepCon->SetnPXLHits(mu->globalTrack()->hitPattern().numberOfValidPixelHits());
459     lepCon->SetnTRKHits(mu->globalTrack()->hitPattern().numberOfValidTrackerHits());
460     lepCon->SetnValidMuHits(mu->globalTrack()->hitPattern().numberOfValidMuonHits());
461     lepCon->SetnMatchSeg(mu->numberOfMatches());
462     lepCon->SetNormChi2(mu->globalTrack()->normalizedChi2());
463     lepCon->SetCaloComp(mu->caloCompatibility());
464     lepCon->SetSegComp(muon::segmentCompatibility(*mu));
465     lepCon->SetEMIso(mu->isolationR03().emEt);
466     lepCon->SetHADIso(mu->isolationR03().hadEt);
467     lepCon->SetTRKIso(mu->isolationR03().sumPt);
468     muCount++;
469     }
470     }
471 naodell 1.27
472    
473 naodell 1.30 ///////////////////////
474     // Get electron info //
475     ///////////////////////
476    
477    
478     Handle<edm::ValueMap<float> > eIDValueMap;
479     iEvent.getByLabel( electronIDMap_ , eIDValueMap );
480     const edm::ValueMap<float> & eIDmap = * eIDValueMap ;
481    
482     Handle<GsfElectronCollection> electrons;
483     iEvent.getByLabel(electronTag_, electrons);
484    
485     int elCount = 0;
486     for (unsigned int i = 0; i < electrons->size(); i++){
487     edm::Ref<reco::GsfElectronCollection> electronRef(electrons,i);
488     if ( eIDmap[electronRef] == 7 && electronRef->pt() > 15){
489     TCElectron* lepCon = new ((*recoElectrons)[elCount]) TCElectron;
490     lepCon->Setp4(electronRef->px(),electronRef->py(),electronRef->pz(),electronRef->p());
491     lepCon->SetVtx(electronRef->gsfTrack()->vx(),electronRef->gsfTrack()->vy(),electronRef->gsfTrack()->vz());
492     lepCon->SetCharge(electronRef->charge());
493     lepCon->SetEta(electronRef->eta());
494     lepCon->SetPhi(electronRef->phi());
495     lepCon->Setdxy(electronRef->gsfTrack()->dxy(vertexBeamSpot.position()));
496     lepCon->SetNormChi2(electronRef->gsfTrack()->normalizedChi2());
497     lepCon->SetEMIso(electronRef->dr03EcalRecHitSumEt());
498     lepCon->SetHADIso(electronRef->dr03HcalTowerSumEt());
499     lepCon->SetTRKIso(electronRef->dr03TkSumPt());
500     elCount++;
501     }
502     }
503 naodell 1.27
504 naodell 1.26
505 naodell 1.30 ////////////////////////
506     // Get gen-level info //
507     ////////////////////////
508    
509    
510     if (!isRealData) {
511    
512     Handle<HepMCProduct > genEvtHandle;
513     iEvent.getByLabel( "generator", genEvtHandle) ;
514     const HepMC::GenEvent* Evt = genEvtHandle->GetEvent();
515    
516     Handle<GenEventInfoProduct> GenEventInfoHandle;
517     iEvent.getByLabel("generator", GenEventInfoHandle);
518    
519     Handle<reco::GenJetCollection> GenJets;
520     iEvent.getByLabel(genJetTag_, GenJets);
521    
522     ptHat = qScale = -1; crossSection = 0;
523    
524     if (GenEventInfoHandle.isValid()) {
525     qScale = GenEventInfoHandle->qScale();
526     ptHat = (GenEventInfoHandle->hasBinningValues() ? GenEventInfoHandle->binningValues()[0] : 0.0);
527     evtWeight = GenEventInfoHandle->weight();
528    
529     h1_ptHat->Fill(ptHat);
530     }
531     struct genParticles {
532     HepMC::GenParticle* genParticleCore;
533     vector<HepMC::GenParticle*> genParticlesEnd;
534     } genForMatching[4];
535     int hardCount = 0;
536    
537     //cout<<"Event: "<<Evt->signal_process_id()<<endl;
538    
539     for (HepMC::GenEvent::particle_const_iterator iGenParticle = Evt->particles_begin(); iGenParticle != Evt->particles_end(); ++iGenParticle) {
540     HepMC::GenParticle *myGenPart = *iGenParticle;
541     if (myGenPart->status() == 23 && hardCount < 4) {
542     genForMatching[hardCount].genParticleCore = myGenPart;
543    
544     for(HepMC::GenVertex::particle_iterator iGenDescend = myGenPart->end_vertex()->particles_begin(HepMC::descendants);
545     iGenDescend != myGenPart->end_vertex()->particles_end(HepMC::descendants); ++iGenDescend) {
546     HepMC::GenParticle *myGenDescend = *iGenDescend;
547     if (myGenDescend->status() == 71) genForMatching[hardCount].genParticlesEnd.push_back(myGenDescend);
548     }
549     ++hardCount;
550     }
551     }
552    
553     for (GenJetCollection::const_iterator jet_iter = GenJets->begin(); jet_iter!= GenJets->end(); ++jet_iter) {
554     reco::GenJet myJet = reco::GenJet(*jet_iter);
555     if (myJet.pt() > 15 && fabs(myJet.eta()) < 2.5) {
556    
557     TCGenJet* jetCon = new ((*genJets)[genCount]) TCGenJet;
558     jetCon->SetP4(myJet.px(), myJet.py(), myJet.pz(), myJet.energy());
559     jetCon->SetHadEnergy(myJet.hadEnergy());
560     jetCon->SetEmEnergy(myJet.emEnergy());
561     jetCon->SetInvEnergy(myJet.invisibleEnergy());
562     jetCon->SetAuxEnergy(myJet.auxiliaryEnergy());
563     jetCon->SetNumConstit(myJet.getGenConstituents().size());
564    
565     //cout<<"~~JET "<<genCount+1<<"~~"<<endl;
566     //cout<<"(pt, phi, eta) = ("<<myJet.pt()<<", "<<myJet.phi()<<", "<<myJet.eta()<<")"<<endl;
567    
568     vector<const reco::GenParticle*> jetParticles = myJet.getGenConstituents();
569     vector<const reco::Candidate*> mothers;
570    
571     for (vector<const reco::GenParticle*>::const_iterator gen_iter = jetParticles.begin(); gen_iter != jetParticles.end(); ++gen_iter) {
572     const reco::GenParticle *myParticle = *gen_iter;
573     const reco::Candidate *myMother = myParticle->mother(0);
574     const reco::Candidate *seedCandidate;
575    
576     if (myMother->status() == 71) {
577     seedCandidate = myParticle;
578     } else while (myMother->status() != 71 && myMother->numberOfMothers() != 0) {
579     seedCandidate = myMother;
580     myMother = myMother->mother();
581     }
582     if (myMother->status() == 4) continue;
583    
584     for (int iter = 0; iter < (int)seedCandidate->numberOfMothers(); ++iter) {
585     const reco::Candidate *jetPartonCandidate = seedCandidate->mother(iter);
586     if (mothers.size() == 0) {
587     mothers.push_back(jetPartonCandidate);
588     continue;
589     }
590     bool newCandidate = true;
591     for (vector<const reco::Candidate*>::const_iterator cand_iter = mothers.begin(); cand_iter != mothers.end(); ++cand_iter) {
592     if (*cand_iter == jetPartonCandidate) {
593     newCandidate = false;
594     break;
595     }
596     }
597     if (newCandidate == true) {
598     mothers.push_back(jetPartonCandidate);
599     //cout<<jetPartonCandidate<<", status: "<<jetPartonCandidate->status()<<" | pdgId : "<<jetPartonCandidate->pdgId()<<endl;
600     }
601     }
602     }
603     vector<unsigned int> matchCount;
604     bool isMixed = false;
605     int sourceIndex = -1;
606    
607     for (vector<const reco::Candidate*>::const_iterator mother_iter = mothers.begin(); mother_iter != mothers.end(); ++mother_iter) {
608     const reco::Candidate *myMother = *mother_iter;
609    
610     for (int i = 0; i <hardCount; ++i) {
611     matchCount.push_back(0);
612     bool matchFound = false;
613    
614     for (vector<HepMC::GenParticle*>::const_iterator match_iter = genForMatching[i].genParticlesEnd.begin();
615     match_iter != genForMatching[i].genParticlesEnd.end();
616     ++match_iter) {
617     HepMC::GenParticle *myMatch = *match_iter;
618     float matchPt = sqrt(pow(myMatch->momentum().px(), 2) + pow(myMatch->momentum().py(), 2));
619    
620     if (myMatch->pdg_id() == myMother->pdgId()) {
621     float dEta = fabs(myMatch->momentum().eta() - myMother->eta());
622     float dPhi = fabs(myMatch->momentum().phi() - myMother->phi());
623     float dPt = fabs(matchPt - myMother->pt());
624    
625     if (dEta < 0.0001 && dPhi < 0.0001 && dPt < 0.0001) {
626     matchFound = true;
627     if (i != sourceIndex && sourceIndex != -1) isMixed = true;
628     sourceIndex = i;
629     ++matchCount[i];
630     break;
631     }
632     }
633     }
634     if (matchFound == true) break;
635     }
636     }
637     if (isMixed) {
638     h1_partonJetMatch->Fill(5);
639     float matchSum1 = matchCount[0] + matchCount[1];
640     float matchSum2 = matchCount[2] + matchCount[3];
641     float D1 = matchCount[0]/matchSum1;//(mothers.size() * genForMatching[0].genParticlesEnd.size());
642     float D2 = matchCount[2]/matchSum2;//(mothers.size() * genForMatching[1].genParticlesEnd.size());
643     //cout<<"Discriminator 1: "<<D1<<" | Discriminator 2: "<<D2<<endl;
644     } else if (sourceIndex == -1) {
645     h1_partonJetMatch->Fill(0);
646     } else {
647     h1_partonJetMatch->Fill(sourceIndex+1);
648     }
649     ++genCount;
650     }
651     }
652     //cout<<"END EVENT"<<endl;
653     }
654 naodell 1.10
655 naodell 1.16 ///////////////////////////
656     //get trigger information//
657     ///////////////////////////
658 andrey 1.2
659 naodell 1.30 if (triggerHLT_) {
660 andrey 1.2
661 naodell 1.7 edm::Handle<TriggerResults> hltR;
662 naodell 1.26 triggerResultsTag_ = InputTag(hlTriggerResults_,"",hltProcess_);
663 naodell 1.7 iEvent.getByLabel(triggerResultsTag_,hltR);
664 naodell 1.26
665 naodell 1.7 const TriggerNames & triggerNames = iEvent.triggerNames(*hltR);
666 naodell 1.26 hlNames=triggerNames.triggerNames();
667    
668     triggerStatus = 0x0;
669 naodell 1.27
670 naodell 1.30 //for(int i = 0; i < (int)mpiTriggers_.size(); ++i) hltPrescale[i] = hltConfig_.prescaleValue(iEvent, iSetup, mpiTriggers_[i]);
671 naodell 1.27
672 naodell 1.30 for (int i=0; i < (int)hlNames.size(); ++i) {
673     if (!triggerDecision(hltR, i)) continue;
674     for (int j = 0; j < (int)mpiTriggers_.size(); ++j){
675     if (hlNames[i].compare(0, mpiTriggers_[j].length(),mpiTriggers_[j]) == 0) {
676     triggerStatus |= 0x01 << j;
677     }
678     }
679     }
680 naodell 1.7 }
681 naodell 1.27
682 naodell 1.30 if((pfCount >= nJets_ || genCount >= nJets_) && vtxCount > 0) sTree -> Fill();
683 naodell 1.15
684 naodell 1.26 recoJets->Clear();
685 naodell 1.27 recoMET->Clear();
686 naodell 1.26 recoElectrons->Clear();
687     recoMuons->Clear();
688 naodell 1.5 primaryVtx->Clear();
689 naodell 1.26 genJets->Clear();
690 naodell 1.1 }
691    
692     // ------------ method called once each job just before starting event loop ------------
693     void MPIntuple::beginJob()
694 naodell 1.26 {
695 naodell 1.24 ntupleFile = new TFile(rootfilename.c_str(), "RECREATE");
696     sTree = new TTree("mpiTree", "Tree for Jets");
697 naodell 1.15
698 naodell 1.27 h1_ptHat = new TH1D("h1_ptHat", "ptHat", 15, 10.0, 160.0);
699 naodell 1.24 h1_fracAssociatedTracks = new TH1D("h1_fracAssociatedTracks", "Fraction of associated jet tracks", 20, 0.0, 1.0);
700     h1_trackDxy = new TH1D("h1_trackDxy", "dxy for all associated tracks", 50, -0.4, 0.4);
701     h1_meanJetTrackZ = new TH1D("h1_meanJetTrackZ", "<z> from jet tracks", 50, -25.0, 25.0);
702     h1_jetVertexZ = new TH1D("h1_jetVertexZ", "z for vertex associated to jet", 50, -25.0, 25.0);
703     h1_associatedSumPt = new TH1D("h1_associatedSumPt", "ratio of sum p_{T} between associatedTracks and jetTracks", 20, 0.0, 1.0);
704     h1_associatedVertexIndex = new TH1D("h1_associatedVertex", "number of jets associated to primary vertex", 5, -0.5, 4.5);
705 naodell 1.30 h1_partonJetMatch = new TH1D("h1_partonJetMatch", "jet association to hardest scattering", 6, -0.5, 5.5);
706 naodell 1.24 h2_nAssTracksVsJetPt = new TH2F("h2_nAssTracksVsJetPt", "nTracks vs Jet p_{T}", 50, 0, 100, 20, -0.5, 19.5);
707     p1_nVtcs = new TProfile("p1_nVtcs", "<N> per run", 5200.0, 132300.0, 137500.0, 0.0, 6.0);
708    
709 naodell 1.26 recoJets = new TClonesArray("TCJet");
710 naodell 1.27 recoMET = new TClonesArray("TCMET");
711 naodell 1.26 recoElectrons = new TClonesArray("TCElectron");
712     recoMuons = new TClonesArray("TCMuon");
713     genJets = new TClonesArray("TCGenJet");
714 naodell 1.24 primaryVtx = new TClonesArray("TCPrimaryVtx");
715 naodell 1.1
716 naodell 1.26 sTree->Branch("recoJets",&recoJets, 6400, 0);
717     sTree->Branch("recoElectrons",&recoElectrons, 6400, 0);
718     sTree->Branch("recoMuons",&recoMuons, 6400, 0);
719     sTree->Branch("genJets",&genJets, 6400, 0);
720 naodell 1.5 sTree->Branch("primaryVtx",&primaryVtx, 6400, 0);
721 naodell 1.28 sTree->Branch("recoMET",&recoMET, 6400, 0);
722 naodell 1.1 sTree->Branch("eventNumber",&eventNumber, "eventNumber/I");
723     sTree->Branch("runNumber",&runNumber, "runNumber/I");
724     sTree->Branch("lumiSection",&lumiSection, "lumiSection/I");
725 naodell 1.6 sTree->Branch("triggerStatus",&triggerStatus, "triggerStatus/i");
726 naodell 1.27 sTree->Branch("hltPrescale",hltPrescale, "hltPrescale[32]/i");
727 naodell 1.8 sTree->Branch("isRealData",&isRealData, "isRealData/i");
728     sTree->Branch("bunchCross",&bunchCross, "bunchCross/i");
729 naodell 1.28 sTree->Branch("lumiDeadCount",&lumiDeadCount, "lumiDeadCount/f");
730     sTree->Branch("lumiLiveFrac",&lumiLiveFrac, "lumiLiveFrac/f");
731     sTree->Branch("intDelLumi",&intDelLumi, "intDelLumi/f");
732     sTree->Branch("ptHat",&ptHat, "ptHat/f");
733     sTree->Branch("qScale", &qScale, "qScale/f");
734 naodell 1.30 sTree->Branch("evtWeight", &evtWeight, "evtWeight/f");
735 naodell 1.28 sTree->Branch("crossSection", &crossSection, "crossSection/f");
736 naodell 1.1 }
737    
738 naodell 1.26 void MPIntuple::beginRun(const edm::Run& iRun, const edm::EventSetup& iEvent)
739     {
740     bool changed = true;
741     hltConfig_.init(iRun, iEvent, hltProcess_, changed);
742     }
743    
744     void MPIntuple::endLuminosityBlock(const edm::LuminosityBlock& iLumi, const edm::EventSetup& iEvent)
745     {
746     edm::Handle<LumiSummary> lumiSummary;
747     iLumi.getByLabel("lumiProducer", lumiSummary);
748    
749 naodell 1.27 lumiDeadCount = lumiSummary->deadcount();
750     lumiLiveFrac = lumiSummary->liveFrac();
751     intDelLumi = lumiSummary->avgInsDelLumi()*93.244;
752    
753     //cout<<iLumi.id().luminosityBlock()<<endl;
754     //cout<<"\t Dead Count = "<<lumiSummary->deadcount()<<endl;
755     //cout<<"\t Fraction of dead time = "<<1 - lumiSummary->liveFrac()<<endl;
756     //cout<<"\t Integrated luminosity = "<<lumiSummary->avgInsDelLumi()*93.244<<endl;
757     //cout<<"\t Dead time corrected luminosity = "<<lumiSummary->avgInsDelLumi()*lumiSummary->liveFrac()*93.244<<endl;
758 naodell 1.26 }
759    
760 naodell 1.30 void MPIntuple::endRun(const edm::Run& iRun, const edm::Event& iEvent, const edm::EventSetup& iSetup)
761 naodell 1.26 {
762 naodell 1.30 Handle<GenRunInfoProduct> GenRunInfoHandle;
763     iEvent.getByLabel("generator", GenRunInfoHandle);
764    
765     if (GenRunInfoHandle.isValid()) {
766     crossSection = GenRunInfoHandle->crossSection();
767     }
768    
769     for(int i = 0; i < (int)mpiTriggers_.size(); ++i) hltPrescale[i] = hltConfig_.prescaleValue(iEvent, iSetup, mpiTriggers_[i]);
770 naodell 1.27 //for (int i = 0; i < (int)mpiTriggers_.size(); ++i) cout << mpiTriggers_[i] << " prescale = " << hltPrescale[i] <<endl;
771 naodell 1.26 }
772 naodell 1.1 // ------------ method called once each job just after ending the event loop ------------
773     void MPIntuple::endJob()
774     {
775 naodell 1.17 ntupleFile->cd();
776    
777 naodell 1.27 h1_ptHat->Write();
778 naodell 1.24 h1_fracAssociatedTracks->Write();
779     h1_meanJetTrackZ->Write();
780 naodell 1.16 h1_trackDxy->Write();
781 naodell 1.24 h1_jetVertexZ->Write();
782     h1_associatedSumPt->Write();
783     h1_associatedVertexIndex->Write();
784 naodell 1.30 h1_partonJetMatch->Write();
785 naodell 1.16 h2_nAssTracksVsJetPt->Write();
786 naodell 1.15 p1_nVtcs->Write();
787    
788 naodell 1.1 ntupleFile->Write();
789     ntupleFile->Close();
790     }
791    
792 naodell 1.30 bool MPIntuple::genParticleMatch(const HepMC::GenParticle& genParticle, const reco::Candidate& motherParticle)
793     {
794     float genPhi, momPhi, genEta, momEta;
795     genPhi = genParticle.momentum().phi();
796     genEta = genParticle.momentum().eta();
797     momPhi = motherParticle.phi();
798     momEta = motherParticle.eta();
799    
800     float dR = sqrt(pow((genPhi - momPhi), 2) + pow((genEta - momEta), 2));
801     if (dR < 0.00001) return true;
802     else return false;
803     }
804 naodell 1.1
805 andrey 1.2 bool MPIntuple::triggerDecision(edm::Handle<edm::TriggerResults> &hltR, int iTrigger)
806     {
807     bool triggerPassed = false;
808     if(hltR->wasrun(iTrigger) &&
809     hltR->accept(iTrigger) &&
810     !hltR->error(iTrigger) ){
811     triggerPassed = true;
812     }
813     return triggerPassed;
814 naodell 1.16 }
815    
816 naodell 1.27 float MPIntuple::sumPtSquared(const Vertex & v)
817 naodell 1.16 {
818 naodell 1.27 float sum = 0.;
819     float pT;
820 naodell 1.16 for (Vertex::trackRef_iterator it = v.tracks_begin(); it != v.tracks_end(); it++) {
821     pT = (**it).pt();
822 naodell 1.27 float epT=(**it).ptError(); pT=pT>epT ? pT-epT : 0;
823 naodell 1.16
824     sum += pT*pT;
825 andrey 1.2 }
826 naodell 1.16 return sum;
827     }
828 andrey 1.2
829 naodell 1.1 //define this as a plug-in
830     DEFINE_FWK_MODULE(MPIntuple);