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