CMS 3D CMS Logo

HGCalImagingAlgo.h
Go to the documentation of this file.
1 #ifndef RecoLocalCalo_HGCalRecAlgos_HGCalImagingAlgo_h
2 #define RecoLocalCalo_HGCalRecAlgos_HGCalImagingAlgo_h
3 
13 
16 
18 
19 // C/C++ headers
20 #include <string>
21 #include <vector>
22 #include <set>
23 
24 
25 #include "KDTreeLinkerAlgoT.h"
26 
27 
28 template <typename T>
29 std::vector<size_t> sorted_indices(const std::vector<T> &v) {
30 
31  // initialize original index locations
32  std::vector<size_t> idx(v.size());
33  for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;
34 
35  // sort indices based on comparing values in v
36  std::sort(idx.begin(), idx.end(),
37  [&v](size_t i1, size_t i2) {return v[i1] > v[i2];});
38 
39  return idx;
40 }
41 
43 {
44 
45 
46  public:
47 
48  enum VerbosityLevel { pDEBUG = 0, pWARNING = 1, pINFO = 2, pERROR = 3 };
49 
51  sigma2(1.0),
52  algoId(reco::CaloCluster::undefined),
54  }
55 
56  HGCalImagingAlgo(float delta_c_in, double kappa_in, double ecut_in,
57  // const CaloSubdetectorTopology *thetopology_p,
58  reco::CaloCluster::AlgoId algoId_in,
59  VerbosityLevel the_verbosity = pERROR) : delta_c(delta_c_in), kappa(kappa_in),
60  ecut(ecut_in),
61  cluster_offset(0),
62  sigma2(1.0),
63  algoId(algoId_in),
64  verbosity(the_verbosity){
65  }
66 
67  HGCalImagingAlgo(float delta_c_in, double kappa_in, double ecut_in,
68  double showerSigma,
69  // const CaloSubdetectorTopology *thetopology_p,
70  reco::CaloCluster::AlgoId algoId_in,
71  VerbosityLevel the_verbosity = pERROR) : delta_c(delta_c_in), kappa(kappa_in),
72  ecut(ecut_in),
73  cluster_offset(0),
74  sigma2(std::pow(showerSigma,2.0)),
75  algoId(algoId_in),
76  verbosity(the_verbosity){
77  }
78 
80  {
81  }
82 
83  void setVerbosity(VerbosityLevel the_verbosity)
84  {
85  verbosity = the_verbosity;
86  }
87 
88  // this is the method that will start the clusterisation (it is possible to invoke this method more than once - but make sure it is with
89  // different hit collections (or else use reset)
91  // this is the method to get the cluster collection out
92  std::vector<reco::BasicCluster> getClusters(bool);
93  // needed to switch between EE and HE with the same algorithm object (to get a single cluster collection)
95  // use this if you want to reuse the same cluster object but don't want to accumulate clusters (hardly useful?)
96  void reset(){
97  current_v.clear();
98  clusters_v.clear();
99  cluster_offset = 0;
100  }
103 
104  private:
105 
106  //max number of layers
107  static const unsigned int maxlayer = 52;
108 
109  // The two parameters used to identify clusters
110  float delta_c;
111  double kappa;
112 
113  // The hit energy cutoff
114  double ecut;
115 
116  // The current offset into the temporary cluster structure
117  unsigned int cluster_offset;
118 
119  // for energy sharing
120  double sigma2; // transverse shower size
121 
122  // The vector of clusters
123  std::vector<reco::BasicCluster> clusters_v;
124 
126 
127  // The algo id
129 
130  // The verbosity level
132 
133 
134  struct Hexel {
135 
136  double x;
137  double y;
138  double z;
140  double weight;
141  double fraction;
143  double rho;
144  double delta;
146  bool isBorder;
147  bool isHalo;
150 
151  Hexel(const HGCRecHit &hit, DetId id_in, bool isHalf, const hgcal::RecHitTools *tools_in) :
152  x(0.),y(0.),z(0.),isHalfCell(isHalf),
153  weight(0.), fraction(1.0), detid(id_in), rho(0.), delta(0.),
154  nearestHigher(-1), isBorder(false), isHalo(false),
155  clusterIndex(-1), tools(tools_in)
156  {
157  const GlobalPoint position( std::move( tools->getPosition( detid ) ) );
158 
159  weight = hit.energy();
160  x = position.x();
161  y = position.y();
162  z = position.z();
163 
164  }
165  Hexel() :
166  x(0.),y(0.),z(0.),isHalfCell(false),
167  weight(0.), fraction(1.0), detid(), rho(0.), delta(0.),
168  nearestHigher(-1), isBorder(false), isHalo(false),
169  clusterIndex(-1),
170  tools(0)
171  {}
172  bool operator > (const Hexel& rhs) const {
173  return (rho > rhs.rho);
174  }
175 
176  };
177 
180 
181 
182  // A vector of vectors of KDNodes holding an Hexel in the clusters - to be used to build CaloClusters of DetIds
183  std::vector< std::vector<KDNode> > current_v;
184 
185  std::vector<size_t> sort_by_delta(const std::vector<KDNode> &v){
186  std::vector<size_t> idx(v.size());
187  for (size_t i = 0; i != idx.size(); ++i) idx[i] = i;
188  sort(idx.begin(), idx.end(),
189  [&v](size_t i1, size_t i2) {return v[i1].data.delta > v[i2].data.delta;});
190  return idx;
191  }
192 
193  std::vector<std::vector<Hexel> > points; //a vector of vectors of hexels, one for each layer
194  //@@EM todo: the number of layers should be obtained programmatically - the range is 1-n instead of 0-n-1...
195 
196  //these functions should be in a helper class.
197  inline double distance2(const Hexel &pt1, const Hexel &pt2) { //distance squared
198  const double dx = pt1.x - pt2.x;
199  const double dy = pt1.y - pt2.y;
200  return (dx*dx + dy*dy);
201  } //distance squaredq
202  inline double distance(const Hexel &pt1, const Hexel &pt2) { //2-d distance on the layer (x-y)
203  return std::sqrt(distance2(pt1,pt2));
204  }
205  double calculateLocalDensity(std::vector<KDNode> &, KDTree &); //return max density
206  double calculateDistanceToHigher(std::vector<KDNode> &, KDTree &);
207  int findAndAssignClusters(std::vector<KDNode> &, KDTree &, double, KDTreeBox &);
208  math::XYZPoint calculatePosition(std::vector<KDNode> &);
209 
210  // attempt to find subclusters within a given set of hexels
211  std::vector<unsigned> findLocalMaximaInCluster(const std::vector<KDNode>&);
212  math::XYZPoint calculatePositionWithFraction(const std::vector<KDNode>&, const std::vector<double>&);
213  double calculateEnergyWithFraction(const std::vector<KDNode>&, const std::vector<double>&);
214  // outputs
215  void shareEnergy(const std::vector<KDNode>&,
216  const std::vector<unsigned>&,
217  std::vector<std::vector<double> >&);
218  };
219 
220 #endif
double calculateDistanceToHigher(std::vector< KDNode > &, KDTree &)
double calculateEnergyWithFraction(const std::vector< KDNode > &, const std::vector< double > &)
hgcal::RecHitTools rhtools_
std::vector< std::vector< KDNode > > current_v
math::XYZPoint calculatePositionWithFraction(const std::vector< KDNode > &, const std::vector< double > &)
double distance(const Hexel &pt1, const Hexel &pt2)
reco::CaloCluster::AlgoId algoId
static const unsigned int maxlayer
void getEventSetup(const edm::EventSetup &es)
double calculateLocalDensity(std::vector< KDNode > &, KDTree &)
T y() const
Definition: PV3DBase.h:63
math::XYZPoint Point
point in the space
void makeClusters(const HGCRecHitCollection &hits)
void getEventSetup(const edm::EventSetup &)
Definition: RecHitTools.cc:66
std::vector< size_t > sort_by_delta(const std::vector< KDNode > &v)
HGCalImagingAlgo(float delta_c_in, double kappa_in, double ecut_in, double showerSigma, reco::CaloCluster::AlgoId algoId_in, VerbosityLevel the_verbosity=pERROR)
void shareEnergy(const std::vector< KDNode > &, const std::vector< unsigned > &, std::vector< std::vector< double > > &)
float energy() const
Definition: CaloRecHit.h:17
T sqrt(T t)
Definition: SSEVec.h:18
unsigned int cluster_offset
T z() const
Definition: PV3DBase.h:64
std::vector< std::vector< Hexel > > points
VerbosityLevel verbosity
std::vector< size_t > sorted_indices(const std::vector< T > &v)
HGCalImagingAlgo(float delta_c_in, double kappa_in, double ecut_in, reco::CaloCluster::AlgoId algoId_in, VerbosityLevel the_verbosity=pERROR)
std::vector< reco::BasicCluster > getClusters(bool)
std::vector< unsigned > findLocalMaximaInCluster(const std::vector< KDNode > &)
const hgcal::RecHitTools * tools
Definition: DetId.h:18
XYZPointD XYZPoint
point in space with cartesian internal representation
Definition: Point3D.h:12
GlobalPoint getPosition(const DetId &id) const
Definition: RecHitTools.cc:73
math::XYZPoint calculatePosition(std::vector< KDNode > &)
void setVerbosity(VerbosityLevel the_verbosity)
KDTreeLinkerAlgo< Hexel, 2 > KDTree
fixed size matrix
Hexel(const HGCRecHit &hit, DetId id_in, bool isHalf, const hgcal::RecHitTools *tools_in)
double distance2(const Hexel &pt1, const Hexel &pt2)
static int position[264][3]
Definition: ReadPGInfo.cc:509
bool operator>(const Hexel &rhs) const
virtual ~HGCalImagingAlgo()
int findAndAssignClusters(std::vector< KDNode > &, KDTree &, double, KDTreeBox &)
T x() const
Definition: PV3DBase.h:62
KDTreeNodeInfoT< Hexel, 2 > KDNode
Power< A, B >::type pow(const A &a, const B &b)
Definition: Power.h:40
def move(src, dest)
Definition: eostools.py:510
std::vector< reco::BasicCluster > clusters_v