ECCE @ EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FastJetAlgoSub.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FastJetAlgoSub.cc
1 
2 #include "FastJetAlgoSub.h"
3 
4 #include <g4jets/Jet.h>
5 #include <g4jets/Jetv1.h>
6 
7 // fastjet includes
8 #include <fastjet/ClusterSequence.hh>
9 #include <fastjet/JetDefinition.hh>
10 #include <fastjet/PseudoJet.hh>
11 
12 // standard includes
13 #include <cstddef>
14 #include <iostream>
15 #include <map>
16 #include <memory>
17 #include <utility>
18 #include <vector>
19 
20 using namespace std;
21 
22 FastJetAlgoSub::FastJetAlgoSub(Jet::ALGO algo, float par, float verbosity)
23  : _verbosity(verbosity)
24  , _algo(algo)
25  , _par(par)
26 {
27  fastjet::ClusterSequence clusseq;
28  if (_verbosity > 0)
29  {
30  clusseq.print_banner();
31  }
32  else
33  {
34  ostringstream nullstream;
35  clusseq.set_fastjet_banner_stream(&nullstream);
36  clusseq.print_banner();
37  clusseq.set_fastjet_banner_stream(&cout);
38  }
39 }
40 
41 void FastJetAlgoSub::identify(std::ostream& os)
42 {
43  os << " FastJetAlgoSub: ";
44  if (_algo == Jet::ANTIKT)
45  os << "ANTIKT r=" << _par;
46  else if (_algo == Jet::KT)
47  os << "KT r=" << _par;
48  else if (_algo == Jet::CAMBRIDGE)
49  os << "CAMBRIDGE r=" << _par;
50  os << endl;
51 }
52 
53 std::vector<Jet*> FastJetAlgoSub::get_jets(std::vector<Jet*> particles)
54 {
55  if (_verbosity > 1) cout << "FastJetAlgoSub::process_event -- entered" << endl;
56 
57  // translate to fastjet
58  std::vector<fastjet::PseudoJet> pseudojets;
59  for (unsigned int ipart = 0; ipart < particles.size(); ++ipart)
60  {
61  float this_e = particles[ipart]->get_e();
62 
63  if (this_e == 0.) continue;
64 
65  float this_px = particles[ipart]->get_px();
66  float this_py = particles[ipart]->get_py();
67  float this_pz = particles[ipart]->get_pz();
68 
69  if (this_e < 0)
70  {
71  // make energy = +1 MeV for purposes of clustering
72  float e_ratio = 0.001 / this_e;
73 
74  this_e = this_e * e_ratio;
75  this_px = this_px * e_ratio;
76  this_py = this_py * e_ratio;
77  this_pz = this_pz * e_ratio;
78 
79  if (_verbosity > 5)
80  {
81  std::cout << " FastJetAlgoSub input particle with negative-E, original kinematics px / py / pz / E = ";
82  std::cout << particles[ipart]->get_px() << " / " << particles[ipart]->get_py() << " / " << particles[ipart]->get_pz() << " / " << particles[ipart]->get_e() << std::endl;
83  std::cout << " --> entering with modified kinematics px / py / pz / E = " << this_px << " / " << this_py << " / " << this_pz << " / " << this_e << std::endl;
84  }
85  }
86 
87  fastjet::PseudoJet pseudojet(this_px, this_py, this_pz, this_e);
88 
89  pseudojet.set_user_index(ipart);
90  pseudojets.push_back(pseudojet);
91  }
92 
93  // run fast jet
94  fastjet::JetDefinition* jetdef = NULL;
95  if (_algo == Jet::ANTIKT)
96  jetdef = new fastjet::JetDefinition(fastjet::antikt_algorithm, _par, fastjet::E_scheme, fastjet::Best);
97  else if (_algo == Jet::KT)
98  jetdef = new fastjet::JetDefinition(fastjet::kt_algorithm, _par, fastjet::E_scheme, fastjet::Best);
99  else if (_algo == Jet::CAMBRIDGE)
100  jetdef = new fastjet::JetDefinition(fastjet::cambridge_algorithm, _par, fastjet::E_scheme, fastjet::Best);
101  else
102  return std::vector<Jet*>();
103  fastjet::ClusterSequence jetFinder(pseudojets, *jetdef);
104  std::vector<fastjet::PseudoJet> fastjets = jetFinder.inclusive_jets();
105  delete jetdef;
106 
107  // translate into jet output...
108  std::vector<Jet*> jets;
109  for (unsigned int ijet = 0; ijet < fastjets.size(); ++ijet)
110  {
111  Jet* jet = new Jetv1();
112 
113  if (_verbosity > 5 && fastjets[ijet].perp() > 15)
114  {
115  std::cout << " FastJetAlgoSub : jet # " << ijet << " comes out of clustering with pt / eta / phi = " << fastjets[ijet].perp() << " / " << fastjets[ijet].eta() << " / " << fastjets[ijet].phi();
116  std::cout << ", px / py / pz / e = " << fastjets[ijet].px() << " / " << fastjets[ijet].py() << " / " << fastjets[ijet].pz() << " / " << fastjets[ijet].e() << std::endl;
117  }
118 
119  float total_px = 0;
120  float total_py = 0;
121  float total_pz = 0;
122  float total_e = 0;
123 
124  // copy components into output jet
125  std::vector<fastjet::PseudoJet> comps = fastjets[ijet].constituents();
126  for (unsigned int icomp = 0; icomp < comps.size(); ++icomp)
127  {
128  Jet* particle = particles[comps[icomp].user_index()];
129 
130  total_px += particle->get_px();
131  total_py += particle->get_py();
132  total_pz += particle->get_pz();
133  total_e += particle->get_e();
134 
135  for (Jet::Iter iter = particle->begin_comp();
136  iter != particle->end_comp();
137  ++iter)
138  {
139  jet->insert_comp(iter->first, iter->second);
140  }
141  }
142 
143  jet->set_px(total_px);
144  jet->set_py(total_py);
145  jet->set_pz(total_pz);
146  jet->set_e(total_e);
147  jet->set_id(ijet);
148 
149  if (_verbosity > 5 && fastjets[ijet].perp() > 15)
150  {
151  std::cout << " FastJetAlgoSub : jet # " << ijet << " after correcting for proper constituent kinematics, pt / eta / phi = " << jet->get_pt() << " / " << jet->get_eta() << " / " << jet->get_phi();
152  std::cout << ", px / py / pz / e = " << jet->get_px() << " / " << jet->get_py() << " / " << jet->get_pz() << " / " << jet->get_e() << std::endl;
153  }
154 
155  jets.push_back(jet);
156  }
157 
158  if (_verbosity > 1) cout << "FastJetAlgoSub::process_event -- exited" << endl;
159 
160  return jets;
161 }