31 #include "fastjet/internal/LazyTiling9SeparateGhosts.hh"
32 #include "fastjet/internal/TilingExtent.hh"
36 FASTJET_BEGIN_NAMESPACE
38 double LazyTiling9SeparateGhosts::ghost_pt2_threshold = 1e-100;
40 LazyTiling9SeparateGhosts::LazyTiling9SeparateGhosts(ClusterSequence & cs) :
41 _cs(cs), _jets(cs.jets())
44 _Rparam = cs.jet_def().R();
45 _R2 = _Rparam * _Rparam;
70 void LazyTiling9SeparateGhosts::_initialise_tiles() {
74 double default_size = max(0.1,_Rparam);
75 _tile_size_eta = default_size;
79 _n_tiles_phi = max(3,
int(floor(twopi/default_size)));
80 _tile_size_phi = twopi / _n_tiles_phi;
86 const double maxrap = 7.0;
89 for(
unsigned int i = 0; i < _jets.size(); i++) {
90 double eta = _jets[i].rap();
93 if (abs(eta) < maxrap) {
94 if (eta < _tiles_eta_min) {_tiles_eta_min = eta;}
95 if (eta > _tiles_eta_max) {_tiles_eta_max = eta;}
100 _tiles_ieta_min = int(floor(_tiles_eta_min/_tile_size_eta));
101 _tiles_ieta_max = int(floor( _tiles_eta_max/_tile_size_eta));
102 _tiles_eta_min = _tiles_ieta_min * _tile_size_eta;
103 _tiles_eta_max = _tiles_ieta_max * _tile_size_eta;
105 _tile_half_size_eta = _tile_size_eta * 0.5;
106 _tile_half_size_phi = _tile_size_phi * 0.5;
109 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
112 for (
int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
113 for (
int iphi = 0; iphi < _n_tiles_phi; iphi++) {
114 Tile3 * tile = & _tiles[_tile_index(ieta,iphi)];
117 tile->ghost_head = NULL;
118 tile->begin_tiles[0] = tile;
119 Tile3 ** pptile = & (tile->begin_tiles[0]);
123 tile->surrounding_tiles = pptile;
124 if (ieta > _tiles_ieta_min) {
128 for (
int idphi = -1; idphi <=+1; idphi++) {
129 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
134 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
137 tile->RH_tiles = pptile;
138 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
141 if (ieta < _tiles_ieta_max) {
142 for (
int idphi = -1; idphi <= +1; idphi++) {
143 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
148 tile->end_tiles = pptile;
150 tile->tagged =
false;
152 tile->max_NN_dist = 0;
154 tile->eta_centre = (ieta+0.5)*_tile_size_eta;
155 tile->phi_centre = (iphi+0.5)*_tile_size_phi;
163 int LazyTiling9SeparateGhosts::_tile_index(
const double eta,
const double phi)
const {
165 if (eta <= _tiles_eta_min) {ieta = 0;}
166 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
169 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
171 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
172 ieta = _tiles_ieta_max-_tiles_ieta_min;}
178 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
179 return (iphi + ieta * _n_tiles_phi);
185 inline void LazyTiling9SeparateGhosts::_tj_set_jetinfo( TiledJet3 *
const jet,
186 const int _jets_index,
bool is_ghost) {
188 _bj_set_jetinfo<>(jet, _jets_index);
191 jet->is_ghost = is_ghost;
194 jet->tile_index = _tile_index(jet->eta, jet->phi);
197 Tile3 * tile = &_tiles[jet->tile_index];
198 jet->previous = NULL;
200 jet->next = tile->ghost_head;
201 tile->ghost_head = jet;
203 jet->next = tile->head;
206 if (jet->next != NULL) {jet->next->previous = jet;}
211 void LazyTiling9SeparateGhosts::_bj_remove_from_tiles(TiledJet3 *
const jet) {
212 Tile3 * tile = & _tiles[jet->tile_index];
214 if (jet->previous == NULL) {
218 tile->ghost_head = jet->next;
220 tile->head = jet->next;
224 jet->previous->next = jet->next;
226 if (jet->next != NULL) {
228 jet->next->previous = jet->previous;
235 void LazyTiling9SeparateGhosts::_print_tiles(TiledJet3 * briefjets )
const {
236 for (vector<Tile3>::const_iterator tile = _tiles.begin();
237 tile < _tiles.end(); tile++) {
238 cout <<
"Tile " << tile - _tiles.begin()<<
" = ";
240 for (TiledJet3 * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
241 list.push_back(jetI-briefjets);
244 sort(list.begin(),list.end());
245 for (
unsigned int i = 0; i < list.size(); i++) {cout <<
" "<<list[i];}
258 void LazyTiling9SeparateGhosts::_add_neighbours_to_tile_union(
const int tile_index,
259 vector<int> & tile_union,
int & n_near_tiles)
const {
260 for (Tile3 *
const * near_tile = _tiles[tile_index].begin_tiles;
261 near_tile != _tiles[tile_index].end_tiles; near_tile++){
263 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
273 inline void LazyTiling9SeparateGhosts::_add_untagged_neighbours_to_tile_union(
274 const int tile_index,
275 vector<int> & tile_union,
int & n_near_tiles) {
276 for (Tile3 ** near_tile = _tiles[tile_index].begin_tiles;
277 near_tile != _tiles[tile_index].end_tiles; near_tile++){
278 if (! (*near_tile)->tagged) {
279 (*near_tile)->tagged =
true;
281 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
293 inline void LazyTiling9SeparateGhosts::_add_untagged_neighbours_to_tile_union_using_max_info(
294 const TiledJet3 * jet,
295 vector<int> & tile_union,
int & n_near_tiles) {
296 Tile3 & tile = _tiles[jet->tile_index];
298 for (Tile3 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
299 if ((*near_tile)->tagged)
continue;
306 if (dist > (*near_tile)->max_NN_dist)
continue;
309 (*near_tile)->tagged =
true;
311 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
329 inline double LazyTiling9SeparateGhosts::_distance_to_tile(
const TiledJet3 * bj,
const Tile3 * tile)
const {
337 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
339 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
348 double dphi = std::abs(bj->phi - tile->phi_centre);
349 if (dphi > pi) dphi = twopi-dphi;
350 dphi -= _tile_half_size_phi;
352 if (dphi < 0) dphi = 0;
354 return dphi*dphi + deta*deta;
366 inline void LazyTiling9SeparateGhosts::_update_jetX_jetI_NN(TiledJet3 * jetX, TiledJet3 * jetI, vector<TiledJet3 *> & jets_for_minheap) {
367 assert(! (jetX->is_ghost || jetI->is_ghost));
368 double dist = _bj_dist(jetI,jetX);
369 if (dist < jetI->NN_dist) {
371 jetI->NN_dist = dist;
374 if (!jetI->minheap_update_needed()) {
375 jetI->label_minheap_update_needed();
376 jets_for_minheap.push_back(jetI);
380 if (dist < jetX->NN_dist) {
382 jetX->NN_dist = dist;
388 inline void LazyTiling9SeparateGhosts::_set_NN(TiledJet3 * jetI,
389 vector<TiledJet3 *> & jets_for_minheap) {
390 assert(! jetI->is_ghost);
394 if (!jetI->minheap_update_needed()) {
395 jetI->label_minheap_update_needed();
396 jets_for_minheap.push_back(jetI);}
398 Tile3 * tile_ptr = &_tiles[jetI->tile_index];
400 for (Tile3 ** near_tile = tile_ptr->begin_tiles;
401 near_tile != tile_ptr->end_tiles; near_tile++) {
404 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile))
continue;
406 for (TiledJet3 * jetJ = (*near_tile)->head; jetJ != NULL; jetJ = jetJ->next) {
407 double dist = _bj_dist(jetI,jetJ);
408 if (dist < jetI->NN_dist && jetJ != jetI) {
409 jetI->NN_dist = dist; jetI->NN = jetJ;
413 for (TiledJet3 * jetJ = (*near_tile)->ghost_head; jetJ != NULL; jetJ = jetJ->next) {
414 double dist = _bj_dist(jetI,jetJ);
415 if (dist < jetI->NN_dist) {
416 jetI->NN_dist = dist; jetI->NN = jetJ;
441 void LazyTiling9SeparateGhosts::run() {
445 int ntot = _jets.size();
446 if (ntot == 0)
return;
448 TiledJet3 * briefjets =
new TiledJet3[ntot];
449 TiledJet3 * jetA = briefjets, * jetB;
452 TiledJet3 oldB = briefjets[0];
457 vector<int> tile_union(3*n_tile_neighbours);
459 TiledJet3 * head = briefjets;
465 for (
int i = 0; i< ntot; i++) {
466 bool is_ghost = _jets[i].perp2() < ghost_pt2_threshold;
468 _tj_set_jetinfo(jetA, i, is_ghost);
472 int nreal = jetA - briefjets;
474 for (
int i = 0; i< ntot; i++) {
475 bool is_ghost = _jets[i].perp2() < ghost_pt2_threshold;
477 _tj_set_jetinfo(jetA, i, is_ghost);
484 vector<Tile3>::iterator tile;
485 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
487 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
489 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
490 double dist = _bj_dist_not_periodic(jetA,jetB);
491 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
492 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
495 for (jetB = tile->ghost_head; jetB != NULL; jetB = jetB->next) {
496 double dist = _bj_dist_not_periodic(jetA,jetB);
497 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
502 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
503 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
506 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
509 for (Tile3 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
510 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
511 double dist_to_tile = _distance_to_tile(jetA, *RTile);
523 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
524 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
525 if (relevant_for_jetA || relevant_for_RTile) {
526 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
527 double dist = _bj_dist(jetA,jetB);
528 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
529 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
533 if (relevant_for_jetA) {
534 for (jetB = (*RTile)->ghost_head; jetB != NULL; jetB = jetB->next) {
535 double dist = _bj_dist(jetA,jetB);
536 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
542 for (Tile3 ** LTile = tile->surrounding_tiles; LTile != tile->RH_tiles; LTile++) {
543 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
544 double dist_to_tile = _distance_to_tile(jetA, *LTile);
547 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
548 if (relevant_for_jetA) {
549 for (jetB = (*LTile)->ghost_head; jetB != NULL; jetB = jetB->next) {
550 double dist = _bj_dist(jetA,jetB);
551 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
582 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
583 tile->max_NN_dist = 0;
584 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
585 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
590 vector<double> diJs(nreal);
591 for (
int i = 0; i < nreal; i++) {
592 diJs[i] = _bj_diJ(&briefjets[i]);
593 briefjets[i].label_minheap_update_done();
595 MinHeap minheap(diJs);
597 vector<TiledJet3 *> jets_for_minheap;
598 jets_for_minheap.reserve(ntot);
603 double diJ_min = minheap.minval() *_invR2;
604 jetA = head + minheap.minloc();
615 if (jetA < jetB) {std::swap(jetA,jetB);}
618 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
621 _bj_remove_from_tiles(jetA);
623 _bj_remove_from_tiles(jetB);
624 bool is_ghost =
false;
625 _tj_set_jetinfo(jetB, nn, is_ghost);
630 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
631 _bj_remove_from_tiles(jetA);
636 if (!jetA->is_ghost) {
637 minheap.remove(jetA-head);
650 int n_near_tiles = 0;
653 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
654 tile_union, n_near_tiles);
656 _add_untagged_neighbours_to_tile_union_using_max_info(&oldB,
657 tile_union,n_near_tiles);
658 jetB->label_minheap_update_needed();
659 jets_for_minheap.push_back(jetB);
667 Tile3 & jetB_tile = _tiles[jetB->tile_index];
668 for (Tile3 ** near_tile = jetB_tile.begin_tiles;
669 near_tile != jetB_tile.end_tiles; near_tile++) {
671 double dist_to_tile = _distance_to_tile(jetB, *near_tile);
674 bool relevant_for_jetB = dist_to_tile <= jetB->NN_dist;
675 bool relevant_for_near_tile = dist_to_tile <= (*near_tile)->max_NN_dist;
676 bool relevant = relevant_for_jetB || relevant_for_near_tile;
681 if ((*near_tile)->tagged) {
682 for (TiledJet3 * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
683 if (jetI->NN == jetA || jetI->NN == jetB) _set_NN(jetI, jets_for_minheap);
684 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
686 (*near_tile)->tagged =
false;
688 for (TiledJet3 * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
689 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
694 if (relevant_for_jetB) {
695 for (TiledJet3 * jetI = (*near_tile)->ghost_head; jetI != NULL; jetI = jetI->next) {
696 double dist = _bj_dist(jetB,jetI);
697 if (dist < jetB->NN_dist) {
698 jetB->NN_dist = dist; jetB->NN = jetI;
741 for (
int itile = 0; itile < n_near_tiles; itile++) {
742 Tile3 * tile_ptr = &_tiles[tile_union[itile]];
743 if (!tile_ptr->tagged)
continue;
744 tile_ptr->tagged =
false;
746 for (TiledJet3 * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
748 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
749 _set_NN(jetI, jets_for_minheap);
756 while (jets_for_minheap.size() > 0) {
757 TiledJet3 * jetI = jets_for_minheap.back();
758 jets_for_minheap.pop_back();
759 minheap.update(jetI-head, _bj_diJ(jetI));
760 jetI->label_minheap_update_done();
763 Tile3 & tile_I = _tiles[jetI->tile_index];
764 if (tile_I.max_NN_dist < jetI->NN_dist) tile_I.max_NN_dist = jetI->NN_dist;
773 FASTJET_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...