Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RhoFluct.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file RhoFluct.cc
1 #include "RhoFluct.h"
2 #include "fastjet/AreaDefinition.hh"
3 #include "fastjet/ClusterSequenceArea.hh"
4 #include "fastjet/Selector.hh"
5 #include "fastjet/tools/BackgroundEstimatorBase.hh"
6 #include "fastjet/tools/JetMedianBackgroundEstimator.hh"
7 
8 
9 #include <TFile.h>
10 #include <TH1F.h>
11 #include <TH2F.h>
12 #include <TRandom3.h>
13 #include <TString.h>
14 #include <TTree.h>
15 #include <TH2D.h>
16 #include <TVector3.h>
17 #include <algorithm>
18 #include <cassert>
20 #include <cmath>
21 #include <fastjet/ClusterSequence.hh>
22 #include <fastjet/JetDefinition.hh>
23 #include <fastjet/PseudoJet.hh>
25 #include <fun4all/Fun4AllServer.h>
26 #include <fun4all/PHTFileServer.h>
27 #include <g4eval/JetEvalStack.h>
28 #include <jetbase/JetInput.h>
29 #include <jetbase/JetMapv1.h>
30 #include <iostream>
31 #include <limits>
32 #include <phool/PHCompositeNode.h>
33 #include <phool/getClass.h>
34 #include <stdexcept>
37 
38 // using namespace fastjet;
39 
41  ( const std::string& _outputfilename
42  , const float _jet_R
43  )
44  : SubsysReco("RhoFluct")
45  , m_outputfilename { _outputfilename }
46  , jet_R { _jet_R }
47 { }
48 
50 {
51  for (unsigned int i = 0; i < m_input_Sub1.size(); ++i) delete m_input_Sub1[i];
52  m_input_Sub1.clear();
53 
54  for (unsigned int i = 0; i < m_input_rhoA.size(); ++i) delete m_input_rhoA[i];
55  m_input_rhoA.clear();
56 }
57 
58 int RhoFluct::Init(PHCompositeNode* topNode)
59 {
61  std::cout << "RhoFluct::Init - Outoput to " << m_outputfilename << std::endl;
62 
63  std::cout << " File out: " << m_outputfilename << std::endl;
65 
66  //Tree
67  m_T = new TTree("T", "RhoFluct Tree");
68 
69  // int m_event;
70  /* m_T->Branch("id", &m_id); */
71  m_T->Branch("rho", &m_rho);
72  m_T->Branch("rho_sigma", &m_rho_sigma);
73  m_T->Branch("cent", &m_cent);
74  m_T->Branch("cent_mdb", &m_cent_mdb);
75  m_T->Branch("cent_epd", &m_cent_epd);
76  m_T->Branch("impactparam", &m_impactparam);
77 
78 
79  // embedded objects
80  m_T->Branch("emb_1TeV_phi", &m_1TeV_phi);
81  m_T->Branch("emb_1TeV_eta", &m_1TeV_eta);
82 
83  //Sub1 Jets
84  m_T->Branch("sub1_ismatched", &m_Sub1_ismatched);
85  m_T->Branch("sub1JetPhi", &m_Sub1JetPhi);
86  m_T->Branch("sub1JetEta", &m_Sub1JetEta);
87  m_T->Branch("sub1JetPt", &m_Sub1JetPt);
88  m_T->Branch("sub1Jet_delPt", &m_Sub1Jet_delpt);
89 
90  //rhoA jets
91  m_T->Branch("rhoA_ismatched", &m_rhoA_ismatched);
92  m_T->Branch("rhoAJetPhi", &m_rhoAJetPhi);
93  m_T->Branch("rhoAJetEta", &m_rhoAJetEta);
94  m_T->Branch("rhoAJetPt", &m_rhoAJetPt);
95  m_T->Branch("rhoAJetArea", &m_rhoAJetArea);
96  m_T->Branch("rhoAJetPtLessRhoA", &m_rhoAJetPtLessRhoA);
97  m_T->Branch("rhoAJet_delPt", &m_rhoAJet_delpt);
98 
100 }
101 
102 int RhoFluct::End(PHCompositeNode* topNode)
103 {
104  std::cout << "RhoFluct::End - Output to " << m_outputfilename << std::endl;
106 
107  m_T->Write();
109 }
110 
112 {
113  topNode->print();
114  std::cout << " Input Selections:" << std::endl;
115  for (unsigned int i = 0; i < m_input_rhoA.size(); ++i) m_input_rhoA[i]->identify();
116  for (unsigned int i = 0; i < m_input_Sub1.size(); ++i) m_input_Sub1[i]->identify();
118 }
119 
121 {
122  ++nevent;
123 
124  // cut for only event with good vertices
125  GlobalVertexMap *mapVtx = findNode::getClass<GlobalVertexMap>(topNode, "GlobalVertexMap");
126  if (mapVtx->size() == 0) return Fun4AllReturnCodes::EVENT_OK;
127 
128  // centrality
129  CentralityInfo* cent_node = findNode::getClass<CentralityInfo>(topNode, "CentralityInfo");
130  if (cent_node)
131  {
132  m_cent_epd = cent_node->get_centile(CentralityInfo::PROP::epd_NS);
133  m_cent_mdb = cent_node->get_centile(CentralityInfo::PROP::mbd_NS);
134  m_impactparam = cent_node->get_quantity(CentralityInfo::PROP::bimp);
135  m_cent = cent_node->get_centile(CentralityInfo::PROP::bimp);
136  }
137 
138  const double ghost_max_rap = 1.1;
139  const double ghost_R = 0.01;
140  const double max_jet_rap = 1.1; // - jet_R;
141 
142  //get the SUB1 jets
143  m_1TeV_phi = rand3.Uniform(-M_PI, M_PI);
144  m_1TeV_eta = rand3.Uniform(-0.7, 0.7);
145 
146  const int TOW_PRINT_INT = 8000;
147 
148  // build the towers for rhoA
149  int n_inputs {0};
150  std::vector<fastjet::PseudoJet> calo_pseudojets;
151  for (auto inp : m_input_rhoA) {
152  if (Verbosity()>TOW_PRINT_INT) std::cout << " Input Tower Set("<<n_inputs++<<"): ";
153  float sum_pt {0.};
154  float n_towers {0};
155  std::vector<Jet *> particles = inp->get_input(topNode);
156  for (unsigned int i = 0; i < particles.size(); ++i)
157  {
158  auto& part = particles[i];
159  float this_e = part->get_e();
160  if (this_e == 0.) continue;
161  float this_px = particles[i]->get_px();
162  float this_py = particles[i]->get_py();
163  float this_pz = particles[i]->get_pz();
164  if (this_e < 0)
165  {
166  // make energy = +1 MeV for purposes of clustering
167  float e_ratio = 0.001 / this_e;
168  this_e = this_e * e_ratio;
169  this_px = this_px * e_ratio;
170  this_py = this_py * e_ratio;
171  this_pz = this_pz * e_ratio;
172  }
173  fastjet::PseudoJet pseudojet(this_px, this_py, this_pz, this_e);
174  /* if (pseudojet.pt() < 0.2) continue; */
175  calo_pseudojets .push_back(pseudojet);
176  if (Verbosity() > TOW_PRINT_INT) {
177  sum_pt += pseudojet.perp();
178  ++n_towers;
179  }
180  }
181  if (Verbosity()>TOW_PRINT_INT) std::cout << " sumpt(" << sum_pt <<") from ntowers("<<n_towers <<")" << std::endl;
182  for (auto &p : particles) delete p;
183  }
184  // add the TeV particle
185  auto embjet = fastjet::PseudoJet();
186  embjet.reset_PtYPhiM(30., m_1TeV_eta, m_1TeV_phi);
187  calo_pseudojets.push_back(embjet);
188 
189  fastjet::Selector jetrap = fastjet::SelectorAbsEtaMax(max_jet_rap);
190  fastjet::Selector not_pure_ghost = !fastjet::SelectorIsPureGhost();
191 
192  fastjet::Selector selection = jetrap && not_pure_ghost;
193  fastjet::AreaDefinition area_def(
194  fastjet::active_area_explicit_ghosts, fastjet::GhostedAreaSpec(ghost_max_rap, 1, ghost_R));
195 
196  fastjet::JetDefinition jet_def_antikt(fastjet::antikt_algorithm, jet_R);
197 
198 
199  fastjet::JetDefinition jet_def_bkgd(fastjet::kt_algorithm, jet_R); // <--
200  fastjet::Selector selector_rm2 = jetrap * (!fastjet::SelectorNHardest(2)); // <--
201  //
202  fastjet::JetMedianBackgroundEstimator bge_rm2 {selector_rm2, jet_def_bkgd, area_def};
203  bge_rm2.set_particles(calo_pseudojets);
204 
205  m_rho = bge_rm2.rho();
206  m_rho_sigma = bge_rm2.sigma();
207 
208  mBGE_mean_area = bge_rm2 .mean_area();
209  mBGE_empty_area = bge_rm2 .empty_area();
210  mBGE_n_empty_jets = bge_rm2 .n_empty_jets();
211  mBGE_n_jets_used = bge_rm2 .n_jets_used();
212 
213  fastjet::ClusterSequenceArea clustSeq(calo_pseudojets, jet_def_antikt, area_def);
214  std::vector<fastjet::PseudoJet> jets
215  = sorted_by_pt( selection( clustSeq.inclusive_jets(5.) ));
216 
217  int fixmek = 0;
218  if (Verbosity()>100 && m_cent_mdb<10) {
219  fixmek = 0;
220  std::cout << " --- rhoA jets [set("<<outevent<<")]" << std::endl;
221  for (auto jet : jets) {
222  if (jet.perp()-m_rho*jet.area() > 5.) {
223  auto phi = jet.phi();
224  while (phi > M_PI) phi -= 2*M_PI;
225  std::cout << Form("k[%i] pt:phi:eta[%5.2f:%5.2f:%5.2f]", fixmek++, (jet.perp()-m_rho*jet.area()), phi, jet.eta()) << std::endl;
226  }
227  }
228  }
229 
230  // check that the leading jet is within R=0.4 of the embedded jet
231  if (jets.size() == 0) {
232  std::cout << " no jets reconstructed; something is very wrong" << std::endl;
233  m_rhoA_ismatched = false;
234  m_rhoAJetPhi = -100.;
235  m_rhoAJetEta = -100.;
236  m_rhoAJetPt = -100.;
237  m_rhoAJetArea = -100.;
238  m_rhoAJetPtLessRhoA = -100.;
239  m_rhoAJet_delpt = -100.;
240  } else {
241  auto& lead_jet = jets[0];
242  m_rhoAJetPhi = lead_jet.phi();
243  m_rhoAJetEta = lead_jet.eta();
244  m_rhoAJetPt = lead_jet.perp();
245  m_rhoAJetArea = lead_jet.area();
247 
248  float dphi = TMath::Abs(lead_jet.phi_std() - m_1TeV_phi);
249  while (dphi>M_PI) dphi = TMath::Abs(dphi-2*M_PI);
250  float dR = TMath::Sq(dphi)+TMath::Sq(lead_jet.eta()-m_1TeV_eta);
251  if (dR > 0.16) {
252  m_rhoA_ismatched = false;
253  m_rhoAJet_delpt = -100.;
254  } else {
255  m_rhoA_ismatched = true;
257  }
258  }
259 
260  // --------------------------------------
261  // now make the jets with Sub1
262  // --------------------------------------
263  std::vector<fastjet::PseudoJet> sub1_pseudojets;
264  for (auto inp : m_input_Sub1) {
265  if (Verbosity()>TOW_PRINT_INT) std::cout << " Input Tower Set("<<n_inputs++<<"): ";
266  float sum_pt {0.};
267  float n_towers {0};
268  std::vector<Jet *> particles = inp->get_input(topNode);
269  for (unsigned int i = 0; i < particles.size(); ++i)
270  {
271  auto& part = particles[i];
272  float this_e = part->get_e();
273  if (this_e == 0.) continue;
274  float this_px = particles[i]->get_px();
275  float this_py = particles[i]->get_py();
276  float this_pz = particles[i]->get_pz();
277  if (this_e < 0)
278  {
279  // make energy = +1 MeV for purposes of clustering
280  float e_ratio = 0.001 / this_e;
281  this_e = this_e * e_ratio;
282  this_px = this_px * e_ratio;
283  this_py = this_py * e_ratio;
284  this_pz = this_pz * e_ratio;
285  }
286  fastjet::PseudoJet pseudojet(this_px, this_py, this_pz, this_e);
287  /* if (pseudojet.pt() < 0.2) continue; */
288  sub1_pseudojets .push_back(pseudojet);
289  if (Verbosity() > TOW_PRINT_INT) {
290  sum_pt += pseudojet.perp();
291  ++n_towers;
292  }
293  }
294  if (Verbosity()>TOW_PRINT_INT) std::cout << " sumpt(" << sum_pt <<") from ntowers("<<n_towers <<")" << std::endl;
295  for (auto &p : particles) delete p;
296  }
297  // add the TeV particle
298  sub1_pseudojets.push_back(embjet);
299 
300  fastjet::ClusterSequenceArea clustSeq_sub1(sub1_pseudojets, jet_def_antikt, area_def);
301  std::vector<fastjet::PseudoJet> jets_sub1
302  = sorted_by_pt( selection( clustSeq_sub1.inclusive_jets(5.) ));
303 
304  // check that the leading jet is within R=0.4 of the embedded jet
305  if (jets_sub1.size() == 0) {
306  std::cout << " no jets_sub1 reconstructed; something is very wrong" << std::endl;
307  m_Sub1_ismatched = false;
308  m_Sub1JetPhi = -100.;
309  m_Sub1JetEta = -100.;
310  m_Sub1JetPt = -100.;
311  m_Sub1Jet_delpt = -100.;
312  } else {
313  auto& lead_jet = jets_sub1[0];
314  m_Sub1JetPhi = lead_jet.phi();
315  m_Sub1JetEta = lead_jet.eta();
316  m_Sub1JetPt = lead_jet.perp();
317 
318  float dphi = TMath::Abs(lead_jet.phi_std() - m_1TeV_phi);
319  while (dphi>M_PI) dphi = TMath::Abs(dphi-2*M_PI);
320  float dR = TMath::Sq(dphi)+TMath::Sq(lead_jet.eta()-m_1TeV_eta);
321  if (dR > 0.16) {
322  m_Sub1_ismatched = false;
323  m_Sub1Jet_delpt = -100.;
324  } else {
325  m_Sub1_ismatched = true;
327  }
328  }
329 
330  m_T->Fill();
332 }
333