FastJet 3.5.0
Loading...
Searching...
No Matches
LazyTiling25.cc
1//FJSTARTHEADER
2// $Id$
3//
4// Copyright (c) 2005-2025, 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 <algorithm>
33#include "fastjet/internal/LazyTiling25.hh"
34#include "fastjet/internal/TilingExtent.hh"
35using namespace std;
36
37
38FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
39
40LazyTiling25::LazyTiling25(ClusterSequence & cs) :
41 _cs(cs), _jets(cs.jets())
42 //, _minheap(_jets.size())
43{
44#ifdef INSTRUMENT2
45 _ncall = 0; // gps tmp
46 _ncall_dtt = 0; // gps tmp
47#endif // INSTRUMENT2
48 _Rparam = cs.jet_def().R();
49 _R2 = _Rparam * _Rparam;
50 _invR2 = 1.0 / _R2;
51 _initialise_tiles();
52}
53
54
55//----------------------------------------------------------------------
56/// Set up the tiles:
57/// - decide the range in eta
58/// - allocate the tiles
59/// - set up the cross-referencing info between tiles
60///
61/// The neighbourhood of a tile is set up as follows
62///
63/// LLRRR
64/// LLRRR
65/// LLXRR
66/// LLLRR
67/// LLLRR
68///
69/// such that tiles is an array containing XLL..LLRR..RR with pointers
70/// | \ RH_tiles
71/// \ surrounding_tiles
72///
73/// with appropriate precautions when close to the edge of the tiled
74/// region.
75///
76void LazyTiling25::_initialise_tiles() {
77
78 // first decide tile sizes (with a lower bound to avoid huge memory use with
79 // very small R)
80 double default_size = max(0.1,_Rparam)/2;
81 _tile_size_eta = default_size;
82 // it makes no sense to go below 5 tiles in phi -- 5 tiles is
83 // sufficient to make sure all pair-wise combinations up to pi in
84 // phi are possible
85 _n_tiles_phi = max(5,int(floor(twopi/default_size)));
86 _tile_size_phi = twopi / _n_tiles_phi; // >= _Rparam and fits in 2pi
87
88#define _FASTJET_TILING25_USE_TILING_ANALYSIS_
89#ifdef _FASTJET_TILING25_USE_TILING_ANALYSIS_
90 // testing
91 TilingExtent tiling_analysis(_cs);
92 _tiles_eta_min = tiling_analysis.minrap();
93 _tiles_eta_max = tiling_analysis.maxrap();
94 //cout << "Using timing analysis " << " " << _tiles_eta_min << " " << _tiles_eta_max << endl;
95#else // not _FASTJET_TILING25_USE_TILING_ANALYSIS_
96 // always include zero rapidity in the tiling region
97 _tiles_eta_min = 0.0;
98 _tiles_eta_max = 0.0;
99 // but go no further than following
100 const double maxrap = 7.0;
101
102 // and find out how much further one should go
103 for(unsigned int i = 0; i < _jets.size(); i++) {
104 double eta = _jets[i].rap();
105 // first check if eta is in range -- to avoid taking into account
106 // very spurious rapidities due to particles with near-zero kt.
107 if (abs(eta) < maxrap) {
108 if (eta < _tiles_eta_min) {_tiles_eta_min = eta;}
109 if (eta > _tiles_eta_max) {_tiles_eta_max = eta;}
110 }
111 }
112 //cout << "NOT using timing analysis " << " " << _tiles_eta_min << " " << _tiles_eta_max << endl;
113#endif // _FASTJET_TILING25_USE_TILING_ANALYSIS_
114
115
116 // now adjust the values of the ieta extents
117
118 // 2014-07-18: the following commented piece of code gives speed
119 // improvements for large-R jets, but occasionally it appears to
120 // hang, e.g. on
121 // gunzip -c < ../data/Pythia-PtMin50-LHC-10kev.dat.gz | time ./example/fastjet_timing_plugins -R 1000 -nev 1000 -strategy -6 -antikt -rapmax 5.0 -repeat 1 -write
122 // 2014-07-23: this was understood because tile centres assumed
123 // tiles_ieta_min = tiles_eta_min (no longer true)
124 //
125#define FASTJET_LAZY25_MIN3TILESY
126#ifdef FASTJET_LAZY25_MIN3TILESY
127 if (_tiles_eta_max - _tiles_eta_min < 3*_tile_size_eta) {
128 // if we have a rapidity coverage that is small compared to the
129 // tile size then we can adjust the grid in rapidity so as to
130 // have exactly 3 tiles. This can give relevant speed improvements
131 // for large R jets
132 _tile_size_eta = (_tiles_eta_max - _tiles_eta_min)/3;
133 _tiles_ieta_min = 0;
134 _tiles_ieta_max = 2;
135 // the eta max value is being taken as the lower edge of the
136 // highest-y tile
137 _tiles_eta_max -= _tile_size_eta;
138 } else {
139#endif //FASTJET_LAZY25_MIN3TILESY
140 _tiles_ieta_min = int(floor(_tiles_eta_min/_tile_size_eta));
141 _tiles_ieta_max = int(floor( _tiles_eta_max/_tile_size_eta));
142 _tiles_eta_min = _tiles_ieta_min * _tile_size_eta;
143 _tiles_eta_max = _tiles_ieta_max * _tile_size_eta;
144#ifdef FASTJET_LAZY25_MIN3TILESY
145 }
146#endif
147 _tile_half_size_eta = _tile_size_eta * 0.5;
148 _tile_half_size_phi = _tile_size_phi * 0.5;
149
150 // set up information about whether we need to allow for "periodic"
151 // wrapping tests in delta_phi calculations
152 vector<bool> use_periodic_delta_phi(_n_tiles_phi, false);
153 if (_n_tiles_phi <= 5) {
154 fill(use_periodic_delta_phi.begin(), use_periodic_delta_phi.end(), true);
155 } else {
156 use_periodic_delta_phi[0] = true;
157 use_periodic_delta_phi[1] = true;
158 use_periodic_delta_phi[_n_tiles_phi-2] = true;
159 use_periodic_delta_phi[_n_tiles_phi-1] = true;
160 }
161
162 // allocate the tiles
163 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
164
165 // now set up the cross-referencing between tiles
166 for (int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
167 for (int iphi = 0; iphi < _n_tiles_phi; iphi++) {
168 Tile25 * tile = & _tiles[_tile_index(ieta,iphi)];
169 // no jets in this tile yet
170 tile->head = NULL; // first element of tiles points to itself
171 tile->begin_tiles[0] = tile;
172 Tile25 ** pptile = & (tile->begin_tiles[0]);
173 pptile++;
174 //
175 // set up L's in column to the left of X
176 tile->surrounding_tiles = pptile;
177 if (ieta > _tiles_ieta_min) {
178 // with the itile subroutine, we can safely run tiles from
179 // idphi=-1 to idphi=+1, because it takes care of
180 // negative and positive boundaries
181 for (int idphi = -2; idphi <=+2; idphi++) {
182 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
183 pptile++;
184 }
185 }
186 // and the column twice to left of X
187 if (ieta > _tiles_ieta_min + 1) {
188 // with the itile subroutine, we can safely run tiles from
189 // idphi=-1 to idphi=+1, because it takes care of
190 // negative and positive boundaries
191 for (int idphi = -2; idphi <= +2; idphi++) {
192 *pptile = & _tiles[_tile_index(ieta-2,iphi+idphi)];
193 pptile++;
194 }
195 }
196 // now set up last two L's (below X)
197 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
198 pptile++;
199 *pptile = & _tiles[_tile_index(ieta,iphi-2)];
200 pptile++;
201 // set up first two R's (above X)
202 tile->RH_tiles = pptile;
203 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
204 pptile++;
205 *pptile = & _tiles[_tile_index(ieta,iphi+2)];
206 pptile++;
207 // set up remaining R's, to the right of X
208 if (ieta < _tiles_ieta_max) {
209 for (int idphi = -2; idphi <= +2; idphi++) {
210 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
211 pptile++;
212 }
213 }
214 // set up remaining R's, and two cols to the right of X
215 if (ieta < _tiles_ieta_max - 1) {
216 for (int idphi = -2; idphi <= +2; idphi++) {
217 *pptile = & _tiles[_tile_index(ieta+2,iphi+idphi)];
218 pptile++;
219 }
220 }
221 // now put semaphore for end tile
222 tile->end_tiles = pptile;
223 // finally make sure tiles are untagged
224 tile->tagged = false;
225 // and store the information about periodicity in phi
226 tile->use_periodic_delta_phi = use_periodic_delta_phi[iphi];
227 // and ensure max distance is sensibly initialised
228 tile->max_NN_dist = 0;
229 // and also position of centre of tile
230 tile->eta_centre = (ieta-_tiles_ieta_min+0.5)*_tile_size_eta + _tiles_eta_min;
231 tile->phi_centre = (iphi+0.5)*_tile_size_phi;
232 }
233 }
234
235}
236
237//----------------------------------------------------------------------
238/// return the tile index corresponding to the given eta,phi point
239int LazyTiling25::_tile_index(const double eta, const double phi) const {
240 int ieta, iphi;
241 if (eta <= _tiles_eta_min) {ieta = 0;}
242 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
243 else {
244 //ieta = int(floor((eta - _tiles_eta_min) / _tile_size_eta));
245 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
246 // following needed in case of rare but nasty rounding errors
247 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
248 ieta = _tiles_ieta_max-_tiles_ieta_min;}
249 }
250 // allow for some extent of being beyond range in calculation of phi
251 // as well
252 //iphi = (int(floor(phi/_tile_size_phi)) + _n_tiles_phi) % _n_tiles_phi;
253 // with just int and no floor, things run faster but beware
254 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
255 return (iphi + ieta * _n_tiles_phi);
256}
257
258
259//----------------------------------------------------------------------
260// sets up information regarding the tiling of the given jet
261inline void LazyTiling25::_tj_set_jetinfo( TiledJet * const jet,
262 const int _jets_index) {
263 // first call the generic setup
264 _bj_set_jetinfo<>(jet, _jets_index);
265
266 // Then do the setup specific to the tiled case.
267
268 // Find out which tile it belonds to
269 jet->tile_index = _tile_index(jet->eta, jet->phi);
270
271 // Insert it into the tile's linked list of jets
272 Tile25 * tile = &_tiles[jet->tile_index];
273 jet->previous = NULL;
274 jet->next = tile->head;
275 if (jet->next != NULL) {jet->next->previous = jet;}
276 tile->head = jet;
277}
278
279
280//----------------------------------------------------------------------
281void LazyTiling25::_bj_remove_from_tiles(TiledJet * const jet) {
282 Tile25 * tile = & _tiles[jet->tile_index];
283
284 if (jet->previous == NULL) {
285 // we are at head of the tile, so reset it.
286 // If this was the only jet on the tile then tile->head will now be NULL
287 tile->head = jet->next;
288 } else {
289 // adjust link from previous jet in this tile
290 jet->previous->next = jet->next;
291 }
292 if (jet->next != NULL) {
293 // adjust backwards-link from next jet in this tile
294 jet->next->previous = jet->previous;
295 }
296}
297
298
299//----------------------------------------------------------------------
300/// output the contents of the tiles
301void LazyTiling25::_print_tiles(TiledJet * briefjets ) const {
302 for (vector<Tile25>::const_iterator tile = _tiles.begin();
303 tile < _tiles.end(); tile++) {
304 cout << "Tile " << tile - _tiles.begin()
305 << " at " << setw(10) << tile->eta_centre << "," << setw(10) << tile->phi_centre
306 << " = ";
307 vector<int> list;
308 for (TiledJet * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
309 list.push_back(jetI-briefjets);
310 //cout <<" "<<jetI-briefjets;
311 }
312 sort(list.begin(),list.end());
313 for (unsigned int i = 0; i < list.size(); i++) {cout <<" "<<list[i];}
314 cout <<"\n";
315 }
316}
317
318
319//----------------------------------------------------------------------
320/// Add to the vector tile_union the tiles that are in the neighbourhood
321/// of the specified tile_index, including itself -- start adding
322/// from position n_near_tiles-1, and increase n_near_tiles as
323/// you go along (could have done it more C++ like with vector with reserved
324/// space, but fear is that it would have been slower, e.g. checking
325/// for end of vector at each stage to decide whether to resize it)
326void LazyTiling25::_add_neighbours_to_tile_union(const int tile_index,
327 vector<int> & tile_union, int & n_near_tiles) const {
328 for (Tile25 * const * near_tile = _tiles[tile_index].begin_tiles;
329 near_tile != _tiles[tile_index].end_tiles; near_tile++){
330 // get the tile number
331 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
332 n_near_tiles++;
333 }
334}
335
336
337//----------------------------------------------------------------------
338/// Like _add_neighbours_to_tile_union, but only adds neighbours if
339/// their "tagged" status is false; when a neighbour is added its
340/// tagged status is set to true.
341inline void LazyTiling25::_add_untagged_neighbours_to_tile_union(
342 const int tile_index,
343 vector<int> & tile_union, int & n_near_tiles) {
344 for (Tile25 ** near_tile = _tiles[tile_index].begin_tiles;
345 near_tile != _tiles[tile_index].end_tiles; near_tile++){
346 if (! (*near_tile)->tagged) {
347 (*near_tile)->tagged = true;
348 // get the tile number
349 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
350 n_near_tiles++;
351 }
352 }
353}
354
355//----------------------------------------------------------------------
356/// Like _add_neighbours_to_tile_union, but adds tiles that are
357/// "neighbours" of a jet (rather than a tile) and only if a
358/// neighbouring tile's max_NN_dist is >= the distance between the jet
359/// and the nearest point on the tile. It ignores tiles that have
360/// already been tagged.
361inline void LazyTiling25::_add_untagged_neighbours_to_tile_union_using_max_info(
362 const TiledJet * jet,
363 vector<int> & tile_union, int & n_near_tiles) {
364 Tile25 & tile = _tiles[jet->tile_index];
365
366 for (Tile25 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
367 if ((*near_tile)->tagged) continue;
368 // here we are not allowed to miss a tile due to some rounding
369 // error. We therefore allow for a margin of security
370 double dist = _distance_to_tile(jet, *near_tile) - tile_edge_security_margin;
371 // cout << " max info looked at tile " << *near_tile - &_tiles[0]
372 // << ", dist = " << dist << " " << (*near_tile)->max_NN_dist
373 // << " -> diff = " << dist-(*near_tile)->max_NN_dist << endl;
374 if (dist > (*near_tile)->max_NN_dist) continue;
375
376 // cout << " max info tagged tile " << *near_tile - &_tiles[0] << endl;
377 (*near_tile)->tagged = true;
378 // get the tile number
379 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
380 n_near_tiles++;
381 }
382}
383
384////--------TMPTMPTMPTMPTMP-----GPS TEMP--------------------
385//ostream & operator<<(ostream & ostr, const TiledJet & jet) {
386// ostr << "j" << setw(3) << jet._jets_index << ":pt2,rap,phi=" ; ostr.flush();
387// ostr << jet.kt2 << ","; ostr.flush();
388// ostr << jet.eta << ","; ostr.flush();
389// ostr << jet.phi; ostr.flush();
390// ostr << ", tile=" << jet.tile_index; ostr.flush();
391// return ostr;
392//}
393
394
395//----------------------------------------------------------------------
396/// returns a particle's distance to the edge of the specified tile
397inline double LazyTiling25::_distance_to_tile(const TiledJet * bj, const Tile25 * tile)
398#ifdef INSTRUMENT2
399 {
400 _ncall_dtt++; // GPS tmp
401#else
402 const {
403#endif // INSTRUMENT2
404 // Note the careful way of checking the minimum potential deta:
405 // unlike the phi case below, we don't calculate the distance to the
406 // centre and subtract spacing/2. This is because of issue of
407 // boundary tiles, which can extend far beyond spacing/2 in eta.
408 // Using the positions of tile centers should instead be safe.
409 double deta;
410 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
411 //else deta = std::abs(bj->eta - tile->eta_centre) - 0.5*_tile_size_eta;
412 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
413 // ------
414 // |
415 // A | B
416 // ------
417 // |
418 // C | D
419 // ------
420
421 double dphi = std::abs(bj->phi - tile->phi_centre);
422 if (dphi > pi) dphi = twopi-dphi;
423 dphi -= _tile_half_size_phi;
424 //dphi -= 0.5*_tile_size_phi;
425 if (dphi < 0) dphi = 0;
426
427 return dphi*dphi + deta*deta;
428}
429
430
431
432
433//----------------------------------------------------------------------
434/// looks at distance between jetX and jetI and updates the NN
435/// information if relevant; also pushes identity of jetI onto
436/// the vector of jets for minheap, to signal that it will have
437/// to be handled later.
438///
439/// GPS TEMP GPS TMP: REMOVE THIS LATER: EVEN LABELLED AS INLINE, THE
440/// CALL ADDS A SUBSTANTIAL PENALTY...
441inline void LazyTiling25::_update_jetX_jetI_NN(TiledJet * jetX, TiledJet * jetI, vector<TiledJet *> & jets_for_minheap) {
442 double dist = _bj_dist(jetI,jetX);
443 if (dist < jetI->NN_dist) {
444 if (jetI != jetX) {
445 jetI->NN_dist = dist;
446 jetI->NN = jetX;
447 // label jetI as needing heap action...
448 if (!jetI->minheap_update_needed()) {
449 jetI->label_minheap_update_needed();
450 jets_for_minheap.push_back(jetI);
451 }
452 }
453 }
454 if (dist < jetX->NN_dist) {
455 if (jetI != jetX) {
456 jetX->NN_dist = dist;
457 jetX->NN = jetI;}
458 }
459}
460
461
462inline void LazyTiling25::_set_NN(TiledJet * jetI,
463 vector<TiledJet *> & jets_for_minheap) {
464 jetI->NN_dist = _R2;
465 jetI->NN = NULL;
466 // label jetI as needing heap action...
467 if (!jetI->minheap_update_needed()) {
468 jetI->label_minheap_update_needed();
469 jets_for_minheap.push_back(jetI);}
470 // now go over tiles that are neighbours of I (include own tile)
471 Tile25 * tile_ptr = &_tiles[jetI->tile_index];
472 //if (tile_ptr->is_near_zero_phi(_tile_size_phi)) {
473 for (Tile25 ** near_tile = tile_ptr->begin_tiles;
474 near_tile != tile_ptr->end_tiles; near_tile++) {
475 // for own tile, this will be zero automatically: should we be clever
476 // and skip the test? (With some doubling of code?)
477 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile)) continue;
478 // and then over the contents of that tile
479 for (TiledJet * jetJ = (*near_tile)->head;
480 jetJ != NULL; jetJ = jetJ->next) {
481 double dist = _bj_dist(jetI,jetJ);
482 if (dist < jetI->NN_dist && jetJ != jetI) {
483 jetI->NN_dist = dist; jetI->NN = jetJ;
484 }
485 }
486 }
487 // } else {
488 // // second copy that exploits the fact that for this tile we needn't worry
489 // // about periodicity
490 // for (Tile25 ** near_tile = tile_ptr->begin_tiles;
491 // near_tile != tile_ptr->end_tiles; near_tile++) {
492 // // for own tile, this will be zero automatically: should we be clever
493 // // and skip the test? (With some doubling of code?)
494 // if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile)) continue;
495 // // and then over the contents of that tile
496 // for (TiledJet * jetJ = (*near_tile)->head;
497 // jetJ != NULL; jetJ = jetJ->next) {
498 // double dist = _bj_dist_not_periodic(jetI,jetJ);
499 // if (dist < jetI->NN_dist && jetJ != jetI) {
500 // jetI->NN_dist = dist; jetI->NN = jetJ;
501 // }
502 // }
503 // }
504 // }
505}
506
507
508void LazyTiling25::run() {
509
510 //_initialise_tiles();
511
512 int n = _jets.size();
513 if (n == 0) return;
514
515 TiledJet * briefjets = new TiledJet[n];
516 TiledJet * jetA = briefjets, * jetB;
517 // avoid warning about uninitialised oldB below;
518 // only valid for n>=1 (hence the test n==0 test above)
519 TiledJet oldB = briefjets[0];
520
521 // will be used quite deep inside loops, but declare it here so that
522 // memory (de)allocation gets done only once
523 vector<int> tile_union(3*25);
524
525 // initialise the basic jet info
526 for (int i = 0; i< n; i++) {
527 _tj_set_jetinfo(jetA, i);
528 //cout << i<<": "<<jetA->tile_index<<"\n";
529 jetA++; // move on to next entry of briefjets
530 }
531 TiledJet * head = briefjets; // a nicer way of naming start
532
533 // set up the initial nearest neighbour information
534 vector<Tile25>::iterator tile;
535 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
536 // first do it on this tile
537 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
538 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
539 double dist = _bj_dist_not_periodic(jetA,jetB);
540 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
541 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
542 }
543 }
544 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
545 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
546 }
547 }
548 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
549 if (tile->use_periodic_delta_phi) {
550 // then do it for RH tiles;
551 for (Tile25 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
552 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
553 double dist_to_tile = _distance_to_tile(jetA, *RTile);
554 // it only makes sense to do a tile if jetA is close enough to the Rtile
555 // either for a jet in the Rtile to be closer to jetA than it's current NN
556 // or if jetA could be closer to something in the Rtile than the largest
557 // NN distance within the RTile.
558 //
559 // GPS note: also tried approach where we perform only the
560 // first test and run over all surrounding tiles
561 // (not just RH ones). The test is passed less
562 // frequently, but one is running over more tiles
563 // and on balance, for the trial event we used, it's
564 // a bit slower.
565 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
566 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
567 if (relevant_for_jetA || relevant_for_RTile) {
568 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
569 double dist = _bj_dist(jetA,jetB);
570 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
571 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
572 }
573 }
574 }
575 }
576 } else {
577 // this second version of the code uses the faster
578 // "not_periodic" version because it knows that the tile is
579 // sufficiently far from the edge.
580 for (Tile25 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
581 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
582 double dist_to_tile = _distance_to_tile(jetA, *RTile);
583 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
584 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
585 if (relevant_for_jetA || relevant_for_RTile) {
586 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
587 double dist = _bj_dist_not_periodic(jetA,jetB);
588 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
589 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
590 }
591 }
592 }
593 }
594 }
595 // no need to do it for LH tiles, since they are implicitly done
596 // when we set NN for both jetA and jetB on the RH tiles.
597 }
598 // Now update the max_NN_dist within each tile. Not strictly
599 // necessary, because existing max_NN_dist is an upper bound. but
600 // costs little and may give some efficiency gain later.
601 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
602 tile->max_NN_dist = 0;
603 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
604 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
605 }
606 }
607
608#ifdef INSTRUMENT2
609 cout << "intermediate ncall, dtt = " << _ncall << " " << _ncall_dtt << endl; // GPS tmp
610#endif // INSTRUMENT2
611
612 // GPS debugging
613 // _print_tiles(briefjets);
614 // for (jetB = briefjets; jetB < briefjets+n; jetB++) {
615 // cout << "Tiled jet " << jetB->_jets_index << " has NN " << jetB->NN-briefjets << endl;
616 // }
617
618 vector<double> diJs(n);
619 for (int i = 0; i < n; i++) {
620 diJs[i] = _bj_diJ(&briefjets[i]);
621 briefjets[i].label_minheap_update_done();
622 }
623 MinHeap minheap(diJs);
624 // have a stack telling us which jets we'll have to update on the heap
625 vector<TiledJet *> jets_for_minheap;
626 jets_for_minheap.reserve(n);
627
628 // now run the recombination loop
629 while (n > 0) {
630
631 double diJ_min = minheap.minval() *_invR2;
632 jetA = head + minheap.minloc();
633
634 // do the recombination between A and B
635 jetB = jetA->NN;
636
637 if (jetB != NULL) {
638 // jet-jet recombination
639 // If necessary relabel A & B to ensure jetB < jetA, that way if
640 // the larger of them == newtail then that ends up being jetA and
641 // the new jet that is added as jetB is inserted in a position that
642 // has a future!
643 if (jetA < jetB) {std::swap(jetA,jetB);}
644
645 int nn; // new jet index
646 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
647
648 // what was jetB will now become the new jet
649 _bj_remove_from_tiles(jetA);
650 oldB = * jetB; // take a copy because we will need it...
651 _bj_remove_from_tiles(jetB);
652 _tj_set_jetinfo(jetB, nn); // cause jetB to become _jets[nn]
653 // (also registers the jet in the tiling)
654 } else {
655 // jet-beam recombination
656 // get the hist_index
657 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
658 _bj_remove_from_tiles(jetA);
659 }
660
661 // remove the minheap entry for jetA
662 minheap.remove(jetA-head);
663
664 int n_near_tiles = 0;
665
666 // GS comment:
667 //
668 // At this stage, we have perforned the clustering in
669 // ClusterSequence and we need to update the NNs. The objects we
670 // deal with are jetA and oldB (the once that have been clustered)
671 // as well as jetB (the result of the clustering)
672 //
673 // There are two types of objects we need to update:
674 // - find jetB NN
675 // - update the NN of points which had jetA or jetB as their NN
676 //
677 // Wile we find jetB's NN, browsing relevant tiles near jetB, we
678 // also search for points which had jetA or jetB as their
679 // NN. These are tagged. Then we list the relevant tiles where we
680 // can potentially have points to update (i.e. points which had
681 // jetA and oldB as NN) in the yet untagged relevant tiles near
682 // jetA and oldB.
683 //
684 // DEBUG:
685 // if (jetB != NULL) {
686 // cout << "jetA = " << jetA->_jets_index << " (tile " << jetA->tile_index << "), "
687 // << "oldB = " << oldB._jets_index << " (tile " << oldB. tile_index << "), "
688 // << "jetB = " << jetB->_jets_index << " (tile " << jetB->tile_index << ")" << endl;
689 // } else {
690 // cout << "jetA = " << jetA->_jets_index << " (tile " << jetA->tile_index << ")" << endl;
691 // }
692
693 // Initialise jetB's NN distance as well as updating it for other
694 // particles. While doing so, examine whether jetA or old jetB was
695 // some other particle's NN.
696 if (jetB != NULL) {
697 Tile25 & jetB_tile = _tiles[jetB->tile_index];
698 for (Tile25 ** near_tile = jetB_tile.begin_tiles;
699 near_tile != jetB_tile.end_tiles; near_tile++) {
700
701 double dist_to_tile = _distance_to_tile(jetB, *near_tile);
702 // use <= in next line so that on first tile, relevant_for_jetB is
703 // set to true
704 bool relevant_for_jetB = dist_to_tile <= jetB->NN_dist;
705 bool relevant_for_near_tile = dist_to_tile <= (*near_tile)->max_NN_dist;
706 bool relevant = relevant_for_jetB || relevant_for_near_tile;
707
708 // cout << " Relevance of tile " << *near_tile - & _tiles[0]
709 // << " wrt jetB is " << relevant << endl;
710
711 if (! relevant) continue;
712 // now label this tile as having been considered (so that we
713 // don't go over it again later)
714 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
715 (*near_tile)->tagged = true;
716 n_near_tiles++;
717
718 // if going over the neighbouring tile's jets, check anyway
719 // whether A or B were nearest neighbours, since it comes at a
720 // modest cost relative to the distance computation (and we would
721 // in most cases have to do it again later anyway).
722 for (TiledJet * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
723 if (jetI->NN == jetA || jetI->NN == jetB) _set_NN(jetI, jets_for_minheap);
724 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
725 }
726 }
727 }
728
729 // first establish the set of tiles over which we are going to
730 // have to run searches for updated and new nearest-neighbours --
731 // basically a combination of vicinity of the tiles of the two old
732 // and one new jet.
733 int n_done_tiles = n_near_tiles;
734 //cout << "Looking at relevant tiles to update for jetA" << endl;
735 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
736 tile_union, n_near_tiles);
737 if (jetB != NULL) {
738 // cout << "Looking at relevant tiles to update for oldB" << endl;
739 _add_untagged_neighbours_to_tile_union_using_max_info(&oldB,
740 tile_union,n_near_tiles);
741 jetB->label_minheap_update_needed();
742 jets_for_minheap.push_back(jetB);
743 }
744
745
746 // first untag the tiles we have already dealt with
747 for (int itile = 0; itile < n_done_tiles; itile++) {
748 _tiles[tile_union[itile]].tagged = false;
749 }
750 // now run over the tiles that were tagged earlier and that we haven't yet
751 // had a change to visit.
752 for (int itile = n_done_tiles; itile < n_near_tiles; itile++) {
753 Tile25 * tile_ptr = &_tiles[tile_union[itile]];
754 tile_ptr->tagged = false;
755 // run over all jets in the current tile
756 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
757 // see if jetI had jetA or jetB as a NN -- if so recalculate the NN
758 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
759 _set_NN(jetI, jets_for_minheap);
760 }
761 }
762 }
763
764 // deal with jets whose minheap entry needs updating
765 //if (verbose) cout << " jets whose NN was modified: " << endl;
766 while (jets_for_minheap.size() > 0) {
767 TiledJet * jetI = jets_for_minheap.back();
768 jets_for_minheap.pop_back();
769 minheap.update(jetI-head, _bj_diJ(jetI));
770 jetI->label_minheap_update_done();
771 // handle max_NN_dist update for all jets that might have
772 // seen a change (increase) of distance
773 Tile25 & tile_I = _tiles[jetI->tile_index];
774 if (tile_I.max_NN_dist < jetI->NN_dist) tile_I.max_NN_dist = jetI->NN_dist;
775 }
776 n--;
777 }
778
779 // final cleaning up;
780 delete[] briefjets;
781#ifdef INSTRUMENT2
782 cout << "ncall, dtt = " << _ncall << " " << _ncall_dtt << endl; // GPS tmp
783#endif // INSTRUMENT2
784
785}
786
787FASTJET_END_NAMESPACE
const double tile_edge_security_margin
Rounding errors in the Lazy strategies may cause the following problem: when browsing tiles in the vi...