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