Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
PartonShower.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file PartonShower.cc
1 /*******************************************************************************
2  * Copyright (c) The JETSCAPE Collaboration, 2018
3  *
4  * Modular, task-based framework for simulating all aspects of heavy-ion collisions
5  *
6  * For the list of contributors see AUTHORS.
7  *
8  * Report issues at https://github.com/JETSCAPE/JETSCAPE/issues
9  *
10  * or via email to bugs.jetscape@gmail.com
11  *
12  * Distributed under the GNU General Public License 3.0 (GPLv3 or later).
13  * See COPYING for details.
14  ******************************************************************************/
15 
16 #include "PartonShower.h"
17 #include <sstream>
18 #include <fstream>
19 #include <iomanip>
20 #include "MakeUniqueHelper.h"
21 
22 using std::setprecision;
23 using std::fixed;
24 using std::to_string;
25 using std::ostringstream;
26 using std::stringstream;
27 
28 namespace Jetscape {
29 
31 
32 node PartonShower::new_vertex(shared_ptr<Vertex> v) {
34  vMap[n] = v;
35  return n;
36 }
37 
39  edge e = graph::new_edge(s, t);
40  pMap[e] = p;
41  return e.id();
42 }
43 
44 /*
45 void PartonShower::FillPartonVec()
46 {
47  VERBOSE(8);
48 
49  edge_iterator eIt, eEnd;
50 
51  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt)
52  pVec.push_back(pMap[*eIt]);
53 }
54 
55 void PartonShower::FillVertexVec()
56 {
57  VERBOSE(8);
58 
59  node_iterator nIt, nEnd;
60 
61  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt)
62  vVec.push_back(vMap[*nIt]);
63 }
64 */
65 
66 /*
67 shared_ptr<Parton> PartonShower::GetPartonAt(int i)
68 {
69  if (pVec.size()==0)
70  FillPartonVec();
71 
72  return pVec[i].lock();
73 }
74 
75 shared_ptr<Vertex> PartonShower::GetVertexAt(int i)
76 {
77  if (vVec.size()==0)
78  FillVertexVec();
79 
80  return vVec[i].lock();
81 }
82 */
83 
84 /*
85 unique_ptr<Parton> PartonShower::GetPartonAt(int i)
86 {
87  if (pVec.size()==0)
88  FillPartonVec();
89 
90  return make_unique<Parton>(*(pVec[i].lock()));
91 }
92 
93 unique_ptr<Vertex> PartonShower::GetVertexAt(int i)
94 {
95  if (vVec.size()==0)
96  FillVertexVec();
97 
98  return make_unique<Vertex>(*(vVec[i].lock()));
99 }
100 */
101 /*
102 void PartonShower::CreateMaps()
103 {
104  VERBOSESHOWER(8);
105  edge_iterator eIt, eEnd;
106  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt)
107  {
108  pToEdgeMap[pMap[*eIt]]=*eIt;
109  }
110 
111  node_iterator nIt, nEnd;
112  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt)
113  {
114  //vToNodeMap[vMap[*nIt]]=*nIt;
115  }
116 }
117 */
118 
119 vector<shared_ptr<Parton>> PartonShower::GetFinalPartons() {
120  //VERBOSESHOWER(8)<<pFinal.size();
121  if (pFinal.size() == 0) {
122  edge_iterator eIt, eEnd;
123  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt) {
124  if (eIt->target().outdeg() < 1) {
125  if (pMap[*eIt]->pstat() > -10) {
126  pFinal.push_back(pMap[*eIt]);
127  }
128  // DEBUG
129  //cout<<eIt->target()<<endl;
130  }
131  }
132  return pFinal;
133  } else {
134  return pFinal;
135  }
136 }
137 
138 vector<fjcore::PseudoJet> PartonShower::GetFinalPartonsForFastJet() {
139  vector<shared_ptr<Parton>> mP =
140  GetFinalPartons(); // to ensure that pFinal is filled
141 
142  vector<fjcore::PseudoJet> forFJ;
143 
144  for (int i = 0; i < mP.size(); i++)
145  forFJ.push_back((*mP[i]).GetPseudoJet());
146 
147  return forFJ;
148 }
149 
151  return GetEdgeAt(n).source().indeg();
152 }
153 
155  return GetEdgeAt(n).target().outdeg();
156 }
157 
159  edge_iterator eIt;
160  eIt = edges_begin();
161  advance(eIt, n);
162  return *eIt;
163 }
164 
166  node_iterator nIt;
167  nIt = nodes_begin();
168  advance(nIt, n);
169  return *nIt;
170 }
171 
173  edge_iterator eIt;
174  eIt = edges_begin();
175  advance(eIt, n);
176  return GetParton(*eIt);
177 }
178 
179 shared_ptr<Vertex> PartonShower::GetVertexAt(int n) {
180  node_iterator nIt;
181  nIt = nodes_begin();
182  advance(nIt, n);
183  return GetVertex(*nIt);
184 }
185 
187  VERBOSESHOWER(8);
188  pFinal.clear(); //pVec.clear();vVec.clear();
189 }
190 
191 void PartonShower::save_node_info_handler(ostream *o, node n) const {
192  *o << "label "
193  << "\"" << n.id() << "(" << fixed << setprecision(2) << vMap[n]->x_in().t()
194  << ")\"" << endl;
195  *o << "x " << vMap[n]->x_in().x() << endl;
196  *o << "y " << vMap[n]->x_in().y() << endl;
197  *o << "z " << vMap[n]->x_in().z() << endl;
198  *o << "t " << vMap[n]->x_in().t() << endl;
199 }
200 
201 void PartonShower::save_edge_info_handler(ostream *o, edge e) const {
202  *o << "label "
203  << "\"(" << fixed << setprecision(2) << pMap[e]->pt() << ")\"" << endl;
204  *o << "plabel " << pMap[e]->plabel() << endl;
205  *o << "pid " << pMap[e]->pid() << endl;
206  *o << "pstat " << pMap[e]->pstat() << endl;
207  *o << "pT " << pMap[e]->pt() << endl;
208  *o << "eta " << pMap[e]->eta() << endl;
209  *o << "phi " << pMap[e]->phi() << endl;
210  *o << "E " << pMap[e]->e() << endl;
211 }
212 
214  VERBOSESHOWER(8);
215  edge_iterator eIt, eEnd;
216  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt) {
217  pMap[*eIt] = nullptr;
218  }
219 
220  node_iterator nIt, nEnd;
221  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt) {
222  vMap[*nIt] = nullptr;
223  }
224 }
225 
227  node_iterator nIt, nEnd;
228  ostringstream os;
229 
230  if (verbose && JetScapeLogger::Instance()->GetVerboseLevel() > 8) {
231  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt)
232  os << *nIt << "=" << vMap[*nIt]->x_in().t() << " ";
233  VERBOSESHOWER(8) << os.str();
234  }
235 
236  if (!verbose) {
237  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt)
238  os << *nIt << "=" << vMap[*nIt]->x_in().t() << " ";
239  cout << "Vertex list : " << os.str() << endl;
240  }
241 }
242 
244  edge_iterator eIt, eEnd;
245  ostringstream os;
246 
247  if (verbose && JetScapeLogger::Instance()->GetVerboseLevel() > 8) {
248  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt)
249  os << *eIt << "=" << pMap[*eIt]->pt() << " ";
250  VERBOSESHOWER(8) << os.str();
251  }
252 
253  if (!verbose) {
254  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt)
255  os << *eIt << "=" << pMap[*eIt]->pt() << " ";
256  cout << "Parton list : " << os.str() << endl;
257  }
258 }
259 
260 // To be extended to store all infos like this in GML
261 // or store vectore of partons and vertecies with relevant graph infos
262 // to construct the graph later .... (TBD)
263 // These handlers are needed if one wants to use load and GML files ...
264 // Obsolete with new JetScape reader ...
265 
267  VERBOSESHOWER(8) << "Load edge ... " << e;
268  struct GML_pair *tmp = read;
269 
270  double pT, eta, phi, E;
271  int plabel, pid, pstat;
272 
273  while (tmp) {
274  //printf ("*KEY* : %s \n", tmp->key);
275  if (((string)(tmp->key)).find("pT") < 1)
276  pT = tmp->value.floating;
277  if (((string)(tmp->key)).find("eta") < 1)
278  eta = tmp->value.floating;
279  if (((string)(tmp->key)).find("phi") < 1)
280  phi = tmp->value.floating;
281  if (((string)(tmp->key)).find("E") < 1)
282  E = tmp->value.floating;
283  if (((string)(tmp->key)).find("plabel") < 1)
284  plabel = tmp->value.integer;
285  if (((string)(tmp->key)).find("pid") < 1)
286  pid = tmp->value.integer;
287  if (((string)(tmp->key)).find("pstat") < 1)
288  pstat = tmp->value.integer;
289 
290  tmp = tmp->next;
291  }
292 
293  pMap[e] = make_shared<Parton>(plabel, pid, pstat, pT, eta, phi, E);
294 }
295 
297  VERBOSESHOWER(8) << "Load node ... " << n;
298  struct GML_pair *tmp = read;
299 
300  double x = 0;
301  double y = 0;
302  double z = 0;
303  double t = 0;
304 
305  while (tmp) {
306  //printf ("*KEY* : %s %f \n", tmp->key, tmp->value.floating);
307  if (((string)(tmp->key)).find("x") < 1)
308  x = tmp->value.floating;
309  if (((string)(tmp->key)).find("y") < 1)
310  y = tmp->value.floating;
311  if (((string)(tmp->key)).find("z") < 1)
312  z = tmp->value.floating;
313  if (((string)(tmp->key)).find("t") < 1)
314  t = tmp->value.floating;
315 
316  tmp = tmp->next;
317  }
318 
319  vMap[n] = make_shared<Vertex>(x, y, z, t);
320 }
321 
322 // use with graphviz (on Mac: brew install graphviz --with-app)
323 // dot GVfile.gv -Tpdf -o outputPDF.pdf
324 
325 void PartonShower::SaveAsGV(string fName) {
326  ofstream gv;
327  gv.open(fName.c_str());
328 
329  // Simple directed graph left->right in dot/gv format for usage with graphviz ...
330  // nodes show (time) and arrows (pT)
331  gv << "digraph \"graph\" {" << endl;
332  gv << endl;
333  gv << "rankdir=\"LR\";" << endl;
334  gv << "node [shape=plaintext, fontsize=11];"
335  << endl; //, shape=circle]; //plaintext];
336  gv << "edge [fontsize=10];" << endl;
337  gv << endl;
338  //gv<<"0 -> 1"<<endl;
339  node_iterator nIt, nEnd;
340 
341  int n = 0;
342  string label;
343  string label2;
344 
345  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt) {
346  label = ("[label=\"" + to_string(n) + "(");
347 
348  label2 = ")\"];";
349  stringstream stream;
350 
351  stream << fixed << setprecision(2) << (vMap[*nIt]->x_in().t());
352  gv << n << " " << label << stream.str() << label2 << endl;
353 
354  n++;
355  }
356 
357  gv << endl;
358 
359  edge_iterator eIt, eEnd;
360 
361  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt) {
362  //label = ("[label=\"(");
363  if ((pMap[*eIt]->pstat()) == -13) {
364  // missing energy-momentum
365  label = ("[style=\"dotted\" color=\"red\"label=\"(");
366  } else if ((pMap[*eIt]->pstat()) == -11) {
367  // liquefied partons
368  label = ("[color=\"red\"label=\"(");
369  } else if ((pMap[*eIt]->pstat()) == -17) {
370  // thermal partons draw from the medium (negative partons)
371  label = ("[color=\"darkorange\"label=\"(");
372  } else if ((pMap[*eIt]->pstat()) == -1) {
373  // thermal partons draw from the medium (negative partons)
374  label = ("[color=\"green\"label=\"(");
375  } else if ((pMap[*eIt]->t()) < 4.0) {
376  // small virtuality parton
377  label = ("[color=\"blue\"label=\"(");
378  } else {
379  label = ("[label=\"(");
380  }
381  label2 = ")\"];";
382  stringstream stream;
383  if ((pMap[*eIt]->pstat()) == -13) {
384  stream << std::scientific << setprecision(1) << (pMap[*eIt]->e()) << ","
385  << (pMap[*eIt]->pt()) << "," << (pMap[*eIt]->t()) << ","
386  << (pMap[*eIt]->pid());
387  } else {
388  stream << fixed << setprecision(2) << (pMap[*eIt]->e()) << ","
389  << (pMap[*eIt]->pt()) << "," << (pMap[*eIt]->t()) << ","
390  << (pMap[*eIt]->pid());
391  }
392 
393  gv << (to_string(eIt->source().id()) + "->" + to_string(eIt->target().id()))
394  << " " << label << stream.str() << label2 << endl;
395  }
396  gv << endl;
397  gv << "}" << endl;
398  gv.close();
399 }
400 
401 void PartonShower::SaveAsGraphML(string fName) {
402  // Think about using tinyxml2 in future (if needed ...)
403 
404  ofstream g;
405  g.open(fName.c_str());
406 
407  g << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << endl;
408  g << "<graphml xmlns=\"http://graphml.graphdrawing.org/xmlns\"" << endl;
409  g << "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" << endl;
410  g << "xsi:schemaLocation=\"http://graphml.graphdrawing.org/xmlns" << endl;
411  g << " http://graphml.graphdrawing.org/xmlns/1.0/graphml.xsd\">" << endl;
412 
413  g << "<key id=\"nlabel\" for=\"node\" attr.name=\"label\" "
414  "attr.type=\"string\"/>"
415  << endl;
416  g << "<key id=\"nx\" for=\"node\" attr.name=\"x\" attr.type=\"double\"/>"
417  << endl;
418  g << "<key id=\"ny\" for=\"node\" attr.name=\"y\" attr.type=\"double\"/>"
419  << endl;
420  g << "<key id=\"nz\" for=\"node\" attr.name=\"z\" attr.type=\"double\"/>"
421  << endl;
422  g << "<key id=\"nt\" for=\"node\" attr.name=\"t\" attr.type=\"double\"/>"
423  << endl;
424 
425  g << "<key id=\"elabel\" for=\"edge\" attr.name=\"label\" "
426  "attr.type=\"string\"/>"
427  << endl;
428  g << "<key id=\"epl\" for=\"edge\" attr.name=\"plabel\" attr.type=\"int\"/>"
429  << endl;
430  g << "<key id=\"epid\" for=\"edge\" attr.name=\"pid\" attr.type=\"int\"/>"
431  << endl;
432  g << "<key id=\"estat\" for=\"edge\" attr.name=\"pstat\" attr.type=\"int\"/>"
433  << endl;
434 
435  g << "<key id=\"ept\" for=\"edge\" attr.name=\"pT\" attr.type=\"double\"/>"
436  << endl;
437  g << "<key id=\"eeta\" for=\"edge\" attr.name=\"eta\" attr.type=\"double\"/>"
438  << endl;
439  g << "<key id=\"ephi\" for=\"edge\" attr.name=\"phi\" attr.type=\"double\"/>"
440  << endl;
441  g << "<key id=\"ee\" for=\"edge\" attr.name=\"E\" attr.type=\"double\"/>"
442  << endl;
443 
444  g << "<graph id=\"G\" edgedefault=\"directed\">" << endl;
445 
446  node_iterator nIt, nEnd;
447  int n = 0;
448 
449  for (nIt = nodes_begin(), nEnd = nodes_end(); nIt != nEnd; ++nIt) {
450  stringstream stream;
451 
452  stream << fixed << setprecision(2) << (vMap[*nIt]->x_in().t());
453 
454  g << "<node id=\"" << n << "\">" << endl;
455  //g<<"<data key=\"nlabel\">"<<to_string(n)+"("+to_string(vMap[*nIt]->x_in().t())+")"<<"</data>"<<endl;
456  g << "<data key=\"nlabel\">" << to_string(n) + "(" + stream.str() + ")"
457  << "</data>" << endl;
458  g << "<data key=\"nx\">" << vMap[*nIt]->x_in().x() << "</data>" << endl;
459  g << "<data key=\"ny\">" << vMap[*nIt]->x_in().y() << "</data>" << endl;
460  g << "<data key=\"nz\">" << vMap[*nIt]->x_in().z() << "</data>" << endl;
461  g << "<data key=\"nt\">" << vMap[*nIt]->x_in().t() << "</data>" << endl;
462  g << "</node>" << endl;
463  n++;
464  }
465 
466  edge_iterator eIt, eEnd;
467  n = 0;
468  for (eIt = edges_begin(), eEnd = edges_end(); eIt != eEnd; ++eIt) {
469  g << "<edge id=\"" << n << "\" source=\"" << to_string(eIt->source().id())
470  << "\" target=\"" << to_string(eIt->target().id()) << "\">" << endl;
471  g << "<data key=\"elabel\">" << to_string((pMap[*eIt]->pt())) << "</data>"
472  << endl;
473  g << "<data key=\"epl\">" << pMap[*eIt]->plabel() << "</data>" << endl;
474  g << "<data key=\"epid\">" << pMap[*eIt]->pid() << "</data>" << endl;
475  g << "<data key=\"estat\">" << pMap[*eIt]->pstat() << "</data>" << endl;
476  g << "<data key=\"ept\">" << pMap[*eIt]->pt() << "</data>" << endl;
477  g << "<data key=\"eeta\">" << pMap[*eIt]->eta() << "</data>" << endl;
478  g << "<data key=\"ephi\">" << pMap[*eIt]->phi() << "</data>" << endl;
479  g << "<data key=\"ee\">" << pMap[*eIt]->e() << "</data>" << endl;
480  g << "</edge>" << endl;
481  n++;
482  }
483 
484  g << "</graph>" << endl;
485  g << "</graphml>" << endl;
486 
487  g.close();
488 
489 } // end namespace Jetscape
490 } // namespace Jetscape