Belle II Software development
SelectorMVA.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/SelectorMVA.h>
10
11#include <analysis/variables/TrackVariables.h>
12#include <analysis/variables/MCTruthVariables.h>
13#include <analysis/variables/Variables.h>
14
15#include <cmath>
16
17using namespace Belle2;
18using namespace CurlTagger;
19
20SelectorMVA::SelectorMVA(bool belleFlag, bool trainFlag, std::string tFileName)
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}
30
32
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}
64
72
74{
75 updateVariables(iPart, jPart);
76 m_TTree -> Fill();
77}
78
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}
112
114{
115 std::stringstream ss((*m_weightfile_representation)->m_data);
117 m_weightfile.getOptions(m_generalOptions);
119}
120
121
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}
134
136{
137 if (m_TrainFlag) {
138 m_TFile -> cd();
139 m_TTree -> Write();
140 m_TFile -> Write();
141 m_TFile -> Close();
142 }
143}
144
146{
147 MVA::SingleDataset dataset(m_generalOptions, getVariables(iPart, jPart));
148 return m_expert.apply(dataset)[0];
149}
void updateVariables(Particle *iPart, Particle *jPart)
updates the value of the MVA variable
virtual std::vector< float > getVariables(Particle *iPart, Particle *jPart) override
returns vector of variables used by this selector.
Float_t m_TrackTanLambdaDiffEW
error weighted track tan lambda diff difference
virtual float getOptimalResponseCut() override
returns optimal cut to use with selector
std::vector< std::string > m_variables
names of variables used by mva
Float_t m_TrackZ0DiffEW
error weighted track Z0 difference
virtual void initialize() override
initialize whatever needs to be initialized (root file etc)
Float_t m_PtDiffEW
error weighted particle Pt difference
Float_t m_PPhi
angle between particle momentum vectors
Float_t m_TrackPhi0DiffEW
error weighted track Phi0 difference
TFile * m_TFile
output file for training data
Definition SelectorMVA.h:76
Float_t m_PzDiffEW
error weighted particle Pz difference
MVA::Weightfile m_weightfile
mva weightfile
Definition SelectorMVA.h:85
Float_t m_TrackD0DiffEW
error weighted track D0 difference
virtual void finalize() override
finalize whatever needs to be finalized (train the MVA)
Float_t m_ChargeProduct
charge(p1) * charge(p2)
void initializeMVA()
initialize the MVA Expert
std::unique_ptr< DBObjPtr< DatabaseRepresentationOfWeightfile > > m_weightfile_representation
Database pointer to the Database representation of the weightfile.
Definition SelectorMVA.h:82
bool m_TrainFlag
applying mva or training it
Definition SelectorMVA.h:70
MVA::FastBDTExpert m_expert
mva expert
Definition SelectorMVA.h:91
virtual void collectTrainingInfo(Particle *iPart, Particle *jPart) override
collect training data and save to a root file
std::string m_TFileName
name of output file for training data
Definition SelectorMVA.h:73
virtual float getResponse(Particle *iPart, Particle *jPart) override
Selector response that this pair of particles come from the same mc/actual particle.
MVA::GeneralOptions m_generalOptions
mva general options (for the expert)
Definition SelectorMVA.h:88
std::string m_target_variable
name of target variable (isCurl)
SelectorMVA(bool belleFlag, bool trainFlag, std::string tFileName)
Constructor.
Float_t m_TrackOmegaDiffEW
error weighted track Omega difference
TTree * m_TTree
training data tree
Definition SelectorMVA.h:79
std::string m_identifier
mva identifier
Definition SelectorMVA.h:95
Wraps the data of a single event into a Dataset.
Definition Dataset.h:135
static Weightfile loadFromStream(std::istream &stream)
Static function which deserializes a Weightfile from a stream.
Class to store reconstructed particles.
Definition Particle.h:76
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
Abstract base class for different kinds of events.