CMS 3D CMS Logo

fastjetfortran_madfks.cc
Go to the documentation of this file.
1 //STARTHEADER
2 //
3 // Copyright (c) 2005-2011, Matteo Cacciari, Gavin P. Salam and Gregory Soyez
4 //
5 //----------------------------------------------------------------------
6 // This file is part of FastJet.
7 //
8 // FastJet is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // The algorithms that underlie FastJet have required considerable
14 // development and are described in hep-ph/0512210. If you use
15 // FastJet as part of work towards a scientific publication, please
16 // include a citation to the FastJet paper.
17 //
18 // FastJet is distributed in the hope that it will be useful,
19 // but WITHOUT ANY WARRANTY; without even the implied warranty of
20 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 // GNU General Public License for more details.
22 //
23 // You should have received a copy of the GNU General Public License
24 // along with FastJet. If not, see <http://www.gnu.org/licenses/>.
25 //----------------------------------------------------------------------
26 //ENDHEADER
27 
28 #include <iostream>
29 #include <memory>
30 
31 #include "fastjet/ClusterSequence.hh"
32 #include "fastjet/ClusterSequenceArea.hh"
33 #include "fastjet/Selector.hh"
34 #include "fastjet/SISConePlugin.hh"
35 
36 using namespace std;
37 using namespace fastjet;
38 
39 FASTJET_BEGIN_NAMESPACE // defined in fastjet/internal/base.hh
40 
43  namespace fwrapper {
44  vector<PseudoJet> input_particles, jets;
45  unique_ptr<JetDefinition::Plugin> plugin;
46  JetDefinition jet_def;
47  unique_ptr<ClusterSequence> cs;
48 
50  void transfer_input_particles(const double *p, const int &npart) {
51  input_particles.resize(0);
52  input_particles.reserve(npart);
53  for (int i = 0; i < npart; i++) {
54  valarray<double> mom(4); // mom[0..3]
55  for (int j = 0; j <= 3; j++) {
56  mom[j] = *(p++);
57  // RF-MZ: reorder the arguments because in madfks energy goes first; in fastjet last.
58  // mom[(j+3) % 4] = *(p++);
59  }
60  PseudoJet psjet(mom);
61  psjet.set_user_index(i);
62  input_particles.push_back(psjet);
63  }
64  }
65 
67  void transfer_jets(double *f77jets, int &njets) {
68  njets = jets.size();
69  for (int i = 0; i < njets; i++) {
70  for (int j = 0; j <= 3; j++) {
71  *f77jets = jets[i][j];
72  // RF-MZ: reorder the arguments because in madfks energy goes first; in fastjet last.
73  //*f77jets = jets[i][(j+3) % 4];
74  f77jets++;
75  }
76  }
77  }
78 
81  void transfer_cluster_transfer(const double *p,
82  const int &npart,
83  const JetDefinition &jet_def,
84  const double &ptmin,
85  double *f77jets,
86  int &njets,
87  int *whichjet,
88  const double &ghost_maxrap = 0.0,
89  const int &nrepeat = 0,
90  const double &ghost_area = 0.0) {
91  // transfer p[4*ipart+0..3] -> input_particles[i]
93 
94  // perform the clustering
95  if (ghost_maxrap == 0.0) {
96  // cluster without areas
97  cs = std::make_unique<ClusterSequence>(input_particles, jet_def);
98  } else {
99  // cluster with areas
100  GhostedAreaSpec area_spec(ghost_maxrap, nrepeat, ghost_area);
101  AreaDefinition area_def(active_area, area_spec);
102  cs = std::make_unique<ClusterSequenceArea>(input_particles, jet_def, area_def);
103  }
104  // extract jets (pt-ordered)
105  jets = sorted_by_pt(cs->inclusive_jets(ptmin));
106 
107  // transfer jets -> f77jets[4*ijet+0..3]
108  transfer_jets(f77jets, njets);
109 
110  // Determine which parton/particle ended-up in which jet
111  // set all jet entrie to zero first
112  for (int ii = 0; ii < npart; ++ii)
113  whichjet[ii] = 0;
114 
115  // Loop over jets and find constituents
116  for (int kk = 0; kk < njets; ++kk) {
117  vector<PseudoJet> constit = cs->constituents(jets[kk]);
118  for (unsigned int ll = 0; ll < constit.size(); ++ll)
119  whichjet[constit[ll].user_index()] = kk + 1;
120  }
121  }
122 }
123 FASTJET_END_NAMESPACE
124 
125 using namespace fastjet::fwrapper;
126 
127 extern "C" {
128 
131 //
132 // Corresponds to the following Fortran subroutine
133 // interface structure:
134 //
135 // SUBROUTINE FASTJETSISCONE(P,NPART,R,F,F77JETS,NJETS,WHICHJET)
136 // DOUBLE PRECISION P(4,*), R, F, F77JETS(4,*)
137 // INTEGER NPART, NJETS, WHICHJET(*)
138 //
139 // where on input
140 //
141 // P the input particle 4-momenta
142 // NPART the number of input momenta
143 // R the radius parameter
144 // F the overlap threshold
145 //
146 // and on output
147 //
148 // F77JETS the output jet momenta (whose second dim should be >= NPART)
149 // sorted in order of decreasing p_t.
150 // NJETS the number of output jets
151 // WHICHJET(i) the jet of parton/particle 'i'
152 //
153 // NOTE: if you are interfacing fastjet to Pythia 6, Pythia stores its
154 // momenta as a matrix of the form P(4000,5), whereas this fortran
155 // interface to fastjet expects them as P(4,NPART), i.e. you must take
156 // the transpose of the Pythia array and drop the fifth component
157 // (particle mass).
158 //
160  const double *p, const int &npart, const double &R, const double &f, double *f77jets, int &njets, int *whichjet) {
161  // prepare jet def
162  plugin = std::make_unique<SISConePlugin>(R, f);
163  jet_def = plugin.get();
164 
165  // do everything
166  // transfer_cluster_transfer(p,npart,jet_def,f77jets,njets,whichjet);
167 }
168 
173 //
174 // Corresponds to the following Fortran subroutine
175 // interface structure:
176 //
177 // SUBROUTINE FASTJETSISCONEWITHAREA(P,NPART,R,F,GHMAXRAP,NREP,GHAREA,F77JETS,NJETS,WHICHJET)
178 // DOUBLE PRECISION P(4,*), R, F, F77JETS(4,*), GHMAXRAP, GHAREA
179 // INTEGER NPART, NJETS, NREP, WHICHJET(*)
180 //
181 // where on input
182 //
183 // P the input particle 4-momenta
184 // NPART the number of input momenta
185 // R the radius parameter
186 // F the overlap threshold
187 // GHMAXRAP the maximum (abs) rapidity covered by ghosts (FastJet default 6.0)
188 // NREP the number of repetitions used to evaluate the area (FastJet default 1)
189 // GHAREA the area of a single ghost (FastJet default 0.01)
190 //
191 // and on output
192 //
193 // F77JETS the output jet momenta (whose second dim should be >= NPART)
194 // sorted in order of decreasing p_t.
195 // NJETS the number of output jets
196 // WHICHJET(i) the jet of parton/particle 'i'
197 //
198 // NOTE: if you are interfacing fastjet to Pythia 6, Pythia stores its
199 // momenta as a matrix of the form P(4000,5), whereas this fortran
200 // interface to fastjet expects them as P(4,NPART), i.e. you must take
201 // the transpose of the Pythia array and drop the fifth component
202 // (particle mass).
203 //
204 void fastjetsisconewitharea_(const double *p,
205  const int &npart,
206  const double &R,
207  const double &f,
208  const double &ghost_rapmax,
209  const int &nrepeat,
210  const double &ghost_area,
211  double *f77jets,
212  int &njets,
213  int *whichjet) {
214  // prepare jet def
215  plugin = std::make_unique<SISConePlugin>(R, f);
216  jet_def = plugin.get();
217 
218  // do everything
219  // transfer_cluster_transfer(p,npart,jet_def,f77jets,njets,whichjet,ghost_rapmax,nrepeat,ghost_area);
220 }
221 
225 //
226 // Corresponds to the following Fortran subroutine
227 // interface structure:
228 //
229 // SUBROUTINE FASTJETPPGENKT(P,NPART,R,PALG,F77JETS,NJETS,WHICHJET)
230 // DOUBLE PRECISION P(4,*), R, PALG, F, F77JETS(4,*)
231 // INTEGER NPART, NJETS, WHICHJET(*)
232 //
233 // where on input
234 //
235 // P the input particle 4-momenta
236 // NPART the number of input momenta
237 // R the radius parameter
238 // PALG the power for the generalised kt alg
239 // (1.0=kt, 0.0=C/A, -1.0 = anti-kt)
240 //
241 // and on output
242 //
243 // F77JETS the output jet momenta (whose second dim should be >= NPART)
244 // sorted in order of decreasing p_t.
245 // NJETS the number of output jets
246 // WHICHJET(i) the jet of parton/particle 'i'
247 //
248 // For the values of PALG that correspond to "standard" cases (1.0=kt,
249 // 0.0=C/A, -1.0 = anti-kt) this routine actually calls the direct
250 // implementation of those algorithms, whereas for other values of
251 // PALG it calls the generalised kt implementation.
252 //
253 // NOTE: if you are interfacing fastjet to Pythia 6, Pythia stores its
254 // momenta as a matrix of the form P(4000,5), whereas this fortran
255 // interface to fastjet expects them as P(4,NPART), i.e. you must take
256 // the transpose of the Pythia array and drop the fifth component
257 // (particle mass).
258 //
259 void fastjetppgenkt_(const double *p,
260  const int &npart,
261  const double &R,
262  const double &ptjetmin,
263  const double &palg,
264  double *f77jets,
265  int &njets,
266  int *whichjet) {
267  // prepare jet def
268  if (palg == 1.0) {
269  jet_def = JetDefinition(kt_algorithm, R);
270  } else if (palg == 0.0) {
271  jet_def = JetDefinition(cambridge_algorithm, R);
272  } else if (palg == -1.0) {
273  jet_def = JetDefinition(antikt_algorithm, R);
274  } else {
275  jet_def = JetDefinition(genkt_algorithm, R, palg);
276  }
277 
278  // do everything
279  transfer_cluster_transfer(p, npart, jet_def, ptjetmin, f77jets, njets, whichjet);
280 }
281 
287 //
288 // Corresponds to the following Fortran subroutine
289 // interface structure:
290 //
291 // SUBROUTINE FASTJETPPGENKTWITHAREA(P,NPART,R,PALG,GHMAXRAP,NREP,GHAREA,F77JETS,NJETS,WHICHJET)
292 // DOUBLE PRECISION P(4,*), R, PALG, GHMAXRAP, GHAREA, F77JETS(4,*)
293 // INTEGER NPART, NREP, NJETS, WHICHJET(*)
294 //
295 // where on input
296 //
297 // P the input particle 4-momenta
298 // NPART the number of input momenta
299 // R the radius parameter
300 // PALG the power for the generalised kt alg
301 // (1.0=kt, 0.0=C/A, -1.0 = anti-kt)
302 // GHMAXRAP the maximum (abs) rapidity covered by ghosts (FastJet default 6.0)
303 // NREP the number of repetitions used to evaluate the area (FastJet default 1)
304 // GHAREA the area of a single ghost (FastJet default 0.01)
305 //
306 // and on output
307 //
308 // F77JETS the output jet momenta (whose second dim should be >= NPART)
309 // sorted in order of decreasing p_t.
310 // NJETS the number of output jets
311 // WHICHJET(i) the jet of parton/particle 'i'
312 //
313 // For the values of PALG that correspond to "standard" cases (1.0=kt,
314 // 0.0=C/A, -1.0 = anti-kt) this routine actually calls the direct
315 // implementation of those algorithms, whereas for other values of
316 // PALG it calls the generalised kt implementation.
317 //
318 // NOTE: if you are interfacing fastjet to Pythia 6, Pythia stores its
319 // momenta as a matrix of the form P(4000,5), whereas this fortran
320 // interface to fastjet expects them as P(4,NPART), i.e. you must take
321 // the transpose of the Pythia array and drop the fifth component
322 // (particle mass).
323 //
324 void fastjetppgenktwitharea_(const double *p,
325  const int &npart,
326  const double &R,
327  const double &palg,
328  const double &ghost_rapmax,
329  const int &nrepeat,
330  const double &ghost_area,
331  double *f77jets,
332  int &njets,
333  int *whichjet) {
334  // prepare jet def
335  if (palg == 1.0) {
336  jet_def = JetDefinition(kt_algorithm, R);
337  } else if (palg == 0.0) {
338  jet_def = JetDefinition(cambridge_algorithm, R);
339  } else if (palg == -1.0) {
340  jet_def = JetDefinition(antikt_algorithm, R);
341  } else {
342  jet_def = JetDefinition(genkt_algorithm, R, palg);
343  }
344 
345  // do everything
346  // transfer_cluster_transfer(p,npart,jet_def,f77jets,njets,whichjet,ghost_rapmax,nrepeat,ghost_area);
347 }
348 
357 //
358 // Corresponds to the following Fortran subroutine
359 // interface structure:
360 //
361 // SUBROUTINE FASTJETCONSTITUENTS(IJET,CONSTITUENT_INDICES,NCONSTITUENTS)
362 // INTEGER IJET
363 // INTEGER CONSTITUENT_INDICES(*)
364 // INTEGER nconstituents
365 //
366 void fastjetconstituents_(const int &ijet, int *constituent_indices, int &nconstituents) {
367  assert(cs.get() != nullptr);
368  assert(ijet > 0 && ijet <= int(jets.size()));
369 
370  vector<PseudoJet> constituents = cs->constituents(jets[ijet - 1]);
371 
372  nconstituents = constituents.size();
373  for (int i = 0; i < nconstituents; i++) {
374  constituent_indices[i] = constituents[i].cluster_hist_index() + 1;
375  }
376 }
377 
384 //
385 // Corresponds to the following Fortran subroutine
386 // interface structure:
387 //
388 // FUNCTION FASTJETAREA(IJET)
389 // DOUBLE PRECISION FASTJETAREA
390 // INTEGER IJET
391 //
392 double fastjetarea_(const int &ijet) {
393  assert(ijet > 0 && ijet <= int(jets.size()));
394  const ClusterSequenceAreaBase *csab = dynamic_cast<const ClusterSequenceAreaBase *>(cs.get());
395  if (csab != nullptr) {
396  // we have areas and can use csab to access all the area-related info
397  return csab->area(jets[ijet - 1]);
398  } else {
399  return 0.;
400  // Error("No area information associated to this jet.");
401  }
402 }
403 
406 //
407 // Corresponds to the following Fortran interface
408 //
409 // FUNCTION FASTJETDMERGE(N)
410 // DOUBLE PRECISION FASTJETDMERGE
411 // INTEGER N
412 //
413 double fastjetdmerge_(const int &n) {
414  assert(cs.get() != nullptr);
415  return cs->exclusive_dmerge(n);
416 }
417 
422 //
423 // Corresponds to the following Fortran interface
424 //
425 // FUNCTION FASTJETDMERGEMAX(N)
426 // DOUBLE PRECISION FASTJETDMERGEMAX
427 // INTEGER N
428 //
429 double fastjetdmergemax_(const int &n) {
430  assert(cs.get() != nullptr);
431  return cs->exclusive_dmerge_max(n);
432 }
433 
438 //
439 // Corresponds to the following Fortran interface
440 //
441 // SUBROUTINE FASTJETGLOBALRHOANDSIGMA(RAPMIN,RAPMAX,PHIMIN,PHIMAX,RHO,SIGMA,MEANAREA)
442 // DOUBLE PRECISION RAPMIN,RAPMAX,PHIMIN,PHIMAX
443 // DOUBLE PRECISION RHO,SIGMA,MEANAREA
444 //
445 void fastjetglobalrhoandsigma_(const double &rapmin,
446  const double &rapmax,
447  const double &phimin,
448  const double &phimax,
449  double &rho,
450  double &sigma,
451  double &meanarea) {
452  const ClusterSequenceAreaBase *csab = dynamic_cast<const ClusterSequenceAreaBase *>(cs.get());
453  if (csab != nullptr) {
454  // we have areas and can use csab to access all the area-related info
455  Selector range = SelectorRapRange(rapmin, rapmax) && SelectorPhiRange(phimin, phimax);
456  bool use_area_4vector = false;
457  csab->get_median_rho_and_sigma(range, use_area_4vector, rho, sigma, meanarea);
458  } else {
459  Error("Clustering with area is necessary in order to be able to evaluate rho.");
460  }
461 }
462 }
fastjetdmergemax_
double fastjetdmergemax_(const int &n)
Definition: fastjetfortran_madfks.cc:429
FastTimerService_cff.range
range
Definition: FastTimerService_cff.py:34
Selector
Functor that operates on <T>
Definition: Selector.h:22
mps_fire.i
i
Definition: mps_fire.py:428
dqmiodumpmetadata.n
n
Definition: dqmiodumpmetadata.py:28
f
double f[11][100]
Definition: MuScleFitUtils.cc:78
fastjet
Definition: BackgroundEstimator.h:8
fwrapper::cs
unique_ptr< ClusterSequence > cs
Definition: fastjetfortran_madfks.cc:47
phimin
float phimin
Definition: ReggeGribovPartonMCHadronizer.h:107
AlCaHLTBitMon_ParallelJobs.p
p
Definition: AlCaHLTBitMon_ParallelJobs.py:153
fastjetdmerge_
double fastjetdmerge_(const int &n)
Definition: fastjetfortran_madfks.cc:413
cms::cuda::assert
assert(be >=bs)
fastjetppgenktwitharea_
void fastjetppgenktwitharea_(const double *p, const int &npart, const double &R, const double &palg, const double &ghost_rapmax, const int &nrepeat, const double &ghost_area, double *f77jets, int &njets, int *whichjet)
Definition: fastjetfortran_madfks.cc:324
fastjetsiscone_
void fastjetsiscone_(const double *p, const int &npart, const double &R, const double &f, double *f77jets, int &njets, int *whichjet)
Definition: fastjetfortran_madfks.cc:159
fastjetglobalrhoandsigma_
void fastjetglobalrhoandsigma_(const double &rapmin, const double &rapmax, const double &phimin, const double &phimax, double &rho, double &sigma, double &meanarea)
Definition: fastjetfortran_madfks.cc:445
fwrapper::input_particles
vector< PseudoJet > input_particles
Definition: fastjetfortran_madfks.cc:44
npart
double npart
Definition: HydjetWrapper.h:46
GetRecoTauVFromDQM_MC_cff.kk
kk
Definition: GetRecoTauVFromDQM_MC_cff.py:84
fwrapper
Definition: fastjetfortran_madfks.cc:43
fastjetppgenkt_
void fastjetppgenkt_(const double *p, const int &npart, const double &R, const double &ptjetmin, const double &palg, double *f77jets, int &njets, int *whichjet)
Definition: fastjetfortran_madfks.cc:259
fwrapper::transfer_jets
void transfer_jets(double *f77jets, int &njets)
helper routine to help transfer jets -> f77jets[4*ijet+0..3]
Definition: fastjetfortran_madfks.cc:67
leef::Error
edm::ErrorSummaryEntry Error
Definition: LogErrorEventFilter.cc:29
fwrapper::jet_def
JetDefinition jet_def
Definition: fastjetfortran_madfks.cc:46
fwrapper::plugin
unique_ptr< JetDefinition::Plugin > plugin
Definition: fastjetfortran_madfks.cc:45
fwrapper::transfer_input_particles
void transfer_input_particles(const double *p, const int &npart)
helper routine to transfer fortran input particles into
Definition: fastjetfortran_madfks.cc:50
phimax
float phimax
Definition: ReggeGribovPartonMCHadronizer.h:106
std
Definition: JetResolutionObject.h:76
BTaggingMonitoring_cff.njets
njets
Definition: BTaggingMonitoring_cff.py:10
fwrapper::transfer_cluster_transfer
void transfer_cluster_transfer(const double *p, const int &npart, const JetDefinition &jet_def, const double &ptmin, double *f77jets, int &njets, int *whichjet, const double &ghost_maxrap=0.0, const int &nrepeat=0, const double &ghost_area=0.0)
Definition: fastjetfortran_madfks.cc:81
ptmin
double ptmin
Definition: HydjetWrapper.h:84
fastjetconstituents_
void fastjetconstituents_(const int &ijet, int *constituent_indices, int &nconstituents)
Definition: fastjetfortran_madfks.cc:366
dqmiolumiharvest.j
j
Definition: dqmiolumiharvest.py:66
dttmaxenums::R
Definition: DTTMax.h:29
cuy.ii
ii
Definition: cuy.py:590
fwrapper::jets
vector< PseudoJet > jets
Definition: fastjetfortran_madfks.cc:44
fastjetarea_
double fastjetarea_(const int &ijet)
Definition: fastjetfortran_madfks.cc:392
fastjetsisconewitharea_
void fastjetsisconewitharea_(const double *p, const int &npart, const double &R, const double &f, const double &ghost_rapmax, const int &nrepeat, const double &ghost_area, double *f77jets, int &njets, int *whichjet)
Definition: fastjetfortran_madfks.cc:204