Belle II Software development
ClusterInfoExtractor.h
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#pragma once
10#include <tracking/spacePointCreation/SpacePoint.h>
11#include <svd/dataobjects/SVDCluster.h>
12#include <tracking/trackingUtilities/utilities/Named.h>
13#include <tracking/trackFindingVXD/variableExtractors/VariableExtractor.h>
14#include <numeric>
15
16
17namespace Belle2 {
24 public:
30 ClusterInfoExtractor(std::vector<TrackingUtilities::Named<float*>>& variableSet, bool useTimingInfo,
31 const std::string& prefix = "") :
32 VariableExtractor(), m_UseTimingInfo(useTimingInfo), m_prefix(prefix)
33 {
34 initializeStats(m_prefix + "charge", variableSet);
35 initializeStats(m_prefix + "seedCharge", variableSet);
36 initializeStats(m_prefix + "size", variableSet);
37 initializeStats(m_prefix + "energyLoss", variableSet);
38 if (m_UseTimingInfo) {
39 initializeStats(m_prefix + "time", variableSet);
40 initializeStats(m_prefix + "timeSigma", variableSet);
41 }
42
43 }
44
46 void extractVariables(std::vector<SpacePoint const*> const& spacePoints)
47 {
48 std::vector<SVDCluster const*> clusters;
49 clusters.reserve(spacePoints.size() * 2);
50
51 for (SpacePoint const* sp : spacePoints) {
52 RelationVector<SVDCluster> relatedClusters = sp->getRelationsTo<SVDCluster>("");
53 for (const SVDCluster& cluster : relatedClusters) {
54 clusters.push_back(&cluster);
55 }
56 }
57
58 // this is fine as it is pointing to the `relatedClusters`, which are in the datastore..
59 std::vector<float> values(clusters.size());
60 for (unsigned int i = 0; i < clusters.size(); ++i) {
61 values[i] = clusters[i]->getCharge();
62 }
63 setStats(m_prefix + "charge", values);
64
65 for (unsigned int i = 0; i < clusters.size(); ++i) {
66 values[i] = clusters[i]->getSeedCharge();
67 }
68 setStats(m_prefix + "seedCharge", values);
69
70
71 for (unsigned int i = 0; i < clusters.size(); ++i) {
72 values[i] = clusters[i]->getSize();
73 }
74 setStats(m_prefix + "size", values);
75
76
77 for (unsigned int i = 0; i < clusters.size(); ++i) {
78 values[i] = clusters[i]->getCharge() / clusters[i]->getSize();
79 }
80 setStats(m_prefix + "energyLoss", values);
81
82 if (m_UseTimingInfo) {
83 for (unsigned int i = 0; i < clusters.size(); ++i) {
84 values[i] = clusters[i]->getClsTime();
85 }
86 setStats(m_prefix + "time", values);
87
88 for (unsigned int i = 0; i < clusters.size(); ++i) {
89 values[i] = clusters[i]->getClsTimeSigma();
90 }
91 setStats(m_prefix + "timeSigma", values);
92 }
93
94 }
95
96 protected:
100 std::string m_prefix;
101
103 void initializeStats(const std::string& identifier, std::vector<TrackingUtilities::Named<float*>>& variables)
104 {
105 addVariable(identifier + "_max", variables);
106 addVariable(identifier + "_min", variables);
107 addVariable(identifier + "_mean", variables);
108 addVariable(identifier + "_std", variables);
109 addVariable(identifier + "_sum", variables);
110 }
111
113 void setStats(const std::string& identifier, std::vector<float>& values)
114 {
115 short size = values.size();
116 if (values.size() == 0) {
117 m_variables.at(identifier + "_max") = NAN;
118 m_variables.at(identifier + "_min") = NAN;
119 m_variables.at(identifier + "_mean") = NAN;
120 m_variables.at(identifier + "_std") = NAN;
121 m_variables.at(identifier + "_sum") = NAN;
122 return;
123 }
124 // mean
125 float sum = std::accumulate(values.begin(), values.end(), 0.0);
126 m_variables.at(identifier + "_sum") = sum;
127 float mean = sum / size;
128 m_variables.at(identifier + "_mean") = mean;
129 // variance and standard deviation
130 float variance = std::accumulate(values.begin(), values.end(), 0.0,
131 [mean, size](float x, float y) {return x + ((y - mean) * (y - mean)) / (size - 1);});
132 float stddev = std::sqrt(variance);
133 m_variables.at(identifier + "_std") = stddev;
134 //min and max
135 float min = *(std::min_element(values.begin(), values.end()));
136 float max = *(std::max_element(values.begin(), values.end()));
137 m_variables.at(identifier + "_min") = min;
138 m_variables.at(identifier + "_max") = max;
139 }
140
141 };
142
143}
void extractVariables(std::vector< SpacePoint const * > const &spacePoints)
extract variables from SpacePoints
std::string m_prefix
prefix that will be added before the variable names
bool m_UseTimingInfo
whether to use timing info from cluster
ClusterInfoExtractor(std::vector< TrackingUtilities::Named< float * > > &variableSet, bool useTimingInfo, const std::string &prefix="")
Constructor fills variableSet with variables to be extracted.
void setStats(const std::string &identifier, std::vector< float > &values)
calculated statistics and saves them in variable set
void initializeStats(const std::string &identifier, std::vector< TrackingUtilities::Named< float * > > &variables)
initialize statistics subsets of variables from clusters that get combined for SPTC
Class for type safe access to objects that are referred to in relations.
The SVD Cluster class This class stores all information about reconstructed SVD clusters.
Definition SVDCluster.h:29
SpacePoint typically is build from 1 PXDCluster or 1-2 SVDClusters.
Definition SpacePoint.h:42
A mixin class to attach a name to an object.
Definition Named.h:23
class to extract individual variables
std::unordered_map< std::string, float > m_variables
unordered_map to associate float value with a string name
void addVariable(const std::string &identifier, std::vector< TrackingUtilities::Named< float * > > &variables)
add a variable to the variable set
Abstract base class for different kinds of events.