Fork me on GitHub

source: git/external/fastjet/contribs/Nsubjettiness/AxesFinder.cc@ 44cb6e4

ImprovedOutputFile Timing dual_readout llp
Last change on this file since 44cb6e4 was 9687203, checked in by mselvaggi <mselvaggi@…>, 11 years ago

added nsubjettiness

  • Property mode set to 100644
File size: 12.5 KB
Line 
1// Nsubjettiness Package
2// Questions/Comments? jthaler@jthaler.net
3//
4// Copyright (c) 2011-14
5// Jesse Thaler, Ken Van Tilburg, Christopher K. Vermilion, and TJ Wilkason
6//
7//----------------------------------------------------------------------
8// This file is part of FastJet contrib.
9//
10// It is free software; you can redistribute it and/or modify it under
11// the terms of the GNU General Public License as published by the
12// Free Software Foundation; either version 2 of the License, or (at
13// your option) any later version.
14//
15// It is distributed in the hope that it will be useful, but WITHOUT
16// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
17// or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
18// License for more details.
19//
20// You should have received a copy of the GNU General Public License
21// along with this code. If not, see <http://www.gnu.org/licenses/>.
22//----------------------------------------------------------------------
23
24#include "AxesFinder.hh"
25
26FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
27
28namespace contrib{
29
30///////
31//
32// Functions for minimization.
33//
34///////
35
36// Given starting axes, update to find better axes by using Kmeans clustering around the old axes
37template <int N>
38std::vector<LightLikeAxis> AxesFinderFromOnePassMinimization::UpdateAxesFast(const std::vector <LightLikeAxis> & old_axes,
39 const std::vector <fastjet::PseudoJet> & inputJets) {
40 assert(old_axes.size() == N);
41
42 // some storage, declared static to save allocation/re-allocation costs
43 static LightLikeAxis new_axes[N];
44 static fastjet::PseudoJet new_jets[N];
45 for (int n = 0; n < N; ++n) {
46 new_axes[n].reset(0.0,0.0,0.0,0.0);
47#ifdef FASTJET2
48 new_jets[n].reset(0.0,0.0,0.0,0.0);
49#else
50 // use cheaper reset if available
51 new_jets[n].reset_momentum(0.0,0.0,0.0,0.0);
52#endif
53 }
54
55 double precision = _precision;
56
57 /////////////// Assignment Step //////////////////////////////////////////////////////////
58 std::vector<int> assignment_index(inputJets.size());
59 int k_assign = -1;
60
61 for (unsigned i = 0; i < inputJets.size(); i++){
62 double smallestDist = std::numeric_limits<double>::max(); //large number
63 for (int k = 0; k < N; k++) {
64 double thisDist = old_axes[k].DistanceSq(inputJets[i]);
65 if (thisDist < smallestDist) {
66 smallestDist = thisDist;
67 k_assign = k;
68 }
69 }
70 if (smallestDist > sq(_Rcutoff)) {k_assign = -1;}
71 assignment_index[i] = k_assign;
72 }
73
74 //////////////// Update Step /////////////////////////////////////////////////////////////
75 double distPhi, old_dist;
76 for (unsigned i = 0; i < inputJets.size(); i++) {
77 int old_jet_i = assignment_index[i];
78 if (old_jet_i == -1) {continue;}
79
80 const fastjet::PseudoJet& inputJet_i = inputJets[i];
81 LightLikeAxis& new_axis_i = new_axes[old_jet_i];
82 double inputPhi_i = inputJet_i.phi();
83 double inputRap_i = inputJet_i.rap();
84
85 // optimize pow() call
86 // add noise (the precision term) to make sure we don't divide by zero
87 if (_beta == 1.0) {
88 double DR = std::sqrt(sq(precision) + old_axes[old_jet_i].DistanceSq(inputJet_i));
89 old_dist = 1.0/DR;
90 } else if (_beta == 2.0) {
91 old_dist = 1.0;
92 } else if (_beta == 0.0) {
93 double DRSq = sq(precision) + old_axes[old_jet_i].DistanceSq(inputJet_i);
94 old_dist = 1.0/DRSq;
95 } else {
96 old_dist = sq(precision) + old_axes[old_jet_i].DistanceSq(inputJet_i);
97 old_dist = std::pow(old_dist, (0.5*_beta-1.0));
98 }
99
100 // TODO: Put some of these addition functions into light-like axes
101 // rapidity sum
102 new_axis_i.set_rap(new_axis_i.rap() + inputJet_i.perp() * inputRap_i * old_dist);
103 // phi sum
104 distPhi = inputPhi_i - old_axes[old_jet_i].phi();
105 if (fabs(distPhi) <= M_PI){
106 new_axis_i.set_phi( new_axis_i.phi() + inputJet_i.perp() * inputPhi_i * old_dist );
107 } else if (distPhi > M_PI) {
108 new_axis_i.set_phi( new_axis_i.phi() + inputJet_i.perp() * (-2*M_PI + inputPhi_i) * old_dist );
109 } else if (distPhi < -M_PI) {
110 new_axis_i.set_phi( new_axis_i.phi() + inputJet_i.perp() * (+2*M_PI + inputPhi_i) * old_dist );
111 }
112 // weights sum
113 new_axis_i.set_weight( new_axis_i.weight() + inputJet_i.perp() * old_dist );
114 // momentum magnitude sum
115 new_jets[old_jet_i] += inputJet_i;
116 }
117 // normalize sums
118 for (int k = 0; k < N; k++) {
119 if (new_axes[k].weight() == 0) {
120 // no particles were closest to this axis! Return to old axis instead of (0,0,0,0)
121 new_axes[k] = old_axes[k];
122 } else {
123 new_axes[k].set_rap( new_axes[k].rap() / new_axes[k].weight() );
124 new_axes[k].set_phi( new_axes[k].phi() / new_axes[k].weight() );
125 new_axes[k].set_phi( std::fmod(new_axes[k].phi() + 2*M_PI, 2*M_PI) );
126 new_axes[k].set_mom( std::sqrt(new_jets[k].modp2()) );
127 }
128 }
129 std::vector<LightLikeAxis> new_axes_vec(N);
130 for (unsigned k = 0; k < N; ++k) new_axes_vec[k] = new_axes[k];
131 return new_axes_vec;
132}
133
134// Given N starting axes, this function updates all axes to find N better axes.
135// (This is just a wrapper for the templated version above.)
136std::vector<LightLikeAxis> AxesFinderFromOnePassMinimization::UpdateAxes(const std::vector <LightLikeAxis> & old_axes,
137 const std::vector <fastjet::PseudoJet> & inputJets) {
138 int N = old_axes.size();
139 switch (N) {
140 case 1: return UpdateAxesFast<1>(old_axes, inputJets);
141 case 2: return UpdateAxesFast<2>(old_axes, inputJets);
142 case 3: return UpdateAxesFast<3>(old_axes, inputJets);
143 case 4: return UpdateAxesFast<4>(old_axes, inputJets);
144 case 5: return UpdateAxesFast<5>(old_axes, inputJets);
145 case 6: return UpdateAxesFast<6>(old_axes, inputJets);
146 case 7: return UpdateAxesFast<7>(old_axes, inputJets);
147 case 8: return UpdateAxesFast<8>(old_axes, inputJets);
148 case 9: return UpdateAxesFast<9>(old_axes, inputJets);
149 case 10: return UpdateAxesFast<10>(old_axes, inputJets);
150 case 11: return UpdateAxesFast<11>(old_axes, inputJets);
151 case 12: return UpdateAxesFast<12>(old_axes, inputJets);
152 case 13: return UpdateAxesFast<13>(old_axes, inputJets);
153 case 14: return UpdateAxesFast<14>(old_axes, inputJets);
154 case 15: return UpdateAxesFast<15>(old_axes, inputJets);
155 case 16: return UpdateAxesFast<16>(old_axes, inputJets);
156 case 17: return UpdateAxesFast<17>(old_axes, inputJets);
157 case 18: return UpdateAxesFast<18>(old_axes, inputJets);
158 case 19: return UpdateAxesFast<19>(old_axes, inputJets);
159 case 20: return UpdateAxesFast<20>(old_axes, inputJets);
160 default: std::cout << "N-jettiness is hard-coded to only allow up to 20 jets!" << std::endl;
161 return std::vector<LightLikeAxis>();
162 }
163
164}
165
166// uses minimization of N-jettiness to continually update axes until convergence.
167// The function returns the axes found at the (local) minimum
168std::vector<fastjet::PseudoJet> AxesFinderFromOnePassMinimization::getBetterAxes(int n_jets, const std::vector <fastjet::PseudoJet> & inputJets, const std::vector<fastjet::PseudoJet>& seedAxes) {
169
170 // convert from PseudoJets to LightLikeAxes
171 std::vector< LightLikeAxis > old_axes(n_jets, LightLikeAxis(0,0,0,0));
172 for (int k = 0; k < n_jets; k++) {
173 old_axes[k].set_rap( seedAxes[k].rap() );
174 old_axes[k].set_phi( seedAxes[k].phi() );
175 }
176
177 // Find new axes by iterating (only one pass here)
178 std::vector< LightLikeAxis > new_axes(n_jets, LightLikeAxis(0,0,0,0));
179 double cmp = std::numeric_limits<double>::max(); //large number
180 int h = 0;
181 while (cmp > _precision && h < _halt) { // Keep updating axes until near-convergence or too many update steps
182 cmp = 0.0;
183 h++;
184 new_axes = UpdateAxes(old_axes, inputJets); // Update axes
185 for (int k = 0; k < n_jets; k++) {
186 cmp += old_axes[k].Distance(new_axes[k]);
187 }
188 cmp = cmp / ((double) n_jets);
189 old_axes = new_axes;
190 }
191
192 // Convert from internal LightLikeAxes to PseudoJet
193 std::vector<fastjet::PseudoJet> outputAxes;
194 for (int k = 0; k < n_jets; k++) {
195 fastjet::PseudoJet temp = old_axes[k].ConvertToPseudoJet();
196 outputAxes.push_back(temp);
197 }
198
199 // this is used to debug the minimization routine to make sure that it works.
200 bool do_debug = false;
201 if (do_debug) {
202 // get this information to make sure that minimization is working properly
203 TauComponents seed_tau_components = _measureFunction.result(inputJets, seedAxes);
204 double seed_tau = seed_tau_components.tau();
205 TauComponents tau_components = _measureFunction.result(inputJets, outputAxes);
206 double outputTau = tau_components.tau();
207 assert(outputTau <= seed_tau);
208 }
209
210 return outputAxes;
211}
212
213PseudoJet AxesFinderFromKmeansMinimization::jiggle(const PseudoJet& axis) {
214 double phi_noise = ((double)rand()/(double)RAND_MAX) * _noise_range * 2.0 - _noise_range;
215 double rap_noise = ((double)rand()/(double)RAND_MAX) * _noise_range * 2.0 - _noise_range;
216
217 double new_phi = axis.phi() + phi_noise;
218 if (new_phi >= 2.0*M_PI) new_phi -= 2.0*M_PI;
219 if (new_phi <= -2.0*M_PI) new_phi += 2.0*M_PI;
220
221 PseudoJet newAxis(0,0,0,0);
222 newAxis.reset_PtYPhiM(axis.perp(),axis.rap() + rap_noise,new_phi);
223 return newAxis;
224}
225
226
227// Repeatedly calls the one pass finder to try to find global minimum
228std::vector<fastjet::PseudoJet> AxesFinderFromKmeansMinimization::getBetterAxes(int n_jets, const std::vector <fastjet::PseudoJet> & inputJets, const std::vector<fastjet::PseudoJet>& seedAxes) {
229
230 // first iteration
231 std::vector<fastjet::PseudoJet> bestAxes = _onePassFinder.getAxes(n_jets, inputJets, seedAxes);
232
233 double bestTau = (_measureFunction.result(inputJets,bestAxes)).tau();
234
235 for (int l = 1; l < _n_iterations; l++) { // Do minimization procedure multiple times (l = 1 to start since first iteration is done already)
236
237 // Add noise to current best axes
238 std::vector< PseudoJet > noiseAxes(n_jets, PseudoJet(0,0,0,0));
239 for (int k = 0; k < n_jets; k++) {
240 noiseAxes[k] = jiggle(bestAxes[k]);
241 }
242
243 std::vector<fastjet::PseudoJet> testAxes = _onePassFinder.getAxes(n_jets, inputJets, noiseAxes);
244 double testTau = (_measureFunction.result(inputJets,testAxes)).tau();
245
246 if (testTau < bestTau) {
247 bestTau = testTau;
248 bestAxes = testAxes;
249 }
250 }
251
252 return bestAxes;
253}
254
255// Uses minimization of the geometric distance in order to find the minimum axes.
256// It continually updates until it reaches convergence or it reaches the maximum number of attempts.
257// This is essentially the same as a stable cone finder.
258std::vector<fastjet::PseudoJet> AxesFinderFromGeometricMinimization::getBetterAxes(int n_jets, const std::vector <fastjet::PseudoJet> & particles, const std::vector<fastjet::PseudoJet>& currentAxes) {
259
260 std::vector<fastjet::PseudoJet> seedAxes = currentAxes;
261 double seedTau = _function->tau(particles, seedAxes);
262
263 for (int i = 0; i < _nAttempts; i++) {
264
265 std::vector<fastjet::PseudoJet> newAxes(seedAxes.size(),fastjet::PseudoJet(0,0,0,0));
266
267 // find closest axis and assign to that
268 for (unsigned int i = 0; i < particles.size(); i++) {
269
270 // start from unclustered beam measure
271 int minJ = -1;
272 double minDist = _function->beam_distance_squared(particles[i]);
273
274 // which axis am I closest to?
275 for (unsigned int j = 0; j < seedAxes.size(); j++) {
276 double tempDist = _function->jet_distance_squared(particles[i],seedAxes[j]);
277 if (tempDist < minDist) {
278 minDist = tempDist;
279 minJ = j;
280 }
281 }
282
283 // if not unclustered, then cluster
284 if (minJ != -1) newAxes[minJ] += particles[i];
285 }
286
287 // calculate tau on new axes
288 seedAxes = newAxes;
289 double tempTau = _function->tau(particles, newAxes);
290
291 // close enough to stop?
292 if (fabs(tempTau - seedTau) < _accuracy) break;
293 seedTau = tempTau;
294 }
295
296 return seedAxes;
297}
298
299// Go from internal LightLikeAxis to PseudoJet
300fastjet::PseudoJet LightLikeAxis::ConvertToPseudoJet() {
301 double px, py, pz, E;
302 E = _mom;
303 pz = (std::exp(2.0*_rap) - 1.0) / (std::exp(2.0*_rap) + 1.0) * E;
304 px = std::cos(_phi) * std::sqrt( std::pow(E,2) - std::pow(pz,2) );
305 py = std::sin(_phi) * std::sqrt( std::pow(E,2) - std::pow(pz,2) );
306 return fastjet::PseudoJet(px,py,pz,E);
307}
308
309} //namespace contrib
310
311FASTJET_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.