31#include "fastjet/internal/LazyTiling9SeparateGhosts.hh"
32#include "fastjet/internal/TilingExtent.hh"
36FASTJET_BEGIN_NAMESPACE
41double LazyTiling9SeparateGhosts::ghost_pt2_threshold = 1e-100;
44LazyTiling9SeparateGhosts::LazyTiling9SeparateGhosts(ClusterSequence & cs) :
45 _cs(cs), _jets(cs.jets())
48 _Rparam = cs.jet_def().R();
49 _R2 = _Rparam * _Rparam;
74void LazyTiling9SeparateGhosts::_initialise_tiles() {
78 double default_size = max(0.1,_Rparam);
79 _tile_size_eta = default_size;
83 _n_tiles_phi = max(3,
int(floor(twopi/default_size)));
84 _tile_size_phi = twopi / _n_tiles_phi;
90 const double maxrap = 7.0;
93 for(
unsigned int i = 0; i < _jets.size(); i++) {
94 double eta = _jets[i].rap();
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;}
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;
109 _tile_half_size_eta = _tile_size_eta * 0.5;
110 _tile_half_size_phi = _tile_size_phi * 0.5;
113 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
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)];
121 tile->ghost_head = NULL;
122 tile->begin_tiles[0] = tile;
123 Tile3 ** pptile = & (tile->begin_tiles[0]);
127 tile->surrounding_tiles = pptile;
128 if (ieta > _tiles_ieta_min) {
132 for (
int idphi = -1; idphi <=+1; idphi++) {
133 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
138 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
141 tile->RH_tiles = pptile;
142 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
145 if (ieta < _tiles_ieta_max) {
146 for (
int idphi = -1; idphi <= +1; idphi++) {
147 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
152 tile->end_tiles = pptile;
154 tile->tagged =
false;
156 tile->max_NN_dist = 0;
158 tile->eta_centre = (ieta+0.5)*_tile_size_eta;
159 tile->phi_centre = (iphi+0.5)*_tile_size_phi;
167int LazyTiling9SeparateGhosts::_tile_index(
const double eta,
const double phi)
const {
169 if (eta <= _tiles_eta_min) {ieta = 0;}
170 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
173 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
175 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
176 ieta = _tiles_ieta_max-_tiles_ieta_min;}
182 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
183 return (iphi + ieta * _n_tiles_phi);
189inline void LazyTiling9SeparateGhosts::_tj_set_jetinfo( TiledJet3 *
const jet,
190 const int _jets_index,
bool is_ghost) {
192 _bj_set_jetinfo<>(jet, _jets_index);
195 jet->is_ghost = is_ghost;
198 jet->tile_index = _tile_index(jet->eta, jet->phi);
201 Tile3 * tile = &_tiles[jet->tile_index];
202 jet->previous = NULL;
204 jet->next = tile->ghost_head;
205 tile->ghost_head = jet;
207 jet->next = tile->head;
210 if (jet->next != NULL) {jet->next->previous = jet;}
215void LazyTiling9SeparateGhosts::_bj_remove_from_tiles(TiledJet3 *
const jet) {
216 Tile3 * tile = & _tiles[jet->tile_index];
218 if (jet->previous == NULL) {
222 tile->ghost_head = jet->next;
224 tile->head = jet->next;
228 jet->previous->next = jet->next;
230 if (jet->next != NULL) {
232 jet->next->previous = jet->previous;
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()<<
" = ";
244 for (TiledJet3 * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
245 list.push_back(jetI-briefjets);
248 sort(list.begin(),list.end());
249 for (
unsigned int i = 0; i < list.size(); i++) {cout <<
" "<<list[i];}
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++){
267 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
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;
285 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
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];
302 for (Tile3 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
303 if ((*near_tile)->tagged)
continue;
310 if (dist > (*near_tile)->max_NN_dist)
continue;
313 (*near_tile)->tagged =
true;
315 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
333inline double LazyTiling9SeparateGhosts::_distance_to_tile(
const TiledJet3 * bj,
const Tile3 * tile)
const {
341 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
343 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
352 double dphi = std::abs(bj->phi - tile->phi_centre);
353 if (dphi > pi) dphi = twopi-dphi;
354 dphi -= _tile_half_size_phi;
356 if (dphi < 0) dphi = 0;
358 return dphi*dphi + deta*deta;
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) {
375 jetI->NN_dist = dist;
378 if (!jetI->minheap_update_needed()) {
379 jetI->label_minheap_update_needed();
380 jets_for_minheap.push_back(jetI);
384 if (dist < jetX->NN_dist) {
386 jetX->NN_dist = dist;
392inline void LazyTiling9SeparateGhosts::_set_NN(TiledJet3 * jetI,
393 vector<TiledJet3 *> & jets_for_minheap) {
394 assert(! jetI->is_ghost);
398 if (!jetI->minheap_update_needed()) {
399 jetI->label_minheap_update_needed();
400 jets_for_minheap.push_back(jetI);}
402 Tile3 * tile_ptr = &_tiles[jetI->tile_index];
404 for (Tile3 ** near_tile = tile_ptr->begin_tiles;
405 near_tile != tile_ptr->end_tiles; near_tile++) {
408 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile))
continue;
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;
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;
445void LazyTiling9SeparateGhosts::run() {
449 int ntot = _jets.size();
450 if (ntot == 0)
return;
452 TiledJet3 * briefjets =
new TiledJet3[ntot];
453 TiledJet3 * jetA = briefjets, * jetB;
456 TiledJet3 oldB = briefjets[0];
461 vector<int> tile_union(3*n_tile_neighbours);
463 TiledJet3 * head = briefjets;
469 for (
int i = 0; i< ntot; i++) {
470 bool is_ghost = _jets[i].perp2() < ghost_pt2_threshold;
472 _tj_set_jetinfo(jetA, i, is_ghost);
476 int nreal = jetA - briefjets;
478 for (
int i = 0; i< ntot; i++) {
479 bool is_ghost = _jets[i].perp2() < ghost_pt2_threshold;
481 _tj_set_jetinfo(jetA, i, is_ghost);
488 vector<Tile3>::iterator tile;
489 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
491 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
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;}
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;}
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;
510 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
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);
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;}
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;}
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);
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;}
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;
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();
599 MinHeap minheap(diJs);
601 vector<TiledJet3 *> jets_for_minheap;
602 jets_for_minheap.reserve(ntot);
607 double diJ_min = minheap.minval() *_invR2;
608 jetA = head + minheap.minloc();
619 if (jetA < jetB) {std::swap(jetA,jetB);}
622 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
625 _bj_remove_from_tiles(jetA);
627 _bj_remove_from_tiles(jetB);
628 bool is_ghost =
false;
629 _tj_set_jetinfo(jetB, nn, is_ghost);
634 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
635 _bj_remove_from_tiles(jetA);
640 if (!jetA->is_ghost) {
641 minheap.remove(jetA-head);
654 int n_near_tiles = 0;
657 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
658 tile_union, n_near_tiles);
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);
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++) {
675 double dist_to_tile = _distance_to_tile(jetB, *near_tile);
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;
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);
690 (*near_tile)->tagged =
false;
692 for (TiledJet3 * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
693 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
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;
745 for (
int itile = 0; itile < n_near_tiles; itile++) {
746 Tile3 * tile_ptr = &_tiles[tile_union[itile]];
747 if (!tile_ptr->tagged)
continue;
748 tile_ptr->tagged =
false;
750 for (TiledJet3 * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
752 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
753 _set_NN(jetI, jets_for_minheap);
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();
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;
const double tile_edge_security_margin
Rounding errors in the Lazy strategies may cause the following problem: when browsing tiles in the vi...