ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/JetAnalysis/src/JetAlgorithmAnalyzer.cc
Revision: 1.34
Committed: Fri Jan 25 21:57:26 2013 UTC (12 years, 3 months ago) by yilmaz
Content type: text/plain
Branch: MAIN
Changes since 1.33: +4 -4 lines
Log Message:
jet id

File Contents

# User Rev Content
1 yilmaz 1.1 #ifndef RecoJets_JetProducers_plugins_JetAlgorithmAnalyzer_h
2     #define RecoJets_JetProducers_plugins_JetAlgorithmAnalyzer_h
3    
4 yilmaz 1.3 //#include "RecoJets/JetProducers/plugins/VirtualJetProducer.h"
5     #include "MyVirtualJetProducer.h"
6 yilmaz 1.1
7     #include "FWCore/ServiceRegistry/interface/Service.h"
8     #include "CommonTools/UtilAlgos/interface/TFileService.h"
9 yilmaz 1.23 #include "DataFormats/PatCandidates/interface/Jet.h"
10 yilmaz 1.32 #include "DataFormats/HeavyIonEvent/interface/CentralityProvider.h"
11 yilmaz 1.13 #include "DataFormats/HeavyIonEvent/interface/EvtPlane.h"
12 yilmaz 1.1 #include "TNtuple.h"
13 yilmaz 1.2 #include "TH2D.h"
14     #include <vector>
15 yilmaz 1.13 #include <iostream>
16     using namespace std;
17 yilmaz 1.2
18     static const int nSteps = 8;
19     static const double PI = 3.141592653589;
20 yilmaz 1.1
21 yilmaz 1.3 class JetAlgorithmAnalyzer : public MyVirtualJetProducer
22 yilmaz 1.1 {
23    
24     public:
25     //
26     // construction/destruction
27     //
28     explicit JetAlgorithmAnalyzer(const edm::ParameterSet& iConfig);
29     virtual ~JetAlgorithmAnalyzer();
30    
31     protected:
32    
33     //
34     // member functions
35     //
36    
37     virtual void produce( edm::Event& iEvent, const edm::EventSetup& iSetup );
38     virtual void runAlgorithm( edm::Event& iEvent, const edm::EventSetup& iSetup );
39     virtual void output( edm::Event & iEvent, edm::EventSetup const& iSetup );
40     template< typename T >
41     void writeBkgJets( edm::Event & iEvent, edm::EventSetup const& iSetup );
42    
43 yilmaz 1.2 void fillNtuple(int output, const std::vector<fastjet::PseudoJet>& jets, int step);
44 yilmaz 1.1 void fillTowerNtuple(const std::vector<fastjet::PseudoJet>& jets, int step);
45     void fillJetNtuple(const std::vector<fastjet::PseudoJet>& jets, int step);
46     void fillBkgNtuple(const PileUpSubtractor* subtractor, int step);
47    
48     private:
49    
50     // trackjet clustering parameters
51     bool useOnlyVertexTracks_;
52     bool useOnlyOnePV_;
53 yilmaz 1.11 float dzTrVtxMax_;
54    
55 yilmaz 1.18 int evtPlaneIndex_;
56 yilmaz 1.11 double phi0_;
57     int nFill_;
58 yilmaz 1.1 float etaMax_;
59     int iev_;
60 yilmaz 1.11 bool avoidNegative_;
61 nart 1.22 double cone_;
62 yilmaz 1.11
63 yilmaz 1.1 bool doAnalysis_;
64    
65 yilmaz 1.25 bool backToBack_;
66 yilmaz 1.2 bool doMC_;
67 yilmaz 1.13 bool doRecoEvtPlane_;
68 yilmaz 1.12 bool doRandomCones_;
69     bool doFullCone_;
70    
71 yilmaz 1.14 bool sumRecHits_;
72    
73 yilmaz 1.12 double hf_;
74     double sumET_;
75     int bin_;
76    
77 yilmaz 1.3 int centBin_;
78 yilmaz 1.11 edm::InputTag centTag_;
79 yilmaz 1.13 edm::InputTag epTag_;
80 nart 1.22 edm::InputTag PatJetSrc_;
81 yilmaz 1.3 const CentralityBins* cbins_;
82    
83 yilmaz 1.6 TNtuple* ntTowers;
84     TNtuple* ntJetTowers;
85    
86 yilmaz 1.2 TNtuple* ntTowersFromEvtPlane;
87    
88 yilmaz 1.1 TNtuple* ntJets;
89     TNtuple* ntPU;
90     TNtuple* ntRandom;
91 yilmaz 1.30 TNtuple* ntRandom0;
92     TNtuple* ntRandom1;
93 yilmaz 1.1
94 yilmaz 1.23 edm::Handle<pat::JetCollection> patjets;
95    
96 yilmaz 1.2 std::vector<TH2D*> hTowers;
97 yilmaz 1.6 std::vector<TH2D*> hJetTowers;
98    
99 yilmaz 1.2 std::vector<TH2D*> hTowersFromEvtPlane;
100    
101     std::vector<TH2D*> hJets;
102 yilmaz 1.3 std::vector<TH1D*> hPU;
103     std::vector<TH1D*> hMean;
104     std::vector<TH1D*> hSigma;
105    
106 yilmaz 1.2 std::vector<TH2D*> hRandom;
107    
108 yilmaz 1.3
109 yilmaz 1.11 TH2D* hPTieta;
110     TH1D* hMeanieta;
111     TH1D* hRMSieta;
112     TH1D* hPUieta;
113 yilmaz 1.3
114 yilmaz 1.1 const CaloGeometry *geo;
115     edm::Service<TFileService> f;
116 yilmaz 1.32 CentralityProvider * centrality_;
117 yilmaz 1.1 };
118    
119    
120     #endif
121     ////////////////////////////////////////////////////////////////////////////////
122     //
123     // JetAlgorithmAnalyzer
124     // ------------------
125     //
126     // 04/21/2009 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
127     ////////////////////////////////////////////////////////////////////////////////
128    
129     //#include "RecoJets/JetProducers/plugins/JetAlgorithmAnalyzer.h"
130     //#include "JetAlgorithmAnalyzer.h"
131    
132     #include "RecoJets/JetProducers/interface/JetSpecific.h"
133    
134     #include "FWCore/Framework/interface/Event.h"
135     #include "FWCore/Framework/interface/EventSetup.h"
136     #include "FWCore/Framework/interface/ESHandle.h"
137 frankma 1.27 #include "FWCore/Utilities/interface/Exception.h"
138 yilmaz 1.1 #include "FWCore/MessageLogger/interface/MessageLogger.h"
139     #include "FWCore/Framework/interface/MakerMacros.h"
140     #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
141     #include "FWCore/ServiceRegistry/interface/Service.h"
142    
143     #include "DataFormats/Common/interface/Handle.h"
144     #include "DataFormats/VertexReco/interface/Vertex.h"
145     #include "DataFormats/VertexReco/interface/VertexFwd.h"
146     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
147     #include "DataFormats/JetReco/interface/GenJetCollection.h"
148     #include "DataFormats/JetReco/interface/PFJetCollection.h"
149     #include "DataFormats/JetReco/interface/BasicJetCollection.h"
150     #include "DataFormats/Candidate/interface/CandidateFwd.h"
151     #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
152     #include "DataFormats/Candidate/interface/LeafCandidate.h"
153     #include "DataFormats/Math/interface/deltaR.h"
154    
155 yilmaz 1.2 #include "SimDataFormats/HiGenData/interface/GenHIEvent.h"
156    
157 yilmaz 1.1 #include "DataFormats/CaloTowers/interface/CaloTower.h"
158     #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
159     #include "Geometry/Records/interface/CaloGeometryRecord.h"
160    
161     #include "fastjet/SISConePlugin.hh"
162     #include "fastjet/CMSIterativeConePlugin.hh"
163     #include "fastjet/ATLASConePlugin.hh"
164     #include "fastjet/CDFMidPointPlugin.hh"
165    
166     #include "CLHEP/Random/RandomEngine.h"
167    
168     #include <iostream>
169     #include <memory>
170     #include <algorithm>
171     #include <limits>
172     #include <cmath>
173    
174     using namespace std;
175     using namespace edm;
176    
177     static const double pi = 3.14159265358979;
178    
179     CLHEP::HepRandomEngine* randomEngine;
180    
181    
182     ////////////////////////////////////////////////////////////////////////////////
183     // construction / destruction
184     ////////////////////////////////////////////////////////////////////////////////
185    
186     //______________________________________________________________________________
187     JetAlgorithmAnalyzer::JetAlgorithmAnalyzer(const edm::ParameterSet& iConfig)
188 yilmaz 1.3 : MyVirtualJetProducer( iConfig ),
189 yilmaz 1.2 phi0_(0),
190 yilmaz 1.25 nFill_(5),
191 yilmaz 1.1 etaMax_(3),
192     iev_(0),
193 yilmaz 1.3 geo(0),
194 yilmaz 1.32 centrality_(0),
195 nart 1.22 cone_(1)
196 yilmaz 1.1 {
197 yilmaz 1.19
198 yilmaz 1.32 doAreaFastjet_ = false;
199     doRhoFastjet_ = false;
200    
201 yilmaz 1.19 puSubtractorName_ = iConfig.getParameter<string> ("subtractorName");
202     subtractor_ = boost::shared_ptr<PileUpSubtractor>(PileUpSubtractorFactory::get()->create( puSubtractorName_, iConfig));
203    
204 yilmaz 1.1 edm::Service<RandomNumberGenerator> rng;
205     randomEngine = &(rng->getEngine());
206    
207     doAnalysis_ = iConfig.getUntrackedParameter<bool>("doAnalysis",true);
208 yilmaz 1.12 doRandomCones_ = iConfig.getUntrackedParameter<bool>("doRandomCones",true);
209 yilmaz 1.25
210     backToBack_ = iConfig.getUntrackedParameter<bool>("doBackToBack",false);
211     if(backToBack_) nFill_ = 2;
212    
213 yilmaz 1.12 doFullCone_ = iConfig.getUntrackedParameter<bool>("doFullCone",true);
214    
215 yilmaz 1.2 doMC_ = iConfig.getUntrackedParameter<bool>("doMC",true);
216 yilmaz 1.13 doRecoEvtPlane_ = iConfig.getUntrackedParameter<bool>("doRecoEvtPlane",true);
217 yilmaz 1.18 evtPlaneIndex_ = iConfig.getUntrackedParameter<int>("evtPlaneIndex",31);
218 yilmaz 1.13
219 yilmaz 1.14 sumRecHits_ = iConfig.getParameter<bool>("sumRecHits");
220    
221 yilmaz 1.11 centTag_ = iConfig.getParameter<InputTag>("centralityTag");
222 yilmaz 1.13 epTag_ = iConfig.getParameter<InputTag>("evtPlaneTag");
223 nart 1.22 PatJetSrc_ = iConfig.getUntrackedParameter<edm::InputTag>("patJetSrc",edm::InputTag("icPu5patJets"));
224 yilmaz 1.3
225     if(doAnalysis_) centBin_ = iConfig.getUntrackedParameter<int>("centrality",0);
226    
227 yilmaz 1.1 avoidNegative_ = iConfig.getParameter<bool>("avoidNegative");
228    
229     if ( iConfig.exists("UseOnlyVertexTracks") )
230     useOnlyVertexTracks_ = iConfig.getParameter<bool>("UseOnlyVertexTracks");
231     else
232     useOnlyVertexTracks_ = false;
233    
234     if ( iConfig.exists("UseOnlyOnePV") )
235     useOnlyOnePV_ = iConfig.getParameter<bool>("UseOnlyOnePV");
236     else
237     useOnlyOnePV_ = false;
238    
239     if ( iConfig.exists("DrTrVtxMax") )
240     dzTrVtxMax_ = iConfig.getParameter<double>("DzTrVtxMax");
241     else
242     dzTrVtxMax_ = false;
243    
244 yilmaz 1.3 produces<reco::CaloJetCollection>("randomCones");
245 yilmaz 1.1 produces<std::vector<bool> >("directions");
246    
247 yilmaz 1.33 if(backToBack_){
248     ntRandom0 = f->make<TNtuple>("ntRandom0","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
249     ntRandom1 = f->make<TNtuple>("ntRandom1","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
250     }else{
251     ntRandom = f->make<TNtuple>("ntRandom","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
252     }
253    
254 yilmaz 1.1 if(doAnalysis_){
255     ntTowers = f->make<TNtuple>("ntTowers","Algorithm Analysis Towers","eta:phi:et:step:event");
256 yilmaz 1.3 ntTowersFromEvtPlane = f->make<TNtuple>("ntTowersFromEvtPlane","Algorithm Analysis Towers","eta:phi:et:step:event");
257 yilmaz 1.6 ntJetTowers = f->make<TNtuple>("ntTowers","Algorithm Analysis Towers","eta:phi:et:step:event");
258 yilmaz 1.3
259 yilmaz 1.1 ntJets = f->make<TNtuple>("ntJets","Algorithm Analysis Jets","eta:phi:et:step:event");
260     ntPU = f->make<TNtuple>("ntPU","Algorithm Analysis Background","eta:mean:sigma:step:event");
261 yilmaz 1.5 ntuple = f->make<TNtuple>("nt","debug","ieta:eta:iphi:phi:pt:em:had");
262    
263 yilmaz 1.11 hPTieta = f->make<TH2D>("hPTieta","hPTieta",23,-11.5,11.5,200,0,10);
264     hRMSieta = f->make<TH1D>("hRMSieta","hPTieta",23,-11.5,11.5);
265     hMeanieta = f->make<TH1D>("hMeanieta","hPTieta",23,-11.5,11.5);
266     hPUieta = f->make<TH1D>("hPUieta","hPTieta",23,-11.5,11.5);
267    
268 yilmaz 1.2 for(int i = 0; i < nSteps; ++i){
269 yilmaz 1.3 hTowers.push_back(f->make<TH2D>(Form("hTowers_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
270     hJets.push_back(f->make<TH2D>(Form("hJets_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
271     hTowersFromEvtPlane.push_back(f->make<TH2D>(Form("hTowersFromEvtPlane_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
272    
273 yilmaz 1.6 hJetTowers.push_back(f->make<TH2D>(Form("hJetTowers_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
274    
275 yilmaz 1.3 hPU.push_back(f->make<TH1D>(Form("hPU_step%d",i),"",200,-5.5,5.5));
276     hMean.push_back(f->make<TH1D>(Form("hMean_step%d",i),"",200,-5.5,5.5));
277     hSigma.push_back(f->make<TH1D>(Form("hSigma_step%d",i),"",200,-5.5,5.5));
278    
279 yilmaz 1.2 }
280    
281 yilmaz 1.1 }
282    
283     }
284    
285    
286     //______________________________________________________________________________
287     JetAlgorithmAnalyzer::~JetAlgorithmAnalyzer()
288     {
289     }
290    
291 yilmaz 1.2 void JetAlgorithmAnalyzer::fillNtuple(int output, const std::vector<fastjet::PseudoJet>& jets, int step){
292 yilmaz 1.1 if(!doAnalysis_) return;
293    
294     TNtuple* nt;
295 yilmaz 1.2 TH2D* h;
296    
297     if(output == 1){
298     nt = ntJets;
299     h = hJets[step];
300     }
301     if(output == 0){
302     nt = ntTowers;
303     h = hTowers[step];
304     }
305     if(output == 2){
306     nt = ntTowersFromEvtPlane;
307     h = hTowersFromEvtPlane[step];
308     }
309 yilmaz 1.6 if(output == 3){
310     nt = ntJetTowers;
311     h = hJetTowers[step];
312     }
313 yilmaz 1.1
314 yilmaz 1.29 bool printDebug = 0;
315    
316 yilmaz 1.4 double totet = 0;
317     int ntow = 0;
318 yilmaz 1.5 int nj = jets.size();
319    
320 yilmaz 1.29 if(printDebug){
321 yilmaz 1.34 // cout<<"step : "<<step<<endl;
322     // cout<<"Size of input : "<<nj<<endl;
323 yilmaz 1.29 }
324 yilmaz 1.1 for(unsigned int i = 0; i < jets.size(); ++i){
325 yilmaz 1.4 const fastjet::PseudoJet& jet = jets[i];
326    
327     double pt = jet.perp();
328 yilmaz 1.11 int ieta = -99;
329 yilmaz 1.4 if(output != 1){
330     reco::CandidatePtr const & itow = inputs_[ jet.user_index() ];
331     pt = itow->et();
332 yilmaz 1.11 ieta = subtractor_->ieta(itow);
333     }
334    
335     if(output == 0 && step == 6){
336     hPTieta->Fill(ieta,pt);
337 yilmaz 1.4 }
338    
339     double phi = jet.phi();
340     if(output == 2){
341     phi = phi - phi0_;
342     if(phi < 0) phi += 2*PI;
343     if(phi > 2*PI) phi -= 2*PI;
344     }
345    
346 yilmaz 1.11 double eta = jet.eta();
347 yilmaz 1.4 if(eta > 0 && eta < 0.08){
348     // if(fabs(eta) < 1.){
349     totet += pt;
350     ntow++;
351     }
352    
353     nt->Fill(jet.eta(),phi,pt,step,iev_);
354     h->Fill(jet.eta(),phi,pt);
355     }
356 yilmaz 1.34 if(printDebug && 0){
357 yilmaz 1.4 cout<<"-----------------------------"<<endl;
358     cout<<"STEP = "<<step<<endl;
359     cout<<"Total ET = "<<totet<<endl;
360     cout<<"Towers counted = "<<ntow<<endl;
361     cout<<"Average tower ET = "<<totet/ntow<<endl;
362     cout<<"-----------------------------"<<endl;
363 yilmaz 1.29 }
364 yilmaz 1.1 }
365    
366    
367     void JetAlgorithmAnalyzer::fillTowerNtuple(const std::vector<fastjet::PseudoJet>& jets, int step){
368     fillNtuple(0,jets,step);
369 yilmaz 1.2 fillNtuple(2,jets,step);
370 yilmaz 1.1 }
371    
372     void JetAlgorithmAnalyzer::fillJetNtuple(const std::vector<fastjet::PseudoJet>& jets, int step){
373     fillNtuple(1,jets,step);
374 yilmaz 1.6
375     for(unsigned int i = 0; i < jets.size(); ++i){
376     const fastjet::PseudoJet& jet = jets[i];
377     std::vector<fastjet::PseudoJet> fjConstituents = sorted_by_pt(fjClusterSeq_->constituents(jet));
378 yilmaz 1.8
379    
380    
381 yilmaz 1.6
382     fillNtuple(3,fjConstituents,step);
383     }
384 yilmaz 1.1 }
385    
386     void JetAlgorithmAnalyzer::fillBkgNtuple(const PileUpSubtractor* subtractor, int step){
387     if(!doAnalysis_) return;
388     CaloTowerCollection col;
389 yilmaz 1.3 for(int ieta = -29; ieta < 30; ++ieta){
390     if(ieta == 0) continue;
391     CaloTowerDetId id(ieta,1);
392     const GlobalPoint& hitpoint = geo->getPosition(id);
393 nart 1.21 // cout<<"iETA "<<ieta<<endl;
394 yilmaz 1.3 double eta = hitpoint.eta();
395 nart 1.21 // cout<<"eta "<<eta<<endl;
396 yilmaz 1.3 math::PtEtaPhiMLorentzVector p4(1,eta,1.,1.);
397     GlobalPoint pos(1,1,1);
398     CaloTower c(id,1.,1.,1.,1,1, p4, pos,pos);
399     col.push_back(c);
400     reco::CandidatePtr ptr(&col,col.size() - 1);
401     double mean = subtractor->getMeanAtTower(ptr);
402     double sigma = subtractor->getSigmaAtTower(ptr);
403     double pu = subtractor->getPileUpAtTower(ptr);
404     ntPU->Fill(eta,mean,sigma,step,iev_);
405     hPU[step]->Fill(eta,pu);
406     hMean[step]->Fill(eta,mean);
407     hSigma[step]->Fill(eta,sigma);
408 yilmaz 1.11
409     if(step == 7){
410     hPUieta->Fill(ieta,pu);
411     hMeanieta->Fill(ieta,mean);
412     hRMSieta->Fill(ieta,sigma);
413     }
414 yilmaz 1.1 }
415     }
416    
417     void JetAlgorithmAnalyzer::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
418     {
419 yilmaz 1.4
420 yilmaz 1.32 if(!centrality_) centrality_ = new CentralityProvider(iSetup);
421     centrality_->newEvent(iEvent,iSetup);
422    
423 yilmaz 1.13 phi0_ = 0;
424 yilmaz 1.4
425 yilmaz 1.1 if(!geo){
426     edm::ESHandle<CaloGeometry> pGeo;
427     iSetup.get<CaloGeometryRecord>().get(pGeo);
428     geo = pGeo.product();
429     }
430    
431 nart 1.22 iEvent.getByLabel(PatJetSrc_,patjets);
432    
433 yilmaz 1.32 hf_ = centrality_->centralityValue();
434     sumET_ = centrality_->raw()->Ntracks();
435     bin_ = centrality_->getBin();
436 yilmaz 1.13
437 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Entered produce\n";
438 yilmaz 1.1 //determine signal vertex
439     vertex_=reco::Jet::Point(0,0,0);
440     if (makeCaloJet(jetTypeE)&&doPVCorrection_) {
441 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Adding PV info\n";
442 yilmaz 1.1 edm::Handle<reco::VertexCollection> pvCollection;
443     iEvent.getByLabel(srcPVs_,pvCollection);
444     if (pvCollection->size()>0) vertex_=pvCollection->begin()->position();
445     }
446    
447     // For Pileup subtraction using offset correction:
448     // set up geometry map
449     if ( doPUOffsetCorr_ ) {
450 yilmaz 1.29 // cout<<"setting up ... "<<endl;
451 yilmaz 1.1 subtractor_->setupGeometryMap(iEvent, iSetup);
452     }
453    
454     // clear data
455 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Clear data\n";
456 yilmaz 1.1 fjInputs_.clear();
457     fjJets_.clear();
458     inputs_.clear();
459 yilmaz 1.2
460 yilmaz 1.13 if(doRecoEvtPlane_){
461     Handle<reco::EvtPlaneCollection> planes;
462     iEvent.getByLabel(epTag_,planes);
463 yilmaz 1.18 phi0_ = (*planes)[evtPlaneIndex_].angle();
464 yilmaz 1.13 }
465 yilmaz 1.3
466 yilmaz 1.2
467 yilmaz 1.1 // get inputs and convert them to the fastjet format (fastjet::PeudoJet)
468     edm::Handle<reco::CandidateView> inputsHandle;
469     iEvent.getByLabel(src_,inputsHandle);
470     for (size_t i = 0; i < inputsHandle->size(); ++i) {
471     inputs_.push_back(inputsHandle->ptrAt(i));
472     }
473 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Got inputs\n";
474 yilmaz 1.1
475     // Convert candidates to fastjet::PseudoJets.
476     // Also correct to Primary Vertex. Will modify fjInputs_
477     // and use inputs_
478     fjInputs_.reserve(inputs_.size());
479     inputTowers();
480 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Inputted towers\n";
481 yilmaz 1.1
482     fillTowerNtuple(fjInputs_,0);
483     fillBkgNtuple(subtractor_.get(),0);
484    
485     // For Pileup subtraction using offset correction:
486     // Subtract pedestal.
487 yilmaz 1.8
488 yilmaz 1.1 if ( doPUOffsetCorr_ ) {
489 yilmaz 1.31 subtractor_->setDefinition(fjJetDefinition_);
490 yilmaz 1.8 subtractor_->reset(inputs_,fjInputs_,fjJets_);
491 yilmaz 1.1 subtractor_->calculatePedestal(fjInputs_);
492    
493     fillTowerNtuple(fjInputs_,1);
494     fillBkgNtuple(subtractor_.get(),1);
495 yilmaz 1.8 subtractor_->subtractPedestal(fjInputs_);
496 yilmaz 1.1
497     fillTowerNtuple(fjInputs_,2);
498     fillBkgNtuple(subtractor_.get(),2);
499    
500 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Subtracted pedestal\n";
501 yilmaz 1.1 }
502    
503     // Run algorithm. Will modify fjJets_ and allocate fjClusterSeq_.
504     // This will use fjInputs_
505     runAlgorithm( iEvent, iSetup );
506    
507     fillTowerNtuple(fjInputs_,3);
508     fillBkgNtuple(subtractor_.get(),3);
509 yilmaz 1.32 // fillJetNtuple(fjJets_,3);
510 yilmaz 1.1
511 yilmaz 1.31 /// if ( doPUOffsetCorr_ ) {
512     /// subtractor_->setAlgorithm(fjClusterSeq_);
513     /// }
514 yilmaz 1.1
515 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Ran algorithm\n";
516 yilmaz 1.1
517     // For Pileup subtraction using offset correction:
518     // Now we find jets and need to recalculate their energy,
519     // mark towers participated in jet,
520     // remove occupied towers from the list and recalculate mean and sigma
521     // put the initial towers collection to the jet,
522     // and subtract from initial towers in jet recalculated mean and sigma of towers
523     if ( doPUOffsetCorr_ ) {
524     vector<fastjet::PseudoJet> orphanInput;
525     subtractor_->calculateOrphanInput(orphanInput);
526     fillTowerNtuple(orphanInput,4);
527     fillBkgNtuple(subtractor_.get(),4);
528 yilmaz 1.32 // fillJetNtuple(fjJets_,4);
529 yilmaz 1.1
530 yilmaz 1.4 //only the id's of the orphan input are used, not their energy
531 yilmaz 1.1 subtractor_->calculatePedestal(orphanInput);
532     fillTowerNtuple(orphanInput,5);
533     fillBkgNtuple(subtractor_.get(),5);
534 yilmaz 1.32 // fillJetNtuple(fjJets_,5);
535 yilmaz 1.1
536     subtractor_->offsetCorrectJets();
537     fillTowerNtuple(orphanInput,6);
538     fillBkgNtuple(subtractor_.get(),6);
539 yilmaz 1.32 // fillJetNtuple(fjJets_,6);
540 yilmaz 1.1
541     }
542    
543     // Write the output jets.
544     // This will (by default) call the member function template
545     // "writeJets", but can be overridden.
546     // this will use inputs_
547     output( iEvent, iSetup );
548     fillBkgNtuple(subtractor_.get(),7);
549 yilmaz 1.32 // fillJetNtuple(fjJets_,7);
550 yilmaz 1.1
551 yilmaz 1.32 // cout<<("VirtualJetProducer") << "Wrote jets\n";
552 yilmaz 1.1
553     ++iev_;
554 yilmaz 1.2
555     doAnalysis_ = false;
556 yilmaz 1.1 return;
557     }
558    
559     void JetAlgorithmAnalyzer::output(edm::Event & iEvent, edm::EventSetup const& iSetup)
560     {
561     // Write jets and constitutents. Will use fjJets_, inputs_
562     // and fjClusterSeq_
563 yilmaz 1.13
564 yilmaz 1.32 cout<<"output running "<<endl;
565     // return;
566 yilmaz 1.13
567 yilmaz 1.1 switch( jetTypeE ) {
568     case JetType::CaloJet :
569 yilmaz 1.32 // writeJets<reco::CaloJet>( iEvent, iSetup);
570 yilmaz 1.10 writeBkgJets<reco::CaloJet>( iEvent, iSetup);
571 yilmaz 1.1 break;
572     case JetType::PFJet :
573 yilmaz 1.32 // writeJets<reco::PFJet>( iEvent, iSetup);
574 yilmaz 1.1 writeBkgJets<reco::PFJet>( iEvent, iSetup);
575     break;
576     case JetType::GenJet :
577 yilmaz 1.32 // writeJets<reco::GenJet>( iEvent, iSetup);
578 yilmaz 1.1 writeBkgJets<reco::GenJet>( iEvent, iSetup);
579     break;
580     case JetType::TrackJet :
581 yilmaz 1.32 // writeJets<reco::TrackJet>( iEvent, iSetup);
582 yilmaz 1.1 writeBkgJets<reco::TrackJet>( iEvent, iSetup);
583     break;
584     case JetType::BasicJet :
585 yilmaz 1.32 // writeJets<reco::BasicJet>( iEvent, iSetup);
586 yilmaz 1.1 writeBkgJets<reco::BasicJet>( iEvent, iSetup);
587     break;
588     default:
589     edm::LogError("InvalidInput") << " invalid jet type in VirtualJetProducer\n";
590     break;
591     };
592    
593     }
594    
595     template< typename T >
596     void JetAlgorithmAnalyzer::writeBkgJets( edm::Event & iEvent, edm::EventSetup const& iSetup )
597     {
598     // produce output jet collection
599    
600 yilmaz 1.34 // cout<<"Started the Random Cones"<<endl;
601 yilmaz 1.32
602    
603 yilmaz 1.1 using namespace reco;
604    
605     std::vector<fastjet::PseudoJet> fjFakeJets_;
606     std::vector<std::vector<reco::CandidatePtr> > constituents_;
607     vector<double> phiRandom;
608     vector<double> etaRandom;
609     vector<double> et;
610 yilmaz 1.15 vector<double> had;
611     vector<double> em;
612 yilmaz 1.1 vector<double> pileUp;
613 yilmaz 1.12 vector<double> mean;
614     vector<double> rms;
615 yilmaz 1.23 vector<double> rawJetPt;
616 nart 1.22 vector<double> dr;
617     vector<bool> matched;
618    
619 yilmaz 1.12
620 yilmaz 1.1 std::auto_ptr<std::vector<bool> > directions(new std::vector<bool>());
621     directions->reserve(nFill_);
622    
623     phiRandom.reserve(nFill_);
624     etaRandom.reserve(nFill_);
625     et.reserve(nFill_);
626     pileUp.reserve(nFill_);
627 yilmaz 1.12 rms.reserve(nFill_);
628     mean.reserve(nFill_);
629 yilmaz 1.16 em.reserve(nFill_);
630 yilmaz 1.15 had.reserve(nFill_);
631 nart 1.22 matched.reserve(nFill_);
632 yilmaz 1.23 rawJetPt.reserve(nFill_);
633 nart 1.22 dr.reserve(nFill_);
634 yilmaz 1.1
635 yilmaz 1.26 double evtJetPt = 0;
636    
637     for(unsigned int j = 0 ; j < patjets->size(); ++j){
638     const pat::Jet& jet = (*patjets)[j];
639     if(jet.pt() > evtJetPt){
640     evtJetPt = jet.pt();
641     }
642     }
643    
644 yilmaz 1.1 fjFakeJets_.reserve(nFill_);
645     constituents_.reserve(nFill_);
646    
647     for(int ijet = 0; ijet < nFill_; ++ijet){
648     vector<reco::CandidatePtr> vec;
649     constituents_.push_back(vec);
650     directions->push_back(true);
651     }
652    
653     for(int ijet = 0; ijet < nFill_; ++ijet){
654     phiRandom[ijet] = 2*pi*randomEngine->flat() - pi;
655     etaRandom[ijet] = 2*etaMax_*randomEngine->flat() - etaMax_;
656 yilmaz 1.25 if(ijet>0 && backToBack_) phiRandom[ijet] = - phiRandom[0];
657 yilmaz 1.1 et[ijet] = 0;
658 yilmaz 1.15 had[ijet] = 0;
659     em[ijet] = 0;
660 yilmaz 1.1 pileUp[ijet] = 0;
661 nart 1.21 mean[ijet]=0;
662     rms[ijet]=0;
663 yilmaz 1.23 rawJetPt[ijet]=-99;
664     dr[ijet]=-99;
665 nart 1.22 matched[ijet]=false;
666 yilmaz 1.1 }
667    
668 nart 1.20
669     for(unsigned int iy = 0; iy < inputs_.size(); ++iy){
670    
671     const reco::CandidatePtr & tower=inputs_[iy];
672     const CaloTower* ctc = dynamic_cast<const CaloTower*>(tower.get());
673 yilmaz 1.1
674 yilmaz 1.9 int ieta = ctc->id().ieta();
675     int iphi = ctc->id().iphi();
676     CaloTowerDetId id(ieta,iphi);
677     const GlobalPoint& hitpoint = geo->getPosition(id);
678     double towEta = hitpoint.eta();
679     double towPhi = hitpoint.phi();
680    
681 yilmaz 1.1 for(int ir = 0; ir < nFill_; ++ir){
682 yilmaz 1.9 if(reco::deltaR(towEta,towPhi,etaRandom[ir],phiRandom[ir]) > rParam_) continue;
683 yilmaz 1.1
684     constituents_[ir].push_back(tower);
685    
686 yilmaz 1.14 if(sumRecHits_){
687     const GlobalPoint& pos=geo->getPosition(ctc->id());
688     double energy = ctc->emEnergy() + ctc->hadEnergy();
689 yilmaz 1.15 double ang = sin(pos.theta());
690 nart 1.21 // towet = energy*ang;
691 yilmaz 1.15 em[ir] += ctc->emEnergy()*ang;
692     had[ir] += ctc->hadEnergy()*ang;
693 yilmaz 1.14 }
694 nart 1.22
695     for(unsigned int j = 0 ; j < patjets->size(); ++j){
696     const pat::Jet& jet = (*patjets)[j];
697 yilmaz 1.23 double thisdr = reco::deltaR(etaRandom[ir],phiRandom[ir],jet.eta(),jet.phi());
698 yilmaz 1.26 if(thisdr < cone_ && jet.pt() > rawJetPt[ir]){
699 yilmaz 1.23 dr[ir] = thisdr;
700 yilmaz 1.26 rawJetPt[ir] = jet.pt();
701 nart 1.22 matched[ir] = true;
702     }
703     }
704 yilmaz 1.23
705 nart 1.21 double towet = tower->et();
706 yilmaz 1.1 double putow = subtractor_->getPileUpAtTower(tower);
707     double etadd = towet - putow;
708     if(avoidNegative_ && etadd < 0.) etadd = 0;
709     et[ir] += etadd;
710     pileUp[ir] += towet - etadd;
711 yilmaz 1.12 mean[ir] += subtractor_->getMeanAtTower(tower);
712     rms[ir] += subtractor_->getSigmaAtTower(tower);
713    
714 yilmaz 1.1 }
715     }
716 yilmaz 1.29 // cout<<"Start filling jets"<<endl;
717 yilmaz 1.30
718     if(backToBack_){
719     int ir = 0;
720     double phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
721 yilmaz 1.31 ///float entry0[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
722     Float_t entry0[100];
723     entry0[0]=etaRandom[ir];
724     entry0[1]=phiRandom[ir];
725     entry0[2]=phiRel;
726     entry0[3]=et[ir];
727     entry0[4]=had[ir];
728     entry0[5]=em[ir];
729     entry0[6]=pileUp[ir];
730     entry0[7]=mean[ir];
731     entry0[8]=rms[ir];
732     entry0[9]=bin_;
733     entry0[10]=hf_;
734     entry0[11]=sumET_;
735     entry0[12]=iev_;
736     entry0[13]=dr[ir];
737     entry0[14]=rawJetPt[ir];
738     entry0[15]=evtJetPt;
739     //Float_t entry0[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
740 yilmaz 1.30 ntRandom0->Fill(entry0);
741     ir = 1;
742     phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
743 yilmaz 1.31 ///float entry1[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
744     Float_t entry1[100];
745     entry1[0]=etaRandom[ir];
746     entry1[1]=phiRandom[ir];
747     entry1[2]=phiRel;
748     entry1[3]=et[ir];
749     entry1[4]=had[ir];
750     entry1[5]=em[ir];
751     entry1[6]=pileUp[ir];
752     entry1[7]=mean[ir];
753     entry1[8]=rms[ir];
754     entry1[9]=bin_;
755     entry1[10]=hf_;
756     entry1[11]=sumET_;
757     entry1[12]=iev_;
758     entry1[13]=dr[ir];
759     entry1[14]=rawJetPt[ir];
760     entry1[15]=evtJetPt;
761     //Float_t entry1[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
762 yilmaz 1.30 ntRandom1->Fill(entry1);
763     }
764    
765 yilmaz 1.1 for(int ir = 0; ir < nFill_; ++ir){
766 yilmaz 1.13 double phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
767 yilmaz 1.31 //float entry[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
768     Float_t entry[100];
769     entry[0]=etaRandom[ir];
770     entry[1]=phiRandom[ir];
771     entry[2]=phiRel;
772     entry[3]=et[ir];
773     entry[4]=had[ir];
774     entry[5]=em[ir];
775     entry[6]=pileUp[ir];
776     entry[7]=mean[ir];
777     entry[8]=rms[ir];
778     entry[9]=bin_;
779     entry[10]=hf_;
780     entry[11]=sumET_;
781     entry[12]=iev_;
782     entry[13]=dr[ir];
783     entry[14]=rawJetPt[ir];
784     entry[15]=evtJetPt;
785     //Float_t entry[100] = {etaRandom[ir],phiRandom[ir],phiRel,et[ir],had[ir],em[ir],pileUp[ir],mean[ir],rms[ir],bin_,hf_,sumET_,iev_,dr[ir],rawJetPt[ir],evtJetPt};
786 yilmaz 1.30 if(!backToBack_)ntRandom->Fill(entry);
787 yilmaz 1.1 if(et[ir] < 0){
788 yilmaz 1.3 // cout<<"Flipping vector"<<endl;
789 yilmaz 1.1 (*directions)[ir] = false;
790     et[ir] = -et[ir];
791     }else{
792 yilmaz 1.3 // cout<<"Keep vector same"<<endl;
793 yilmaz 1.1 (*directions)[ir] = true;
794     }
795 nart 1.21 // cout<<"Lorentz"<<endl;
796 yilmaz 1.1
797     math::PtEtaPhiMLorentzVector p(et[ir],etaRandom[ir],phiRandom[ir],0);
798     fastjet::PseudoJet jet(p.px(),p.py(),p.pz(),p.energy());
799     fjFakeJets_.push_back(jet);
800     }
801    
802     std::auto_ptr<std::vector<T> > jets(new std::vector<T>() );
803     jets->reserve(fjFakeJets_.size());
804    
805     for (unsigned int ijet=0;ijet<fjFakeJets_.size();++ijet) {
806     // allocate this jet
807     T jet;
808     // get the fastjet jet
809     const fastjet::PseudoJet& fjJet = fjFakeJets_[ijet];
810    
811     // convert them to CandidatePtr vector
812     std::vector<CandidatePtr> constituents =
813     constituents_[ijet];
814    
815     writeSpecific(jet,
816     Particle::LorentzVector(fjJet.px(),
817     fjJet.py(),
818     fjJet.pz(),
819     fjJet.E()),
820     vertex_,
821     constituents, iSetup);
822    
823     // calcuate the jet area
824     double jetArea=0.0;
825     jet.setJetArea (jetArea);
826     if(doPUOffsetCorr_){
827     jet.setPileup(pileUp[ijet]);
828     }else{
829     jet.setPileup (0.0);
830     }
831    
832     // add to the list
833     jets->push_back(jet);
834     }
835    
836     // put the jets in the collection
837 yilmaz 1.28 // iEvent.put(jets,"randomCones");
838     // iEvent.put(directions,"directions");
839 yilmaz 1.1 }
840    
841 yilmaz 1.8 //void JetAlgorithmAnalyzer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup, std::vector<fastjet::PseudoJet>& input )
842 yilmaz 1.1 void JetAlgorithmAnalyzer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup)
843     {
844 yilmaz 1.8 if ( !doAreaFastjet_ && !doRhoFastjet_) {
845     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequence( fjInputs_, *fjJetDefinition_ ) );
846     } else {
847     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequenceArea( fjInputs_, *fjJetDefinition_ , *fjActiveArea_ ) );
848     }
849     fjJets_ = fastjet::sorted_by_pt(fjClusterSeq_->inclusive_jets(jetPtMin_));
850 yilmaz 1.1
851     }
852    
853    
854    
855    
856    
857     ////////////////////////////////////////////////////////////////////////////////
858     // define as cmssw plugin
859     ////////////////////////////////////////////////////////////////////////////////
860    
861     DEFINE_FWK_MODULE(JetAlgorithmAnalyzer);
862