Belle II Software development
ParticleList.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 <analysis/dataobjects/ParticleList.h>
10
11// framework - DataStore
12#include <framework/datastore/StoreArray.h>
13#include <framework/datastore/StoreObjPtr.h>
14
15// framework aux
16#include <framework/logging/Logger.h>
17#include <framework/utilities/HTML.h>
18
19#include <algorithm>
20
21using namespace std;
22using namespace Belle2;
23
24ParticleList::~ParticleList()
25{
26 delete m_antiList;
27}
28
29void ParticleList::initialize(int pdg, const std::string& name, const std::string& particleStoreName)
30{
31 m_pdg = pdg;
32 m_pdgbar = pdg;
33 m_particleStore = particleStoreName;
34
35 m_thisListName = name;
36 m_antiListName.clear();
37
38 std::string label = m_thisListName.substr(m_thisListName.find_first_of(':') + 1);
39 if ((Const::finalStateParticlesSet.contains(Const::ParticleType(abs(m_pdg))) and label == "all") or label == "V0")
40 m_isReserved = true;
41}
42
43void ParticleList::setParticleCollectionName(const std::string& name, bool forAntiParticle)
44{
45 m_particleStore = name;
46
47 if (forAntiParticle and !m_antiListName.empty())
49}
50
52{
53 if (m_isReserved)
54 B2FATAL("ParticleList::addParticle The ParticleList " << m_thisListName <<
55 " is reserved and forbidden to be manipulated.");
56
57 if (particle->getArrayName() != m_particleStore) {
58 B2ERROR("ParticleList::addParticle particle is from different store array, not added");
59 return;
60 }
61
62 int iparticle = particle->getArrayIndex();
63 if (iparticle < 0) {
64 B2ERROR("ParticleList::addParticle particle is not in a store array, not added");
65 return;
66 }
67
68 int pdg = particle->getPDGCode();
69 Particle::EFlavorType type = particle->getFlavorType();
70 addParticle((unsigned) iparticle, pdg, type, true);
71}
72
73void ParticleList::bindAntiParticleList(ParticleList& antiList, bool includingAntiList)
74{
75 m_pdgbar = antiList.getPDGCode();
77
78 if (abs(m_pdgbar) != abs(m_pdg))
79 B2ERROR("ParticleList::bindAntiParticleList invalid (inconsistent) PDG codes!");
80
81 if (includingAntiList)
82 antiList.bindAntiParticleList(*this, false);
83}
84
85void ParticleList::addParticle(unsigned iparticle, int pdg, Particle::EFlavorType type, bool includingAntiList)
86{
87 if (m_isReserved)
88 B2FATAL("ParticleList::addParticle The ParticleList " << m_thisListName <<
89 " is reserved and forbidden to be manipulated.");
90
91 if (abs(pdg) != abs(getPDGCode())) {
92 B2ERROR("ParticleList::addParticle PDG codes do not match, not added");
93 return;
94 }
95
96 if (type == Particle::c_Unflavored) {
97 // this is self-conjugated particle
98 // add it to the self-conjugated list of this (and anti-particle list if exists)
99
100 // check if the particle is already in this list
101 if (std::find(m_scList.begin(), m_scList.end(), iparticle) == m_scList.end()) {
102 m_scList.push_back(iparticle);
103 } else {
104 B2WARNING("ParticleList::addParticle Trying to add Particle with index=" << iparticle
105 << " to the ParticleList=" << m_thisListName << " that is already included!");
106 return;
107 }
108
109 // add it to the self-conjugated list
110 if (includingAntiList and !m_antiListName.empty())
111 getAntiParticleList().addParticle(iparticle, pdg, type, false);
112 } else if (type == Particle::c_Flavored) {
113 unsigned antiParticle = (pdg == getPDGCode()) ? 0 : 1;
114
115 if (antiParticle)
116 getAntiParticleList().addParticle(iparticle, pdg, type, false);
117 else {
118 if (std::find(m_fsList.begin(), m_fsList.end(), iparticle) == m_fsList.end()) {
119 m_fsList.push_back(iparticle);
120 } else {
121 B2WARNING("ParticleList::addParticle Trying to add Particle with index=" << iparticle
122 << " to the ParticleList=" << m_thisListName << "that is already included! Particle not added");
123 }
124 }
125 } else {
126 B2ERROR("ParticleList::addParticle invalid flavor type, not added");
127 }
128}
129
130void ParticleList::removeParticles(const std::vector<unsigned int>& toRemove, bool removeFromAntiList)
131{
132 if (m_isReserved)
133 B2FATAL("ParticleList::removeParticles The ParticleList " << m_thisListName <<
134 " is reserved and forbidden to be manipulated.");
135
136 std::vector<int> newList;
137 // remove Particles from flavor-specific list of this Particle List
138 for (int i : m_fsList) {
139 if (std::find(toRemove.begin(), toRemove.end(), i) == toRemove.end())
140 newList.push_back(i);
141 }
142 m_fsList = newList;
143
144 // remove Particles from self-conjugated list of this Particle List
145 newList.clear();
146 for (int i : m_scList) {
147 if (std::find(toRemove.begin(), toRemove.end(), i) == toRemove.end())
148 newList.push_back(i);
149 }
150 m_scList = newList;
151
152 if (removeFromAntiList and !m_antiListName.empty()) {
153 getAntiParticleList().removeParticles(toRemove, false);
154 }
155}
156
157void ParticleList::clear(bool includingAntiList)
158{
159 if (m_isReserved)
160 B2FATAL("ParticleList::clear The ParticleList " << m_thisListName <<
161 " is reserved and forbidden to be manipulated.");
162
163 m_fsList.clear();
164 m_scList.clear();
165
166 if (includingAntiList and !m_antiListName.empty()) {
167 getAntiParticleList().clear(false);
168 }
169}
170
171Particle* ParticleList::getParticle(unsigned i, bool includingAntiList) const
172{
174
175 if (i < m_fsList.size()) {
176 return Particles[m_fsList[i]];
177 } else if (i < m_fsList.size() + m_scList.size()) {
178 i -= m_fsList.size();
179 return Particles[m_scList[i]];
180 }
181
182 if (includingAntiList and !m_antiListName.empty())
183 return getAntiParticleList().getParticle(i - m_fsList.size() - m_scList.size(), false);
184
185 return nullptr;
186}
187
188Particle* ParticleList::getParticleWithMdstIdx(unsigned int mdstIdx, bool includingAntiList) const
189{
190 const unsigned int n = this->getListSize(includingAntiList);
191 for (unsigned i = 0; i < n; i++) {
192
193 auto particle = this->getParticle(i, includingAntiList);
194
195 if (particle->getMdstArrayIndex() == mdstIdx) {
196 return particle;
197 }
198 }
199 return nullptr;
200}
201
202Particle* ParticleList::getParticleWithMdstSource(int mdstSource, bool includingAntiList) const
203{
204 const unsigned int n = this->getListSize(includingAntiList);
205 for (unsigned i = 0; i < n; i++) {
206
207 auto particle = this->getParticle(i, includingAntiList);
208
209 if (particle->getMdstSource() == mdstSource) {
210 return particle;
211 }
212 }
213 return nullptr;
214}
215
216unsigned ParticleList::getListSize(bool includingAntiList) const
217{
218 unsigned size = 0;
219
220 // FlavorSpecific particles of this list
221 size += m_fsList.size();
222 // SelfConjugated particles of this list = SelfConjugated particles of anti-particle list
223 size += m_scList.size();
224
225 if (includingAntiList) {
226 // FlavorSpecific particles of anti-particle list
227 size += getNParticlesOfType(EParticleType::c_FlavorSpecificParticle, true);
228 }
229
230 return size;
231}
232
233const std::vector<int>& ParticleList::getList(EParticleType K, bool forAntiParticle) const
234{
235 if (!forAntiParticle) {
236 if (K == c_FlavorSpecificParticle)
237 return m_fsList;
238 else
239 return m_scList;
240 } else {
241 const static std::vector<int> emptyList;
242 if (m_antiListName.empty())
243 return emptyList;
244
245 return getAntiParticleList().getList(K);
246 }
247}
248
249bool ParticleList::contains(const Particle* p, bool includingAntiList) const
250{
251 const int index = p->getArrayIndex();
252 for (int i = 0; i < 3; i++) {
253 if (i == 1 && !includingAntiList)
254 continue;
255
256 const std::vector<int>& currentList = getList((i < 2) ? c_FlavorSpecificParticle : c_SelfConjugatedParticle, i == 1);
257 if (std::find(currentList.begin(), currentList.end(), index) != currentList.end())
258 return true;
259 }
260 return false;
261}
262
263int ParticleList::getIndex(const Particle* p, bool includingAntiList) const
264{
265 const int index = p->getArrayIndex();
266
267 auto it_fs = std::find(m_fsList.begin(), m_fsList.end(), index);
268 if (it_fs != m_fsList.end()) {
269 return std::distance(m_fsList.begin(), it_fs);
270 }
271
272 auto it_sc = std::find(m_scList.begin(), m_scList.end(), index);
273 if (it_sc != m_scList.end()) {
274 return std::distance(m_scList.begin(), it_sc) + m_fsList.size();
275 }
276
277 if (includingAntiList and !m_antiListName.empty()) {
278 int indexAnti = getAntiParticleList().getIndex(p, false);
279 if (indexAnti != -1)
280 return indexAnti + m_fsList.size() + m_scList.size();
281 }
282
283 return -1;
284}
285
287{
289}
290
291std::string ParticleList::getInfoHTML() const
292{
293 std::stringstream stream;
294 unsigned thisFSCount = getNParticlesOfType(c_FlavorSpecificParticle);
295 unsigned thisSCCount = getNParticlesOfType(c_SelfConjugatedParticle);
296 unsigned antiFSCount = getNParticlesOfType(c_FlavorSpecificParticle, true);
297 unsigned antiSCCount = getNParticlesOfType(c_SelfConjugatedParticle, true);
298
299 if (!m_antiListName.empty()) {
300 stream << " ParticleLists: " << m_thisListName << " (" << thisFSCount << "+" << thisSCCount << ")"
301 << " + " << m_antiListName << " (" << antiFSCount << "+" << antiSCCount << ")";
302 } else {
303 stream << " ParticleList : " << m_thisListName << " (" << thisFSCount << "+" << thisSCCount << ")";
304 }
305 return HTML::escape(stream.str());
306}
307
309{
310 if (!m_antiList) {
312 if (!m_antiList->isValid()) {
313 B2FATAL("Anti-particle list " << m_antiListName << " for " << m_thisListName <<
314 " not found, even though one was set via bindAntiParticleList(). Maybe you only saved one list into a .root file?");
315 }
316 }
317 return **m_antiList;
318}
#define K(x)
macro autogenerated by FFTW
The ParticleType class for identifying different particle types.
Definition: Const.h:408
static const ParticleSet finalStateParticlesSet
set of final set particles that can be created by the ParticleLoader
Definition: Const.h:657
ParticleList is a container class that stores a collection of Particle objects.
Definition: ParticleList.h:140
void bindAntiParticleList(ParticleList &antiList, bool includingAntiList=true)
Binds particle and anti-particle ParticleLists.
Definition: ParticleList.cc:73
int getPDGCode() const
Returns PDG code.
Definition: ParticleList.h:239
Particle * getParticleWithMdstSource(int mdstSource, bool includingAntiList=true) const
Returns the particle from the list matching the given mdst source, if any is found.
std::string m_antiListName
name of ParticleList for anti-particles
Definition: ParticleList.h:391
Particle * getParticle(unsigned i, bool includingAntiList=true) const
Returns i-th particle from the list and anti list if requested.
void addParticle(const Particle *particle)
Adds a new particle to the list (safe method)
Definition: ParticleList.cc:51
bool m_isReserved
transient
Definition: ParticleList.h:396
bool contains(const Particle *p, bool includingAntiList=true) const
Returns true if and only if 'p' is already in this list.
const std::vector< int > & getList(EParticleType K, bool forAntiParticle=false) const
Returns list of StoreArray<Particle> indices.
ParticleList & getAntiParticleList() const
Returns bound anti-particle list.
unsigned getListSize(bool includingAntiList=true) const
Returns total number of particles in this list and anti list if requested.
void setParticleCollectionName(const std::string &name, bool forAntiParticle=true)
Sets Particle store array name to which particle list refers.
Definition: ParticleList.cc:43
int m_pdg
PDG code of Particle.
Definition: ParticleList.h:382
std::string m_particleStore
name of Particle store array
Definition: ParticleList.h:388
int getIndex(const Particle *p, bool includingAntiList=true) const
Returns index of the given particle 'p' in this list.
std::string m_thisListName
name of this ParticleList
Definition: ParticleList.h:390
void clear(bool includingAntiList=true)
Remove all elements from list, afterwards getListSize() will be 0.
StoreObjPtr< ParticleList > * m_antiList
keep anti-list around for performance.
Definition: ParticleList.h:394
std::vector< int > m_fsList
list of 0-based indices of flavor-specific Particles (particles that have an anti-particle)
Definition: ParticleList.h:384
std::string getParticleListName() const
Returns the name this ParticleList.
Definition: ParticleList.h:277
unsigned getNParticlesOfType(EParticleType K, bool forAntiParticle=false) const
Returns the number of flavor-specific particles or self-conjugated particles in this list or its anti...
Definition: ParticleList.h:319
int m_pdgbar
PDG code of antiparticle.
Definition: ParticleList.h:383
EParticleType
Type of Particle (determines in which of the two internal lists the particle is stored).
Definition: ParticleList.h:149
void print() const
Prints the list.
std::string getInfoHTML() const
Return a short summary of this object's contents in HTML format.
std::vector< int > m_scList
list of 0-based indices of self-conjugated Particles (particles that do not have an anti-particle)
Definition: ParticleList.h:386
Particle * getParticleWithMdstIdx(unsigned int mdstIdx, bool includingAntiList=true) const
Returns the particle from the list matching the given mdst array index, if any is found.
void initialize(int pdg, const std::string &name, const std::string &particleStoreName="Particles")
Sets the PDG code and name of this ParticleList.
Definition: ParticleList.cc:29
void removeParticles(const std::vector< unsigned int > &toRemove, bool removeFromAntiList=true)
Remove given elements from list.
Class to store reconstructed particles.
Definition: Particle.h:76
EFlavorType
describes flavor type, see getFlavorType().
Definition: Particle.h:96
@ c_Unflavored
Is its own antiparticle or we don't know whether it is a particle/antiparticle.
Definition: Particle.h:97
@ c_Flavored
Is either particle or antiparticle.
Definition: Particle.h:98
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
std::string escape(const std::string &str)
Convert &, <, > etc.
Definition: HTML.cc:159
std::string htmlToPlainText(const std::string &html)
Reformat given HTML string into terminal-friendly plain text.
Definition: HTML.cc:138
Abstract base class for different kinds of events.
STL namespace.