Belle II Software  release-05-02-19
FastBDTClassifierAnalyzerModule.cc
1 /*************************************************************************
2 * BASF2 (Belle Analysis Framework 2) *
3 * Copyright(C) 2016 - Belle II Collaboration *
4 * *
5 * Author: The Belle II Collaboration *
6 * Contributors: Thomas Madlener *
7 * *
8 * This software is provided "as is" without any warranty. *
9 **************************************************************************/
10 
11 #include <fstream>
12 #include <TFile.h>
13 #include <TTree.h>
14 
15 #include <tracking/modules/vxdtfRedesign/FastBDTClassifierAnalyzerModule.h>
16 #include <tracking/spacePointCreation/MapHelperFunctions.h>
17 
18 using namespace Belle2;
19 
20 REG_MODULE(FastBDTClassifierAnalyzer);
21 
23 {
25  "analyzes performance of given FastBDT on a test and a training set and determines a global classification cut. TODO");
26 
27  addParam("fbdtFileName", m_PARAMfbdtFileName, "file name of the fbdtclassifier");
28  addParam("trainSamples", m_PARAMtrainSampleFileName, "filename of the training samples");
29  addParam("testSamples", m_PARAMtestSampleFileName, "filename of the test samples");
30  addParam("outputFileName", m_PARAMrootOutFileName, "output filename", std::string("FBDTAnalyzer_out.root"));
31 }
32 
34 {
35  std::ifstream fbdt(m_PARAMfbdtFileName);
36  if (!fbdt.is_open()) {
37  B2ERROR("Could not open file: " << m_PARAMfbdtFileName << ".");
38  }
39 
40  std::ifstream train(m_PARAMtrainSampleFileName);
41  if (!train.is_open()) {
42  B2ERROR("Could not open file: " << m_PARAMtrainSampleFileName << ".");
43  }
44 
45  std::ifstream test(m_PARAMtestSampleFileName);
46  if (!test.is_open()) {
47  B2ERROR("Could not open file: " << m_PARAMtestSampleFileName << ".");
48  }
49 
50  B2DEBUG(1, "Reading Classifier from file: " << m_PARAMfbdtFileName << ".");
52  fbdt.close();
53  B2DEBUG(1, "Done");
54 
55  B2DEBUG(1, "Reading training samples from file: " << m_PARAMtrainSampleFileName << ".");
57  train.close();
58 
59  B2DEBUG(1, "Reading training samples from file: " << m_PARAMtestSampleFileName << ".");
61  test.close();
62 }
63 
65 {
66  std::ofstream ofs("analyze_trout.dat");
67  B2DEBUG(10, "Processing the training sample");
68  for (const auto& event : m_trainSample) {
69  m_trainOutput.insert(std::make_pair(event.signal, m_classifier.analyze(event.hits)));
70  ofs << event.signal << " " << m_classifier.analyze(event.hits) << std::endl;
71  }
72  ofs.close();
73 
74  B2DEBUG(10, "Processing the test sample");
75  for (const auto& event : m_testSample) {
76  m_testOutput.insert(std::make_pair(event.signal, m_classifier.analyze(event.hits)));
77  }
78 
79  auto trainBgOut = getValuesToKey(m_trainOutput, 0);
80  auto trainSigOut = getValuesToKey(m_trainOutput, 1);
81 
82  auto testBgOut = getValuesToKey(m_testOutput, 0);
83  auto testSigOut = getValuesToKey(m_testOutput, 1);
84 
85  TFile* outfile = new TFile(m_PARAMrootOutFileName.c_str(), "RECREATE");
86  TTree* tree = new TTree("classifierOutputs", "outputs of FBDTClassifier for the different samples");
87  tree->Branch("train_bg_outputs", &trainBgOut);
88  tree->Branch("train_sig_outputs", &trainSigOut);
89  tree->Branch("test_bg_outputs", &testBgOut);
90  tree->Branch("test_sig_outputs", &testSigOut);
91 
92  tree->Fill();
93  outfile->cd();
94  outfile->Write();
95  outfile->Close();
96 }
Belle2::FBDTClassifier::analyze
double analyze(const std::array< double, Ndims > &hits) const
calculate the output of the FastBDT.
Definition: FBDTClassifier.h:102
Belle2::Module::setDescription
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:216
Belle2::readSamplesFromStream
static void readSamplesFromStream(std::istream &is, std::vector< FBDTTrainSample< Ndims > > &samples)
read samples from stream and append them to samples
Definition: FBDTClassifierHelper.h:52
REG_MODULE
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:652
Belle2::FastBDTClassifierAnalyzerModule::m_PARAMrootOutFileName
std::string m_PARAMrootOutFileName
output file name
Definition: FastBDTClassifierAnalyzerModule.h:58
Belle2::Module::event
virtual void event()
This method is the core of the module.
Definition: Module.h:159
Belle2::FastBDTClassifierAnalyzerModule::initialize
void initialize() override
Module initialization.
Definition: FastBDTClassifierAnalyzerModule.cc:33
Belle2::FastBDTClassifierAnalyzerModule::m_classifier
Belle2::FBDTClassifier< 9 > m_classifier
classifier
Definition: FastBDTClassifierAnalyzerModule.h:62
Belle2::Module
Base class for Modules.
Definition: Module.h:74
Belle2::FBDTClassifier::readFromStream
void readFromStream(std::istream &is)
read all the necessary data from stream and fill the Forest and the FeatureBinnings NOTE: uses FastBD...
Definition: FBDTClassifier.cc:24
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::FastBDTClassifierAnalyzerModule::m_PARAMfbdtFileName
std::string m_PARAMfbdtFileName
weight file name
Definition: FastBDTClassifierAnalyzerModule.h:55
Belle2::FastBDTClassifierAnalyzerModule::m_PARAMtrainSampleFileName
std::string m_PARAMtrainSampleFileName
training sample file name
Definition: FastBDTClassifierAnalyzerModule.h:56
Belle2::FastBDTClassifierAnalyzerModule::m_trainOutput
std::multimap< int, double > m_trainOutput
map containing output for each training event
Definition: FastBDTClassifierAnalyzerModule.h:63
Belle2::FastBDTClassifierAnalyzerModule::m_trainSample
std::vector< TrainSample > m_trainSample
vector for training sample
Definition: FastBDTClassifierAnalyzerModule.h:60
Belle2::FastBDTClassifierAnalyzerModule::m_testSample
std::vector< TrainSample > m_testSample
vector for test sample
Definition: FastBDTClassifierAnalyzerModule.h:61
Belle2::Module::addParam
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:562
Belle2::FastBDTClassifierAnalyzerModule::terminate
void terminate() override
Module termination.
Definition: FastBDTClassifierAnalyzerModule.cc:64
Belle2::FastBDTClassifierAnalyzerModule::m_PARAMtestSampleFileName
std::string m_PARAMtestSampleFileName
test sample file name
Definition: FastBDTClassifierAnalyzerModule.h:57
Belle2::FastBDTClassifierAnalyzerModule::FastBDTClassifierAnalyzerModule
FastBDTClassifierAnalyzerModule()
Constructor.
Definition: FastBDTClassifierAnalyzerModule.cc:22
Belle2::getValuesToKey
std::vector< typename MapType::mapped_type > getValuesToKey(const MapType &aMap, typename MapType::key_type aKey)
get all values stored in the map for a given key
Definition: MapHelperFunctions.h:89
Belle2::FastBDTClassifierAnalyzerModule::m_testOutput
std::multimap< int, double > m_testOutput
map containing output for each test event
Definition: FastBDTClassifierAnalyzerModule.h:64