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:
29 ClusterInfoExtractor(std::vector<TrackingUtilities::Named<float*>>& variableSet, bool useTimingInfo) :
30 VariableExtractor(), m_UseTimingInfo(useTimingInfo)
31 {
32 initializeStats("charge", variableSet);
33 initializeStats("seedCharge", variableSet);
34 initializeStats("size", variableSet);
35 initializeStats("energyLoss", variableSet);
36 if (m_UseTimingInfo) {
37 initializeStats("time", variableSet);
38 initializeStats("timeSigma", variableSet);
39 }
40
41 }
42
44 void extractVariables(std::vector<SpacePoint const*> const& spacePoints)
45 {
46 std::vector<SVDCluster const*> clusters;
47 clusters.reserve(spacePoints.size() * 2);
48
49 for (SpacePoint const* sp : spacePoints) {
50 RelationVector<SVDCluster> relatedClusters = sp->getRelationsTo<SVDCluster>("");
51 for (const SVDCluster& cluster : relatedClusters) {
52 clusters.push_back(&cluster);
53 }
54 }
55
56 // this is fine as it is pointing to the `relatedClusters`, which are in the datastore..
57 // cppcheck-suppress invalidLifetime
58 std::vector<float> values(clusters.size());
59 for (unsigned int i = 0; i < clusters.size(); ++i) {
60 values[i] = clusters[i]->getCharge();
61 }
62 setStats("charge", values);
63
64 for (unsigned int i = 0; i < clusters.size(); ++i) {
65 values[i] = clusters[i]->getSeedCharge();
66 }
67 setStats("seedCharge", values);
68
69
70 for (unsigned int i = 0; i < clusters.size(); ++i) {
71 values[i] = clusters[i]->getSize();
72 }
73 setStats("size", values);
74
75
76 for (unsigned int i = 0; i < clusters.size(); ++i) {
77 values[i] = clusters[i]->getCharge() / clusters[i]->getSize();
78 }
79 setStats("energyLoss", values);
80
81 if (m_UseTimingInfo) {
82 for (unsigned int i = 0; i < clusters.size(); ++i) {
83 values[i] = clusters[i]->getClsTime();
84 }
85 setStats("time", values);
86
87 for (unsigned int i = 0; i < clusters.size(); ++i) {
88 values[i] = clusters[i]->getClsTimeSigma();
89 }
90 setStats("timeSigma", values);
91 }
92
93 }
94
95 protected:
98
100 void initializeStats(const std::string& identifier, std::vector<TrackingUtilities::Named<float*>>& variables)
101 {
102 addVariable(identifier + "_max", variables);
103 addVariable(identifier + "_min", variables);
104 addVariable(identifier + "_mean", variables);
105 addVariable(identifier + "_std", variables);
106 addVariable(identifier + "_sum", variables);
107 }
108
110 void setStats(const std::string& identifier, std::vector<float>& values)
111 {
112 short size = values.size();
113 if (values.size() == 0) {
114 m_variables.at(identifier + "_max") = NAN;
115 m_variables.at(identifier + "_min") = NAN;
116 m_variables.at(identifier + "_mean") = NAN;
117 m_variables.at(identifier + "_std") = NAN;
118 m_variables.at(identifier + "_sum") = NAN;
119 return;
120 }
121 // mean
122 float sum = std::accumulate(values.begin(), values.end(), 0.0);
123 m_variables.at(identifier + "_sum") = sum;
124 float mean = sum / size;
125 m_variables.at(identifier + "_mean") = mean;
126 // variance and standard deviation
127 float variance = std::accumulate(values.begin(), values.end(), 0.0,
128 [mean, size](float x, float y) {return x + ((y - mean) * (y - mean)) / (size - 1);});
129 float stddev = std::sqrt(variance);
130 m_variables.at(identifier + "_std") = stddev;
131 //min and max
132 float min = *(std::min_element(values.begin(), values.end()));
133 float max = *(std::max_element(values.begin(), values.end()));
134 m_variables.at(identifier + "_min") = min;
135 m_variables.at(identifier + "_max") = max;
136 }
137
138 };
139
140}
void extractVariables(std::vector< SpacePoint const * > const &spacePoints)
extract variables from SpacePoints
ClusterInfoExtractor(std::vector< TrackingUtilities::Named< float * > > &variableSet, bool useTimingInfo)
Constructor fills variableSet with variables to be extracted.
bool m_UseTimingInfo
whether to use timing info from cluster
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.