ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/UserCode/CmsHi/JetAnalysis/extras/BackgroundJetProducer.cc
Revision: 1.2
Committed: Sun Jul 11 16:22:38 2010 UTC (14 years, 10 months ago) by yilmaz
Content type: text/plain
Branch: MAIN
CVS Tags: HiForest_V02_85, HiForest_V02_84, HiForest_V02_83, HiForest_V02_82, HiForest_V02_81, HiForest_V02_80, HiForest_V02_79, HiForest_V02_78, HiForest_V02_77, HiForest_V02_76, HiForest_V02_75, HiForest_V02_74, HiForest_V02_73, HiForest_V02_72, HiForest_V02_71, HiForest_V02_70, HiForest_V02_69, HiForest_V02_68, HiForest_V02_67, HiForest_V02_66, HiForest_V02_65, HiForest_V02_64, HiForest_V02_63, HiForest_V02_62, HiForest_V02_61, HiForest_V02_60, HiForest_V02_59, HiForest_V02_58, HiForest_V02_57, HiForest_V02_56, HiForest_V02_55, HiForest_V02_54, 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, QM_2012, HiForest_V02_25, HiForest_V02_24, HiForest_V02_23, HiForest_V02_22, HiForest_V02_21, HiForest_V02_20, HiForest_V02_19, HiForest_V02_18, HiForest_V02_17, HiForest_V02_16, HiForest_V02_15, HiForest_V02_14, HiForest_V02_13, HiForest_V02_12, HiForest_V02_11, HiForest_V02_10, HiForest_V02_09, HiForest_V02_08, HiForest_V02_07, HiForest_V02_06, HiForest_V02_05, HiForest_V02_04, HiForest_V02_03, HiForest_V02_02, HiForest_V02_01, HiForest_V02_00, hi44X_02, hi413_03, hi441_1, hi441_0, hi413_11, hi413_10, hi413_09, hi413_08, hi413_07, hi413_06, hi413_05, hi413_04, hi413_02, hi39X_01, tag_d20110915, cmssw39x_base, cmssw39X_base, HEAD
Branch point for: branch_44x, cmssw39x_branch
Changes since 1.1: +0 -5 lines
Log Message:
double loop fix

File Contents

# User Rev Content
1 yilmaz 1.1 ////////////////////////////////////////////////////////////////////////////////
2     //
3     // BackgroundJetProducer
4     // ------------------
5     //
6     // 04/21/2009 Philipp Schieferdecker <philipp.schieferdecker@cern.ch>
7     ////////////////////////////////////////////////////////////////////////////////
8    
9     #include "RecoJets/JetProducers/plugins/BackgroundJetProducer.h"
10    
11     #include "RecoJets/JetProducers/interface/JetSpecific.h"
12    
13     #include "FWCore/Framework/interface/Event.h"
14     #include "FWCore/Framework/interface/EventSetup.h"
15     #include "FWCore/Framework/interface/ESHandle.h"
16     #include "FWCore/Utilities/interface/CodedException.h"
17     #include "FWCore/MessageLogger/interface/MessageLogger.h"
18     #include "FWCore/Framework/interface/MakerMacros.h"
19     #include "FWCore/Utilities/interface/RandomNumberGenerator.h"
20     #include "FWCore/ServiceRegistry/interface/Service.h"
21    
22     #include "DataFormats/Common/interface/Handle.h"
23     #include "DataFormats/VertexReco/interface/Vertex.h"
24     #include "DataFormats/VertexReco/interface/VertexFwd.h"
25     #include "DataFormats/JetReco/interface/CaloJetCollection.h"
26     #include "DataFormats/JetReco/interface/GenJetCollection.h"
27     #include "DataFormats/JetReco/interface/PFJetCollection.h"
28     #include "DataFormats/JetReco/interface/BasicJetCollection.h"
29     #include "DataFormats/Candidate/interface/CandidateFwd.h"
30     #include "DataFormats/RecoCandidate/interface/RecoChargedCandidate.h"
31     #include "DataFormats/Candidate/interface/LeafCandidate.h"
32     #include "DataFormats/Math/interface/deltaR.h"
33    
34     #include "Geometry/CaloGeometry/interface/CaloGeometry.h"
35     #include "Geometry/Records/interface/CaloGeometryRecord.h"
36    
37     #include "fastjet/SISConePlugin.hh"
38     #include "fastjet/CMSIterativeConePlugin.hh"
39     #include "fastjet/ATLASConePlugin.hh"
40     #include "fastjet/CDFMidPointPlugin.hh"
41    
42     #include "CLHEP/Random/RandomEngine.h"
43    
44     #include <iostream>
45     #include <memory>
46     #include <algorithm>
47     #include <limits>
48     #include <cmath>
49    
50     using namespace std;
51     using namespace edm;
52    
53     static const double pi = 3.14159265358979;
54    
55     CLHEP::HepRandomEngine* randomEngine;
56    
57    
58    
59     ////////////////////////////////////////////////////////////////////////////////
60     // construction / destruction
61     ////////////////////////////////////////////////////////////////////////////////
62    
63     //______________________________________________________________________________
64     BackgroundJetProducer::BackgroundJetProducer(const edm::ParameterSet& iConfig)
65     : VirtualJetProducer( iConfig ),
66     nFill_(5),
67     etaMax_(3),
68     geo(0)
69     {
70     edm::Service<RandomNumberGenerator> rng;
71     randomEngine = &(rng->getEngine());
72    
73     avoidNegative_ = iConfig.getParameter<bool>("avoidNegative");
74    
75     if ( iConfig.exists("UseOnlyVertexTracks") )
76     useOnlyVertexTracks_ = iConfig.getParameter<bool>("UseOnlyVertexTracks");
77     else
78     useOnlyVertexTracks_ = false;
79    
80     if ( iConfig.exists("UseOnlyOnePV") )
81     useOnlyOnePV_ = iConfig.getParameter<bool>("UseOnlyOnePV");
82     else
83     useOnlyOnePV_ = false;
84    
85     if ( iConfig.exists("DrTrVtxMax") )
86     dzTrVtxMax_ = iConfig.getParameter<double>("DzTrVtxMax");
87     else
88     dzTrVtxMax_ = false;
89    
90     produces<std::vector<bool> >("directions");
91    
92     }
93    
94    
95     //______________________________________________________________________________
96     BackgroundJetProducer::~BackgroundJetProducer()
97     {
98     }
99    
100     void BackgroundJetProducer::output(edm::Event & iEvent, edm::EventSetup const& iSetup)
101     {
102     // Write jets and constitutents. Will use fjJets_, inputs_
103     // and fjClusterSeq_
104     switch( jetTypeE ) {
105     case JetType::CaloJet :
106     writeBkgJets<reco::CaloJet>( iEvent, iSetup);
107     break;
108     case JetType::PFJet :
109     writeBkgJets<reco::PFJet>( iEvent, iSetup);
110     break;
111     case JetType::GenJet :
112     writeBkgJets<reco::GenJet>( iEvent, iSetup);
113     break;
114     case JetType::TrackJet :
115     writeBkgJets<reco::TrackJet>( iEvent, iSetup);
116     break;
117     case JetType::BasicJet :
118     writeBkgJets<reco::BasicJet>( iEvent, iSetup);
119     break;
120     default:
121     edm::LogError("InvalidInput") << " invalid jet type in VirtualJetProducer\n";
122     break;
123     };
124    
125     }
126    
127     template< typename T >
128     void BackgroundJetProducer::writeBkgJets( edm::Event & iEvent, edm::EventSetup const& iSetup )
129     {
130     // produce output jet collection
131    
132     using namespace reco;
133    
134     if(!geo){
135     edm::ESHandle<CaloGeometry> pGeo;
136     iSetup.get<CaloGeometryRecord>().get(pGeo);
137     geo = pGeo.product();
138     }
139    
140     std::vector<fastjet::PseudoJet> fjFakeJets_;
141     std::vector<std::vector<reco::CandidatePtr> > constituents_;
142     vector<double> phiRandom;
143     vector<double> etaRandom;
144     vector<double> et;
145     vector<double> pileUp;
146     std::auto_ptr<std::vector<bool> > directions(new std::vector<bool>());
147     directions->reserve(nFill_);
148    
149     phiRandom.reserve(nFill_);
150     etaRandom.reserve(nFill_);
151     et.reserve(nFill_);
152     pileUp.reserve(nFill_);
153    
154     fjFakeJets_.reserve(nFill_);
155     constituents_.reserve(nFill_);
156    
157     constituents_.reserve(nFill_);
158     for(int ijet = 0; ijet < nFill_; ++ijet){
159     vector<reco::CandidatePtr> vec;
160     constituents_.push_back(vec);
161     directions->push_back(true);
162     }
163    
164     for(int ijet = 0; ijet < nFill_; ++ijet){
165     phiRandom[ijet] = 2*pi*randomEngine->flat() - pi;
166     etaRandom[ijet] = 2*etaMax_*randomEngine->flat() - etaMax_;
167     et[ijet] = 0;
168     pileUp[ijet] = 0;
169     }
170    
171     for (vector<fastjet::PseudoJet>::const_iterator input_object = fjInputs_.begin (),
172     fjInputsEnd = fjInputs_.end();
173     input_object != fjInputsEnd; ++input_object) {
174    
175     const reco::CandidatePtr & tower=inputs_[input_object->user_index()];
176     const CaloTower* ctc = dynamic_cast<const CaloTower*>(tower.get());
177     int ieta = ctc->id().ieta();
178     int iphi = ctc->id().iphi();
179     CaloTowerDetId id(ieta,iphi);
180     const GlobalPoint& hitpoint = geo->getPosition(id);
181     double towEta = hitpoint.eta();
182     double towPhi = hitpoint.phi();
183    
184     for(int ir = 0; ir < nFill_; ++ir){
185     if(reco::deltaR(towEta,towPhi,etaRandom[ir],phiRandom[ir]) > rParam_) continue;
186    
187     constituents_[ir].push_back(tower);
188    
189     double towet = tower->et();
190     double putow = subtractor_->getPileUpAtTower(tower);
191     double etadd = towet - putow;
192     if(avoidNegative_ && etadd < 0.) etadd = 0;
193     et[ir] += etadd;
194     pileUp[ir] += towet - etadd;
195     }
196     }
197    
198     cout<<"Start filling jets"<<endl;
199    
200     for(int ir = 0; ir < nFill_; ++ir){
201     if(et[ir] < 0){
202     cout<<"Flipping vector"<<endl;
203     (*directions)[ir] = false;
204     et[ir] = -et[ir];
205     }else{
206     cout<<"Keep vector same"<<endl;
207     (*directions)[ir] = true;
208     }
209     cout<<"Lorentz"<<endl;
210    
211     math::PtEtaPhiMLorentzVector p(et[ir],etaRandom[ir],phiRandom[ir],0);
212     fastjet::PseudoJet jet(p.px(),p.py(),p.pz(),p.energy());
213     fjFakeJets_.push_back(jet);
214     }
215    
216     std::auto_ptr<std::vector<T> > jets(new std::vector<T>() );
217     jets->reserve(fjFakeJets_.size());
218    
219     for (unsigned int ijet=0;ijet<fjFakeJets_.size();++ijet) {
220     // allocate this jet
221     T jet;
222     // get the fastjet jet
223     const fastjet::PseudoJet& fjJet = fjFakeJets_[ijet];
224    
225     // convert them to CandidatePtr vector
226     std::vector<CandidatePtr> constituents =
227     constituents_[ijet];
228    
229     writeSpecific(jet,
230     Particle::LorentzVector(fjJet.px(),
231     fjJet.py(),
232     fjJet.pz(),
233     fjJet.E()),
234     vertex_,
235     constituents, iSetup);
236    
237     // calcuate the jet area
238     double jetArea=0.0;
239     jet.setJetArea (jetArea);
240     if(doPUOffsetCorr_){
241     jet.setPileup(pileUp[ijet]);
242     }else{
243     jet.setPileup (0.0);
244     }
245    
246     // add to the list
247     jets->push_back(jet);
248     }
249    
250     // put the jets in the collection
251     iEvent.put(jets);
252     iEvent.put(directions,"directions");
253     // calculate rho (median pT per unit area, for PU&UE subtraction down the line
254     std::auto_ptr<double> rho(new double(0.0));
255     if (doRhoFastjet_) {
256     fastjet::ClusterSequenceArea const * clusterSequenceWithArea =
257     dynamic_cast<fastjet::ClusterSequenceArea const *> ( &*fjClusterSeq_ );
258     *rho = clusterSequenceWithArea->median_pt_per_unit_area(*fjRangeDef_);
259     }
260     iEvent.put(rho);
261     }
262    
263     void BackgroundJetProducer::runAlgorithm( edm::Event & iEvent, edm::EventSetup const& iSetup){
264     if ( !doAreaFastjet_ && !doRhoFastjet_) {
265     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequence( fjInputs_, *fjJetDefinition_ ) );
266     } else {
267     fjClusterSeq_ = ClusterSequencePtr( new fastjet::ClusterSequenceArea( fjInputs_, *fjJetDefinition_ , *fjActiveArea_ ) );
268     }
269     fjJets_ = fastjet::sorted_by_pt(fjClusterSeq_->inclusive_jets(jetPtMin_));
270    
271     }
272    
273    
274    
275    
276    
277     ////////////////////////////////////////////////////////////////////////////////
278     // define as cmssw plugin
279     ////////////////////////////////////////////////////////////////////////////////
280    
281     DEFINE_FWK_MODULE(BackgroundJetProducer);
282