35 #include "fastjet/internal/LazyTiling9.hh"
36 #include "fastjet/internal/TilingExtent.hh"
40 #define _FASTJET_TILING2_USE_TILING_ANALYSIS_
42 FASTJET_BEGIN_NAMESPACE
46 LazyTiling9::LazyTiling9(ClusterSequence & cs) :
47 _cs(cs), _jets(cs.jets())
54 _Rparam = cs.jet_def().R();
55 _R2 = _Rparam * _Rparam;
80 void LazyTiling9::_initialise_tiles() {
84 double default_size = max(0.1,_Rparam);
85 _tile_size_eta = default_size;
89 _n_tiles_phi = max(3,
int(floor(twopi/default_size)));
90 _tile_size_phi = twopi / _n_tiles_phi;
92 #ifdef _FASTJET_TILING2_USE_TILING_ANALYSIS_
94 TilingExtent tiling_analysis(_cs);
95 _tiles_eta_min = tiling_analysis.minrap();
96 _tiles_eta_max = tiling_analysis.maxrap();
100 _tiles_eta_min = 0.0;
101 _tiles_eta_max = 0.0;
103 const double maxrap = 7.0;
106 for(
unsigned int i = 0; i < _jets.size(); i++) {
107 double eta = _jets[i].rap();
110 if (abs(eta) < maxrap) {
111 if (eta < _tiles_eta_min) {_tiles_eta_min = eta;}
112 if (eta > _tiles_eta_max) {_tiles_eta_max = eta;}
131 #define FASTJET_LAZY9_MIN2TILESY
132 #ifdef FASTJET_LAZY9_MIN2TILESY
133 if (_tiles_eta_max - _tiles_eta_min < 2*_tile_size_eta) {
138 _tile_size_eta = (_tiles_eta_max - _tiles_eta_min)/2;
143 _tiles_eta_max -= _tile_size_eta;
145 #endif //FASTJET_LAZY9_MIN2TILESY
146 _tiles_ieta_min = int(floor(_tiles_eta_min/_tile_size_eta));
147 _tiles_ieta_max = int(floor( _tiles_eta_max/_tile_size_eta));
148 _tiles_eta_min = _tiles_ieta_min * _tile_size_eta;
149 _tiles_eta_max = _tiles_ieta_max * _tile_size_eta;
150 #ifdef FASTJET_LAZY9_MIN2TILESY
154 _tile_half_size_eta = _tile_size_eta * 0.5;
155 _tile_half_size_phi = _tile_size_phi * 0.5;
159 vector<bool> use_periodic_delta_phi(_n_tiles_phi,
false);
160 if (_n_tiles_phi <= 3) {
161 fill(use_periodic_delta_phi.begin(), use_periodic_delta_phi.end(),
true);
163 use_periodic_delta_phi[0] =
true;
164 use_periodic_delta_phi[_n_tiles_phi-1] =
true;
168 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
171 for (
int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
172 for (
int iphi = 0; iphi < _n_tiles_phi; iphi++) {
173 Tile2 * tile = & _tiles[_tile_index(ieta,iphi)];
176 tile->begin_tiles[0] = tile;
177 Tile2 ** pptile = & (tile->begin_tiles[0]);
181 tile->surrounding_tiles = pptile;
182 if (ieta > _tiles_ieta_min) {
186 for (
int idphi = -1; idphi <=+1; idphi++) {
187 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
192 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
195 tile->RH_tiles = pptile;
196 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
199 if (ieta < _tiles_ieta_max) {
200 for (
int idphi = -1; idphi <= +1; idphi++) {
201 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
206 tile->end_tiles = pptile;
208 tile->tagged =
false;
210 tile->use_periodic_delta_phi = use_periodic_delta_phi[iphi];
212 tile->max_NN_dist = 0;
214 tile->eta_centre = (ieta-_tiles_ieta_min+0.5)*_tile_size_eta + _tiles_eta_min;
215 tile->phi_centre = (iphi+0.5)*_tile_size_phi;
223 int LazyTiling9::_tile_index(
const double eta,
const double phi)
const {
225 if (eta <= _tiles_eta_min) {ieta = 0;}
226 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
229 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
231 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
232 ieta = _tiles_ieta_max-_tiles_ieta_min;}
238 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
239 return (iphi + ieta * _n_tiles_phi);
245 inline void LazyTiling9::_tj_set_jetinfo( TiledJet *
const jet,
246 const int _jets_index) {
248 _bj_set_jetinfo<>(jet, _jets_index);
253 jet->tile_index = _tile_index(jet->eta, jet->phi);
256 Tile2 * tile = &_tiles[jet->tile_index];
257 jet->previous = NULL;
258 jet->next = tile->head;
259 if (jet->next != NULL) {jet->next->previous = jet;}
265 void LazyTiling9::_bj_remove_from_tiles(TiledJet *
const jet) {
266 Tile2 * tile = & _tiles[jet->tile_index];
268 if (jet->previous == NULL) {
271 tile->head = jet->next;
274 jet->previous->next = jet->next;
276 if (jet->next != NULL) {
278 jet->next->previous = jet->previous;
285 void LazyTiling9::_print_tiles(TiledJet * briefjets )
const {
286 for (vector<Tile2>::const_iterator tile = _tiles.begin();
287 tile < _tiles.end(); tile++) {
288 cout <<
"Tile " << tile - _tiles.begin()<<
" = ";
290 for (TiledJet * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
291 list.push_back(jetI-briefjets);
294 sort(list.begin(),list.end());
295 for (
unsigned int i = 0; i < list.size(); i++) {cout <<
" "<<list[i];}
308 void LazyTiling9::_add_neighbours_to_tile_union(
const int tile_index,
309 vector<int> & tile_union,
int & n_near_tiles)
const {
310 for (Tile2 *
const * near_tile = _tiles[tile_index].begin_tiles;
311 near_tile != _tiles[tile_index].end_tiles; near_tile++){
313 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
323 inline void LazyTiling9::_add_untagged_neighbours_to_tile_union(
324 const int tile_index,
325 vector<int> & tile_union,
int & n_near_tiles) {
326 for (Tile2 ** near_tile = _tiles[tile_index].begin_tiles;
327 near_tile != _tiles[tile_index].end_tiles; near_tile++){
328 if (! (*near_tile)->tagged) {
329 (*near_tile)->tagged =
true;
331 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
343 inline void LazyTiling9::_add_untagged_neighbours_to_tile_union_using_max_info(
344 const TiledJet * jet,
345 vector<int> & tile_union,
int & n_near_tiles) {
346 Tile2 & tile = _tiles[jet->tile_index];
348 for (Tile2 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
349 if ((*near_tile)->tagged)
continue;
356 if (dist > (*near_tile)->max_NN_dist)
continue;
359 (*near_tile)->tagged =
true;
361 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
379 inline double LazyTiling9::_distance_to_tile(
const TiledJet * bj,
const Tile2 * tile)
385 #endif // INSTRUMENT2
392 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
394 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
403 double dphi = std::abs(bj->phi - tile->phi_centre);
404 if (dphi > pi) dphi = twopi-dphi;
405 dphi -= _tile_half_size_phi;
407 if (dphi < 0) dphi = 0;
409 return dphi*dphi + deta*deta;
423 inline void LazyTiling9::_update_jetX_jetI_NN(TiledJet * jetX, TiledJet * jetI, vector<TiledJet *> & jets_for_minheap) {
424 double dist = _bj_dist(jetI,jetX);
425 if (dist < jetI->NN_dist) {
427 jetI->NN_dist = dist;
430 if (!jetI->minheap_update_needed()) {
431 jetI->label_minheap_update_needed();
432 jets_for_minheap.push_back(jetI);
436 if (dist < jetX->NN_dist) {
438 jetX->NN_dist = dist;
444 inline void LazyTiling9::_set_NN(TiledJet * jetI,
445 vector<TiledJet *> & jets_for_minheap) {
449 if (!jetI->minheap_update_needed()) {
450 jetI->label_minheap_update_needed();
451 jets_for_minheap.push_back(jetI);}
453 Tile2 * tile_ptr = &_tiles[jetI->tile_index];
455 for (Tile2 ** near_tile = tile_ptr->begin_tiles;
456 near_tile != tile_ptr->end_tiles; near_tile++) {
459 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile))
continue;
461 for (TiledJet * jetJ = (*near_tile)->head;
462 jetJ != NULL; jetJ = jetJ->next) {
463 double dist = _bj_dist(jetI,jetJ);
464 if (dist < jetI->NN_dist && jetJ != jetI) {
465 jetI->NN_dist = dist; jetI->NN = jetJ;
490 void LazyTiling9::run() {
494 int n = _jets.size();
497 TiledJet * briefjets =
new TiledJet[n];
498 TiledJet * jetA = briefjets, * jetB;
501 TiledJet oldB = briefjets[0];
505 vector<int> tile_union(3*n_tile_neighbours);
508 for (
int i = 0; i< n; i++) {
509 _tj_set_jetinfo(jetA, i);
513 TiledJet * head = briefjets;
535 vector<Tile2>::iterator tile;
536 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
538 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
539 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
540 double dist = _bj_dist_not_periodic(jetA,jetB);
541 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
542 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
545 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
546 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
549 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
550 if (tile->use_periodic_delta_phi) {
552 for (Tile2 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
553 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
554 double dist_to_tile = _distance_to_tile(jetA, *RTile);
566 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
567 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
568 if (relevant_for_jetA || relevant_for_RTile) {
569 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
570 double dist = _bj_dist(jetA,jetB);
571 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
572 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
581 for (Tile2 ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
582 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
583 double dist_to_tile = _distance_to_tile(jetA, *RTile);
584 bool relevant_for_jetA = dist_to_tile <= jetA->NN_dist;
585 bool relevant_for_RTile = dist_to_tile <= (*RTile)->max_NN_dist;
586 if (relevant_for_jetA || relevant_for_RTile) {
587 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
588 double dist = _bj_dist_not_periodic(jetA,jetB);
589 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
590 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
602 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
603 tile->max_NN_dist = 0;
604 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
605 if (jetA->NN_dist > tile->max_NN_dist) tile->max_NN_dist = jetA->NN_dist;
610 cout <<
"intermediate ncall, dtt = " << _ncall <<
" " << _ncall_dtt << endl;
611 #endif // INSTRUMENT2
613 vector<double> diJs(n);
614 for (
int i = 0; i < n; i++) {
615 diJs[i] = _bj_diJ(&briefjets[i]);
616 briefjets[i].label_minheap_update_done();
618 MinHeap minheap(diJs);
620 vector<TiledJet *> jets_for_minheap;
621 jets_for_minheap.reserve(n);
624 int history_location = n-1;
627 double diJ_min = minheap.minval() *_invR2;
628 jetA = head + minheap.minloc();
640 if (jetA < jetB) {std::swap(jetA,jetB);}
643 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
646 _bj_remove_from_tiles(jetA);
648 _bj_remove_from_tiles(jetB);
649 _tj_set_jetinfo(jetB, nn);
654 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
655 _bj_remove_from_tiles(jetA);
659 minheap.remove(jetA-head);
661 int n_near_tiles = 0;
667 Tile2 & jetB_tile = _tiles[jetB->tile_index];
668 for (Tile2 ** 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;
677 if (! relevant)
continue;
680 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
681 (*near_tile)->tagged =
true;
688 for (TiledJet * jetI = (*near_tile)->head; jetI != NULL; jetI = jetI->next) {
689 if (jetI->NN == jetA || jetI->NN == jetB) _set_NN(jetI, jets_for_minheap);
690 _update_jetX_jetI_NN(jetB, jetI, jets_for_minheap);
699 int n_done_tiles = n_near_tiles;
700 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
701 tile_union, n_near_tiles);
703 _add_untagged_neighbours_to_tile_union_using_max_info(&oldB,
704 tile_union,n_near_tiles);
705 jetB->label_minheap_update_needed();
706 jets_for_minheap.push_back(jetB);
711 for (
int itile = 0; itile < n_done_tiles; itile++) {
712 _tiles[tile_union[itile]].tagged =
false;
716 for (
int itile = n_done_tiles; itile < n_near_tiles; itile++) {
717 Tile2 * tile_ptr = &_tiles[tile_union[itile]];
718 tile_ptr->tagged =
false;
720 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
722 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
723 _set_NN(jetI, jets_for_minheap);
730 while (jets_for_minheap.size() > 0) {
731 TiledJet * jetI = jets_for_minheap.back();
732 jets_for_minheap.pop_back();
733 minheap.update(jetI-head, _bj_diJ(jetI));
734 jetI->label_minheap_update_done();
737 Tile2 & tile_I = _tiles[jetI->tile_index];
738 if (tile_I.max_NN_dist < jetI->NN_dist) tile_I.max_NN_dist = jetI->NN_dist;
746 cout <<
"ncall, dtt = " << _ncall <<
" " << _ncall_dtt << endl;
747 #endif // INSTRUMENT2
751 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...