CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
Multi5x5ClusterAlgo.cc
Go to the documentation of this file.
1 
3 
4 // Geometry
11 
14 
15 //temporary sorter, I'm sure this must exist already in CMSSW
16 template <class T1,class T2,typename Comp=std::less<T1> > struct PairSortByFirst {
17  Comp comp;
18  bool operator()(const std::pair<T1,T2>& lhs,const std::pair<T1,T2>&rhs){return comp(lhs.first,rhs.first);}
19  bool operator()(const T1& lhs,const std::pair<T1,T2>&rhs){return comp(lhs,rhs.first);}
20  bool operator()(const std::pair<T1,T2>& lhs,const T1 &rhs){return comp(lhs.first,rhs);}
21  bool operator()(const T1& lhs,const T1 &rhs){return comp(lhs,rhs);}
22 };
23 
25  bool operator()(const EBDetId& lhs,const EBDetId& rhs){
26  if(lhs.ieta()<rhs.ieta()) return true;
27  else if(lhs.ieta()>rhs.ieta()) return false;
28  else return lhs.iphi()<rhs.iphi();
29  }
30 };
31 
33  bool operator()(const EEDetId& lhs,const EEDetId& rhs){
34  if(lhs.zside()<rhs.zside()) return true;
35  else if(lhs.zside()>rhs.zside()) return false;
36  else { //z is equal, onto ix
37  if(lhs.ix()<rhs.ix()) return true;
38  else if(lhs.ix()>rhs.ix()) return false;
39  else return lhs.iy()<rhs.iy();
40  }
41  }
42 };
43 
44 // Return a vector of clusters from a collection of EcalRecHits:
45 //
46 std::vector<reco::BasicCluster> Multi5x5ClusterAlgo::makeClusters(
47  const EcalRecHitCollection* hits,
48  const CaloSubdetectorGeometry *geometry_p,
49  const CaloSubdetectorTopology *topology_p,
50  const CaloSubdetectorGeometry *geometryES_p,
51  reco::CaloID::Detectors detector,
52  bool regional,
53  const std::vector<EcalEtaPhiRegion>& regions
54  )
55 {
56  seeds.clear();
57  used_s.clear();
58  canSeed_s.clear();
59  clusters_v.clear();
60 
61  recHits_ = hits;
62 
63  double threshold = 0;
64  std::string ecalPart_string;
66  if (detector == reco::CaloID::DET_ECAL_ENDCAP)
67  {
69  threshold = ecalEndcapSeedThreshold;
70  ecalPart_string = "EndCap";
71  }
72  if (detector == reco::CaloID::DET_ECAL_BARREL)
73  {
75  threshold = ecalBarrelSeedThreshold;
76  ecalPart_string = "Barrel";
77  }
78 
79  LogTrace("EcalClusters") << "-------------------------------------------------------------";
80  LogTrace("EcalClusters") << "Island algorithm invoked for ECAL" << ecalPart_string ;
81  LogTrace("EcalClusters") << "Looking for seeds, energy threshold used = " << threshold << " GeV";
82 
83 
84  int nregions=0;
85  if(regional) nregions=regions.size();
86 
87  if(!regional || nregions) {
88 
90  for(it = hits->begin(); it != hits->end(); it++)
91  {
92  double energy = it->energy();
93  if (energy < threshold) continue; // need to check to see if this line is useful!
94 
95  const CaloCellGeometry *thisCell = geometry_p->getGeometry(it->id());
96  GlobalPoint position = thisCell->getPosition();
97 
98  // Require that RecHit is within clustering region in case
99  // of regional reconstruction
100  bool withinRegion = false;
101  if (regional) {
102  std::vector<EcalEtaPhiRegion>::const_iterator region;
103  for (region=regions.begin(); region!=regions.end(); region++) {
104  if (region->inRegion(position)) {
105  withinRegion = true;
106  break;
107  }
108  }
109  }
110 
111  if (!regional || withinRegion) {
112  float ET = it->energy() * sin(position.theta());
113  if (ET > threshold) seeds.push_back(*it);
114  }
115  }
116 
117  }
118 
119  sort(seeds.begin(), seeds.end(), EcalRecHitLess());
120 
121  LogTrace("EcalClusters") << "Total number of seeds found in event = " << seeds.size();
122 
123 
124  mainSearch(hits, geometry_p, topology_p, geometryES_p);
125  sort(clusters_v.rbegin(), clusters_v.rend(), ClusterEtLess());
126 
127  LogTrace("EcalClusters") << "---------- end of main search. clusters have been sorted ----";
128 
129 
130  return clusters_v;
131 
132 }
133 
134 // Search for clusters
135 //
137  const CaloSubdetectorGeometry *geometry_p,
138  const CaloSubdetectorTopology *topology_p,
139  const CaloSubdetectorGeometry *geometryES_p
140  )
141 {
142 
143  LogTrace("EcalClusters") << "Building clusters............";
144 
145  // Loop over seeds:
146  std::vector<EcalRecHit>::iterator it;
147  for (it = seeds.begin(); it != seeds.end(); it++)
148  {
149 
150  // check if this crystal is able to seed
151  // (event though it is already used)
152  bool usedButCanSeed = false;
153  if (canSeed_s.find(it->id()) != canSeed_s.end()) usedButCanSeed = true;
154 
155  // avoid seeding for anomalous channels
156  if(! it->checkFlag(EcalRecHit::kGood)){ // if rechit is good, no need for further checks
157  if (it->checkFlags( v_chstatus_ )) continue;
158  }
159 
160  // make sure the current seed does not belong to a cluster already.
161  if ((used_s.find(it->id()) != used_s.end()) && (usedButCanSeed == false))
162  {
163  if (it == seeds.begin())
164  {
165  LogTrace("EcalClusters") << "##############################################################" ;
166  LogTrace("EcalClusters") << "DEBUG ALERT: Highest energy seed already belongs to a cluster!";
167  LogTrace("EcalClusters") << "##############################################################";
168 
169  }
170 
171  // seed crystal is used or is used and cannot seed a cluster
172  // so continue to the next seed crystal...
173  continue;
174  }
175 
176  // clear the vector of hits in current cluster
177  current_v.clear();
178 
179  // Create a navigator at the seed and get seed
180  // energy
181  CaloNavigator<DetId> navigator(it->id(), topology_p);
182  DetId seedId = navigator.pos();
183  EcalRecHitCollection::const_iterator seedIt = hits->find(seedId);
184  navigator.setHome(seedId);
185 
186  // Is the seed a local maximum?
187  bool localMaxima = checkMaxima(navigator, hits);
188 
189  if (localMaxima)
190  {
191  // build the 5x5 taking care over which crystals
192  // can seed new clusters and which can't
193  prepareCluster(navigator, hits, geometry_p);
194  }
195 
196  // If some crystals in the current vector then
197  // make them into a cluster
198  if (current_v.size() > 0)
199  {
200  makeCluster(hits, geometry_p, geometryES_p, seedIt, usedButCanSeed);
201  }
202 
203  } // End loop on seed crystals
204 
205 
208 
209 
210  for(size_t clusNr=0;clusNr<protoClusters_.size();clusNr++){
211  if(!protoClusters_[clusNr].containsSeed()){
212  const EcalRecHit& seedHit =protoClusters_[clusNr].seed();
213  typedef std::vector<std::pair<DetId,int> >::iterator It;
214  std::pair<It,It> result = std::equal_range(whichClusCrysBelongsTo_.begin(),whichClusCrysBelongsTo_.end(),seedHit.id(),PairSortByFirst<DetId,int>());
215 
216  if(result.first!=result.second) protoClusters_[result.first->second].removeHit(seedHit);
217  protoClusters_[clusNr].addSeed();
218 
219  }
220  }
221  }
222 
223 
224 
225  for(size_t clusNr=0;clusNr<protoClusters_.size();clusNr++){
226  const ProtoBasicCluster& protoCluster= protoClusters_[clusNr];
227  Point position;
228  position = posCalculator_.Calculate_Location(protoCluster.hits(), hits,geometry_p, geometryES_p);
229  clusters_v.push_back(reco::BasicCluster(protoCluster.energy(), position, reco::CaloID(detector_), protoCluster.hits(),
230  reco::CaloCluster::multi5x5, protoCluster.seed().id()));
231  }
232 
233  protoClusters_.clear();
234  whichClusCrysBelongsTo_.clear();
235 }
236 
239  const CaloSubdetectorGeometry *geometryES,
241  bool seedOutside)
242 {
243 
244  double energy = 0;
245  //double chi2 = 0;
246  reco::CaloID caloID;
247  Point position;
248  position = posCalculator_.Calculate_Location(current_v, hits,geometry, geometryES);
249 
250  std::vector<std::pair<DetId, float> >::iterator it;
251  for (it = current_v.begin(); it != current_v.end(); it++)
252  {
253  EcalRecHitCollection::const_iterator itt = hits->find( (*it).first );
254  EcalRecHit hit_p = *itt;
255  energy += hit_p.energy();
256  //chi2 += 0;
257  if ( (*it).first.subdetId() == EcalBarrel ) {
259  } else {
261  }
262 
263  }
264  //chi2 /= energy;
265 
266  LogTrace("EcalClusters") << "******** NEW CLUSTER ********";
267  LogTrace("EcalClusters") << "No. of crystals = " << current_v.size();
268  LogTrace("EcalClusters") << " Energy = " << energy ;
269  LogTrace("EcalClusters") << " Phi = " << position.phi();
270  LogTrace("EcalClusters") << " Eta " << position.eta();
271  LogTrace("EcalClusters") << "*****************************";
272 
273 
274  // to be a valid cluster the cluster energy
275  // must be at least the seed energy
276  double seedEnergy = seedIt->energy();
277  if ((seedOutside && energy>=0) || (!seedOutside && energy >= seedEnergy))
278  {
279  if(reassignSeedCrysToClusterItSeeds_){ //if we're not doing this, we dont need this info so lets not bother filling it
280  for(size_t hitNr=0;hitNr<current_v.size();hitNr++) whichClusCrysBelongsTo_.push_back(std::pair<DetId,int>(current_v[hitNr].first,protoClusters_.size()));
281  }
282  protoClusters_.push_back(ProtoBasicCluster(energy,*seedIt,current_v));
283 
284  // clusters_v.push_back(reco::BasicCluster(energy, position, reco::CaloID(detector_), current_v, reco::CaloCluster::multi5x5, seedIt->id()));
285 
286  // if no valid cluster was built,
287  // then free up these crystals to be used in the next...
288  } else {
289  std::vector<std::pair<DetId, float> >::iterator iter;
290  for (iter = current_v.begin(); iter != current_v.end(); iter++)
291  {
292  used_s.erase(iter->first);
293  } //for(iter)
294  } //else
295 
296 }
297 
299  const EcalRecHitCollection *hits)
300 {
301 
302  bool maxima = true;
304  EcalRecHitCollection::const_iterator seedHit = hits->find(navigator.pos());
305  double seedEnergy = seedHit->energy();
306 
307  std::vector<DetId> swissCrossVec;
308  swissCrossVec.clear();
309 
310  swissCrossVec.push_back(navigator.west());
311  navigator.home();
312  swissCrossVec.push_back(navigator.east());
313  navigator.home();
314  swissCrossVec.push_back(navigator.north());
315  navigator.home();
316  swissCrossVec.push_back(navigator.south());
317  navigator.home();
318 
319  std::vector<DetId>::const_iterator detItr;
320  for (unsigned int i = 0; i < swissCrossVec.size(); ++i)
321  {
322 
323  // look for this hit
324  thisHit = recHits_->find(swissCrossVec[i]);
325 
326  // continue if this hit was not found
327  if ((swissCrossVec[i] == DetId(0)) || thisHit == recHits_->end()) continue;
328 
329  // the recHit has to be skipped in the local maximum search if it was found
330  // in the map of channels to be excluded
331  uint32_t rhFlag = thisHit->recoFlag();
332  std::vector<int>::const_iterator vit = std::find(v_chstatus_.begin(), v_chstatus_.end(), rhFlag);
333  if (vit != v_chstatus_.end()) continue;
334 
335  // if this crystal has more energy than the seed then we do
336  // not have a local maxima
337  if (thisHit->energy() > seedEnergy)
338  {
339  maxima = false;
340  break;
341  }
342  }
343 
344  return maxima;
345 
346 }
347 
349  const EcalRecHitCollection *hits,
351 {
352 
353  DetId thisDet;
354  std::set<DetId>::iterator setItr;
355 
356  // now add the 5x5 taking care to mark the edges
357  // as able to seed and where overlapping in the central
358  // region with crystals that were previously able to seed
359  // change their status so they are not able to seed
360  //std::cout << std::endl;
361  for (int dx = -2; dx < 3; ++dx)
362  {
363  for (int dy = -2; dy < 3; ++ dy)
364  {
365 
366  // navigate in free steps forming
367  // a full 5x5
368  thisDet = navigator.offsetBy(dx, dy);
369  navigator.home();
370 
371  // add the current crystal
372  //std::cout << "adding " << dx << ", " << dy << std::endl;
373  addCrystal(thisDet);
374 
375  // now consider if we are in an edge (outer 16)
376  // or central (inner 9) region
377  if ((abs(dx) > 1) || (abs(dy) > 1))
378  {
379  // this is an "edge" so should be allowed to seed
380  // provided it is not already used
381  //std::cout << " setting can seed" << std::endl;
382  canSeed_s.insert(thisDet);
383  } // end if "edge"
384  else
385  {
386  // or else we are in the central 3x3
387  // and must remove any of these crystals from the canSeed set
388  setItr = canSeed_s.find(thisDet);
389  if (setItr != canSeed_s.end())
390  {
391  //std::cout << " unsetting can seed" << std::endl;
392  canSeed_s.erase(setItr);
393  }
394  } // end if "centre"
395 
396 
397  } // end loop on dy
398 
399  } // end loop on dx
400 
401  //std::cout << "*** " << std::endl;
402  //std::cout << " current_v contains " << current_v.size() << std::endl;
403  //std::cout << "*** " << std::endl;
404 }
405 
406 
408 {
409 
411  if ((thisIt != recHits_->end()) && (thisIt->id() != DetId(0)))
412  {
413  if ((used_s.find(thisIt->id()) == used_s.end()))
414  {
415  //std::cout << " ... this is a good crystal and will be added" << std::endl;
416  current_v.push_back( std::pair<DetId, float>(det, 1.) ); // by default hit energy fractions are set at 1.
417  used_s.insert(det);
418  }
419  }
420 
421 }
422 
424 {
425  std::vector<std::pair<DetId,float> >::iterator hitPos;
426  for(hitPos=hits_.begin();hitPos<hits_.end();hitPos++){
427  if(hitToRM.id()==hitPos->first) break;
428  }
429  if(hitPos!=hits_.end()){
430  hits_.erase(hitPos);
431  energy_-=hitToRM.energy();
432  return true;
433  }return false;
434 }
435 
436 //now the tricky thing is to make sure we insert it in the right place in the hit vector
437 //order is from -2,-2, -2,-1 to 2,2 with seed being 0,0 (eta,phi)
439 {
440  typedef std::vector<std::pair<DetId,float> >::iterator It;
441  std::pair<It,It> hitPos;
442 
443  if(seed_.id().subdetId()==EcalBarrel){
444  hitPos = std::equal_range(hits_.begin(),hits_.end(),seed_.id(),PairSortByFirst<DetId,float,EBDetIdSorter>());
445  }else{
446  hitPos = std::equal_range(hits_.begin(),hits_.end(),seed_.id(),PairSortByFirst<DetId,float,EEDetIdSorter>());
447  }
448 
449  if(hitPos.first==hitPos.second){//it doesnt already exist in the vec, add it
450  hits_.insert(hitPos.first,std::pair<DetId,float>(seed_.id(),1.));
451  energy_+=seed_.energy();
452  containsSeed_=true;
453 
454  return true;
455  }else return false;
456 
457 }
458 
460 {
461  for(size_t hitNr=0;hitNr<hits_.size();hitNr++){
462  if(seed_.id()==hits_[hitNr].first) return true;
463  }
464  return false;
465 }
int i
Definition: DBlmapReader.cc:9
int ix() const
Definition: EEDetId.h:76
void makeCluster(const EcalRecHitCollection *hits, const CaloSubdetectorGeometry *geometry_p, const CaloSubdetectorGeometry *geometryES_p, const EcalRecHitCollection::const_iterator &seedIt, bool seedOutside)
std::vector< reco::BasicCluster > makeClusters(const EcalRecHitCollection *hits, const CaloSubdetectorGeometry *geometry, const CaloSubdetectorTopology *topology_p, const CaloSubdetectorGeometry *geometryES_p, reco::CaloID::Detectors detector, bool regional=false, const std::vector< EcalEtaPhiRegion > &regions=std::vector< EcalEtaPhiRegion >())
std::set< DetId > canSeed_s
const EcalRecHitCollection * recHits_
Sin< T >::type sin(const T &t)
Definition: Sin.h:22
std::vector< std::pair< DetId, float > > hits_
std::vector< EcalRecHit >::const_iterator const_iterator
const std::vector< std::pair< DetId, float > > & hits() const
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:7
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
tuple result
Definition: mps_fire.py:83
Geom::Theta< T > theta() const
Definition: PV3DBase.h:75
int iphi() const
get the crystal iphi
Definition: EBDetId.h:53
void mainSearch(const EcalRecHitCollection *hits, const CaloSubdetectorGeometry *geometry_p, const CaloSubdetectorTopology *topology_p, const CaloSubdetectorGeometry *geometryES_p)
std::vector< int > v_chstatus_
T offsetBy(int deltaX, int deltaY) const
Free movement of arbitray steps.
Definition: CaloNavigator.h:80
void prepareCluster(CaloNavigator< DetId > &navigator, const EcalRecHitCollection *hits, const CaloSubdetectorGeometry *geometry)
std::vector< std::pair< DetId, int > > whichClusCrysBelongsTo_
bool operator()(const std::pair< T1, T2 > &lhs, const T1 &rhs)
T west() const
move the navigator west
Definition: CaloNavigator.h:59
void addCrystal(const DetId &det)
int zside() const
Definition: EEDetId.h:70
std::set< DetId > used_s
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
float energy() const
Definition: EcalRecHit.h:68
bool operator()(const EEDetId &lhs, const EEDetId &rhs)
std::vector< std::pair< DetId, float > > current_v
int iy() const
Definition: EEDetId.h:82
T south() const
move the navigator south
Definition: CaloNavigator.h:45
int ieta() const
get the crystal ieta
Definition: EBDetId.h:51
T pos() const
get the current position
Definition: CaloNavigator.h:32
bool operator()(const T1 &lhs, const T1 &rhs)
#define LogTrace(id)
std::vector< ProtoBasicCluster > protoClusters_
const_iterator end() const
bool removeHit(const EcalRecHit &hitToRM)
std::vector< EcalRecHit > seeds
T east() const
move the navigator east
Definition: CaloNavigator.h:52
void home() const
move the navigator back to the starting point
Definition: DetId.h:18
DetId id() const
get the id
Definition: EcalRecHit.h:76
bool operator()(const EBDetId &lhs, const EBDetId &rhs)
bool operator()(const std::pair< T1, T2 > &lhs, const std::pair< T1, T2 > &rhs)
bool checkMaxima(CaloNavigator< DetId > &navigator, const EcalRecHitCollection *hits)
bool operator()(const T1 &lhs, const std::pair< T1, T2 > &rhs)
ESHandle< TrackerGeometry > geometry
math::XYZPoint Point
point in the space
iterator find(key_type k)
math::XYZPoint Calculate_Location(const HitsAndFractions &iDetIds, const edm::SortedCollection< HitType > *iRecHits, const CaloSubdetectorGeometry *iSubGeom, const CaloSubdetectorGeometry *iESGeom=0)
Definition: PositionCalc.h:68
static int position[264][3]
Definition: ReadPGInfo.cc:509
reco::CaloID::Detectors detector_
The ecal region used.
T north() const
move the navigator north
Definition: CaloNavigator.h:38
std::vector< reco::BasicCluster > clusters_v
#define ET
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
const_iterator begin() const