Belle II Software development
CDCTriggerMLP.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#include <trg/cdc/dataobjects/CDCTriggerMLP.h>
9#include <cmath>
10
11using namespace Belle2;
12
14 nNodes{27, 27, 2}, trained(false), targetVars(3), outputScale{ -1., 1., -1., 1.},
15 phiRangeUse{0., 2. * M_PI}, invptRangeUse{ -5., 5.}, thetaRangeUse{0., M_PI},
16 phiRangeTrain{0., 2. * M_PI}, invptRangeTrain{ -5., 5.}, thetaRangeTrain{0., M_PI},
17 maxHitsPerSL(1), SLpattern(0), SLpatternMask(0), tMax(256),
18 relevantID{ -1., 1.,
19 -10., 1.,
20 -1., 1.,
21 -1., 10.,
22 -1., 1.,
23 -10.5, 1.,
24 -1., 1.,
25 -1., 11.,
26 -1., 1.},
27 et_option("etf_or_fastestpriority")
28{
29 weights.assign(nWeightsCal(), 0.);
30}
31
32CDCTriggerMLP::CDCTriggerMLP(std::vector<unsigned short>& nodes,
33 unsigned short targets,
34 std::vector<float>& outputscale,
35 std::vector<float>& phirangeUse,
36 std::vector<float>& invptrangeUse,
37 std::vector<float>& thetarangeUse,
38 std::vector<float>& phirangeTrain,
39 std::vector<float>& invptrangeTrain,
40 std::vector<float>& thetarangeTrain,
41 unsigned short maxHits,
42 unsigned long pattern,
43 unsigned long patternMask,
44 unsigned short tmax,
45 const std::string& etoption):
46 nNodes(nodes), trained(false), targetVars(targets), outputScale(outputscale),
47 phiRangeUse(phirangeUse), invptRangeUse(invptrangeUse), thetaRangeUse(thetarangeUse),
48 phiRangeTrain(phirangeTrain), invptRangeTrain(invptrangeTrain), thetaRangeTrain(thetarangeTrain),
49 maxHitsPerSL(maxHits), SLpattern(pattern), SLpatternMask(patternMask),
50 tMax(tmax),
51 relevantID{ -1., 1.,
52 -10., 1.,
53 -1., 1.,
54 -1., 10.,
55 -1., 1.,
56 -10.5, 1.,
57 -1., 1.,
58 -1., 11.,
59 -1., 1.},
60 et_option(etoption)
61{
62 weights.assign(nWeightsCal(), 0.);
63}
64
65unsigned
67{
68 unsigned nWeights = 0;
69 if (nLayers() > 1) {
70 nWeights = (nNodes[0] + 1) * nNodes[1];
71 for (unsigned il = 1; il < nLayers() - 1; ++il) {
72 nWeights += (nNodes[il] + 1) * nNodes[il + 1];
73 }
74 }
75 return nWeights;
76}
77
78bool
80{
81 return ((phiRangeUse[0] <= (phi - 2. * M_PI) && (phi - 2. * M_PI) <= phiRangeUse[1]) ||
82 (phiRangeUse[0] <= phi && phi <= phiRangeUse[1]) ||
83 (phiRangeUse[0] <= (phi + 2. * M_PI) && (phi + 2. * M_PI) <= phiRangeUse[1]));
84}
85
86bool
88{
89 return (invptRangeUse[0] <= 1. / pt && 1. / pt <= invptRangeUse[1]);
90}
91
92bool
94{
95 return (invptRangeUse[0] <= invpt && invpt <= invptRangeUse[1]);
96}
97
98bool
100{
101 return (thetaRangeUse[0] <= theta && theta <= thetaRangeUse[1]);
102}
103
104bool
106{
107 return ((phiRangeTrain[0] <= (phi - 2. * M_PI) && (phi - 2. * M_PI) <= phiRangeTrain[1]) ||
108 (phiRangeTrain[0] <= phi && phi <= phiRangeTrain[1]) ||
109 (phiRangeTrain[0] <= (phi + 2. * M_PI) && (phi + 2. * M_PI) <= phiRangeTrain[1]));
110}
111
112bool
114{
115 return (invptRangeTrain[0] <= 1. / pt && 1. / pt <= invptRangeTrain[1]);
116}
117
118bool
120{
121 return (invptRangeTrain[0] <= invpt && invpt <= invptRangeTrain[1]);
122}
123
124bool
126{
127 return (thetaRangeTrain[0] <= theta && theta <= thetaRangeTrain[1]);
128}
129
130bool
131CDCTriggerMLP::isRelevant(float relId, unsigned iSL) const
132{
133 return (relevantID[2 * iSL] <= relId && relId <= relevantID[2 * iSL + 1]);
134}
135
136float
137CDCTriggerMLP::scaleId(double relId, unsigned iSL) const
138{
139 float scale = 2. / (relevantID[2 * iSL + 1] - relevantID[2 * iSL]);
140 // round down to nearest power of 2
141 scale = pow(2, floor(log2(scale)));
142 float offset = (relevantID[2 * iSL] + relevantID[2 * iSL + 1]) / 2.;
143 return scale * (relId - offset);
144}
145
146std::vector<float>
147CDCTriggerMLP::scaleTarget(std::vector<float> target) const
148{
149 std::vector<float> scaled;
150 scaled.assign(target.size(), 0.);
151 for (unsigned i = 0; i < target.size(); ++i) {
152 scaled[i] = 2. * (target[i] - outputScale[2 * i]) / (outputScale[2 * i + 1] - outputScale[2 * i]) - 1.;
153 }
154 return scaled;
155}
156
157std::vector<float>
158CDCTriggerMLP::unscaleTarget(std::vector<float> target) const
159{
160 std::vector<float> unscaled;
161 unscaled.assign(target.size(), 0.);
162 for (unsigned i = 0; i < target.size(); ++i) {
163 unscaled[i] = (target[i] + 1.) * (outputScale[2 * i + 1] - outputScale[2 * i]) / 2. + outputScale[2 * i];
164 }
165 return unscaled;
166}
167
168int
170{
171 return (targetVars & 1) ? 0 : -1;
172}
173
174int
176{
177 return (targetVars & 2) ? (targetVars & 1) : -1;
178}
unsigned nWeightsCal() const
calculate number of weights from number of nodes
std::vector< unsigned short > nNodes
Number of nodes in each layer, not including bias nodes.
std::vector< float > phiRangeUse
Phi region in radian for which this expert is used.
std::vector< float > unscaleTarget(std::vector< float > target) const
scale target value from [-1, 1] to outputScale
CDCTriggerMLP()
default constructor.
std::vector< float > invptRangeTrain
Charge / Pt region in 1/GeV for which this expert is trained.
std::vector< float > scaleTarget(std::vector< float > target) const
scale target value from outputScale to [-1, 1]
std::vector< float > outputScale
Output[i] of the MLP is scaled from [-1, 1] to [outputScale[2i], outputScale[2i+1]].
bool inThetaRangeTrain(float theta) const
check whether given theta value is in training sector
std::vector< float > relevantID
Hits must be within ID region around 2D track to be used as input.
std::vector< float > thetaRangeUse
Theta region in radian for which this expert is trained.
bool inPtRangeTrain(float pt) const
check whether given pt value is in training sector
float scaleId(double relId, unsigned iSL) const
scale relative TS ID from relevant range to approximately [-1, 1] (to facilitate the FPGA implementat...
bool inInvptRangeUse(float invpt) const
check whether given 1/pt value is in sector
bool isRelevant(float relId, unsigned iSL) const
check whether given relative TS ID is in relevant range
unsigned short targetVars
output variables: 1: z, 2: theta, 3: (z, theta)
std::vector< float > weights
Weights of the network.
bool inPhiRangeUse(float phi) const
check whether given phi value is in sector
unsigned nLayers() const
get number of layers
Definition: CDCTriggerMLP.h:52
std::vector< float > phiRangeTrain
Phi region in radian for which this expert is used.
std::vector< float > thetaRangeTrain
Theta region in radian for which this expert is trained.
std::vector< float > invptRangeUse
Charge / Pt region in 1/GeV for which this expert is used.
bool inInvptRangeTrain(float invpt) const
check whether given 1/pt value is in training sector
bool inThetaRangeUse(float theta) const
check whether given theta value is in sector
bool inPtRangeUse(float pt) const
check whether given pt value is in sector
unsigned nWeights() const
get number of weights from length of weights vector
Definition: CDCTriggerMLP.h:56
int thetaIndex() const
get target index for theta (-1 if no output is trained for theta)
int zIndex() const
get target index for z (-1 if no output is trained for z)
bool inPhiRangeTrain(float phi) const
check whether given phi value is in training sector
Abstract base class for different kinds of events.