Belle II Software development
CDCTriggerMLPData.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 CDCTRIGGERMLPDATA_H
9#define CDCTRIGGERMLPDATA_H
10
11#include <framework/logging/Logger.h>
12#include <iostream>
13#include <fstream>
14#include <TObject.h>
15#include "boost/iostreams/filter/gzip.hpp"
16#include "boost/iostreams/filtering_streambuf.hpp"
17#include "boost/iostreams/filtering_stream.hpp"
18#include "boost/multi_array.hpp"
19#define BOOST_MULTI_ARRAY_NO_GENERATORS
20
21namespace Belle2 {
27 class CDCTriggerMLPData : public TObject {
28 public:
29 struct HeaderSet {
30 float relID[18];
31 unsigned exPert;
32 HeaderSet() {}
33 HeaderSet(std::vector<float> line)
34 {
35 if (line.size() == 19) {
36 exPert = line[0];
37 for (unsigned i = 1; i <= 19; i++) {
38 relID[i] = line[i];
39 }
40 } else {B2ERROR("ERROR! wrong lengthof relID vector!");}
41 }
42 void operator= (std::vector<float> line)
43 {
44 if (line.size() == 19) {
45 exPert = line[0];
46 for (unsigned i = 1; i <= 19; i++) {
47 relID[i] = line[i];
48 }
49 } else {B2ERROR("ERROR! wrong length of relID vector!");}
50
51
52 }
53 HeaderSet(unsigned expert, std::vector<float> relevantID)
54 {
55 if (relevantID.size() == 18) {
56 for (unsigned i = 0; i < 18; ++i) {relID[i] = relevantID[i];}
57 } else {B2ERROR("ERROR! wrong length of relID vector!");}
58 exPert = expert;
59 }
60 friend std::ostream& operator << (std::ostream& out, const HeaderSet& hset)
61 {
62 out << hset.exPert << '\t';
63 for (unsigned i = 0; i < 18; ++i) { out << hset.relID[i] << '\t';}
64 return out;
65 }
66 friend std::istream& operator >> (std::istream& in, HeaderSet& hset)
67 {
68 std::string help;
69 std::string helpline;
70 help = "";
71 helpline = "";
72 std::getline(in, helpline, '\n');
73 if (helpline.length() < 2) {return in;}
74 std::stringstream ss(helpline);
75 std::getline(ss, help, '\t');
76
77 hset.exPert = std::stoul(help);
78 for (unsigned i = 0; i < 18; ++i) {
79 help = "";
80 std::getline(ss, help, '\t');
81 hset.relID[i] = std::stof(help);
82 }
83 return in;
84 }
85
86
87 };
91 template <const unsigned inLen, const unsigned outLen>
92 struct NeuroSet {
93 float input[inLen];
94 float target[outLen];
95 int exp;
96 int run;
97 int subrun;
98 int evt;
99 int track;
100 int ntracks;
101 float nnscalez;
102 float nnscaletheta;
103 int nnrawz;
104 int nnrawtheta;
105 float inphi;
106 float intheta;
107 unsigned expert;
108 float ininvpt;
109 std::string headline;
110 NeuroSet()
111 {
112 headline = "Experiment\tRun\tSubrun\tEvent\tTrack\tnTracks\tExpert\tiNodes\toNodes\t";
113 for (unsigned i = 0; i < inLen / 3; ++i) {
114 headline += "SL" + std::to_string(i) + "-relID\t";
115 headline += "SL" + std::to_string(i) + "-driftT\t";
116 headline += "SL" + std::to_string(i) + "-alpha\t";
117 }
118 headline += "RecoZ\tRecoTheta\tScaleZ\tRawZ\tScaleTheta\tRawTheta\t2DPhi\t3DTheta\t2DinvPt\n";
119 }
120
121 NeuroSet(float xin[inLen], float xout[outLen], int xexp, int xrun, int xsubrun, int xevt, int xtrack, unsigned xexpert,
122 int xntracks, int xnnrawz, int xnnrawtheta, float xnnscalez, float xnnscaletheta, float xinphi, float xintheta, float xininvpt)
123 {
124 for (unsigned i = 0; i < inLen; ++i) {
125 input[i] = xin[i];
126 }
127 for (unsigned i = 0; i < outLen; ++i) {
128 target[i] = xout[i];
129 }
130 exp = xexp;
131 run = xrun;
132 subrun = xsubrun;
133 evt = xevt;
134 track = xtrack;
135 expert = xexpert;
136
137 ntracks = xntracks;
138 nnscalez = xnnscalez;
139 nnscaletheta = xnnscaletheta;
140 nnrawz = xnnrawz;
141 nnrawtheta = xnnrawtheta;
142 inphi = xinphi;
143 intheta = xintheta;
144 ininvpt = xininvpt;
145 headline = "Experiment\tRun\tSubrun\tEvent\tTrack\tnTracks\tExpert\tiNodes\toNodes\t";
146 for (unsigned i = 0; i < inLen / 3; ++i) {
147 headline += "SL" + std::to_string(i) + "-relID\t";
148 headline += "SL" + std::to_string(i) + "-driftT\t";
149 headline += "SL" + std::to_string(i) + "-alpha\t";
150 }
151 headline += "RecoZ\tRecoTheta\tScaleZ\tRawZ\tScaleTheta\tRawTheta\t2DPhi\t3DTheta\t2DinvPt\n";
152
153 }
154 friend std::ostream& operator << (std::ostream& out, const NeuroSet& dset)
155 {
156 out << dset.exp << '\t' << dset.run << '\t' << dset.subrun << '\t' << dset.evt << '\t' << dset.track << '\t' << dset.ntracks << '\t'
157 << dset.expert << '\t'
158 << inLen << '\t' << outLen << '\t';
159 for (auto indata : dset.input) {out << indata << '\t';}
160 for (auto outdata : dset.target) {out << outdata << '\t';}
161 out << dset.nnscalez << '\t' << dset.nnrawz << '\t' << dset.nnscaletheta << '\t' << dset.nnrawtheta << '\t' << dset.inphi << '\t' <<
162 dset.intheta << '\t' << dset.ininvpt << '\t';
163 return out;
164 }
165 friend std::istream& operator >> (std::istream& in, NeuroSet& dset)
166 {
167 std::string help;
168 help = "";
169 std::getline(in, help, '\t');
170 if (help.length() < 2) {return in;}
171 dset.exp = std::stoi(help);
172 help = "";
173 std::getline(in, help, '\t');
174 dset.run = std::stoi(help);
175 help = "";
176 std::getline(in, help, '\t');
177 dset.subrun = std::stoi(help);
178 help = "";
179 std::getline(in, help, '\t');
180 dset.evt = std::stoi(help);
181 help = "";
182 std::getline(in, help, '\t');
183 dset.track = std::stoi(help);
184 help = "";
185 std::getline(in, help, '\t');
186 dset.ntracks = std::stoi(help);
187 help = "";
188 std::getline(in, help, '\t');
189 dset.expert = std::stoul(help);
190 // check if input and target size match:
191 std::string insize = "";
192 std::string outsize = "";
193 std::getline(in, insize, '\t');
194 if (std::stoul(insize) != inLen) {
195 B2ERROR("Input and output format of neurotrigger training data does not match!");
196 }
197 std::getline(in, outsize, '\t');
198 if (std::stoul(outsize) != outLen) {
199 B2ERROR("Input and output format of neurotrigger training data does not match!");
200 }
201 for (unsigned i = 0; i < inLen; ++i) {
202 help = "";
203 std::getline(in, help, '\t');
204 try {
205 dset.input[i] = std::stof(help);
206 } catch (const std::out_of_range& oor) {
207 B2WARNING("out of range error: " << help << " is not in float range!");
208 dset.input[i] = 0.;
209 }
210 }
211 for (unsigned i = 0; i < outLen; ++i) {
212 help = "";
213 std::getline(in, help, '\t');
214 try {
215 dset.target[i] = std::stof(help);
216 } catch (const std::out_of_range& oor) {
217 B2WARNING("out of range error: " << help << " is not in float range!");
218 dset.target[i] = 0.;
219 }
220 }
221 help = "";
222 std::getline(in, help, '\t');
223 dset.nnscalez = std::stof(help);
224 help = "";
225 std::getline(in, help, '\t');
226 dset.nnrawz = std::stoi(help);
227 help = "";
228 std::getline(in, help, '\t');
229 dset.nnscaletheta = std::stof(help);
230 help = "";
231 std::getline(in, help, '\t');
232 dset.nnrawtheta = std::stoi(help);
233 help = "";
234 std::getline(in, help, '\t');
235 dset.inphi = std::stof(help);
236 help = "";
237 std::getline(in, help, '\t');
238 dset.intheta = std::stof(help);
239 help = "";
240 std::getline(in, help, '\t');
241 dset.ininvpt = std::stof(help);
242 help = "";
243 return in;
244 }
245 };
246
251
253 void addCounters(unsigned nWires)
254 {
255 std::vector<unsigned short> counters;
256 counters.assign(nWires, 0);
257 m_hitCounters.push_back(counters);
258 }
262 void addHit(unsigned iSL, int iTS);
265 template<unsigned inLen, unsigned outLen>
266 void addSample(const NeuroSet<inLen, outLen>& dsample)
267 {
268 std::vector<float> in(dsample.input, dsample.input + sizeof dsample.input / sizeof dsample.input[0]);
269 m_inputSamples.push_back(in);
270 std::vector<float> out(dsample.target, dsample.target + sizeof dsample.target / sizeof dsample.target[0]);
271 m_targetSamples.push_back(out);
272 m_expList.push_back(dsample.exp);
273 m_runList.push_back(dsample.run);
274 m_subRunList.push_back(dsample.subrun);
275 m_evtList.push_back(dsample.evt);
276 m_trackList.push_back(dsample.track);
277
278 }
279
281 void addSample(const std::vector<float>& input, const std::vector<float>& target, const int& expnumber, const int& runnumber,
282 const int& subrunnumber, const int& eventnumber, const int& tracknumber)
283 {
284 m_inputSamples.push_back(input);
285 m_targetSamples.push_back(target);
286 m_expList.push_back(expnumber);
287 m_runList.push_back(runnumber);
288 m_subRunList.push_back(subrunnumber);
289 m_evtList.push_back(eventnumber);
290 m_trackList.push_back(tracknumber);
291 }
292
294 short getTrackCounter() const { return m_trackCounter; }
298 unsigned short getHitCounter(unsigned iSL, int iTS) const;
300 unsigned nSamples() const { return m_targetSamples.size(); }
302 const std::vector<float>& getInput(unsigned i) const { return m_inputSamples[i]; }
304 const std::vector<float>& getTarget(unsigned i) const { return m_targetSamples[i]; }
305 const std::vector<int>& getevtList() const { return getEvtList(); }
306 const std::vector<int>& getEvtList() const { return m_evtList; }
307 const std::vector<int>& getExpList() const { return m_expList; }
308 const std::vector<int>& getRunList() const { return m_runList; }
309 const std::vector<int>& getSubRunList() const { return m_subRunList; }
310 const std::vector<int>& getTrackList() const { return m_trackList; }
311 const int& getevtnum(unsigned i) const { return m_evtList[i]; }
312 private:
314 std::vector<std::vector<float>> m_inputSamples;
316 std::vector<std::vector<float>> m_targetSamples;
319 std::vector<std::vector<unsigned short>> m_hitCounters;
323 std::vector<int> m_expList;
324 std::vector<int> m_runList;
325 std::vector<int> m_subRunList;
327 std::vector<int> m_evtList;
328 std::vector<int> m_trackList;
329
332 };
334}
335#endif
Struct for training data of a single MLP for the neuro trigger.
std::vector< std::vector< float > > m_inputSamples
list of input vectors for network training.
std::vector< int > m_evtList
List for debug purposes to store the event number for every entry of input/target vector.
const std::vector< float > & getInput(unsigned i) const
get input vector of sample i
const std::vector< float > & getTarget(unsigned i) const
get target value of sample i
short getTrackCounter() const
get track counter
std::vector< std::vector< float > > m_targetSamples
list of target values for network training.
ClassDef(CDCTriggerMLPData, 3)
Needed to make the ROOT object storable.
short m_trackCounter
number of tracks used for the hit counter.
void addHit(unsigned iSL, int iTS)
increase counter for super layer and track segment number in super layer.
std::vector< std::vector< unsigned short > > m_hitCounters
hit counter of active track segment IDs, used to determine the relevant id range for an MLP.
void countTrack()
increase track counter
unsigned short getHitCounter(unsigned iSL, int iTS) const
get hit counter for super layer and track segment number is super layer.
unsigned nSamples() const
get number of samples (same for input and target)
void addSample(const std::vector< float > &input, const std::vector< float > &target, const int &expnumber, const int &runnumber, const int &subrunnumber, const int &eventnumber, const int &tracknumber)
add a pair of input and target
void addCounters(unsigned nWires)
add hit counters for a layer with nWires
CDCTriggerMLPData()
default constructor.
~CDCTriggerMLPData()
destructor, empty because we don't allocate memory anywhere.
std::vector< int > m_expList
Vectors for experiment, run, event and tracknumber.
Abstract base class for different kinds of events.
Struct to keep one set of training data for either training, validation or testing.