1- // -*- C++ -*-
2- //
3- // Package: HiJetBackground/HiFJRhoProducer
4- // Class: HiFJRhoProducer
5- //
6- /* *\class HiFJRhoProducer HiFJRhoProducer.cc HiJetBackground/HiFJRhoProducer/plugins/HiFJRhoProducer.cc
7-
8- Description: [one line class summary]
9-
10- Implementation:
11- [Notes on implementation]
12- */
13- //
141// Original Author: Marta Verweij
152// Created: Thu, 16 Jul 2015 10:57:12 GMT
16- //
17- //
183
4+ // system include files
195#include < memory>
206#include < string>
7+ #include < vector>
218
22- #include " RecoHI/HiJetAlgos/plugins/HiFJRhoProducer.h"
23-
24- #include " FWCore/ParameterSet/interface/ParameterSet.h"
25- #include " FWCore/Framework/interface/ESHandle.h"
26- #include " FWCore/Framework/interface/MakerMacros.h"
27- #include " FWCore/Framework/interface/ConsumesCollector.h"
28-
29- #include " DataFormats/Common/interface/View.h"
9+ // user include files
3010#include " DataFormats/Common/interface/Handle.h"
31- #include " DataFormats/PatCandidates/interface/PackedCandidate.h"
11+ #include " DataFormats/Common/interface/View.h"
12+ #include " DataFormats/JetReco/interface/Jet.h"
3213#include " DataFormats/ParticleFlowCandidate/interface/PFCandidate.h"
3314#include " DataFormats/PatCandidates/interface/Jet.h"
15+ #include " DataFormats/PatCandidates/interface/PackedCandidate.h"
16+ #include " FWCore/Framework/interface/Event.h"
17+ #include " FWCore/Framework/interface/global/EDProducer.h"
18+ #include " FWCore/ParameterSet/interface/ConfigurationDescriptions.h"
19+ #include " FWCore/ParameterSet/interface/ParameterSet.h"
20+ #include " FWCore/ParameterSet/interface/ParameterSetDescription.h"
21+ #include " FWCore/Utilities/interface/StreamID.h"
22+
23+ // class declaration
24+ class HiFJRhoProducer : public edm ::global::EDProducer<> {
25+ public:
26+ explicit HiFJRhoProducer (const edm::ParameterSet&);
27+ ~HiFJRhoProducer () override = default ;
28+
29+ static void fillDescriptions (edm::ConfigurationDescriptions& descriptions);
30+
31+ private:
32+ void produce (edm::StreamID, edm::Event&, edm::EventSetup const &) const override ;
33+
34+ double calcMedian (std::vector<double >& v) const ;
35+ double calcMd (reco::Jet const & jet) const ;
36+ bool isPackedCandidate (reco::Candidate const * candidate) const ;
37+
38+ // members
39+ const edm::EDGetTokenT<edm::View<reco::Jet>> jetsToken_; // input kt jet source
40+ const unsigned int nExcl_; // number of leading jets to exclude
41+ const unsigned int nExcl2_; // number of leading jets to exclude in 2nd eta region
42+ const double etaMaxExcl_; // max eta for jets to exclude
43+ const double ptMinExcl_; // min pt for excluded jets
44+ const double etaMaxExcl2_; // max eta for jets to exclude in 2nd eta region
45+ const double ptMinExcl2_; // min pt for excluded jets in 2nd eta region
46+ const std::vector<double > etaRanges_; // eta boundaries for rho calculation regions
47+ mutable std::once_flag checkJetCand_; // check if using packed candidates and cache the information
48+ mutable bool usingPackedCand_ = false ;
49+ };
3450
35- // using namespace edm;
3651using namespace reco ;
37- // using namespace pat;
3852
39- //
40- // constants, enums and typedefs
41- //
42-
43- //
44- // static data member definitions
45- //
46-
47- //
48- // constructors and destructor
49- //
53+ // constructor
5054HiFJRhoProducer::HiFJRhoProducer (const edm::ParameterSet& iConfig)
51- : src_( iConfig.getParameter<edm::InputTag>(" jetSource" )),
55+ : jetsToken_(consumes<edm::View<reco::Jet>>( iConfig.getParameter<edm::InputTag>(" jetSource" ) )),
5256 nExcl_(iConfig.getParameter<int >(" nExcl" )),
57+ nExcl2_(iConfig.getParameter<int >(" nExcl2" )),
5358 etaMaxExcl_(iConfig.getParameter<double >(" etaMaxExcl" )),
5459 ptMinExcl_(iConfig.getParameter<double >(" ptMinExcl" )),
55- nExcl2_(iConfig.getParameter<int >(" nExcl2" )),
5660 etaMaxExcl2_(iConfig.getParameter<double >(" etaMaxExcl2" )),
5761 ptMinExcl2_(iConfig.getParameter<double >(" ptMinExcl2" )),
58- checkJetCand(true ),
59- usingPackedCand(false ) {
60- jetsToken_ = consumes<edm::View<reco::Jet>>(src_);
61-
62- // register your products
62+ etaRanges_(iConfig.getParameter<std::vector<double >>(" etaRanges" )) {
63+ // register your products
6364 produces<std::vector<double >>(" mapEtaEdges" );
6465 produces<std::vector<double >>(" mapToRho" );
6566 produces<std::vector<double >>(" mapToRhoM" );
6667 produces<std::vector<double >>(" ptJets" );
6768 produces<std::vector<double >>(" areaJets" );
6869 produces<std::vector<double >>(" etaJets" );
69- etaRanges = iConfig.getParameter <std::vector<double >>(" etaRanges" );
70- }
71-
72- HiFJRhoProducer::~HiFJRhoProducer () {
73- // do anything here that needs to be done at desctruction time
74- // (e.g. close files, deallocate resources etc.)
7570}
7671
77- // ------------ method called to produce the data ------------
78- void HiFJRhoProducer::produce (edm::Event& iEvent, const edm::EventSetup& iSetup) {
79- // Get the vector of jets
72+ // method called for each event to produce the data
73+ void HiFJRhoProducer::produce (edm::StreamID, edm:: Event& iEvent, edm::EventSetup const & iSetup) const {
74+ // get the inputs jets
8075 edm::Handle<edm::View<reco::Jet>> jets;
8176 iEvent.getByToken (jetsToken_, jets);
8277
83- int neta = ( int )etaRanges .size ();
78+ int neta = static_cast < int >(etaRanges_ .size () );
8479 auto mapEtaRangesOut = std::make_unique<std::vector<double >>(neta, -999 .);
8580
8681 for (int ieta = 0 ; ieta < neta; ieta++) {
87- mapEtaRangesOut->at (ieta) = etaRanges [ieta];
82+ mapEtaRangesOut->at (ieta) = etaRanges_ [ieta];
8883 }
8984 auto mapToRhoOut = std::make_unique<std::vector<double >>(neta - 1 , 1e-6 );
9085 auto mapToRhoMOut = std::make_unique<std::vector<double >>(neta - 1 , 1e-6 );
9186
92- int njets = jets->size ();
87+ int njets = static_cast < int >( jets->size () );
9388
94- auto ptJetsOut = std::make_unique<std::vector<double >>(njets, 1e-6 );
95- auto areaJetsOut = std::make_unique<std::vector<double >>(njets, 1e-6 );
96- auto etaJetsOut = std::make_unique<std::vector<double >>(njets, 1e-6 );
89+ auto ptJetsOut = std::make_unique<std::vector<double >>();
90+ ptJetsOut->reserve (njets);
91+ auto areaJetsOut = std::make_unique<std::vector<double >>();
92+ areaJetsOut->reserve (njets);
93+ auto etaJetsOut = std::make_unique<std::vector<double >>();
94+ etaJetsOut->reserve (njets);
9795
98- double rhoVec[999 ];
99- double rhomVec[999 ];
100- double etaVec[999 ];
96+ std::vector<double > rhoVec;
97+ rhoVec.reserve (njets);
98+ std::vector<double > rhomVec;
99+ rhomVec.reserve (njets);
101100
102- // int neta = (int)mapEtaRangesOut->size();
103101 int nacc = 0 ;
104102 unsigned int njetsEx = 0 ;
105103 unsigned int njetsEx2 = 0 ;
106- for (auto jet = jets-> begin (); jet != jets-> end (); ++jet ) {
107- if (njetsEx < nExcl_ && fabs (jet-> eta ()) < etaMaxExcl_ && jet-> pt () > ptMinExcl_) {
108- njetsEx++ ;
104+ for (auto const & jet : * jets) {
105+ if (njetsEx < nExcl_ and fabs (jet. eta ()) < etaMaxExcl_ and jet. pt () > ptMinExcl_) {
106+ ++njetsEx ;
109107 continue ;
110108 }
111- if (njetsEx2 < nExcl2_ && fabs (jet-> eta ()) < etaMaxExcl2_ && fabs (jet-> eta ()) > etaMaxExcl_ &&
112- jet-> pt () > ptMinExcl2_) {
113- njetsEx2++ ;
109+ if (njetsEx2 < nExcl2_ and fabs (jet. eta ()) < etaMaxExcl2_ and fabs (jet. eta ()) > etaMaxExcl_ and
110+ jet. pt () > ptMinExcl2_) {
111+ ++njetsEx2 ;
114112 continue ;
115113 }
116- float pt = jet-> pt ();
117- float area = jet-> jetArea ();
118- float eta = jet-> eta ();
114+ float pt = jet. pt ();
115+ float area = jet. jetArea ();
116+ float eta = jet. eta ();
119117
120118 if (eta < mapEtaRangesOut->at (0 ) || eta > mapEtaRangesOut->at (neta - 1 ))
121119 continue ;
122120 if (area > 0 .) {
123- rhoVec[nacc] = pt / area;
124- rhomVec[nacc] = calcMd (&*jet) / area;
125- etaVec[nacc] = eta;
126- ptJetsOut->at (nacc) = pt;
127- areaJetsOut->at (nacc) = area;
128- etaJetsOut->at (nacc) = eta;
121+ rhoVec.push_back (pt / area);
122+ rhomVec.push_back (calcMd (jet) / area);
123+ ptJetsOut->push_back (pt);
124+ areaJetsOut->push_back (area);
125+ etaJetsOut->push_back (eta);
129126 ++nacc;
130127 }
131128 }
132129
133- ptJetsOut->resize (nacc);
134- areaJetsOut->resize (nacc);
135- etaJetsOut->resize (nacc);
136- // calculate rho and rhom in eta ranges
137- double radius = 0.2 ; // distance kt clusters needs to be from edge
138- for (int ieta = 0 ; ieta < (neta - 1 ); ieta++) {
130+ // calculate rho and rhom in eta ranges
131+ const double radius = 0.2 ; // distance kt clusters needs to be from edge
132+ for (int ieta = 0 ; ieta < (neta - 1 ); ++ieta) {
139133 std::vector<double > rhoVecCur;
134+ rhoVecCur.reserve (nacc);
140135 std::vector<double > rhomVecCur;
136+ rhomVecCur.reserve (nacc);
141137
142138 double etaMin = mapEtaRangesOut->at (ieta) + radius;
143139 double etaMax = mapEtaRangesOut->at (ieta + 1 ) - radius;
144140
145- int naccCur = 0 ;
146- for (int i = 0 ; i < nacc; i++) {
147- if (etaVec[i] >= etaMin && etaVec[i] < etaMax) {
141+ for (int i = 0 ; i < nacc; ++i) {
142+ if ((*etaJetsOut)[i] >= etaMin and (*etaJetsOut)[i] < etaMax) {
148143 rhoVecCur.push_back (rhoVec[i]);
149144 rhomVecCur.push_back (rhomVec[i]);
150- ++naccCur;
151- } // eta selection
152- } // accepted jet loop
153-
154- if (naccCur > 0 ) {
155- double rhoCur = calcMedian (rhoVecCur);
156- double rhomCur = calcMedian (rhomVecCur);
157- mapToRhoOut->at (ieta) = rhoCur;
158- mapToRhoMOut->at (ieta) = rhomCur;
145+ } // eta selection
146+ } // accepted jet loop
147+
148+ if (not rhoVecCur.empty ()) {
149+ mapToRhoOut->at (ieta) = calcMedian (rhoVecCur);
150+ mapToRhoMOut->at (ieta) = calcMedian (rhomVecCur);
159151 }
160- } // eta ranges
152+ } // eta ranges
161153
162154 iEvent.put (std::move (mapEtaRangesOut), " mapEtaEdges" );
163155 iEvent.put (std::move (mapToRhoOut), " mapToRho" );
164156 iEvent.put (std::move (mapToRhoMOut), " mapToRhoM" );
165-
166157 iEvent.put (std::move (ptJetsOut), " ptJets" );
167158 iEvent.put (std::move (areaJetsOut), " areaJets" );
168159 iEvent.put (std::move (etaJetsOut), " etaJets" );
169-
170- return ;
171160}
172161
173- // ------------ method called once each stream before processing any runs, lumis or events ------------
174- void HiFJRhoProducer::beginStream (edm::StreamID) {}
175-
176- // ------------ method called once each stream after processing all runs, lumis and events ------------
177- void HiFJRhoProducer::endStream () {}
178-
179- double HiFJRhoProducer::calcMd (const reco::Jet* jet) {
180- //
181- // get md as defined in http://arxiv.org/pdf/1211.2811.pdf
182- //
162+ double HiFJRhoProducer::calcMd (reco::Jet const & jet) const {
163+ // compute md as defined in http://arxiv.org/pdf/1211.2811.pdf
183164
184- // Loop over the jet constituents
165+ // loop over the jet constituents
185166 double sum = 0 .;
186- for (auto daughter : jet-> getJetConstituentsQuick ()) {
187- if (isPackedCandidate (daughter)) { // packed candidate situation
188- auto part = static_cast <const pat::PackedCandidate*>(daughter);
167+ for (auto const * daughter : jet. getJetConstituentsQuick ()) {
168+ if (isPackedCandidate (daughter)) { // packed candidate situation
169+ auto part = static_cast <pat::PackedCandidate const *>(daughter);
189170 sum += sqrt (part->mass () * part->mass () + part->pt () * part->pt ()) - part->pt ();
190171 } else {
191- auto part = static_cast <const reco::PFCandidate*>(daughter);
172+ auto part = static_cast <reco::PFCandidate const *>(daughter);
192173 sum += sqrt (part->mass () * part->mass () + part->pt () * part->pt ()) - part->pt ();
193174 }
194175 }
195176
196177 return sum;
197178}
198179
199- bool HiFJRhoProducer::isPackedCandidate (const reco::Candidate* candidate) {
200- if (checkJetCand) {
180+ bool HiFJRhoProducer::isPackedCandidate (const reco::Candidate* candidate) const {
181+ // check if using packed candidates on the first call and cache the information
182+ std::call_once (checkJetCand_, [&]() {
201183 if (typeid (pat::PackedCandidate) == typeid (*candidate))
202- usingPackedCand = true ;
184+ usingPackedCand_ = true ;
203185 else if (typeid (reco::PFCandidate) == typeid (*candidate))
204- usingPackedCand = false ;
186+ usingPackedCand_ = false ;
205187 else
206188 throw cms::Exception (" WrongJetCollection" , " Jet constituents are not particle flow candidates" );
207- checkJetCand = false ;
208- }
209- return usingPackedCand;
189+ });
190+ return usingPackedCand_;
210191}
211192
212193void HiFJRhoProducer::fillDescriptions (edm::ConfigurationDescriptions& descriptions) {
@@ -223,21 +204,22 @@ void HiFJRhoProducer::fillDescriptions(edm::ConfigurationDescriptions& descripti
223204}
224205
225206// --------- method to calculate median ------------------
226- double HiFJRhoProducer::calcMedian (std::vector<double >& v) {
227- // post-condition: After returning, the elements in v may be reordered and the resulting order is implementation defined.
228- // works for even and odd collections
207+ double HiFJRhoProducer::calcMedian (std::vector<double >& v) const {
208+ // post-condition: After returning, the elements in v may be reordered and the resulting order is implementation defined.
209+ // works for even and odd collections
229210 if (v.empty ()) {
230211 return 0.0 ;
231212 }
232213 auto n = v.size () / 2 ;
233214 std::nth_element (v.begin (), v.begin () + n, v.end ());
234215 auto med = v[n];
235- if (!(v.size () & 1 )) { // If the set size is even
216+ if (!(v.size () & 1 )) { // if the set size is even
236217 auto max_it = std::max_element (v.begin (), v.begin () + n);
237218 med = (*max_it + med) / 2.0 ;
238219 }
239220 return med;
240221}
241222
242- // define this as a plug-in
223+ // define this as a plug-in
224+ #include " FWCore/Framework/interface/MakerMacros.h"
243225DEFINE_FWK_MODULE (HiFJRhoProducer);
0 commit comments