Fork me on GitHub

source: git/external/fastjet/LazyTiling9.cc@ f10999a

ImprovedOutputFile Timing dual_readout llp
Last change on this file since f10999a was 35cdc46, checked in by Pavel Demin <demin@…>, 10 years ago

upgrade FastJet to version 3.1.0-beta.1, upgrade Nsubjettiness to version 2.1.0, add SoftKiller version 1.0.0

  • Property mode set to 100644
File size: 28.1 KB
RevLine 
[35cdc46]1//FJSTARTHEADER
2// $Id: LazyTiling9.cc 3477 2014-07-29 14:34:39Z salam $
3//
4// Copyright (c) 2005-2014, 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#include <iomanip>
33#include <limits>
34#include <cmath>
35#include "fastjet/internal/LazyTiling9.hh"
36#include "fastjet/internal/TilingExtent.hh"
37using namespace std;
38
39// uncomment the line below to use TilingExtent in LazyTiling9
40#define _FASTJET_TILING2_USE_TILING_ANALYSIS_
41
42FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
43
44
45
46LazyTiling9::LazyTiling9(ClusterSequence & cs) :
47 _cs(cs), _jets(cs.jets())
48 //, _minheap(_jets.size())
49{
50#ifdef INSTRUMENT2
51 _ncall = 0; // gps tmp
52 _ncall_dtt = 0; // gps tmp
53#endif // INSTRUMENT2
54 _Rparam = cs.jet_def().R();
55 _R2 = _Rparam * _Rparam;
56 _invR2 = 1.0 / _R2;
57 _initialise_tiles();
58}
59
60
61//----------------------------------------------------------------------
62/// Set up the tiles:
63/// - decide the range in eta
64/// - allocate the tiles
65/// - set up the cross-referencing info between tiles
66///
67/// The neighbourhood of a tile is set up as follows
68///
69/// LRR
70/// LXR
71/// LLR
72///
73/// such that tiles is an array containing XLLLLRRRR with pointers
74/// | \ RH_tiles
75/// \ surrounding_tiles
76///
77/// with appropriate precautions when close to the edge of the tiled
78/// region.
79///
80void LazyTiling9::_initialise_tiles() {
81
82 // first decide tile sizes (with a lower bound to avoid huge memory use with
83 // very small R)
84 double default_size = max(0.1,_Rparam);
85 _tile_size_eta = default_size;
86 // it makes no sense to go below 3 tiles in phi -- 3 tiles is
87 // sufficient to make sure all pair-wise combinations up to pi in
88 // phi are possible
89 _n_tiles_phi = max(3,int(floor(twopi/default_size)));
90 _tile_size_phi = twopi / _n_tiles_phi; // >= _Rparam and fits in 2pi
91
92#ifdef _FASTJET_TILING2_USE_TILING_ANALYSIS_
93 // testing
94 TilingExtent tiling_analysis(_cs);
95 _tiles_eta_min = tiling_analysis.minrap();
96 _tiles_eta_max = tiling_analysis.maxrap();
97 //cout << "Using timing analysis " << " " << _tiles_eta_min << " " << _tiles_eta_max << endl;
98#else
99 // always include zero rapidity in the tiling region
100 _tiles_eta_min = 0.0;
101 _tiles_eta_max = 0.0;
102 // but go no further than following
103 const double maxrap = 7.0;
104
105 // and find out how much further one should go
106 for(unsigned int i = 0; i < _jets.size(); i++) {
107 double eta = _jets[i].rap();
108 // first check if eta is in range -- to avoid taking into account
109 // very spurious rapidities due to particles with near-zero kt.
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;}
113 }
114 }
115 //cout << "NOT using timing analysis " << " " << _tiles_eta_min << " " << _tiles_eta_max << endl;
116#endif
117
118 // Now adjust the values for the rapidity ("eta") range.
119 //
120 // When tile_size_eta is large, we have two options:
121 // - always have at least two tiles in rapidity (FASTJET_LAZY9_MIN2TILESY),
122 // currently split down the middle of the rapidity extent of the particles
123 // - take whatever we get from the original
124 // _tiles_ieta_min[max] = int(floor(_tiles_eta_min[max]/_tile_size_eta));
125 // which will sometimes leave us with just one tile in Y;
126 //
127 // For events that are symetric in Y this should not change
128 // anything, but for asymmetric ones FASTJET_LAZY9_MIN2TILESY may be
129 // a bit faster.
130 //
131#define FASTJET_LAZY9_MIN2TILESY
132#ifdef FASTJET_LAZY9_MIN2TILESY
133 if (_tiles_eta_max - _tiles_eta_min < 2*_tile_size_eta) {
134 // if we have a rapidity coverage that is small compared to the
135 // tile size then we can adjust the grid in rapidity so as to
136 // have exactly 3 tiles. This can give relevant speed improvements
137 // for large R jets
138 _tile_size_eta = (_tiles_eta_max - _tiles_eta_min)/2;
139 _tiles_ieta_min = 0;
140 _tiles_ieta_max = 1;
141 // the eta max value is being taken as the lower edge of the
142 // highest-y tile
143 _tiles_eta_max -= _tile_size_eta;
144 } else {
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
151 }
152#endif
153
154 _tile_half_size_eta = _tile_size_eta * 0.5;
155 _tile_half_size_phi = _tile_size_phi * 0.5;
156
157 // set up information about whether we need to allow for "periodic"
158 // wrapping tests in delta_phi calculations
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);
162 } else {
163 use_periodic_delta_phi[0] = true;
164 use_periodic_delta_phi[_n_tiles_phi-1] = true;
165 }
166
167 // allocate the tiles
168 _tiles.resize((_tiles_ieta_max-_tiles_ieta_min+1)*_n_tiles_phi);
169
170 // now set up the cross-referencing between tiles
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)];
174 // no jets in this tile yet
175 tile->head = NULL; // first element of tiles points to itself
176 tile->begin_tiles[0] = tile;
177 Tile2 ** pptile = & (tile->begin_tiles[0]);
178 pptile++;
179 //
180 // set up L's in column to the left of X
181 tile->surrounding_tiles = pptile;
182 if (ieta > _tiles_ieta_min) {
183 // with the itile subroutine, we can safely run tiles from
184 // idphi=-1 to idphi=+1, because it takes care of
185 // negative and positive boundaries
186 for (int idphi = -1; idphi <=+1; idphi++) {
187 *pptile = & _tiles[_tile_index(ieta-1,iphi+idphi)];
188 pptile++;
189 }
190 }
191 // now set up last L (below X)
192 *pptile = & _tiles[_tile_index(ieta,iphi-1)];
193 pptile++;
194 // set up first R (above X)
195 tile->RH_tiles = pptile;
196 *pptile = & _tiles[_tile_index(ieta,iphi+1)];
197 pptile++;
198 // set up remaining R's, to the right of X
199 if (ieta < _tiles_ieta_max) {
200 for (int idphi = -1; idphi <= +1; idphi++) {
201 *pptile = & _tiles[_tile_index(ieta+1,iphi+idphi)];
202 pptile++;
203 }
204 }
205 // now put semaphore for end tile
206 tile->end_tiles = pptile;
207 // finally make sure tiles are untagged
208 tile->tagged = false;
209 // and store the information about periodicity in phi
210 tile->use_periodic_delta_phi = use_periodic_delta_phi[iphi];
211 // and ensure max distance is sensibly initialised
212 tile->max_NN_dist = 0;
213 // and also position of centre of tile
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;
216 }
217 }
218
219}
220
221//----------------------------------------------------------------------
222/// return the tile index corresponding to the given eta,phi point
223int LazyTiling9::_tile_index(const double eta, const double phi) const {
224 int ieta, iphi;
225 if (eta <= _tiles_eta_min) {ieta = 0;}
226 else if (eta >= _tiles_eta_max) {ieta = _tiles_ieta_max-_tiles_ieta_min;}
227 else {
228 //ieta = int(floor((eta - _tiles_eta_min) / _tile_size_eta));
229 ieta = int(((eta - _tiles_eta_min) / _tile_size_eta));
230 // following needed in case of rare but nasty rounding errors
231 if (ieta > _tiles_ieta_max-_tiles_ieta_min) {
232 ieta = _tiles_ieta_max-_tiles_ieta_min;}
233 }
234 // allow for some extent of being beyond range in calculation of phi
235 // as well
236 //iphi = (int(floor(phi/_tile_size_phi)) + _n_tiles_phi) % _n_tiles_phi;
237 // with just int and no floor, things run faster but beware
238 iphi = int((phi+twopi)/_tile_size_phi) % _n_tiles_phi;
239 return (iphi + ieta * _n_tiles_phi);
240}
241
242
243//----------------------------------------------------------------------
244// sets up information regarding the tiling of the given jet
245inline void LazyTiling9::_tj_set_jetinfo( TiledJet * const jet,
246 const int _jets_index) {
247 // first call the generic setup
248 _bj_set_jetinfo<>(jet, _jets_index);
249
250 // Then do the setup specific to the tiled case.
251
252 // Find out which tile it belonds to
253 jet->tile_index = _tile_index(jet->eta, jet->phi);
254
255 // Insert it into the tile's linked list of jets
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;}
260 tile->head = jet;
261}
262
263
264//----------------------------------------------------------------------
265void LazyTiling9::_bj_remove_from_tiles(TiledJet * const jet) {
266 Tile2 * tile = & _tiles[jet->tile_index];
267
268 if (jet->previous == NULL) {
269 // we are at head of the tile, so reset it.
270 // If this was the only jet on the tile then tile->head will now be NULL
271 tile->head = jet->next;
272 } else {
273 // adjust link from previous jet in this tile
274 jet->previous->next = jet->next;
275 }
276 if (jet->next != NULL) {
277 // adjust backwards-link from next jet in this tile
278 jet->next->previous = jet->previous;
279 }
280}
281
282
283//----------------------------------------------------------------------
284/// output the contents of the tiles
285void 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()<<" = ";
289 vector<int> list;
290 for (TiledJet * jetI = tile->head; jetI != NULL; jetI = jetI->next) {
291 list.push_back(jetI-briefjets);
292 //cout <<" "<<jetI-briefjets;
293 }
294 sort(list.begin(),list.end());
295 for (unsigned int i = 0; i < list.size(); i++) {cout <<" "<<list[i];}
296 cout <<"\n";
297 }
298}
299
300
301//----------------------------------------------------------------------
302/// Add to the vector tile_union the tiles that are in the neighbourhood
303/// of the specified tile_index, including itself -- start adding
304/// from position n_near_tiles-1, and increase n_near_tiles as
305/// you go along (could have done it more C++ like with vector with reserved
306/// space, but fear is that it would have been slower, e.g. checking
307/// for end of vector at each stage to decide whether to resize it)
308void 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++){
312 // get the tile number
313 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
314 n_near_tiles++;
315 }
316}
317
318
319//----------------------------------------------------------------------
320/// Like _add_neighbours_to_tile_union, but only adds neighbours if
321/// their "tagged" status is false; when a neighbour is added its
322/// tagged status is set to true.
323inline 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;
330 // get the tile number
331 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
332 n_near_tiles++;
333 }
334 }
335}
336
337//----------------------------------------------------------------------
338/// Like _add_neighbours_to_tile_union, but adds tiles that are
339/// "neighbours" of a jet (rather than a tile) and only if a
340/// neighbouring tile's max_NN_dist is >= the distance between the jet
341/// and the nearest point on the tile. It ignores tiles that have
342/// already been tagged.
343inline 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];
347
348 for (Tile2 ** near_tile = tile.begin_tiles; near_tile != tile.end_tiles; near_tile++){
349 if ((*near_tile)->tagged) continue;
350 double dist = _distance_to_tile(jet, *near_tile);
351 // cout << " max info looked at tile " << *near_tile - &_tiles[0]
352 // << ", dist = " << dist << " " << (*near_tile)->max_NN_dist
353 // << endl;
354 if (dist > (*near_tile)->max_NN_dist) continue;
355
356 // cout << " max info tagged tile " << *near_tile - &_tiles[0] << endl;
357 (*near_tile)->tagged = true;
358 // get the tile number
359 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
360 n_near_tiles++;
361 }
362}
363
364////--------TMPTMPTMPTMPTMP-----GPS TEMP--------------------
365//ostream & operator<<(ostream & ostr, const TiledJet & jet) {
366// ostr << "j" << setw(3) << jet._jets_index << ":pt2,rap,phi=" ; ostr.flush();
367// ostr << jet.kt2 << ","; ostr.flush();
368// ostr << jet.eta << ","; ostr.flush();
369// ostr << jet.phi; ostr.flush();
370// ostr << ", tile=" << jet.tile_index; ostr.flush();
371// return ostr;
372//}
373
374
375//----------------------------------------------------------------------
376/// returns a particle's distance to the edge of the specified tile
377inline double LazyTiling9::_distance_to_tile(const TiledJet * bj, const Tile2 * tile)
378#ifdef INSTRUMENT2
379 {
380 _ncall_dtt++; // GPS tmp
381#else
382 const {
383#endif // INSTRUMENT2
384 // Note the careful way of checking the minimum potential deta:
385 // unlike the phi case below, we don't calculate the distance to the
386 // centre and subtract spacing/2. This is because of issue of
387 // boundary tiles, which can extend far beyond spacing/2 in eta.
388 // Using the positions of tile centers should instead be safe.
389 double deta;
390 if (_tiles[bj->tile_index].eta_centre == tile->eta_centre) deta = 0;
391 //else deta = std::abs(bj->eta - tile->eta_centre) - 0.5*_tile_size_eta;
392 else deta = std::abs(bj->eta - tile->eta_centre) - _tile_half_size_eta;
393 // ------
394 // |
395 // A | B
396 // ------
397 // |
398 // C | D
399 // ------
400
401 double dphi = std::abs(bj->phi - tile->phi_centre);
402 if (dphi > pi) dphi = twopi-dphi;
403 dphi -= _tile_half_size_phi;
404 //dphi -= 0.5*_tile_size_phi;
405 if (dphi < 0) dphi = 0;
406
407 return dphi*dphi + deta*deta;
408}
409
410
411
412
413//----------------------------------------------------------------------
414/// looks at distance between jetX and jetI and updates the NN
415/// information if relevant; also pushes identity of jetI onto
416/// the vector of jets for minheap, to signal that it will have
417/// to be handled later.
418///
419/// GPS TEMP GPS TMP: REMOVE THIS LATER: EVEN LABELLED AS INLINE, THE
420/// CALL ADDS A SUBSTANTIAL PENALTY...
421inline void LazyTiling9::_update_jetX_jetI_NN(TiledJet * jetX, TiledJet * jetI, vector<TiledJet *> & jets_for_minheap) {
422 double dist = _bj_dist(jetI,jetX);
423 if (dist < jetI->NN_dist) {
424 if (jetI != jetX) {
425 jetI->NN_dist = dist;
426 jetI->NN = jetX;
427 // label jetI as needing heap action...
428 if (!jetI->minheap_update_needed()) {
429 jetI->label_minheap_update_needed();
430 jets_for_minheap.push_back(jetI);
431 }
432 }
433 }
434 if (dist < jetX->NN_dist) {
435 if (jetI != jetX) {
436 jetX->NN_dist = dist;
437 jetX->NN = jetI;}
438 }
439}
440
441
442inline void LazyTiling9::_set_NN(TiledJet * jetI,
443 vector<TiledJet *> & jets_for_minheap) {
444 jetI->NN_dist = _R2;
445 jetI->NN = NULL;
446 // label jetI as needing heap action...
447 if (!jetI->minheap_update_needed()) {
448 jetI->label_minheap_update_needed();
449 jets_for_minheap.push_back(jetI);}
450 // now go over tiles that are neighbours of I (include own tile)
451 Tile2 * tile_ptr = &_tiles[jetI->tile_index];
452 //if (tile_ptr->is_near_zero_phi(_tile_size_phi)) {
453 for (Tile2 ** near_tile = tile_ptr->begin_tiles;
454 near_tile != tile_ptr->end_tiles; near_tile++) {
455 // for own tile, this will be zero automatically: should we be clever
456 // and skip the test? (With some doubling of code?)
457 if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile)) continue;
458 // and then over the contents of that tile
459 for (TiledJet * jetJ = (*near_tile)->head;
460 jetJ != NULL; jetJ = jetJ->next) {
461 double dist = _bj_dist(jetI,jetJ);
462 if (dist < jetI->NN_dist && jetJ != jetI) {
463 jetI->NN_dist = dist; jetI->NN = jetJ;
464 }
465 }
466 }
467 // } else {
468 // // second copy that exploits the fact that for this tile we needn't worry
469 // // about periodicity
470 // for (Tile2 ** near_tile = tile_ptr->begin_tiles;
471 // near_tile != tile_ptr->end_tiles; near_tile++) {
472 // // for own tile, this will be zero automatically: should we be clever
473 // // and skip the test? (With some doubling of code?)
474 // if (jetI->NN_dist < _distance_to_tile(jetI, *near_tile)) continue;
475 // // and then over the contents of that tile
476 // for (TiledJet * jetJ = (*near_tile)->head;
477 // jetJ != NULL; jetJ = jetJ->next) {
478 // double dist = _bj_dist_not_periodic(jetI,jetJ);
479 // if (dist < jetI->NN_dist && jetJ != jetI) {
480 // jetI->NN_dist = dist; jetI->NN = jetJ;
481 // }
482 // }
483 // }
484 // }
485}
486
487
488void LazyTiling9::run() {
489
490 //_initialise_tiles();
491
492 int n = _jets.size();
493 if (n == 0) return;
494
495 TiledJet * briefjets = new TiledJet[n];
496 TiledJet * jetA = briefjets, * jetB;
497 // avoid warning about uninitialised oldB below;
498 // only valid for n>=1 (hence the test n==0 test above)
499 TiledJet oldB = briefjets[0];
500
501 // will be used quite deep inside loops, but declare it here so that
502 // memory (de)allocation gets done only once
503 vector<int> tile_union(3*n_tile_neighbours);
504
505 // initialise the basic jet info
506 for (int i = 0; i< n; i++) {
507 _tj_set_jetinfo(jetA, i);
508 //cout << i<<": "<<jetA->tile_index<<"\n";
509 jetA++; // move on to next entry of briefjets
510 }
511 TiledJet * head = briefjets; // a nicer way of naming start
512
513
514 // // count the contents of the tiles
515 // for (int ieta = _tiles_ieta_min; ieta <= _tiles_ieta_max; ieta++) {
516 // for (int iphi = 0; iphi < _n_tiles_phi; iphi++) {
517 // Tile2 * tile = & _tiles[_tile_index(ieta,iphi)];
518 // int njets = 0;
519 // const TiledJet * jet = tile->head;
520 // while (jet != 0) {
521 // njets++;
522 // jet = jet->next;
523 // }
524 // cout << ieta
525 // << " " << iphi
526 // << " " << tile->jet_count()
527 // << endl;
528 // }
529 // }
530
531
532 // set up the initial nearest neighbour information
533 vector<Tile2>::iterator tile;
534 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
535 // first do it on this 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;}
541 }
542 }
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;
545 }
546 }
547 for (tile = _tiles.begin(); tile != _tiles.end(); tile++) {
548 if (tile->use_periodic_delta_phi) {
549 // then do it for RH tiles;
550 for (Tile2 ** 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);
553 // it only makes sense to do a tile if jetA is close enough to the Rtile
554 // either for a jet in the Rtile to be closer to jetA than it's current NN
555 // or if jetA could be closer to something in the Rtile than the largest
556 // NN distance within the RTile.
557 //
558 // GPS note: also tried approach where we perform only the
559 // first test and run over all surrounding tiles
560 // (not just RH ones). The test is passed less
561 // frequently, but one is running over more tiles
562 // and on balance, for the trial event we used, it's
563 // a bit slower.
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;}
571 }
572 }
573 }
574 }
575 } else {
576 // this second version of the code uses the faster
577 // "not_periodic" version because it knows that the tile is
578 // sufficiently far from the edge.
579 for (Tile2 ** 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;}
589 }
590 }
591 }
592 }
593 }
594 // no need to do it for LH tiles, since they are implicitly done
595 // when we set NN for both jetA and jetB on the RH tiles.
596 }
597 // Now update the max_NN_dist within each tile. Not strictly
598 // necessary, because existing max_NN_dist is an upper bound. but
599 // costs little and may give some efficiency gain later.
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;
604 }
605 }
606
607#ifdef INSTRUMENT2
608 cout << "intermediate ncall, dtt = " << _ncall << " " << _ncall_dtt << endl; // GPS tmp
609#endif // INSTRUMENT2
610
611 vector<double> diJs(n);
612 for (int i = 0; i < n; i++) {
613 diJs[i] = _bj_diJ(&briefjets[i]);
614 briefjets[i].label_minheap_update_done();
615 }
616 MinHeap minheap(diJs);
617 // have a stack telling us which jets we'll have to update on the heap
618 vector<TiledJet *> jets_for_minheap;
619 jets_for_minheap.reserve(n);
620
621 // now run the recombination loop
622 int history_location = n-1;
623 while (n > 0) {
624
625 double diJ_min = minheap.minval() *_invR2;
626 jetA = head + minheap.minloc();
627
628 // do the recombination between A and B
629 history_location++;
630 jetB = jetA->NN;
631
632 if (jetB != NULL) {
633 // jet-jet recombination
634 // If necessary relabel A & B to ensure jetB < jetA, that way if
635 // the larger of them == newtail then that ends up being jetA and
636 // the new jet that is added as jetB is inserted in a position that
637 // has a future!
638 if (jetA < jetB) {std::swap(jetA,jetB);}
639
640 int nn; // new jet index
641 _cs.plugin_record_ij_recombination(jetA->_jets_index, jetB->_jets_index, diJ_min, nn);
642
643 // what was jetB will now become the new jet
644 _bj_remove_from_tiles(jetA);
645 oldB = * jetB; // take a copy because we will need it...
646 _bj_remove_from_tiles(jetB);
647 _tj_set_jetinfo(jetB, nn); // cause jetB to become _jets[nn]
648 // (also registers the jet in the tiling)
649 } else {
650 // jet-beam recombination
651 // get the hist_index
652 _cs.plugin_record_iB_recombination(jetA->_jets_index, diJ_min);
653 _bj_remove_from_tiles(jetA);
654 }
655
656 // remove the minheap entry for jetA
657 minheap.remove(jetA-head);
658
659 int n_near_tiles = 0;
660
661 // Initialise jetB's NN distance as well as updating it for other
662 // particles. While doing so, examine whether jetA or old jetB was
663 // some other particle's NN.
664 if (jetB != NULL) {
665 Tile2 & jetB_tile = _tiles[jetB->tile_index];
666 for (Tile2 ** near_tile = jetB_tile.begin_tiles;
667 near_tile != jetB_tile.end_tiles; near_tile++) {
668
669 double dist_to_tile = _distance_to_tile(jetB, *near_tile);
670 // use <= in next line so that on first tile, relevant_for_jetB is
671 // set to true
672 bool relevant_for_jetB = dist_to_tile <= jetB->NN_dist;
673 bool relevant_for_near_tile = dist_to_tile <= (*near_tile)->max_NN_dist;
674 bool relevant = relevant_for_jetB || relevant_for_near_tile;
675 if (! relevant) continue;
676 // now label this tile as having been considered (so that we
677 // don't go over it again later)
678 tile_union[n_near_tiles] = *near_tile - & _tiles[0];
679 (*near_tile)->tagged = true;
680 n_near_tiles++;
681
682 // if going over the neighbouring tile's jets, check anyway
683 // whether A or B were nearest neighbours, since it comes at a
684 // modest cost relative to the distance computation (and we would
685 // in most cases have to do it again later anyway).
686 for (TiledJet * 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);
689 }
690 }
691 }
692
693 // first establish the set of tiles over which we are going to
694 // have to run searches for updated and new nearest-neighbours --
695 // basically a combination of vicinity of the tiles of the two old
696 // and one new jet.
697 int n_done_tiles = n_near_tiles;
698 _add_untagged_neighbours_to_tile_union_using_max_info(jetA,
699 tile_union, n_near_tiles);
700 if (jetB != NULL) {
701 _add_untagged_neighbours_to_tile_union_using_max_info(&oldB,
702 tile_union,n_near_tiles);
703 jetB->label_minheap_update_needed();
704 jets_for_minheap.push_back(jetB);
705 }
706
707
708 // first untag the tiles we have already dealt with
709 for (int itile = 0; itile < n_done_tiles; itile++) {
710 _tiles[tile_union[itile]].tagged = false;
711 }
712 // now run over the tiles that were tagged earlier and that we haven't yet
713 // had a change to visit.
714 for (int itile = n_done_tiles; itile < n_near_tiles; itile++) {
715 Tile2 * tile_ptr = &_tiles[tile_union[itile]];
716 tile_ptr->tagged = false;
717 // run over all jets in the current tile
718 for (TiledJet * jetI = tile_ptr->head; jetI != NULL; jetI = jetI->next) {
719 // see if jetI had jetA or jetB as a NN -- if so recalculate the NN
720 if (jetI->NN == jetA || (jetI->NN == jetB && jetB != NULL)) {
721 _set_NN(jetI, jets_for_minheap);
722 }
723 }
724 }
725
726 // deal with jets whose minheap entry needs updating
727 //if (verbose) cout << " jets whose NN was modified: " << endl;
728 while (jets_for_minheap.size() > 0) {
729 TiledJet * jetI = jets_for_minheap.back();
730 jets_for_minheap.pop_back();
731 minheap.update(jetI-head, _bj_diJ(jetI));
732 jetI->label_minheap_update_done();
733 // handle max_NN_dist update for all jets that might have
734 // seen a change (increase) of distance
735 Tile2 & tile_I = _tiles[jetI->tile_index];
736 if (tile_I.max_NN_dist < jetI->NN_dist) tile_I.max_NN_dist = jetI->NN_dist;
737 }
738 n--;
739 }
740
741 // final cleaning up;
742 delete[] briefjets;
743#ifdef INSTRUMENT2
744 cout << "ncall, dtt = " << _ncall << " " << _ncall_dtt << endl; // GPS tmp
745#endif // INSTRUMENT2
746
747}
748
749FASTJET_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.