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