Belle II Software  release-06-00-14
CurlTaggerModule.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/modules/CurlTagger/CurlTaggerModule.h>
10 
11 #include <framework/datastore/StoreObjPtr.h>
12 
13 #include <analysis/dataobjects/ParticleList.h>
14 
15 #include <analysis/variables/TrackVariables.h>
16 #include <analysis/variables/Variables.h>
17 #include <analysis/variables/MCTruthVariables.h>
18 
19 #include <vector>
20 #include <string>
21 
22 //Module Includes
23 #include <analysis/modules/CurlTagger/Bundle.h>
24 #include <analysis/modules/CurlTagger/SelectorCut.h>
25 #include <analysis/modules/CurlTagger/SelectorMVA.h>
26 
27 
28 using namespace Belle2;
29 
30 //-----------------------------------------------------------------
31 // Register the Module
32 //-----------------------------------------------------------------
33 REG_MODULE(CurlTagger)
34 
35 //-----------------------------------------------------------------
36 // Implementation
37 //-----------------------------------------------------------------
38 
40 {
41  // Set module properties
42  setDescription(
43  R"DOC("Curl Tagger is a tool designed to identify and tag extra tracks caused by low pt particles curling around the detector. For further documentation please see 'tagCurlTracks' in modularAnalysis.")DOC");
44 
45  // Parameter definitions
46  addParam("particleLists", m_ParticleLists, "input particle lists to check for curls or use for training");
47  addParam("belle", m_BelleFlag, "flag to distinuguish Belle (true) from Belle II (false) data", false);
48  addParam("ptCut", m_PtCut, "preselection pt Cut", 0.6);
49  addParam("selectorType", m_SelectorType,
50  "the name of the selector to use when deciding if two reconstructed particles are the same true particle, available : 'cut', 'mva'",
51  std::string("cut"));
52  addParam("mcTruth", m_McStatsFlag,
53  "additionally bundles the particles using their genParticleIndex and tags them with extraInfo(isTruthCurl) and extraInfo(truthBundleSize).",
54  false);
55  addParam("train", m_TrainFlag, "flag for training the MVA or other methods if needed", false);
56 
57  addParam("responseCut", m_ResponseCut, "minimum allowed selector response for a match.", 0.324);
58 }
59 
61 
63 {
64  if (Variable::particlePt(p) > m_PtCut) {return false;}
65  if (!(Variable::trackNCDCHits(p) > 0 || Variable::trackNVXDHits(p) > 0)) {return false;} //should never happen anyway but might as well check
66  if (p -> getCharge() == 0) {return false;}
67  return true;
68 }
69 
71 {
72  //initialise the selection function chosen by user
73  if (m_SelectorType.compare("cut") == 0) {
75  //Only really works for belle data right now
76  if (!m_BelleFlag) {
77  B2WARNING("Curl Tagger 'cut' selector is only calibrated for Belle");
78  }
79 
80  } else if (m_SelectorType.compare("mva") == 0) {
82  } else {
83  B2ERROR("Curl Track Tagger - Selector type does not exists.");
84  }
85 
86  //initialise the selector if it has an initialize function
88 }
89 
91 {
92 }
93 
95 {
96  for (auto& iList : m_ParticleLists) {
97  StoreObjPtr<ParticleList> particleList(iList);
98 
99  //check particle List exists and has particles
100  if (!particleList) {
101  B2ERROR("ParticleList " << iList << " not found");
102  continue;
103  }
104  unsigned int particleListSize = particleList -> getListSize();
105  if (particleListSize == 0) {
106  continue;
107  }
108 
109  // Classify
110  if (!m_TrainFlag) {
111  std::vector<CurlTagger::Bundle> bundles;
112  std::vector<CurlTagger::Bundle> truthBundles; //only used if mcstatsFlag is true but empty lists are basically free
113 
114  for (unsigned int i = 0; i < particleListSize; i++) {
115 
116  Particle* iPart = particleList -> getParticle(i);
117  iPart -> addExtraInfo("isCurl", 0);
118  iPart -> addExtraInfo("bundleSize", 0);
119  if (m_McStatsFlag) {
120  iPart -> addExtraInfo("isTruthCurl", 0);
121  iPart -> addExtraInfo("truthBundleSize", 0);
122  }
123  if (!passesPreSelection(iPart)) {continue;}
124 
125  bool addedParticleToBundle = false;
126  std::vector<float> bundlesResponse;
127 
128  for (CurlTagger::Bundle bundle : bundles) {
129  unsigned int bundleSize = bundle.size();
130  float averageResponse = 0;
131 
132  for (unsigned int b = 0; b < bundleSize; b++) {
133  Particle* bPart = bundle.getParticle(b);
134  averageResponse += m_Selector -> getResponse(iPart, bPart);
135  }
136 
137  averageResponse /= bundleSize;
138  bundlesResponse.push_back(averageResponse);
139  } //bundles
140 
141  if (bundlesResponse.size() > 0) {
142  auto maxElement = std::max_element(bundlesResponse.begin(), bundlesResponse.end());
143  if (*maxElement > m_ResponseCut) {
144  int maxPosition = std::distance(std::begin(bundlesResponse), maxElement);
145  bundles[maxPosition].addParticle(iPart);
146  addedParticleToBundle = true;
147  }
148  }
149 
150  if (!addedParticleToBundle) {
151  CurlTagger::Bundle tempBundle = CurlTagger::Bundle(false);
152  tempBundle.addParticle(iPart);
153  bundles.push_back(tempBundle);
154  }
155 
156  if (m_McStatsFlag) {
157  bool addedParticleToTruthBundle = false;
158  for (auto& truthBundle : truthBundles) {
159  Particle* bPart = truthBundle.getParticle(0);
160  if (Variable::genParticleIndex(iPart) == Variable::genParticleIndex(bPart)) {
161  truthBundle.addParticle(iPart);
162  addedParticleToTruthBundle = true;
163  break;
164  } // same genParticleIndex
165  } //truthBundles
166  if (!addedParticleToTruthBundle) {
167  CurlTagger::Bundle truthTempBundle = CurlTagger::Bundle(true);
168  truthTempBundle.addParticle(iPart);
169  truthBundles.push_back(truthTempBundle);
170  } //create new truth bundle
171  }//MCStatsFlag
172  } // iParticle
173  for (CurlTagger::Bundle bundle : bundles) {
174  bundle.tagCurlInfo();
175  if (m_McStatsFlag) {
176  bundle.tagSizeInfo();
177  }
178  }
179  if (m_McStatsFlag) {
180  for (CurlTagger::Bundle truthBundle : truthBundles) {
181  truthBundle.tagCurlInfo();
182  truthBundle.tagSizeInfo();
183  }
184  }
185  } else {// !TrainFlag
186  for (unsigned int i = 0; i < particleListSize; i++) {
187  Particle* iPart = particleList -> getParticle(i);
188  if (!passesPreSelection(iPart)) {continue;}
189 
190  for (unsigned int j = 0; j < particleListSize; j++) {
191  Particle* jPart = particleList -> getParticle(j);
192  if (i == j) {continue;}
193  if (!passesPreSelection(jPart)) {continue;}
194 
195  m_Selector->collectTrainingInfo(iPart, jPart);
196  } //jPart
197  } //iPart
198  } // Training events
199  } // particle Lists
200 }
201 
203 {
204 }
205 
207 {
208  m_Selector->finalize();
209  delete m_Selector;
210 }
This module is designed to tag curl tracks.
std::vector< std::string > m_ParticleLists
input particle lists
bool passesPreSelection(Particle *particle)
preselects particles that may be curl tracks
virtual void initialize() override
initialise
virtual void event() override
event code - all curl track selection done here
virtual void endRun() override
end run - unused
virtual void terminate() override
termination
virtual ~CurlTaggerModule() override
destructor
bool m_TrainFlag
switch between training and classifying
virtual void beginRun() override
begin run - unused
std::string m_SelectorType
name of selector function to use
bool m_McStatsFlag
if true also does some truth based matching and tags the particles with truthCurl info
double m_PtCut
preselection pt cut
bool m_BelleFlag
flags if data/mc comes from belle or belle II
CurlTagger::Selector * m_Selector
contains the selector used
double m_ResponseCut
min classifier response to consider a match
class to contain particles identified to come from the same actual/mc particle
Definition: Bundle.h:25
void addParticle(Particle *particle)
adds Particle to Bundle
Definition: Bundle.cc:31
Simple cut based selector for curl tracks taken from Belle note 1079.
Definition: SelectorCut.h:24
MVA based selector for tagging curl tracks in Belle and Belle II.
Definition: SelectorMVA.h:31
virtual void finalize()
finalise selector if needed
Definition: Selector.h:41
virtual void initialize()
initialise selector if needed
Definition: Selector.h:38
virtual void collectTrainingInfo(Particle *, Particle *)
collect information for training for mva or other selectors
Definition: Selector.h:44
Base class for Modules.
Definition: Module.h:72
Class to store reconstructed particles.
Definition: Particle.h:74
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
Abstract base class for different kinds of events.