Belle II Software  release-08-01-10
TRGCDCTSStreamModule.cc
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 
9 //-----------------------------------------------------------------------------
10 // Description : A trigger module for CDC
11 //-----------------------------------------------------------------------------
12 
13 #define TRGCDC_SHORT_NAMES
14 
15 #include <iostream>
16 #include <fstream>
17 
18 #include "trg/cdc/modules/tsstream/TRGCDCTSStreamModule.h"
19 #include "trg/trg/Debug.h"
20 #include "trg/trg/BitStream.h"
21 #include "trg/cdc/Wire.h"
22 #include "trg/cdc/Segment.h"
23 #include "trg/cdc/Layer.h"
24 
25 using namespace std;
26 
27 namespace Belle2 {
34  REG_MODULE(TRGCDCTSStream);
35 
36  string
37  TRGCDCTSStreamModule::version() const
38  {
39  return string("TRGCDCTSStreamModule 0.00");
40  }
41 
42  TRGCDCTSStreamModule::TRGCDCTSStreamModule()
43  : Module::Module(),
44  _debugLevel(0),
45  _mode(0),
46  _streamFilename("unknown"),
47  _cdc(0),
48  _out(0)
49  {
50 
51  string desc = "TRGCDCTSStreamModule(" + version() + ")";
52  setDescription(desc);
53 
54  addParam("DebugLevel",
56  "TRGCDCTSStream debug level",
57  _debugLevel);
58 
59  addParam("Mode", _mode, "TRGCDCTSStream mode", _mode);
60 
61  addParam("OutputStreamFile",
63  "The filename of bit stream",
65 
66  if (TRGDebug::level())
67  cout << "TRGCDCTSStreamModule ... created" << endl;
68  }
69 
71  {
72 
73  if (_cdc)
74  TRGCDC::getTRGCDC("good-bye");
75 
76  if (TRGDebug::level())
77  cout << "TRGCDCTSStreamModule ... destructed " << endl;
78  }
79 
80  void
82  {
83  if (TRGDebug::level()) {
84  cout << "TRGCDCTSStreamModule::initialize ... options" << endl;
85  cout << TRGDebug::tab(4) << "debug level = " << _debugLevel << endl;
86  cout << TRGDebug::tab(4) << " mode = " << _mode << endl;
87  cout << TRGDebug::tab(4) << "output file = " << _streamFilename << endl;
88  }
89 
90  if (_streamFilename != "unknown") {
91  _out = new ofstream(_streamFilename.c_str(), ios::out | ios::binary);
92  unsigned val = TRGBSRecord_Comment;
93  _out->write((char*) & val, 4);
94  const string cmt = "test data ";
95  val = cmt.size() * 8;
96  _out->write((char*) & val, 4);
97  _out->write(cmt.c_str(), cmt.size());
98  }
99  }
100 
101  void
103  {
104 
106 
107  //...Super layer loop...
108  for (unsigned l = 0; l < _cdc->nSegmentLayers(); l++) {
109  const Belle2::TRGCDCLayer* lyr = _cdc->segmentLayer(l);
110  const unsigned nWires = lyr->nCells();
111 
112  //...Clear old pointers...
113  _wires[l].clear();
114 
115  //...TS loop...
116  for (unsigned i = 0; i < nWires; i++) {
117  const TCSegment& s = (TCSegment&) * (* lyr)[i];
118  _wires[l].push_back(s.wires()[5]);
119  }
120  }
121 
122  if (_out) {
123  unsigned val = TRGBSRecord_BeginRun;
124  _out->write((char*) & val, 4);
125  val = 0;
126  _out->write((char*) & val, 4);
127  }
128 
129  if (TRGDebug::level())
130  cout << "TRGCDCTSStreamModule ... beginRun called. TRGCDC version="
131  << _cdc->version() << endl;
132  }
133 
134  void
136  {
137 
138  //...To dump wire hits...
139  if (TRGDebug::level())
140  _cdc->dump("trgWireCentralHits");
141 
142  if (_out) {
143  unsigned val = TRGBSRecord_BeginEvent;
144  _out->write((char*) & val, 4);
145  val = 0;
146  _out->write((char*) & val, 4);
147  }
148 
149  //...Clock loop (from 0 to 99 cycles, about 800 ns)...
150  for (unsigned c = 0; c < 100; c++) {
151 
152  if (_out) {
153  unsigned val = TRGBSRecord_Clock;
154  _out->write((char*) & val, 4);
155  val = 32;
156  _out->write((char*) & val, 4);
157  val = c;
158  _out->write((char*) & val, 4);
159  }
160 
161  //...Super layer loop...
162  for (unsigned l = 0; l < 9; l++) {
163 
164  //...Bit stream for this super layer...
165  TRGBitStream stream;
166 
167  //...Wire loop...
168  for (unsigned i = 0; i < _wires[l].size(); i++) {
169  const TRGSignal& s = _wires[l][i]->signal();
170  bool hit = s.state(c);
171  stream.append(hit);
172  }
173 
174  if (_out) {
175  unsigned val = TRGBSRecord_SegmentSL0;
176  val += l;
177  _out->write((char*) & val, 4);
178  val = stream.size();
179  _out->write((char*) & val, 4);
180  for (unsigned i = 0; i < stream.sizeInChar(); i++) {
181  char cs = stream.c(i);
182  _out->write(& cs, 1);
183  }
184  }
185 
186  if (TRGDebug::level()) {
187  cout << "Super layer " << l << ", clock " << c << endl;
188  stream.dump();
189  }
190  }
191  }
192 
193  if (_out) {
194  unsigned val = TRGBSRecord_EndEvent;
195  _out->write((char*) & val, 4);
196  val = 0;
197  _out->write((char*) & val, 4);
198  }
199 
200  }
201 
202  void
204  {
205  if (_out) {
206  unsigned val = TRGBSRecord_EndRun;
207  _out->write((char*) & val, 4);
208  val = 0;
209  _out->write((char*) & val, 4);
210  }
211 
212  if (TRGDebug::level())
213  cout << "TRGCDCTSStreamModule ... endRun called " << endl;
214  }
215 
216  void
218  {
219  if (_out) {
220  _out->flush();
221  _out->close();
222  }
223 
224  if (TRGDebug::level())
225  cout << "TRGCDCTSStreamModule ... terminate called " << endl;
226  }
227 
229 } // namespace Belle2
Base class for Modules.
Definition: Module.h:72
void setDescription(const std::string &description)
Sets the description of the module.
Definition: Module.cc:214
A class to represent a bit stream.
Definition: BitStream.h:45
A class to represent a cell layer.
Definition: Layer.h:33
std::ofstream * _out
A pointer to an output file.
int _mode
Mode for streaming data.
std::string _streamFilename
The filename of bit stream.
std::vector< const TRGCDCWire * > _wires[9]
Storage for TS central wires.
TRGCDC * _cdc
A pointer to a TRGCDC;.
A class to represent a digitized signal. Unit is nano second.
Definition: Signal.h:23
void addParam(const std::string &name, T &paramVariable, const std::string &description, const T &defaultValue)
Adds a new parameter to the module.
Definition: Module.h:560
#define REG_MODULE(moduleName)
Register the given module (without 'Module' suffix) with the framework.
Definition: Module.h:650
virtual void initialize() override
Initilizes TRGCDCTSStreamModule.
virtual void event() override
Called event by event.
virtual void endRun() override
Called when run ended.
virtual void terminate() override
Called when processing ended.
virtual void beginRun() override
Called when new run started.
std::string version(void) const
returns version of TRGCDCTSStreamModule.
virtual ~TRGCDCTSStreamModule()
Destructor.
static std::string tab(void)
returns tab spaces.
Definition: Debug.cc:47
unsigned nSegmentLayers(void) const
returns # of track segment layers.
Definition: TRGCDC.h:1070
static TRGCDC * getTRGCDC(void)
returns TRGCDC object.
Definition: TRGCDC.cc:192
const TRGCDCLayer * segmentLayer(unsigned id) const
returns a pointer to a track segment layer.
Definition: TRGCDC.h:1061
void dump(const std::string &message) const
dumps debug information.
Definition: TRGCDC.cc:767
static int level(void)
returns the debug level.
Definition: Debug.cc:67
unsigned nCells(void) const
returns # of cells.
Definition: Layer.h:194
std::string version(void) const
returns version.
Definition: TRGCDC.cc:98
Abstract base class for different kinds of events.