Belle II Software  release-08-01-10
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/NeuroTriggerParameters.h>
13 #include <trg/cdc/dataobjects/CDCTriggerMLP.h>
14 #include <framework/datastore/StoreArray.h>
15 #include <framework/datastore/StoreObjPtr.h>
16 #include <framework/database/DBObjPtr.h>
17 #include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
18 #include <trg/cdc/dbobjects/CDCTriggerNeuroConfig.h>
19 #include <framework/dataobjects/BinnedEventT0.h>
20 
21 namespace Belle2 {
27  class CDCTriggerTrack;
28 
40  class NeuroTrigger {
41  public:
46  struct Parameters {
52  unsigned nMLP = 1;
57  std::vector<std::vector<float>> nHidden = {{3.}};
59  bool targetZ = true;
61  bool targetTheta = false;
63  bool multiplyHidden = true;
65  std::vector<std::vector<float>> outputScale = {{ -1., 1.}};
67  std::vector<std::vector<float>> phiRange = {{0., 360.}};
69  std::vector<std::vector<float>> invptRange = {{ -5., 5.}};
71  std::vector<std::vector<float>> thetaRange = {{17., 150.}};
74  std::vector<std::vector<float>> phiRangeTrain = {{0., 360.}};
77  std::vector<std::vector<float>> invptRangeTrain = {{ -5., 5.}};
80  std::vector<std::vector<float>> thetaRangeTrain = {{17., 150.}};
82  std::vector<unsigned short> maxHitsPerSL = {1};
88  std::vector<unsigned long> SLpattern = {0};
95  std::vector<unsigned long> SLpatternMask = {0};
97  unsigned tMax = 256;
112  std::string et_option = "etf_or_fastestpriority";
115  };
116 
119 
121  virtual ~NeuroTrigger() {}
122 
124  void initialize(const Parameters& p);
125  void initialize(const NeuroTriggerParameters& p);
126 
128  std::vector<unsigned> getRangeIndices(const NeuroTriggerParameters& p, unsigned isector);
129  std::vector<unsigned> getRangeIndices(const Parameters& p, unsigned isector);
130 
135  void save(const std::string& filename, const std::string& arrayname = "MLPs");
137  bool loadIDHist(const std::string& filename);
138 
144  bool load(const std::string& filename, const std::string& arrayname = "MLPs");
145 
148  void setConstants();
149 
151  void setPrecision(const std::vector<unsigned>& precision) { m_precision = precision; }
152 
155  void initializeCollections(std::string hitCollectionName, std::string eventTimeName, const std::string& et_option);
156  void initializeCollections(std::string hitCollectionName);
157 
159  CDCTriggerMLP& operator[](unsigned index) { return m_MLPs[index]; }
161  const CDCTriggerMLP& operator[](unsigned index) const { return m_MLPs[index]; }
162 
164  unsigned nSectors() const { return m_MLPs.size(); }
165 
167  void addMLP(const CDCTriggerMLP& newMLP) { m_MLPs.push_back(newMLP); }
168 
176  std::vector<int> selectMLPs(float phi0, float invpt, float theta);
177 
185  std::vector<int> selectMLPsTrain(float phi0, float invpt, float theta);
186 
192  int selectMLPbyPattern(std::vector<int>& MLPs, unsigned long pattern, const bool neurotrackinputmode);
193 
195  void updateTrack(const CDCTriggerTrack& track);
196 
198  void updateTrackFix(const CDCTriggerTrack& track);
199 
202  double getRelId(const CDCTriggerSegmentHit& hit);
204  int getLowestTime(unsigned isector, RelationVector<CDCTriggerSegmentHit> Hits, bool onlyAxials);
220  void getEventTime(unsigned isector, const CDCTriggerTrack& track, std::string et_option, const bool);
221 
226  void getEventTime(unsigned isector, const CDCTriggerTrack& track);
227 
229  std::string get_et_option()
230  {
231  std::string eto = m_MLPs[0].get_et_option();
232  for (unsigned int i = 0; i < m_MLPs.size(); ++i) {
233  if (m_MLPs[i].get_et_option() != eto) {
234  B2ERROR("Timing options in the expert networks in the CDC Neurotrigger differ!");
235  }
236  }
237  return eto;
238 
239  }
240 
246  unsigned long getInputPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
252  unsigned long getCompleteHitPattern(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
257  unsigned long getPureDriftThreshold(unsigned isector, const CDCTriggerTrack& track, const bool neurotrackinputmode);
258 
265  std::vector<unsigned> selectHitsHWSim(unsigned isector, const CDCTriggerTrack& track);
266 
273  std::vector<unsigned> selectHits(unsigned isector, const CDCTriggerTrack& track,
274  bool returnAllRelevant = false);
275 
281  std::vector<float> getInputVector(unsigned isector, const std::vector<unsigned>& hitIds);
282 
287  std::vector<float> runMLP(unsigned isector, const std::vector<float>& input);
288 
290  std::vector<float> runMLPFix(unsigned isector, std::vector<float> input);
291 
292  private:
294  std::vector<CDCTriggerMLP> m_MLPs = {};
296  double m_radius[9][2] = {};
298  unsigned m_TSoffset[10] = {};
300  double m_idRef[9][2] = {};
302  double m_alpha[9][2] = {};
304  int m_T0 = 0;
306  bool m_hasT0 = false;
314  std::vector<unsigned> m_precision;
315 
321  std::string m_hitCollectionName;
324 
325  };
327 }
328 #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:40
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:302
std::vector< CDCTriggerMLP > m_MLPs
List of networks.
Definition: NeuroTrigger.h:294
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:323
virtual ~NeuroTrigger()
Default destructor.
Definition: NeuroTrigger.h:121
void initialize(const Parameters &p)
Set parameters and get some network independent parameters.
std::vector< unsigned > getRangeIndices(const NeuroTriggerParameters &p, unsigned isector)
Get indices for sector ranges in parameter lists.
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:317
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:314
std::string get_et_option()
Return value of m_et_option.
Definition: NeuroTrigger.h:229
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:304
void save(const std::string &filename, const std::string &arrayname="MLPs")
Save MLPs to file.
void getEventTime(unsigned isector, const CDCTriggerTrack &track)
DEPRECATED!! Read out the event time and store it.
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:118
bool loadIDHist(const std::string &filename)
function to load idhist from file
std::vector< int > selectMLPsTrain(float phi0, float invpt, float theta)
Select all matching expert MLPs based on the given track parameters.
CDCTriggerMLP & operator[](unsigned index)
return reference to a neural network
Definition: NeuroTrigger.h:159
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:298
void addMLP(const CDCTriggerMLP &newMLP)
add an MLP to the list of networks
Definition: NeuroTrigger.h:167
double m_radius[9][2]
Radius of the CDC layers with priority wires (2 per super layer)
Definition: NeuroTrigger.h:296
const CDCTriggerMLP & operator[](unsigned index) const
return const reference to a neural network
Definition: NeuroTrigger.h:161
void setPrecision(const std::vector< unsigned > &precision)
set fixed point precision
Definition: NeuroTrigger.h:151
StoreObjPtr< BinnedEventT0 > m_eventTime
StoreObjPtr containing the event time.
Definition: NeuroTrigger.h:319
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:300
std::string m_hitCollectionName
Name of the StoreArray containing the input track segment hits.
Definition: NeuroTrigger.h:321
unsigned nSectors() const
return number of neural networks
Definition: NeuroTrigger.h:164
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:306
Class for type safe access to objects that are referred to in relations.
Abstract base class for different kinds of events.
Struct to keep neurotrigger parameters.
Definition: NeuroTrigger.h:46
std::vector< unsigned long > SLpatternMask
Super layer pattern mask for which MLP is trained for all networks.
Definition: NeuroTrigger.h:95
std::vector< std::vector< float > > outputScale
Output scale for all networks.
Definition: NeuroTrigger.h:65
std::vector< unsigned short > maxHitsPerSL
Maximum number of hits in a single super layer for all networks.
Definition: NeuroTrigger.h:82
bool targetZ
train z as output
Definition: NeuroTrigger.h:59
unsigned tMax
Maximal drift time, identical for all networks.
Definition: NeuroTrigger.h:97
bool multiplyHidden
If true, multiply nHidden with number of input nodes.
Definition: NeuroTrigger.h:63
std::vector< std::vector< float > > thetaRange
Theta region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:71
std::vector< std::vector< float > > phiRange
Phi region for which MLP is used in degree for all networks.
Definition: NeuroTrigger.h:67
std::vector< std::vector< float > > invptRangeTrain
Charge / Pt region for which MLP is trained in 1/GeV for all networks.
Definition: NeuroTrigger.h:77
unsigned nMLP
Number of networks.
Definition: NeuroTrigger.h:52
std::vector< std::vector< float > > phiRangeTrain
Phi region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:74
std::vector< std::vector< float > > invptRange
Charge / Pt region for which MLP is used in 1/GeV for all networks.
Definition: NeuroTrigger.h:69
std::vector< unsigned long > SLpattern
Super layer pattern for which MLP is trained for all networks.
Definition: NeuroTrigger.h:88
bool targetTheta
train theta as output
Definition: NeuroTrigger.h:61
std::string et_option
Determine, how the event time should be obtained.
Definition: NeuroTrigger.h:112
std::vector< std::vector< float > > thetaRangeTrain
Theta region for which MLP is trained in degree for all networks.
Definition: NeuroTrigger.h:80
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:57