ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/Betchart/TopRefTuple/interface/Tuple_Jet.h
Revision: 1.1
Committed: Wed Nov 7 21:53:10 2012 UTC (12 years, 5 months ago) by bbetchar
Content type: text/plain
Branch: MAIN
Log Message:
Grab ntuplizing codes from UserCode/SusyCAF

File Contents

# User Rev Content
1 bbetchar 1.1 #ifndef TUPLE_JET
2     #define TUPLE_JET
3    
4     #include "FWCore/Framework/interface/EDProducer.h"
5     #include "FWCore/Framework/interface/Frameworkfwd.h"
6     #include "FWCore/Framework/interface/Event.h"
7     #include "FWCore/Utilities/interface/InputTag.h"
8    
9     #include "DataFormats/JetReco/interface/PFJet.h"
10     #include "DataFormats/JetReco/interface/JPTJet.h"
11     #include "DataFormats/JetReco/interface/CaloJet.h"
12     #include "DataFormats/PatCandidates/interface/Jet.h"
13    
14     #include "PhysicsTools/SelectorUtils/interface/JetIDSelectionFunctor.h"
15     #include "PhysicsTools/SelectorUtils/interface/PFJetIDSelectionFunctor.h"
16    
17     #include "DataFormats/TrackReco/interface/Track.h"
18     #include "DataFormats/TrackReco/interface/TrackBase.h"
19     #include "DataFormats/TrackReco/interface/TrackFwd.h"
20     #include "DataFormats/VertexReco/interface/Vertex.h"
21    
22     #include "CondFormats/JetMETObjects/interface/JetCorrectorParameters.h"
23     #include "CondFormats/JetMETObjects/interface/JetCorrectionUncertainty.h"
24     #include "JetMETCorrections/Objects/interface/JetCorrectionsRecord.h"
25    
26     template< typename T >
27     class Tuple_Jet : public edm::EDProducer {
28     public:
29     explicit Tuple_Jet(const edm::ParameterSet&);
30     private:
31     void produce(edm::Event&, const edm::EventSetup& );
32     void initSpecial(); void produceSpecial(edm::Event&, const edm::Handle<edm::View<T> >&);
33     void initPF(); void producePF(edm::Event&, const edm::Handle<edm::View<T> >&);
34     void initCalo(); void produceCalo(edm::Event&, const edm::Handle<edm::View<T> >&);
35     void initJetID(); void produceJetID(edm::Event&, const edm::Handle<edm::View<T> >&);
36     void initBJetTag(); void produceBJetTag(edm::Event&, const edm::Handle<edm::View<T> >&);
37     void initMPT(); void produceMPT(edm::Event&, const edm::Handle<edm::View<T> >&);
38     void initGenJetMatch(); void produceGenJetMatch(edm::Event&, const edm::Handle<edm::View<T> >&);
39    
40     template <class I> int indexOfMatch( const reco::GenJet*, const I, const I);
41     std::auto_ptr<std::vector<double> > correctionFactors(const edm::Handle<edm::View<T> >&);
42     const reco::CaloJet* getJet( const edm::RefToBase<T>& );
43    
44     const edm::ParameterSet& config;
45     const edm::InputTag jetsInputTag, genJetsInputTag, allJetsInputTag;
46     const std::string Prefix,Suffix,JecRecord;
47     const bool caloSpecific, jptSpecific, pfSpecific, mpt, jetid, gen;
48    
49    
50     enum VERTEX {VINIT=0, V_PRIMARY=0, V_PILEUP=1, VSIZE=2};
51     static const std::string vertex_names[];
52    
53     struct dsz {
54     const reco::Track& track;
55     dsz(const reco::Track& t) : track(t) {}
56     double operator()(const reco::Vertex& v) {return fabs(track.dsz(v.position()));}
57     };
58     };
59    
60     template <class T>
61     const std::string Tuple_Jet<T>::vertex_names[2] = {"Primary", "PileUp"};
62    
63     template<class T> Tuple_Jet<T>::
64     Tuple_Jet(const edm::ParameterSet& cfg) :
65     config(cfg),
66     jetsInputTag(cfg.getParameter<edm::InputTag>("InputTag")),
67     genJetsInputTag(cfg.getParameter<edm::InputTag>("GenInputTag")),
68     allJetsInputTag(cfg.getParameter<edm::InputTag>("AllJets")),
69     Prefix(cfg.getParameter<std::string>("Prefix")),
70     Suffix(cfg.getParameter<std::string>("Suffix")),
71     JecRecord(cfg.getParameter<std::string>("JecRecord")),
72     caloSpecific(cfg.getParameter<bool>("Calo")),
73     jptSpecific(cfg.getParameter<bool>("JPT")),
74     pfSpecific(cfg.getParameter<bool>("PF")),
75     mpt(cfg.getParameter<bool>("MPT")),
76     jetid(cfg.getParameter<bool>("JetID")),
77     gen(cfg.getParameter<bool>("GenInfo"))
78     {
79     produces <std::vector<reco::Candidate::LorentzVector> > ( Prefix + "CorrectedP4" + Suffix );
80     produces <std::vector<double> > ( Prefix + "CorrFactor" + Suffix );
81     produces <std::vector<float> > ( Prefix + "JecUnc" + Suffix );
82     produces <std::vector<float> > ( Prefix + "Area" + Suffix );
83     produces <std::vector<float> > ( Prefix + "Eta2Moment" + Suffix );
84     produces <std::vector<float> > ( Prefix + "Phi2Moment" + Suffix );
85     produces <reco::Candidate::LorentzVector> ( Prefix + "DroppedSumP4" + Suffix );
86     produces <float> ( Prefix + "DroppedSumPT" + Suffix );
87     produces <float> ( Prefix + "DroppedSumET" + Suffix );
88     initSpecial();
89     }
90    
91     JetCorrectionUncertainty* jetCorrUnc(const edm::EventSetup& setup, const std::string& jecRecord) {
92     edm::ESHandle<JetCorrectorParametersCollection> JetCorParColl;
93     setup.get<JetCorrectionsRecord>().get(jecRecord,JetCorParColl);
94     return new JetCorrectionUncertainty((*JetCorParColl)["Uncertainty"]);
95     }
96    
97     float uncFunc(JetCorrectionUncertainty* jCU, const reco::Candidate::LorentzVector& jet) {
98     jCU->setJetEta(jet.eta());
99     jCU->setJetPt(jet.pt());// the uncertainty is a function of the corrected pt
100     try {return jCU->getUncertainty(true);} // sigh,,, they are still throwing exceptions
101     catch (...) {
102     std::cout << "JetCorrectionUncertainty::getUncertainty threw exception on jet with pt( " << jet.pt() << " ) and eta ( " << jet.eta() << " )." << std::endl;
103     return 0.0;
104     }
105     }
106    
107     template< typename T >
108     void Tuple_Jet<T>::
109     produce(edm::Event& evt, const edm::EventSetup& setup) {
110     typedef reco::Candidate::LorentzVector LorentzV;
111     edm::Handle<edm::View<T> > jets; evt.getByLabel(jetsInputTag, jets);
112     edm::Handle<edm::View<T> > allJets; evt.getByLabel(allJetsInputTag, allJets);
113    
114     std::auto_ptr<std::vector<LorentzV> > p4 ( new std::vector<LorentzV>() ) ;
115     std::auto_ptr<std::vector<float> > jetArea ( new std::vector<float>() ) ;
116     std::auto_ptr<std::vector<float> > jecUnc ( new std::vector<float>() ) ;
117     std::auto_ptr<std::vector<float> > eta2mom ( new std::vector<float>() ) ;
118     std::auto_ptr<std::vector<float> > phi2mom ( new std::vector<float>() ) ;
119     std::auto_ptr<LorentzV> droppedSumP4 ( new LorentzV() ) ;
120     std::auto_ptr<float> droppedSumPT ( new float(0) ) ;
121     std::auto_ptr<float> droppedSumET ( new float(0) ) ;
122    
123     JetCorrectionUncertainty* jCU = jetCorrUnc(setup, JecRecord);
124    
125     for(unsigned i=0; jets.isValid() && i<(*jets).size(); i++) {
126     p4->push_back((*jets)[i].p4());
127     eta2mom->push_back((*jets)[i].etaetaMoment());
128     phi2mom->push_back((*jets)[i].phiphiMoment());
129     jetArea->push_back((*jets)[i].jetArea());
130     *droppedSumP4 -= (*jets)[i].p4();
131     *droppedSumPT -= (*jets)[i].pt();
132     *droppedSumET -= (*jets)[i].p4().Et();
133    
134     jecUnc->push_back(uncFunc(jCU, (*jets)[i].p4()));
135     }
136     for(unsigned i=0; i<(*allJets).size(); i++) {
137     *droppedSumP4 += (*allJets)[i].p4();
138     *droppedSumPT += (*allJets)[i].pt();
139     *droppedSumET += (*allJets)[i].p4().Et();
140     }
141    
142     delete jCU;
143    
144     evt.put( p4, Prefix + "CorrectedP4" + Suffix );
145     evt.put( correctionFactors(jets), Prefix + "CorrFactor" + Suffix) ;
146     evt.put( jecUnc, Prefix + "JecUnc" + Suffix );
147     evt.put( jetArea, Prefix + "Area" + Suffix );
148     evt.put( eta2mom, Prefix + "Eta2Moment" + Suffix );
149     evt.put( phi2mom, Prefix + "Phi2Moment" + Suffix );
150     evt.put( droppedSumP4, Prefix + "DroppedSumP4" + Suffix );
151     evt.put( droppedSumPT, Prefix + "DroppedSumPT" + Suffix );
152     evt.put( droppedSumET, Prefix + "DroppedSumET" + Suffix );
153    
154     produceSpecial(evt, jets);
155     }
156    
157     template<class T>
158     std::auto_ptr<std::vector<double> > Tuple_Jet<T>::
159     correctionFactors(const edm::Handle<edm::View<T> >& jets) {
160     if(jets.isValid()) return std::auto_ptr<std::vector<double> >(new std::vector<double>(jets->size(),1.0) ) ;
161     else return std::auto_ptr<std::vector<double> >(new std::vector<double>());
162     }
163    
164     template<>
165     std::auto_ptr<std::vector<double> > Tuple_Jet<pat::Jet>::
166     correctionFactors(const edm::Handle<edm::View<pat::Jet> >& jets) {
167     std::auto_ptr<std::vector<double> > correction ( new std::vector<double>() );
168     for(unsigned i=0; jets.isValid() && i<(*jets).size(); i++)
169     correction->push_back( (*jets)[i].jecSetsAvailable() ?
170     (*jets)[i].energy() / (*jets)[i].correctedJet("Uncorrected").energy() :
171     1.0 );
172     return correction;
173     }
174    
175    
176    
177    
178     template<> void Tuple_Jet<reco::PFJet>::initSpecial() { initPF(); }
179     template<> void Tuple_Jet<reco::CaloJet>::initSpecial() { initCalo(); }
180     template<> void Tuple_Jet<reco::JPTJet>::initSpecial() { initCalo(); }
181     template<> void Tuple_Jet<pat::Jet>::initSpecial() {
182     initPF();
183     initCalo();
184     initMPT();
185     initJetID();
186     initBJetTag();
187     initGenJetMatch();
188     }
189    
190     template<> void Tuple_Jet<reco::PFJet>::produceSpecial(edm::Event& e,const edm::Handle<edm::View<reco::PFJet> >& h) {producePF(e,h);}
191     template<> void Tuple_Jet<reco::CaloJet>::produceSpecial(edm::Event& e,const edm::Handle<edm::View<reco::CaloJet> >& h) {produceCalo(e,h);}
192     template<> void Tuple_Jet<reco::JPTJet>::produceSpecial(edm::Event& e,const edm::Handle<edm::View<reco::JPTJet> >& h) {produceCalo(e,h);}
193     template<> void Tuple_Jet<pat::Jet>::produceSpecial(edm::Event& e,const edm::Handle<edm::View<pat::Jet> >& h) {
194     producePF(e,h);
195     produceCalo(e,h);
196     produceMPT(e,h);
197     produceJetID(e,h);
198     produceBJetTag(e,h);
199     produceGenJetMatch(e,h);
200     }
201    
202    
203    
204    
205     template<class T> void Tuple_Jet<T>::
206     initPF() { if(!pfSpecific) return;
207     produces <std::vector<float> > (Prefix + "FchargedHad" + Suffix);
208     produces <std::vector<float> > (Prefix + "FneutralHad" + Suffix);
209     produces <std::vector<float> > (Prefix + "FchargedEm" + Suffix);
210     produces <std::vector<float> > (Prefix + "FneutralEm" + Suffix);
211     produces <std::vector<float> > (Prefix + "FchargedMu" + Suffix);
212    
213     produces <std::vector<float> > (Prefix + "EchargedHad" + Suffix);
214     produces <std::vector<float> > (Prefix + "EneutralHad" + Suffix);
215     produces <std::vector<float> > (Prefix + "EchargedEM" + Suffix);
216     produces <std::vector<float> > (Prefix + "EneutralEM" + Suffix);
217     produces <std::vector<float> > (Prefix + "Ephoton" + Suffix);
218     produces <std::vector<float> > (Prefix + "Eelectron" + Suffix);
219     produces <std::vector<float> > (Prefix + "Emuon" + Suffix);
220     produces <std::vector<float> > (Prefix + "EHFHad" + Suffix);
221     produces <std::vector<float> > (Prefix + "EHFEM" + Suffix);
222    
223     produces <std::vector<unsigned> > (Prefix + "Ncharged" + Suffix);
224     produces <std::vector<unsigned> > (Prefix + "Nneutral" + Suffix);
225     produces <std::vector<unsigned> > (Prefix + "Nmuon" + Suffix);
226    
227     produces <std::vector<unsigned> > (Prefix + "NchargedHad" + Suffix);
228     produces <std::vector<unsigned> > (Prefix + "NneutralHad" + Suffix);
229     produces <std::vector<unsigned> > (Prefix + "Nphoton" + Suffix);
230     produces <std::vector<unsigned> > (Prefix + "Nelectron" + Suffix);
231     produces <std::vector<unsigned> > (Prefix + "NHFHad" + Suffix);
232     produces <std::vector<unsigned> > (Prefix + "NHFEM" + Suffix);
233    
234     produces <std::vector<int> > ( Prefix + "PFJetIDloose" + Suffix );
235     produces <std::vector<int> > ( Prefix + "PFJetIDtight" + Suffix );
236    
237     }
238    
239     template<class T> void Tuple_Jet<T>::
240     producePF(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) { if(!pfSpecific) return;
241     std::auto_ptr<std::vector<float> > FchargedHad( new std::vector<float>() );
242     std::auto_ptr<std::vector<float> > FneutralHad( new std::vector<float>() );
243     std::auto_ptr<std::vector<float> > FchargedEm( new std::vector<float>() );
244     std::auto_ptr<std::vector<float> > FneutralEm( new std::vector<float>() );
245     std::auto_ptr<std::vector<float> > FchargedMu( new std::vector<float>() );
246    
247     std::auto_ptr<std::vector<float> > EchargedHad( new std::vector<float>() );
248     std::auto_ptr<std::vector<float> > EneutralHad( new std::vector<float>() );
249     std::auto_ptr<std::vector<float> > EchargedEM( new std::vector<float>() );
250     std::auto_ptr<std::vector<float> > EneutralEM( new std::vector<float>() );
251     std::auto_ptr<std::vector<float> > Ephoton( new std::vector<float>() );
252     std::auto_ptr<std::vector<float> > Eelectron( new std::vector<float>() );
253     std::auto_ptr<std::vector<float> > Emuon( new std::vector<float>() );
254     std::auto_ptr<std::vector<float> > EHFHad( new std::vector<float>() );
255     std::auto_ptr<std::vector<float> > EHFEM( new std::vector<float>() );
256    
257     std::auto_ptr<std::vector<unsigned> > Ncharged( new std::vector<unsigned>() );
258     std::auto_ptr<std::vector<unsigned> > Nneutral( new std::vector<unsigned>() );
259     std::auto_ptr<std::vector<unsigned> > Nmuon( new std::vector<unsigned>() );
260    
261     std::auto_ptr<std::vector<unsigned> > NchargedHad( new std::vector<unsigned>() );
262     std::auto_ptr<std::vector<unsigned> > NneutralHad( new std::vector<unsigned>() );
263     std::auto_ptr<std::vector<unsigned> > Nphoton( new std::vector<unsigned>() );
264     std::auto_ptr<std::vector<unsigned> > Nelectron( new std::vector<unsigned>() );
265     std::auto_ptr<std::vector<unsigned> > NHFHad( new std::vector<unsigned>() );
266     std::auto_ptr<std::vector<unsigned> > NHFEM( new std::vector<unsigned>() );
267    
268     std::auto_ptr<std::vector<int> > pfjetidloose ( new std::vector<int>() ) ;
269     std::auto_ptr<std::vector<int> > pfjetidtight ( new std::vector<int>() ) ;
270    
271     PFJetIDSelectionFunctor
272     pfLooseJetID(PFJetIDSelectionFunctor::FIRSTDATA, PFJetIDSelectionFunctor::LOOSE),
273     pfTightJetID(PFJetIDSelectionFunctor::FIRSTDATA, PFJetIDSelectionFunctor::TIGHT);
274    
275     for( unsigned i=0; jets.isValid() && i<(*jets).size(); i++) {
276     pat::strbitset
277     passLooseCuts( pfLooseJetID .getBitTemplate() ),
278     passTightCuts( pfTightJetID .getBitTemplate() );
279    
280     FchargedHad->push_back( (*jets)[i].chargedHadronEnergyFraction() );
281     FneutralHad->push_back( (*jets)[i].neutralHadronEnergyFraction() );
282     FchargedEm->push_back( (*jets)[i].chargedEmEnergyFraction() );
283     FneutralEm->push_back( (*jets)[i].neutralEmEnergyFraction() );
284     FchargedMu->push_back( (*jets)[i].chargedMuEnergyFraction() );
285     Ncharged->push_back( (unsigned) (*jets)[i].chargedMultiplicity() );
286     Nneutral->push_back( (unsigned) (*jets)[i].neutralMultiplicity() );
287     Nmuon->push_back( (unsigned) (*jets)[i].muonMultiplicity() );
288    
289     EchargedHad->push_back( (*jets)[i].chargedHadronEnergy() );
290     EneutralHad->push_back( (*jets)[i].neutralHadronEnergy() );
291     EchargedEM ->push_back( (*jets)[i].chargedEmEnergy() );
292     EneutralEM ->push_back( (*jets)[i].neutralEmEnergy() );
293     Ephoton ->push_back( (*jets)[i].photonEnergy() );
294     Eelectron ->push_back( (*jets)[i].electronEnergy() );
295     Emuon ->push_back( (*jets)[i].muonEnergy() );
296     EHFHad ->push_back( (*jets)[i].HFHadronEnergy() );
297     EHFEM ->push_back( (*jets)[i].HFEMEnergy() );
298    
299     NchargedHad->push_back( (unsigned) (*jets)[i].chargedHadronMultiplicity() );
300     NneutralHad->push_back( (unsigned) (*jets)[i].neutralHadronMultiplicity() );
301     Nphoton ->push_back( (unsigned) (*jets)[i].photonMultiplicity() );
302     Nelectron ->push_back( (unsigned) (*jets)[i].electronMultiplicity() );
303     NHFHad ->push_back( (unsigned) (*jets)[i].HFHadronMultiplicity() );
304     NHFEM ->push_back( (unsigned) (*jets)[i].HFEMMultiplicity() );
305    
306     pfjetidloose->push_back(pfLooseJetID( (*jets)[i], passLooseCuts ));
307     pfjetidtight->push_back(pfTightJetID( (*jets)[i], passTightCuts ));
308    
309     }
310    
311     evt.put(FchargedHad, Prefix + "FchargedHad" + Suffix);
312     evt.put(FneutralHad, Prefix + "FneutralHad" + Suffix);
313     evt.put(FchargedEm, Prefix + "FchargedEm" + Suffix);
314     evt.put(FneutralEm, Prefix + "FneutralEm" + Suffix);
315     evt.put(FchargedMu, Prefix + "FchargedMu" + Suffix);
316     evt.put(Ncharged, Prefix + "Ncharged" + Suffix);
317     evt.put(Nneutral, Prefix + "Nneutral" + Suffix);
318     evt.put(Nmuon, Prefix + "Nmuon" + Suffix);
319    
320     evt.put(EchargedHad, Prefix + "EchargedHad" + Suffix);
321     evt.put(EneutralHad, Prefix + "EneutralHad" + Suffix);
322     evt.put(EchargedEM , Prefix + "EchargedEM" + Suffix);
323     evt.put(EneutralEM , Prefix + "EneutralEM" + Suffix);
324     evt.put(Ephoton , Prefix + "Ephoton" + Suffix);
325     evt.put(Eelectron , Prefix + "Eelectron" + Suffix);
326     evt.put(Emuon , Prefix + "Emuon" + Suffix);
327     evt.put(EHFHad , Prefix + "EHFHad" + Suffix);
328     evt.put(EHFEM , Prefix + "EHFEM" + Suffix);
329    
330     evt.put(NchargedHad, Prefix + "NchargedHad" + Suffix);
331     evt.put(NneutralHad, Prefix + "NneutralHad" + Suffix);
332     evt.put(Nphoton , Prefix + "Nphoton" + Suffix);
333     evt.put(Nelectron , Prefix + "Nelectron" + Suffix);
334     evt.put(NHFHad , Prefix + "NHFHad" + Suffix);
335     evt.put(NHFEM , Prefix + "NHFEM" + Suffix);
336    
337     evt.put( pfjetidloose, Prefix + "PFJetIDloose" + Suffix );
338     evt.put( pfjetidtight, Prefix + "PFJetIDtight" + Suffix );
339    
340     }
341    
342     template<class T> void Tuple_Jet<T>::
343     initCalo() {
344     if( !( caloSpecific || jptSpecific ) ) return;
345     produces <std::vector<float> > ( Prefix + "EmEnergyFraction" + Suffix );
346     produces <std::vector<float> > ( Prefix + "EnergyFractionHadronic" + Suffix );
347     produces <std::vector<float> > ( Prefix + "TowersArea" + Suffix );
348     produces <std::vector<float> > ( Prefix + "MaxEInEmTowers" + Suffix );
349     produces <std::vector<float> > ( Prefix + "MaxEInHadTowers" + Suffix );
350     produces <std::vector<float> > ( Prefix + "HadEnergyInHB" + Suffix );
351     produces <std::vector<float> > ( Prefix + "HadEnergyInHE" + Suffix );
352     produces <std::vector<float> > ( Prefix + "HadEnergyInHO" + Suffix );
353     produces <std::vector<float> > ( Prefix + "HadEnergyInHF" + Suffix );
354     produces <std::vector<float> > ( Prefix + "EmEnergyInEB" + Suffix );
355     produces <std::vector<float> > ( Prefix + "EmEnergyInEE" + Suffix );
356     produces <std::vector<float> > ( Prefix + "EmEnergyInHF" + Suffix );
357     produces <std::vector<int> > ( Prefix + "N60Towers" + Suffix );
358     produces <std::vector<int> > ( Prefix + "N90Towers" + Suffix );
359    
360     }
361    
362     template<class T> void Tuple_Jet<T>::
363     produceCalo(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) {
364    
365     if( !( caloSpecific || jptSpecific ) ) return;
366     std::auto_ptr<std::vector<float> > emEnergyFraction ( new std::vector<float>() ) ;
367     std::auto_ptr<std::vector<float> > energyFractionHadronic ( new std::vector<float>() ) ;
368     std::auto_ptr<std::vector<float> > towersArea ( new std::vector<float>() ) ;
369     std::auto_ptr<std::vector<float> > maxEInEmTowers ( new std::vector<float>() ) ;
370     std::auto_ptr<std::vector<float> > maxEInHadTowers ( new std::vector<float>() ) ;
371     std::auto_ptr<std::vector<float> > hadEnergyInHB ( new std::vector<float>() ) ;
372     std::auto_ptr<std::vector<float> > hadEnergyInHE ( new std::vector<float>() ) ;
373     std::auto_ptr<std::vector<float> > hadEnergyInHO ( new std::vector<float>() ) ;
374     std::auto_ptr<std::vector<float> > hadEnergyInHF ( new std::vector<float>() ) ;
375     std::auto_ptr<std::vector<float> > emEnergyInEB ( new std::vector<float>() ) ;
376     std::auto_ptr<std::vector<float> > emEnergyInEE ( new std::vector<float>() ) ;
377     std::auto_ptr<std::vector<float> > emEnergyInHF ( new std::vector<float>() ) ;
378     std::auto_ptr<std::vector<int> > n60 ( new std::vector<int>() ) ;
379     std::auto_ptr<std::vector<int> > n90 ( new std::vector<int>() ) ;
380    
381     for( unsigned i=0; jets.isValid() && i<(*jets).size(); i++) {
382    
383     edm::RefToBase<T> jet_ref( jets, i );
384     const reco::CaloJet* jet = getJet( jet_ref );
385     if ( !jet ) continue;
386    
387     emEnergyFraction->push_back(jet->emEnergyFraction());
388     energyFractionHadronic->push_back(jet->energyFractionHadronic());
389     towersArea->push_back(jet->towersArea());
390     maxEInEmTowers->push_back(jet->maxEInEmTowers());
391     maxEInHadTowers->push_back(jet->maxEInHadTowers());
392     hadEnergyInHB->push_back(jet->hadEnergyInHB());
393     hadEnergyInHE->push_back(jet->hadEnergyInHE());
394     hadEnergyInHO->push_back(jet->hadEnergyInHO());
395     hadEnergyInHF->push_back(jet->hadEnergyInHF());
396     emEnergyInEB->push_back(jet->emEnergyInEB());
397     emEnergyInEE->push_back(jet->emEnergyInEE());
398     emEnergyInHF->push_back(jet->emEnergyInHF());
399     n60->push_back(jet->n60());
400     n90->push_back(jet->n90());
401     }
402    
403     evt.put( emEnergyFraction, Prefix + "EmEnergyFraction" + Suffix );
404     evt.put( energyFractionHadronic, Prefix + "EnergyFractionHadronic" + Suffix );
405     evt.put( towersArea, Prefix + "TowersArea" + Suffix );
406     evt.put( maxEInEmTowers, Prefix + "MaxEInEmTowers" + Suffix );
407     evt.put( maxEInHadTowers, Prefix + "MaxEInHadTowers" + Suffix );
408     evt.put( hadEnergyInHB, Prefix + "HadEnergyInHB" + Suffix );
409     evt.put( hadEnergyInHE, Prefix + "HadEnergyInHE" + Suffix );
410     evt.put( hadEnergyInHO, Prefix + "HadEnergyInHO" + Suffix );
411     evt.put( hadEnergyInHF, Prefix + "HadEnergyInHF" + Suffix );
412     evt.put( emEnergyInEB, Prefix + "EmEnergyInEB" + Suffix );
413     evt.put( emEnergyInEE, Prefix + "EmEnergyInEE" + Suffix );
414     evt.put( emEnergyInHF, Prefix + "EmEnergyInHF" + Suffix );
415     evt.put( n60, Prefix + "N60Towers" + Suffix );
416     evt.put( n90, Prefix + "N90Towers" + Suffix );
417    
418     }
419    
420    
421    
422     template<class T> void Tuple_Jet<T>::
423     initJetID() {
424     if( !( ( caloSpecific || jptSpecific ) && jetid ) ) return;
425     produces <std::vector<double> > ( Prefix + "JetIDFHPD" + Suffix );
426     produces <std::vector<double> > ( Prefix + "JetIDFRBX" + Suffix );
427     produces <std::vector<double> > ( Prefix + "JetIDFSubDet1" + Suffix );
428     produces <std::vector<double> > ( Prefix + "JetIDFSubDet2" + Suffix );
429     produces <std::vector<double> > ( Prefix + "JetIDFSubDet3" + Suffix );
430     produces <std::vector<double> > ( Prefix + "JetIDFSubDet4" + Suffix );
431     produces <std::vector<double> > ( Prefix + "JetIDResEMF" + Suffix );
432     produces <std::vector<int> > ( Prefix + "JetIDN90Hits" + Suffix );
433     produces <std::vector<int> > ( Prefix + "JetIDminimal" + Suffix );
434     produces <std::vector<int> > ( Prefix + "JetIDloose" + Suffix );
435     produces <std::vector<int> > ( Prefix + "JetIDtight" + Suffix );
436     produces <std::vector<int> > ( Prefix + "JetIDnECALTowers" + Suffix );
437     produces <std::vector<int> > ( Prefix + "JetIDnHCALTowers" + Suffix );
438     }
439    
440     template<class T> void Tuple_Jet<T>::
441     produceJetID(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) {
442     if( !( ( caloSpecific || jptSpecific ) && jetid ) ) return;
443     std::auto_ptr<std::vector<double> > fHPD ( new std::vector<double>() ) ;
444     std::auto_ptr<std::vector<double> > fRBX ( new std::vector<double>() ) ;
445     std::auto_ptr<std::vector<double> > fSubDet1 ( new std::vector<double>() ) ;
446     std::auto_ptr<std::vector<double> > fSubDet2 ( new std::vector<double>() ) ;
447     std::auto_ptr<std::vector<double> > fSubDet3 ( new std::vector<double>() ) ;
448     std::auto_ptr<std::vector<double> > fSubDet4 ( new std::vector<double>() ) ;
449     std::auto_ptr<std::vector<double> > resEMF ( new std::vector<double>() ) ;
450     std::auto_ptr<std::vector<int> > n90Hits ( new std::vector<int>() ) ;
451     std::auto_ptr<std::vector<int> > jetidminimal ( new std::vector<int>() ) ;
452     std::auto_ptr<std::vector<int> > jetidloose ( new std::vector<int>() ) ;
453     std::auto_ptr<std::vector<int> > jetidtight ( new std::vector<int>() ) ;
454     std::auto_ptr<std::vector<int> > NECALTowers ( new std::vector<int>() ) ;
455     std::auto_ptr<std::vector<int> > NHCALTowers ( new std::vector<int>() ) ;
456    
457     JetIDSelectionFunctor
458     minimalJetID(JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::MINIMAL),
459     looseJetID(JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::LOOSE),
460     tightJetID(JetIDSelectionFunctor::PURE09, JetIDSelectionFunctor::TIGHT);
461     for( unsigned i=0; jets.isValid() && i<(*jets).size(); i++ ) {
462    
463     edm::RefToBase<T> jet_ref( jets, i );
464     const reco::CaloJet* jet = getJet( jet_ref );
465     if ( !jet ) continue;
466    
467     pat::strbitset
468     passMinimalCuts( minimalJetID.getBitTemplate() ),
469     passLooseCuts( looseJetID .getBitTemplate() ),
470     passTightCuts( tightJetID .getBitTemplate() );
471    
472     fHPD->push_back((*jets)[i].jetID().fHPD);
473     fRBX->push_back((*jets)[i].jetID().fRBX);
474     fSubDet1->push_back((*jets)[i].jetID().fSubDetector1);
475     fSubDet2->push_back((*jets)[i].jetID().fSubDetector2);
476     fSubDet3->push_back((*jets)[i].jetID().fSubDetector3);
477     fSubDet4->push_back((*jets)[i].jetID().fSubDetector4);
478     resEMF->push_back((*jets)[i].jetID().restrictedEMF);
479     n90Hits->push_back((int)((*jets)[i].jetID().n90Hits));
480     jetidminimal->push_back(minimalJetID(*jet, (*jets)[i].jetID(), passMinimalCuts));
481     jetidloose->push_back(looseJetID(*jet, (*jets)[i].jetID(), passLooseCuts ));
482     jetidtight->push_back(tightJetID(*jet, (*jets)[i].jetID(), passTightCuts ));
483     NECALTowers->push_back((*jets)[i].jetID().nECALTowers);
484     NHCALTowers->push_back((*jets)[i].jetID().nHCALTowers);
485     }
486    
487     evt.put( fHPD, Prefix + "JetIDFHPD" + Suffix );
488     evt.put( fRBX, Prefix + "JetIDFRBX" + Suffix );
489     evt.put( fSubDet1, Prefix + "JetIDFSubDet1" + Suffix );
490     evt.put( fSubDet2, Prefix + "JetIDFSubDet2" + Suffix );
491     evt.put( fSubDet3, Prefix + "JetIDFSubDet3" + Suffix );
492     evt.put( fSubDet4, Prefix + "JetIDFSubDet4" + Suffix );
493     evt.put( resEMF, Prefix + "JetIDResEMF" + Suffix );
494     evt.put( n90Hits, Prefix + "JetIDN90Hits" + Suffix );
495     evt.put( jetidminimal, Prefix + "JetIDminimal" + Suffix );
496     evt.put( jetidloose, Prefix + "JetIDloose" + Suffix );
497     evt.put( jetidtight, Prefix + "JetIDtight" + Suffix );
498     evt.put( NECALTowers, Prefix + "JetIDnECALTowers" + Suffix );
499     evt.put( NHCALTowers, Prefix + "JetIDnHCALTowers" + Suffix );
500     }
501    
502    
503    
504     template<class T> void Tuple_Jet<T>::
505     initMPT() { if(!mpt) return;
506     const std::vector<int> qualities(config.getParameter<std::vector<int> >("TrackQualities"));
507     typedef reco::TrackBase::Vector Vector;
508     for(unsigned j=0; j < qualities.size(); ++j) {
509     std::string name = (qualities[j]<0 ? "All" : reco::Track::qualityNames[qualities[j]]) + "Tracks" + Suffix;
510     name[0] = toupper((unsigned char) name[0]);
511     for(unsigned v=VINIT; v<VSIZE; v++) {
512     std::string name2 = "with" + vertex_names[v] + name;
513     produces <std::vector<unsigned> > (Prefix + "Count" + name2);
514     produces <std::vector<Vector> > (Prefix + "SumP3" + name2);
515     }
516     }
517     }
518    
519     template<class T> void Tuple_Jet<T>::
520     produceMPT(edm::Event& evt, const edm::Handle<edm::View<T> >& jets) { if(!mpt) return;
521     typedef reco::TrackBase::Vector Vector;
522     const std::vector<int> qualities(config.getParameter<std::vector<int> >("TrackQualities"));
523     const double maxD0(config.getParameter<double>("MaxD0Trk"));
524     edm::Handle<reco::VertexCollection> vertices;
525     evt.getByLabel(config.getParameter<edm::InputTag>("PrimaryVertexTag"), vertices);
526     const reco::Vertex& PrimaryVertex = vertices->front();
527    
528     std::vector< std::vector< std::vector<unsigned> > > ntracks( VSIZE, std::vector< std::vector<unsigned> > (qualities.size(), std::vector<unsigned>(jets->size(),0) )) ;
529     std::vector< std::vector< std::vector <Vector> > > sump3( VSIZE, std::vector< std::vector <Vector> > (qualities.size(), std::vector<Vector>(jets->size(), Vector()) )) ;
530    
531     for( unsigned i=0; jets.isValid() && i< jets->size(); ++i ) {
532     for (reco::TrackRefVector::iterator trk = (*jets)[i].associatedTracks().begin(); trk != (*jets)[i].associatedTracks().end(); ++trk) {
533     if ( fabs((*trk)->dxy(PrimaryVertex.position())) < maxD0 ) {
534    
535     std::vector<double> dist; std::transform(vertices->begin(), vertices->end(), back_inserter(dist), dsz(*(*trk)) );
536     std::vector<double>::const_iterator dmin = min_element(dist.begin(), dist.end());
537     unsigned v = (dmin==dist.begin()) ? V_PRIMARY : V_PILEUP;
538    
539     for(unsigned q=0; q<qualities.size(); ++q) {
540     if((*trk)->quality(reco::TrackBase::TrackQuality(qualities[q]))) {
541     ntracks[v][q][i]++;
542     sump3[v][q][i] += (*trk)->momentum();
543     }
544     }
545     }
546     }
547     }
548    
549     for(unsigned q=0; q<qualities.size(); ++q) {
550     std::string name = (qualities[q]<0 ? "All" : reco::Track::qualityNames[qualities[q]]) + "Tracks" + Suffix;
551     name[0] = toupper((unsigned char) name[0]);
552     for(unsigned v=VINIT; v<VSIZE; v++) {
553     std::string name2 = "with" + vertex_names[v] + name;
554     evt.put( std::auto_ptr<std::vector<unsigned> > ( new std::vector<unsigned>( ntracks[v][q].begin(), ntracks[v][q].end())), Prefix + "Count" + name2);
555     evt.put( std::auto_ptr<std::vector<Vector> > ( new std::vector<Vector>( sump3[v][q].begin(), sump3[v][q].end())), Prefix + "SumP3" + name2);
556     }
557     }
558     }
559    
560    
561     template<class T> void Tuple_Jet<T>::
562     initBJetTag(){
563     //btag discriminators
564     produces <std::vector<float> > (Prefix + "TrkCountingHighEffBJetTags" + Suffix);
565     produces <std::vector<float> > (Prefix + "TrkCountingHighPurBJetTags" + Suffix);
566     produces <std::vector<float> > (Prefix + "SimpleSecondaryVertexHighEffBJetTags" + Suffix);
567     produces <std::vector<float> > (Prefix + "SimpleSecondaryVertexHighPurBJetTags" + Suffix);
568     produces <std::vector<float> > (Prefix + "CombinedSecondaryVertexBJetTags" + Suffix);
569     produces <std::vector<float> > (Prefix + "CombinedSecondaryVertexMVABJetTags" + Suffix);
570     produces <std::vector<float> > (Prefix + "JetProbabilityBJetTags" + Suffix);
571     produces <std::vector<float> > (Prefix + "JetBProbabilityBJetTags" + Suffix);
572     produces <std::vector<float> > (Prefix + "SoftElectronByIP3dBJetTags" + Suffix);
573     produces <std::vector<float> > (Prefix + "SoftElectronByPtBJetTags" + Suffix);
574     produces <std::vector<float> > (Prefix + "SoftMuonBJetTags" + Suffix);
575     produces <std::vector<float> > (Prefix + "SoftMuonByIP3dBJetTags" + Suffix);
576     produces <std::vector<float> > (Prefix + "SoftMuonByPtBJetTags" + Suffix);
577     produces <std::vector<int> > (Prefix + "genJetFlavour" + Suffix);
578    
579     }
580    
581     template<class T> void Tuple_Jet<T>::
582     produceBJetTag(edm::Event& evt, const edm::Handle<edm::View<T> >& jets){
583    
584     //btag discriminators
585     std::auto_ptr<std::vector<float> > TrkCountHighEffBJetTags (new std::vector<float>() );
586     std::auto_ptr<std::vector<float> > TrkCountHighPurBJetTags (new std::vector<float>() );
587     std::auto_ptr<std::vector<float> > SimpSecVertHEBJetTags (new std::vector<float>() );
588     std::auto_ptr<std::vector<float> > SimpSecVertHPBJetTags (new std::vector<float>() );
589     std::auto_ptr<std::vector<float> > CombSecVertBJetTags (new std::vector<float>() );
590     std::auto_ptr<std::vector<float> > CombSecVertMVABJetTags (new std::vector<float>() );
591     std::auto_ptr<std::vector<float> > jetProbBJetTags (new std::vector<float>() );
592     std::auto_ptr<std::vector<float> > jetBProbBJetTags (new std::vector<float>() );
593     std::auto_ptr<std::vector<float> > SoftElecIPBJetTags (new std::vector<float>() );
594     std::auto_ptr<std::vector<float> > SoftElecPtBJetTags (new std::vector<float>() );
595     std::auto_ptr<std::vector<float> > SoftMuonBJetTags (new std::vector<float>() );
596     std::auto_ptr<std::vector<float> > SoftMuonIPBJetTags (new std::vector<float>() );
597     std::auto_ptr<std::vector<float> > SoftMuonPtBJetTags (new std::vector<float>() );
598     std::auto_ptr<std::vector<int> > JetFlavour(new std::vector<int>() );
599    
600     if(jets.isValid()){
601     for (unsigned i=0; i<(*jets).size(); i++) {
602     //btag discriminators
603     TrkCountHighEffBJetTags->push_back((*jets)[i].bDiscriminator("trackCountingHighEffBJetTags"));
604     TrkCountHighPurBJetTags->push_back((*jets)[i].bDiscriminator("trackCountingHighPurBJetTags"));
605     SimpSecVertHEBJetTags->push_back((*jets)[i].bDiscriminator("simpleSecondaryVertexHighEffBJetTags"));
606     SimpSecVertHPBJetTags->push_back((*jets)[i].bDiscriminator("simpleSecondaryVertexHighPurBJetTags"));
607     CombSecVertBJetTags->push_back((*jets)[i].bDiscriminator("combinedSecondaryVertexBJetTags"));
608     CombSecVertMVABJetTags->push_back((*jets)[i].bDiscriminator("combinedSecondaryVertexMVABJetTags"));
609     jetProbBJetTags->push_back((*jets)[i].bDiscriminator("jetProbabilityBJetTags"));
610     jetBProbBJetTags->push_back((*jets)[i].bDiscriminator("jetBProbabilityBJetTags"));
611     SoftElecIPBJetTags->push_back((*jets)[i].bDiscriminator("softElectronByIP3dBJetTags"));
612     SoftElecPtBJetTags->push_back((*jets)[i].bDiscriminator("softElectronByPtBJetTags"));
613     SoftMuonBJetTags->push_back((*jets)[i].bDiscriminator("softMuonBJetTags"));
614     SoftMuonIPBJetTags->push_back((*jets)[i].bDiscriminator("softMuonByIP3dBJetTags"));
615     SoftMuonPtBJetTags->push_back((*jets)[i].bDiscriminator("softMuonByPtBJetTags"));
616     JetFlavour->push_back((*jets)[i].partonFlavour());
617     }
618     }
619    
620    
621     //btag discriminators
622     evt.put(TrkCountHighEffBJetTags, Prefix + "TrkCountingHighEffBJetTags" + Suffix);
623     evt.put(TrkCountHighPurBJetTags, Prefix + "TrkCountingHighPurBJetTags" + Suffix);
624     evt.put(SimpSecVertHEBJetTags, Prefix + "SimpleSecondaryVertexHighEffBJetTags" + Suffix);
625     evt.put(SimpSecVertHPBJetTags, Prefix + "SimpleSecondaryVertexHighPurBJetTags" + Suffix);
626     evt.put(CombSecVertBJetTags, Prefix + "CombinedSecondaryVertexBJetTags" + Suffix);
627     evt.put(CombSecVertMVABJetTags, Prefix + "CombinedSecondaryVertexMVABJetTags" + Suffix);
628     evt.put(jetProbBJetTags, Prefix + "JetProbabilityBJetTags" + Suffix);
629     evt.put(jetBProbBJetTags, Prefix + "JetBProbabilityBJetTags" + Suffix);
630     evt.put(SoftElecIPBJetTags, Prefix + "SoftElectronByIP3dBJetTags" + Suffix);
631     evt.put(SoftElecPtBJetTags, Prefix + "SoftElectronByPtBJetTags" + Suffix);
632     evt.put(SoftMuonBJetTags, Prefix + "SoftMuonBJetTags" + Suffix);
633     evt.put(SoftMuonIPBJetTags, Prefix + "SoftMuonByIP3dBJetTags" + Suffix);
634     evt.put(SoftMuonPtBJetTags, Prefix + "SoftMuonByPtBJetTags" + Suffix);
635     evt.put(JetFlavour, Prefix + "genJetFlavour" + Suffix);
636     }
637    
638    
639    
640     template<class T> void Tuple_Jet<T>::
641     initGenJetMatch() { if(gen) produces <std::vector<int> > (Prefix + "GenJetMatchIndex" + Suffix);}
642    
643     template<class T> void Tuple_Jet<T>::
644     produceGenJetMatch(edm::Event& evt, const edm::Handle<edm::View<T> >& jets){
645     if(!gen) return;
646     edm::Handle<edm::View<reco::GenJet> > genjets;
647     evt.getByLabel(genJetsInputTag, genjets);
648     std::auto_ptr<std::vector<int> > genjetMatchIndex( new std::vector<int>() );
649     if(jets.isValid() && genjets.isValid())
650     for (typename edm::View<T>::const_iterator jet = jets->begin(); jet!=jets->end(); ++jet )
651     genjetMatchIndex->push_back( indexOfMatch( jet->genJet(), genjets->begin(), genjets->end()) );
652     evt.put(genjetMatchIndex, Prefix + "GenJetMatchIndex" + Suffix);
653     }
654    
655     template<class T> template<class I> int Tuple_Jet<T>::
656     indexOfMatch( const reco::GenJet* genjet, const I begin, const I end) {
657     for(I it=begin; it!=end; ++it) if ( genjet && genjet->p4() == it->p4() ) return it-begin; //p4 comparisons
658     return -1;
659     }
660    
661    
662    
663     template<class T> const reco::CaloJet* Tuple_Jet<T>::getJet( const edm::RefToBase<T>& jet_ref ) {
664     std::cout << "Don't use this!!!" << std::endl;
665     return 0;
666     }
667    
668     template<> const reco::CaloJet* Tuple_Jet<reco::CaloJet>::getJet( const edm::RefToBase<reco::CaloJet>& jet_ref ) {
669     return jet_ref.get();
670     }
671    
672     template<> const reco::CaloJet* Tuple_Jet<reco::JPTJet>::getJet( const edm::RefToBase<reco::JPTJet>& jet_ref ) {
673     const reco::JPTJet* tmp = jet_ref.get();
674     if ( tmp ) { return static_cast<const reco::CaloJet*>( tmp->getCaloJetRef().get() ); }
675     else { return 0; }
676     }
677    
678     template<> const reco::CaloJet* Tuple_Jet<pat::Jet>::getJet( const edm::RefToBase<pat::Jet>& jet_ref ) {
679     if ( caloSpecific ) { return static_cast<const reco::CaloJet*>( jet_ref->originalObjectRef().get() ); }
680     else if ( jptSpecific ) {
681     const reco::JPTJet* tmp = static_cast<const reco::JPTJet*>( jet_ref->originalObjectRef().get() );
682     if ( tmp ) { return static_cast<const reco::CaloJet*>( tmp->getCaloJetRef().get() ); }
683     }
684     return 0;
685     }
686    
687     #endif
688    
689