CMS 3D CMS Logo

PileUpSubtractor.cc
Go to the documentation of this file.
1 
3 
8 
14 
15 #include <map>
16 #include <memory>
17 
18 using namespace std;
19 
21  geo_ = nullptr;
22  doAreaFastjet_ = iConfig.getParameter<bool>("doAreaFastjet");
23  doRhoFastjet_ = iConfig.getParameter<bool>("doRhoFastjet");
24  nSigmaPU_ = iConfig.getParameter<double>("nSigmaPU");
25  radiusPU_ = iConfig.getParameter<double>("radiusPU");
26  jetPtMin_ = iConfig.getParameter<double>("jetPtMin");
27  puPtMin_ = iConfig.getParameter<double>("puPtMin");
28  ghostEtaMax = iConfig.getParameter<double>("Ghost_EtaMax");
29  activeAreaRepeats = iConfig.getParameter<int>("Active_Area_Repeats");
30  ghostArea = iConfig.getParameter<double>("GhostArea");
31 
32  if (doAreaFastjet_ || doRhoFastjet_) {
33  fjActiveArea_ = std::make_shared<fastjet::ActiveAreaSpec>(ghostEtaMax, activeAreaRepeats, ghostArea);
34  if ((ghostEtaMax < 0) || (activeAreaRepeats < 0) || (ghostArea < 0))
35  throw cms::Exception("doAreaFastjet or doRhoFastjet")
36  << "Parameters ghostEtaMax, activeAreaRepeats or ghostArea for doAreaFastjet/doRhoFastjet are not defined."
37  << std::endl;
38  }
39 }
40 
42  std::vector<fastjet::PseudoJet>& towers,
43  std::vector<fastjet::PseudoJet>& output) {
44  inputs_ = &input;
45  fjInputs_ = &towers;
46  fjJets_ = &output;
47  fjOriginalInputs_ = (*fjInputs_);
48  for (unsigned int i = 0; i < fjInputs_->size(); ++i) {
49  fjOriginalInputs_[i].set_user_index((*fjInputs_)[i].user_index());
50  }
51 }
52 
54  fjJetDefinition_ = std::make_shared<fastjet::JetDefinition>(*jetDef);
55 }
56 
58  LogDebug("PileUpSubtractor") << "The subtractor setting up geometry...\n";
59 
60  if (geo_ == nullptr) {
62  iSetup.get<CaloGeometryRecord>().get(pG);
63  geo_ = pG.product();
64  std::vector<DetId> alldid = geo_->getValidDetIds();
65 
66  int ietaold = -10000;
67  ietamax_ = -10000;
68  ietamin_ = 10000;
69  for (std::vector<DetId>::const_iterator did = alldid.begin(); did != alldid.end(); did++) {
70  if ((*did).det() == DetId::Hcal) {
71  HcalDetId hid = HcalDetId(*did);
72  if ((hid).depth() == 1) {
73  allgeomid_.push_back(*did);
74 
75  if ((hid).ieta() != ietaold) {
76  ietaold = (hid).ieta();
77  geomtowers_[(hid).ieta()] = 1;
78  if ((hid).ieta() > ietamax_)
79  ietamax_ = (hid).ieta();
80  if ((hid).ieta() < ietamin_)
81  ietamin_ = (hid).ieta();
82  } else {
83  geomtowers_[(hid).ieta()]++;
84  }
85  }
86  }
87  }
88  }
89 
90  for (int i = ietamin_; i < ietamax_ + 1; i++) {
91  ntowersWithJets_[i] = 0;
92  }
93 }
94 
95 //
96 // Calculate mean E and sigma from jet collection "coll".
97 //
98 void PileUpSubtractor::calculatePedestal(vector<fastjet::PseudoJet> const& coll) {
99  LogDebug("PileUpSubtractor") << "The subtractor calculating pedestals...\n";
100  map<int, double> emean2;
101  map<int, int> ntowers;
102 
103  int ietaold = -10000;
104  int ieta0 = -100;
105 
106  // Initial values for emean_, emean2, esigma_, ntowers
107 
108  for (int i = ietamin_; i < ietamax_ + 1; i++) {
109  emean_[i] = 0.;
110  emean2[i] = 0.;
111  esigma_[i] = 0.;
112  ntowers[i] = 0;
113  }
114 
115  for (vector<fastjet::PseudoJet>::const_iterator input_object = coll.begin(), fjInputsEnd = coll.end();
116  input_object != fjInputsEnd;
117  ++input_object) {
118  const reco::CandidatePtr& originalTower = (*inputs_)[input_object->user_index()];
119  ieta0 = ieta(originalTower);
120  double Original_Et = originalTower->et();
121  if (ieta0 - ietaold != 0) {
122  emean_[ieta0] = emean_[ieta0] + Original_Et;
123  emean2[ieta0] = emean2[ieta0] + Original_Et * Original_Et;
124  ntowers[ieta0] = 1;
125  ietaold = ieta0;
126  } else {
127  emean_[ieta0] = emean_[ieta0] + Original_Et;
128  emean2[ieta0] = emean2[ieta0] + Original_Et * Original_Et;
129  ntowers[ieta0]++;
130  }
131  }
132 
133  for (map<int, int>::const_iterator gt = geomtowers_.begin(); gt != geomtowers_.end(); gt++) {
134  int it = (*gt).first;
135 
136  double e1 = (*(emean_.find(it))).second;
137  double e2 = (*emean2.find(it)).second;
138  int nt = (*gt).second - (*(ntowersWithJets_.find(it))).second;
139 
140  LogDebug("PileUpSubtractor") << " ieta : " << it << " number of towers : " << nt << " e1 : " << e1 << " e2 : " << e2
141  << "\n";
142  if (nt > 0) {
143  emean_[it] = e1 / nt;
144  double eee = e2 / nt - e1 * e1 / (nt * nt);
145  if (eee < 0.)
146  eee = 0.;
147  esigma_[it] = nSigmaPU_ * sqrt(eee);
148  } else {
149  emean_[it] = 0.;
150  esigma_[it] = 0.;
151  }
152  LogDebug("PileUpSubtractor") << " ieta : " << it << " Pedestals : " << emean_[it] << " " << esigma_[it] << "\n";
153  }
154 }
155 
156 //
157 // Subtract mean and sigma from fjInputs_
158 //
159 void PileUpSubtractor::subtractPedestal(vector<fastjet::PseudoJet>& coll) {
160  LogDebug("PileUpSubtractor") << "The subtractor subtracting pedestals...\n";
161 
162  int it = -100;
163  for (vector<fastjet::PseudoJet>::iterator input_object = coll.begin(), fjInputsEnd = coll.end();
164  input_object != fjInputsEnd;
165  ++input_object) {
166  reco::CandidatePtr const& itow = (*inputs_)[input_object->user_index()];
167 
168  it = ieta(itow);
169 
170  double etnew = itow->et() - (*(emean_.find(it))).second - (*(esigma_.find(it))).second;
171  float mScale = etnew / input_object->Et();
172  if (etnew < 0.)
173  mScale = 0.;
174 
175  math::XYZTLorentzVectorD towP4(input_object->px() * mScale,
176  input_object->py() * mScale,
177  input_object->pz() * mScale,
178  input_object->e() * mScale);
179 
180  int index = input_object->user_index();
181  input_object->reset_momentum(towP4.px(), towP4.py(), towP4.pz(), towP4.energy());
182  input_object->set_user_index(index);
183  }
184 }
185 
186 void PileUpSubtractor::calculateOrphanInput(vector<fastjet::PseudoJet>& orphanInput) {
187  LogDebug("PileUpSubtractor") << "The subtractor calculating orphan input...\n";
188 
189  (*fjInputs_) = fjOriginalInputs_;
190 
191  vector<int> jettowers; // vector of towers indexed by "user_index"
192  vector<pair<int, int> > excludedTowers; // vector of excluded ieta, iphi values
193 
194  vector<fastjet::PseudoJet>::iterator pseudojetTMP = fjJets_->begin(), fjJetsEnd = fjJets_->end();
195  for (; pseudojetTMP != fjJetsEnd; ++pseudojetTMP) {
196  if (pseudojetTMP->perp() < puPtMin_)
197  continue;
198 
199  // find towers within radiusPU_ of this jet
200  for (vector<HcalDetId>::const_iterator im = allgeomid_.begin(); im != allgeomid_.end(); im++) {
201  double dr = reco::deltaR(geo_->getPosition((DetId)(*im)), (*pseudojetTMP));
202  vector<pair<int, int> >::const_iterator exclude =
203  find(excludedTowers.begin(), excludedTowers.end(), pair<int, int>(im->ieta(), im->iphi()));
204  if (dr < radiusPU_ && exclude == excludedTowers.end()) {
205  ntowersWithJets_[(*im).ieta()]++;
206  excludedTowers.push_back(pair<int, int>(im->ieta(), im->iphi()));
207  }
208  }
209  vector<fastjet::PseudoJet>::const_iterator it = fjInputs_->begin(), fjInputsEnd = fjInputs_->end();
210 
211  for (; it != fjInputsEnd; ++it) {
212  int index = it->user_index();
213  int ie = ieta((*inputs_)[index]);
214  int ip = iphi((*inputs_)[index]);
215  vector<pair<int, int> >::const_iterator exclude =
216  find(excludedTowers.begin(), excludedTowers.end(), pair<int, int>(ie, ip));
217  if (exclude != excludedTowers.end()) {
218  jettowers.push_back(index);
219  } //dr < radiusPU_
220  } // initial input collection
221  } // pseudojets
222 
223  //
224  // Create a new collections from the towers not included in jets
225  //
226  for (vector<fastjet::PseudoJet>::const_iterator it = fjInputs_->begin(), fjInputsEnd = fjInputs_->end();
227  it != fjInputsEnd;
228  ++it) {
229  int index = it->user_index();
230  vector<int>::const_iterator itjet = find(jettowers.begin(), jettowers.end(), index);
231  if (itjet == jettowers.end()) {
232  const reco::CandidatePtr& originalTower = (*inputs_)[index];
233  fastjet::PseudoJet orphan(originalTower->px(), originalTower->py(), originalTower->pz(), originalTower->energy());
234  orphan.set_user_index(index);
235 
236  orphanInput.push_back(orphan);
237  }
238  }
239 }
240 
242  LogDebug("PileUpSubtractor") << "The subtractor correcting jets...\n";
243  jetOffset_.clear();
244  using namespace reco;
245 
246  //
247  // Reestimate energy of jet (energy of jet with initial map)
248  //
249  jetOffset_.reserve(fjJets_->size());
250  vector<fastjet::PseudoJet>::iterator pseudojetTMP = fjJets_->begin(), jetsEnd = fjJets_->end();
251  for (; pseudojetTMP != jetsEnd; ++pseudojetTMP) {
252  int ijet = pseudojetTMP - fjJets_->begin();
253  jetOffset_[ijet] = 0;
254 
255  std::vector<fastjet::PseudoJet> towers = fastjet::sorted_by_pt(pseudojetTMP->constituents());
256  double newjetet = 0.;
257  for (vector<fastjet::PseudoJet>::const_iterator ito = towers.begin(), towEnd = towers.end(); ito != towEnd; ++ito) {
258  const reco::CandidatePtr& originalTower = (*inputs_)[ito->user_index()];
259  int it = ieta(originalTower);
260  double Original_Et = originalTower->et();
261  double etnew = Original_Et - (*emean_.find(it)).second - (*esigma_.find(it)).second;
262  if (etnew < 0.)
263  etnew = 0;
264  newjetet = newjetet + etnew;
265  jetOffset_[ijet] += Original_Et - etnew;
266  }
267  double mScale = newjetet / pseudojetTMP->Et();
268  LogDebug("PileUpSubtractor") << "pseudojetTMP->Et() : " << pseudojetTMP->Et() << '\n';
269  LogDebug("PileUpSubtractor") << "newjetet : " << newjetet << '\n';
270  LogDebug("PileUpSubtractor") << "jetOffset_[ijet] : " << jetOffset_[ijet] << '\n';
271  LogDebug("PileUpSubtractor") << "pseudojetTMP->Et() - jetOffset_[ijet] : " << pseudojetTMP->Et() - jetOffset_[ijet]
272  << '\n';
273  LogDebug("PileUpSubtractor") << "Scale is : " << mScale << '\n';
274  int cshist = pseudojetTMP->cluster_hist_index();
275  pseudojetTMP->reset_momentum(pseudojetTMP->px() * mScale,
276  pseudojetTMP->py() * mScale,
277  pseudojetTMP->pz() * mScale,
278  pseudojetTMP->e() * mScale);
279  pseudojetTMP->set_cluster_hist_index(cshist);
280  }
281 }
282 
283 double PileUpSubtractor::getCone(double cone, double eta, double phi, double& et, double& pu) {
284  pu = 0;
285 
286  for (vector<HcalDetId>::const_iterator im = allgeomid_.begin(); im != allgeomid_.end(); im++) {
287  if (im->depth() != 1)
288  continue;
289  const GlobalPoint& point = geo_->getPosition((DetId)(*im));
290  double dr = reco::deltaR(point.eta(), point.phi(), eta, phi);
291  if (dr < cone) {
292  pu += (*emean_.find(im->ieta())).second + (*esigma_.find(im->ieta())).second;
293  }
294  }
295 
296  return pu;
297 }
298 
300  int it = ieta(in);
301  return (*emean_.find(it)).second;
302 }
303 
305  int it = ieta(in);
306  return (*esigma_.find(it)).second;
307 }
308 
310  int it = ieta(in);
311  return (*emean_.find(it)).second + (*esigma_.find(it)).second;
312 }
313 
315  int it = ieta(in);
316 
317  int n = (*(geomtowers_.find(it))).second - (*(ntowersWithJets_.find(it))).second;
318  return n;
319 }
320 
322  int it = ieta(in);
323  int n = (*(ntowersWithJets_.find(it))).second;
324  return n;
325 }
326 
328  int it = 0;
329  const CaloTower* ctc = dynamic_cast<const CaloTower*>(in.get());
330  if (ctc) {
331  it = ctc->id().ieta();
332  } else {
333  throw cms::Exception("Invalid Constituent") << "CaloJet constituent is not of CaloTower type";
334  }
335  return it;
336 }
337 
339  int it = 0;
340  const CaloTower* ctc = dynamic_cast<const CaloTower*>(in.get());
341  if (ctc) {
342  it = ctc->id().iphi();
343  } else {
344  throw cms::Exception("Invalid Constituent") << "CaloJet constituent is not of CaloTower type";
345  }
346  return it;
347 }
348 
edm::ESHandle::product
T const * product() const
Definition: ESHandle.h:86
PluginFactory.h
Handle.h
PileUpSubtractor::ieta
int ieta(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:327
PileUpSubtractor::iphi
int iphi(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:338
mps_fire.i
i
Definition: mps_fire.py:355
input
static const std::string input
Definition: EdmProvDump.cc:48
MessageLogger.h
PileUpSubtractor::reset
virtual void reset(std::vector< edm::Ptr< reco::Candidate > > &input, std::vector< fastjet::PseudoJet > &towers, std::vector< fastjet::PseudoJet > &output)
Definition: PileUpSubtractor.cc:41
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
ESHandle.h
nt
int nt
Definition: AMPTWrapper.h:42
convertSQLitetoXML_cfg.output
output
Definition: convertSQLitetoXML_cfg.py:32
PileUpSubtractor.h
CaloGeometryRecord
Definition: CaloGeometryRecord.h:30
PileUpSubtractor::getSigmaAtTower
virtual double getSigmaAtTower(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:304
math::XYZTLorentzVectorD
ROOT::Math::LorentzVector< ROOT::Math::PxPyPzE4D< double > > XYZTLorentzVectorD
Lorentz vector with cylindrical internal representation using pseudorapidity.
Definition: LorentzVector.h:14
DetId::Hcal
Definition: DetId.h:28
CaloTower::id
CaloTowerDetId id() const
Definition: CaloTower.h:127
PileUpSubtractor::JetDefPtr
std::shared_ptr< fastjet::JetDefinition > JetDefPtr
Definition: PileUpSubtractor.h:28
edm::second
U second(std::pair< T, U > const &p)
Definition: ParameterSet.cc:215
PileUpSubtractor::getMeanAtTower
virtual double getMeanAtTower(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:299
reco
fixed size matrix
Definition: AlignmentAlgorithmBase.h:45
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
LEDCalibrationChannels.iphi
iphi
Definition: LEDCalibrationChannels.py:64
CandidateFwd.h
deltaR.h
DetId
Definition: DetId.h:17
PileUpSubtractor::setupGeometryMap
virtual void setupGeometryMap(edm::Event &iEvent, const edm::EventSetup &iSetup)
Definition: PileUpSubtractor.cc:57
PileUpSubtractor::setDefinition
virtual void setDefinition(JetDefPtr const &jetDef)
Definition: PileUpSubtractor.cc:53
edm::EventSetup::get
T get() const
Definition: EventSetup.h:73
PVValHelper::eta
Definition: PVValidationHelpers.h:69
mathSSE::sqrt
T sqrt(T t)
Definition: SSEVec.h:19
PileUpSubtractor::PileUpSubtractor
PileUpSubtractor(const edm::ParameterSet &iConfig, edm::ConsumesCollector &&iC)
Definition: PileUpSubtractor.cc:20
edm::ESHandle< CaloGeometry >
PileUpSubtractor::getCone
virtual double getCone(double cone, double eta, double phi, double &et, double &pu)
Definition: PileUpSubtractor.cc:283
Point3DBase< float, GlobalTag >
EDM_REGISTER_PLUGINFACTORY
#define EDM_REGISTER_PLUGINFACTORY(_factory_, _category_)
Definition: PluginFactory.h:89
LEDCalibrationChannels.depth
depth
Definition: LEDCalibrationChannels.py:65
PileUpSubtractor::getPileUpAtTower
virtual double getPileUpAtTower(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:309
PileUpSubtractor::calculateOrphanInput
virtual void calculateOrphanInput(std::vector< fastjet::PseudoJet > &orphanInput)
Definition: PileUpSubtractor.cc:186
CaloGeometryRecord.h
PileUpSubtractor::calculatePedestal
virtual void calculatePedestal(std::vector< fastjet::PseudoJet > const &coll)
Definition: PileUpSubtractor.cc:98
LEDCalibrationChannels.ieta
ieta
Definition: LEDCalibrationChannels.py:63
LogDebug
#define LogDebug(id)
Definition: MessageLogger.h:670
edm::ParameterSet
Definition: ParameterSet.h:36
edmplugin::PluginFactory
Definition: PluginFactory.h:34
recoMuon::in
Definition: RecoMuonEnumerators.h:6
StorageManager_cfg.e1
e1
Definition: StorageManager_cfg.py:16
HcalDetId
Definition: HcalDetId.h:12
iEvent
int iEvent
Definition: GenABIO.cc:224
CaloTower
Definition: CaloTower.h:26
EgHLTOffHistBins_cfi.et
et
Definition: EgHLTOffHistBins_cfi.py:8
edm::EventSetup
Definition: EventSetup.h:57
get
#define get
edm::Ptr< reco::Candidate >
CaloTowerDetId::iphi
int iphi() const
get the tower iphi
Definition: CaloTowerDetId.cc:30
edm::ParameterSet::getParameter
T getParameter(std::string const &) const
HLT_2018_cff.towers
towers
Definition: HLT_2018_cff.py:35030
PileUpSubtractor::offsetCorrectJets
virtual void offsetCorrectJets()
Definition: PileUpSubtractor.cc:241
std
Definition: JetResolutionObject.h:76
CaloGeometry::getValidDetIds
std::vector< DetId > getValidDetIds() const
Get the list of all valid detector ids.
Definition: CaloGeometry.cc:75
CaloTowerDetId::ieta
int ieta() const
get the tower ieta
Definition: CaloTowerDetId.h:30
PileUpSubtractor::getNwithJets
int getNwithJets(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:321
flavorHistoryFilter_cfi.dr
dr
Definition: flavorHistoryFilter_cfi.py:37
Exception
Definition: hltDiff.cc:246
CaloGeometry.h
reco::deltaR
constexpr auto deltaR(const T1 &t1, const T2 &t2) -> decltype(t1.eta())
Definition: deltaR.h:30
PileUpSubtractor::getN
int getN(const reco::CandidatePtr &in) const
Definition: PileUpSubtractor.cc:314
AlignmentPI::index
index
Definition: AlignmentPayloadInspectorHelper.h:46
muons2muons_cfi.pu
pu
Definition: muons2muons_cfi.py:31
Candidate.h
View.h
point
*vegas h *****************************************************used in the default bin number in original ***version of VEGAS is ***a higher bin number might help to derive a more precise ***grade subtle point
Definition: invegas.h:5
edm::Event
Definition: Event.h:73
edm::ConsumesCollector
Definition: ConsumesCollector.h:39
PileUpSubtractor::subtractPedestal
virtual void subtractPedestal(std::vector< fastjet::PseudoJet > &coll)
Definition: PileUpSubtractor.cc:159