Belle II Software  release-06-02-00
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 
11 using namespace Belle2;
12 
14  nNodes{27, 27, 2}, trained(false), targetVars(3), outputScale{ -1., 1., -1., 1.},
15  phiRange{0., 2. * M_PI}, invptRange{ -5., 5.}, thetaRange{0., M_PI},
16  maxHitsPerSL(1), SLpattern(0), SLpatternMask(0), tMax(256),
17  relevantID{ -1., 1.,
18  -10., 1.,
19  -1., 1.,
20  -1., 10.,
21  -1., 1.,
22  -10.5, 1.,
23  -1., 1.,
24  -1., 11.,
25  -1., 1.},
26  et_option("etf_or_fastestpriority"),
27  T0fromHits(false)
28 {
29  weights.assign(nWeightsCal(), 0.);
30 }
31 
32 CDCTriggerMLP::CDCTriggerMLP(std::vector<unsigned short>& nodes,
33  unsigned short targets,
34  std::vector<float>& outputscale,
35  std::vector<float>& phirange,
36  std::vector<float>& invptrange,
37  std::vector<float>& thetarange,
38  unsigned short maxHits,
39  unsigned long pattern,
40  unsigned long patternMask,
41  unsigned short tmax,
42  bool calcT0,
43  const std::string& etoption):
44  nNodes(nodes), trained(false), targetVars(targets), outputScale(outputscale),
45  phiRange(phirange), invptRange(invptrange), thetaRange(thetarange),
46  maxHitsPerSL(maxHits), SLpattern(pattern), SLpatternMask(patternMask),
47  tMax(tmax),
48  relevantID{ -1., 1.,
49  -10., 1.,
50  -1., 1.,
51  -1., 10.,
52  -1., 1.,
53  -10.5, 1.,
54  -1., 1.,
55  -1., 11.,
56  -1., 1.},
57  et_option(etoption),
58  T0fromHits(calcT0)
59 {
60  weights.assign(nWeightsCal(), 0.);
61 }
62 
63 unsigned
65 {
66  unsigned nWeights = 0;
67  if (nLayers() > 1) {
68  nWeights = (nNodes[0] + 1) * nNodes[1];
69  for (unsigned il = 1; il < nLayers() - 1; ++il) {
70  nWeights += (nNodes[il] + 1) * nNodes[il + 1];
71  }
72  }
73  return nWeights;
74 }
75 
76 bool
77 CDCTriggerMLP::inPhiRange(float phi) const
78 {
79  return ((phiRange[0] <= (phi - 2. * M_PI) && (phi - 2. * M_PI) <= phiRange[1]) ||
80  (phiRange[0] <= phi && phi <= phiRange[1]) ||
81  (phiRange[0] <= (phi + 2. * M_PI) && (phi + 2. * M_PI) <= phiRange[1]));
82 }
83 
84 bool
85 CDCTriggerMLP::inPtRange(float pt) const
86 {
87  return (invptRange[0] <= 1. / pt && 1. / pt <= invptRange[1]);
88 }
89 
90 bool
91 CDCTriggerMLP::inInvptRange(float invpt) const
92 {
93  return (invptRange[0] <= invpt && invpt <= invptRange[1]);
94 }
95 
96 bool
97 CDCTriggerMLP::inThetaRange(float theta) const
98 {
99  return (thetaRange[0] <= theta && theta <= thetaRange[1]);
100 }
101 
102 bool
103 CDCTriggerMLP::isRelevant(float relId, unsigned iSL) const
104 {
105  return (relevantID[2 * iSL] <= relId && relId <= relevantID[2 * iSL + 1]);
106 }
107 
108 float
109 CDCTriggerMLP::scaleId(double relId, unsigned iSL) const
110 {
111  float scale = 2. / (relevantID[2 * iSL + 1] - relevantID[2 * iSL]);
112  // round down to nearest power of 2
113  scale = pow(2, floor(log2(scale)));
114  float offset = (relevantID[2 * iSL] + relevantID[2 * iSL + 1]) / 2.;
115  return scale * (relId - offset);
116 }
117 
118 std::vector<float>
119 CDCTriggerMLP::scaleTarget(std::vector<float> target) const
120 {
121  std::vector<float> scaled;
122  scaled.assign(target.size(), 0.);
123  for (unsigned i = 0; i < target.size(); ++i) {
124  scaled[i] = 2. * (target[i] - outputScale[2 * i]) / (outputScale[2 * i + 1] - outputScale[2 * i]) - 1.;
125  }
126  return scaled;
127 }
128 
129 std::vector<float>
130 CDCTriggerMLP::unscaleTarget(std::vector<float> target) const
131 {
132  std::vector<float> unscaled;
133  unscaled.assign(target.size(), 0.);
134  for (unsigned i = 0; i < target.size(); ++i) {
135  unscaled[i] = (target[i] + 1.) * (outputScale[2 * i + 1] - outputScale[2 * i]) / 2. + outputScale[2 * i];
136  }
137  return unscaled;
138 }
139 
140 int
142 {
143  return (targetVars & 1) ? 0 : -1;
144 }
145 
146 int
148 {
149  return (targetVars & 2) ? (targetVars & 1) : -1;
150 }
unsigned nWeightsCal() const
calculate number of weights from number of nodes
std::vector< float > thetaRange
Theta region in radian for which this expert is trained.
bool inPhiRange(float phi) const
check whether given phi value is in sector
bool inThetaRange(float theta) const
check whether given theta value is in sector
std::vector< unsigned short > nNodes
Number of nodes in each layer, not including bias nodes.
std::vector< float > unscaleTarget(std::vector< float > target) const
scale target value from [-1, 1] to outputScale
CDCTriggerMLP()
default constructor.
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 inPtRange(float pt) const
check whether given pt value is in sector
std::vector< float > relevantID
Hits must be within ID region around 2D track to be used as input.
float scaleId(double relId, unsigned iSL) const
scale relative TS ID from relevant range to approximately [-1, 1] (to facilitate the FPGA implementat...
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.
unsigned nLayers() const
get number of layers
Definition: CDCTriggerMLP.h:49
std::vector< float > phiRange
Phi region in radian for which this expert is trained.
bool inInvptRange(float invpt) const
check whether given 1/pt value is in sector
unsigned nWeights() const
get number of weights from length of weights vector
Definition: CDCTriggerMLP.h:53
int thetaIndex() const
get target index for theta (-1 if no output is trained for theta)
std::vector< float > invptRange
Charge / Pt region in 1/GeV for which this expert is trained.
int zIndex() const
get target index for z (-1 if no output is trained for z)
Abstract base class for different kinds of events.