ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/JetAnalysis/src/JetAlgorithmAnalyzer.cc
Revision: 1.24
Committed: Sat Dec 11 17:03:11 2010 UTC (14 years, 4 months ago) by yilmaz
Content type: text/plain
Branch: MAIN
Changes since 1.23: +0 -1 lines
Log Message:
bug fix

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