CMS 3D CMS Logo

HiFJRhoProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: HiJetBackground/HiFJRhoProducer
4 // Class: HiFJRhoProducer
5 //
13 //
14 // Original Author: Marta Verweij
15 // Created: Thu, 16 Jul 2015 10:57:12 GMT
16 //
17 //
18 
19 #include <memory>
20 #include <string>
21 
23 
28 
34 
35 //using namespace edm;
36 using namespace reco;
37 //using namespace pat;
38 
39 //
40 // constants, enums and typedefs
41 //
42 
43 
44 //
45 // static data member definitions
46 //
47 
48 //
49 // constructors and destructor
50 //
52  src_(iConfig.getParameter<edm::InputTag>("jetSource")),
53  nExcl_(iConfig.getParameter<int>("nExcl")),
54  etaMaxExcl_(iConfig.getParameter<double>("etaMaxExcl")),
55  ptMinExcl_(iConfig.getParameter<double>("ptMinExcl")),
56  nExcl2_(iConfig.getParameter<int>("nExcl2")),
57  etaMaxExcl2_(iConfig.getParameter<double>("etaMaxExcl2")),
58  ptMinExcl2_(iConfig.getParameter<double>("ptMinExcl2")),
59  checkJetCand(true),
60  usingPackedCand(false)
61 {
62  jetsToken_ = consumes<edm::View<reco::Jet> >(src_);
63 
64  //register your products
65  produces<std::vector<double > >("mapEtaEdges");
66  produces<std::vector<double > >("mapToRho");
67  produces<std::vector<double > >("mapToRhoM");
68  produces<std::vector<double > >("ptJets");
69  produces<std::vector<double > >("areaJets");
70  produces<std::vector<double > >("etaJets");
71  etaRanges = iConfig.getParameter<std::vector<double> >("etaRanges");
72 
73 }
74 
75 
77 {
78 
79  // do anything here that needs to be done at desctruction time
80  // (e.g. close files, deallocate resources etc.)
81 
82 }
83 
84 // ------------ method called to produce the data ------------
86 {
87  // Get the vector of jets
89  iEvent.getByToken(jetsToken_, jets);
90 
91  int neta = (int)etaRanges.size();
92  auto mapEtaRangesOut = std::make_unique<std::vector<double>>(neta,-999.);
93 
94  for(int ieta = 0; ieta < neta; ieta++){
95  mapEtaRangesOut->at(ieta) = etaRanges[ieta];
96  }
97  auto mapToRhoOut = std::make_unique<std::vector<double>>(neta-1,1e-6);
98  auto mapToRhoMOut = std::make_unique<std::vector<double>>(neta-1,1e-6);
99 
100  int njets = jets->size();
101 
102  auto ptJetsOut = std::make_unique<std::vector<double>>(njets,1e-6);
103  auto areaJetsOut = std::make_unique<std::vector<double>>(njets,1e-6);
104  auto etaJetsOut = std::make_unique<std::vector<double>>(njets,1e-6);
105 
106  double rhoVec[999];
107  double rhomVec[999];
108  double etaVec[999];
109 
110  // int neta = (int)mapEtaRangesOut->size();
111  int nacc = 0;
112  unsigned int njetsEx = 0;
113  unsigned int njetsEx2 = 0;
114  for(auto jet = jets->begin(); jet != jets->end(); ++jet) {
115  if(njetsEx<nExcl_ && fabs(jet->eta())<etaMaxExcl_ && jet->pt()>ptMinExcl_) {
116  njetsEx++;
117  continue;
118  }
119  if(njetsEx2<nExcl2_ && fabs(jet->eta())<etaMaxExcl2_ && fabs(jet->eta())>etaMaxExcl_ && jet->pt()>ptMinExcl2_) {
120  njetsEx2++;
121  continue;
122  }
123  float pt = jet->pt();
124  float area = jet->jetArea();
125  float eta = jet->eta();
126 
127  if(eta<mapEtaRangesOut->at(0) || eta>mapEtaRangesOut->at(neta-1)) continue;
128  if(area>0.) {
129  rhoVec[nacc] = pt/area;
130  rhomVec[nacc] = calcMd(&*jet)/area;
131  etaVec[nacc] = eta;
132  ptJetsOut->at(nacc) = pt;
133  areaJetsOut->at(nacc) = area;
134  etaJetsOut->at(nacc) = eta;
135  ++nacc;
136  }
137  }
138 
139  ptJetsOut->resize(nacc);
140  areaJetsOut->resize(nacc);
141  etaJetsOut->resize(nacc);
142  //calculate rho and rhom in eta ranges
143  double radius = 0.2; //distance kt clusters needs to be from edge
144  for(int ieta = 0; ieta<(neta-1); ieta++) {
145  std::vector<double> rhoVecCur;
146  std::vector<double> rhomVecCur;
147 
148  double etaMin = mapEtaRangesOut->at(ieta)+radius;
149  double etaMax = mapEtaRangesOut->at(ieta+1)-radius;
150 
151  int naccCur = 0 ;
152  double rhoCurSum = 0.;
153  double rhomCurSum = 0.;
154  for(int i = 0; i<nacc; i++) {
155  if(etaVec[i]>=etaMin && etaVec[i]<etaMax) {
156  rhoVecCur.push_back(rhoVec[i]);
157  rhomVecCur.push_back(rhomVec[i]);
158 
159  rhoCurSum += rhoVec[i];
160  rhomCurSum += rhomVec[i];
161  ++naccCur;
162  }//eta selection
163  }//accepted jet loop
164 
165  if(naccCur>0) {
166  double rhoCur = calcMedian(rhoVecCur);
167  double rhomCur = calcMedian(rhomVecCur);
168  mapToRhoOut->at(ieta) = rhoCur;
169  mapToRhoMOut->at(ieta) = rhomCur;
170  }
171  }//eta ranges
172 
173  iEvent.put(std::move(mapEtaRangesOut),"mapEtaEdges");
174  iEvent.put(std::move(mapToRhoOut),"mapToRho");
175  iEvent.put(std::move(mapToRhoMOut),"mapToRhoM");
176 
177  iEvent.put(std::move(ptJetsOut),"ptJets");
178  iEvent.put(std::move(areaJetsOut),"areaJets");
179  iEvent.put(std::move(etaJetsOut),"etaJets");
180 
181  return;
182 }
183 
184 // ------------ method called once each stream before processing any runs, lumis or events ------------
185 void
187 {
188 }
189 
190 // ------------ method called once each stream after processing all runs, lumis and events ------------
191 void
193 }
194 
196  //
197  //get md as defined in http://arxiv.org/pdf/1211.2811.pdf
198  //
199 
200  //Loop over the jet constituents
201  double sum = 0.;
202  for(auto daughter : jet->getJetConstituentsQuick()){
203  if(isPackedCandidate(daughter)){ //packed candidate situation
204  auto part = static_cast<const pat::PackedCandidate*>(daughter);
205  sum += sqrt(part->mass()*part->mass() + part->pt()*part->pt()) - part->pt();
206  } else {
207  auto part = static_cast<const reco::PFCandidate*>(daughter);
208  sum += sqrt(part->mass()*part->mass() + part->pt()*part->pt()) - part->pt();
209  }
210  }
211 
212  return sum;
213 }
214 
216  if(checkJetCand) {
217  if(typeid(pat::PackedCandidate)==typeid(*candidate)) usingPackedCand = true;
218  else if(typeid(reco::PFCandidate)==typeid(*candidate)) usingPackedCand = false;
219  else throw cms::Exception("WrongJetCollection", "Jet constituents are not particle flow candidates");
220  checkJetCand = false;
221  }
222  return usingPackedCand;
223 }
224 
227  desc.add<edm::InputTag>("jetSource",edm::InputTag("kt4PFJets"));
228  desc.add<int>("nExcl", 2);
229  desc.add<double>("etaMaxExcl",2.);
230  desc.add<double>("ptMinExcl",20.);
231  desc.add<int>("nExcl2", 2);
232  desc.add<double>("etaMaxExcl2",2.);
233  desc.add<double>("ptMinExcl2",20.);
234  desc.add<std::vector<double> >("etaRanges");
235  descriptions.add("hiFJRhoProducer",desc);
236 }
237 
238 
239 //--------- method to calculate median ------------------
240 double HiFJRhoProducer::calcMedian(std::vector<double> &v)
241 {
242  //post-condition: After returning, the elements in v may be reordered and the resulting order is implementation defined.
243  //works for even and odd collections
244  if(v.empty()) {
245  return 0.0;
246  }
247  auto n = v.size() / 2;
248  std::nth_element(v.begin(), v.begin()+n, v.end());
249  auto med = v[n];
250  if(!(v.size() & 1)) { //If the set size is even
251  auto max_it = std::max_element(v.begin(), v.begin()+n);
252  med = (*max_it + med) / 2.0;
253  }
254  return med;
255 }
256 
257 
258 //define this as a plug-in
T getParameter(std::string const &) const
HiFJRhoProducer(const edm::ParameterSet &)
int i
Definition: DBlmapReader.cc:9
OrphanHandle< PROD > put(std::unique_ptr< PROD > product)
Put a new product.
Definition: Event.h:122
double calcMedian(std::vector< double > &v)
bool getByToken(EDGetToken token, Handle< PROD > &result) const
Definition: Event.h:457
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
edm::InputTag src_
Base class for all types of Jets.
Definition: Jet.h:20
std::vector< double > etaRanges
virtual void beginStream(edm::StreamID) override
bool isPackedCandidate(const reco::Candidate *candidate)
int iEvent
Definition: GenABIO.cc:230
T sqrt(T t)
Definition: SSEVec.h:18
static void fillDescriptions(edm::ConfigurationDescriptions &descriptions)
vector< PseudoJet > jets
unsigned int nExcl_
ParameterDescriptionBase * add(U const &iLabel, T const &value)
virtual void produce(edm::Event &, const edm::EventSetup &) override
double calcMd(const reco::Jet *jet)
part
Definition: HCALResponse.h:20
void add(std::string const &label, ParameterSetDescription const &psetDescription)
edm::EDGetTokenT< edm::View< reco::Jet > > jetsToken_
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:39
fixed size matrix
HLT enums.
virtual void endStream() override
virtual std::vector< const reco::Candidate * > getJetConstituentsQuick() const
quick list of constituents
def move(src, dest)
Definition: eostools.py:510
unsigned int nExcl2_