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