ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/JetAnalysis/src/JetAlgorithmAnalyzer.cc
Revision: 1.31
Committed: Wed Sep 5 18:53:34 2012 UTC (12 years, 7 months ago) by yilmaz
Content type: text/plain
Branch: MAIN
CVS Tags: HiForest_V02_53, HiForest_V02_52, HiForest_V02_51, HiForest_V02_50, HiForest_V02_49, HiForest_V02_48, HiForest_V02_47, HiForest_V02_46, HiForest_V02_45, HiForest_V02_44, HiForest_V02_43, HiForest_V02_42, HiForest_V02_41, HiForest_V02_40, HiForest_V02_39, HiForest_V02_38, HiForest_V02_37, HiForest_V02_36, HiForest_V02_35, HiForest_V02_34, HiForest_V02_33, HiForest_V02_32, HiForest_V02_31, HiForest_V02_30, HiForest_V02_27, HiForest_V02_26
Changes since 1.30: +61 -6 lines
Log Message:
update to 52x

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.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<reco::Centrality> cent;
95     edm::Handle<pat::JetCollection> patjets;
96    
97 yilmaz 1.2 std::vector<TH2D*> hTowers;
98 yilmaz 1.6 std::vector<TH2D*> hJetTowers;
99    
100 yilmaz 1.2 std::vector<TH2D*> hTowersFromEvtPlane;
101    
102     std::vector<TH2D*> hJets;
103 yilmaz 1.3 std::vector<TH1D*> hPU;
104     std::vector<TH1D*> hMean;
105     std::vector<TH1D*> hSigma;
106    
107 yilmaz 1.2 std::vector<TH2D*> hRandom;
108    
109 yilmaz 1.3
110 yilmaz 1.11 TH2D* hPTieta;
111     TH1D* hMeanieta;
112     TH1D* hRMSieta;
113     TH1D* hPUieta;
114 yilmaz 1.3
115 yilmaz 1.1 const CaloGeometry *geo;
116     edm::Service<TFileService> f;
117     };
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 nart 1.22 cbins_(0),
195     cone_(1)
196 yilmaz 1.1 {
197 yilmaz 1.19
198     puSubtractorName_ = iConfig.getParameter<string> ("subtractorName");
199     subtractor_ = boost::shared_ptr<PileUpSubtractor>(PileUpSubtractorFactory::get()->create( puSubtractorName_, iConfig));
200    
201 yilmaz 1.1 edm::Service<RandomNumberGenerator> rng;
202     randomEngine = &(rng->getEngine());
203    
204     doAnalysis_ = iConfig.getUntrackedParameter<bool>("doAnalysis",true);
205 yilmaz 1.12 doRandomCones_ = iConfig.getUntrackedParameter<bool>("doRandomCones",true);
206 yilmaz 1.25
207     backToBack_ = iConfig.getUntrackedParameter<bool>("doBackToBack",false);
208     if(backToBack_) nFill_ = 2;
209    
210 yilmaz 1.12 doFullCone_ = iConfig.getUntrackedParameter<bool>("doFullCone",true);
211    
212 yilmaz 1.2 doMC_ = iConfig.getUntrackedParameter<bool>("doMC",true);
213 yilmaz 1.13 doRecoEvtPlane_ = iConfig.getUntrackedParameter<bool>("doRecoEvtPlane",true);
214 yilmaz 1.18 evtPlaneIndex_ = iConfig.getUntrackedParameter<int>("evtPlaneIndex",31);
215 yilmaz 1.13
216 yilmaz 1.14 sumRecHits_ = iConfig.getParameter<bool>("sumRecHits");
217    
218 yilmaz 1.11 centTag_ = iConfig.getParameter<InputTag>("centralityTag");
219 yilmaz 1.13 epTag_ = iConfig.getParameter<InputTag>("evtPlaneTag");
220 nart 1.22 PatJetSrc_ = iConfig.getUntrackedParameter<edm::InputTag>("patJetSrc",edm::InputTag("icPu5patJets"));
221 yilmaz 1.3
222     if(doAnalysis_) centBin_ = iConfig.getUntrackedParameter<int>("centrality",0);
223    
224 yilmaz 1.1 avoidNegative_ = iConfig.getParameter<bool>("avoidNegative");
225    
226     if ( iConfig.exists("UseOnlyVertexTracks") )
227     useOnlyVertexTracks_ = iConfig.getParameter<bool>("UseOnlyVertexTracks");
228     else
229     useOnlyVertexTracks_ = false;
230    
231     if ( iConfig.exists("UseOnlyOnePV") )
232     useOnlyOnePV_ = iConfig.getParameter<bool>("UseOnlyOnePV");
233     else
234     useOnlyOnePV_ = false;
235    
236     if ( iConfig.exists("DrTrVtxMax") )
237     dzTrVtxMax_ = iConfig.getParameter<double>("DzTrVtxMax");
238     else
239     dzTrVtxMax_ = false;
240    
241 yilmaz 1.3 produces<reco::CaloJetCollection>("randomCones");
242 yilmaz 1.1 produces<std::vector<bool> >("directions");
243    
244     if(doAnalysis_){
245     ntTowers = f->make<TNtuple>("ntTowers","Algorithm Analysis Towers","eta:phi:et:step:event");
246 yilmaz 1.3 ntTowersFromEvtPlane = f->make<TNtuple>("ntTowersFromEvtPlane","Algorithm Analysis Towers","eta:phi:et:step:event");
247 yilmaz 1.6 ntJetTowers = f->make<TNtuple>("ntTowers","Algorithm Analysis Towers","eta:phi:et:step:event");
248 yilmaz 1.3
249 yilmaz 1.1 ntJets = f->make<TNtuple>("ntJets","Algorithm Analysis Jets","eta:phi:et:step:event");
250     ntPU = f->make<TNtuple>("ntPU","Algorithm Analysis Background","eta:mean:sigma:step:event");
251 yilmaz 1.30 if(backToBack_){
252     ntRandom0 = f->make<TNtuple>("ntRandom0","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
253     ntRandom1 = f->make<TNtuple>("ntRandom1","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
254     }else{
255     ntRandom = f->make<TNtuple>("ntRandom","Algorithm Analysis Background","eta:phi:phiRel:et:had:em:pu:mean:rms:bin:hf:sumET:event:dR:matchedJetPt:evtJetPt");
256     }
257 yilmaz 1.2
258 yilmaz 1.5 ntuple = f->make<TNtuple>("nt","debug","ieta:eta:iphi:phi:pt:em:had");
259    
260 yilmaz 1.11 hPTieta = f->make<TH2D>("hPTieta","hPTieta",23,-11.5,11.5,200,0,10);
261     hRMSieta = f->make<TH1D>("hRMSieta","hPTieta",23,-11.5,11.5);
262     hMeanieta = f->make<TH1D>("hMeanieta","hPTieta",23,-11.5,11.5);
263     hPUieta = f->make<TH1D>("hPUieta","hPTieta",23,-11.5,11.5);
264    
265 yilmaz 1.2 for(int i = 0; i < nSteps; ++i){
266 yilmaz 1.3 hTowers.push_back(f->make<TH2D>(Form("hTowers_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
267     hJets.push_back(f->make<TH2D>(Form("hJets_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
268     hTowersFromEvtPlane.push_back(f->make<TH2D>(Form("hTowersFromEvtPlane_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
269    
270 yilmaz 1.6 hJetTowers.push_back(f->make<TH2D>(Form("hJetTowers_step%d",i),"",200,-5.5,5.5,200,-0.02,6.3));
271    
272 yilmaz 1.3 hPU.push_back(f->make<TH1D>(Form("hPU_step%d",i),"",200,-5.5,5.5));
273     hMean.push_back(f->make<TH1D>(Form("hMean_step%d",i),"",200,-5.5,5.5));
274     hSigma.push_back(f->make<TH1D>(Form("hSigma_step%d",i),"",200,-5.5,5.5));
275    
276 yilmaz 1.2 }
277    
278 yilmaz 1.1 }
279    
280     }
281    
282    
283     //______________________________________________________________________________
284     JetAlgorithmAnalyzer::~JetAlgorithmAnalyzer()
285     {
286     }
287    
288 yilmaz 1.2 void JetAlgorithmAnalyzer::fillNtuple(int output, const std::vector<fastjet::PseudoJet>& jets, int step){
289 yilmaz 1.1 if(!doAnalysis_) return;
290    
291     TNtuple* nt;
292 yilmaz 1.2 TH2D* h;
293    
294     if(output == 1){
295     nt = ntJets;
296     h = hJets[step];
297     }
298     if(output == 0){
299     nt = ntTowers;
300     h = hTowers[step];
301     }
302     if(output == 2){
303     nt = ntTowersFromEvtPlane;
304     h = hTowersFromEvtPlane[step];
305     }
306 yilmaz 1.6 if(output == 3){
307     nt = ntJetTowers;
308     h = hJetTowers[step];
309     }
310 yilmaz 1.1
311 yilmaz 1.29 bool printDebug = 0;
312    
313 yilmaz 1.4 double totet = 0;
314     int ntow = 0;
315 yilmaz 1.5 int nj = jets.size();
316    
317 yilmaz 1.29 if(printDebug){
318 yilmaz 1.5 cout<<"step : "<<step<<endl;
319     cout<<"Size of input : "<<nj<<endl;
320 yilmaz 1.29 }
321 yilmaz 1.1 for(unsigned int i = 0; i < jets.size(); ++i){
322 yilmaz 1.4 const fastjet::PseudoJet& jet = jets[i];
323    
324     double pt = jet.perp();
325 yilmaz 1.11 int ieta = -99;
326 yilmaz 1.4 if(output != 1){
327     reco::CandidatePtr const & itow = inputs_[ jet.user_index() ];
328     pt = itow->et();
329 yilmaz 1.11 ieta = subtractor_->ieta(itow);
330     }
331    
332     if(output == 0 && step == 6){
333     hPTieta->Fill(ieta,pt);
334 yilmaz 1.4 }
335    
336     double phi = jet.phi();
337     if(output == 2){
338     phi = phi - phi0_;
339     if(phi < 0) phi += 2*PI;
340     if(phi > 2*PI) phi -= 2*PI;
341     }
342    
343 yilmaz 1.11 double eta = jet.eta();
344 yilmaz 1.4 if(eta > 0 && eta < 0.08){
345     // if(fabs(eta) < 1.){
346     totet += pt;
347     ntow++;
348     }
349    
350     nt->Fill(jet.eta(),phi,pt,step,iev_);
351     h->Fill(jet.eta(),phi,pt);
352     }
353 yilmaz 1.29 if(printDebug){
354 yilmaz 1.4 cout<<"-----------------------------"<<endl;
355     cout<<"STEP = "<<step<<endl;
356     cout<<"Total ET = "<<totet<<endl;
357     cout<<"Towers counted = "<<ntow<<endl;
358     cout<<"Average tower ET = "<<totet/ntow<<endl;
359     cout<<"-----------------------------"<<endl;
360 yilmaz 1.29 }
361 yilmaz 1.1 }
362    
363    
364     void JetAlgorithmAnalyzer::fillTowerNtuple(const std::vector<fastjet::PseudoJet>& jets, int step){
365     fillNtuple(0,jets,step);
366 yilmaz 1.2 fillNtuple(2,jets,step);
367 yilmaz 1.1 }
368    
369     void JetAlgorithmAnalyzer::fillJetNtuple(const std::vector<fastjet::PseudoJet>& jets, int step){
370     fillNtuple(1,jets,step);
371 yilmaz 1.6
372     for(unsigned int i = 0; i < jets.size(); ++i){
373     const fastjet::PseudoJet& jet = jets[i];
374     std::vector<fastjet::PseudoJet> fjConstituents = sorted_by_pt(fjClusterSeq_->constituents(jet));
375 yilmaz 1.8
376    
377    
378 yilmaz 1.6
379     fillNtuple(3,fjConstituents,step);
380     }
381 yilmaz 1.1 }
382    
383     void JetAlgorithmAnalyzer::fillBkgNtuple(const PileUpSubtractor* subtractor, int step){
384     if(!doAnalysis_) return;
385     CaloTowerCollection col;
386 yilmaz 1.3 for(int ieta = -29; ieta < 30; ++ieta){
387     if(ieta == 0) continue;
388     CaloTowerDetId id(ieta,1);
389     const GlobalPoint& hitpoint = geo->getPosition(id);
390 nart 1.21 // cout<<"iETA "<<ieta<<endl;
391 yilmaz 1.3 double eta = hitpoint.eta();
392 nart 1.21 // cout<<"eta "<<eta<<endl;
393 yilmaz 1.3 math::PtEtaPhiMLorentzVector p4(1,eta,1.,1.);
394     GlobalPoint pos(1,1,1);
395     CaloTower c(id,1.,1.,1.,1,1, p4, pos,pos);
396     col.push_back(c);
397     reco::CandidatePtr ptr(&col,col.size() - 1);
398     double mean = subtractor->getMeanAtTower(ptr);
399     double sigma = subtractor->getSigmaAtTower(ptr);
400     double pu = subtractor->getPileUpAtTower(ptr);
401     ntPU->Fill(eta,mean,sigma,step,iev_);
402     hPU[step]->Fill(eta,pu);
403     hMean[step]->Fill(eta,mean);
404     hSigma[step]->Fill(eta,sigma);
405 yilmaz 1.11
406     if(step == 7){
407     hPUieta->Fill(ieta,pu);
408     hMeanieta->Fill(ieta,mean);
409     hRMSieta->Fill(ieta,sigma);
410     }
411 yilmaz 1.1 }
412     }
413    
414     void JetAlgorithmAnalyzer::produce(edm::Event& iEvent,const edm::EventSetup& iSetup)
415     {
416 yilmaz 1.4
417 yilmaz 1.6 // if(doAnalysis_) subtractor_->setDebug(1);
418     // else subtractor_->setDebug(0);
419 yilmaz 1.13 phi0_ = 0;
420 yilmaz 1.4
421 yilmaz 1.1 if(!geo){
422     edm::ESHandle<CaloGeometry> pGeo;
423     iSetup.get<CaloGeometryRecord>().get(pGeo);
424     geo = pGeo.product();
425     }
426    
427 nart 1.22 iEvent.getByLabel(PatJetSrc_,patjets);
428 yilmaz 1.13 iEvent.getByLabel(centTag_,cent);
429     if(!cbins_) cbins_ = getCentralityBinsFromDB(iSetup);
430 nart 1.22
431 yilmaz 1.13 hf_ = cent->EtHFhitSum();
432     sumET_ = cent->EtMidRapiditySum();
433     bin_ = cbins_->getBin(hf_);
434    
435     if(centBin_ >= 0 && bin_ != centBin_) return;
436    
437 yilmaz 1.1 LogDebug("VirtualJetProducer") << "Entered produce\n";
438     //determine signal vertex
439     vertex_=reco::Jet::Point(0,0,0);
440     if (makeCaloJet(jetTypeE)&&doPVCorrection_) {
441     LogDebug("VirtualJetProducer") << "Adding PV info\n";
442     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     LogDebug("VirtualJetProducer") << "Clear data\n";
456     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     LogDebug("VirtualJetProducer") << "Got inputs\n";
474    
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     LogDebug("VirtualJetProducer") << "Inputted towers\n";
481    
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     LogDebug("VirtualJetProducer") << "Subtracted pedestal\n";
501     }
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     fillJetNtuple(fjJets_,3);
510    
511 yilmaz 1.31 /// if ( doPUOffsetCorr_ ) {
512     /// subtractor_->setAlgorithm(fjClusterSeq_);
513     /// }
514 yilmaz 1.1
515     LogDebug("VirtualJetProducer") << "Ran algorithm\n";
516    
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     fillJetNtuple(fjJets_,4);
529    
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     fillJetNtuple(fjJets_,5);
535    
536     subtractor_->offsetCorrectJets();
537     fillTowerNtuple(orphanInput,6);
538     fillBkgNtuple(subtractor_.get(),6);
539     fillJetNtuple(fjJets_,6);
540    
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     fillJetNtuple(fjJets_,7);
550    
551     LogDebug("VirtualJetProducer") << "Wrote jets\n";
552    
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 nart 1.21 // cout<<"output running "<<endl;
565 yilmaz 1.13
566 yilmaz 1.1 switch( jetTypeE ) {
567     case JetType::CaloJet :
568 yilmaz 1.10 writeJets<reco::CaloJet>( iEvent, iSetup);
569     writeBkgJets<reco::CaloJet>( iEvent, iSetup);
570 yilmaz 1.1 break;
571     case JetType::PFJet :
572 yilmaz 1.3 writeJets<reco::PFJet>( iEvent, iSetup);
573 yilmaz 1.1 writeBkgJets<reco::PFJet>( iEvent, iSetup);
574     break;
575     case JetType::GenJet :
576 yilmaz 1.3 writeJets<reco::GenJet>( iEvent, iSetup);
577 yilmaz 1.1 writeBkgJets<reco::GenJet>( iEvent, iSetup);
578     break;
579     case JetType::TrackJet :
580 yilmaz 1.3 writeJets<reco::TrackJet>( iEvent, iSetup);
581 yilmaz 1.1 writeBkgJets<reco::TrackJet>( iEvent, iSetup);
582     break;
583     case JetType::BasicJet :
584 yilmaz 1.3 writeJets<reco::BasicJet>( iEvent, iSetup);
585 yilmaz 1.1 writeBkgJets<reco::BasicJet>( iEvent, iSetup);
586     break;
587     default:
588     edm::LogError("InvalidInput") << " invalid jet type in VirtualJetProducer\n";
589     break;
590     };
591    
592     }
593    
594     template< typename T >
595     void JetAlgorithmAnalyzer::writeBkgJets( edm::Event & iEvent, edm::EventSetup const& iSetup )
596     {
597     // produce output jet collection
598    
599     using namespace reco;
600    
601     std::vector<fastjet::PseudoJet> fjFakeJets_;
602     std::vector<std::vector<reco::CandidatePtr> > constituents_;
603     vector<double> phiRandom;
604     vector<double> etaRandom;
605     vector<double> et;
606 yilmaz 1.15 vector<double> had;
607     vector<double> em;
608 yilmaz 1.1 vector<double> pileUp;
609 yilmaz 1.12 vector<double> mean;
610     vector<double> rms;
611 yilmaz 1.23 vector<double> rawJetPt;
612 nart 1.22 vector<double> dr;
613     vector<bool> matched;
614    
615 yilmaz 1.12
616 yilmaz 1.1 std::auto_ptr<std::vector<bool> > directions(new std::vector<bool>());
617     directions->reserve(nFill_);
618    
619     phiRandom.reserve(nFill_);
620     etaRandom.reserve(nFill_);
621     et.reserve(nFill_);
622     pileUp.reserve(nFill_);
623 yilmaz 1.12 rms.reserve(nFill_);
624     mean.reserve(nFill_);
625 yilmaz 1.16 em.reserve(nFill_);
626 yilmaz 1.15 had.reserve(nFill_);
627 nart 1.22 matched.reserve(nFill_);
628 yilmaz 1.23 rawJetPt.reserve(nFill_);
629 nart 1.22 dr.reserve(nFill_);
630 yilmaz 1.1
631 yilmaz 1.26 double evtJetPt = 0;
632    
633     for(unsigned int j = 0 ; j < patjets->size(); ++j){
634     const pat::Jet& jet = (*patjets)[j];
635     if(jet.pt() > evtJetPt){
636     evtJetPt = jet.pt();
637     }
638     }
639    
640 yilmaz 1.1 fjFakeJets_.reserve(nFill_);
641     constituents_.reserve(nFill_);
642    
643     constituents_.reserve(nFill_);
644     for(int ijet = 0; ijet < nFill_; ++ijet){
645     vector<reco::CandidatePtr> vec;
646     constituents_.push_back(vec);
647     directions->push_back(true);
648     }
649    
650     for(int ijet = 0; ijet < nFill_; ++ijet){
651     phiRandom[ijet] = 2*pi*randomEngine->flat() - pi;
652     etaRandom[ijet] = 2*etaMax_*randomEngine->flat() - etaMax_;
653 yilmaz 1.25 if(ijet>0 && backToBack_) phiRandom[ijet] = - phiRandom[0];
654 yilmaz 1.1 et[ijet] = 0;
655 yilmaz 1.15 had[ijet] = 0;
656     em[ijet] = 0;
657 yilmaz 1.1 pileUp[ijet] = 0;
658 nart 1.21 mean[ijet]=0;
659     rms[ijet]=0;
660 yilmaz 1.23 rawJetPt[ijet]=-99;
661     dr[ijet]=-99;
662 nart 1.22 matched[ijet]=false;
663 yilmaz 1.1 }
664    
665 nart 1.20
666     for(unsigned int iy = 0; iy < inputs_.size(); ++iy){
667    
668     /*
669 yilmaz 1.1 for (vector<fastjet::PseudoJet>::const_iterator input_object = fjInputs_.begin (),
670     fjInputsEnd = fjInputs_.end();
671     input_object != fjInputsEnd; ++input_object) {
672 nart 1.20 */
673    
674     const reco::CandidatePtr & tower=inputs_[iy];
675     const CaloTower* ctc = dynamic_cast<const CaloTower*>(tower.get());
676 yilmaz 1.1
677 yilmaz 1.9 int ieta = ctc->id().ieta();
678     int iphi = ctc->id().iphi();
679     CaloTowerDetId id(ieta,iphi);
680     const GlobalPoint& hitpoint = geo->getPosition(id);
681     double towEta = hitpoint.eta();
682     double towPhi = hitpoint.phi();
683    
684 yilmaz 1.1 for(int ir = 0; ir < nFill_; ++ir){
685 yilmaz 1.9 if(reco::deltaR(towEta,towPhi,etaRandom[ir],phiRandom[ir]) > rParam_) continue;
686 yilmaz 1.1
687     constituents_[ir].push_back(tower);
688    
689 yilmaz 1.14 if(sumRecHits_){
690     const GlobalPoint& pos=geo->getPosition(ctc->id());
691     double energy = ctc->emEnergy() + ctc->hadEnergy();
692 yilmaz 1.15 double ang = sin(pos.theta());
693 nart 1.21 // towet = energy*ang;
694 yilmaz 1.15 em[ir] += ctc->emEnergy()*ang;
695     had[ir] += ctc->hadEnergy()*ang;
696 yilmaz 1.14 }
697 nart 1.22
698     for(unsigned int j = 0 ; j < patjets->size(); ++j){
699     const pat::Jet& jet = (*patjets)[j];
700 yilmaz 1.23 double thisdr = reco::deltaR(etaRandom[ir],phiRandom[ir],jet.eta(),jet.phi());
701 yilmaz 1.26 if(thisdr < cone_ && jet.pt() > rawJetPt[ir]){
702 yilmaz 1.23 dr[ir] = thisdr;
703 yilmaz 1.26 rawJetPt[ir] = jet.pt();
704 nart 1.22 matched[ir] = true;
705     }
706     }
707 yilmaz 1.23
708 nart 1.21 double towet = tower->et();
709 yilmaz 1.1 double putow = subtractor_->getPileUpAtTower(tower);
710     double etadd = towet - putow;
711     if(avoidNegative_ && etadd < 0.) etadd = 0;
712     et[ir] += etadd;
713     pileUp[ir] += towet - etadd;
714 yilmaz 1.12 mean[ir] += subtractor_->getMeanAtTower(tower);
715     rms[ir] += subtractor_->getSigmaAtTower(tower);
716    
717 yilmaz 1.1 }
718     }
719 yilmaz 1.29 // cout<<"Start filling jets"<<endl;
720 yilmaz 1.30
721     if(backToBack_){
722     int ir = 0;
723     double phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
724 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};
725     Float_t entry0[100];
726     entry0[0]=etaRandom[ir];
727     entry0[1]=phiRandom[ir];
728     entry0[2]=phiRel;
729     entry0[3]=et[ir];
730     entry0[4]=had[ir];
731     entry0[5]=em[ir];
732     entry0[6]=pileUp[ir];
733     entry0[7]=mean[ir];
734     entry0[8]=rms[ir];
735     entry0[9]=bin_;
736     entry0[10]=hf_;
737     entry0[11]=sumET_;
738     entry0[12]=iev_;
739     entry0[13]=dr[ir];
740     entry0[14]=rawJetPt[ir];
741     entry0[15]=evtJetPt;
742     //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};
743 yilmaz 1.30 ntRandom0->Fill(entry0);
744     ir = 1;
745     phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
746 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};
747     Float_t entry1[100];
748     entry1[0]=etaRandom[ir];
749     entry1[1]=phiRandom[ir];
750     entry1[2]=phiRel;
751     entry1[3]=et[ir];
752     entry1[4]=had[ir];
753     entry1[5]=em[ir];
754     entry1[6]=pileUp[ir];
755     entry1[7]=mean[ir];
756     entry1[8]=rms[ir];
757     entry1[9]=bin_;
758     entry1[10]=hf_;
759     entry1[11]=sumET_;
760     entry1[12]=iev_;
761     entry1[13]=dr[ir];
762     entry1[14]=rawJetPt[ir];
763     entry1[15]=evtJetPt;
764     //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};
765 yilmaz 1.30 ntRandom1->Fill(entry1);
766     }
767    
768 yilmaz 1.1 for(int ir = 0; ir < nFill_; ++ir){
769 yilmaz 1.13 double phiRel = reco::deltaPhi(phiRandom[ir],phi0_);
770 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};
771     Float_t entry[100];
772     entry[0]=etaRandom[ir];
773     entry[1]=phiRandom[ir];
774     entry[2]=phiRel;
775     entry[3]=et[ir];
776     entry[4]=had[ir];
777     entry[5]=em[ir];
778     entry[6]=pileUp[ir];
779     entry[7]=mean[ir];
780     entry[8]=rms[ir];
781     entry[9]=bin_;
782     entry[10]=hf_;
783     entry[11]=sumET_;
784     entry[12]=iev_;
785     entry[13]=dr[ir];
786     entry[14]=rawJetPt[ir];
787     entry[15]=evtJetPt;
788     //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};
789 yilmaz 1.30 if(!backToBack_)ntRandom->Fill(entry);
790 yilmaz 1.1 if(et[ir] < 0){
791 yilmaz 1.3 // cout<<"Flipping vector"<<endl;
792 yilmaz 1.1 (*directions)[ir] = false;
793     et[ir] = -et[ir];
794     }else{
795 yilmaz 1.3 // cout<<"Keep vector same"<<endl;
796 yilmaz 1.1 (*directions)[ir] = true;
797     }
798 nart 1.21 // cout<<"Lorentz"<<endl;
799 yilmaz 1.1
800     math::PtEtaPhiMLorentzVector p(et[ir],etaRandom[ir],phiRandom[ir],0);
801     fastjet::PseudoJet jet(p.px(),p.py(),p.pz(),p.energy());
802     fjFakeJets_.push_back(jet);
803     }
804    
805     std::auto_ptr<std::vector<T> > jets(new std::vector<T>() );
806     jets->reserve(fjFakeJets_.size());
807    
808     for (unsigned int ijet=0;ijet<fjFakeJets_.size();++ijet) {
809     // allocate this jet
810     T jet;
811     // get the fastjet jet
812     const fastjet::PseudoJet& fjJet = fjFakeJets_[ijet];
813    
814     // convert them to CandidatePtr vector
815     std::vector<CandidatePtr> constituents =
816     constituents_[ijet];
817    
818     writeSpecific(jet,
819     Particle::LorentzVector(fjJet.px(),
820     fjJet.py(),
821     fjJet.pz(),
822     fjJet.E()),
823     vertex_,
824     constituents, iSetup);
825    
826     // calcuate the jet area
827     double jetArea=0.0;
828     jet.setJetArea (jetArea);
829     if(doPUOffsetCorr_){
830     jet.setPileup(pileUp[ijet]);
831     }else{
832     jet.setPileup (0.0);
833     }
834    
835     // add to the list
836     jets->push_back(jet);
837     }
838    
839     // put the jets in the collection
840 yilmaz 1.28 // iEvent.put(jets,"randomCones");
841     // iEvent.put(directions,"directions");
842 yilmaz 1.1 }
843    
844 yilmaz 1.8 //void JetAlgorithmAnalyzer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup, std::vector<fastjet::PseudoJet>& input )
845 yilmaz 1.1 void JetAlgorithmAnalyzer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup)
846     {
847 yilmaz 1.8 if ( !doAreaFastjet_ && !doRhoFastjet_) {
848     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequence( fjInputs_, *fjJetDefinition_ ) );
849     } else {
850     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequenceArea( fjInputs_, *fjJetDefinition_ , *fjActiveArea_ ) );
851     }
852     fjJets_ = fastjet::sorted_by_pt(fjClusterSeq_->inclusive_jets(jetPtMin_));
853 yilmaz 1.1
854     }
855    
856    
857    
858    
859    
860     ////////////////////////////////////////////////////////////////////////////////
861     // define as cmssw plugin
862     ////////////////////////////////////////////////////////////////////////////////
863    
864     DEFINE_FWK_MODULE(JetAlgorithmAnalyzer);
865