FastJet 3.5.2
Loading...
Searching...
No Matches
TilingExtent.cc
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2026, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
5//
6//----------------------------------------------------------------------
7// This file is part of FastJet.
8//
9// FastJet is free software; you can redistribute it and/or modify
10// it under the terms of the GNU General Public License as published by
11// the Free Software Foundation; either version 2 of the License, or
12// (at your option) any later version.
13//
14// The algorithms that underlie FastJet have required considerable
15// development. They are described in the original FastJet paper,
16// hep-ph/0512210 and in the manual, arXiv:1111.6097. If you use
17// FastJet as part of work towards a scientific publication, please
18// quote the version you use and include a citation to the manual and
19// optionally also to hep-ph/0512210.
20//
21// FastJet is distributed in the hope that it will be useful,
22// but WITHOUT ANY WARRANTY; without even the implied warranty of
23// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24// GNU General Public License for more details.
25//
26// You should have received a copy of the GNU General Public License
27// along with FastJet. If not, see <http://www.gnu.org/licenses/>.
28//----------------------------------------------------------------------
29//FJENDHEADER
30
31#include <iomanip>
32#include <limits>
33#include <cmath>
34#include "fastjet/internal/TilingExtent.hh"
35using namespace std;
36
37
38FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
39
41 _determine_rapidity_extent(cs.jets());
42}
43
44TilingExtent::TilingExtent(const vector<PseudoJet> &particles) {
45 _determine_rapidity_extent(particles);
46}
47
48void TilingExtent::_determine_rapidity_extent(const vector<PseudoJet> & particles) {
49 // have a binning of rapidity that goes from -nrap to nrap
50 // in bins of size 1; the left and right-most bins include
51 // include overflows from smaller/larger rapidities
52 int nrap = 20;
53 int nbins = 2*nrap;
54 vector<double> counts(nbins, 0);
55
56 // get the minimum and maximum rapidities and at the same time bin
57 // the multiplicities as a function of rapidity to help decide how
58 // far out it's worth going
59 _minrap = numeric_limits<double>::max();
60 _maxrap = -numeric_limits<double>::max();
61 int ibin;
62 for (unsigned i = 0; i < particles.size(); i++) {
63 // ignore particles with infinite rapidity
64 if (particles[i].E() == abs(particles[i].pz())) continue;
65 double rap = particles[i].rap();
66 if (rap < _minrap) _minrap = rap;
67 if (rap > _maxrap) _maxrap = rap;
68 // now bin the rapidity to decide how far to go with the tiling.
69 // Remember the bins go from ibin=0 (rap=-infinity..-19)
70 // to ibin = nbins-1 (rap=19..infinity for nrap=20)
71 ibin = int(rap+nrap);
72 if (ibin < 0) ibin = 0;
73 if (ibin >= nbins) ibin = nbins - 1;
74 counts[ibin]++;
75 }
76
77 if (_minrap > _maxrap) {
78 // Occurs if particles have E=|pz|; this will usually mean all
79 // rapidities are "infinite" (1e5+|pz|). Special use cases (e.g.
80 // PanScales log-accuracy tests) may have manually rapidities that
81 // are large, but physically correct, while still triggering E=|pz|
82 //
83 // In any case, leaving _minrap > _maxrap leads to a badly
84 // initialised tiling and segfaults, so we set things up, with a
85 // "pretend" range that effectively puts the particles into two
86 // bins, sufficiently distant that that they will usually not
87 // communicate with each other in tiled clutering, but not so
88 // distant that we have a huge number of empty tiles.
89 _minrap = -2.5;
90 _maxrap = 2.5;
91 for (unsigned i = 0; i < particles.size(); i++) {
92 double rap = particles[i].pz() > 0 ? _maxrap : _minrap;
93 ibin = int(rap+nrap);
94 if (ibin < 0) ibin = 0;
95 if (ibin >= nbins) ibin = nbins - 1;
96 counts[ibin]++;
97 }
98 }
99
100 // now figure out the particle count in the busiest bin
101 double max_in_bin = 0;
102 for (ibin = 0; ibin < nbins; ibin++) {
103 if (max_in_bin < counts[ibin]) max_in_bin = counts[ibin];
104 }
105
106 // and find _minrap, _maxrap such that edge bin never contains more
107 // than some fraction of busiest, and at least a few particles; first do
108 // it from left. NB: the thresholds chosen here are largely
109 // guesstimates as to what might work.
110 //
111 // 2014-07-17: in some tests at high multiplicity (100k) and particles going up to
112 // about 7.3, anti-kt R=0.4, we found that 0.25 gave 20% better run times
113 // than the original value of 0.5.
114 const double allowed_max_fraction = 0.25;
115 // the edge bins should also contain at least min_multiplicity particles
116 const double min_multiplicity = 4;
117 // now calculate how much we can accumulate into an edge bin
118 double allowed_max_cumul = floor(max(max_in_bin * allowed_max_fraction, min_multiplicity));
119 // make sure we don't require more particles in a bin than max_in_bin
120 if (allowed_max_cumul > max_in_bin) allowed_max_cumul = max_in_bin;
121
122 // start scan over rapidity bins from the left, to find out minimum rapidity of tiling
123 double cumul_lo = 0;
124 _cumul2 = 0;
125 for (ibin = 0; ibin < nbins; ibin++) {
126 cumul_lo += counts[ibin];
127 if (cumul_lo >= allowed_max_cumul) {
128 double y = ibin-nrap;
129 if (y > _minrap) _minrap = y;
130 break;
131 }
132 }
133 assert(ibin != nbins); // internal consistency check that you found a bin
134 _cumul2 += cumul_lo*cumul_lo;
135
136 // ibin_lo is the index of the leftmost bin that should be considered
137 int ibin_lo = ibin;
138
139 // then do it from right, to find out maximum rapidity of tiling
140 double cumul_hi = 0;
141 for (ibin = nbins-1; ibin >= 0; ibin--) {
142 cumul_hi += counts[ibin];
143 if (cumul_hi >= allowed_max_cumul) {
144 double y = ibin-nrap+1; // +1 here is the rapidity bin width
145 if (y < _maxrap) _maxrap = y;
146 break;
147 }
148 }
149 assert(ibin >= 0); // internal consistency check that you found a bin
150
151 // ibin_hi is the index of the rightmost bin that should be considered
152 int ibin_hi = ibin;
153
154 // consistency check
155 assert(ibin_hi >= ibin_lo);
156
157 // now work out cumul2
158 if (ibin_hi == ibin_lo) {
159 // if there is a single bin (potentially including overflows
160 // from both sides), cumul2 is the square of the total contents
161 // of that bin, which we obtain from cumul_lo and cumul_hi minus
162 // the double counting of part that is contained in both
163 // (putting double
164 _cumul2 = pow(double(cumul_lo + cumul_hi - counts[ibin_hi]), 2);
165 } else {
166 // otherwise we have a straightforward sum of squares of bin
167 // contents
168 _cumul2 += cumul_hi*cumul_hi;
169
170 // now get the rest of the squared bin contents
171 for (ibin = ibin_lo+1; ibin < ibin_hi; ibin++) {
172 _cumul2 += counts[ibin]*counts[ibin];
173 }
174 }
175
176}
177
178
179FASTJET_END_NAMESPACE
deals with clustering
const std::vector< PseudoJet > & jets() const
allow the user to access the internally stored _jets() array, which contains both the initial particl...
TilingExtent(ClusterSequence &cs)
constructor that takes a ClusterSequence in a state where the initial particles have been set up,...