Belle II Software  release-05-01-25
InclusiveBtagReconstructionModule.cc
1 /**************************************************************************
2  * BASF2 (Belle Analysis Framework 2) *
3  * Copyright(C) 2013 - Belle II Collaboration *
4  * *
5  * Author: The Belle II Collaboration *
6  * Contributors: Hyacinth Stypula, Thomas Stypula *
7  * *
8  * This software is provided "as is" without any warranty. *
9  **************************************************************************/
10 
11 #include <analysis/modules/InclusiveBtagReconstruction/InclusiveBtagReconstructionModule.h>
12 
13 #include <framework/datastore/StoreObjPtr.h>
14 #include <framework/datastore/StoreArray.h>
15 
16 #include <analysis/dataobjects/Particle.h>
17 #include <analysis/dataobjects/ParticleList.h>
18 #include <analysis/DecayDescriptor/ParticleListName.h>
19 
20 #include <unordered_set>
21 #include <map>
22 #include <vector>
23 #include <TLorentzVector.h>
24 
25 using namespace Belle2;
26 
27 //-----------------------------------------------------------------
28 // Register the Module
29 //-----------------------------------------------------------------
30 REG_MODULE(InclusiveBtagReconstruction)
31 
32 //-----------------------------------------------------------------
33 // Implementation
34 //-----------------------------------------------------------------
35 
37 {
38  // Set module properties
39  setDescription("Inclusive Btag reconstruction");
40 
41  // Parameter definitions
42  addParam("upsilonListName", m_upsilonListName, "Name of the ParticleList to be filled with Upsilon(4S) -> B:sig anti-B:tag",
43  std::string("Upsilon(4S)"));
44  addParam("bsigListName", m_bsigListName, "Name of the Bsig ParticleList", std::string(""));
45  addParam("btagListName", m_btagListName, "Name of the Btag ParticleList", std::string(""));
46  addParam("inputListsNames", m_inputListsNames, "List of names of the ParticleLists which are used to reconstruct Btag from");
47 }
48 
50 
52 {
54  bsigList.isRequired();
55  for (const std::string& inputListName : m_inputListsNames) {
56  StoreObjPtr<ParticleList> inputList(inputListName);
57  inputList.isRequired();
58  }
59 
61  btagList.registerInDataStore();
62 
64  antiBtagList.registerInDataStore();
65 
67  upsilonList.registerInDataStore();
68 }
69 
71 {
73 
75  if (!valid)
76  B2ERROR("Invalid Bsig list name: " << m_bsigListName);
77 
79  int pdgCode = mother->getPDGCode();
80 
82  btagList.create();
83  btagList->initialize(-pdgCode, btagList.getName());
84 
86  antiBtagList.create();
87  antiBtagList->initialize(pdgCode, antiBtagList.getName());
88  btagList->bindAntiParticleList(*antiBtagList);
89 
91  upsilonList.create();
92  upsilonList->initialize(300553, upsilonList.getName());
93 
94  const unsigned int n = bsigList->getListSize();
95  for (unsigned i = 0; i < n; i++) { // find Btag(s) for each Bsig
96  const Particle* bsig = bsigList->getParticle(i);
97  const std::vector<const Particle*>& bsigFinalStateDaughters = bsig->getFinalStateDaughters();
98  std::unordered_set<int> mdstSourcesOfBsigFinalStateDaughters;
99  std::map<int, std::vector<int>> btagDaughtersMap;
100 
101  for (const Particle* daughter : bsigFinalStateDaughters) {
102  mdstSourcesOfBsigFinalStateDaughters.insert(daughter->getMdstSource());
103  }
104  auto mdstSourcesEnd = mdstSourcesOfBsigFinalStateDaughters.end();
105 
106  // make a map of Btag daughters
107  for (const std::string& inputListName : m_inputListsNames) {
108  StoreObjPtr<ParticleList> inputList(inputListName);
109  const unsigned int m = inputList->getListSize();
110  for (unsigned j = 0; j < m; j++) {
111  const Particle* particle = inputList->getParticle(j);
112  const std::vector<const Particle*>& particleFinalStateDaughters = particle->getFinalStateDaughters();
113 
114  // check if particle shares something with bsig...
115  bool append = true;
116  for (const Particle* daughter : particleFinalStateDaughters) {
117  int mdstSource = daughter->getMdstSource();
118  if (mdstSourcesOfBsigFinalStateDaughters.find(mdstSource) != mdstSourcesEnd) {
119  append = false;
120  break;
121  }
122  if (append) {
123  auto it = btagDaughtersMap.find(mdstSource);
124  if (it != btagDaughtersMap.end()) { // check for mdstSource overlaps
125  it->second.push_back(particle->getArrayIndex());
126  } else {
127  btagDaughtersMap[mdstSource] = {particle->getArrayIndex()};
128  }
129  }
130  }
131  }
132  }
133 
134  // combine map entries to form Btag candidates
135  Map2Vector map2vector;
136  std::vector<std::vector<int>> btagCandidates;
137  map2vector.convert(btagDaughtersMap, btagCandidates);
138 
139  StoreArray<Particle> particles;
140 
141  for (std::vector<int> daughterIndices : btagCandidates) {
142  std::map<int, size_t> nonFinalStateIndicesCount;
143  TLorentzVector momentum;
144  for (int index : daughterIndices) {
145  // check if there are non-final-state particles. If yes, the momentum should be added just once.
146  if ((particles[index]->getFinalStateDaughters()).size() > 1) {
147  auto it = nonFinalStateIndicesCount.find(index);
148  if (it != nonFinalStateIndicesCount.end()) {
149  nonFinalStateIndicesCount[index]++;
150  continue;
151  } else {
152  nonFinalStateIndicesCount[index] = 1;
153  }
154  }
155  momentum += particles[index]->get4Vector();
156  }
157  // check the number of the daughters to make sure that the not-final-state particles are not mixed with the other particles that come from the same mdstSource
158  bool rightDaughtersCount = true;
159  for (auto& it : nonFinalStateIndicesCount) {
160  if (it.second != (particles[(it.first)]->getFinalStateDaughters()).size()) {
161  rightDaughtersCount = false;
162  break;
163  }
164  }
165  if (rightDaughtersCount == false) {
166  continue;
167  }
168 
169  //remove repeated index in daughterIndices
170  std::vector<int>::iterator it;
171  std::sort(daughterIndices.begin(), daughterIndices.end());
172  it = std::unique(daughterIndices.begin(), daughterIndices.end());
173  daughterIndices.resize(std::distance(daughterIndices.begin(), it));
174 
175  Particle btagCandidate(momentum, -1 * bsig->getPDGCode(), bsig->getFlavorType(), daughterIndices, bsig->getArrayPointer());
176  Particle* btag = particles.appendNew(btagCandidate);
177  btagList->addParticle(btag);
178 
179  Particle upsilon(momentum + bsig->get4Vector(), 300553, Particle::c_Unflavored, { bsig->getArrayIndex(), btag->getArrayIndex() },
180  bsig->getArrayPointer());
181  upsilonList->addParticle(particles.appendNew(upsilon));
182  }
183  }
184 }
185 
186 void Map2Vector::convert(std::map<int, std::vector<int>>& input, std::vector<std::vector<int>>& output)
187 {
188  makeEntries(input.begin(), input.end(), 0, output);
189 }
190 
191 void Map2Vector::makeEntries(std::map<int, std::vector<int>>::iterator positionOnTheMap,
192  const std::map<int, std::vector<int>>::const_iterator& end,
193  unsigned i, std::vector<std::vector<int>>& output)
194 {
195  if (positionOnTheMap == end) {
196  output.push_back(m_combination);
197  } else {
198  std::vector<int>& v = positionOnTheMap->second;
199  ++positionOnTheMap;
200  for (int k : v) {
201  if (i < m_combination.size()) m_combination[i] = k;
202  else m_combination.push_back(k);
203  makeEntries(positionOnTheMap, end, i + 1, output);
204  }
205  }
206 };
Belle2::Particle::getPDGCode
int getPDGCode(void) const
Returns PDG code.
Definition: Particle.h:380
Belle2::Particle::getArrayPointer
TClonesArray * getArrayPointer() const
Returns the pointer to the store array which holds the daughter particles.
Definition: Particle.h:844
Belle2::InclusiveBtagReconstructionModule::m_inputListsNames
std::vector< std::string > m_inputListsNames
Names of the ParticleLists to be used to reconstruct Btag.
Definition: InclusiveBtagReconstructionModule.h:61
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::InclusiveBtagReconstructionModule::event
virtual void event() override
process event
Definition: InclusiveBtagReconstructionModule.cc:70
Belle2::Map2Vector::m_combination
std::vector< int > m_combination
Vector containing current combination of numbers (e.g.
Definition: InclusiveBtagReconstructionModule.h:101
Belle2::DecayDescriptorParticle
Represents a particle in the DecayDescriptor.
Definition: DecayDescriptorParticle.h:37
Belle2::InclusiveBtagReconstructionModule::m_bsigListName
std::string m_bsigListName
Name of the Bsig ParticleList.
Definition: InclusiveBtagReconstructionModule.h:59
Belle2::DecayDescriptor::init
bool init(const std::string &str)
Initialise the DecayDescriptor from given string.
Definition: DecayDescriptor.cc:47
Belle2::Map2Vector::convert
void convert(std::map< int, std::vector< int > > &input, std::vector< std::vector< int > > &output)
Do the conversion using makeEntries().
Definition: InclusiveBtagReconstructionModule.cc:186
Belle2::ParticleListName::antiParticleListName
std::string antiParticleListName(const std::string &listName)
Returns name of anti-particle-list corresponding to listName.
Definition: ParticleListName.cc:10
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::Particle::getFinalStateDaughters
std::vector< const Belle2::Particle * > getFinalStateDaughters() const
Returns a vector of pointers to Final State daughter particles.
Definition: Particle.cc:613
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::StoreObjPtr
Type-safe access to single objects in the data store.
Definition: ParticleList.h:33
Belle2::Particle::c_Unflavored
@ c_Unflavored
Is its own antiparticle or we don't know whether it is a particle/antiparticle.
Definition: Particle.h:96
Belle2::InclusiveBtagReconstructionModule::m_upsilonListName
std::string m_upsilonListName
Name of the ParticleList to be filled with Upsilon(4S) -> B:sig anti-B:tag
Definition: InclusiveBtagReconstructionModule.h:58
Belle2::InclusiveBtagReconstructionModule::m_btagListName
std::string m_btagListName
Name of the Btag ParticleList.
Definition: InclusiveBtagReconstructionModule.h:60
Belle2::Particle
Class to store reconstructed particles.
Definition: Particle.h:77
Belle2::InclusiveBtagReconstructionModule::initialize
virtual void initialize() override
initialize the module (setup the data store)
Definition: InclusiveBtagReconstructionModule.cc:51
Belle2::InclusiveBtagReconstructionModule::~InclusiveBtagReconstructionModule
virtual ~InclusiveBtagReconstructionModule()
Destructor.
Belle2::Map2Vector::makeEntries
void makeEntries(std::map< int, std::vector< int >>::iterator positionOnTheMap, const std::map< int, std::vector< int >>::const_iterator &end, unsigned i, std::vector< std::vector< int >> &output)
Recursively iterates over a map until the end is reached, then the output is ready.
Definition: InclusiveBtagReconstructionModule.cc:191
Belle2::Particle::getFlavorType
EFlavorType getFlavorType() const
Returns flavor type of the decay (for FS particles: flavor type of particle)
Definition: Particle.h:395
Belle2::StoreArray< Particle >
Belle2::Particle::get4Vector
TLorentzVector get4Vector() const
Returns Lorentz vector.
Definition: Particle.h:464
Belle2::InclusiveBtagReconstructionModule
Inclusively reconstructs anti-B:tag from input ParticleLists for given B:sig.
Definition: InclusiveBtagReconstructionModule.h:42
Belle2::DecayDescriptor::getMother
const DecayDescriptorParticle * getMother() const
return mother.
Definition: DecayDescriptor.h:136
Belle2::Map2Vector
Helper class to make a vector of all possible combinations of int numbers contained in the input vect...
Definition: InclusiveBtagReconstructionModule.h:74
Belle2::InclusiveBtagReconstructionModule::m_decaydescriptor
DecayDescriptor m_decaydescriptor
Decay descriptor for parsing the user specifed DecayString.
Definition: InclusiveBtagReconstructionModule.h:63