32 #include "fastjet/internal/LazyTiling25.hh"
33 #include "fastjet/internal/TilingExtent.hh"
37 FASTJET_BEGIN_NAMESPACE
39 LazyTiling25::LazyTiling25(ClusterSequence & cs) :
40 _cs(cs), _jets(cs.jets())
47 _Rparam = cs.jet_def().R();
48 _R2 = _Rparam * _Rparam;
75 void LazyTiling25::_initialise_tiles() {
79 double default_size = max(0.1,_Rparam)/2;
80 _tile_size_eta = default_size;
84 _n_tiles_phi = max(5,
int(floor(twopi/default_size)));
85 _tile_size_phi = twopi / _n_tiles_phi;
87 #define _FASTJET_TILING25_USE_TILING_ANALYSIS_
88 #ifdef _FASTJET_TILING25_USE_TILING_ANALYSIS_
90 TilingExtent tiling_analysis(_cs);
91 _tiles_eta_min = tiling_analysis.minrap();
92 _tiles_eta_max = tiling_analysis.maxrap();
94 #else // not _FASTJET_TILING25_USE_TILING_ANALYSIS_
99 const double maxrap = 7.0;
102 for(
unsigned int i = 0; i < _jets.size(); i++) {
103 double eta = _jets[i].rap();
106 if (abs(eta) < maxrap) {
107 if (eta < _tiles_eta_min) {_tiles_eta_min = eta;}
108 if (eta > _tiles_eta_max) {_tiles_eta_max = eta;}
112 #endif // _FASTJET_TILING25_USE_TILING_ANALYSIS_
124 #define FASTJET_LAZY25_MIN3TILESY
125 #ifdef FASTJET_LAZY25_MIN3TILESY
126 if (_tiles_eta_max - _tiles_eta_min < 3*_tile_size_eta) {
131 _tile_size_eta = (_tiles_eta_max - _tiles_eta_min)/3;
136 _tiles_eta_max -= _tile_size_eta;
138 #endif //FASTJET_LAZY25_MIN3TILESY
139 _tiles_ieta_min = int(floor(_tiles_eta_min/_tile_size_eta));
140 _tiles_ieta_max = int(floor( _tiles_eta_max/_tile_size_eta));
141 _tiles_eta_min = _tiles_ieta_min * _tile_size_eta;
142 _tiles_eta_max = _tiles_ieta_max * _tile_size_eta;
143 #ifdef FASTJET_LAZY25_MIN3TILESY
146 _tile_half_size_eta = _tile_size_eta * 0.5;
147 _tile_half_size_phi = _tile_size_phi * 0.5;
151 vector<bool> use_periodic_delta_phi(_n_tiles_phi,
false);
152 if (_n_tiles_phi <= 5) {
153 fill(use_periodic_delta_phi.begin(), use_periodic_delta_phi.end(),
true);
155 use_periodic_delta_phi[0] =
true;
156 use_periodic_delta_phi[1] =
true;
157 use_periodic_delta_phi[_n_tiles_phi-2] =
true;
158 use_periodic_delta_phi[_n_tiles_phi-1] =
true;
162 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
165 for (
int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
166 for (
int iphi = 0; iphi < _n_tiles_phi; iphi++) {
167 Tile25 * tile = & _tiles[_tile_index(ieta,iphi)];
170 tile->begin_tiles[0] = tile;
171 Tile25 ** pptile = & (tile->begin_tiles[0]);
175 tile->surrounding_tiles = pptile;
176 if (ieta > _tiles_ieta_min) {
180 for (
int idphi = -2; idphi <=+2; idphi++) {
181 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
186 if (ieta > _tiles_ieta_min + 1) {
190 for (
int idphi = -2; idphi <= +2; idphi++) {
191 *pptile = & _tiles[_tile_index(ieta-2,iphi+idphi)];
196 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
198 *pptile = & _tiles[_tile_index(ieta,iphi-2)];
201 tile->RH_tiles = pptile;
202 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
204 *pptile = & _tiles[_tile_index(ieta,iphi+2)];
207 if (ieta < _tiles_ieta_max) {
208 for (
int idphi = -2; idphi <= +2; idphi++) {
209 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
214 if (ieta < _tiles_ieta_max - 1) {
215 for (
int idphi = -2; idphi <= +2; idphi++) {
216 *pptile = & _tiles[_tile_index(ieta+2,iphi+idphi)];
221 tile->end_tiles = pptile;
223 tile->tagged =
false;
225 tile->use_periodic_delta_phi = use_periodic_delta_phi[iphi];
227 tile->max_NN_dist = 0;
229 tile->eta_centre = (ieta-_tiles_ieta_min+0.5)*_tile_size_eta + _tiles_eta_min;
230 tile->phi_centre = (iphi+0.5)*_tile_size_phi;
238 int LazyTiling25::_tile_index(
const double eta,
const double phi)
const {
240 if (eta <= _tiles_eta_min) {ieta = 0;}
241 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
244 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
246 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
247 ieta = _tiles_ieta_max-_tiles_ieta_min;}
253 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
254 return (iphi + ieta * _n_tiles_phi);
260 inline void LazyTiling25::_tj_set_jetinfo( TiledJet *
const jet,
261 const int _jets_index) {
263 _bj_set_jetinfo<>(jet, _jets_index);
268 jet->tile_index = _tile_index(jet->eta, jet->phi);
271 Tile25 * tile = &_tiles[jet->tile_index];
272 jet->previous = NULL;
273 jet->next = tile->head;
274 if (jet->next != NULL) {jet->next->previous = jet;}
280 void LazyTiling25::_bj_remove_from_tiles(TiledJet *
const jet) {
281 Tile25 * tile = & _tiles[jet->tile_index];
283 if (jet->previous == NULL) {
286 tile->head = jet->next;
289 jet->previous->next = jet->next;
291 if (jet->next != NULL) {
293 jet->next->previous = jet->previous;
300 void LazyTiling25::_print_tiles(TiledJet * briefjets )
const {
301 for (vector<Tile25>::const_iterator tile = _tiles.begin();
302 tile < _tiles.end(); tile++) {
303 cout <<
"Tile " << tile - _tiles.begin()
304 <<
" at " << setw(10) << tile->eta_centre <<
"," << setw(10) << tile->phi_centre
307 for (TiledJet * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
308 list.push_back(jetI-briefjets);
311 sort(list.begin(),list.end());
312 for (
unsigned int i = 0; i < list.size(); i++) {cout <<
" "<<list[i];}
325 void LazyTiling25::_add_neighbours_to_tile_union(
const int tile_index,
326 vector<int> & tile_union,
int & n_near_tiles)
const {
327 for (Tile25 *
const * near_tile = _tiles[tile_index].begin_tiles;
328 near_tile != _tiles[tile_index].end_tiles; near_tile++){
330 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
340 inline void LazyTiling25::_add_untagged_neighbours_to_tile_union(
341 const int tile_index,
342 vector<int> & tile_union,
int & n_near_tiles) {
343 for (Tile25 ** near_tile = _tiles[tile_index].begin_tiles;
344 near_tile != _tiles[tile_index].end_tiles; near_tile++){
345 if (! (*near_tile)->tagged) {
346 (*near_tile)->tagged =
true;
348 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
360 inline void LazyTiling25::_add_untagged_neighbours_to_tile_union_using_max_info(
361 const TiledJet * jet,
362 vector<int> & tile_union,
int & n_near_tiles) {
363 Tile25 & tile = _tiles[jet->tile_index];
365 for (Tile25 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
366 if ((*near_tile)->tagged)
continue;
373 if (dist > (*near_tile)->max_NN_dist)
continue;
376 (*near_tile)->tagged =
true;
378 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
396 inline double LazyTiling25::_distance_to_tile(
const TiledJet * bj,
const Tile25 * tile)
402 #endif // INSTRUMENT2
409 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
411 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
420 double dphi = std::abs(bj->phi - tile->phi_centre);
421 if (dphi > pi) dphi = twopi-dphi;
422 dphi -= _tile_half_size_phi;
424 if (dphi < 0) dphi = 0;
426 return dphi*dphi + deta*deta;
440 inline void LazyTiling25::_update_jetX_jetI_NN(TiledJet * jetX, TiledJet * jetI, vector<TiledJet *> & jets_for_minheap) {
441 double dist = _bj_dist(jetI,jetX);
442 if (dist < jetI->NN_dist) {
444 jetI->NN_dist = dist;
447 if (!jetI->minheap_update_needed()) {
448 jetI->label_minheap_update_needed();
449 jets_for_minheap.push_back(jetI);
453 if (dist < jetX->NN_dist) {
455 jetX->NN_dist = dist;
461 inline void LazyTiling25::_set_NN(TiledJet * jetI,
462 vector<TiledJet *> & jets_for_minheap) {
466 if (!jetI->minheap_update_needed()) {
467 jetI->label_minheap_update_needed();
468 jets_for_minheap.push_back(jetI);}
470 Tile25 * tile_ptr = &_tiles[jetI->tile_index];
472 for (Tile25 ** near_tile = tile_ptr->begin_tiles;
473 near_tile != tile_ptr->end_tiles; near_tile++) {
476 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile))
continue;
478 for (TiledJet * jetJ = (*near_tile)->head;
479 jetJ != NULL; jetJ = jetJ->next) {
480 double dist = _bj_dist(jetI,jetJ);
481 if (dist < jetI->NN_dist && jetJ != jetI) {
482 jetI->NN_dist = dist; jetI->NN = jetJ;
507 void LazyTiling25::run() {
511 int n = _jets.size();
514 TiledJet * briefjets =
new TiledJet[n];
515 TiledJet * jetA = briefjets, * jetB;
518 TiledJet oldB = briefjets[0];
522 vector<int> tile_union(3*25);
525 for (
int i = 0; i< n; i++) {
526 _tj_set_jetinfo(jetA, i);
530 TiledJet * head = briefjets;
533 vector<Tile25>::iterator tile;
534 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
536 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
537 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
538 double dist = _bj_dist_not_periodic(jetA,jetB);
539 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
540 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
543 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
544 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
547 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
548 if (tile->use_periodic_delta_phi) {
550 for (Tile25 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
551 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
552 double dist_to_tile = _distance_to_tile(jetA, *RTile);
564 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
565 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
566 if (relevant_for_jetA || relevant_for_RTile) {
567 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
568 double dist = _bj_dist(jetA,jetB);
569 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
570 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
579 for (Tile25 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
580 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
581 double dist_to_tile = _distance_to_tile(jetA, *RTile);
582 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
583 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
584 if (relevant_for_jetA || relevant_for_RTile) {
585 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
586 double dist = _bj_dist_not_periodic(jetA,jetB);
587 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
588 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
600 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
601 tile->max_NN_dist = 0;
602 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
603 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
608 cout <<
"intermediate ncall, dtt = " << _ncall <<
" " << _ncall_dtt << endl;
609 #endif // INSTRUMENT2
617 vector<double> diJs(n);
618 for (
int i = 0; i < n; i++) {
619 diJs[i] = _bj_diJ(&briefjets[i]);
620 briefjets[i].label_minheap_update_done();
622 MinHeap minheap(diJs);
624 vector<TiledJet *> jets_for_minheap;
625 jets_for_minheap.reserve(n);
628 int history_location = n-1;
631 double diJ_min = minheap.minval() *_invR2;
632 jetA = head + minheap.minloc();
644 if (jetA < jetB) {std::swap(jetA,jetB);}
647 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
650 _bj_remove_from_tiles(jetA);
652 _bj_remove_from_tiles(jetB);
653 _tj_set_jetinfo(jetB, nn);
658 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
659 _bj_remove_from_tiles(jetA);
663 minheap.remove(jetA-head);
665 int n_near_tiles = 0;
671 Tile25 & jetB_tile = _tiles[jetB->tile_index];
672 for (Tile25 ** 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;
681 if (! relevant)
continue;
684 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
685 (*near_tile)->tagged =
true;
692 for (TiledJet * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
693 if (jetI->NN == jetA || jetI->NN == jetB) _set_NN(jetI, jets_for_minheap);
694 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
703 int n_done_tiles = n_near_tiles;
704 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
705 tile_union, n_near_tiles);
707 _add_untagged_neighbours_to_tile_union_using_max_info(&oldB,
708 tile_union,n_near_tiles);
709 jetB->label_minheap_update_needed();
710 jets_for_minheap.push_back(jetB);
715 for (
int itile = 0; itile < n_done_tiles; itile++) {
716 _tiles[tile_union[itile]].tagged =
false;
720 for (
int itile = n_done_tiles; itile < n_near_tiles; itile++) {
721 Tile25 * tile_ptr = &_tiles[tile_union[itile]];
722 tile_ptr->tagged =
false;
724 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
726 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
727 _set_NN(jetI, jets_for_minheap);
734 while (jets_for_minheap.size() > 0) {
735 TiledJet * jetI = jets_for_minheap.back();
736 jets_for_minheap.pop_back();
737 minheap.update(jetI-head, _bj_diJ(jetI));
738 jetI->label_minheap_update_done();
741 Tile25 & tile_I = _tiles[jetI->tile_index];
742 if (tile_I.max_NN_dist < jetI->NN_dist) tile_I.max_NN_dist = jetI->NN_dist;
750 cout <<
"ncall, dtt = " << _ncall <<
" " << _ncall_dtt << endl;
751 #endif // INSTRUMENT2
755 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...