ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/interface/Tuple_Jet.h
Revision: 1.3
Committed: Wed Nov 14 06:54:22 2012 UTC (12 years, 5 months ago) by bbetchar
Content type: text/plain
Branch: MAIN
Changes since 1.2: +8 -7 lines
Log Message:
polar lorentz

File Contents

# User Rev Content
1 bbetchar 1.1 #ifndef TUPLE_JET
2     #define TUPLE_JET
3    
4 bbetchar 1.2 #include "boost/foreach.hpp"
5 bbetchar 1.3 #include "TopQuarkAnalysis/TopRefTuple/interface/fTypes.h"
6 bbetchar 1.1 #include "FWCore/Framework/interface/EDProducer.h"
7     #include "FWCore/Framework/interface/Frameworkfwd.h"
8     #include "FWCore/Framework/interface/Event.h"
9    
10     #include "DataFormats/JetReco/interface/PFJet.h"
11     #include "DataFormats/PatCandidates/interface/Jet.h"
12    
13     #include "PhysicsTools/SelectorUtils/interface/PFJetIDSelectionFunctor.h"
14    
15     #include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
16     #include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
17     #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
18    
19     template< typename T >
20     class Tuple_Jet : public edm::EDProducer {
21     public:
22     explicit Tuple_Jet(const edm::ParameterSet&);
23     private:
24     void produce(edm::Event&, const edm::EventSetup& );
25     void initSpecial(); void produceSpecial(edm::Event&, const edm::Handle<edm::View<T> >&);
26     void initPF(); void producePF(edm::Event&, const edm::Handle<edm::View<T> >&);
27     void initBJetTag(); void produceBJetTag(edm::Event&, const edm::Handle<edm::View<T> >&);
28     void initGenJetMatch(); void produceGenJetMatch(edm::Event&, const edm::Handle<edm::View<T> >&);
29    
30     template <class I> int indexOfMatch( const reco::GenJet*, const I, const I);
31 bbetchar 1.2 std::auto_ptr<std::vector<float> > correctionFactors(const edm::Handle<edm::View<T> >&);
32 bbetchar 1.1
33 bbetchar 1.2 const edm::InputTag jetsTag, genTag;
34     const std::string prefix,jecRecord;
35     const std::vector<std::string> btagNames;
36     const bool pfSpecific, gen;
37 bbetchar 1.1
38     };
39    
40     template<class T> Tuple_Jet<T>::
41     Tuple_Jet(const edm::ParameterSet& cfg) :
42 bbetchar 1.2 jetsTag(cfg.getParameter<edm::InputTag>("jetsTag")),
43     genTag(cfg.getParameter<edm::InputTag>("genTag")),
44     prefix(cfg.getParameter<std::string>("prefix")),
45     jecRecord(cfg.getParameter<std::string>("jecRecord")),
46     btagNames(cfg.getParameter<std::vector<std::string> >("bTags")),
47     pfSpecific(cfg.getParameter<bool>("pfInfo")),
48     gen(cfg.getParameter<bool>("genInfo"))
49 bbetchar 1.1 {
50 bbetchar 1.3 produces <std::vector<fTypes::dPolarLorentzV> > ( prefix + "P4" );
51     produces <std::vector<float> > ( prefix + "JecUnc" );
52     produces <std::vector<float> > ( prefix + "Area" );
53     produces <std::vector<float> > ( prefix + "Eta2Moment" );
54     produces <std::vector<float> > ( prefix + "Phi2Moment" );
55 bbetchar 1.1 initSpecial();
56     }
57    
58     JetCorrectionUncertainty* jetCorrUnc(const edm::EventSetup& setup, const std::string& jecRecord) {
59     edm::ESHandle<JetCorrectorParametersCollection> JetCorParColl;
60     setup.get<JetCorrectionsRecord>().get(jecRecord,JetCorParColl);
61     return new JetCorrectionUncertainty((*JetCorParColl)["Uncertainty"]);
62     }
63    
64     float uncFunc(JetCorrectionUncertainty* jCU, const reco::Candidate::LorentzVector& jet) {
65     jCU->setJetEta(jet.eta());
66     jCU->setJetPt(jet.pt());// the uncertainty is a function of the corrected pt
67     try {return jCU->getUncertainty(true);} // sigh,,, they are still throwing exceptions
68     catch (...) {
69     std::cout << "JetCorrectionUncertainty::getUncertainty threw exception on jet with pt( " << jet.pt() << " ) and eta ( " << jet.eta() << " )." << std::endl;
70     return 0.0;
71     }
72     }
73    
74     template< typename T >
75     void Tuple_Jet<T>::
76     produce(edm::Event& evt, const edm::EventSetup& setup) {
77 bbetchar 1.3 typedef fTypes::dPolarLorentzV LorentzV;
78 bbetchar 1.2 edm::Handle<edm::View<T> > jets; evt.getByLabel(jetsTag, jets);
79 bbetchar 1.1
80     std::auto_ptr<std::vector<LorentzV> > p4 ( new std::vector<LorentzV>() ) ;
81     std::auto_ptr<std::vector<float> > jetArea ( new std::vector<float>() ) ;
82     std::auto_ptr<std::vector<float> > jecUnc ( new std::vector<float>() ) ;
83     std::auto_ptr<std::vector<float> > eta2mom ( new std::vector<float>() ) ;
84     std::auto_ptr<std::vector<float> > phi2mom ( new std::vector<float>() ) ;
85    
86 bbetchar 1.2 if(jets.isValid()) {
87     JetCorrectionUncertainty* jCU = jetCorrUnc(setup, jecRecord);
88     for(typename edm::View<T>::const_iterator jet = jets->begin(); jet!=jets->end(); jet++) {
89 bbetchar 1.3 p4->push_back(LorentzV(jet->pt(),jet->eta(),jet->phi(),jet->mass()));
90 bbetchar 1.2 eta2mom->push_back(jet->etaetaMoment());
91     phi2mom->push_back(jet->phiphiMoment());
92     jetArea->push_back(jet->jetArea());
93    
94     jecUnc->push_back(uncFunc(jCU, jet->p4()));
95     }
96     delete jCU;
97 bbetchar 1.1 }
98    
99 bbetchar 1.2 evt.put( p4, prefix + "P4" );
100     evt.put( jecUnc, prefix + "JecUnc" );
101     evt.put( jetArea, prefix + "Area" );
102     evt.put( eta2mom, prefix + "Eta2Moment" );
103     evt.put( phi2mom, prefix + "Phi2Moment" );
104 bbetchar 1.1
105     produceSpecial(evt, jets);
106     }
107    
108     template<> void Tuple_Jet<pat::Jet>::initSpecial() {
109     initPF();
110     initBJetTag();
111     initGenJetMatch();
112     }
113    
114     template<> void Tuple_Jet<pat::Jet>::produceSpecial(edm::Event& e,const edm::Handle<edm::View<pat::Jet> >& h) {
115     producePF(e,h);
116     produceBJetTag(e,h);
117     produceGenJetMatch(e,h);
118     }
119    
120    
121 bbetchar 1.2 template<class T> void Tuple_Jet<T>::
122     initPF() { if(!pfSpecific) return;
123     produces <std::vector<float> > (prefix + "FchargedHad" );
124     produces <std::vector<float> > (prefix + "FneutralHad" );
125     produces <std::vector<float> > (prefix + "FchargedEm" );
126     produces <std::vector<float> > (prefix + "FneutralEm" );
127     produces <std::vector<float> > (prefix + "FchargedMu" );
128 bbetchar 1.1
129    
130 bbetchar 1.2 produces <std::vector<unsigned> > (prefix + "Ncharged" );
131     produces <std::vector<unsigned> > (prefix + "Nneutral" );
132     produces <std::vector<unsigned> > (prefix + "Nmuon" );
133 bbetchar 1.1
134 bbetchar 1.2 produces <std::vector<bool> > ( prefix + "PFJetIDloose" );
135     produces <std::vector<bool> > ( prefix + "PFJetIDtight" );
136 bbetchar 1.1
137     }
138    
139     template<class T> void Tuple_Jet<T>::
140     producePF(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) { if(!pfSpecific) return;
141     std::auto_ptr<std::vector<float> > FchargedHad( new std::vector<float>() );
142     std::auto_ptr<std::vector<float> > FneutralHad( new std::vector<float>() );
143     std::auto_ptr<std::vector<float> > FchargedEm( new std::vector<float>() );
144     std::auto_ptr<std::vector<float> > FneutralEm( new std::vector<float>() );
145     std::auto_ptr<std::vector<float> > FchargedMu( new std::vector<float>() );
146    
147     std::auto_ptr<std::vector<unsigned> > Ncharged( new std::vector<unsigned>() );
148     std::auto_ptr<std::vector<unsigned> > Nneutral( new std::vector<unsigned>() );
149     std::auto_ptr<std::vector<unsigned> > Nmuon( new std::vector<unsigned>() );
150    
151 bbetchar 1.2 std::auto_ptr<std::vector<bool> > pfjetidloose ( new std::vector<bool>() ) ;
152     std::auto_ptr<std::vector<bool> > pfjetidtight ( new std::vector<bool>() ) ;
153 bbetchar 1.1
154     PFJetIDSelectionFunctor
155     pfLooseJetID(PFJetIDSelectionFunctor::FIRSTDATA, PFJetIDSelectionFunctor::LOOSE),
156     pfTightJetID(PFJetIDSelectionFunctor::FIRSTDATA, PFJetIDSelectionFunctor::TIGHT);
157    
158 bbetchar 1.2 if(jets.isValid()) {
159     for(typename edm::View<T>::const_iterator jet = jets->begin(); jet!=jets->end(); jet++) {
160     pat::strbitset
161     passLooseCuts( pfLooseJetID .getBitTemplate() ),
162     passTightCuts( pfTightJetID .getBitTemplate() );
163    
164     FchargedHad->push_back( jet->chargedHadronEnergyFraction() );
165     FneutralHad->push_back( jet->neutralHadronEnergyFraction() );
166     FchargedEm->push_back( jet->chargedEmEnergyFraction() );
167     FneutralEm->push_back( jet->neutralEmEnergyFraction() );
168     FchargedMu->push_back( jet->chargedMuEnergyFraction() );
169     Ncharged->push_back( (unsigned) jet->chargedMultiplicity() );
170     Nneutral->push_back( (unsigned) jet->neutralMultiplicity() );
171     Nmuon->push_back( (unsigned) jet->muonMultiplicity() );
172    
173     pfjetidloose->push_back(pfLooseJetID( *jet, passLooseCuts ));
174     pfjetidtight->push_back(pfTightJetID( *jet, passTightCuts ));
175     }
176 bbetchar 1.1 }
177    
178 bbetchar 1.2 evt.put(FchargedHad, prefix + "FchargedHad" );
179     evt.put(FneutralHad, prefix + "FneutralHad" );
180     evt.put(FchargedEm, prefix + "FchargedEm" );
181     evt.put(FneutralEm, prefix + "FneutralEm" );
182     evt.put(FchargedMu, prefix + "FchargedMu" );
183     evt.put(Ncharged, prefix + "Ncharged" );
184     evt.put(Nneutral, prefix + "Nneutral" );
185     evt.put(Nmuon, prefix + "Nmuon" );
186 bbetchar 1.1
187 bbetchar 1.2 evt.put( pfjetidloose, prefix + "PFJetIDloose" );
188     evt.put( pfjetidtight, prefix + "PFJetIDtight" );
189 bbetchar 1.1
190     }
191    
192    
193     template<class T> void Tuple_Jet<T>::
194     initBJetTag(){
195 bbetchar 1.2 produces <std::vector<int> > (prefix + "GenJetFlavour" );
196     BOOST_FOREACH(const std::string& btag, btagNames)
197     produces <std::vector<float> > (prefix + btag);
198 bbetchar 1.1 }
199    
200     template<class T> void Tuple_Jet<T>::
201 bbetchar 1.2 produceBJetTag(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) {
202     std::auto_ptr<std::vector<int> > flavour(new std::vector<int>() );
203     std::map<std::string, std::vector<float>* > btags;
204     BOOST_FOREACH(const std::string& btag, btagNames)
205     btags[btag] = new std::vector<float>();
206    
207     if(jets.isValid()) {
208     for(typename edm::View<T>::const_iterator jet = jets->begin(); jet!=jets->end(); jet++) {
209     flavour->push_back(jet->partonFlavour());
210     BOOST_FOREACH(const std::string& btag, btagNames)
211     btags[btag]->push_back(jet->bDiscriminator(btag+"BJetTags"));
212 bbetchar 1.1 }
213     }
214    
215 bbetchar 1.2 evt.put( flavour, prefix + "GenJetFlavour" );
216     BOOST_FOREACH(const std::string& btag, btagNames)
217     evt.put( std::auto_ptr<std::vector<float> >(btags[btag]), prefix + btag );
218 bbetchar 1.1 }
219    
220     template<class T> void Tuple_Jet<T>::
221 bbetchar 1.2 initGenJetMatch() { if(gen) produces <std::vector<int> > (prefix + "GenJetMatchIndex" );}
222 bbetchar 1.1
223     template<class T> void Tuple_Jet<T>::
224     produceGenJetMatch(edm::Event& evt, const edm::Handle<edm::View<T> >& jets){
225     if(!gen) return;
226     edm::Handle<edm::View<reco::GenJet> > genjets;
227 bbetchar 1.2 evt.getByLabel(genTag, genjets);
228 bbetchar 1.1 std::auto_ptr<std::vector<int> > genjetMatchIndex( new std::vector<int>() );
229     if(jets.isValid() && genjets.isValid())
230     for (typename edm::View<T>::const_iterator jet = jets->begin(); jet!=jets->end(); ++jet )
231     genjetMatchIndex->push_back( indexOfMatch( jet->genJet(), genjets->begin(), genjets->end()) );
232 bbetchar 1.2 evt.put(genjetMatchIndex, prefix + "GenJetMatchIndex" );
233 bbetchar 1.1 }
234    
235     template<class T> template<class I> int Tuple_Jet<T>::
236     indexOfMatch( const reco::GenJet* genjet, const I begin, const I end) {
237     for(I it=begin; it!=end; ++it) if ( genjet && genjet->p4() == it->p4() ) return it-begin; //p4 comparisons
238     return -1;
239     }
240    
241    
242     #endif
243    
244