Belle II Software  release-05-02-19
MCParticle.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2010-2011 Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Martin Ritter *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <mdst/dataobjects/MCParticle.h>
12 #include <framework/logging/Logger.h>
13 #include <framework/datastore/DataStore.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/utilities/HTML.h>
16 
17 #include <TDatabasePDG.h>
18 
19 #include <iostream>
20 #include <sstream>
21 
22 using namespace std;
23 using namespace Belle2;
24 
25 const double MCParticle::c_epsilon = 10e-7;
26 
27 
28 void MCParticle::setMassFromPDG()
29 {
30  if (TDatabasePDG::Instance()->GetParticle(m_pdg) == NULL) throw(ParticlePDGNotKnownError() << m_pdg);
31  m_mass = TDatabasePDG::Instance()->GetParticle(m_pdg)->Mass();
32 }
33 
34 
35 float MCParticle::getCharge() const
36 {
37  // Geant4 "optical photon" (m_pdg == 0) is not known to TDatabasePDG::Instance().
38  if (m_pdg == 0) {
39  return 0.0;
40  }
41 
42  if (TDatabasePDG::Instance()->GetParticle(m_pdg) == NULL) {
43  B2ERROR("PDG=" << m_pdg << " ***code unknown to TDatabasePDG");
44  return 0.0;
45  }
46 
47  return TDatabasePDG::Instance()->GetParticle(m_pdg)->Charge() / 3.0;
48 }
49 
50 
51 vector<MCParticle*> MCParticle::getDaughters() const
52 {
53  vector<MCParticle*> result;
54  if (m_firstDaughter > 0) {
55  fixParticleList();
56  if (m_lastDaughter > m_plist->GetEntriesFast()) throw LastChildIndexOutOfRangError();
57  TClonesArray& plist = *m_plist;
58  result.reserve(m_lastDaughter - m_firstDaughter + 1);
59  for (int i = m_firstDaughter - 1; i < m_lastDaughter; i++) {
60  result.push_back(static_cast<MCParticle*>(plist[i]));
61  }
62  }
63  return result;
64 }
65 
66 int MCParticle::getNDaughters() const
67 {
68  if (getFirstDaughter() == 0) //no daughters
69  return 0;
70  return getLastDaughter() - getFirstDaughter() + 1;
71 }
72 
73 void MCParticle::fixParticleList() const
74 {
75  if (m_plist != 0) return;
76 
77  TClonesArray* plist(0);
78 
79  //Search default location
80  //TODO: this could be replaced with RelationsObject::getArrayIndex()/getArrayName()
81  StoreArray<MCParticle> MCParticles;
82  if (MCParticles && MCParticles.getPtr()->IndexOf(this) >= 0) {
83  plist = MCParticles.getPtr();
84  } else {
85  //Search all StoreArrays which happen to store MCParticles
86  const DataStore::StoreEntryMap& map = DataStore::Instance().getStoreEntryMap(DataStore::c_Event);
87  for (DataStore::StoreEntryConstIter iter = map.begin(); iter != map.end(); ++iter) {
88  TClonesArray* value = dynamic_cast<TClonesArray*>(iter->second.ptr);
89  if (value && value->GetClass() == Class() && value->IndexOf(this) >= 0) {
90  plist = value;
91  break;
92  }
93  }
94  }
95  //Could not find any collection, raise exception
96  if (!plist) {
97  B2ERROR("Could not determine StoreArray the MCParticle belongs to !");
98  throw NoParticleListSetError();
99  }
100 
101  //Set plist pointer and index for whole array
102  for (int i = 0; i < plist->GetEntriesFast(); i++) {
103  MCParticle& mc = *(static_cast<MCParticle*>(plist->At(i)));
104  mc.m_plist = plist;
105  mc.m_index = i + 1;
106  }
107 }
108 std::string MCParticle::getName() const
109 {
110  const TParticlePDG* p = TDatabasePDG::Instance()->GetParticle(m_pdg);
111  if (p)
112  return p->GetName();
113  else //handle unknown PDG codes
114  return std::to_string(m_pdg);
115 }
116 std::string MCParticle::getInfoHTML() const
117 {
118  std::stringstream out;
119  out << "<b>Charge</b>=" << (int)getCharge();
120  out << ", <b>PDG</b>=" << getPDG();
121  out << " (" << getName() << ")";
122  out << "<br>";
123  out << "<b>isPrimaryParticle</b>=" << isPrimaryParticle();
124  out << ",<b>isInitial</b>=" << isInitial();
125  out << ",<b>isVirtual</b>=" << isVirtual();
126  out << "<br>";
127 
128  out << "<b>pT</b>=" << getMomentum().Pt();
129  out << ", <b>pZ</b>=" << m_momentum_z;
130  out << "<br>";
131  std::string unitType = HTML::chooseUnitOfLength(getProductionVertex());
132  int precision = 3;
133  out << "<b>V</b>=" << HTML::getStringConvertToUnit(getProductionVertex(), precision, unitType);
134 
135  const MCParticle* mom = getMother();
136  if (mom) {
137  out << "<br>";
138  out << "<b>Mother</b>: " << mom->getArrayName() << "[" << mom->getArrayIndex() << "] (" << mom->getName() << ")";
139  }
140  return out.str();
141 }
prepareAsicCrosstalkSimDB.e
e
aux.
Definition: prepareAsicCrosstalkSimDB.py:53
Belle2::DataStore::StoreEntryMap
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition: DataStore.h:89
Belle2::RelationsInterface::getArrayName
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
Definition: RelationsObject.h:379
Belle2::MCParticle::getArrayIndex
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition: MCParticle.h:255
Belle2::MCParticle::getName
virtual std::string getName() const
Return name of this particle.
Definition: MCParticle.cc:108
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreArray::getPtr
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition: StoreArray.h:321
Belle2::DataStore::StoreEntryConstIter
StoreEntryMap::const_iterator StoreEntryConstIter
const_iterator for a StoreEntry map.
Definition: DataStore.h:91
Belle2::MCParticle
A Class to store the Monte Carlo particle information.
Definition: MCParticle.h:43
Belle2::StoreArray< MCParticle >