CMS 3D CMS Logo

HiFJRhoProducer.cc
Go to the documentation of this file.
1 // Original Author: Marta Verweij
2 // Created: Thu, 16 Jul 2015 10:57:12 GMT
3 
4 // system include files
5 #include <memory>
6 #include <string>
7 #include <vector>
8 
9 // user include files
22 
23 // class declaration
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 };
50 
51 using namespace reco;
52 
53 // constructor
55  : jetsToken_(consumes<edm::View<reco::Jet>>(iConfig.getParameter<edm::InputTag>("jetSource"))),
56  nExcl_(iConfig.getParameter<int>("nExcl")),
57  nExcl2_(iConfig.getParameter<int>("nExcl2")),
58  etaMaxExcl_(iConfig.getParameter<double>("etaMaxExcl")),
59  ptMinExcl_(iConfig.getParameter<double>("ptMinExcl")),
60  etaMaxExcl2_(iConfig.getParameter<double>("etaMaxExcl2")),
61  ptMinExcl2_(iConfig.getParameter<double>("ptMinExcl2")),
62  etaRanges_(iConfig.getParameter<std::vector<double>>("etaRanges")) {
63  // register your products
64  produces<std::vector<double>>("mapEtaEdges");
65  produces<std::vector<double>>("mapToRho");
66  produces<std::vector<double>>("mapToRhoM");
67  produces<std::vector<double>>("ptJets");
68  produces<std::vector<double>>("areaJets");
69  produces<std::vector<double>>("etaJets");
70 }
71 
72 // method called for each event to produce the data
74  // get the inputs jets
76  iEvent.getByToken(jetsToken_, jets);
77 
78  int neta = static_cast<int>(etaRanges_.size());
79  auto mapEtaRangesOut = std::make_unique<std::vector<double>>(neta, -999.);
80 
81  for (int ieta = 0; ieta < neta; ieta++) {
82  mapEtaRangesOut->at(ieta) = etaRanges_[ieta];
83  }
84  auto mapToRhoOut = std::make_unique<std::vector<double>>(neta - 1, 1e-6);
85  auto mapToRhoMOut = std::make_unique<std::vector<double>>(neta - 1, 1e-6);
86 
87  int njets = static_cast<int>(jets->size());
88 
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);
95 
96  std::vector<double> rhoVec;
97  rhoVec.reserve(njets);
98  std::vector<double> rhomVec;
99  rhomVec.reserve(njets);
100 
101  int nacc = 0;
102  unsigned int njetsEx = 0;
103  unsigned int njetsEx2 = 0;
104  for (auto const& jet : *jets) {
105  if (njetsEx < nExcl_ and fabs(jet.eta()) < etaMaxExcl_ and jet.pt() > ptMinExcl_) {
106  ++njetsEx;
107  continue;
108  }
109  if (njetsEx2 < nExcl2_ and fabs(jet.eta()) < etaMaxExcl2_ and fabs(jet.eta()) > etaMaxExcl_ and
110  jet.pt() > ptMinExcl2_) {
111  ++njetsEx2;
112  continue;
113  }
114  float pt = jet.pt();
115  float area = jet.jetArea();
116  float eta = jet.eta();
117 
118  if (eta < mapEtaRangesOut->at(0) || eta > mapEtaRangesOut->at(neta - 1))
119  continue;
120  if (area > 0.) {
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);
126  ++nacc;
127  }
128  }
129 
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) {
133  std::vector<double> rhoVecCur;
134  rhoVecCur.reserve(nacc);
135  std::vector<double> rhomVecCur;
136  rhomVecCur.reserve(nacc);
137 
138  double etaMin = mapEtaRangesOut->at(ieta) + radius;
139  double etaMax = mapEtaRangesOut->at(ieta + 1) - radius;
140 
141  for (int i = 0; i < nacc; ++i) {
142  if ((*etaJetsOut)[i] >= etaMin and (*etaJetsOut)[i] < etaMax) {
143  rhoVecCur.push_back(rhoVec[i]);
144  rhomVecCur.push_back(rhomVec[i]);
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);
151  }
152  } // eta ranges
153 
154  iEvent.put(std::move(mapEtaRangesOut), "mapEtaEdges");
155  iEvent.put(std::move(mapToRhoOut), "mapToRho");
156  iEvent.put(std::move(mapToRhoMOut), "mapToRhoM");
157  iEvent.put(std::move(ptJetsOut), "ptJets");
158  iEvent.put(std::move(areaJetsOut), "areaJets");
159  iEvent.put(std::move(etaJetsOut), "etaJets");
160 }
161 
162 double HiFJRhoProducer::calcMd(reco::Jet const& jet) const {
163  // compute md as defined in http://arxiv.org/pdf/1211.2811.pdf
164 
165  // loop over the jet constituents
166  double sum = 0.;
167  for (auto const* daughter : jet.getJetConstituentsQuick()) {
168  if (isPackedCandidate(daughter)) { // packed candidate situation
169  auto part = static_cast<pat::PackedCandidate const*>(daughter);
170  sum += sqrt(part->mass() * part->mass() + part->pt() * part->pt()) - part->pt();
171  } else {
172  auto part = static_cast<reco::PFCandidate const*>(daughter);
173  sum += sqrt(part->mass() * part->mass() + part->pt() * part->pt()) - part->pt();
174  }
175  }
176 
177  return sum;
178 }
179 
181  // check if using packed candidates on the first call and cache the information
182  std::call_once(checkJetCand_, [&]() {
183  if (typeid(pat::PackedCandidate) == typeid(*candidate))
184  usingPackedCand_ = true;
185  else if (typeid(reco::PFCandidate) == typeid(*candidate))
186  usingPackedCand_ = false;
187  else
188  throw cms::Exception("WrongJetCollection", "Jet constituents are not particle flow candidates");
189  });
190  return usingPackedCand_;
191 }
192 
195  desc.add<edm::InputTag>("jetSource", edm::InputTag("kt4PFJets"));
196  desc.add<int>("nExcl", 2);
197  desc.add<double>("etaMaxExcl", 2.);
198  desc.add<double>("ptMinExcl", 20.);
199  desc.add<int>("nExcl2", 2);
200  desc.add<double>("etaMaxExcl2", 2.);
201  desc.add<double>("ptMinExcl2", 20.);
202  desc.add<std::vector<double>>("etaRanges", {});
203  descriptions.add("hiFJRhoProducer", desc);
204 }
205 
206 //--------- method to calculate median ------------------
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
210  if (v.empty()) {
211  return 0.0;
212  }
213  auto n = v.size() / 2;
214  std::nth_element(v.begin(), v.begin() + n, v.end());
215  auto med = v[n];
216  if (!(v.size() & 1)) { // if the set size is even
217  auto max_it = std::max_element(v.begin(), v.begin() + n);
218  med = (*max_it + med) / 2.0;
219  }
220  return med;
221 }
222 
223 // define this as a plug-in
HiFJRhoProducer(const edm::ParameterSet &)
~HiFJRhoProducer() override=default
Base class for all types of Jets.
Definition: Jet.h:20
double calcMedian(std::vector< double > &v) const
const double etaMaxExcl2_
const unsigned int nExcl2_
int iEvent
Definition: GenABIO.cc:224
const double etaMaxExcl_
const int neta
Definition: Jet.py:1
const std::vector< double > etaRanges_
T sqrt(T t)
Definition: SSEVec.h:23
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
bool isPackedCandidate(reco::Candidate const *candidate) const
double calcMd(reco::Jet const &jet) const
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:16
const double ptMinExcl2_
part
Definition: HCALResponse.h:20
void add(std::string const &label, ParameterSetDescription const &psetDescription)
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:41
fixed size matrix
HLT enums.
void produce(edm::StreamID, edm::Event &, edm::EventSetup const &) const override
std::once_flag checkJetCand_
const double ptMinExcl_
const unsigned int nExcl_
def move(src, dest)
Definition: eostools.py:511
const edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_