StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Event.cc
1 // Event.cc is a part of the PYTHIA event generator.
2 // Copyright (C) 2014 Torbjorn Sjostrand.
3 // PYTHIA is licenced under the GNU GPL version 2, see COPYING for details.
4 // Please respect the MCnet Guidelines, see GUIDELINES for details.
5 
6 // Function definitions (not found in the header) for the
7 // Particle and Event classes, and some related global functions.
8 
9 #include "Pythia8/Event.h"
10 
11 namespace Pythia8 {
12 
13 //==========================================================================
14 
15 // Particle class.
16 // This class holds info on a particle in general.
17 
18 //--------------------------------------------------------------------------
19 
20 // Constants: could be changed here if desired, but normally should not.
21 // These are of technical nature, as described for each.
22 
23 // Small number to avoid division by zero.
24 const double Particle::TINY = 1e-20;
25 
26 //--------------------------------------------------------------------------
27 
28 // Set pointer to the particle data species of the particle.
29 
30 void Particle::setPDEPtr(ParticleDataEntry* pdePtrIn) {
31  pdePtr = pdePtrIn; if (pdePtrIn != 0 || evtPtr == 0) return;
32  pdePtr = (*evtPtr).particleDataPtr->particleDataEntryPtr( idSave);}
33 
34 //--------------------------------------------------------------------------
35 
36 // Functions for rapidity and pseudorapidity.
37 
38 double Particle::y() const {
39  double temp = log( ( pSave.e() + abs(pSave.pz()) ) / max( TINY, mT() ) );
40  return (pSave.pz() > 0) ? temp : -temp;
41 }
42 
43 double Particle::eta() const {
44  double temp = log( ( pSave.pAbs() + abs(pSave.pz()) ) / max( TINY, pT() ) );
45  return (pSave.pz() > 0) ? temp : -temp;
46 }
47 
48 //--------------------------------------------------------------------------
49 
50 // Method to find the index of the particle in the event record.
51 
52 int Particle::index() const { if (evtPtr == 0) return -1;
53  return (long(this) - long(&((*evtPtr)[0]))) / sizeof(Particle);
54 }
55 
56 //--------------------------------------------------------------------------
57 
58 // Convert internal Pythia status codes to the HepMC status conventions.
59 
60 int Particle::statusHepMC() const {
61 
62  // Positive codes are final particles. Status -12 are beam particles.
63  if (statusSave > 0) return 1;
64  if (statusSave == -12) return 4;
65  if (evtPtr == 0) return 0;
66 
67  // Hadrons, muons, taus that decay normally are status 2.
68  if (isHadron() || abs(idSave) == 13 || abs(idSave) == 15) {
69  // Particle should not decay into itself (e.g. Bose-Einstein).
70  if ( (*evtPtr)[daughter1Save].id() != idSave) {
71  int statusDau = (*evtPtr)[daughter1Save].statusAbs();
72  if (statusDau > 90 && statusDau < 95) return 2;
73  }
74  }
75 
76  // Other acceptable negative codes as their positive counterpart.
77  if (statusSave <= -11 && statusSave >= -200) return -statusSave;
78 
79  // Unacceptable codes as 0.
80  return 0;
81 
82 }
83 
84 //--------------------------------------------------------------------------
85 
86 // Trace the first and last copy of one and the same particle.
87 
88 int Particle::iTopCopy() const {
89 
90  if (evtPtr == 0) return -1;
91  int iUp = index();
92  while ( iUp > 0 && (*evtPtr)[iUp].mother2() == (*evtPtr)[iUp].mother1()
93  && (*evtPtr)[iUp].mother1() > 0) iUp = (*evtPtr)[iUp].mother1();
94  return iUp;
95 
96 }
97 
98 int Particle::iBotCopy() const {
99 
100  if (evtPtr == 0) return -1;
101  int iDn = index();
102  while ( iDn > 0 && (*evtPtr)[iDn].daughter2() == (*evtPtr)[iDn].daughter1()
103  && (*evtPtr)[iDn].daughter1() > 0) iDn = (*evtPtr)[iDn].daughter1();
104  return iDn;
105 
106 }
107 
108 //--------------------------------------------------------------------------
109 
110 // Trace the first and last copy of one and the same particle,
111 // also through shower branchings, making use of flavour matches.
112 // Stops tracing when this gives ambiguities.
113 
114 int Particle::iTopCopyId() const {
115 
116  if (evtPtr == 0) return -1;
117  int iUp = index();
118  for ( ; ; ) {
119  int mother1up = (*evtPtr)[iUp].mother1();
120  int id1up = (mother1up > 0) ? (*evtPtr)[mother1up].id() : 0;
121  int mother2up = (*evtPtr)[iUp].mother2();
122  int id2up = (mother2up > 0) ? (*evtPtr)[mother2up].id() : 0;
123  if (mother2up != mother1up && id2up == id1up) break;
124  if (id1up == idSave) {
125  iUp = mother1up;
126  continue;
127  }
128  if (id2up == idSave) {
129  iUp = mother2up;
130  continue;
131  }
132  break;
133  }
134  return iUp;
135 
136 }
137 
138 int Particle::iBotCopyId() const {
139 
140  if (evtPtr == 0) return -1;
141  int iDn = index();
142  for ( ; ; ) {
143  int daughter1dn = (*evtPtr)[iDn].daughter1();
144  int id1dn = (daughter1dn > 0) ? (*evtPtr)[daughter1dn].id() : 0;
145  int daughter2dn = (*evtPtr)[iDn].daughter2();
146  int id2dn = (daughter2dn > 0) ? (*evtPtr)[daughter2dn].id() : 0;
147  if (daughter2dn != daughter1dn && id2dn == id1dn) break;
148  if (id1dn == idSave) {
149  iDn = daughter1dn;
150  continue;
151  }
152  if (id2dn == idSave) {
153  iDn = daughter2dn;
154  continue;
155  }
156  break;
157  }
158  return iDn;
159 
160 }
161 
162 //--------------------------------------------------------------------------
163 
164 // Find complete list of mothers.
165 
166 vector<int> Particle::motherList() const {
167 
168  // Vector of all the mothers; created empty. Done if no event pointer.
169  vector<int> motherVec;
170  if (evtPtr == 0) return motherVec;
171 
172  // Special cases in the beginning, where the meaning of zero is unclear.
173  int statusSaveAbs = abs(statusSave);
174  if (statusSaveAbs == 11 || statusSaveAbs == 12) ;
175  else if (mother1Save == 0 && mother2Save == 0) motherVec.push_back(0);
176 
177  // One mother or a carbon copy
178  else if (mother2Save == 0 || mother2Save == mother1Save)
179  motherVec.push_back(mother1Save);
180 
181  // A range of mothers from string fragmentation.
182  else if ( (statusSaveAbs > 80 && statusSaveAbs < 90)
183  || (statusSaveAbs > 100 && statusSaveAbs < 107) )
184  for (int iRange = mother1Save; iRange <= mother2Save; ++iRange)
185  motherVec.push_back(iRange);
186 
187  // Two separate mothers.
188  else {
189  motherVec.push_back( min(mother1Save, mother2Save) );
190  motherVec.push_back( max(mother1Save, mother2Save) );
191  }
192 
193  // Done.
194  return motherVec;
195 
196 }
197 
198 //--------------------------------------------------------------------------
199 
200 // Find complete list of daughters.
201 
202 vector<int> Particle::daughterList() const {
203 
204  // Vector of all the daughters; created empty. Done if no event pointer.
205  vector<int> daughterVec;
206  if (evtPtr == 0) return daughterVec;
207 
208  // Simple cases: no or one daughter.
209  if (daughter1Save == 0 && daughter2Save == 0) ;
210  else if (daughter2Save == 0 || daughter2Save == daughter1Save)
211  daughterVec.push_back(daughter1Save);
212 
213  // A range of daughters.
214  else if (daughter2Save > daughter1Save)
215  for (int iRange = daughter1Save; iRange <= daughter2Save; ++iRange)
216  daughterVec.push_back(iRange);
217 
218  // Two separated daughters.
219  else {
220  daughterVec.push_back(daughter2Save);
221  daughterVec.push_back(daughter1Save);
222  }
223 
224  // Special case for two incoming beams: attach further
225  // initiators and remnants that have beam as mother.
226  if (abs(statusSave) == 12 || abs(statusSave) == 13) {
227  int i = index();
228  for (int iDau = i + 1; iDau < evtPtr->size(); ++iDau)
229  if ((*evtPtr)[iDau].mother1() == i) {
230  bool isIn = false;
231  for (int iIn = 0; iIn < int(daughterVec.size()); ++iIn)
232  if (iDau == daughterVec[iIn]) isIn = true;
233  if (!isIn) daughterVec.push_back(iDau);
234  }
235  }
236 
237  // Done.
238  return daughterVec;
239 
240 }
241 
242 //--------------------------------------------------------------------------
243 
244 // Find complete list of sisters. Optionally traces up with iTopCopy
245 // and down with iBotCopy to give sisters at same level of evolution.
246 
247 vector<int> Particle::sisterList(bool traceTopBot) const {
248 
249  // Vector of all the sisters; created empty.
250  vector<int> sisterVec;
251  if (evtPtr == 0 || abs(statusSave) == 11) return sisterVec;
252 
253  // Find all daughters of the mother.
254  int iUp = (traceTopBot) ? iTopCopy() : index();
255  int iMother = (*evtPtr)[iUp].mother1();
256  vector<int> daughterVec = (*evtPtr)[iMother].daughterList();
257 
258  // Copy all daughters, excepting the input particle itself.
259  for (int j = 0; j < int(daughterVec.size()); ++j) {
260  int iDau = daughterVec[j];
261  if (iDau != iUp) {
262  int iDn = (traceTopBot) ? (*evtPtr)[iDau].iBotCopy() : iDau;
263  sisterVec.push_back( iDn);
264  }
265  }
266 
267  // Done.
268  return sisterVec;
269 
270 }
271 
272 //--------------------------------------------------------------------------
273 
274 // Check whether a given particle is an arbitrarily-steps-removed
275 // mother to another. For the parton -> hadron transition, only
276 // first-rank hadrons are associated with the respective end quark.
277 
278 bool Particle::isAncestor(int iAncestor) const {
279 
280  // Begin loop to trace upwards from the daughter.
281  if (evtPtr == 0) return false;
282  int iUp = index();
283  int sizeNow = (*evtPtr).size();
284  for ( ; ; ) {
285 
286  // If positive match then done.
287  if (iUp == iAncestor) return true;
288 
289  // If out of range then failed to find match.
290  if (iUp <= 0 || iUp > sizeNow) return false;
291 
292  // If unique mother then keep on moving up the chain.
293  int mother1up = (*evtPtr)[iUp].mother1();
294  int mother2up = (*evtPtr)[iUp].mother2();
295  if (mother2up == mother1up || mother2up == 0) {iUp = mother1up; continue;}
296 
297  // If many mothers, except hadronization, then fail tracing.
298  int statusUp = (*evtPtr)[iUp].statusAbs();
299  if (statusUp < 81 || statusUp > 86) return false;
300 
301  // For hadronization step, fail if not first rank, else move up.
302  if (statusUp == 82) {
303  iUp = (iUp + 1 < sizeNow && (*evtPtr)[iUp + 1].mother1() == mother1up)
304  ? mother1up : mother2up; continue;
305  }
306  if (statusUp == 83) {
307  if ((*evtPtr)[iUp - 1].mother1() == mother1up) return false;
308  iUp = mother1up; continue;
309  }
310  if (statusUp == 84) {
311  if (iUp + 1 < sizeNow && (*evtPtr)[iUp + 1].mother1() == mother1up)
312  return false;
313  iUp = mother1up; continue;
314  }
315 
316  // Fail for ministring -> one hadron and for junctions.
317  return false;
318 
319  }
320  // End of loop. Should never reach beyond here.
321  return false;
322 
323 }
324 
325 //--------------------------------------------------------------------------
326 
327 // Recursively remove the decay products of a particle, update it to be
328 // undecayed, and update all mother/daughter indices to be correct.
329 // Warning: assumes that decay chains are nicely ordered.
330 
331 bool Particle::undoDecay() {
332 
333  // Do not remove daughters of a parton, i.e. entry carrying colour.
334  if (evtPtr == 0) return false;
335  int i = index();
336  if (i < 0 || i >= int((*evtPtr).size())) return false;
337  if (colSave != 0 || acolSave != 0) return false;
338 
339  // Find range of daughters to remove.
340  int dau1 = daughter1Save;
341  if (dau1 == 0) return false;
342  int dau2 = daughter2Save;
343  if (dau2 == 0) dau2 = dau1;
344 
345  // Refuse if any of the daughters have other mothers.
346  for (int j = dau1; j <= dau2; ++j) if ((*evtPtr)[j].mother1() != i
347  || ((*evtPtr)[j].mother2() != i && (*evtPtr)[j].mother2() != 0) )
348  return false;
349 
350  // Initialize range arrays for daughters and granddaughters.
351  vector<int> dauBeg, dauEnd;
352  dauBeg.push_back( dau1);
353  dauEnd.push_back( dau2);
354 
355  // Begin recursive search through all decay chains.
356  int iRange = 0;
357  do {
358  for (int j = dauBeg[iRange]; j <= dauEnd[iRange]; ++j)
359  if ((*evtPtr)[j].status() < 0) {
360 
361  // Find new daughter range, if present.
362  dau1 = (*evtPtr)[j].daughter1();
363  if (dau1 == 0) return false;
364  dau2 = (*evtPtr)[j].daughter2();
365  if (dau2 == 0) dau2 = dau1;
366 
367  // Check if the range duplicates or contradicts existing ones.
368  bool isNew = true;
369  for (int iR = 0; iR < int(dauBeg.size()); ++iR) {
370  if (dau1 == dauBeg[iR] && dau2 == dauEnd[iR]) isNew = false;
371  else if (dau1 >= dauBeg[iR] && dau1 <= dauEnd[iR]) return false;
372  else if (dau2 >= dauBeg[iR] && dau2 <= dauEnd[iR]) return false;
373  }
374 
375  // Add new range where relevant. Keep ranges ordered.
376  if (isNew) {
377  dauBeg.push_back( dau1);
378  dauEnd.push_back( dau2);
379  for (int iR = int(dauBeg.size()) - 1; iR > 0; --iR) {
380  if (dauBeg[iR] < dauBeg[iR - 1]) {
381  swap( dauBeg[iR], dauBeg[iR - 1]);
382  swap( dauEnd[iR], dauEnd[iR - 1]);
383  } else break;
384  }
385  }
386 
387  // End of recursive search all decay chains.
388  }
389  } while (++iRange < int(dauBeg.size()));
390 
391  // Join adjacent ranges to reduce number of erase steps.
392  if (int(dauBeg.size()) > 1) {
393  int iRJ = 0;
394  do {
395  if (dauEnd[iRJ] + 1 == dauBeg[iRJ + 1]) {
396  for (int iRB = iRJ + 1; iRB < int(dauBeg.size()) - 1; ++iRB)
397  dauBeg[iRB] = dauBeg[iRB + 1];
398  for (int iRE = iRJ; iRE < int(dauEnd.size()) - 1; ++iRE)
399  dauEnd[iRE] = dauEnd[iRE + 1];
400  dauBeg.pop_back();
401  dauEnd.pop_back();
402  } else ++iRJ;
403  } while (iRJ < int(dauBeg.size()) - 1);
404  }
405 
406  // Iterate over relevant ranges, from bottom up.
407  for (int iR = int(dauBeg.size()) - 1; iR >= 0; --iR) {
408  dau1 = dauBeg[iR];
409  dau2 = dauEnd[iR];
410  int nRem = dau2 - dau1 + 1;
411 
412  // Remove daughters in each range.
413  (*evtPtr).remove( dau1, dau2);
414 
415  // Update subsequent history to account for removed indices.
416  for (int j = 0; j < int((*evtPtr).size()); ++j) {
417  if ((*evtPtr)[j].mother1() > dau2)
418  (*evtPtr)[j].mother1( (*evtPtr)[j].mother1() - nRem );
419  if ((*evtPtr)[j].mother2() > dau2)
420  (*evtPtr)[j].mother2( (*evtPtr)[j].mother2() - nRem );
421  if ((*evtPtr)[j].daughter1() > dau2)
422  (*evtPtr)[j].daughter1( (*evtPtr)[j].daughter1() - nRem );
423  if ((*evtPtr)[j].daughter2() > dau2)
424  (*evtPtr)[j].daughter2( (*evtPtr)[j].daughter2() - nRem );
425  }
426  }
427 
428  // Update mother that has been undecayed.
429  statusSave = abs(statusSave);
430  daughter1Save = 0;
431  daughter2Save = 0;
432 
433  // Done.
434  return true;
435 
436 }
437 
438 //--------------------------------------------------------------------------
439 
440 // Particle name, with status but imposed maximum length -> may truncate.
441 
442 string Particle::nameWithStatus(int maxLen) const {
443 
444  if (pdePtr == 0) return " ";
445  string temp = (statusSave > 0) ? pdePtr->name(idSave)
446  : "(" + pdePtr->name(idSave) + ")";
447  while (int(temp.length()) > maxLen) {
448  // Remove from end, excluding closing bracket and charge.
449  int iRem = temp.find_last_not_of(")+-0");
450  temp.erase(iRem, 1);
451  }
452  return temp;
453 }
454 
455 //--------------------------------------------------------------------------
456 
457 // Add offsets to mother and daughter pointers (must be non-negative).
458 
459 void Particle::offsetHistory( int minMother, int addMother, int minDaughter,
460  int addDaughter) {
461 
462  if (addMother < 0 || addDaughter < 0) return;
463  if ( mother1Save > minMother ) mother1Save += addMother;
464  if ( mother2Save > minMother ) mother2Save += addMother;
465  if (daughter1Save > minDaughter) daughter1Save += addDaughter;
466  if (daughter2Save > minDaughter) daughter2Save += addDaughter;
467 
468 }
469 
470 //--------------------------------------------------------------------------
471 
472 // Add offsets to colour and anticolour (must be positive).
473 
474 void Particle::offsetCol( int addCol) {
475 
476  if (addCol < 0) return;
477  if ( colSave > 0) colSave += addCol;
478  if (acolSave > 0) acolSave += addCol;
479 
480 }
481 
482 //--------------------------------------------------------------------------
483 
484 // Invariant mass of a pair and its square.
485 // (Not part of class proper, but tightly linked.)
486 
487 double m(const Particle& pp1, const Particle& pp2) {
488  double m2 = pow2(pp1.e() + pp2.e()) - pow2(pp1.px() + pp2.px())
489  - pow2(pp1.py() + pp2.py()) - pow2(pp1.pz() + pp2.pz());
490  return (m2 > 0. ? sqrt(m2) : 0.);
491 }
492 
493 double m2(const Particle& pp1, const Particle& pp2) {
494  double m2 = pow2(pp1.e() + pp2.e()) - pow2(pp1.px() + pp2.px())
495  - pow2(pp1.py() + pp2.py()) - pow2(pp1.pz() + pp2.pz());
496  return m2;
497 }
498 
499 //==========================================================================
500 
501 // Event class.
502 // This class holds info on the complete event record.
503 
504 //--------------------------------------------------------------------------
505 
506 // Constants: could be changed here if desired, but normally should not.
507 // These are of technical nature, as described for each.
508 
509 // Maxmimum number of mothers or daughter indices per line in listing.
510 const int Event::IPERLINE = 20;
511 
512 //--------------------------------------------------------------------------
513 
514 // Copy all information from one event record to another.
515 
516 Event& Event::operator=( const Event& oldEvent) {
517 
518  // Do not copy if same.
519  if (this != &oldEvent) {
520 
521  // Reset all current info in the event.
522  clear();
523 
524  // Copy particle data table; needed for individual particles.
525  particleDataPtr = oldEvent.particleDataPtr;
526 
527  // Copy all the particles one by one.
528  maxColTag = 100;
529  for (int i = 0; i < oldEvent.size(); ++i) append( oldEvent[i] );
530 
531  // Copy all the junctions one by one.
532  for (int i = 0; i < oldEvent.sizeJunction(); ++i)
533  appendJunction( oldEvent.getJunction(i) );
534 
535  // Copy all other values.
536  startColTag = oldEvent.startColTag;
537  maxColTag = oldEvent.maxColTag;
538  savedSize = oldEvent.savedSize;
539  savedJunctionSize = oldEvent.savedJunctionSize;
540  scaleSave = oldEvent.scaleSave;
541  scaleSecondSave = oldEvent.scaleSecondSave;
542  headerList = oldEvent.headerList;
543 
544  // Done.
545  }
546  return *this;
547 
548 }
549 
550 //--------------------------------------------------------------------------
551 
552 // Add a copy of an existing particle at the end of the event record;
553 // return index. Three cases, depending on sign of new status code:
554 // Positive: copy is viewed as daughter, status of original is negated.
555 // Negative: copy is viewed as mother, status of original is unchanged.
556 // Zero: the new is a perfect carbon copy (maybe to be changed later).
557 
558 int Event::copy(int iCopy, int newStatus) {
559 
560  // Protect against attempt to copy negative entries (e.g., junction codes)
561  // or entries beyond end of record.
562  if (iCopy < 0 || iCopy >= size()) return -1;
563 
564  // Simple carbon copy.
565  entry.push_back(entry[iCopy]);
566  int iNew = entry.size() - 1;
567 
568  // Set up to make new daughter of old.
569  if (newStatus > 0) {
570  entry[iCopy].daughters(iNew,iNew);
571  entry[iCopy].statusNeg();
572  entry[iNew].mothers(iCopy, iCopy);
573  entry[iNew].status(newStatus);
574 
575  // Set up to make new mother of old.
576  } else if (newStatus < 0) {
577  entry[iCopy].mothers(iNew,iNew);
578  entry[iNew].daughters(iCopy, iCopy);
579  entry[iNew].status(newStatus);
580  }
581 
582  // Done.
583  return iNew;
584 
585 }
586 
587 //--------------------------------------------------------------------------
588 
589 // Print an event - special cases that rely on the general method.
590 // Not inline to make them directly callable in (some) debuggers.
591 
592 void Event::list() const {
593  list(false, false, cout);
594 }
595 
596 void Event::list(ostream& os) const {
597  list(false, false, os);
598 }
599 
600 void Event::list(bool showScaleAndVertex, bool showMothersAndDaughters)
601  const {
602  list(showScaleAndVertex, showMothersAndDaughters, cout);
603 }
604 
605 //--------------------------------------------------------------------------
606 
607 // Print an event.
608 
609 void Event::list(bool showScaleAndVertex, bool showMothersAndDaughters,
610  ostream& os) const {
611 
612  // Header.
613  os << "\n -------- PYTHIA Event Listing " << headerList << "----------"
614  << "-------------------------------------------------\n \n no "
615  << " id name status mothers daughters colou"
616  << "rs p_x p_y p_z e m \n";
617  if (showScaleAndVertex)
618  os << " scale pol "
619  << " xProd yProd zProd tProd "
620  << " tau\n";
621 
622  // At high energy switch to scientific format for momenta.
623  bool useFixed = (entry.empty() || entry[0].e() < 1e5);
624 
625  // Listing of complete event.
626  Vec4 pSum;
627  double chargeSum = 0.;
628  for (int i = 0; i < int(entry.size()); ++i) {
629  const Particle& pt = entry[i];
630 
631  // Basic line for a particle, always printed.
632  os << setw(6) << i << setw(10) << pt.id() << " " << left
633  << setw(18) << pt.nameWithStatus(18) << right << setw(4)
634  << pt.status() << setw(6) << pt.mother1() << setw(6)
635  << pt.mother2() << setw(6) << pt.daughter1() << setw(6)
636  << pt.daughter2() << setw(6) << pt.col() << setw(6) << pt.acol()
637  << ( (useFixed) ? fixed : scientific ) << setprecision(3)
638  << setw(11) << pt.px() << setw(11) << pt.py() << setw(11)
639  << pt.pz() << setw(11) << pt.e() << setw(11) << pt.m() << "\n";
640 
641  // Optional extra line for scale value, polarization and production vertex.
642  if (showScaleAndVertex)
643  os << " " << setw(11) << pt.scale()
644  << " " << fixed << setprecision(3) << setw(11) << pt.pol()
645  << " " << scientific << setprecision(3)
646  << setw(11) << pt.xProd() << setw(11) << pt.yProd()
647  << setw(11) << pt.zProd() << setw(11) << pt.tProd()
648  << setw(11) << pt.tau() << "\n";
649 
650  // Optional extra line, giving a complete list of mothers and daughters.
651  if (showMothersAndDaughters) {
652  int linefill = 2;
653  os << " mothers:";
654  vector<int> allMothers = motherList(i);
655  for (int j = 0; j < int(allMothers.size()); ++j) {
656  os << " " << allMothers[j];
657  if (++linefill == IPERLINE) {os << "\n "; linefill = 0;}
658  }
659  os << "; daughters:";
660  vector<int> allDaughters = daughterList(i);
661  for (int j = 0; j < int(allDaughters.size()); ++j) {
662  os << " " << allDaughters[j];
663  if (++linefill == IPERLINE) {os << "\n "; linefill = 0;}
664  }
665  if (linefill !=0) os << "\n";
666  }
667 
668  // Extra blank separation line when each particle spans more than one line.
669  if (showScaleAndVertex || showMothersAndDaughters) os << "\n";
670 
671  // Statistics on momentum and charge.
672  if (entry[i].status() > 0) {
673  pSum += entry[i].p();
674  chargeSum += entry[i].charge();
675  }
676  }
677 
678  // Line with sum charge, momentum, energy and invariant mass.
679  os << fixed << setprecision(3) << " "
680  << "Charge sum:" << setw(7) << chargeSum << " Momentum sum:"
681  << ( (useFixed) ? fixed : scientific ) << setprecision(3)
682  << setw(11) << pSum.px() << setw(11) << pSum.py() << setw(11)
683  << pSum.pz() << setw(11) << pSum.e() << setw(11) << pSum.mCalc()
684  << "\n";
685 
686  // Listing finished.
687  os << "\n -------- End PYTHIA Event Listing ----------------------------"
688  << "-------------------------------------------------------------------"
689  << endl;
690 }
691 
692 //--------------------------------------------------------------------------
693 
694 // Recursively remove the decay products of particle i, update it to be
695 // undecayed, and update all mother/daughter indices to be correct.
696 // Warning: assumes that decay chains are nicely ordered.
697 
698 bool Event::undoDecay(int i) {
699 
700  // Do not remove daughters of a parton, i.e. entry carrying colour.
701  if (i < 0 || i >= int(entry.size())) return false;
702  if (entry[i].col() != 0 || entry[i].acol() != 0) return false;
703 
704  // Find range of daughters to remove.
705  int dau1 = entry[i].daughter1();
706  if (dau1 == 0) return false;
707  int dau2 = entry[i].daughter2();
708  if (dau2 == 0) dau2 = dau1;
709 
710  // Refuse if any of the daughters have other mothers.
711  for (int j = dau1; j <= dau2; ++j) if (entry[j].mother1() != i
712  || (entry[j].mother2() != i && entry[j].mother2() != 0) ) return false;
713 
714  // Initialize range arrays for daughters and granddaughters.
715  vector<int> dauBeg, dauEnd;
716  dauBeg.push_back( dau1);
717  dauEnd.push_back( dau2);
718 
719  // Begin recursive search through all decay chains.
720  int iRange = 0;
721  do {
722  for (int j = dauBeg[iRange]; j <= dauEnd[iRange]; ++j)
723  if (entry[j].status() < 0) {
724 
725  // Find new daughter range, if present.
726  dau1 = entry[j].daughter1();
727  if (dau1 == 0) return false;
728  dau2 = entry[j].daughter2();
729  if (dau2 == 0) dau2 = dau1;
730 
731  // Check if the range duplicates or contradicts existing ones.
732  bool isNew = true;
733  for (int iR = 0; iR < int(dauBeg.size()); ++iR) {
734  if (dau1 == dauBeg[iR] && dau2 == dauEnd[iR]) isNew = false;
735  else if (dau1 >= dauBeg[iR] && dau1 <= dauEnd[iR]) return false;
736  else if (dau2 >= dauBeg[iR] && dau2 <= dauEnd[iR]) return false;
737  }
738 
739  // Add new range where relevant. Keep ranges ordered.
740  if (isNew) {
741  dauBeg.push_back( dau1);
742  dauEnd.push_back( dau2);
743  for (int iR = int(dauBeg.size()) - 1; iR > 0; --iR) {
744  if (dauBeg[iR] < dauBeg[iR - 1]) {
745  swap( dauBeg[iR], dauBeg[iR - 1]);
746  swap( dauEnd[iR], dauEnd[iR - 1]);
747  } else break;
748  }
749  }
750 
751  // End of recursive search all decay chains.
752  }
753  } while (++iRange < int(dauBeg.size()));
754 
755  // Join adjacent ranges to reduce number of erase steps.
756  if (int(dauBeg.size()) > 1) {
757  int iRJ = 0;
758  do {
759  if (dauEnd[iRJ] + 1 == dauBeg[iRJ + 1]) {
760  for (int iRB = iRJ + 1; iRB < int(dauBeg.size()) - 1; ++iRB)
761  dauBeg[iRB] = dauBeg[iRB + 1];
762  for (int iRE = iRJ; iRE < int(dauEnd.size()) - 1; ++iRE)
763  dauEnd[iRE] = dauEnd[iRE + 1];
764  dauBeg.pop_back();
765  dauEnd.pop_back();
766  } else ++iRJ;
767  } while (iRJ < int(dauBeg.size()) - 1);
768  }
769 
770  // Iterate over relevant ranges, from bottom up.
771  for (int iR = int(dauBeg.size()) - 1; iR >= 0; --iR) {
772  dau1 = dauBeg[iR];
773  dau2 = dauEnd[iR];
774  int nRem = dau2 - dau1 + 1;
775 
776  // Remove daughters in each range.
777  entry.erase( entry.begin() + dau1, entry.begin() + dau2 + 1);
778 
779  // Update subsequent history to account for removed indices.
780  for (int j = 0; j < int(entry.size()); ++j) {
781  if (entry[j].mother1() > dau2)
782  entry[j].mother1( entry[j].mother1() - nRem );
783  if (entry[j].mother2() > dau2)
784  entry[j].mother2( entry[j].mother2() - nRem );
785  if (entry[j].daughter1() > dau2)
786  entry[j].daughter1( entry[j].daughter1() - nRem );
787  if (entry[j].daughter2() > dau2)
788  entry[j].daughter2( entry[j].daughter2() - nRem );
789  }
790  }
791 
792  // Update mother that has been undecayed.
793  entry[i].statusPos();
794  entry[i].daughters();
795 
796  // Done.
797  return true;
798 
799 }
800 
801 //--------------------------------------------------------------------------
802 
803 // Find complete list of mothers.
804 
805 vector<int> Event::motherList(int i) const {
806 
807  // Vector of all the mothers; created empty.
808  vector<int> mothers;
809 
810  // Read out the two official mother indices and status code.
811  int mother1 = entry[i].mother1();
812  int mother2 = entry[i].mother2();
813  int statusAbs = entry[i].statusAbs();
814 
815  // Special cases in the beginning, where the meaning of zero is unclear.
816  if (statusAbs == 11 || statusAbs == 12) ;
817  else if (mother1 == 0 && mother2 == 0) mothers.push_back(0);
818 
819  // One mother or a carbon copy
820  else if (mother2 == 0 || mother2 == mother1) mothers.push_back(mother1);
821 
822  // A range of mothers from string fragmentation.
823  else if ( (statusAbs > 80 && statusAbs < 90)
824  || (statusAbs > 100 && statusAbs < 107) )
825  for (int iRange = mother1; iRange <= mother2; ++iRange)
826  mothers.push_back(iRange);
827 
828  // Two separate mothers.
829  else {
830  mothers.push_back( min(mother1, mother2) );
831  mothers.push_back( max(mother1, mother2) );
832  }
833 
834  // Done.
835  return mothers;
836 
837 }
838 
839 //--------------------------------------------------------------------------
840 
841 // Find complete list of daughters.
842 
843 vector<int> Event::daughterList(int i) const {
844 
845  // Vector of all the daughters; created empty.
846  vector<int> daughters;
847 
848  // Read out the two official daughter indices.
849  int daughter1 = entry[i].daughter1();
850  int daughter2 = entry[i].daughter2();
851 
852  // Simple cases: no or one daughter.
853  if (daughter1 == 0 && daughter2 == 0) ;
854  else if (daughter2 == 0 || daughter2 == daughter1)
855  daughters.push_back(daughter1);
856 
857  // A range of daughters.
858  else if (daughter2 > daughter1)
859  for (int iRange = daughter1; iRange <= daughter2; ++iRange)
860  daughters.push_back(iRange);
861 
862  // Two separated daughters.
863  else {
864  daughters.push_back(daughter2);
865  daughters.push_back(daughter1);
866  }
867 
868  // Special case for two incoming beams: attach further
869  // initiators and remnants that have beam as mother.
870  if (entry[i].statusAbs() == 12 || entry[i].statusAbs() == 13)
871  for (int iDau = i + 1; iDau < size(); ++iDau)
872  if (entry[iDau].mother1() == i) {
873  bool isIn = false;
874  for (int iIn = 0; iIn < int(daughters.size()); ++iIn)
875  if (iDau == daughters[iIn]) isIn = true;
876  if (!isIn) daughters.push_back(iDau);
877  }
878 
879  // Done.
880  return daughters;
881 
882 }
883 
884 //--------------------------------------------------------------------------
885 
886 // Convert internal Pythia status codes to the HepMC status conventions.
887 
888 int Event::statusHepMC(int i) const {
889 
890  // Positive codes are final particles. Status -12 are beam particles.
891  int statusNow = entry[i].status();
892  if (statusNow > 0) return 1;
893  if (statusNow == -12) return 4;
894 
895  // Hadrons, muons, taus that decay normally are status 2.
896  int idNow = entry[i].id();
897  if (entry[i].isHadron() || abs(idNow) == 13 || abs(idNow) == 15) {
898  int iDau = entry[i].daughter1();
899  // Particle should not decay into itself (e.g. Bose-Einstein).
900  if ( entry[iDau].id() != idNow) {
901  int statusDau = entry[ iDau ].statusAbs();
902  if (statusDau > 90 && statusDau < 95) return 2;
903  }
904  }
905 
906  // Other acceptable negative codes as their positive counterpart.
907  if (statusNow <= -11 && statusNow >= -200) return -statusNow;
908 
909  // Unacceptable codes as 0.
910  return 0;
911 
912 }
913 
914 //--------------------------------------------------------------------------
915 
916 // Trace the first and last copy of one and the same particle.
917 
918 int Event::iTopCopy( int i) const {
919 
920  int iUp = i;
921  while ( iUp > 0 && entry[iUp].mother2() == entry[iUp].mother1()
922  && entry[iUp].mother1() > 0) iUp = entry[iUp].mother1();
923  return iUp;
924 
925 }
926 
927 int Event::iBotCopy( int i) const {
928 
929  int iDn = i;
930  while ( iDn > 0 && entry[iDn].daughter2() == entry[iDn].daughter1()
931  && entry[iDn].daughter1() > 0) iDn = entry[iDn].daughter1();
932  return iDn;
933 
934 }
935 
936 //--------------------------------------------------------------------------
937 
938 // Trace the first and last copy of one and the same particle,
939 // also through shower branchings, making use of flavour matches.
940 // Stops tracing when this gives ambiguities.
941 
942 int Event::iTopCopyId( int i) const {
943 
944  int id = entry[i].id();
945  int iUp = i;
946  for ( ; ; ) {
947  int mother1 = entry[iUp].mother1();
948  int id1 = (mother1 > 0) ? entry[mother1].id() : 0;
949  int mother2 = entry[iUp].mother2();
950  int id2 = (mother2 > 0) ? entry[mother2].id() : 0;
951  if (mother2 != mother1 && id2 == id1) break;
952  if (id1 == id) {
953  iUp = mother1;
954  continue;
955  }
956  if (id2 == id) {
957  iUp = mother2;
958  continue;
959  }
960  break;
961  }
962  return iUp;
963 
964 }
965 
966 int Event::iBotCopyId( int i) const {
967 
968  int id = entry[i].id();
969  int iDn = i;
970  for ( ; ; ) {
971  int daughter1 = entry[iDn].daughter1();
972  int id1 = (daughter1 > 0) ? entry[daughter1].id() : 0;
973  int daughter2 = entry[iDn].daughter2();
974  int id2 = (daughter2 > 0) ? entry[daughter2].id() : 0;
975  if (daughter2 != daughter1 && id2 == id1) break;
976  if (id1 == id) {
977  iDn = daughter1;
978  continue;
979  }
980  if (id2 == id) {
981  iDn = daughter2;
982  continue;
983  }
984  break;
985  }
986  return iDn;
987 
988 }
989 
990 //--------------------------------------------------------------------------
991 
992 // Find complete list of sisters.
993 
994 vector<int> Event::sisterList(int i) const {
995 
996  // Vector of all the sisters; created empty.
997  vector<int> sisters;
998  if (entry[i].statusAbs() == 11) return sisters;
999 
1000  // Find mother and all its daughters.
1001  int iMother = entry[i].mother1();
1002  vector<int> daughters = daughterList(iMother);
1003 
1004  // Copy all daughters, excepting the input particle itself.
1005  for (int j = 0; j < int(daughters.size()); ++j)
1006  if (daughters[j] != i) sisters.push_back( daughters[j] );
1007 
1008  // Done.
1009  return sisters;
1010 
1011 }
1012 
1013 //--------------------------------------------------------------------------
1014 
1015 // Find complete list of sisters. Traces up with iTopCopy and
1016 // down with iBotCopy to give sisters at same level of evolution.
1017 // Should this not give any final particles, the search is widened.
1018 
1019 vector<int> Event::sisterListTopBot(int i, bool widenSearch) const {
1020 
1021  // Vector of all the sisters; created empty.
1022  vector<int> sisters;
1023  if (entry[i].statusAbs() == 11) return sisters;
1024 
1025  // Trace up to first copy of current particle.
1026  int iUp = iTopCopy(i);
1027 
1028  // Find mother and all its daughters.
1029  int iMother = entry[iUp].mother1();
1030  vector<int> daughters = daughterList(iMother);
1031 
1032  // Trace all daughters down, excepting the input particle itself.
1033  for (int jD = 0; jD < int(daughters.size()); ++jD)
1034  if (daughters[jD] != iUp)
1035  sisters.push_back( iBotCopy( daughters[jD] ) );
1036 
1037  // Prune any non-final particles from list.
1038  int jP = 0;
1039  while (jP < int(sisters.size())) {
1040  if (entry[sisters[jP]].status() > 0) ++jP;
1041  else {
1042  sisters[jP] = sisters.back();
1043  sisters.pop_back();
1044  }
1045  }
1046 
1047  // If empty list then restore immediate daughters.
1048  if (sisters.size() == 0 && widenSearch) {
1049  for (int jR = 0; jR < int(daughters.size()); ++jR)
1050  if (daughters[jR] != iUp)
1051  sisters.push_back( iBotCopy( daughters[jR] ) );
1052 
1053  // Then trace all daughters, not only bottom copy.
1054  for (int jT = 0; jT < int(sisters.size()); ++jT) {
1055  daughters = daughterList( sisters[jT] );
1056  for (int k = 0; k < int(daughters.size()); ++k)
1057  sisters.push_back( daughters[k] );
1058  }
1059 
1060  // And then prune any non-final particles from list.
1061  int jN = 0;
1062  while (jN < int(sisters.size())) {
1063  if (entry[sisters[jN]].status() > 0) ++jN;
1064  else {
1065  sisters[jN] = sisters.back();
1066  sisters.pop_back();
1067  }
1068  }
1069  }
1070 
1071  // Done.
1072  return sisters;
1073 
1074 }
1075 
1076 //--------------------------------------------------------------------------
1077 
1078 // Check whether a given particle is an arbitrarily-steps-removed
1079 // mother to another. For the parton -> hadron transition, only
1080 // first-rank hadrons are associated with the respective end quark.
1081 
1082 bool Event::isAncestor(int i, int iAncestor) const {
1083 
1084  // Begin loop to trace upwards from the daughter.
1085  int iUp = i;
1086  for ( ; ; ) {
1087 
1088  // If positive match then done.
1089  if (iUp == iAncestor) return true;
1090 
1091  // If out of range then failed to find match.
1092  if (iUp <= 0 || iUp > size()) return false;
1093 
1094  // If unique mother then keep on moving up the chain.
1095  int mother1 = entry[iUp].mother1();
1096  int mother2 = entry[iUp].mother2();
1097  if (mother2 == mother1 || mother2 == 0) {iUp = mother1; continue;}
1098 
1099  // If many mothers, except hadronization, then fail tracing.
1100  int status = entry[iUp].statusAbs();
1101  if (status < 81 || status > 86) return false;
1102 
1103  // For hadronization step, fail if not first rank, else move up.
1104  if (status == 82) {
1105  iUp = (iUp + 1 < size() && entry[iUp + 1].mother1() == mother1)
1106  ? mother1 : mother2; continue;
1107  }
1108  if (status == 83) {
1109  if (entry[iUp - 1].mother1() == mother1) return false;
1110  iUp = mother1; continue;
1111  }
1112  if (status == 84) {
1113  if (iUp + 1 < size() && entry[iUp + 1].mother1() == mother1)
1114  return false;
1115  iUp = mother1; continue;
1116  }
1117 
1118  // Fail for ministring -> one hadron and for junctions.
1119  return false;
1120 
1121  }
1122  // End of loop. Should never reach beyond here.
1123  return false;
1124 
1125 }
1126 
1127 //--------------------------------------------------------------------------
1128 
1129 // Erase junction stored in specified slot and move up the ones under.
1130 
1131 void Event::eraseJunction(int i) {
1132 
1133  for (int j = i; j < int(junction.size()) - 1; ++j)
1134  junction[j] = junction[j + 1];
1135  junction.pop_back();
1136 
1137 }
1138 
1139 //--------------------------------------------------------------------------
1140 
1141 // Print the junctions in an event.
1142 
1143 void Event::listJunctions(ostream& os) const {
1144 
1145  // Header.
1146  os << "\n -------- PYTHIA Junction Listing "
1147  << headerList.substr(0,30) << "\n \n no kind col0 col1 col2 "
1148  << "endc0 endc1 endc2 stat0 stat1 stat2\n";
1149 
1150  // Loop through junctions in event and list them.
1151  for (int i = 0; i < sizeJunction(); ++i)
1152  os << setw(6) << i << setw(6) << kindJunction(i) << setw(6)
1153  << colJunction(i, 0) << setw(6) << colJunction(i, 1) << setw(6)
1154  << colJunction(i, 2) << setw(6) << endColJunction(i, 0) << setw(6)
1155  << endColJunction(i, 1) << setw(6) << endColJunction(i, 2) << setw(6)
1156  << statusJunction(i, 0) << setw(6) << statusJunction(i, 1) << setw(6)
1157  << statusJunction(i, 2) << "\n";
1158 
1159  // Alternative if no junctions. Listing finished.
1160  if (sizeJunction() == 0) os << " no junctions present \n";
1161  os << "\n -------- End PYTHIA Junction Listing --------------------"
1162  << "------" << endl;
1163 }
1164 
1165 //--------------------------------------------------------------------------
1166 
1167 // Operator overloading allows to append one event to an existing one.
1168 
1169 Event& Event::operator+=( const Event& addEvent) {
1170 
1171  // Find offsets. One less since won't copy line 0.
1172  int offsetIdx = entry.size() - 1;
1173  int offsetCol = maxColTag;
1174 
1175  // Add energy to zeroth line and calculate new invariant mass.
1176  entry[0].p( entry[0].p() + addEvent[0].p() );
1177  entry[0].m( entry[0].mCalc() );
1178 
1179  // Read out particles from line 1 (not 0) onwards.
1180  Particle temp;
1181  for (int i = 1; i < addEvent.size(); ++i) {
1182  temp = addEvent[i];
1183 
1184  // Add offset to nonzero mother, daughter and colour indices.
1185  if (temp.mother1() > 0) temp.mother1( temp.mother1() + offsetIdx );
1186  if (temp.mother2() > 0) temp.mother2( temp.mother2() + offsetIdx );
1187  if (temp.daughter1() > 0) temp.daughter1( temp.daughter1() + offsetIdx );
1188  if (temp.daughter2() > 0) temp.daughter2( temp.daughter2() + offsetIdx );
1189  if (temp.col() > 0) temp.col( temp.col() + offsetCol );
1190  if (temp.acol() > 0) temp.acol( temp.acol() + offsetCol );
1191 
1192  // Append particle to summed event.
1193  append( temp );
1194  }
1195 
1196  // Read out junctions one by one.
1197  Junction tempJ;
1198  int begCol, endCol;
1199  for (int i = 0; i < addEvent.sizeJunction(); ++i) {
1200  tempJ = addEvent.getJunction(i);
1201 
1202  // Add colour offsets to all three legs.
1203  for (int j = 0; j < 3; ++j) {
1204  begCol = tempJ.col(j);
1205  endCol = tempJ.endCol(j);
1206  if (begCol > 0) begCol += offsetCol;
1207  if (endCol > 0) endCol += offsetCol;
1208  tempJ.cols( j, begCol, endCol);
1209  }
1210 
1211  // Append junction to summed event.
1212  appendJunction( tempJ );
1213  }
1214 
1215  // Set header that indicates character as sum of events.
1216  headerList = "(combination of several events) -------";
1217 
1218  // Done.
1219  return *this;
1220 
1221 }
1222 
1223 //==========================================================================
1224 
1225 } // end namespace Pythia8
Definition: AgUStep.h:26