FastJet  3.0.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
ClusterSequence_Delaunay.cc
1 //STARTHEADER
2 // $Id: ClusterSequence_Delaunay.cc 3114 2013-05-04 08:46:00Z salam $
3 //
4 // Copyright (c) 2005-2011, 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 and are described in hep-ph/0512210. If you use
16 // FastJet as part of work towards a scientific publication, please
17 // include a citation to the FastJet paper.
18 //
19 // FastJet is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 // GNU General Public License for more details.
23 //
24 // You should have received a copy of the GNU General Public License
25 // along with FastJet. If not, see <http://www.gnu.org/licenses/>.
26 //----------------------------------------------------------------------
27 //ENDHEADER
28 
29 
30 #include "fastjet/Error.hh"
31 #include "fastjet/PseudoJet.hh"
32 #include "fastjet/ClusterSequence.hh"
33 #include "fastjet/internal/DynamicNearestNeighbours.hh"
34 #include<iostream>
35 #include<sstream>
36 #include<cmath>
37 #include <cstdlib>
38 #include<cassert>
39 #include<memory>
40 //
41 #ifndef DROP_CGAL // in case we do not have the code for CGAL
42 #include "fastjet/internal/Dnn4piCylinder.hh"
43 #include "fastjet/internal/Dnn3piCylinder.hh"
44 #include "fastjet/internal/Dnn2piCylinder.hh"
45 #endif // DROP_CGAL
46 
47 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
48 
49 using namespace std;
50 
51 
52 //----------------------------------------------------------------------
53 /// Run the clustering using a Hierarchical Delaunay triangulation and
54 /// STL maps to achieve O(N*ln N) behaviour.
55 ///
56 /// There may be internally asserted assumptions about absence of
57 /// points with coincident eta-phi coordinates.
58 void ClusterSequence::_delaunay_cluster () {
59 
60  int n = _jets.size();
61 
62  vector<EtaPhi> points(n); // recall EtaPhi is just a typedef'd pair<double>
63  for (int i = 0; i < n; i++) {
64  points[i] = EtaPhi(_jets[i].rap(),_jets[i].phi_02pi());
65  points[i].sanitize(); // make sure things are in the right range
66  }
67 
68  // initialise our DNN structure with the set of points
69  auto_ptr<DynamicNearestNeighbours> DNN;
70 #ifndef DROP_CGAL // strategy = NlnN* are not supported if we drop CGAL...
71  bool verbose = false;
72  bool ignore_nearest_is_mirror = (_Rparam < twopi);
73  if (_strategy == NlnN4pi) {
74  DNN.reset(new Dnn4piCylinder(points,verbose));
75  } else if (_strategy == NlnN3pi) {
76  DNN.reset(new Dnn3piCylinder(points,ignore_nearest_is_mirror,verbose));
77  } else if (_strategy == NlnN) {
78  DNN.reset(new Dnn2piCylinder(points,ignore_nearest_is_mirror,verbose));
79  } else
80 #else
81  if (_strategy == NlnN4pi || _strategy == NlnN3pi || _strategy == NlnN) {
82  ostringstream err;
83  err << "ERROR: Requested strategy "<<strategy_string()<<" but it is not"<<endl;
84  err << " supported because FastJet was compiled without CGAL"<<endl;
85  throw Error(err.str());
86  //assert(false);
87  }
88 #endif // DROP_CGAL
89  {
90  ostringstream err;
91  err << "ERROR: Unrecognized value for strategy: "<<_strategy<<endl;
92  assert(false);
93  throw Error(err.str());
94  }
95 
96  // We will find nearest neighbour for each vertex, and include
97  // distance in map (NB DistMap is a typedef given in the .h file)
98  DistMap DijMap;
99 
100  // fill the map with the minimal (as far as we know) subset of Dij
101  // distances (i.e. nearest neighbour ones).
102  for (int ii = 0; ii < n; ii++) {
103  _add_ktdistance_to_map(ii, DijMap, DNN.get());
104  }
105 
106  // run the clustering (go up to i=n-1, but then will stop half-way down,
107  // when we reach that point -- it will be the final beam jet and there
108  // will be no nearest neighbours to find).
109  for (int i=0;i<n;i++) {
110  // find nearest vertices
111  // NB: skip cases where the point is not there anymore!
112  TwoVertices SmallestDijPair;
113  int jet_i, jet_j;
114  double SmallestDij;
115  bool Valid2;
116  bool recombine_with_beam;
117  do {
118  SmallestDij = DijMap.begin()->first;
119  SmallestDijPair = DijMap.begin()->second;
120  jet_i = SmallestDijPair.first;
121  jet_j = SmallestDijPair.second;
122  // distance is immediately removed regardless of whether or not
123  // it is used.
124  // Some temporary testing code relating to problems with the gcc-3.2 compiler
125  //cout << "got here and size is "<< DijMap.size()<< " and it is "<<SmallestDij <<"\n";
126  //cout << jet_i << " "<< jet_j<<"\n";
127  DijMap.erase(DijMap.begin());
128  //cout << "got beyond here\n";
129 
130  // need to "prime" the validity of jet_j in such a way that
131  // if it corresponds to the beam then it is automatically valid.
132  recombine_with_beam = (jet_j == BeamJet);
133  if (!recombine_with_beam) {Valid2 = DNN->Valid(jet_j);}
134  else {Valid2 = true;}
135 
136  } while ( !DNN->Valid(jet_i) || !Valid2);
137 
138 
139  // The following part acts just on jet momenta and on the history.
140  // The action on the nearest-neighbour structures takes place
141  // later (only if at least 2 jets are around).
142  if (! recombine_with_beam) {
143  int nn; // will be index of new jet
144  _do_ij_recombination_step(jet_i, jet_j, SmallestDij, nn);
145  //OBS // merge the two jets, add new jet, remove old ones
146  //OBS _jets.push_back(_jets[jet_i] + _jets[jet_j]);
147  //OBS
148  //OBS int nn = _jets.size()-1;
149  //OBS _jets[nn].set_cluster_hist_index(n+i);
150  //OBS
151  //OBS // get corresponding indices in history structure
152  //OBS int hist_i = _jets[jet_i].cluster_hist_index();
153  //OBS int hist_j = _jets[jet_j].cluster_hist_index();
154  //OBS
155  //OBS
156  //OBS _add_step_to_history(n+i,min(hist_i,hist_j), max(hist_i,hist_j),
157  //OBS _jets.size()-1, SmallestDij);
158 
159  // add new point to points vector
160  EtaPhi newpoint(_jets[nn].rap(), _jets[nn].phi_02pi());
161  newpoint.sanitize(); // make sure it is in correct range
162  points.push_back(newpoint);
163  } else {
164  // recombine the jet with the beam
165  _do_iB_recombination_step(jet_i, SmallestDij);
166  //OBS _add_step_to_history(n+i,_jets[jet_i].cluster_hist_index(),BeamJet,
167  //OBS Invalid, SmallestDij);
168  }
169 
170  // exit the loop because we do not want to look for nearest neighbours
171  // etc. of zero partons
172  if (i == n-1) {break;}
173 
174  vector<int> updated_neighbours;
175  if (! recombine_with_beam) {
176  // update DNN
177  int point3;
178  DNN->RemoveCombinedAddCombination(jet_i, jet_j,
179  points[points.size()-1], point3,
180  updated_neighbours);
181  // C++ beginners' comment: static_cast to unsigned int is necessary
182  // to do away with warnings about type mismatch between point3 (int)
183  // and points.size (unsigned int)
184  if (static_cast<unsigned int> (point3) != points.size()-1) {
185  throw Error("INTERNAL ERROR: point3 != points.size()-1");}
186  } else {
187  // update DNN
188  DNN->RemovePoint(jet_i, updated_neighbours);
189  }
190 
191  // update map
192  vector<int>::iterator it = updated_neighbours.begin();
193  for (; it != updated_neighbours.end(); ++it) {
194  int ii = *it;
195  _add_ktdistance_to_map(ii, DijMap, DNN.get());
196  }
197 
198  } // end clustering loop
199 
200 }
201 
202 
203 //----------------------------------------------------------------------
204 /// Add the current kt distance for particle ii to the map (DijMap)
205 /// using information from the DNN object. Work as follows:
206 ///
207 /// . if the kt is zero then it's nearest neighbour is taken to be the
208 /// the beam jet and the distance is zero.
209 ///
210 /// . if cylinder distance to nearest neighbour > _Rparam then it is
211 /// yiB that is smallest and this is added to map.
212 ///
213 /// . otherwise if the nearest neighbour jj has a larger kt then add
214 /// dij to the map.
215 ///
216 /// . otherwise do nothing
217 ///
218 void ClusterSequence::_add_ktdistance_to_map(
219  const int & ii,
220  DistMap & DijMap,
221  const DynamicNearestNeighbours * DNN) {
222 
223  double yiB = jet_scale_for_algorithm(_jets[ii]);
224  if (yiB == 0.0) {
225  // in this case convention is that we do not worry about distances
226  // but directly state that nearest neighbour is beam
227  DijMap.insert(DijEntry(yiB, TwoVertices(ii,-1)));
228  } else {
229  double DeltaR2 = DNN->NearestNeighbourDistance(ii) * _invR2;
230  // Logic of following bit is: only add point to map if it has
231  // smaller kt2 than nearest neighbour j (if it has larger kt,
232  // then: either it is j's nearest neighbour and then we will
233  // include dij when we come to j; or it is not j's nearest
234  // neighbour and j will recombine with someone else).
235 
236  // If DeltaR2 > 1.0 then in any case it will recombine with beam rather
237  // than with any neighbours.
238  // (put general normalisation here at some point)
239  if (DeltaR2 > 1.0) {
240  DijMap.insert(DijEntry(yiB, TwoVertices(ii,-1)));
241  } else {
242  double kt2i = jet_scale_for_algorithm(_jets[ii]);
243  int jj = DNN->NearestNeighbourIndex(ii);
244  if (kt2i <= jet_scale_for_algorithm(_jets[jj])) {
245  double dij = DeltaR2 * kt2i;
246  DijMap.insert(DijEntry(dij, TwoVertices(ii,jj)));
247  }
248  }
249  }
250 }
251 
252 
253 FASTJET_END_NAMESPACE
254