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