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