CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
CosmicClusterAlgo.cc
Go to the documentation of this file.
1 
3 
4 #include <vector> //TEMP JHAUPT 4-27
5 
6 // Geometry
12 
14 
15 
16 // Return a vector of clusters from a collection of EcalRecHits:
17 //
18 std::vector<reco::BasicCluster> CosmicClusterAlgo::makeClusters(
19  const EcalRecHitCollection* hits,
20  const EcalUncalibratedRecHitCollection *uncalibhits,
21  const CaloSubdetectorGeometry *geometry_p,
22  const CaloSubdetectorTopology *topology_p,
23  const CaloSubdetectorGeometry *geometryES_p,
24  EcalPart ecalPart,
25  bool regional,
26  const std::vector<EcalEtaPhiRegion>& regions)
27 {
28  seeds.clear();
29  used_s.clear();
30  canSeed_s.clear();
31  clusters_v.clear();
32 
33  recHits_ = hits;
34  uncalibRecHits_ = uncalibhits;
35 
36  inEB = true;
37  double threshold = 0;
38  std::string ecalPart_string;
39  if (ecalPart == endcap)
40  {
41  threshold = ecalEndcapSeedThreshold;
42  ecalPart_string = "EndCap";
43  inEB = false;
44  }
45  if (ecalPart == barrel)
46  {
47  threshold = ecalBarrelSeedThreshold;
48  ecalPart_string = "Barrel";
49  inEB = true;
50  }
51 
52  if (verbosity < pINFO)
53  {
54  std::cout << "-------------------------------------------------------------" << std::endl;
55  std::cout << "Island algorithm invoked for ECAL" << ecalPart_string << std::endl;
56  std::cout << "Looking for seeds, threshold used = " << threshold << " ADC" <<std::endl;
57  }
58 
59  int nregions=0;
60  if(regional) nregions=regions.size();
61 
62  if(!regional || nregions) {
63 
65  for(it = recHits_->begin(); it != recHits_->end(); it++)
66  {
67 
68  if (!uncalibRecHits_){
69  if (verbosity < pINFO)
70  {
71  std::cout << "-------------------------------------------------------------" << std::endl;
72  std::cout << "No Uncalibrated RecHits no Uncalibrated rec hit collection available" << std::endl;
73  }
74  continue;
75  }
76 
77  // if rechit affected by features other than these, do not allow if seeding
78  // features are hard coded, for now; add severity?
79  uint32_t rhFlag = (*it).recoFlag();
80  if (!(
81  rhFlag == EcalRecHit::kGood ||
82  rhFlag == EcalRecHit::kOutOfTime ||
83  rhFlag == EcalRecHit::kPoorCalib
84  )
85  ) continue;
86 
88 
89  if (itt == uncalibRecHits_->end()){
90  if (verbosity < pINFO)
91  {
92  std::cout << "-------------------------------------------------------------" << std::endl;
93  std::cout << "No Uncalibrated RecHit associated with the RecHit Probably no Uncalibrated rec hit collection available" << std::endl;
94  }
95  continue;
96  }
97 
98  EcalUncalibratedRecHit uhit_p = *itt;
99 
100  // looking for cluster seeds
101  if (uhit_p.amplitude() < (inEB ? ecalBarrelSeedThreshold : ecalEndcapSeedThreshold) ) continue; //
102 
103  const CaloCellGeometry *thisCell = geometry_p->getGeometry(it->id());
104  GlobalPoint position = thisCell->getPosition();
105 
106  // Require that RecHit is within clustering region in case
107  // of regional reconstruction
108  bool withinRegion = false;
109  if (regional) {
110  std::vector<EcalEtaPhiRegion>::const_iterator region;
111  for (region=regions.begin(); region!=regions.end(); region++) {
112  if (region->inRegion(position)) {
113  withinRegion = true;
114  break;
115  }
116  }
117  }
118 
119  if (!regional || withinRegion) {
120  //float ET = it->energy() * sin(position.theta()); JHaupt Out 4-27-08 Et not needed for Cosmic Events...
121  // if (energy >= threshold)
122  seeds.push_back(*it); // JHaupt 4-27-2008 Et -> energy, most likely not needed as there is already a threshold requirement.
123  }
124  }
125 
126  }
127 
128  sort(seeds.begin(), seeds.end(), EcalRecHitLess());
129 
130  if (verbosity < pINFO)
131  {
132  std::cout << "JH Total number of seeds found in event = " << seeds.size() << std::endl;
133  for (EcalRecHitCollection::const_iterator ji = seeds.begin(); ji != seeds.end(); ++ji)
134  {
135  //std::cout << "JH Seed Energy " << ji->energy() << " hashed " << ((EBDetId)ji->id()).hashedIndex() << std::endl;
136 
137  }
138  }
139 
140  mainSearch(geometry_p,topology_p,geometryES_p,ecalPart );
141  sort(clusters_v.rbegin(), clusters_v.rend(),ClusterEtLess());
142 
143  if (verbosity < pINFO)
144  {
145  std::cout << "---------- end of main search. clusters have been sorted ----" << std::endl;
146  }
147 
148  return clusters_v;
149 
150 }
151 
152 // Search for clusters
153 //
155  const CaloSubdetectorTopology *topology_p,
156  const CaloSubdetectorGeometry *geometryES_p,
157  EcalPart ecalPart )
158 {
159 
160  if (verbosity < pINFO)
161  {
162  std::cout << "Building clusters............" << std::endl;
163  }
164 
165  // Loop over seeds:
166  std::vector<EcalRecHit>::iterator it;
167  for (it = seeds.begin(); it != seeds.end(); it++)
168  {
169 
170  // check if this crystal is able to seed
171  // (event though it is already used)
172  bool usedButCanSeed = false;
173  if (canSeed_s.find(it->id()) != canSeed_s.end()) usedButCanSeed = true;
174 
175  // make sure the current seed does not belong to a cluster already.
176  if ((used_s.find(it->id()) != used_s.end()) && (usedButCanSeed == false))
177  {
178  if (it == seeds.begin())
179  {
180  if (verbosity < pINFO)
181  {
182  std::cout << "##############################################################" << std::endl;
183  std::cout << "DEBUG ALERT: Highest energy seed already belongs to a cluster!" << std::endl;
184  std::cout << "##############################################################" << std::endl;
185  }
186  }
187 
188  // seed crystal is used or is used and cannot seed a cluster
189  // so continue to the next seed crystal...
190  continue;
191  }
192 
193  // clear the vector of hits in current clusterconst EcalRecHitCollection* hits,
194  current_v9.clear();
195  current_v25.clear();
196  current_v25Sup.clear();
197 
198  // Create a navigator at the seed
199  CaloNavigator<DetId> navigator(it->id(), topology_p);
200  DetId seedId = navigator.pos();
201  navigator.setHome(seedId);
202 
203  // Is the seed a local maximum?
204  bool localMaxima = checkMaxima(navigator);
205 
206  if (localMaxima)
207  {
208  // build the 5x5 taking care over which crystals //JHaupt 4-27-08 3x3 is a good option...
209  // can seed new clusters and which can't
210  prepareCluster(navigator, geometry_p);
211  }
212 
213  // If some crystals in the current vector then
214  // make them into a cluster
215  if (current_v25.size() > 0)
216  {
217  makeCluster(geometry_p, geometryES_p, seedId);
218  }
219 
220  } // End loop on seed crystals
221 
222 }
223 
226  const CaloSubdetectorGeometry *geometryES,
227  DetId seedId)
228 {
229 
230  double energy = 0;
231  double energySecond = 0.;//JHaupt 4-27-08 Added for the second crystal stream
232  double energyMax = 0.;//JHaupt 4-27-08 Added for the max crystal stream
233  double chi2 = 0;
234  DetId detFir;
235  DetId detSec;
236  //bool goodCluster = false; //JHaupt 4-27-08 Added so that some can be earased.. used another day Might not be needed as seeds are energy ordered...
237 
238 
239  std::vector<DetId>::iterator it;
240  for (it = current_v9.begin(); it != current_v9.end(); it++)
241  {
242  // Martina - check recoFlag for crystals sorrounding the good one
244  // double-check that iterator can be dereferenced
245  if(itt==recHits_->end()) continue;
246  // if rechit affected by features other than these, do not allow 2 crystal seeding either
247  // features are hard coded, for now; add severity?
248  uint32_t rhFlag = (*itt).recoFlag();
249  if (!(
250  rhFlag == EcalRecHit::kGood ||
251  rhFlag == EcalRecHit::kOutOfTime ||
252  rhFlag == EcalRecHit::kPoorCalib
253  )
254  ) continue;
255 
257 
259  EcalUncalibratedRecHit uhit_p = *ittu;
260 
261  if (uhit_p.amplitude() > energySecond ) {energySecond = uhit_p.amplitude(); detSec = uhit_p.id();}
262  if (energySecond > energyMax ) {std::swap(energySecond,energyMax); std::swap(detFir,detSec);}
263  }
264 
265  //if ((detFir.det() == DetId::Ecal) && (detFir.subdetId() == EcalEndcap)) inEB = false;
266  //We ignore the possiblity that the cluster may span into the EE
267 
268  if ((energyMax < (inEB ? ecalBarrelSingleThreshold : ecalEndcapSingleThreshold)) && (energySecond < (inEB ? ecalBarrelSecondThreshold : ecalEndcapSecondThreshold) )) return;
269 
270  for (it = current_v25.begin(); it != current_v25.end(); it++)
271  {
273  EcalRecHit hit_p = *itt;
274 
276  EcalUncalibratedRecHit uhit_p = *ittu;
277 
279  {
280  // energy fraction = 1
281  current_v25Sup.push_back( std::pair<DetId, float>( hit_p.id(), 1.) );
282  energy += hit_p.energy(); //Keep the fully corrected energy
283  chi2 += 0;
284  }
285  }
286 
287  Point position;
288  position = posCalculator_.Calculate_Location(current_v25Sup,recHits_, geometry, geometryES);
289 
290  //don't write empty clusters
291  if (energy == 0 && position == Point(0,0,0)) return;
292 
293  chi2 /= energy;
294  if (verbosity < pINFO)
295  {
296  std::cout << "JH******** NEW CLUSTER ********" << std::endl;
297  std::cout << "JHNo. of crystals = " << current_v25Sup.size() << std::endl;
298  std::cout << "JH Energy = " << energy << std::endl;
299  std::cout << "JH Phi = " << position.phi() << std::endl;
300  std::cout << "JH Eta = " << position.eta() << std::endl;
301  std::cout << "JH*****************************" << std::endl;
302  // specialize this
303 // std::cout << "JH****Emax**** "<<energyMax << " ieta " <<detFir.ieta() <<" iphi "<<detFir.ieta() << std::endl;
304 // std::cout << "JH****Esec**** "<<energySecond << " ieta " <<detSec.ieta() <<" iphi "<<detSec.ieta() << std::endl;
305  }
306  clusters_v.push_back(reco::BasicCluster(energy, position, reco::CaloID(), current_v25Sup, reco::CaloCluster::multi5x5, seedId));
307 }
308 
309 bool CosmicClusterAlgo::checkMaxima(CaloNavigator<DetId> &navigator)
310 {
311 
312  bool maxima = true;
314  EcalRecHitCollection::const_iterator seedHit = recHits_->find(navigator.pos());
315  double thisEnergy = 0.;
316  double seedEnergy = seedHit->energy();
317 
318  std::vector<DetId> swissCrossVec;
319  swissCrossVec.clear();
320 
321  swissCrossVec.push_back(navigator.west());
322  navigator.home();
323  swissCrossVec.push_back(navigator.east());
324  navigator.home();
325  swissCrossVec.push_back(navigator.north());
326  navigator.home();
327  swissCrossVec.push_back(navigator.south());
328  navigator.home();
329 
330  std::vector<DetId>::const_iterator detItr;
331  for (unsigned int i = 0; i < swissCrossVec.size(); ++i)
332  {
333  thisHit = recHits_->find(swissCrossVec[i]);
334  if ((swissCrossVec[i] == DetId(0)) || thisHit == recHits_->end()) thisEnergy = 0.0;
335  else thisEnergy = thisHit->energy();
336  if (thisEnergy > seedEnergy)
337  {
338  maxima = false;
339  break;
340  }
341  }
342 
343  return maxima;
344 
345 }
346 
347 void CosmicClusterAlgo::prepareCluster(CaloNavigator<DetId> &navigator,
349 {
350 
351  DetId thisDet;
352  std::set<DetId>::iterator setItr;
353 
354  // now add the 5x5 taking care to mark the edges
355  // as able to seed and where overlapping in the central
356  // region with crystals that were previously able to seed
357  // change their status so they are not able to seed
358  //std::cout << std::endl;
359  for (int dx = -2; dx < 3; ++dx) //for (int dx = -2; dx < 3; ++dx)
360  {
361  for (int dy = -2; dy < 3; ++ dy) //for (int dy = -2; dy < 3; ++ dy)
362  {
363 
364  // navigate in free steps forming
365  // a full 5x5
366  thisDet = navigator.offsetBy(dx, dy);
367  navigator.home();
368 
369  // add the current crystal
370  //std::cout << "adding " << dx << ", " << dy << std::endl;
371 
372 
373 
374  // now consider if we are in an edge (outer 16)
375  // or central (inner 9) region
376  if ((abs(dx) > 1) || (abs(dy) > 1))
377  {
378  // this is an "edge" so should be allowed to seed
379  // provided it is not already used
380  //std::cout << " setting can seed" << std::endl;
381  addCrystal(thisDet,false); //These are in the V25
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  addCrystal(thisDet,true); //These are in the V9
390  if (setItr != canSeed_s.end())
391  {
392  //std::cout << " unsetting can seed" << std::endl;
393  canSeed_s.erase(setItr);
394  }
395  } // end if "centre"
396 
397 
398  } // end loop on dy
399 
400  } // end loop on dx
401 
402  //std::cout << "*** " << std::endl;
403  //std::cout << " current_v contains " << current_v.size() << std::endl;
404  //std::cout << "*** " << std::endl;
405 }
406 
407 
408 void CosmicClusterAlgo::addCrystal(const DetId &det, const bool in9)
409 {
410 
412  if ((thisIt != recHits_->end()) && (thisIt->id() != DetId(0)))
413  {
414  if ((used_s.find(thisIt->id()) == used_s.end()))
415  {
417  used_s.insert(det);
418  if ((thisIt->energy() >= -1.) && !(thisItu->chi2() < -1.))
419  {
420  if (in9) current_v9.push_back(det);
421  current_v25.push_back(det);
422  }
423 
424  }
425  }
426 
427 }
const EcalUncalibratedRecHitCollection * uncalibRecHits_
int i
Definition: DBlmapReader.cc:9
double ecalBarrelSingleThreshold
std::vector< EcalRecHit >::const_iterator const_iterator
std::vector< DetId > current_v9
std::vector< reco::BasicCluster > clusters_v
void mainSearch(const CaloSubdetectorGeometry *geometry_p, const CaloSubdetectorTopology *topology_p, const CaloSubdetectorGeometry *geometryES_p, EcalPart ecalPart)
static int position[TOTALCHAMBERS][3]
Definition: ReadPGInfo.cc:509
virtual const CaloCellGeometry * getGeometry(const DetId &id) const
Get the cell geometry of a given detector id. Should return false if not found.
double ecalEndcapSingleThreshold
std::vector< reco::BasicCluster > makeClusters(const EcalRecHitCollection *hits, const EcalUncalibratedRecHitCollection *uncalibhits, const CaloSubdetectorGeometry *geometry, const CaloSubdetectorTopology *topology_p, const CaloSubdetectorGeometry *geometryES_p, EcalPart ecalPart, bool regional=false, const std::vector< EcalEtaPhiRegion > &regions=std::vector< EcalEtaPhiRegion >())
std::vector< std::pair< DetId, float > > current_v25Sup
float energy() const
Definition: CaloRecHit.h:17
void swap(edm::DataFrameContainer &lhs, edm::DataFrameContainer &rhs)
double ecalBarrelSecondThreshold
const EcalRecHitCollection * recHits_
void prepareCluster(CaloNavigator< DetId > &navigator, const CaloSubdetectorGeometry *geometry)
Abs< T >::type abs(const T &t)
Definition: Abs.h:22
const_iterator end() const
std::vector< EcalRecHit > seeds
Definition: DetId.h:18
double ecalEndcapSecondThreshold
DetId id() const
get the id
Definition: EcalRecHit.h:80
void makeCluster(const CaloSubdetectorGeometry *geometry_p, const CaloSubdetectorGeometry *geometryES_p, DetId seedId)
std::vector< DetId > current_v25
VerbosityLevel verbosity
std::set< DetId > canSeed_s
ESHandle< TrackerGeometry > geometry
void addCrystal(const DetId &det, const bool in9)
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
tuple cout
Definition: gather_cfg.py:121
math::XYZPoint Point
point in the space
bool checkMaxima(CaloNavigator< DetId > &navigator)
const GlobalPoint & getPosition() const
Returns the position of reference for this cell.
PositionCalc posCalculator_
std::set< DetId > used_s
const_iterator begin() const