Belle II Software  release-05-01-25
TRGCDCTRGPackerTB.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGCDCTRGPackerTB.cc
5 // Section : TRG CDC
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A program to test TRGPacker on CDCFE
10 //-----------------------------------------------------------------------------
11 // $Log$
12 //-----------------------------------------------------------------------------
13 
14 #define TRG_SHORT_NAMES
15 
16 #include <cstdlib>
17 #include <iostream>
18 #include <fstream>
19 #include <string>
20 #include <map>
21 #include <string.h>
22 #include "trg/trg/Utilities.h"
23 
24 using namespace std;
25 using namespace Belle2;
26 
27 #define DEBUG_LEVEL 1
28 #define NAME "TRGCDCTRGPackerTB"
29 #define VERSION "version 0.00"
30 #define ENV_PATH "BELLE2_LOCAL_DIR"
31 #define N_FRONT 500
32 #define NOT_CONNECTED 99999
33 
34 int
35 main(int argc, char* argv[])
36 {
37 
38  cout << NAME << " ... " << VERSION << endl;
39  const string tab = " ";
40 
41  //...Check arguments...
42  if (argc < 2) {
43  cout << NAME << " !!! two arguments necessary" << endl
44  << tab << " 1 : input data file" << endl;
45  return -1;
46  }
47 
48  //...Date...
49  // string ts0 = TRGUtil::dateStringF();
50  // string ts1 = TRGUtil::dateString();
51 
52  //...Get a path to data...
53 // const string path = getenv(ENV_PATH);
54 // const string inname = path + "/data/trg/" + argv[1];
55 // const string outname = path + "/data/trg/" + argv[1] + ".out";
56  const string inname = argv[1];
57  const string outname = string(argv[1]) + ".out";
58  cout << tab << "input data : " << inname << endl;
59  cout << tab << "output data : " << outname << endl;
60 
61  //...Open configuration data...
62  ifstream infile(inname.c_str(), ios::in);
63  if (infile.fail()) {
64  cout << NAME << " !!! can not open file" << endl
65  << " " << inname << endl;
66  return -2;
67  }
68 
69  //...Read data
70  char b[800];
71  unsigned lines = 0;
72  multimap<unsigned, unsigned> signals;
73  while (! infile.eof()) {
74  infile.getline(b, 800);
75  string l(b);
76 
77  if (l.size() == 0)
78  continue;
79 
80  bool skip = false;
81  unsigned id = 999;
82  unsigned pos = 999;
83  for (unsigned i = 0; i < 2; i++) {
84  string car = TRGUtil::carstring(l);
85  l = TRGUtil::cdrstring(l);
86 
87  if (car[0] == '#') {
88  skip = true;
89  break;
90  }
91 
92  if (i == 0) {
93  id = atoi(car.c_str());
94  } else if (i == 1) {
95  pos = atoi(car.c_str());
96  }
97  }
98 
99  if (skip)
100  continue;
101 
102  //...Store signal...
103  signals.insert(pair<unsigned, unsigned>(id, pos));
104 
105  if (DEBUG_LEVEL)
106  cout << lines
107  << " : " << id
108  << " " << pos << endl;
109 
110  ++lines;
111  }
112  infile.close();
113 
114  //...Make signal bit map...
115  unsigned bm[48];
116  memset(bm, 0, sizeof(unsigned) * 48);
117  for (map<unsigned, unsigned>::iterator i = signals.begin();
118  i != signals.end();
119  ++i) {
120 
121  bm[i->first] |= (1 << i->second);
122 
123  if (DEBUG_LEVEL)
124  cout << i->first << " -> " << i->second << endl;
125  }
126 
127  //...Open output data...
128  ofstream outfile(outname.c_str(), ios::out);
129  if (outfile.fail()) {
130  cout << NAME << " !!! can not open file" << endl
131  << " " << outname << endl;
132  return -2;
133  }
134 
135  //...Make an output...
136  outfile << "Input signal bit map" << endl;
137  for (unsigned i = 0; i < 48; i++) {
138  outfile.width(2);
139  outfile << i << " : ";
140  outfile.width(6);
141  outfile << bm[i] << " : ";
142  for (unsigned j = 0; j < 32; j++) {
143  if (j && ((j % 4) == 0))
144  outfile << " ";
145  unsigned x = ((bm[i] >> (31 - j)) & 1);
146  if (x)
147  outfile << 1;
148  else
149  outfile << ".";
150  }
151  outfile << endl;
152  }
153 
154  //...Simulate TRGPacker (hit pattern in 16ns x2)...
155  outfile << "Hit pattern in 16ns" << endl;
156  outfile << " "
157  << "7654321_987654321_987654321_987654321_9876543210" << endl;
158  unsigned long long hitptn[2];
159  memset(hitptn, 0, sizeof(unsigned long long) * 2);
160  for (unsigned i = 0; i < 2; i++) {
161  for (unsigned j = 0; j < 48; j++) {
162  bool hit = (bm[j] >> (i * 16)) & 0xffff;
163  if (hit)
164  hitptn[i] |= ((unsigned long long) 1 << j);
165 // cout << "j,bm[j]=" << j << "," << bm[j] << "," << hit << ","
166 // << hitptn[i] << endl;
167 
168  }
169  outfile << i << " : ";
170  outfile.width(12);
171  outfile << hex << hitptn[i] << " : ";
172  for (unsigned k = 0; k < 48; k++) {
173  unsigned x = ((hitptn[i] >> (47 - k)) & 1);
174  if (x)
175  outfile << 1;
176  else
177  outfile << ".";
178  }
179  outfile << endl;
180  }
181 
182  //...Simulate TRGPacker (fine timing in 16ns x2)...
183  outfile << "Fine timing of central cells in 16ns" << endl;
184  unsigned fineTiming[2][48];
185  memset(fineTiming, 0, sizeof(unsigned) * 2 * 48);
186  for (unsigned i = 0; i < 2; i++) {
187  outfile << i << " : ";
188  for (unsigned j = 0; j < 48; j++) {
189  unsigned timing = (bm[j] >> (i * 16)) & 0xffff;
190  fineTiming[i][j] = timing;
191  outfile.width(4);
192  outfile << hex << fineTiming[i][j];
193  if ((j % 8) == 7)
194  outfile << endl << " ";
195  else
196  outfile << " ";
197 
198  }
199  outfile << endl;
200  }
201 
202 
203 
204 
205  //...Termination...
206  cout << NAME << " ... terminated" << endl;
207 }
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