9#include "trg/cdc/modules/neurotrigger/CDCTriggerNeuroIDHistModule.h"
10#include <framework/datastore/StoreArray.h>
11#include <mdst/dataobjects/MCParticle.h>
12#include <tracking/dataobjects/RecoTrack.h>
13#include <trg/cdc/dataobjects/CDCTriggerSegmentHit.h>
14#include <trg/cdc/dataobjects/CDCTriggerTrack.h>
15#include <framework/datastore/StoreObjPtr.h>
16#include <framework/dataobjects/EventMetaData.h>
17#include <framework/core/ModuleParam.templateDetails.h>
19#include <cdc/geometry/CDCGeometryPar.h>
20#include <framework/gearbox/Unit.h>
26#include "boost/iostreams/filter/gzip.hpp"
27#include "boost/iostreams/filtering_streambuf.hpp"
28#include "boost/iostreams/filtering_stream.hpp"
29#include "boost/multi_array.hpp"
30#define BOOST_MULTI_ARRAY_NO_GENERATORS
44 addParam(
"hitCollectionName", m_hitCollectionName,
45 "Name of the input StoreArray of CDCTriggerSegmentHits.",
48 "Name of the StoreArray holding the 2D input tracks.",
49 std::string(
"TRGCDC2DFinderTracks"));
51 "If true, use RecoTracks as targets instead of MCParticles.",
54 "Name of the MCParticle/RecoTrack collection used as target values.",
55 std::string(
"MCParticles"));
57 "Name of the config file, where all the parameters and the IDHist configuration is written.",
60 "Name of the config file, where all the parameters and the IDHist configuration is read in from.",
63 "amount of events used for creating the IDHist. If it is 0, "
64 "all Events are used.",
82 B2ERROR(
"Configuration file is missing! Make sure to give the configuration file as a parameter. \
83 An example file neuroconfig_example.conf has been saved.");
96 for (
int iSL = 0; iSL < 9; ++iSL) {
100 layerId += (iSL > 0 ? 6 : 7);
118 B2ERROR(
"No networks defined, please make sure to have a proper configuration file! Example file will be created here: ./neurotrigger_default.conf");
134 if (targetvector[4] == 0) {
139 float thetaTarget = targetvector[2];
140 float zTarget = targetvector[3];
147 float phi0 =
m_tracks[itrack]->getPhi0();
148 float invpt =
m_tracks[itrack]->getKappa(1.5);
149 float theta = atan2(1.,
m_tracks[itrack]->getCotTheta());
151 if (sectors.size() == 0)
continue;
153 std::vector<float> targetRaw = {};
155 targetRaw.push_back(zTarget);
157 targetRaw.push_back(thetaTarget);
158 for (
unsigned i = 0; i < sectors.size(); ++i) {
159 int isector = sectors[i];
160 std::vector<float> target =
m_NeuroTrigger[isector].scaleTarget(targetRaw);
162 bool outOfRange =
false;
163 for (
unsigned itarget = 0; itarget < target.size(); ++itarget) {
164 if (fabs(target[itarget]) > 1.) {
166 target[itarget] /= fabs(target[itarget]);
205 B2INFO(
"Training sample preparation for NeuroTrigger finished, stopping event loop.");
208 eventMetaData->setEndOfData();
221 B2WARNING(
"ID ranges are already set in the config file, they will be updated now!");
223 B2ERROR(
"The ID ranges in the config file are already locked and cannot be updated!");
235 std::vector<NNTParam<float>> expertline;
236 expertline.push_back(
float(isector));
237 expertline.back().lock();
238 for (
auto x : hset.relID) {
239 expertline.push_back(x);
240 expertline.back().lock();
Struct for training data of a single MLP for the neuro trigger.
bool m_trainOnRecoTracks
Switch between MCParticles or RecoTracks as targets.
NeuroTriggerParameters m_neuroParameters
Parameters for the NeuroTrigger.
NeuroTrigger m_NeuroTrigger
Instance of the NeuroTrigger.
std::string m_targetCollectionName
Name of the MCParticles/RecoTracks collection used as target values.
std::string m_inputCollectionName
name of the event time StoreObjPtr
StoreArray< CDCTriggerTrack > m_tracks
List of input tracks.
std::vector< CDCTriggerMLPData > m_trainSets_prepare
dataset for all idhist prepare data
int m_nPrepare
Number of samples to prepare input ranges.
std::string m_writeconfigFileName
name for the output configuration file which holds all the parameters and the idhist tables for each ...
std::string m_configFileName
name for the input configuration file which holds all the parameters and the idhist tables for each e...
Combination of several CDCHits to a track segment hit for the trigger.
The Class for CDC Geometry Parameters.
static CDCGeometryPar & Instance(const CDCGeometry *=nullptr)
Static method to get a reference to the CDCGeometryPar instance.
A Class to store the Monte Carlo particle information.
void setDescription(const std::string &description)
Sets the description of the module.
void lock()
lock the variable, to make it read only
std::vector< NNTParam< unsigned short > > maxHitsPerSL
Maximum number of hits in a single super layer for all networks.
NNTParam< unsigned > nMLP
Number of networks.
std::vector< NNTParam< unsigned long > > SLpatternMask
Super layer pattern mask for which MLP is trained for all networks.
std::vector< NNTParam< unsigned long > > SLpattern
Super layer pattern for which MLP is trained for all networks.
NNTParam< bool > rescaleTarget
flag to allow for target tracks lying out of the output range to be rescaled during training.
std::vector< std::vector< NNTParam< float > > > IDRanges
relative ID range of the relevant wire IDs of the track segments that are taken into consideration wh...
NNTParam< bool > multiplyHidden
If true, multiply nHidden with number of input nodes.
NNTParam< bool > targetTheta
train theta as output
NNTParam< unsigned > nOutput
number of output nodes
NNTParam< bool > targetZ
train z as output
NNTParam< bool > cutSum
only used in the idhist module.
void loadconfigtxt(const std::string &filename)
load the configuration from a file
void saveconfigtxt(const std::string &filename)
save the configuration to a file
NNTParam< double > relevantCut
only used in the idhist module.
NNTParam< unsigned > nInput
Network parameters.
double getRelId(const CDCTriggerSegmentHit &hit)
Calculate phi position of a hit relative to 2D track (scaled to number of wires).
void initialize(const Parameters &p)
Set parameters and get some network independent parameters.
void updateTrack(const CDCTriggerTrack &track)
Calculate 2D phi position and arclength for the given track and store them.
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< int > selectMLPsTrain(float phi0, float invpt, float theta)
Select all matching expert MLPs based on the given track parameters.
unsigned nSectors() const
return number of neural networks
This is the Reconstruction Event-Data Model Track.
RelationVector< TO > getRelationsTo(const std::string &name="", const std::string &namedRelation="") const
Get the relations that point from this object to another store array.
bool isRequired(const std::string &name="")
Ensure this array/object has been registered previously.
Accessor to arrays stored in the data store.
int getEntries() const
Get the number of objects in the array.
Type-safe access to single objects in the data store.
void addParam(const std::string &name, T ¶mVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
virtual void initialize() override
Initialize the module.
virtual void event() override
Called once for each event.
virtual void terminate() override
Do the training for all sectors.
CDCTriggerNeuroIDHistModule()
Constructor, for setting module description and parameters.
Abstract base class for different kinds of events.