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 };
88
91 //template <const unsigned intype> -- no longer need template for vector
92 struct NeuroSet {
93 //To make it suitable for any input, change it to vector
94 std::vector<float> input;
95 std::vector<float> target;
96 int exp;
97 int run;
98 int subrun;
99 int evt;
100 int track;
101 int ntracks;
102 float nnscalez;
103 float nnscaletheta;
104 int nnrawz;
105 int nnrawtheta;
106 float inphi;
107 float intheta;
108 unsigned expert;
109 float ininvpt;
110 float pt; // Add to check pt dependence
111 std::string headline;
112 NeuroSet(int inlen = 27)
113 {
114 headline = "Experiment\tRun\tSubrun\tEvent\tTrack\tnTracks\tExpert\tiNodes\toNodes\t";
115 unsigned InputPerSL = inlen / 9; // Suppose always use 9 SL
116 for (unsigned i = 0; i < inlen / InputPerSL; ++i) {
117 headline += "SL" + std::to_string(i) + "-relID\t";
118 headline += "SL" + std::to_string(i) + "-driftT\t";
119 headline += "SL" + std::to_string(i) + "-alpha\t";
120 for (unsigned j = 0; j < InputPerSL - 3; ++j) {
121 headline += "SL" + std::to_string(i) + "-extra_input" + std::to_string(j) + "\t";
122 }
123 }
124 headline += "RecoZ\tRecoTheta\tScaleZ\tRawZ\tScaleTheta\tRawTheta\t2DPhi\t3DTheta\t2DinvPt\t2DPt\n";
125 }
126
127 NeuroSet(std::vector<float> xin, std::vector<float> xout, int xexp, int xrun, int xsubrun, int xevt, int xtrack, unsigned xexpert,
128 int xntracks, int xnnrawz, int xnnrawtheta, float xnnscalez, float xnnscaletheta, float xinphi, float xintheta, float xininvpt,
129 float xinpt)
130 {
131 input = xin;
132 target = xout;
133 exp = xexp;
134 run = xrun;
135 subrun = xsubrun;
136 evt = xevt;
137 track = xtrack;
138 expert = xexpert;
139
140 ntracks = xntracks;
141 nnscalez = xnnscalez;
142 nnscaletheta = xnnscaletheta;
143 nnrawz = xnnrawz;
144 nnrawtheta = xnnrawtheta;
145 inphi = xinphi;
146 intheta = xintheta;
147 ininvpt = xininvpt;
148 pt = xinpt;
149 headline = "Experiment\tRun\tSubrun\tEvent\tTrack\tnTracks\tExpert\tiNodes\toNodes\t";
150 unsigned inlen = input.size();
151 unsigned InputPerSL = inlen / 9; // Suppose always use 9 SL
152 for (unsigned i = 0; i < inlen / InputPerSL; ++i) {
153 headline += "SL" + std::to_string(i) + "-relID\t";
154 headline += "SL" + std::to_string(i) + "-driftT\t";
155 headline += "SL" + std::to_string(i) + "-alpha\t";
156 for (unsigned j = 0; j < InputPerSL - 3; ++j) {
157 headline += "SL" + std::to_string(i) + "-extra_input" + std::to_string(j) + "\t";
158 }
159 }
160 headline += "RecoZ\tRecoTheta\tScaleZ\tRawZ\tScaleTheta\tRawTheta\t2DPhi\t3DTheta\t2DinvPt\t2DPt\n";
161
162 }
163 friend std::ostream& operator << (std::ostream& out, const NeuroSet& dset)
164 {
165 out << dset.exp << '\t' << dset.run << '\t' << dset.subrun << '\t' << dset.evt << '\t' << dset.track << '\t' << dset.ntracks << '\t'
166 << dset.expert << '\t'
167 << dset.input.size() << '\t' << dset.target.size() << '\t';
168 for (auto indata : dset.input) {out << indata << '\t';}
169 for (auto outdata : dset.target) {out << outdata << '\t';}
170 out << dset.nnscalez << '\t' << dset.nnrawz << '\t' << dset.nnscaletheta << '\t' << dset.nnrawtheta << '\t' << dset.inphi << '\t' <<
171 dset.intheta << '\t' << dset.ininvpt << '\t' << dset.pt;
172 return out;
173 }
174 friend std::istream& operator >> (std::istream& in, NeuroSet& dset)
175 {
176 std::string help;
177 help = "";
178 std::getline(in, help, '\t');
179 if (help.length() < 2) {return in;}
180 dset.exp = std::stoi(help);
181 help = "";
182 std::getline(in, help, '\t');
183 dset.run = std::stoi(help);
184 help = "";
185 std::getline(in, help, '\t');
186 dset.subrun = std::stoi(help);
187 help = "";
188 std::getline(in, help, '\t');
189 dset.evt = std::stoi(help);
190 help = "";
191 std::getline(in, help, '\t');
192 dset.track = std::stoi(help);
193 help = "";
194 std::getline(in, help, '\t');
195 dset.ntracks = std::stoi(help);
196 help = "";
197 std::getline(in, help, '\t');
198 dset.expert = std::stoul(help);
199 // check if input and target size match:
200 std::string insize = "";
201 std::string outsize = "";
202 std::getline(in, insize, '\t');
203 dset.input.assign(std::stoul(insize), -1);
204 /*
205 if (std::stoul(insize) != inLen) {
206 B2ERROR("Input and output format of neurotrigger training data does not match!");
207 }
208 */
209 std::getline(in, outsize, '\t');
210 dset.target.assign(std::stoul(outsize), -1);
211 /*
212 if (std::stoul(outsize) != outLen) {
213 B2ERROR("Input and output format of neurotrigger training data does not match!");
214 }
215 */
216 for (unsigned i = 0; i < std::stoul(insize); ++i) {
217 help = "";
218 std::getline(in, help, '\t');
219 try {
220 dset.input[i] = std::stof(help);
221 } catch (const std::out_of_range& oor) {
222 B2WARNING("out of range error: " << help << " is not in float range!");
223 dset.input[i] = 0.;
224 }
225 }
226 for (unsigned i = 0; i < std::stoul(outsize); ++i) {
227 help = "";
228 std::getline(in, help, '\t');
229 try {
230 dset.target[i] = std::stof(help);
231 } catch (const std::out_of_range& oor) {
232 B2WARNING("out of range error: " << help << " is not in float range!");
233 dset.target[i] = 0.;
234 }
235 }
236 help = "";
237 std::getline(in, help, '\t');
238 dset.nnscalez = std::stof(help);
239 help = "";
240 std::getline(in, help, '\t');
241 dset.nnrawz = std::stoi(help);
242 help = "";
243 std::getline(in, help, '\t');
244 dset.nnscaletheta = std::stof(help);
245 help = "";
246 std::getline(in, help, '\t');
247 dset.nnrawtheta = std::stoi(help);
248 help = "";
249 std::getline(in, help, '\t');
250 dset.inphi = std::stof(help);
251 help = "";
252 std::getline(in, help, '\t');
253 dset.intheta = std::stof(help);
254 help = "";
255 std::getline(in, help, '\t');
256 dset.ininvpt = std::stof(help);
257 help = "";
258 return in;
259 }
260 };
261
266
268 void addCounters(unsigned nWires)
269 {
270 std::vector<unsigned short> counters;
271 counters.assign(nWires, 0);
272 m_hitCounters.push_back(counters);
273 }
274
277 void addHit(unsigned iSL, int iTS);
280 //template<unsigned inLen, unsigned outLen>
281 //void addSample(const NeuroSet<inLen, outLen>& dsample)
282 void addSample(const NeuroSet& dsample)
283 {
284 //std::vector<float> in(dsample.input, dsample.input + sizeof dsample.input / sizeof dsample.input[0]);
285 m_inputSamples.push_back(dsample.input);
286 //std::vector<float> out(dsample.target, dsample.target + sizeof dsample.target / sizeof dsample.target[0]);
287 m_targetSamples.push_back(dsample.target);
288 m_expList.push_back(dsample.exp);
289 m_runList.push_back(dsample.run);
290 m_subRunList.push_back(dsample.subrun);
291 m_evtList.push_back(dsample.evt);
292 m_trackList.push_back(dsample.track);
293
294 }
295
297 void addSample(const std::vector<float>& input, const std::vector<float>& target, const int& expnumber, const int& runnumber,
298 const int& subrunnumber, const int& eventnumber, const int& tracknumber)
299 {
300 m_inputSamples.push_back(input);
301 m_targetSamples.push_back(target);
302 m_expList.push_back(expnumber);
303 m_runList.push_back(runnumber);
304 m_subRunList.push_back(subrunnumber);
305 m_evtList.push_back(eventnumber);
306 m_trackList.push_back(tracknumber);
307 }
308
310 short getTrackCounter() const { return m_trackCounter; }
314 unsigned short getHitCounter(unsigned iSL, int iTS) const;
316 unsigned nSamples() const { return m_targetSamples.size(); }
318 const std::vector<float>& getInput(unsigned i) const { return m_inputSamples[i]; }
320 const std::vector<float>& getTarget(unsigned i) const { return m_targetSamples[i]; }
321 const std::vector<int>& getevtList() const { return getEvtList(); }
322 const std::vector<int>& getEvtList() const { return m_evtList; }
323 const std::vector<int>& getExpList() const { return m_expList; }
324 const std::vector<int>& getRunList() const { return m_runList; }
325 const std::vector<int>& getSubRunList() const { return m_subRunList; }
326 const std::vector<int>& getTrackList() const { return m_trackList; }
327 const int& getevtnum(unsigned i) const { return m_evtList[i]; }
328 private:
330 std::vector<std::vector<float>> m_inputSamples;
332 std::vector<std::vector<float>> m_targetSamples;
335 std::vector<std::vector<unsigned short>> m_hitCounters;
339 std::vector<int> m_expList;
340 std::vector<int> m_runList;
341 std::vector<int> m_subRunList;
343 std::vector<int> m_evtList;
344 std::vector<int> m_trackList;
345
348 };
349
350}
351#endif
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.
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.
ClassDef(CDCTriggerMLPData, 5)
Needed to make the ROOT object storable.
std::vector< int > m_expList
Vectors for experiment, run, event and tracknumber.
Abstract base class for different kinds of events.