CMS 3D CMS Logo

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