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