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