Belle II Software development
SelectorMVA Class Reference

MVA based selector for tagging curl tracks in Belle and Belle II. More...

#include <SelectorMVA.h>

Inheritance diagram for SelectorMVA:
Selector

Public Member Functions

 SelectorMVA (bool belleFlag, bool trainFlag, std::string tFileName)
 Constructor.
 
 ~SelectorMVA ()
 Destructor.
 
virtual float getResponse (Particle *iPart, Particle *jPart) override
 Selector response that this pair of particles come from the same mc/actual particle.
 
virtual float getOptimalResponseCut () override
 returns optimal cut to use with selector
 
virtual std::vector< float > getVariables (Particle *iPart, Particle *jPart) override
 returns vector of variables used by this selector.
 
virtual void initialize () override
 initialize whatever needs to be initialized (root file etc)

 
virtual void collectTrainingInfo (Particle *iPart, Particle *jPart) override
 collect training data and save to a root file
 
virtual void finalize () override
 finalize whatever needs to be finalized (train the MVA)
 
void initializeMVA ()
 initialize the MVA Expert
 

Private Member Functions

void updateVariables (Particle *iPart, Particle *jPart)
 updates the value of the MVA variable
 

Private Attributes

bool m_TrainFlag
 applying mva or training it
 
std::string m_TFileName
 name of output file for training data
 
TFile * m_TFile
 output file for training data
 
TTree * m_TTree
 training data tree
 
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
 Database pointer to the Database representation of the weightfile.
 
MVA::Weightfile m_weightfile
 mva weightfile
 
MVA::GeneralOptions m_generalOptions
 mva general options (for the expert)
 
MVA::FastBDTExpert m_expert
 mva expert
 
std::string m_identifier
 mva identifier
 
std::vector< std::string > m_datafiles
 name of datafile
 
std::vector< std::string > m_variables
 names of variables used by mva
 
std::string m_target_variable
 name of target variable (isCurl)
 
Float_t m_PPhi
 angle between particle momentum vectors
 
Float_t m_ChargeProduct
 charge(p1) * charge(p2)
 
Float_t m_PtDiffEW
 error weighted particle Pt difference
 
Float_t m_PzDiffEW
 error weighted particle Pz difference
 
Float_t m_TrackD0DiffEW
 error weighted track D0 difference
 
Float_t m_TrackZ0DiffEW
 error weighted track Z0 difference
 
Float_t m_TrackTanLambdaDiffEW
 error weighted track tan lambda diff difference
 
Float_t m_TrackPhi0DiffEW
 error weighted track Phi0 difference
 
Float_t m_TrackOmegaDiffEW
 error weighted track Omega difference
 
Bool_t m_IsCurl
 isCurl Truth
 

Detailed Description

MVA based selector for tagging curl tracks in Belle and Belle II.

Definition at line 34 of file SelectorMVA.h.

Constructor & Destructor Documentation

◆ SelectorMVA()

SelectorMVA ( bool  belleFlag,
bool  trainFlag,
std::string  tFileName 
)

Constructor.

Definition at line 20 of file SelectorMVA.cc.

21{
22 m_TrainFlag = trainFlag;
23 m_TFileName = tFileName;
24 if (belleFlag) {
25 m_identifier = "CurlTagger_FastBDT_Belle";
26 } else {
27 m_identifier = "CurlTagger_FastBDT_BelleII";
28 }
29}
bool m_TrainFlag
applying mva or training it
Definition: SelectorMVA.h:70
std::string m_TFileName
name of output file for training data
Definition: SelectorMVA.h:73
std::string m_identifier
mva identifier
Definition: SelectorMVA.h:95

Member Function Documentation

◆ collectTrainingInfo()

void collectTrainingInfo ( Particle iPart,
Particle jPart 
)
overridevirtual

collect training data and save to a root file

Reimplemented from Selector.

Definition at line 73 of file SelectorMVA.cc.

74{
75 updateVariables(iPart, jPart);
76 m_TTree -> Fill();
77}
void updateVariables(Particle *iPart, Particle *jPart)
updates the value of the MVA variable
Definition: SelectorMVA.cc:33
TTree * m_TTree
training data tree
Definition: SelectorMVA.h:79

◆ finalize()

void finalize ( )
overridevirtual

finalize whatever needs to be finalized (train the MVA)

Reimplemented from Selector.

Definition at line 135 of file SelectorMVA.cc.

136{
137 if (m_TrainFlag) {
138 m_TFile -> cd();
139 m_TTree -> Write();
140 m_TFile -> Write();
141 m_TFile -> Close();
142 }
143}
TFile * m_TFile
output file for training data
Definition: SelectorMVA.h:76

◆ getOptimalResponseCut()

float getOptimalResponseCut ( )
overridevirtual

returns optimal cut to use with selector

Reimplemented from Selector.

Definition at line 122 of file SelectorMVA.cc.

123{
124 if (m_TrainFlag) {
125 return 0.5;
126 }
127 std::string elementIdentfier = "optimal_cut";
128 if (!m_weightfile.containsElement(elementIdentfier)) {
129 B2FATAL("No optimal cut stored in curlTagger MVA payload!");
130 }
131 // require the default value for the compiler to deduce the template class
132 return m_weightfile.getElement(elementIdentfier, 0.5);
133}
MVA::Weightfile m_weightfile
mva weightfile
Definition: SelectorMVA.h:85
T getElement(const std::string &identifier) const
Returns a stored element from the xml tree.
Definition: Weightfile.h:151
bool containsElement(const std::string &identifier) const
Returns true if given element is stored in the property tree.
Definition: Weightfile.h:160

◆ getResponse()

float getResponse ( Particle iPart,
Particle jPart 
)
overridevirtual

Selector response that this pair of particles come from the same mc/actual particle.

Implements Selector.

Definition at line 145 of file SelectorMVA.cc.

146{
147 MVA::SingleDataset dataset(m_generalOptions, getVariables(iPart, jPart));
148 return m_expert.apply(dataset)[0];
149}
virtual std::vector< float > getVariables(Particle *iPart, Particle *jPart) override
returns vector of variables used by this selector.
Definition: SelectorMVA.cc:65
MVA::FastBDTExpert m_expert
mva expert
Definition: SelectorMVA.h:91
MVA::GeneralOptions m_generalOptions
mva general options (for the expert)
Definition: SelectorMVA.h:88
virtual std::vector< float > apply(Dataset &test_data) const override
Apply this expert onto a dataset.
Definition: FastBDT.cc:251
Wraps the data of a single event into a Dataset.
Definition: Dataset.h:135

◆ getVariables()

std::vector< float > getVariables ( Particle iPart,
Particle jPart 
)
overridevirtual

returns vector of variables used by this selector.

Implements Selector.

Definition at line 65 of file SelectorMVA.cc.

66{
67 updateVariables(iPart, jPart);
71}
Float_t m_TrackTanLambdaDiffEW
error weighted track tan lambda diff difference
Definition: SelectorMVA.h:127
Float_t m_TrackZ0DiffEW
error weighted track Z0 difference
Definition: SelectorMVA.h:124
Float_t m_PtDiffEW
error weighted particle Pt difference
Definition: SelectorMVA.h:115
Float_t m_PPhi
angle between particle momentum vectors
Definition: SelectorMVA.h:109
Float_t m_TrackPhi0DiffEW
error weighted track Phi0 difference
Definition: SelectorMVA.h:130
Float_t m_PzDiffEW
error weighted particle Pz difference
Definition: SelectorMVA.h:118
Float_t m_TrackD0DiffEW
error weighted track D0 difference
Definition: SelectorMVA.h:121
Float_t m_ChargeProduct
charge(p1) * charge(p2)
Definition: SelectorMVA.h:112
Float_t m_TrackOmegaDiffEW
error weighted track Omega difference
Definition: SelectorMVA.h:133

◆ initialize()

void initialize ( )
overridevirtual

initialize whatever needs to be initialized (root file etc)

Reimplemented from Selector.

Definition at line 79 of file SelectorMVA.cc.

80{
81 if (m_TrainFlag) {
82 //make training data
83 m_TFile = TFile::Open(m_TFileName.c_str(), "RECREATE");
84 m_TTree = new TTree("ntuple", "Training Data for the Curl Tagger MVA");
85
86 m_TTree -> Branch("PPhi", &m_PPhi, "PPhi/F");
87 m_TTree -> Branch("ChargeProduct", &m_ChargeProduct, "ChargeProduct/F");
88 m_TTree -> Branch("PtDiffEW", &m_PtDiffEW, "PtDiffEW/F");
89 m_TTree -> Branch("PzDiffEW", &m_PzDiffEW, "PzDiffEW/F");
90 m_TTree -> Branch("TrackD0DiffEW", &m_TrackD0DiffEW, "TrackD0DiffEW/F");
91 m_TTree -> Branch("TrackZ0DiffEW", &m_TrackZ0DiffEW, "TrackZ0DiffEW/F");
92 m_TTree -> Branch("TrackTanLambdaDiffEW", &m_TrackTanLambdaDiffEW, "TrackTanLambdaDiffEW/F");
93 m_TTree -> Branch("TrackPhi0DiffEW", &m_TrackPhi0DiffEW, "TrackPhi0DiffEW/F");
94 m_TTree -> Branch("TrackOmegaDiffEW", &m_TrackOmegaDiffEW, "TrackOmegaDiffEW/F");
95
96 m_TTree -> Branch("IsCurl", &m_IsCurl, "IsCurl/O");
97
98 m_target_variable = "IsCurl";
99 m_variables = {"PPhi", "ChargeProduct", "PtDiffEW",
100 "PzDiffEW", "TrackD0DiffEW", "TrackZ0DiffEW",
101 "TrackTanLambdaDiffEW", "TrackPhi0DiffEW", "TrackOmegaDiffEW"
102 };
103
104 } else {
105 // normal application
106 m_weightfile_representation = std::make_unique<DBObjPtr<DatabaseRepresentationOfWeightfile>>(
107 MVA::makeSaveForDatabase(m_identifier));
108 (*m_weightfile_representation.get()).addCallback([this]() { initializeMVA();});
110 }
111}
std::vector< std::string > m_variables
names of variables used by mva
Definition: SelectorMVA.h:101
void initializeMVA()
initialize the MVA Expert
Definition: SelectorMVA.cc:113
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the Database representation of the weightfile.
Definition: SelectorMVA.h:82
Bool_t m_IsCurl
isCurl Truth
Definition: SelectorMVA.h:136
std::string m_target_variable
name of target variable (isCurl)
Definition: SelectorMVA.h:104

◆ initializeMVA()

void initializeMVA ( )

initialize the MVA Expert

Definition at line 113 of file SelectorMVA.cc.

114{
115 std::stringstream ss((*m_weightfile_representation)->m_data);
119}
virtual void load(Weightfile &weightfile) override
Load the expert from a Weightfile.
Definition: FastBDT.cc:192
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Definition: Weightfile.cc:251
void getOptions(Options &options) const
Fills an Option object from the xml tree.
Definition: Weightfile.cc:67

◆ updateVariables()

void updateVariables ( Particle iPart,
Particle jPart 
)
private

updates the value of the MVA variable

Definition at line 33 of file SelectorMVA.cc.

34{
35 if (m_TrainFlag) {
36 m_IsCurl = Variable::genParticleIndex(iPart) == Variable::genParticleIndex(jPart);
37 }
38 m_ChargeProduct = iPart->getCharge() * jPart->getCharge();
39
40 m_PPhi = acos(iPart->getMomentum().Unit().Dot(jPart->getMomentum().Unit()));
41
42 m_PtDiffEW = std::abs(Variable::particlePt(iPart) - Variable::particlePt(jPart)) / sqrt(pow(Variable::particlePtErr(
43 iPart), 2) + pow(Variable::particlePtErr(jPart), 2));
44
45 m_PzDiffEW = std::abs(Variable::particlePz(iPart) - Variable::particlePz(jPart)) / sqrt(pow(Variable::particlePzErr(
46 iPart), 2) + pow(Variable::particlePzErr(jPart), 2));
47
48 m_TrackD0DiffEW = std::abs(Variable::trackD0(iPart) - Variable::trackD0(jPart)) / sqrt(pow(Variable::trackD0Error(
49 iPart), 2) + pow(Variable::trackD0Error(jPart), 2));
50
51 m_TrackZ0DiffEW = std::abs(Variable::trackZ0(iPart) - Variable::trackZ0(jPart)) / sqrt(pow(Variable::trackZ0Error(
52 iPart), 2) + pow(Variable::trackZ0Error(jPart), 2));
53
54 m_TrackTanLambdaDiffEW = std::abs(Variable::trackTanLambda(iPart) - Variable::trackTanLambda(jPart)) / sqrt(pow(
55 Variable::trackTanLambdaError(
56 iPart), 2) + pow(Variable::trackTanLambdaError(jPart), 2));
57
58 m_TrackPhi0DiffEW = std::abs(Variable::trackPhi0(iPart) - Variable::trackPhi0(jPart)) / sqrt(pow(Variable::trackPhi0Error(
59 iPart), 2) + pow(Variable::trackPhi0Error(jPart), 2));
60
61 m_TrackOmegaDiffEW = std::abs(Variable::trackOmega(iPart) - Variable::trackOmega(jPart)) / sqrt(pow(Variable::trackOmegaError(
62 iPart), 2) + pow(Variable::trackOmegaError(jPart), 2));
63}
double getCharge(void) const
Returns particle charge.
Definition: Particle.cc:653
ROOT::Math::XYZVector getMomentum() const
Returns momentum vector.
Definition: Particle.h:580
double sqrt(double a)
sqrt for double
Definition: beamHelpers.h:28

Member Data Documentation

◆ m_ChargeProduct

Float_t m_ChargeProduct
private

charge(p1) * charge(p2)

Definition at line 112 of file SelectorMVA.h.

◆ m_datafiles

std::vector<std::string> m_datafiles
private

name of datafile

Definition at line 98 of file SelectorMVA.h.

◆ m_expert

MVA::FastBDTExpert m_expert
private

mva expert

Definition at line 91 of file SelectorMVA.h.

◆ m_generalOptions

MVA::GeneralOptions m_generalOptions
private

mva general options (for the expert)

Definition at line 88 of file SelectorMVA.h.

◆ m_identifier

std::string m_identifier
private

mva identifier

Definition at line 95 of file SelectorMVA.h.

◆ m_IsCurl

Bool_t m_IsCurl
private

isCurl Truth

Definition at line 136 of file SelectorMVA.h.

◆ m_PPhi

Float_t m_PPhi
private

angle between particle momentum vectors

Definition at line 109 of file SelectorMVA.h.

◆ m_PtDiffEW

Float_t m_PtDiffEW
private

error weighted particle Pt difference

Definition at line 115 of file SelectorMVA.h.

◆ m_PzDiffEW

Float_t m_PzDiffEW
private

error weighted particle Pz difference

Definition at line 118 of file SelectorMVA.h.

◆ m_target_variable

std::string m_target_variable
private

name of target variable (isCurl)

Definition at line 104 of file SelectorMVA.h.

◆ m_TFile

TFile* m_TFile
private

output file for training data

Definition at line 76 of file SelectorMVA.h.

◆ m_TFileName

std::string m_TFileName
private

name of output file for training data

Definition at line 73 of file SelectorMVA.h.

◆ m_TrackD0DiffEW

Float_t m_TrackD0DiffEW
private

error weighted track D0 difference

Definition at line 121 of file SelectorMVA.h.

◆ m_TrackOmegaDiffEW

Float_t m_TrackOmegaDiffEW
private

error weighted track Omega difference

Definition at line 133 of file SelectorMVA.h.

◆ m_TrackPhi0DiffEW

Float_t m_TrackPhi0DiffEW
private

error weighted track Phi0 difference

Definition at line 130 of file SelectorMVA.h.

◆ m_TrackTanLambdaDiffEW

Float_t m_TrackTanLambdaDiffEW
private

error weighted track tan lambda diff difference

Definition at line 127 of file SelectorMVA.h.

◆ m_TrackZ0DiffEW

Float_t m_TrackZ0DiffEW
private

error weighted track Z0 difference

Definition at line 124 of file SelectorMVA.h.

◆ m_TrainFlag

bool m_TrainFlag
private

applying mva or training it

Definition at line 70 of file SelectorMVA.h.

◆ m_TTree

TTree* m_TTree
private

training data tree

Definition at line 79 of file SelectorMVA.h.

◆ m_variables

std::vector<std::string> m_variables
private

names of variables used by mva

Definition at line 101 of file SelectorMVA.h.

◆ m_weightfile

MVA::Weightfile m_weightfile
private

mva weightfile

Definition at line 85 of file SelectorMVA.h.

◆ m_weightfile_representation

std::unique_ptr<DBObjPtr<DatabaseRepresentationOfWeightfile> > m_weightfile_representation
private

Database pointer to the Database representation of the weightfile.

Definition at line 82 of file SelectorMVA.h.


The documentation for this class was generated from the following files: