Belle II Software  release-05-01-25
TRGCDCTSStreamReader.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGCDCTSStreamReader.cc
5 // Section : TRG CDC
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A program to read Track Segment (TS) stream data
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #define TRG_SHORT_NAMES
15 #define TRGCDC_SHORT_NAMES
16 
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include "trg/trg/BitStream.h"
21 #include "trg/cdc/Wire.h"
22 #ifdef TRGCDC_DISPLAY
23 #include "framework/gearbox/Gearbox.h"
24 #include "trg/cdc/DisplayRphi.h"
25 #include "trg/cdc/DisplayHough.h"
26 namespace Belle2_TRGCDC {
27  Belle2::TRGCDCDisplayRphi* D = 0;
28 }
29 #endif
30 
31 using namespace std;
32 using namespace Belle2;
33 #ifdef TRGCDC_DISPLAY
34 using namespace Belle2_TRGCDC;
35 #endif
36 
37 #define DEBUG_LEVEL 1
38 #define PROGRAM_NAME "TRGCDCTSStreamReader"
39 #define PROGRAM_VERSION "version 0.03"
40 #define ENV_PATH "BELLE2_LOCAL_DIR"
41 #define CONFIG "TRGCDCWireConfig_0_20101110_0836.dat"
42 
43 int
44 main(int argc, char* argv[])
45 {
46 
47  cout << PROGRAM_NAME << " ... " << PROGRAM_VERSION << endl;
48  const string tab = " ";
49 
50  //...Check arguments...
51  if (argc < 2) {
52  cout << PROGRAM_NAME << " !!! one argument necessary" << endl
53  << tab << " 1 : Track Segment Stream data file" << endl;
54  return -1;
55  }
56 
57  //...1st argument...
58  const string inname = argv[1];
59  cout << tab << "Track Segment Stream : " << inname << endl;
60 
61  //...Open configuration data...
62  ifstream infile(inname.c_str(), ios::in | ios::binary);
63  if (infile.fail()) {
64  cout << PROGRAM_NAME << " !!! can not open file" << endl
65  << " " << inname << endl;
66  return -2;
67  }
68 
69 #ifdef TRGCDC_DISPLAY
70  //...Gearbox...
71  const string path = getenv(ENV_PATH);
72  std::vector<std::string> m_backends;
73  std::string m_filename = path + "/data/geometry/Belle2.xml";
74  m_backends.push_back("file:");
75  Gearbox& gearbox = Gearbox::getInstance();
76  gearbox.setBackends(m_backends);
77  gearbox.open(m_filename);
78 
79  //...TRGCDC...
80  const string cname = path + "/data/trg/" + CONFIG;
81 // TRGCDC * cdc = TRGCDC::getTRGCDC(cname, false, 100, 100);
82  TRGCDC* cdc = TRGCDC::getTRGCDC(cname);
83  const TRGClock& clock = cdc->systemClock();
84 
85  //...Wire layer ID for the central wires...
86  unsigned LID[9] = {2, 10, 16, 22, 28, 34, 40, 46, 52};
87 
88  //...Display...
89  D->clear();
90  D->show();
91 #endif
92 
93  //...Prepare buffers...
94  vector<int*> clks;
95  vector<TRGBitStream*> bits[9];
96  vector<const TCWire*> wires;
97 
98  //...Read stream...
99  while (! infile.eof()) {
100 
101  //...Read record type...
102  unsigned rec = 0;
103  infile.read((char*) & rec, 4);
104  unsigned siz = 0;
105  infile.read((char*) & siz, 4);
106 
107  cout << "... " << hex << rec << "," << dec << siz << endl;
108 
109  //...Record manipulation...
110  switch (rec) {
111 
112  case TRGBSRecord_Comment: {
113  unsigned csiz = siz / 8;
114  if (siz % 8) ++csiz;
115  char* buf = new char[csiz + 1];
116  infile.read(buf, csiz);
117  buf[csiz] = 0;
118 
119  cout << "siz,csiz=" << siz << "," << csiz << endl;
120 
121  if (DEBUG_LEVEL)
122  cout << "Comment : " << buf << endl;
123  break;
124  }
125 
126  case TRGBSRecord_BeginRun:
127  if (DEBUG_LEVEL)
128  cout << "BeginRun : " << siz << endl;
129  break;
130 
131  case TRGBSRecord_EndRun:
132  if (DEBUG_LEVEL)
133  cout << "EndRun : " << siz << endl;
134  break;
135 
136  case TRGBSRecord_BeginEvent:
137 #ifdef TRGCDC_DISPLAY
138  D->clear();
139  D->beginningOfEvent();
140 #endif
141  for (unsigned i = 0; i < 9; i++) {
142  for (unsigned j = 0; j < bits[i].size(); j++)
143  delete bits[i][j];
144  bits[i].clear();
145  }
146  for (unsigned i = 0; i < clks.size(); i++)
147  delete clks[i];
148  clks.clear();
149  wires.clear();
150  if (DEBUG_LEVEL)
151  cout << "BeginEvent : " << siz << endl;
152  break;
153 
154  case TRGBSRecord_EndEvent:
155 #ifdef TRGCDC_DISPLAY
156  for (unsigned i = 0; i < 9; i++) {
157  vector<TRGSignal> t = TRGBitStream::TRGBitStream2TRGSignal(
158  clock,
159  0,
160  bits[i]);
161  for (unsigned j = 0; j < t.size(); j++) {
162  if (t[j].active())
163  wires.push_back(cdc->wire(LID[i], j));
164  }
165  }
166  D->area().append(wires);
167  D->endOfEvent();
168  D->run();
169 
170 #endif
171  if (DEBUG_LEVEL)
172  cout << "EndEvent : " << siz << endl;
173  break;
174 
175  case TRGBSRecord_Clock: {
176  unsigned clk = 0;
177  infile.read((char*) & clk, 4);
178  clks.push_back(new int(clk));
179  if (DEBUG_LEVEL)
180  cout << "Clock : " << clk << endl;
181  break;
182  }
183 
184  case TRGBSRecord_SegmentSL0:
185  case TRGBSRecord_SegmentSL1:
186  case TRGBSRecord_SegmentSL2:
187  case TRGBSRecord_SegmentSL3:
188  case TRGBSRecord_SegmentSL4:
189  case TRGBSRecord_SegmentSL5:
190  case TRGBSRecord_SegmentSL6:
191  case TRGBSRecord_SegmentSL7:
192  case TRGBSRecord_SegmentSL8: {
193  unsigned superLayer = rec - TRGBSRecord_SegmentSL0;
194  unsigned csiz = siz / 8;
195  if (siz % 8) ++csiz;
196  char* buf = new char[csiz];
197  infile.read(buf, csiz);
198  TRGBitStream* bs = new TRGBitStream(buf, siz);
199  bits[superLayer].push_back(bs);
200  if (DEBUG_LEVEL)
201  bs->dump();
202  break;
203  }
204 
205  default:
206  cout << PROGRAM_NAME << " !!! unknown record found : "
207  << "record type = " << hex << rec << endl;
208  break;
209  }
210  }
211  infile.close();
212 
213  //...Termination...
214  cout << PROGRAM_NAME << " ... terminated" << endl;
215 }
Belle2::TRGBitStream
A class to represent a bit stream.
Definition: BitStream.h:50
Belle2::TRGBitStream::dump
void dump(const std::string &message="", const std::string &pre="") const
dumps contents. "message" is to select information to dump. "pre" will be printed in head of each lin...
Definition: BitStream.cc:81
main
int main(int argc, char **argv)
Run all tests.
Definition: test_main.cc:77
Belle2
Abstract base class for different kinds of events.
Definition: MillepedeAlgorithm.h:19
Belle2::Gearbox
Singleton class responsible for loading detector parameters from an XML file.
Definition: Gearbox.h:44
Belle2::TRGCDC
The instance of TRGCDC is a singleton.
Definition: TRGCDC.h:70
Belle2::TRGClock
A class to represent a digitized signal. Unit is nano second.
Definition: Clock.h:43