CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
ParticleTowerProducer.cc
Go to the documentation of this file.
1 // -*- C++ -*-
2 //
3 // Package: ParticleTowerProducer
4 // Class: ParticleTowerProducer
5 //
13 //
14 // Original Author: Yetkin Yilmaz,32 4-A08,+41227673039,
15 // Created: Thu Jan 20 19:53:58 CET 2011
16 // $Id: ParticleTowerProducer.cc,v 1.9 2012/02/14 08:43:00 mnguyen Exp $
17 //
18 //
19 
20 
21 // system include files
22 #include <memory>
23 
24 
25 // user include files
28 
31 
33 
35 
37 
42 
46 
47 #include "TMath.h"
48 #include "TRandom.h"
49 
50 
51 
52 
53 //
54 // constants, enums and typedefs
55 //
56 
57 
58 //
59 // static data member definitions
60 //
61 
62 //
63 // constructors and destructor
64 //
66  geo_(0)
67 {
68  //register your products
69  src_ = iConfig.getParameter<edm::InputTag>("src");
70  useHF_ = iConfig.getUntrackedParameter<bool>("useHF");
71 
72  produces<CaloTowerCollection>();
73 
74  //now do what ever other initialization is needed
75  random_ = new TRandom();
76  PI = TMath::Pi();
77 
78 
79 
80 }
81 
82 
84 {
85 
86  // do anything here that needs to be done at desctruction time
87  // (e.g. close files, deallocate resources etc.)
88 
89 }
90 
91 
92 //
93 // member functions
94 //
95 
96 // ------------ method called to produce the data ------------
97 void
99 {
100  using namespace edm;
101 
102  if(!geo_){
104  iSetup.get<CaloGeometryRecord>().get(pG);
105  geo_ = pG.product();
106  }
107 
108 
109  resetTowers(iEvent, iSetup);
110 
111 
113  iEvent.getByLabel(src_, inputsHandle);
114 
115  for(reco::PFCandidateCollection::const_iterator ci = inputsHandle->begin(); ci!=inputsHandle->end(); ++ci) {
116 
117  const reco::PFCandidate& particle = *ci;
118 
119  // put a cutoff if you want
120  //if(particle.et() < 0.3) continue;
121 
122  double eta = particle.eta();
123 
124  if(!useHF_ && fabs(eta) > 3. ) continue;
125 
126  int ieta = eta2ieta(eta);
127  if(eta<0) ieta *= -1;
128 
129  int iphi = phi2iphi(particle.phi(),ieta);
130 
131 
133 
134  // check against the old method (boundaries slightly shifted in the endcaps
135  /*
136  HcalDetId hid_orig = getNearestTower(particle);
137 
138  if(hid != hid_orig)
139  {
140  if(abs((hid).ieta()-(hid_orig).ieta())>1 || abs((hid).iphi()-(hid_orig).iphi()) >1 ){
141 
142  int phi_diff = 1;
143  if(abs((hid).ieta())>39 || abs((hid_orig).ieta())>39) phi_diff = 4;
144  else if(abs((hid).ieta())>20 || abs((hid_orig).ieta())>20) phi_diff = 2;
145 
146  if(abs((hid).ieta()-(hid_orig).ieta()) > 1 || abs((hid).iphi()-(hid_orig).iphi()) > phi_diff ){
147  std::cout<<" eta "<<eta<<std::endl;
148  std::cout<<" hid "<<hid<<std::endl;
149  std::cout<<" old method "<<hid_orig<<std::endl;
150  }
151  }
152  }
153  */
154  towers_[hid] += particle.et();
155  }
156 
157 
158  std::auto_ptr<CaloTowerCollection> prod(new CaloTowerCollection());
159 
160  for ( std::map< DetId, double >::const_iterator iter = towers_.begin();
161  iter != towers_.end(); ++iter ){
162 
163  CaloTowerDetId newTowerId(iter->first.rawId());
164  double et = iter->second;
165 
166  if(et>0){
167 
168  GlobalPoint pos =geo_->getGeometry(newTowerId)->getPosition();
169 
170  if(!useHF_ && fabs(pos.eta()) > 3. ) continue;
171 
172  // currently sets et = pt, mass to zero
173  // pt, eta , phi, mass
174  reco::Particle::PolarLorentzVector p4(et,pos.eta(),pos.phi(),0.);
175 
176  CaloTower newTower(newTowerId,et,0,0,0,0,p4,pos,pos);
177  prod->push_back(newTower);
178  }
179  }
180 
181 
182  //For reference, Calo Tower Constructors
183 
184  /*
185  CaloTower(const CaloTowerDetId& id,
186  double emE, double hadE, double outerE,
187  int ecal_tp, int hcal_tp,
188  const PolarLorentzVector p4,
189  GlobalPoint emPosition, GlobalPoint hadPosition);
190 
191  CaloTower(const CaloTowerDetId& id,
192  double emE, double hadE, double outerE,
193  int ecal_tp, int hcal_tp,
194  const LorentzVector p4,
195  GlobalPoint emPosition, GlobalPoint hadPosition);
196  */
197 
198 
199  iEvent.put(prod);
200 
201 
202 }
203 
204 // ------------ method called once each job just before starting event loop ------------
205 void
207 {
208  // tower edges from fast sim, used starting at index 30 for the HF
209  const double etatow[42] = {0.000, 0.087, 0.174, 0.261, 0.348, 0.435, 0.522, 0.609, 0.696, 0.783, 0.870, 0.957, 1.044, 1.131, 1.218, 1.305, 1.392, 1.479, 1.566, 1.653, 1.740, 1.830, 1.930, 2.043, 2.172, 2.322, 2.500, 2.650, 2.853, 3.000, 3.139, 3.314, 3.489, 3.664, 3.839, 4.013, 4.191, 4.363, 4.538, 4.716, 4.889, 5.191};
210 
211  // tower centers derived directly from by looping over HCAL towers and printing out the values, used up until eta = 3 where the HF/Endcap interface becomes problematic
212  const double etacent[40] = {
213  0.0435, // 1
214  0.1305, // 2
215  0.2175, // 3
216  0.3045, // 4
217  0.3915, // 5
218  0.4785, // 6
219  0.5655, // 7
220  0.6525, // 8
221  0.7395, // 9
222  0.8265, // 10
223  0.9135, // 11
224  1.0005, // 12
225  1.0875, // 13
226  1.1745, // 14
227  1.2615, // 15
228  1.3485, // 16
229  1.4355, // 17
230  1.5225, // 18
231  1.6095, // 19
232  1.6965, // 20
233  1.785 , // 21
234  1.88 , // 22
235  1.9865, // 23
236  2.1075, // 24
237  2.247 , // 25
238  2.411 , // 26
239  2.575 , // 27
240  2.759 , // 28
241  2.934 , // 29
242  2.90138,// 29
243  3.04448,// 30
244  3.21932,// 31
245  3.39462,// 32
246  3.56966,// 33
247  3.74485,// 34
248  3.91956,// 35
249  4.09497,// 36
250  4.27007,// 37
251  4.44417,// 38
252  4.62046 // 39
253  };
254 
255  // Use the real towers centrers for the barrel and endcap up to eta=3
256  etaedge[0] = 0.;
257  for(int i=1;i<30;i++){
258  etaedge[i] = (etacent[i-1]-etaedge[i-1])*2.0 + etaedge[i-1];
259  //std::cout<<" i "<<i<<" etaedge "<<etaedge[i]<<std::endl;
260  }
261  // beyond eta = 3 just use the fast sim values
262  if(useHF_){
263  for(int i=30;i<42;i++){
264  etaedge[i]=etatow[i];
265  //std::cout<<" i "<<i<<" etaedge "<<etaedge[i]<<std::endl;
266  }
267  }
268 }
269 
270 // ------------ method called once each job just after ending the event loop ------------
271 void
273 }
274 
275 
277 {
278 
279  std::vector<DetId> alldid = geo_->getValidDetIds();
280 
281  for(std::vector<DetId>::const_iterator did=alldid.begin(); did != alldid.end(); did++){
282  if( (*did).det() == DetId::Hcal ){
283  HcalDetId hid = HcalDetId(*did);
284  if( hid.depth() == 1 ) {
285 
286  if(!useHF_){
288  //if((hid).iphi()==1)std::cout<<" ieta "<<(hid).ieta()<<" eta "<<pos.eta()<<" iphi "<<(hid).iphi()<<" phi "<<pos.phi()<<std::endl;
289  if(fabs(pos.eta())>3.) continue;
290  }
291  towers_[(*did)] = 0.;
292  }
293 
294  }
295  }
296 
297 
298 
299 
300  //print out tower eta boundaries
301  /*
302  int current_ieta=0;
303  float testphi =0.;
304  for(double testeta=1.7; testeta<=5.4;testeta+=0.00001){
305  HcalDetId hid = getNearestTower(testeta,testphi);
306  if((hid).ieta()!=current_ieta) {
307  std::cout<<" new ieta "<<current_ieta<<" testeta "<<testeta<<std::endl;
308  current_ieta = (hid).ieta();
309  }
310  }
311  */
312 
313 }
314 
315 
316 
317 
319 
320  double eta = in.eta();
321  double phi = in.phi();
322 
323  double minDeltaR = 9999;
324 
325  std::vector<DetId> alldid = geo_->getValidDetIds();
326 
327  DetId returnId;
328 
329  //int nclosetowers=0;
330 
331  for(std::vector<DetId>::const_iterator did=alldid.begin(); did != alldid.end(); did++){
332  if( (*did).det() == DetId::Hcal ){
333 
334  HcalDetId hid(*did);
335 
336  // which layer is irrelevant for an eta-phi map, no?
337 
338  if( hid.depth() != 1 ) continue;
339 
341 
342  double hcalEta = pos.eta();
343  double hcalPhi = pos.phi();
344 
345  //std::cout<<" transverse distance = "<<sqrt(pos.x()*pos.x()+pos.y()*pos.y())<<std::endl;
346 
347  //std::cout<<" ieta "<<(hid).ieta()<<" iphi "<<(hid).iphi()<<" hcalEta "<<hcalEta<<" hcalPhi "<<hcalPhi<<std::endl;
348 
349  double deltaR = reco::deltaR(eta,phi,hcalEta,hcalPhi);
350 
351  // need to factor in the size of the tower
352  double towersize = 0.087;
353 
354  int ieta = (hid).ieta();
355 
356  if(abs(ieta)>21){
357  if(abs(ieta)>29) towersize=0.175;
358  else{
359  if(abs(ieta)==22) towersize=0.1;
360  else if(abs(ieta)==23) towersize=0.113;
361  else if(abs(ieta)==24) towersize=0.129;
362  else if(abs(ieta)==25) towersize=0.16;
363  else if(abs(ieta)==26) towersize=0.168;
364  else if(abs(ieta)==27) towersize=0.15;
365  else if(abs(ieta)==28) towersize=0.218;
366  else if(abs(ieta)==29) towersize=0.132;
367  }
368  }
369 
370  deltaR /= towersize;
371  //if(deltaR<1/3.) nclosetowers++;
372 
373  if(deltaR<minDeltaR){
374  returnId = DetId(*did);
375  minDeltaR = deltaR;
376  }
377 
378  //if(abs(eta-hcalEta)<towersize/2. && abs(phi-hcalPhi)<towersize/2.) break;
379  }
380  }
381  //if(nclosetowers>1)std::cout<<"eta "<<eta<<" phi "<<phi<<" minDeltaR "<<minDeltaR<<" nclosetowers "<<nclosetowers<<std::endl;
382  return returnId;
383 
384 
385 }
386 
387 
389 
390  // get closest tower by delta R matching
391  // Now obsolute, instead use faster premade eta-phi grid
392 
393  double minDeltaR = 9999;
394 
395  std::vector<DetId> alldid = geo_->getValidDetIds();
396 
397  DetId returnId;
398 
399  //int nclosetowers=0;
400 
401  for(std::vector<DetId>::const_iterator did=alldid.begin(); did != alldid.end(); did++){
402  if( (*did).det() == DetId::Hcal ){
403 
404  HcalDetId hid(*did);
405 
406  // which layer is irrelevant for an eta-phi map, no?
407 
408  if( hid.depth() != 1 ) continue;
409 
411 
412  double hcalEta = pos.eta();
413  double hcalPhi = pos.phi();
414 
415  //std::cout<<" transverse distance = "<<sqrt(pos.x()*pos.x()+pos.y()*pos.y())<<std::endl;
416 
417  //std::cout<<" ieta "<<(hid).ieta()<<" iphi "<<(hid).iphi()<<" hcalEta "<<hcalEta<<" hcalPhi "<<hcalPhi<<std::endl;
418 
419  double deltaR = reco::deltaR(eta,phi,hcalEta,hcalPhi);
420 
421  // need to factor in the size of the tower
422  double towersize = 0.087;
423 
424  int ieta = (hid).ieta();
425 
426  if(abs(ieta)>21){
427  if(abs(ieta)>29) towersize=0.175;
428  else{
429  if(abs(ieta)==22) towersize=0.1;
430  else if(abs(ieta)==23) towersize=0.113;
431  else if(abs(ieta)==24) towersize=0.129;
432  else if(abs(ieta)==25) towersize=0.16;
433  else if(abs(ieta)==26) towersize=0.168;
434  else if(abs(ieta)==27) towersize=0.15;
435  else if(abs(ieta)==28) towersize=0.218;
436  else if(abs(ieta)==29) towersize=0.132;
437  }
438  }
439 
440  deltaR /= towersize;
441  //if(deltaR<1/3.) nclosetowers++;
442 
443  if(deltaR<minDeltaR){
444  returnId = DetId(*did);
445  minDeltaR = deltaR;
446  }
447 
448  //if(abs(eta-hcalEta)<towersize/2. && abs(phi-hcalPhi)<towersize/2.) break;
449  }
450  }
451  //if(nclosetowers>1)std::cout<<"eta "<<eta<<" phi "<<phi<<" minDeltaR "<<minDeltaR<<" nclosetowers "<<nclosetowers<<std::endl;
452  return returnId;
453 
454 
455 }
456 
457 
458 
459 
460 // Taken from FastSimulation/CalorimeterProperties/src/HCALProperties.cc
461 // Note this returns an abs(ieta)
463  // binary search in the array of towers eta edges
464  int size = 42;
465  if(!useHF_) size = 30;
466 
467  double x = fabs(eta);
468  int curr = size / 2;
469  int step = size / 4;
470  int iter;
471  int prevdir = 0;
472  int actudir = 0;
473 
474  for (iter = 0; iter < size ; iter++) {
475 
476  if( curr >= size || curr < 1 )
477  std::cout << " ParticleTowerProducer::eta2ieta - wrong current index = "
478  << curr << " !!!" << std::endl;
479 
480  if ((x <= etaedge[curr]) && (x > etaedge[curr-1])) break;
481  prevdir = actudir;
482  if(x > etaedge[curr]) {actudir = 1;}
483  else {actudir = -1;}
484  if(prevdir * actudir < 0) { if(step > 1) step /= 2;}
485  curr += actudir * step;
486  if(curr > size) curr = size;
487  else { if(curr < 1) {curr = 1;}}
488 
489  /*
490  std::cout << " HCALProperties::eta2ieta end of iter." << iter
491  << " curr, etaedge[curr-1], etaedge[curr] = "
492  << curr << " " << etaedge[curr-1] << " " << etaedge[curr] << std::endl;
493  */
494 
495  }
496 
497  /*
498  std::cout << " HCALProperties::eta2ieta for input x = " << x
499  << " found index = " << curr-1
500  << std::endl;
501  */
502 
503  return curr;
504 }
505 
506 int ParticleTowerProducer::phi2iphi(double phi, int ieta) const {
507 
508  if(phi<0) phi += 2.*PI;
509  else if(phi> 2.*PI) phi -= 2.*PI;
510 
511  int iphi = (int) TMath::Ceil(phi/2.0/PI*72.);
512  // take into account larger granularity in endcap (x2) and at the end of the HF (x4)
513  if(abs(ieta)>20){
514  if(abs(ieta)<40) iphi -= (iphi+1)%2;
515  else {
516  iphi -= (iphi+1)%4;
517  if(iphi==-1) iphi=71;
518  }
519  }
520 
521  return iphi;
522 
523 }
524 
525 uint32_t
526 ParticleTowerProducer::denseIndex(int ieta, int iphi, double eta) const
527 {
528  const uint32_t ie ( abs(ieta) ) ;
529  //const uint32_t ip ( iphi - 1 ) ;
530  const uint32_t ip ( iphi ) ;
531  const int dp ( 1 );
532  const int zn ( eta < 0 ? 1 : 0 ); // 1 means negative z
533 
534  // vanilla towers
535  /*
536  return ( ( 0 > ieta ? 0 : allNTot ) +
537  ( ( barIEta >= ie ? ( ie - 1 )*barNPhi + ip :
538  ( endIEta >= ie ? barNTot + ( ie - 1 - barIEta )*endNPhi + ip/2 :
539  barNTot + endNTot + ( ie - 1 - endIEta )*forNPhi + ip/4 ) ) ) ) ;
540 
541  */
542 
543 
544  // Hcal towers
546  if(ie<=16) sd = HcalBarrel;
547  //else if(fabs(eta)< 2.853) sd = HcalEndcap;
548  else if(fabs(eta)< 3.) sd = HcalEndcap; // Use the endcap until eta =3
549  else sd = HcalForward;
550 
551  return ( ( sd == HcalBarrel ) ?
552  ( ip - 1 )*18 + dp - 1 + ie - ( ie<16 ? 1 : 0 ) + zn*1296 :
553  ( ( sd == HcalEndcap ) ?
554  2*1296 + ( ip - 1 )*8 + ( ip/2 )*20 +
555  ( ( ie==16 || ie==17 ) ? ie - 16 :
556  ( ( ie>=18 && ie<=20 ) ? 2 + 2*( ie - 18 ) + dp - 1 :
557  ( ( ie>=21 && ie<=26 ) ? 8 + 2*( ie - 21 ) + dp - 1 :
558  ( ( ie>=27 && ie<=28 ) ? 20 + 3*( ie - 27 ) + dp - 1 :
559  26 + 2*( ie - 29 ) + dp - 1 ) ) ) ) + zn*1296 :
560  ( ( sd == HcalOuter ) ?
561  2*1296 + 2*1296 + ( ip - 1 )*15 + ( ie - 1 ) + zn*1080 :
562  ( ( sd == HcalForward ) ?
563  2*1296 + 2*1296 + 2*1080 +
564  ( ( ip - 1 )/4 )*4 + ( ( ip - 1 )/2 )*22 +
565  2*( ie - 29 ) + ( dp - 1 ) + zn*864 : -1 ) ) ) ) ;
566 
567 }
568 
569 
570 
571  //define this as a plug-in
const double Pi
T getParameter(std::string const &) const
T getUntrackedParameter(std::string const &, T const &) const
int i
Definition: DBlmapReader.cc:9
static const double etatow[]
int phi2iphi(double phi, int ieta) const
virtual double et() const
transverse energy
list step
Definition: launcher.py:15
#define DEFINE_FWK_MODULE(type)
Definition: MakerMacros.h:17
Geom::Phi< T > phi() const
Definition: PV3DBase.h:68
#define abs(x)
Definition: mlp_lapack.h:159
T eta() const
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
virtual double eta() const
momentum pseudorapidity
uint32_t denseIndex(int ieta, int iphi, double eta) const
ParticleTowerProducer(const edm::ParameterSet &)
double deltaR(double eta1, double phi1, double eta2, double phi2)
Definition: deltaR.h:19
void resetTowers(edm::Event &iEvent, const edm::EventSetup &iSetup)
int depth() const
get the tower depth
Definition: HcalDetId.h:42
int iEvent
Definition: GenABIO.cc:243
std::map< DetId, double > towers_
static HcalDetId detIdFromDenseIndex(uint32_t di)
Definition: HcalDetId.cc:155
OrphanHandle< PROD > put(std::auto_ptr< PROD > product)
Put a new product.
Definition: Event.h:85
double p4[4]
Definition: TauolaWrapper.h:92
HcalSubdetector
Definition: HcalAssistant.h:32
bool getByLabel(InputTag const &tag, Handle< PROD > &result) const
Definition: Event.h:356
static const double etacent[]
double deltaR(double eta1, double eta2, double phi1, double phi2)
Definition: TreeUtility.cc:17
Definition: DetId.h:20
int eta2ieta(double eta) const
double sd
const T & get() const
Definition: EventSetup.h:55
T const * product() const
Definition: ESHandle.h:62
std::vector< DetId > getValidDetIds() const
Get the list of all valid detector ids.
Definition: CaloGeometry.cc:90
T eta() const
Definition: PV3DBase.h:75
Particle reconstructed by the particle flow algorithm.
Definition: PFCandidate.h:33
edm::SortedCollection< CaloTower > CaloTowerCollection
Definition: CaloTowerFwd.h:15
const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id.
Definition: CaloGeometry.cc:76
math::PtEtaPhiMLorentzVector PolarLorentzVector
Lorentz vector.
Definition: Particle.h:27
CaloGeometry const * geo_
DetId getNearestTower(const reco::PFCandidate &in) const
tuple cout
Definition: gather_cfg.py:121
virtual void produce(edm::Event &, const edm::EventSetup &)
x
Definition: VDTMath.h:216
virtual double phi() const
momentum azimuthal angle
tuple size
Write out results.
Definition: DDAxes.h:10