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