FastJet 3.4.1
ClusterSequence_TiledN2.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
32// The tiled N^2 part of the ClusterSequence class -- separated out
33// from the rest of the class implementation so as to speed up
34// compilation of this particular part while it is under test.
35
36#include<iostream>
37#include<vector>
38#include<cmath>
39#include<algorithm>
40#include "fastjet/PseudoJet.hh"
41#include "fastjet/ClusterSequence.hh"
42#include "fastjet/internal/MinHeap.hh"
43#include "fastjet/internal/TilingExtent.hh"
44
45FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
46
47using namespace std;
48
49
50//----------------------------------------------------------------------
51void ClusterSequence::_bj_remove_from_tiles(TiledJet * const jet) {
52 Tile * tile = & _tiles[jet->tile_index];
53
54 if (jet->previous == NULL) {
55 // we are at head of the tile, so reset it.
56 // If this was the only jet on the tile then tile->head will now be NULL
57 tile->head = jet->next;
58 } else {
59 // adjust link from previous jet in this tile
60 jet->previous->next = jet->next;
61 }
62 if (jet->next != NULL) {
63 // adjust backwards-link from next jet in this tile
64 jet->next->previous = jet->previous;
65 }
66}
67
68//----------------------------------------------------------------------
69/// Set up the tiles:
70/// - decide the range in eta
71/// - allocate the tiles
72/// - set up the cross-referencing info between tiles
73///
74/// The neighbourhood of a tile is set up as follows
75///
76/// LRR
77/// LXR
78/// LLR
79///
80/// such that tiles is an array containing XLLLLRRRR with pointers
81/// | \ RH_tiles
82/// \ surrounding_tiles
83///
84/// with appropriate precautions when close to the edge of the tiled
85/// region.
86///
87void ClusterSequence::_initialise_tiles() {
88
89 // first decide tile sizes (with a lower bound to avoid huge memory use with
90 // very small R)
91 double default_size = max(0.1,_Rparam);
92 _tile_size_eta = default_size;
93 // it makes no sense to go below 3 tiles in phi -- 3 tiles is
94 // sufficient to make sure all pair-wise combinations up to pi in
95 // phi are possible
96 _n_tiles_phi = max(3,int(floor(twopi/default_size)));
97 _tile_size_phi = twopi / _n_tiles_phi; // >= _Rparam and fits in 2pi
98
99 TilingExtent tiling_analysis(*this);
100 _tiles_eta_min = tiling_analysis.minrap();
101 _tiles_eta_max = tiling_analysis.maxrap();
102
103 // // always include zero rapidity in the tiling region
104 // _tiles_eta_min = 0.0;
105 // _tiles_eta_max = 0.0;
106 // // but go no further than following
107 // const double maxrap = 7.0;
108 //
109 // // and find out how much further one should go
110 // for(unsigned int i = 0; i < _jets.size(); i++) {
111 // double eta = _jets[i].rap();
112 // // first check if eta is in range -- to avoid taking into account
113 // // very spurious rapidities due to particles with near-zero kt.
114 // if (abs(eta) < maxrap) {
115 // if (eta < _tiles_eta_min) {_tiles_eta_min = eta;}
116 // if (eta > _tiles_eta_max) {_tiles_eta_max = eta;}
117 // }
118 // }
119
120 // now adjust the values
121 _tiles_ieta_min = int(floor(_tiles_eta_min/_tile_size_eta));
122 _tiles_ieta_max = int(floor( _tiles_eta_max/_tile_size_eta));
123 _tiles_eta_min = _tiles_ieta_min * _tile_size_eta;
124 _tiles_eta_max = _tiles_ieta_max * _tile_size_eta;
125
126 // allocate the tiles
127 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
128
129 // now set up the cross-referencing between tiles
130 for (int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
131 for (int iphi = 0; iphi < _n_tiles_phi; iphi++) {
132 Tile * tile = & _tiles[_tile_index(ieta,iphi)];
133 // no jets in this tile yet
134 tile->head = NULL; // first element of tiles points to itself
135 tile->begin_tiles[0] = tile;
136 Tile ** pptile = & (tile->begin_tiles[0]);
137 pptile++;
138 //
139 // set up L's in column to the left of X
140 tile->surrounding_tiles = pptile;
141 if (ieta > _tiles_ieta_min) {
142 // with the itile subroutine, we can safely run tiles from
143 // idphi=-1 to idphi=+1, because it takes care of
144 // negative and positive boundaries
145 for (int idphi = -1; idphi <=+1; idphi++) {
146 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
147 pptile++;
148 }
149 }
150 // now set up last L (below X)
151 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
152 pptile++;
153 // set up first R (above X)
154 tile->RH_tiles = pptile;
155 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
156 pptile++;
157 // set up remaining R's, to the right of X
158 if (ieta < _tiles_ieta_max) {
159 for (int idphi = -1; idphi <= +1; idphi++) {
160 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
161 pptile++;
162 }
163 }
164 // now put semaphore for end tile
165 tile->end_tiles = pptile;
166 // finally make sure tiles are untagged
167 tile->tagged = false;
168 }
169 }
170
171}
172
173
174//----------------------------------------------------------------------
175/// return the tile index corresponding to the given eta,phi point
176int ClusterSequence::_tile_index(const double eta, const double phi) const {
177 int ieta, iphi;
178 if (eta <= _tiles_eta_min) {ieta = 0;}
179 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
180 else {
181 //ieta = int(floor((eta - _tiles_eta_min) / _tile_size_eta));
182 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
183 // following needed in case of rare but nasty rounding errors
184 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
185 ieta = _tiles_ieta_max-_tiles_ieta_min;}
186 }
187 // allow for some extent of being beyond range in calculation of phi
188 // as well
189 //iphi = (int(floor(phi/_tile_size_phi)) + _n_tiles_phi) % _n_tiles_phi;
190 // with just int and no floor, things run faster but beware
191 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
192 return (iphi + ieta * _n_tiles_phi);
193}
194
195
196//----------------------------------------------------------------------
197// overloaded version which additionally sets up information regarding the
198// tiling
199inline void ClusterSequence::_tj_set_jetinfo( TiledJet * const jet,
200 const int _jets_index) {
201 // first call the generic setup
202 _bj_set_jetinfo<>(jet, _jets_index);
203
204 // Then do the setup specific to the tiled case.
205
206 // Find out which tile it belonds to
207 jet->tile_index = _tile_index(jet->eta, jet->phi);
208
209 // Insert it into the tile's linked list of jets
210 Tile * tile = &_tiles[jet->tile_index];
211 jet->previous = NULL;
212 jet->next = tile->head;
213 if (jet->next != NULL) {jet->next->previous = jet;}
214 tile->head = jet;
215}
216
217
218//----------------------------------------------------------------------
219/// output the contents of the tiles
220void ClusterSequence::_print_tiles(TiledJet * briefjets ) const {
221 for (vector<Tile>::const_iterator tile = _tiles.begin();
222 tile < _tiles.end(); tile++) {
223 cout << "Tile " << tile - _tiles.begin()<<" = ";
224 vector<int> list;
225 for (TiledJet * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
226 list.push_back(jetI-briefjets);
227 //cout <<" "<<jetI-briefjets;
228 }
229 sort(list.begin(),list.end());
230 for (unsigned int i = 0; i < list.size(); i++) {cout <<" "<<list[i];}
231 cout <<"\n";
232 }
233}
234
235
236//----------------------------------------------------------------------
237/// Add to the vector tile_union the tiles that are in the neighbourhood
238/// of the specified tile_index, including itself -- start adding
239/// from position n_near_tiles-1, and increase n_near_tiles as
240/// you go along (could have done it more C++ like with vector with reserved
241/// space, but fear is that it would have been slower, e.g. checking
242/// for end of vector at each stage to decide whether to resize it)
243void ClusterSequence::_add_neighbours_to_tile_union(const int tile_index,
244 vector<int> & tile_union, int & n_near_tiles) const {
245 for (Tile * const * near_tile = _tiles[tile_index].begin_tiles;
246 near_tile != _tiles[tile_index].end_tiles; near_tile++){
247 // get the tile number
248 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
249 n_near_tiles++;
250 }
251}
252
253
254//----------------------------------------------------------------------
255/// Like _add_neighbours_to_tile_union, but only adds neighbours if
256/// their "tagged" status is false; when a neighbour is added its
257/// tagged status is set to true.
258///
259/// Note that with a high level of warnings (-pedantic -Wextra -ansi,
260/// gcc complains about tile_index maybe being used uninitialised for
261/// oldB in ClusterSequence::_minheap_faster_tiled_N2_cluster(). We
262/// have explicitly checked that it was harmless so we could disable
263/// the gcc warning by hand using the construct below
264///
265/// #pragma GCC diagnostic push
266/// #pragma GCC diagnostic ignored "-Wpragmas"
267/// #pragma GCC diagnostic ignored "-Wuninitialized"
268/// #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
269/// ...
270/// #pragma GCC diagnostic pop
271///
272/// the @GCC diagnostic push/pop directive was only introduced in
273/// gcc-4.6, so for broader usage, we'd need to insert #pragma GCC
274/// diagnostic ignored "-Wpragmas" at the top of this file
275inline void ClusterSequence::_add_untagged_neighbours_to_tile_union(
276 const int tile_index,
277 vector<int> & tile_union, int & n_near_tiles) {
278 for (Tile ** near_tile = _tiles[tile_index].begin_tiles;
279 near_tile != _tiles[tile_index].end_tiles; near_tile++){
280 if (! (*near_tile)->tagged) {
281 (*near_tile)->tagged = true;
282 // get the tile number
283 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
284 n_near_tiles++;
285 }
286 }
287}
288
289
290//----------------------------------------------------------------------
291/// run a tiled clustering
292void ClusterSequence::_tiled_N2_cluster() {
293
294 _initialise_tiles();
295
296 int n = _jets.size();
297 TiledJet * briefjets = new TiledJet[n];
298 TiledJet * jetA = briefjets, * jetB;
299 TiledJet oldB;
300 oldB.tile_index=0; // prevents a gcc warning
301
302 // will be used quite deep inside loops, but declare it here so that
303 // memory (de)allocation gets done only once
304 vector<int> tile_union(3*n_tile_neighbours);
305
306 // initialise the basic jet info
307 for (int i = 0; i< n; i++) {
308 _tj_set_jetinfo(jetA, i);
309 //cout << i<<": "<<jetA->tile_index<<"\n";
310 jetA++; // move on to next entry of briefjets
311 }
312 TiledJet * tail = jetA; // a semaphore for the end of briefjets
313 TiledJet * head = briefjets; // a nicer way of naming start
314
315 // set up the initial nearest neighbour information
316 vector<Tile>::const_iterator tile;
317 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
318 // first do it on this tile
319 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
320 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
321 double dist = _bj_dist(jetA,jetB);
322 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
323 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
324 }
325 }
326 // then do it for RH tiles
327 for (Tile ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
328 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
329 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
330 double dist = _bj_dist(jetA,jetB);
331 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
332 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
333 }
334 }
335 }
336 }
337
338 // now create the diJ (where J is i's NN) table -- remember that
339 // we differ from standard normalisation here by a factor of R2
340 double * diJ = new double[n];
341 jetA = head;
342 for (int i = 0; i < n; i++) {
343 diJ[i] = _bj_diJ(jetA);
344 jetA++; // have jetA follow i
345 }
346
347 // now run the recombination loop
348 while (tail != head) {
349
350 // find the minimum of the diJ on this round
351 double diJ_min = diJ[0];
352 int diJ_min_jet = 0;
353 for (int i = 1; i < n; i++) {
354 if (diJ[i] < diJ_min) {diJ_min_jet = i; diJ_min = diJ[i];}
355 }
356
357 // do the recombination between A and B
358 jetA = & briefjets[diJ_min_jet];
359 jetB = jetA->NN;
360 // put the normalisation back in
361 diJ_min *= _invR2;
362
363 //if (n == 19) {cout << "Hello "<<jetA-head<<" "<<jetB-head<<"\n";}
364
365 //cout <<" WILL RECOMBINE "<< jetA-briefjets<<" "<<jetB-briefjets<<"\n";
366
367 if (jetB != NULL) {
368 // jet-jet recombination
369 // If necessary relabel A & B to ensure jetB < jetA, that way if
370 // the larger of them == newtail then that ends up being jetA and
371 // the new jet that is added as jetB is inserted in a position that
372 // has a future!
373 if (jetA < jetB) {std::swap(jetA,jetB);}
374
375 int nn; // new jet index
376 _do_ij_recombination_step(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
377
378 // what was jetB will now become the new jet
379 _bj_remove_from_tiles(jetA);
380 oldB = * jetB; // take a copy because we will need it...
381 _bj_remove_from_tiles(jetB);
382 _tj_set_jetinfo(jetB, nn); // also registers the jet in the tiling
383 } else {
384 // jet-beam recombination
385 _do_iB_recombination_step(jetA->_jets_index, diJ_min);
386
387 _bj_remove_from_tiles(jetA);
388 }
389
390 // first establish the set of tiles over which we are going to
391 // have to run searches for updated and new nearest-neighbours
392 int n_near_tiles = 0;
393 _add_neighbours_to_tile_union(jetA->tile_index, tile_union, n_near_tiles);
394 if (jetB != NULL) {
395 bool sort_it = false;
396 if (jetB->tile_index != jetA->tile_index) {
397 sort_it = true;
398 _add_neighbours_to_tile_union(jetB->tile_index,tile_union,n_near_tiles);
399 }
400 if (oldB.tile_index != jetA->tile_index &&
401 oldB.tile_index != jetB->tile_index) {
402 sort_it = true;
403 _add_neighbours_to_tile_union(oldB.tile_index,tile_union,n_near_tiles);
404 }
405
406 if (sort_it) {
407 // sort the tiles before then compressing the list
408 sort(tile_union.begin(), tile_union.begin()+n_near_tiles);
409 // and now condense the list
410 int nnn = 1;
411 for (int i = 1; i < n_near_tiles; i++) {
412 if (tile_union[i] != tile_union[nnn-1]) {
413 tile_union[nnn] = tile_union[i];
414 nnn++;
415 }
416 }
417 n_near_tiles = nnn;
418 }
419 }
420
421 // now update our nearest neighbour info and diJ table
422 // first reduce size of table
423 tail--; n--;
424 if (jetA == tail) {
425 // there is nothing to be done
426 } else {
427 // Copy last jet contents and diJ info into position of jetA
428 *jetA = *tail;
429 diJ[jetA - head] = diJ[tail-head];
430 // IN the tiling fix pointers to tail and turn them into
431 // pointers to jetA (from predecessors, successors and the tile
432 // head if need be)
433 if (jetA->previous == NULL) {
434 _tiles[jetA->tile_index].head = jetA;
435 } else {
436 jetA->previous->next = jetA;
437 }
438 if (jetA->next != NULL) {jetA->next->previous = jetA;}
439 }
440
441 // Initialise jetB's NN distance as well as updating it for
442 // other particles.
443 for (int itile = 0; itile < n_near_tiles; itile++) {
444 Tile * tile_ptr = &_tiles[tile_union[itile]];
445 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
446 // see if jetI had jetA or jetB as a NN -- if so recalculate the NN
447 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
448 jetI->NN_dist = _R2;
449 jetI->NN = NULL;
450 // now go over tiles that are neighbours of I (include own tile)
451 for (Tile ** near_tile = tile_ptr->begin_tiles;
452 near_tile != tile_ptr->end_tiles; near_tile++) {
453 // and then over the contents of that tile
454 for (TiledJet * jetJ = (*near_tile)->head;
455 jetJ != NULL; jetJ = jetJ->next) {
456 double dist = _bj_dist(jetI,jetJ);
457 if (dist < jetI->NN_dist && jetJ != jetI) {
458 jetI->NN_dist = dist; jetI->NN = jetJ;
459 }
460 }
461 }
462 diJ[jetI-head] = _bj_diJ(jetI); // update diJ
463 }
464 // check whether new jetB is closer than jetI's current NN and
465 // if need to update things
466 if (jetB != NULL) {
467 double dist = _bj_dist(jetI,jetB);
468 if (dist < jetI->NN_dist) {
469 if (jetI != jetB) {
470 jetI->NN_dist = dist;
471 jetI->NN = jetB;
472 diJ[jetI-head] = _bj_diJ(jetI); // update diJ...
473 }
474 }
475 if (dist < jetB->NN_dist) {
476 if (jetI != jetB) {
477 jetB->NN_dist = dist;
478 jetB->NN = jetI;}
479 }
480 }
481 }
482 }
483
484
485 if (jetB != NULL) {diJ[jetB-head] = _bj_diJ(jetB);}
486 //cout << n<<" "<<briefjets[95].NN-briefjets<<" "<<briefjets[95].NN_dist <<"\n";
487
488 // remember to update pointers to tail
489 for (Tile ** near_tile = _tiles[tail->tile_index].begin_tiles;
490 near_tile!= _tiles[tail->tile_index].end_tiles; near_tile++){
491 // and then the contents of that tile
492 for (TiledJet * jetJ = (*near_tile)->head;
493 jetJ != NULL; jetJ = jetJ->next) {
494 if (jetJ->NN == tail) {jetJ->NN = jetA;}
495 }
496 }
497
498 //for (int i = 0; i < n; i++) {
499 // if (briefjets[i].NN-briefjets >= n && briefjets[i].NN != NULL) {cout <<"YOU MUST BE CRAZY for n ="<<n<<", i = "<<i<<", NN = "<<briefjets[i].NN-briefjets<<"\n";}
500 //}
501
502
503 if (jetB != NULL) {diJ[jetB-head] = _bj_diJ(jetB);}
504 //cout << briefjets[95].NN-briefjets<<" "<<briefjets[95].NN_dist <<"\n";
505
506 }
507
508 // final cleaning up;
509 delete[] diJ;
510 delete[] briefjets;
511}
512
513
514//----------------------------------------------------------------------
515/// run a tiled clustering
516void ClusterSequence::_faster_tiled_N2_cluster() {
517
518 _initialise_tiles();
519
520 int n = _jets.size();
521 TiledJet * briefjets = new TiledJet[n];
522 TiledJet * jetA = briefjets, * jetB;
523 TiledJet oldB;
524 oldB.tile_index=0; // prevents a gcc warning
525
526 // will be used quite deep inside loops, but declare it here so that
527 // memory (de)allocation gets done only once
528 vector<int> tile_union(3*n_tile_neighbours);
529
530 // initialise the basic jet info
531 for (int i = 0; i< n; i++) {
532 _tj_set_jetinfo(jetA, i);
533 //cout << i<<": "<<jetA->tile_index<<"\n";
534 jetA++; // move on to next entry of briefjets
535 }
536 TiledJet * head = briefjets; // a nicer way of naming start
537
538 // set up the initial nearest neighbour information
539 vector<Tile>::const_iterator tile;
540 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
541 // first do it on this tile
542 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
543 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
544 double dist = _bj_dist(jetA,jetB);
545 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
546 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
547 }
548 }
549 // then do it for RH tiles
550 for (Tile ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
551 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
552 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
553 double dist = _bj_dist(jetA,jetB);
554 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
555 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
556 }
557 }
558 }
559 // no need to do it for LH tiles, since they are implicitly done
560 // when we set NN for both jetA and jetB on the RH tiles.
561 }
562
563 // now create the diJ (where J is i's NN) table -- remember that
564 // we differ from standard normalisation here by a factor of R2
565 // (corrected for at the end).
566 struct diJ_plus_link {
567 double diJ; // the distance
568 TiledJet * jet; // the jet (i) for which we've found this distance
569 // (whose NN will the J).
570 };
571 diJ_plus_link * diJ = new diJ_plus_link[n];
572 jetA = head;
573 for (int i = 0; i < n; i++) {
574 diJ[i].diJ = _bj_diJ(jetA); // kt distance * R^2
575 diJ[i].jet = jetA; // our compact diJ table will not be in
576 jetA->diJ_posn = i; // one-to-one corresp. with non-compact jets,
577 // so set up bi-directional correspondence here.
578 jetA++; // have jetA follow i
579 }
580
581 // now run the recombination loop
582 while (n > 0) {
583
584 // find the minimum of the diJ on this round
585 diJ_plus_link * best, *stop; // pointers a bit faster than indices
586 // could use best to keep track of diJ min, but it turns out to be
587 // marginally faster to have a separate variable (avoids n
588 // dereferences at the expense of n/2 assignments).
589 double diJ_min = diJ[0].diJ; // initialise the best one here.
590 best = diJ; // and here
591 stop = diJ+n;
592 for (diJ_plus_link * here = diJ+1; here != stop; here++) {
593 if (here->diJ < diJ_min) {best = here; diJ_min = here->diJ;}
594 }
595
596 // do the recombination between A and B
597 jetA = best->jet;
598 jetB = jetA->NN;
599 // put the normalisation back in
600 diJ_min *= _invR2;
601
602 if (jetB != NULL) {
603 // jet-jet recombination
604 // If necessary relabel A & B to ensure jetB < jetA, that way if
605 // the larger of them == newtail then that ends up being jetA and
606 // the new jet that is added as jetB is inserted in a position that
607 // has a future!
608 if (jetA < jetB) {std::swap(jetA,jetB);}
609
610 int nn; // new jet index
611 _do_ij_recombination_step(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
612
613 // what was jetB will now become the new jet
614 _bj_remove_from_tiles(jetA);
615 oldB = * jetB; // take a copy because we will need it...
616 _bj_remove_from_tiles(jetB);
617 _tj_set_jetinfo(jetB, nn); // cause jetB to become _jets[nn]
618 // (also registers the jet in the tiling)
619 } else {
620 // jet-beam recombination
621 // get the hist_index
622 _do_iB_recombination_step(jetA->_jets_index, diJ_min);
623 _bj_remove_from_tiles(jetA);
624 }
625
626 // first establish the set of tiles over which we are going to
627 // have to run searches for updated and new nearest-neighbours --
628 // basically a combination of vicinity of the tiles of the two old
629 // and one new jet.
630 int n_near_tiles = 0;
631 _add_untagged_neighbours_to_tile_union(jetA->tile_index,
632 tile_union, n_near_tiles);
633 if (jetB != NULL) {
634 if (jetB->tile_index != jetA->tile_index) {
635 _add_untagged_neighbours_to_tile_union(jetB->tile_index,
636 tile_union,n_near_tiles);
637 }
638 if (oldB.tile_index != jetA->tile_index &&
639 oldB.tile_index != jetB->tile_index) {
640 _add_untagged_neighbours_to_tile_union(oldB.tile_index,
641 tile_union,n_near_tiles);
642 }
643 }
644
645 // now update our nearest neighbour info and diJ table
646 // first reduce size of table
647 n--;
648 // then compactify the diJ by taking the last of the diJ and copying
649 // it to the position occupied by the diJ for jetA
650 diJ[n].jet->diJ_posn = jetA->diJ_posn;
651 diJ[jetA->diJ_posn] = diJ[n];
652
653 // Initialise jetB's NN distance as well as updating it for
654 // other particles.
655 // Run over all tiles in our union
656 for (int itile = 0; itile < n_near_tiles; itile++) {
657 Tile * tile_ptr = &_tiles[tile_union[itile]];
658 tile_ptr->tagged = false; // reset tag, since we're done with unions
659 // run over all jets in the current tile
660 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
661 // see if jetI had jetA or jetB as a NN -- if so recalculate the NN
662 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
663 jetI->NN_dist = _R2;
664 jetI->NN = NULL;
665 // now go over tiles that are neighbours of I (include own tile)
666 for (Tile ** near_tile = tile_ptr->begin_tiles;
667 near_tile != tile_ptr->end_tiles; near_tile++) {
668 // and then over the contents of that tile
669 for (TiledJet * jetJ = (*near_tile)->head;
670 jetJ != NULL; jetJ = jetJ->next) {
671 double dist = _bj_dist(jetI,jetJ);
672 if (dist < jetI->NN_dist && jetJ != jetI) {
673 jetI->NN_dist = dist; jetI->NN = jetJ;
674 }
675 }
676 }
677 diJ[jetI->diJ_posn].diJ = _bj_diJ(jetI); // update diJ kt-dist
678 }
679 // check whether new jetB is closer than jetI's current NN and
680 // if jetI is closer than jetB's current (evolving) nearest
681 // neighbour. Where relevant update things
682 if (jetB != NULL) {
683 double dist = _bj_dist(jetI,jetB);
684 if (dist < jetI->NN_dist) {
685 if (jetI != jetB) {
686 jetI->NN_dist = dist;
687 jetI->NN = jetB;
688 diJ[jetI->diJ_posn].diJ = _bj_diJ(jetI); // update diJ...
689 }
690 }
691 if (dist < jetB->NN_dist) {
692 if (jetI != jetB) {
693 jetB->NN_dist = dist;
694 jetB->NN = jetI;}
695 }
696 }
697 }
698 }
699
700 // finally, register the updated kt distance for B
701 if (jetB != NULL) {diJ[jetB->diJ_posn].diJ = _bj_diJ(jetB);}
702
703 }
704
705 // final cleaning up;
706 delete[] diJ;
707 delete[] briefjets;
708}
709
710//----------------------------------------------------------------------
711/// run a tiled clustering, with our minheap for keeping track of the
712/// smallest dij
713void ClusterSequence::_minheap_faster_tiled_N2_cluster() {
714
715 _initialise_tiles();
716
717 int n = _jets.size();
718 TiledJet * briefjets = new TiledJet[n];
719 TiledJet * jetA = briefjets, * jetB;
720 TiledJet oldB;
721 oldB.tile_index=0; // prevents a gcc warning
722
723 // will be used quite deep inside loops, but declare it here so that
724 // memory (de)allocation gets done only once
725 vector<int> tile_union(3*n_tile_neighbours);
726
727 // initialise the basic jet info
728 for (int i = 0; i< n; i++) {
729 _tj_set_jetinfo(jetA, i);
730 //cout << i<<": "<<jetA->tile_index<<"\n";
731 jetA++; // move on to next entry of briefjets
732 }
733 TiledJet * head = briefjets; // a nicer way of naming start
734
735 // set up the initial nearest neighbour information
736 vector<Tile>::const_iterator tile;
737 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
738 // first do it on this tile
739 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
740 for (jetB = tile->head; jetB != jetA; jetB = jetB->next) {
741 double dist = _bj_dist(jetA,jetB);
742 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
743 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
744 }
745 }
746 // then do it for RH tiles
747 for (Tile ** RTile = tile->RH_tiles; RTile != tile->end_tiles; RTile++) {
748 for (jetA = tile->head; jetA != NULL; jetA = jetA->next) {
749 for (jetB = (*RTile)->head; jetB != NULL; jetB = jetB->next) {
750 double dist = _bj_dist(jetA,jetB);
751 if (dist < jetA->NN_dist) {jetA->NN_dist = dist; jetA->NN = jetB;}
752 if (dist < jetB->NN_dist) {jetB->NN_dist = dist; jetB->NN = jetA;}
753 }
754 }
755 }
756 // no need to do it for LH tiles, since they are implicitly done
757 // when we set NN for both jetA and jetB on the RH tiles.
758 }
759
760
761 //// now create the diJ (where J is i's NN) table -- remember that
762 //// we differ from standard normalisation here by a factor of R2
763 //// (corrected for at the end).
764 //struct diJ_plus_link {
765 // double diJ; // the distance
766 // TiledJet * jet; // the jet (i) for which we've found this distance
767 // // (whose NN will the J).
768 //};
769 //diJ_plus_link * diJ = new diJ_plus_link[n];
770 //jetA = head;
771 //for (int i = 0; i < n; i++) {
772 // diJ[i].diJ = _bj_diJ(jetA); // kt distance * R^2
773 // diJ[i].jet = jetA; // our compact diJ table will not be in
774 // jetA->diJ_posn = i; // one-to-one corresp. with non-compact jets,
775 // // so set up bi-directional correspondence here.
776 // jetA++; // have jetA follow i
777 //}
778
779 vector<double> diJs(n);
780 for (int i = 0; i < n; i++) {
781 diJs[i] = _bj_diJ(&briefjets[i]);
782 briefjets[i].label_minheap_update_done();
783 }
784 MinHeap minheap(diJs);
785 // have a stack telling us which jets we'll have to update on the heap
786 vector<TiledJet *> jets_for_minheap;
787 jets_for_minheap.reserve(n);
788
789 // now run the recombination loop
790 while (n > 0) {
791
792 double diJ_min = minheap.minval() *_invR2;
793 jetA = head + minheap.minloc();
794
795 // do the recombination between A and B
796 jetB = jetA->NN;
797
798 if (jetB != NULL) {
799 // jet-jet recombination
800 // If necessary relabel A & B to ensure jetB < jetA, that way if
801 // the larger of them == newtail then that ends up being jetA and
802 // the new jet that is added as jetB is inserted in a position that
803 // has a future!
804 if (jetA < jetB) {std::swap(jetA,jetB);}
805
806 int nn; // new jet index
807 _do_ij_recombination_step(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
808
809 // what was jetB will now become the new jet
810 _bj_remove_from_tiles(jetA);
811 oldB = * jetB; // take a copy because we will need it...
812 _bj_remove_from_tiles(jetB);
813 _tj_set_jetinfo(jetB, nn); // cause jetB to become _jets[nn]
814 // (also registers the jet in the tiling)
815 } else {
816 // jet-beam recombination
817 // get the hist_index
818 _do_iB_recombination_step(jetA->_jets_index, diJ_min);
819 _bj_remove_from_tiles(jetA);
820 }
821
822 // remove the minheap entry for jetA
823 minheap.remove(jetA-head);
824
825 // first establish the set of tiles over which we are going to
826 // have to run searches for updated and new nearest-neighbours --
827 // basically a combination of vicinity of the tiles of the two old
828 // and one new jet.
829 int n_near_tiles = 0;
830 _add_untagged_neighbours_to_tile_union(jetA->tile_index,
831 tile_union, n_near_tiles);
832 if (jetB != NULL) {
833 if (jetB->tile_index != jetA->tile_index) {
834 _add_untagged_neighbours_to_tile_union(jetB->tile_index,
835 tile_union,n_near_tiles);
836 }
837 if (oldB.tile_index != jetA->tile_index &&
838 oldB.tile_index != jetB->tile_index) {
839 // GS: the line below generates a warning that oldB.tile_index
840 // may be used uninitialised. However, to reach this point, we
841 // ned jetB != NULL (see test a few lines above) and is jetB
842 // !=NULL, one would have gone through "oldB = *jetB before
843 // (see piece of code ~20 line above), so the index is
844 // initialised. We do not do anything to avoid the warning to
845 // avoid any potential speed impact.
846 _add_untagged_neighbours_to_tile_union(oldB.tile_index,
847 tile_union,n_near_tiles);
848 }
849 // indicate that we'll have to update jetB in the minheap
850 jetB->label_minheap_update_needed();
851 jets_for_minheap.push_back(jetB);
852 }
853
854
855 // Initialise jetB's NN distance as well as updating it for
856 // other particles.
857 // Run over all tiles in our union
858 for (int itile = 0; itile < n_near_tiles; itile++) {
859 Tile * tile_ptr = &_tiles[tile_union[itile]];
860 tile_ptr->tagged = false; // reset tag, since we're done with unions
861 // run over all jets in the current tile
862 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
863 // see if jetI had jetA or jetB as a NN -- if so recalculate the NN
864 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
865 jetI->NN_dist = _R2;
866 jetI->NN = NULL;
867 // label jetI as needing heap action...
868 if (!jetI->minheap_update_needed()) {
869 jetI->label_minheap_update_needed();
870 jets_for_minheap.push_back(jetI);}
871 // now go over tiles that are neighbours of I (include own tile)
872 for (Tile ** near_tile = tile_ptr->begin_tiles;
873 near_tile != tile_ptr->end_tiles; near_tile++) {
874 // and then over the contents of that tile
875 for (TiledJet * jetJ = (*near_tile)->head;
876 jetJ != NULL; jetJ = jetJ->next) {
877 double dist = _bj_dist(jetI,jetJ);
878 if (dist < jetI->NN_dist && jetJ != jetI) {
879 jetI->NN_dist = dist; jetI->NN = jetJ;
880 }
881 }
882 }
883 }
884 // check whether new jetB is closer than jetI's current NN and
885 // if jetI is closer than jetB's current (evolving) nearest
886 // neighbour. Where relevant update things
887 if (jetB != NULL) {
888 double dist = _bj_dist(jetI,jetB);
889 if (dist < jetI->NN_dist) {
890 if (jetI != jetB) {
891 jetI->NN_dist = dist;
892 jetI->NN = jetB;
893 // label jetI as needing heap action...
894 if (!jetI->minheap_update_needed()) {
895 jetI->label_minheap_update_needed();
896 jets_for_minheap.push_back(jetI);}
897 }
898 }
899 if (dist < jetB->NN_dist) {
900 if (jetI != jetB) {
901 jetB->NN_dist = dist;
902 jetB->NN = jetI;}
903 }
904 }
905 }
906 }
907
908 // deal with jets whose minheap entry needs updating
909 while (jets_for_minheap.size() > 0) {
910 TiledJet * jetI = jets_for_minheap.back();
911 jets_for_minheap.pop_back();
912 minheap.update(jetI-head, _bj_diJ(jetI));
913 jetI->label_minheap_update_done();
914 }
915 n--;
916 }
917
918 // final cleaning up;
919 delete[] briefjets;
920}
921
922
923FASTJET_END_NAMESPACE
924