Belle II Software  release-06-00-14
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 
11 #include <trg/cdc/dataobjects/CDCTriggerMLP.h>
12 #include <framework/datastore/StoreArray.h>
13 #include <framework/datastore/StoreObjPtr.h>
14 #include <framework/database/DBObjPtr.h>
15 #include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
16 #include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
17 #include <framework/dataobjects/BinnedEventT0.h>
18 
19 namespace Belle2 {
25  class CDCTriggerTrack;
26 
38  class NeuroTrigger {
39  public:
44  struct Parameters {
50  unsigned nMLP = 1;
55  std::vector<std::vector<float>> nHidden = {{3.}};
57  bool targetZ = true;
59  bool targetTheta = false;
61  bool multiplyHidden = true;
63  std::vector<std::vector<float>> outputScale = {{ -1., 1.}};
65  std::vector<std::vector<float>> phiRange = {{0., 360.}};
67  std::vector<std::vector<float>> invptRange = {{ -5., 5.}};
69  std::vector<std::vector<float>> thetaRange = {{17., 150.}};
72  std::vector<std::vector<float>> phiRangeTrain = {{0., 360.}};
75  std::vector<std::vector<float>> invptRangeTrain = {{ -5., 5.}};
78  std::vector<std::vector<float>> thetaRangeTrain = {{17., 150.}};
80  std::vector<unsigned short> maxHitsPerSL = {1};
86  std::vector<unsigned long> SLpattern = {0};
93  std::vector<unsigned long> SLpatternMask = {0};
95  unsigned tMax = 256;
109  std::string et_option = "etf_or_fastestpriority";
111  bool T0fromHits = false;
112 
113  };
114 
117 
119  virtual ~NeuroTrigger() {}
120 
122  void initialize(const Parameters& p);
123 
125  std::vector<unsigned> getRangeIndices(const Parameters& p, unsigned isector);
126 
131  void save(const std::string& filename, const std::string& arrayname = "MLPs");
137  bool load(const std::string& filename, const std::string& arrayname = "MLPs");
138 
141  void setConstants();
142 
144  void setPrecision(const std::vector<unsigned>& precision) { m_precision = precision; }
145 
148  void initializeCollections(std::string hitCollectionName, std::string eventTimeName, const std::string& et_option);
149 
151  CDCTriggerMLP& operator[](unsigned index) { return m_MLPs[index]; }
153  const CDCTriggerMLP& operator[](unsigned index) const { return m_MLPs[index]; }
154 
156  unsigned nSectors() const { return m_MLPs.size(); }
157 
159  void addMLP(const CDCTriggerMLP& newMLP) { m_MLPs.push_back(newMLP); }
160 
168  std::vector<int> selectMLPs(float phi0, float invpt, float theta);
169 
175  int selectMLPbyPattern(std::vector<int>& MLPs, unsigned long pattern, const bool neurotrackinputmode);
176 
178  void updateTrack(const CDCTriggerTrack& track);
179 
181  void updateTrackFix(const CDCTriggerTrack& track);
182 
185  double getRelId(const CDCTriggerSegmentHit& hit);
201  void getEventTime(unsigned isector, const CDCTriggerTrack& track, std::string et_option, const bool);
202 
207  void getEventTime(unsigned isector, const CDCTriggerTrack& track);
208 
210  std::string get_et_option()
211  {
212  std::string eto = m_MLPs[0].get_et_option();
213  for (unsigned int i = 0; i < m_MLPs.size(); ++i) {
214  if (m_MLPs[i].get_et_option() != eto) {
215  B2ERROR("Timing options in the expert networks in the CDC Neurotrigger differ!");
216  }
217  }
218  return eto;
219 
220  }
221 
227  unsigned long getInputPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
233  unsigned long getCompleteHitPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
238  unsigned long getPureDriftThreshold(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
239 
246  std::vector<unsigned> selectHitsHWSim(unsigned isector, const CDCTriggerTrack& track);
247 
254  std::vector<unsigned> selectHits(unsigned isector, const CDCTriggerTrack& track,
255  bool returnAllRelevant = false);
256 
262  std::vector<float> getInputVector(unsigned isector, const std::vector<unsigned>& hitIds);
263 
268  std::vector<float> runMLP(unsigned isector, const std::vector<float>& input);
269 
271  std::vector<float> runMLPFix(unsigned isector, std::vector<float> input);
272 
273  private:
275  std::vector<CDCTriggerMLP> m_MLPs = {};
277  double m_radius[9][2] = {};
279  unsigned m_TSoffset[10] = {};
281  double m_idRef[9][2] = {};
283  double m_alpha[9][2] = {};
285  int m_T0 = 0;
287  bool m_hasT0 = false;
295  std::vector<unsigned> m_precision;
296 
302  std::string m_hitCollectionName;
305 
306  };
308 }
309 #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:38
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:283
std::vector< CDCTriggerMLP > m_MLPs
List of networks.
Definition: NeuroTrigger.h:275
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:304
virtual ~NeuroTrigger()
Default destructor.
Definition: NeuroTrigger.h:119
void initialize(const Parameters &p)
Set parameters and get some network independent parameters.
Definition: NeuroTrigger.cc:27
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:298
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:295
std::string get_et_option()
Return value of m_et_option.
Definition: NeuroTrigger.h:210
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:285
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:116
CDCTriggerMLP & operator[](unsigned index)
return reference to a neural network
Definition: NeuroTrigger.h:151
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:279
void addMLP(const CDCTriggerMLP &newMLP)
add an MLP to the list of networks
Definition: NeuroTrigger.h:159
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:277
const CDCTriggerMLP & operator[](unsigned index) const
return const reference to a neural network
Definition: NeuroTrigger.h:153
void setPrecision(const std::vector< unsigned > &precision)
set fixed point precision
Definition: NeuroTrigger.h:144
StoreObjPtr< BinnedEventT0 > m_eventTime
StoreObjPtr containing the event time.
Definition: NeuroTrigger.h:300
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.
double m_idRef[9][2]
2D phi position of current track scaled to number of wires
Definition: NeuroTrigger.h:281
std::string m_hitCollectionName
Name of the StoreArray containing the input track segment hits.
Definition: NeuroTrigger.h:302
unsigned nSectors() const
return number of neural networks
Definition: NeuroTrigger.h:156
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:287
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:44
std::vector< unsigned long > SLpatternMask
Super layer pattern mask for which MLP is trained for all networks.
Definition: NeuroTrigger.h:93
std::vector< std::vector< float > > outputScale
Output scale for all networks.
Definition: NeuroTrigger.h:63
std::vector< unsigned short > maxHitsPerSL
Maximum number of hits in a single super layer for all networks.
Definition: NeuroTrigger.h:80
bool targetZ
train z as output
Definition: NeuroTrigger.h:57
unsigned tMax
Maximal drift time, identical for all networks.
Definition: NeuroTrigger.h:95
bool multiplyHidden
If true, multiply nHidden with number of input nodes.
Definition: NeuroTrigger.h:61
std::vector< std::vector< float > > thetaRange
Theta region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:69
std::vector< std::vector< float > > phiRange
Phi region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:65
std::vector< std::vector< float > > invptRangeTrain
Charge / Pt region for which MLP is trained in 1/GeV for all networks.
Definition: NeuroTrigger.h:75
unsigned nMLP
Number of networks.
Definition: NeuroTrigger.h:50
std::vector< std::vector< float > > phiRangeTrain
Phi region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:72
std::vector< std::vector< float > > invptRange
Charge / Pt region for which MLP is used in 1/GeV for all networks.
Definition: NeuroTrigger.h:67
std::vector< unsigned long > SLpattern
Super layer pattern for which MLP is trained for all networks.
Definition: NeuroTrigger.h:86
bool targetTheta
train theta as output
Definition: NeuroTrigger.h:59
std::string et_option
Determine, how the event time should be obtained.
Definition: NeuroTrigger.h:109
std::vector< std::vector< float > > thetaRangeTrain
Theta region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:78
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:55
bool T0fromHits
DEPRECATED!! If true, determine event time from relevant hits if it is missing.
Definition: NeuroTrigger.h:111