CMS 3D CMS Logo

SpikeAndDoubleSpikeCleaner.cc
Go to the documentation of this file.
2 #include <cmath>
3 
4 namespace {
5  std::pair<double,double> dCrack(double phi, double eta) {
6  constexpr double oneOverCrystalSize=1.0/0.0175;
7  constexpr double pi=M_PI;
8  constexpr double twopi=2*pi;
9  // the result below is from unrolling
10  // the lazy-eval loop in PFClusterAlgo::dCrack
11  constexpr double cPhi[18] = {2.97025, 2.621184149601134, 2.272118299202268,
12  1.9230524488034024, 1.5739865984045365,
13  1.2249207480056705, 0.8758548976068048,
14  0.5267890472079388, 0.1777231968090729,
15  -0.17134265358979306, -0.520408503988659,
16  -0.8694743543875245, -1.2185402047863905,
17  -1.5676060551852569, -1.9166719055841224,
18  -2.265737755982988, -2.6148036063818543,
19  -2.9638694567807207};
20  constexpr double cEta[9] = {0.0,
21  4.44747e-01, -4.44747e-01,
22  7.92824e-01, -7.92824e-01,
23  1.14090e+00, -1.14090e+00,
24  1.47464e+00, -1.47464e+00};
25  // shift for eta < 0
26  constexpr double delta_cPhi = 0.00638;
27  // let's calculate dphi
28  double defi = 0;
29  if( eta < 0 ) phi+=delta_cPhi;
30  if( phi >= -pi && phi <= pi ) {
31  //the problem of the extrema
32  if( phi < cPhi[17] || phi >= cPhi[0] ) {
33  if( phi < 0 ) phi += 2*pi;
34  defi = std::min(std::abs(phi-cPhi[0]),std::abs(phi-cPhi[17]-twopi));
35  } else { // between these extrema
36  bool OK = false;
37  unsigned i = 16;
38  while(!OK) {
39  if( phi < cPhi[i] ) {
40  defi = std::min(std::abs(phi-cPhi[i+1]),std::abs(phi-cPhi[i]));
41  OK=true;
42  } else {
43  i -= 1;
44  }
45  }// end while
46  }
47  } else { // if there's a problem assume we're in a crack
48  defi = 0;
49  }
50  // let's calculate deta
51  double deta = 999.0;
52  for( const double etaGap : cEta ) {
53  deta = std::min(deta,std::abs(eta-etaGap));
54  }
55  defi *= oneOverCrystalSize;
56  deta *= oneOverCrystalSize;
57  return std::make_pair(defi,deta);
58  }
59 }
60 
64  _layerMap({ {"PS2",(int)PFLayer::PS2},
65  {"PS1",(int)PFLayer::PS1},
66  {"ECAL_ENDCAP",(int)PFLayer::ECAL_ENDCAP},
67  {"ECAL_BARREL",(int)PFLayer::ECAL_BARREL},
68  {"NONE",(int)PFLayer::NONE},
69  {"HCAL_BARREL1",(int)PFLayer::HCAL_BARREL1},
70  {"HCAL_BARREL2_RING0",(int)PFLayer::HCAL_BARREL2},
71  // hack to deal with ring1 in HO
72  {"HCAL_BARREL2_RING1",100*(int)PFLayer::HCAL_BARREL2},
73  {"HCAL_ENDCAP",(int)PFLayer::HCAL_ENDCAP},
74  {"HF_EM",(int)PFLayer::HF_EM},
75  {"HF_HAD",(int)PFLayer::HF_HAD} }) {
76  const std::vector<edm::ParameterSet>& thresholds =
77  conf.getParameterSetVector("cleaningByDetector");
78  for( const auto& pset : thresholds ) {
80  const std::string& det = pset.getParameter<std::string>("detector");
81  info._minS4S1_a = pset.getParameter<double>("minS4S1_a");
82  info._minS4S1_b = pset.getParameter<double>("minS4S1_b");
83  info._doubleSpikeS6S2 = pset.getParameter<double>("doubleSpikeS6S2");
84  info._eneThreshMod = pset.getParameter<double>("energyThresholdModifier");
85  info._fracThreshMod =
86  pset.getParameter<double>("fractionThresholdModifier");
87  info._doubleSpikeThresh = pset.getParameter<double>("doubleSpikeThresh");
88  info._singleSpikeThresh = pset.getParameter<double>("singleSpikeThresh");
89  auto entry = _layerMap.find(det);
90  if( entry == _layerMap.end() ) {
91  throw cms::Exception("InvalidDetectorLayer")
92  << "Detector layer : " << det << " is not in the list of recognized"
93  << " detector layers!";
94  }
95  _thresholds.emplace(_layerMap.find(det)->second,info);
96  }
97 }
98 
99 
102  std::vector<bool>& mask ) {
103  //need to run over energy sorted rechits
104  auto const & hits = *input;
105  std::vector<unsigned > ordered_hits(hits.size());
106  for( unsigned i = 0; i < hits.size(); ++i ) ordered_hits[i]=i;
107  std::sort(ordered_hits.begin(),ordered_hits.end(),[&](unsigned i, unsigned j) { return hits[i].energy()>hits[j].energy();});
108 
109  for( const auto& idx : ordered_hits ) {
110  const unsigned i = idx;
111  if( !mask[i] ) continue; // don't need to re-mask things :-)
112  const reco::PFRecHit& rechit = hits[i];
113  int hitlayer = (int)rechit.layer();
114  if( hitlayer == PFLayer::HCAL_BARREL2 &&
115  std::abs(rechit.positionREP().eta()) > 0.34 ) {
116  hitlayer *= 100;
117  }
118  const spike_cleaning& clean = _thresholds.find(hitlayer)->second;
119  if( rechit.energy() < clean._singleSpikeThresh ) continue;
120 
121  //Fix needed for HF. Here, we find the (up to) five companion rechits
122  //to work in conjunction with the neighbours4() implementation below for the full HF surrounding energy
123  float compsumE = 0.0;
124  if ((hitlayer==PFLayer::HF_EM || hitlayer==PFLayer::HF_HAD)) {
125  int comp = 1;
126  if (hitlayer==PFLayer::HF_EM) comp = 2;
127  const HcalDetId& detid = (HcalDetId)rechit.detId();
128  int heta = detid.ieta();
129  int hphi = detid.iphi();
130 
131  //At eta>39, phi segmentation changes
132  int predphi =2;
133  if (std::abs(heta)>39) predphi=4;
134 
135  int curphiL = hphi-predphi;
136  int curphiH = hphi+predphi;
137 
138  //HcalDetId valid phi range (0-72)
139  while (curphiL<0) curphiL+=72;
140  while (curphiH>72) curphiH-=72;
141 
142  std::pair<std::vector<int>, std::vector<int>> phietas({heta,heta+1,heta-1,heta,heta},{hphi,hphi,hphi,curphiL,curphiH});
143 
144  std::vector<uint32_t> rawDetIds;
145  for(unsigned in=0;in<phietas.first.size();in++) {
146  HcalDetId tempID (HcalForward, phietas.first[in], phietas.second[in], comp);
147  rawDetIds.push_back(tempID.rawId());
148  }
149 
150  for( const auto& jdx : ordered_hits ) {
151  const unsigned j = jdx;
152  const reco::PFRecHit& matchrechit = hits[j];
153  for( const auto& iID : rawDetIds ) if (iID==matchrechit.detId())compsumE+=matchrechit.energy();
154  }
155  }
156  //End of fix needed for HF
157 
158  const double rhenergy = rechit.energy();
159  // single spike cleaning
160  auto const & neighbours4 = rechit.neighbours4();
161  double surroundingEnergy = compsumE;
162  for( auto k : neighbours4 ) {
163  if( !mask[k] ) continue;
164  auto const & neighbour = hits[k];
165  const double sum = neighbour.energy(); //energyUp is just rechit energy?
166  surroundingEnergy += sum;
167  }
168  // wannaBeSeed.energyUp()/wannaBeSeed.energy() : 1.;
169  // Fraction 1 is the balance between the hit and its neighbours
170  // from both layers
171  const double fraction1 = surroundingEnergy/rhenergy;
172  // removed spurious comments from old pfcluster algo...
173  // look there if you want more history
174  const double f1Cut = ( clean._minS4S1_a*std::log10(rechit.energy()) +
175  clean._minS4S1_b );
176  if( fraction1 < f1Cut ) {
177  const double eta = rechit.positionREP().eta();
178  const double aeta = std::abs(eta);
179  const double phi = rechit.positionREP().phi();
180  std::pair<double,double> dcr = dCrack(phi,eta);
181  const double dcrmin = ( rechit.layer() == PFLayer::ECAL_BARREL ?
182  std::min(dcr.first,dcr.second):
183  dcr.second );
184  if( aeta < 5.0 &&
185  ( (aeta < 2.85 && dcrmin > 1.0) ||
186  (rhenergy > clean._eneThreshMod*clean._singleSpikeThresh &&
187  fraction1 < f1Cut/clean._fracThreshMod ) ) ) {
188  mask[i] = false;
189  }
190  }//if initial fraction cut (single spike)
191  // double spike removal
192  if( mask[i] && rhenergy > clean._doubleSpikeThresh ) {
193  //Determine energy surrounding the seed and the most energetic neighbour
194  double surroundingEnergyi = 0.0;
195  double enmax = -999.0;
196  unsigned int mostEnergeticNeighbour=0;
197  auto const & neighbours4i = rechit.neighbours4();
198  for( auto k : neighbours4i ) {
199  if( !mask[k] ) continue;
200  auto const & neighbour = hits[k];
201  const double nenergy = neighbour.energy();
202  surroundingEnergyi += nenergy;
203  if( nenergy > enmax ) {
204  enmax = nenergy;
205  mostEnergeticNeighbour = k;
206  }
207  }
208  // is there an energetic neighbour
209  if( enmax > 0.0 ) {
210  double surroundingEnergyj = 0.0;
211  auto const & neighbours4j =
212  hits[mostEnergeticNeighbour].neighbours4();
213  for(auto k : neighbours4j ) {
214  //if( !mask[k] && k != i) continue; // leave out?
215  surroundingEnergyj += hits[k].energy();
216  }
217  // The energy surrounding the double spike candidate
218  const double surroundingEnergyFraction =
219  (surroundingEnergyi+surroundingEnergyj) /
220  (rechit.energy()+hits[mostEnergeticNeighbour].energy()) - 1.;
221  if ( surroundingEnergyFraction < clean._doubleSpikeS6S2 ) {
222  const double eta = rechit.positionREP().eta();
223  const double aeta = std::abs(eta);
224  const double phi = rechit.positionREP().phi();
225  std::pair<double,double> dcr = dCrack(phi,eta);
226  const double dcrmin = ( rechit.layer() == PFLayer::ECAL_BARREL ?
227  std::min(dcr.first,dcr.second):
228  dcr.second );
229  if( aeta < 5.0 &&
230  ( (aeta < 2.85 && dcrmin > 1.0) ||
231  (rhenergy > clean._eneThreshMod*clean._doubleSpikeThresh &&
232  surroundingEnergyFraction < clean._doubleSpikeS6S2/clean._fracThreshMod )
233  ) ) {
234  mask[i] = false;
235  mask[mostEnergeticNeighbour] = false;
236  }
237  }
238  } // was there an energetic neighbour ?
239  }// if double spike thresh
240  } // rechit loop
241 }
float phi() const
momentum azimuthal angle
Definition: PtEtaPhiMass.h:53
static const TGPicture * info(bool iBackgroundIsBlack)
SpikeAndDoubleSpikeCleaner(const edm::ParameterSet &conf)
unsigned detId() const
rechit detId
Definition: PFRecHit.h:108
void clean(const edm::Handle< reco::PFRecHitCollection > &input, std::vector< bool > &mask) override
#define constexpr
static std::string const input
Definition: EdmProvDump.cc:44
const Double_t pi
uint32_t rawId() const
get the raw id
Definition: DetId.h:43
PFLayer::Layer layer() const
rechit layer
Definition: PFRecHit.h:111
Particle flow rechit (rechit + geometry and topology information). See clustering algorithm in PFClus...
Definition: PFRecHit.h:31
int ieta() const
get the cell ieta
Definition: HcalDetId.h:56
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
T min(T a, T b)
Definition: MathUtil.h:58
float energy() const
rechit energy
Definition: PFRecHit.h:114
#define M_PI
int k[5][pyjets_maxn]
std::pair< int, edm::FunctionWithDict > OK
Definition: findMethod.cc:136
int iphi() const
get the cell iphi
Definition: HcalDetId.cc:103
std::unordered_map< int, spike_cleaning > _thresholds
float eta() const
momentum pseudorapidity
Definition: PtEtaPhiMass.h:51
Neighbours neighbours4() const
Definition: PFRecHit.h:87
const std::unordered_map< std::string, int > _layerMap
RhoEtaPhi const & positionREP() const
Definition: PFRecHit.h:131