stablejet is hosted by Hepforge, IPPP Durham
StableJet
PtPartitioningIndex.cc
1 #include <cassert>
2 #include <algorithm>
3 #include <limits.h>
4 
5 #include "PtPartitioningIndex.h"
6 
7 namespace stab {
8 
9  unsigned PtPartitioningIndex::max_element( const unsigned * clu, unsigned size ) const
10  {
11  unsigned max = 0U;
12  for ( unsigned i=0; i != size; i++ ) {
13  const unsigned tmp = clu[i];
14  if ( tmp > max && tmp < UINT_MAX ) {
15  max = tmp;
16  }
17  }
18 
19  return max;
20 
21  }
22 
23  double PtPartitioningIndex::index(const PseudoJet* inputParticles,
24  const unsigned* clustering1,
25  const unsigned* clustering2,
26  const unsigned len) const
27  {
28  assert(inputParticles);
29  assert(clustering1);
30  assert(clustering2);
31  assert(len);
32 
33  //unsigned n1 = *std::max_element(clustering1, clustering1+len)+1U;
34  //unsigned n2 = *std::max_element(clustering2, clustering2+len)+1U;
35  unsigned n1 = this->max_element(clustering1,len)+1U;
36  unsigned n2 = this->max_element(clustering2,len)+1U;
37 
38  if (n1 > n2)
39  {
40  std::swap(clustering1, clustering2);
41  std::swap(n1, n2);
42  }
43 
44  const unsigned matlen = n1*n2;
45  assert(matlen);
46 
47  if (storage_.size() < matlen)
48  storage_.resize(matlen);
49  double* data = &storage_[0];
50 
51  for (unsigned i=0; i<matlen; ++i)
52  data[i] = 0.0;
53 
54  long double ptsum = 0.0L;
55  long double ptsumsq = 0.0L;
56  for (unsigned i=0; i<len; ++i)
57  {
58  if ( clustering1[i] == UINT_MAX || clustering2[i] == UINT_MAX ) continue;
59  const double pt = inputParticles[i].pt();
60  ptsum += pt;
61  ptsumsq += pt*pt;
62  data[clustering1[i]*n2 + clustering2[i]] += pt;
63  }
64  assert(ptsum > 0.0L);
65 
66  return processInterClusterMatrix(data, n1, n2, ptsum, ptsumsq);
67  }
68 
69  double BestPtMatchIndex::processInterClusterMatrix(
70  const double* mat, const unsigned nrows,
71  const unsigned ncols, const double etSum,
72  double /* ptSumsq */) const
73  {
74  const unsigned matlen = nrows*ncols;
75  if (storage_.size() < matlen)
76  storage_.resize(matlen);
77  ElementSort* data = &storage_[0];
78 
79  const unsigned nflags = nrows + ncols;
80  if (intstorage_.size() < nflags)
81  intstorage_.resize(nflags);
82  int *rowTaken = &intstorage_[0];
83  int *colTaken = rowTaken + nrows;
84  for (unsigned i=0; i<nflags; ++i)
85  rowTaken[i] = 0;
86 
87  ElementSort* d = data;
88  for (unsigned i=0; i<nrows; ++i)
89  for (unsigned j=0; j<ncols; ++j, ++d)
90  {
91  d->ptsum = *mat++;
92  d->row = i;
93  d->col = j;
94  }
95 
96  std::sort(data, data+matlen);
97  long double usedsum = 0.0L;
98  ElementSort* const dmax = data + matlen;
99  unsigned k = 0;
100  const unsigned kmax = nrows < ncols ? nrows : ncols;
101  for (d = data; d!=dmax && k<kmax; ++d)
102  if (!rowTaken[d->row] && !colTaken[d->col])
103  {
104  rowTaken[d->row] = 1;
105  colTaken[d->col] = 1;
106  usedsum += d->ptsum;
107  ++k;
108  }
109 
110  return static_cast<double>(usedsum)/etSum;
111  }
112 
113  double TransverseRandIndex2::processInterClusterMatrix(
114  const double* mat, const unsigned nrows,
115  const unsigned ncols, const double etSum,
116  const double etSumsq) const
117  {
118  const unsigned nsums = nrows + ncols;
119  if (storage_.size() < nsums)
120  storage_.resize(nsums);
121  long double* rowSums = &storage_[0];
122  long double* colSums = rowSums + nrows;
123  for (unsigned i=0; i<ncols; ++i)
124  colSums[i] = 0.0L;
125 
126  long double sumsq = 0.0L;
127  for (unsigned i=0; i<nrows; ++i)
128  {
129  long double rsum = 0.0L;
130  const double* m = mat + i*ncols;
131  for (unsigned j=0; j<ncols; ++j)
132  {
133  rsum += m[j];
134  colSums[j] += m[j];
135  sumsq += m[j]*m[j];
136  }
137  rowSums[i] = rsum;
138  }
139 
140  long double crsq = 0.0L;
141  for (unsigned i=0; i<nsums; ++i)
142  crsq += rowSums[i]*rowSums[i];
143 
144  const double norm = 0.5*(etSum*etSum - etSumsq);
145  assert(norm > 0.0);
146 
147  return 1.0 - (0.5*crsq - sumsq)/norm;
148  }
149 }
double index(const PseudoJet *inputParticles, const unsigned *clustering1, const unsigned *clustering2, unsigned size) const
Calculate the stability index.