ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/MPIAnalyzer/src/MPIntuple.cc
Revision: 1.33
Committed: Wed Apr 6 10:30:52 2011 UTC (14 years, 1 month ago) by naodell
Content type: text/plain
Branch: MAIN
Changes since 1.32: +6 -2 lines
Log Message:
*** empty log message ***

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