Belle II Software light-2607-kasei
MCParticle.cc
1/**************************************************************************
2 * basf2 (Belle II Analysis Software Framework) *
3 * Author: The Belle II Collaboration *
4 * *
5 * See git log for contributors and copyright holders. *
6 * This file is licensed under LGPL-3.0, see LICENSE.md. *
7 **************************************************************************/
8
9#include <mdst/dataobjects/MCParticle.h>
10#include <framework/logging/Logger.h>
11#include <framework/datastore/DataStore.h>
12#include <framework/datastore/StoreArray.h>
13#include <framework/utilities/HTML.h>
14#include <framework/utilities/Conversion.h>
15
16#include <TDatabasePDG.h>
17
18#include <iostream>
19#include <sstream>
20#include <boost/algorithm/string.hpp>
21
22using namespace std;
23using namespace Belle2;
24
25
27{
28 if (TDatabasePDG::Instance()->GetParticle(m_pdg) == nullptr)
29 throw(ParticlePDGNotKnownError() << m_pdg);
30 m_mass = TDatabasePDG::Instance()->GetParticle(m_pdg)->Mass();
31}
32
33
35{
36 // Geant4 "optical photon" (m_pdg == 0) is not known to TDatabasePDG::Instance().
37 if (m_pdg == 0) {
38 return 0.0;
39 }
40
41 if (TDatabasePDG::Instance()->GetParticle(m_pdg) == nullptr) {
42 B2ERROR("PDG=" << m_pdg << " ***code unknown to TDatabasePDG");
43 return 0.0;
44 }
45
46 return TDatabasePDG::Instance()->GetParticle(m_pdg)->Charge() / 3.0;
47}
48
49
50vector<MCParticle*> MCParticle::getDaughters() const
51{
52 vector<MCParticle*> result;
53 if (m_firstDaughter > 0) {
55 if (m_lastDaughter > m_plist->GetEntriesFast()) throw LastChildIndexOutOfRangError();
56 const TClonesArray& plist = *m_plist;
57 result.reserve(m_lastDaughter - m_firstDaughter + 1);
58 for (int i = m_firstDaughter - 1; i < m_lastDaughter; i++) {
59 result.push_back(static_cast<MCParticle*>(plist[i]));
60 }
61 }
62 return result;
63}
64
66{
67 if (i >= getNDaughters()) {
68 return nullptr;
69 }
70 return getDaughters().at(i);
71}
72
74{
75 if (getFirstDaughter() == 0) //no daughters
76 return 0;
77 return getLastDaughter() - getFirstDaughter() + 1;
78}
79
81{
82 if (m_plist != 0) return;
83
84 TClonesArray* plist(0);
85
86 //Search default location
87 //TODO: this could be replaced with RelationsObject::getArrayIndex()/getArrayName()
88 StoreArray<MCParticle> MCParticles;
89 if (MCParticles && MCParticles.getPtr()->IndexOf(this) >= 0) {
90 plist = MCParticles.getPtr();
91 } else {
92 //Search all StoreArrays which happen to store MCParticles
94 for (DataStore::StoreEntryConstIter iter = map.begin(); iter != map.end(); ++iter) {
95 TClonesArray* value = dynamic_cast<TClonesArray*>(iter->second.ptr);
96 if (value && value->GetClass() == Class() && value->IndexOf(this) >= 0) {
97 plist = value;
98 break;
99 }
100 }
101 }
102 //Could not find any collection, raise exception
103 if (!plist) {
104 B2ERROR("Could not determine StoreArray the MCParticle belongs to !");
105 throw NoParticleListSetError();
106 }
107
108 //Set plist pointer and index for whole array
109 for (int i = 0; i < plist->GetEntriesFast(); i++) {
110 MCParticle& mc = *(static_cast<MCParticle*>(plist->At(i)));
111 mc.m_plist = plist;
112 mc.m_index = i + 1;
113 }
114}
115std::string MCParticle::getName() const
116{
117 const TParticlePDG* p = TDatabasePDG::Instance()->GetParticle(m_pdg);
118 if (p)
119 return p->GetName();
120 else //handle unknown PDG codes
121 return std::to_string(m_pdg);
122}
123std::string MCParticle::getInfoHTML() const
124{
125 std::stringstream out;
126 out << "<b>Charge</b>=" << (int)getCharge();
127 out << ", <b>PDG</b>=" << getPDG();
128 out << " (" << getName() << ")";
129 out << "<br>";
130 out << "<b>isPrimaryParticle</b>=" << isPrimaryParticle();
131 out << ",<b>isInitial</b>=" << isInitial();
132 out << ",<b>isVirtual</b>=" << isVirtual();
133 out << "<br>";
134
135 out << "<b>pT</b>=" << getMomentum().Rho();
136 out << ", <b>pZ</b>=" << m_momentum_z;
137 out << "<br>";
138 std::string unitType = HTML::chooseUnitOfLength(getProductionVertex());
139 int precision = 3;
140 out << "<b>V</b>=" << HTML::getStringConvertToUnit(getProductionVertex(), precision, unitType);
141
142 const MCParticle* mom = getMother();
143 if (mom) {
144 out << "<br>";
145 out << "<b>Mother</b>: " << mom->getArrayName() << "[" << mom->getArrayIndex() << "] (" << mom->getName() << ")";
146 }
147 return out.str();
148}
149
150const MCParticle* MCParticle::getParticleFromGeneralizedIndexString(const std::string& generalizedIndex) const
151{
152 // Split the generalizedIndex string in a vector of strings.
153 std::vector<std::string> generalizedIndexes;
154 boost::split(generalizedIndexes, generalizedIndex, boost::is_any_of(":"));
155
156 if (generalizedIndexes.empty()) {
157 B2WARNING("Generalized index of MC daughter particle is empty. Skipping.");
158 return nullptr;
159 }
160
161 // To explore a decay tree of unknown depth, we need a place to store
162 // both the root particle and the daughter particle at each iteration
163 const MCParticle* dauPart =
164 nullptr; // This will be eventually returned
165 const MCParticle* currentPart = this; // This is the root particle of the next iteration
166
167 // Loop over the generalizedIndexes until you get to the particle you want
168 for (auto& indexString : generalizedIndexes) {
169 // indexString is a string. First try to convert it into an int
170 int dauIndex = 0;
171 try {
172 dauIndex = Belle2::convertString<int>(indexString);
173 } catch (std::invalid_argument&) {
174 B2WARNING("Found the string " << indexString << "instead of a daughter index.");
175 return nullptr;
176 }
177
178 // Check that the daughter index is smaller than the number of daughters of the current root particle
179 if (dauIndex >= int(currentPart->getNDaughters()) or dauIndex < 0) {
180 B2WARNING("Daughter index out of range" << LogVar("daughter index", dauIndex));
181 B2WARNING("Trying to access non-existing particle.");
182 return nullptr;
183 } else {
184 dauPart = currentPart->getDaughter(dauIndex); // Pick the particle indicated by the generalizedIndex
185 currentPart = dauPart;
186 }
187 }
188 return dauPart;
189}
StoreEntryMap::const_iterator StoreEntryConstIter
const_iterator for a StoreEntry map.
Definition DataStore.h:89
StoreEntryMap & getStoreEntryMap(EDurability durability)
Get a reference to the object/array map.
Definition DataStore.h:325
@ c_Event
Different object in each event, all objects/arrays are invalidated after event() function has been ca...
Definition DataStore.h:59
static DataStore & Instance()
Instance of singleton Store.
Definition DataStore.cc:53
std::map< std::string, StoreEntry > StoreEntryMap
Map for StoreEntries.
Definition DataStore.h:87
int m_lastDaughter
1-based index of last daughter particle in collection, 0 if no daughters
Definition MCParticle.h:549
int m_firstDaughter
1-based index of first daughter particle in collection, 0 if no daughters
Definition MCParticle.h:548
float m_mass
mass of the particle
Definition MCParticle.h:535
virtual std::string getName() const override
Return name of this particle.
const MCParticle * getParticleFromGeneralizedIndexString(const std::string &generalizedIndex) const
Explores the decay tree of the MC particle and returns the (grand^n)daughter identified by a generali...
std::vector< Belle2::MCParticle * > getDaughters() const
Get vector of all daughter particles, empty vector if none.
Definition MCParticle.cc:50
int getArrayIndex() const
Get 0-based index of the particle in the corresponding MCParticle list.
Definition MCParticle.h:234
ROOT::Math::XYZVector getProductionVertex() const
Return production vertex position.
Definition MCParticle.h:178
virtual std::string getInfoHTML() const override
Return a short summary of this object's contents in HTML format.
int m_pdg
PDG-Code of the particle.
Definition MCParticle.h:534
const MCParticle * getDaughter(int i) const
Return i-th daughter.
Definition MCParticle.cc:65
int getNDaughters() const
Return number of daughter MCParticles.
Definition MCParticle.cc:73
int getLastDaughter() const
Get 1-based index of last daughter, 0 if no daughters.
Definition MCParticle.h:249
float getCharge() const
Return the particle charge defined in TDatabasePDG.
Definition MCParticle.cc:34
float m_momentum_z
momentum of particle, z component
Definition MCParticle.h:539
void fixParticleList() const
Search the DataStore for the corresponding MCParticle array.
Definition MCParticle.cc:80
int getPDG() const
Return PDG code of particle.
Definition MCParticle.h:101
ROOT::Math::XYZVector getMomentum() const
Return momentum.
Definition MCParticle.h:187
MCParticle()
Default constructor for ROOT.
Definition MCParticle.h:72
TClonesArray * m_plist
Internal pointer to DataStore Array containing particles belonging to this collection.
Definition MCParticle.h:522
int getFirstDaughter() const
Get 1-based index of first daughter, 0 if no daughters.
Definition MCParticle.h:241
void setMassFromPDG()
Sets the mass for the particle from the particle's PDG code.
Definition MCParticle.cc:26
std::string getArrayName() const
Get name of array this object is stored in, or "" if not found.
Accessor to arrays stored in the data store.
Definition StoreArray.h:113
TClonesArray * getPtr() const
Raw access to the underlying TClonesArray.
Definition StoreArray.h:311
Class to store variables with their name which were sent to the logging service.
T convertString(const std::string &str)
Converts a string to type T (one of float, double, long double, int, long int, unsigned long int).
bool isInitial() const
Check if particle is an initial particle such as ISR.
Definition MCParticle.h:581
MCParticle * getMother() const
Returns a pointer to the mother particle.
Definition MCParticle.h:591
bool isPrimaryParticle() const
Check if particle is a primary particle which was created by the generator (and not,...
Definition MCParticle.h:586
bool isVirtual() const
Check if particle is virtual.
Definition MCParticle.h:566
std::string getStringConvertToUnit(const ROOT::Math::XYZVector &vec, int precision=2, const std::string &unitType="cm")
get a string with vector coordinates: (x, y, z).
Definition HTML.cc:85
std::string chooseUnitOfLength(const ROOT::Math::XYZVector &vec)
get a string with a unit type to convert a vector, so that it is easily readable.
Definition HTML.cc:102
Abstract base class for different kinds of events.
STL namespace.