ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MPIAnalyzer/src/MPIntuple.cc
Revision: 1.31
Committed: Sat Mar 26 19:28:05 2011 UTC (14 years, 1 month ago) by naodell
Content type: text/plain
Branch: MAIN
Changes since 1.30: +29 -12 lines
Log Message:
Next commit will have revision to jet-parton matching

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