Belle II Software  release-06-02-00
NeuroTrigger.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 #ifndef NEUROTRIGGER_H
9 #define NEUROTRIGGER_H
10 #pragma once
11 #include <string>
12 #include <trg/cdc/dataobjects/CDCTriggerMLP.h>
13 #include <framework/datastore/StoreArray.h>
14 #include <framework/datastore/StoreObjPtr.h>
15 #include <framework/database/DBObjPtr.h>
16 #include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
17 #include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
18 #include <framework/dataobjects/BinnedEventT0.h>
19 
20 namespace Belle2 {
26  class CDCTriggerTrack;
27 
39  class NeuroTrigger {
40  public:
45  struct Parameters {
51  unsigned nMLP = 1;
56  std::vector<std::vector<float>> nHidden = {{3.}};
58  bool targetZ = true;
60  bool targetTheta = false;
62  bool multiplyHidden = true;
64  std::vector<std::vector<float>> outputScale = {{ -1., 1.}};
66  std::vector<std::vector<float>> phiRange = {{0., 360.}};
68  std::vector<std::vector<float>> invptRange = {{ -5., 5.}};
70  std::vector<std::vector<float>> thetaRange = {{17., 150.}};
73  std::vector<std::vector<float>> phiRangeTrain = {{0., 360.}};
76  std::vector<std::vector<float>> invptRangeTrain = {{ -5., 5.}};
79  std::vector<std::vector<float>> thetaRangeTrain = {{17., 150.}};
81  std::vector<unsigned short> maxHitsPerSL = {1};
87  std::vector<unsigned long> SLpattern = {0};
94  std::vector<unsigned long> SLpatternMask = {0};
96  unsigned tMax = 256;
110  std::string et_option = "etf_or_fastestpriority";
112  bool T0fromHits = false;
113 
114  };
115 
118 
120  virtual ~NeuroTrigger() {}
121 
123  void initialize(const Parameters& p);
124 
126  std::vector<unsigned> getRangeIndices(const Parameters& p, unsigned isector);
127 
132  void save(const std::string& filename, const std::string& arrayname = "MLPs");
138  bool load(const std::string& filename, const std::string& arrayname = "MLPs");
139 
142  void setConstants();
143 
145  void setPrecision(const std::vector<unsigned>& precision) { m_precision = precision; }
146 
149  void initializeCollections(std::string hitCollectionName, std::string eventTimeName, const std::string& et_option);
150  void initializeCollections(std::string hitCollectionName);
151 
153  CDCTriggerMLP& operator[](unsigned index) { return m_MLPs[index]; }
155  const CDCTriggerMLP& operator[](unsigned index) const { return m_MLPs[index]; }
156 
158  unsigned nSectors() const { return m_MLPs.size(); }
159 
161  void addMLP(const CDCTriggerMLP& newMLP) { m_MLPs.push_back(newMLP); }
162 
170  std::vector<int> selectMLPs(float phi0, float invpt, float theta);
171 
177  int selectMLPbyPattern(std::vector<int>& MLPs, unsigned long pattern, const bool neurotrackinputmode);
178 
180  void updateTrack(const CDCTriggerTrack& track);
181 
183  void updateTrackFix(const CDCTriggerTrack& track);
184 
187  double getRelId(const CDCTriggerSegmentHit& hit);
189  int getLowestTime(unsigned isector, RelationVector<CDCTriggerSegmentHit> Hits, bool onlyAxials);
205  void getEventTime(unsigned isector, const CDCTriggerTrack& track, std::string et_option, const bool);
206 
211  void getEventTime(unsigned isector, const CDCTriggerTrack& track);
212 
214  std::string get_et_option()
215  {
216  std::string eto = m_MLPs[0].get_et_option();
217  for (unsigned int i = 0; i < m_MLPs.size(); ++i) {
218  if (m_MLPs[i].get_et_option() != eto) {
219  B2ERROR("Timing options in the expert networks in the CDC Neurotrigger differ!");
220  }
221  }
222  return eto;
223 
224  }
225 
231  unsigned long getInputPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
237  unsigned long getCompleteHitPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
242  unsigned long getPureDriftThreshold(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
243 
250  std::vector<unsigned> selectHitsHWSim(unsigned isector, const CDCTriggerTrack& track);
251 
258  std::vector<unsigned> selectHits(unsigned isector, const CDCTriggerTrack& track,
259  bool returnAllRelevant = false);
260 
266  std::vector<float> getInputVector(unsigned isector, const std::vector<unsigned>& hitIds);
267 
272  std::vector<float> runMLP(unsigned isector, const std::vector<float>& input);
273 
275  std::vector<float> runMLPFix(unsigned isector, std::vector<float> input);
276 
277  private:
279  std::vector<CDCTriggerMLP> m_MLPs = {};
281  double m_radius[9][2] = {};
283  unsigned m_TSoffset[10] = {};
285  double m_idRef[9][2] = {};
287  double m_alpha[9][2] = {};
289  int m_T0 = 0;
291  bool m_hasT0 = false;
299  std::vector<unsigned> m_precision;
300 
306  std::string m_hitCollectionName;
309 
310  };
312 }
313 #endif
Class to keep all parameters of an expert MLP for the neuro trigger.
Definition: CDCTriggerMLP.h:20
Combination of several CDCHits to a track segment hit for the trigger.
Track created by the CDC trigger.
Class for accessing objects in the database.
Definition: DBObjPtr.h:21
Class to represent the CDC Neurotrigger.
Definition: NeuroTrigger.h:39
unsigned long getPureDriftThreshold(unsigned isector, const CDCTriggerTrack &track, const bool neurotrackinputmode)
Get the drift threshold bits, where the time of the TS was outside of the accepted time window and th...
double m_alpha[9][2]
2D crossing angle of current track
Definition: NeuroTrigger.h:287
std::vector< CDCTriggerMLP > m_MLPs
List of networks.
Definition: NeuroTrigger.h:279
double getRelId(const CDCTriggerSegmentHit &hit)
Calculate phi position of a hit relative to 2D track (scaled to number of wires).
DBObjPtr< CDCTriggerNeuroConfig > m_cdctriggerneuroconfig
get NNT payload from database.
Definition: NeuroTrigger.h:308
virtual ~NeuroTrigger()
Default destructor.
Definition: NeuroTrigger.h:120
void initialize(const Parameters &p)
Set parameters and get some network independent parameters.
Definition: NeuroTrigger.cc:28
std::vector< int > selectMLPs(float phi0, float invpt, float theta)
Select all matching expert MLPs based on the given track parameters.
void updateTrack(const CDCTriggerTrack &track)
Calculate 2D phi position and arclength for the given track and store them.
bool load(const std::string &filename, const std::string &arrayname="MLPs")
Load MLPs from file.
std::vector< unsigned > selectHits(unsigned isector, const CDCTriggerTrack &track, bool returnAllRelevant=false)
Select best hits for each super layer.
std::vector< float > runMLPFix(unsigned isector, std::vector< float > input)
Run an expert MLP with fixed point arithmetic.
void initializeCollections(std::string hitCollectionName, std::string eventTimeName, const std::string &et_option)
set the hit collection and event time to required and store the hit collection name
std::vector< float > getInputVector(unsigned isector, const std::vector< unsigned > &hitIds)
Calculate input values for MLP.
StoreArray< CDCTriggerSegmentHit > m_segmentHits
StoreArray containing the input track segment hits.
Definition: NeuroTrigger.h:302
int selectMLPbyPattern(std::vector< int > &MLPs, unsigned long pattern, const bool neurotrackinputmode)
Select one MLP from a list of sector indices.
std::vector< unsigned > m_precision
Fixed point precision in bit after radix point.
Definition: NeuroTrigger.h:299
std::string get_et_option()
Return value of m_et_option.
Definition: NeuroTrigger.h:214
unsigned long getInputPattern(unsigned isector, const CDCTriggerTrack &track, const bool neurotrackinputmode)
Calculate input pattern for MLP.
int m_T0
Event time of current event / track.
Definition: NeuroTrigger.h:289
void save(const std::string &filename, const std::string &arrayname="MLPs")
Save MLPs to file.
void updateTrackFix(const CDCTriggerTrack &track)
Calculate 2D phi position and arclength for the given track and store them.
std::vector< unsigned > selectHitsHWSim(unsigned isector, const CDCTriggerTrack &track)
Select hits for each super layer from the ones related to input track.
NeuroTrigger()
Default constructor.
Definition: NeuroTrigger.h:117
CDCTriggerMLP & operator[](unsigned index)
return reference to a neural network
Definition: NeuroTrigger.h:153
void setConstants()
Loads parameters from the geometry and precalculates some constants that will be needed.
unsigned m_TSoffset[10]
Number of track segments up to super layer.
Definition: NeuroTrigger.h:283
void addMLP(const CDCTriggerMLP &newMLP)
add an MLP to the list of networks
Definition: NeuroTrigger.h:161
std::vector< unsigned > getRangeIndices(const Parameters &p, unsigned isector)
Get indices for sector ranges in parameter lists.
double m_radius[9][2]
Radius of the CDC layers with priority wires (2 per super layer)
Definition: NeuroTrigger.h:281
const CDCTriggerMLP & operator[](unsigned index) const
return const reference to a neural network
Definition: NeuroTrigger.h:155
void setPrecision(const std::vector< unsigned > &precision)
set fixed point precision
Definition: NeuroTrigger.h:145
StoreObjPtr< BinnedEventT0 > m_eventTime
StoreObjPtr containing the event time.
Definition: NeuroTrigger.h:304
unsigned long getCompleteHitPattern(unsigned isector, const CDCTriggerTrack &track, const bool neurotrackinputmode)
Get complete hit pattern of neurotrack.
void getEventTime(unsigned isector, const CDCTriggerTrack &track, std::string et_option, const bool)
Read out the event time and store it.
int getLowestTime(unsigned isector, RelationVector< CDCTriggerSegmentHit > Hits, bool onlyAxials)
helper function to get the fastest priority time of given ts array
double m_idRef[9][2]
2D phi position of current track scaled to number of wires
Definition: NeuroTrigger.h:285
std::string m_hitCollectionName
Name of the StoreArray containing the input track segment hits.
Definition: NeuroTrigger.h:306
unsigned nSectors() const
return number of neural networks
Definition: NeuroTrigger.h:158
std::vector< float > runMLP(unsigned isector, const std::vector< float > &input)
Run an expert MLP.
bool m_hasT0
Flag to show if stored event time is valid.
Definition: NeuroTrigger.h:291
Class for type safe access to objects that are referred to in relations.
Accessor to arrays stored in the data store.
Definition: StoreArray.h:113
Type-safe access to single objects in the data store.
Definition: StoreObjPtr.h:95
Abstract base class for different kinds of events.
Struct to keep neurotrigger parameters.
Definition: NeuroTrigger.h:45
std::vector< unsigned long > SLpatternMask
Super layer pattern mask for which MLP is trained for all networks.
Definition: NeuroTrigger.h:94
std::vector< std::vector< float > > outputScale
Output scale for all networks.
Definition: NeuroTrigger.h:64
std::vector< unsigned short > maxHitsPerSL
Maximum number of hits in a single super layer for all networks.
Definition: NeuroTrigger.h:81
bool targetZ
train z as output
Definition: NeuroTrigger.h:58
unsigned tMax
Maximal drift time, identical for all networks.
Definition: NeuroTrigger.h:96
bool multiplyHidden
If true, multiply nHidden with number of input nodes.
Definition: NeuroTrigger.h:62
std::vector< std::vector< float > > thetaRange
Theta region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:70
std::vector< std::vector< float > > phiRange
Phi region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:66
std::vector< std::vector< float > > invptRangeTrain
Charge / Pt region for which MLP is trained in 1/GeV for all networks.
Definition: NeuroTrigger.h:76
unsigned nMLP
Number of networks.
Definition: NeuroTrigger.h:51
std::vector< std::vector< float > > phiRangeTrain
Phi region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:73
std::vector< std::vector< float > > invptRange
Charge / Pt region for which MLP is used in 1/GeV for all networks.
Definition: NeuroTrigger.h:68
std::vector< unsigned long > SLpattern
Super layer pattern for which MLP is trained for all networks.
Definition: NeuroTrigger.h:87
bool targetTheta
train theta as output
Definition: NeuroTrigger.h:60
std::string et_option
Determine, how the event time should be obtained.
Definition: NeuroTrigger.h:110
std::vector< std::vector< float > > thetaRangeTrain
Theta region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:79
std::vector< std::vector< float > > nHidden
Number of nodes in each hidden layer for all networks or factor to multiply with number of inputs.
Definition: NeuroTrigger.h:56
bool T0fromHits
DEPRECATED!! If true, determine event time from relevant hits if it is missing.
Definition: NeuroTrigger.h:112