Belle II Software  release-05-01-25
TRGCDCConfig.cc
1 //-----------------------------------------------------------------------------
2 // $Id$
3 //-----------------------------------------------------------------------------
4 // Filename : TRGCDCConfig.cc
5 // Section : TRG CDC
6 // Owner : Yoshihito Iwasaki
7 // Email : yoshihito.iwasaki@kek.jp
8 //-----------------------------------------------------------------------------
9 // Description : A program to generate CDC trigger configuration data
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 <vector>
21 #include "trg/trg/Utilities.h"
22 
23 using namespace std;
24 using namespace Belle2;
25 
26 #define DEBUG_LEVEL 0
27 #define NAME "TRGCDCConfig"
28 #define VERSION "version 0.01"
29 #define ENV_PATH "BELLE2_LOCAL_DIR"
30 #define N_FRONT 500
31 #define NOT_CONNECTED 99999
32 
33 int
34 main(int argc, char* argv[])
35 {
36 
37  cout << NAME << " ... " << VERSION << endl;
38  const string tab = " ";
39 
40  //...Check arguments...
41  if (argc < 3) {
42  cout << NAME << " !!! two arguments necessary" << endl
43  << tab << " 1 : CDCWireConfig data file" << endl
44  << tab << " 2 : Version for new config file" << endl;
45  return -1;
46  }
47 
48  //...Date...
49  string ts0 = TRGUtil::dateStringF();
50  string ts1 = TRGUtil::dateString();
51 
52  //...2nd argument...
53  const string version = argv[2];
54 
55  //...1st argument...
56  const string outname = "TRGCDCConfig_" + version + "_" + ts0 + ".dat";
57 
58  //...Get path to data...
59  const string path = getenv(ENV_PATH);
60  const string inname = path + "/data/trg/" + argv[1];
61  cout << tab << "CDC Wire Config : " << inname << endl;
62  cout << tab << "CDC Trigger Config : " << outname << endl;
63 
64  //...Open configuration data...
65  ifstream infile(inname.c_str(), ios::in);
66  if (infile.fail()) {
67  cout << NAME << " !!! can not open file" << endl
68  << " " << inname << endl;
69  return -2;
70  }
71 
72  //...Get CDC geometry...
73 // TRGCDC & cdc = * TRGCDC::getTRGCDC();
74 // cdc.dump("geometry");
75 // return 0;
76 
77  //...Read configuration data
78  string cdcVersion = "";
79  char b[800];
80  unsigned lines = 0;
81  unsigned id[100];
82  string type[100];
83  unsigned sid[100];
84  unsigned nw[100];
85  unsigned lid[100];
86  unsigned asid[100];
87  unsigned assid[100];
88  while (! infile.eof()) {
89  infile.getline(b, 800);
90  string l(b);
91 
92  bool skip = false;
93  for (unsigned i = 0; i < 7; i++) {
94  string car = TRGUtil::carstring(l);
95  l = TRGUtil::cdrstring(l);
96 
97  if (car[0] == '#') {
98  skip = true;
99  break;
100  } else if (car == "CDC") {
101  cdcVersion = string(b);
102  skip = true;
103  break;
104  }
105 
106  if (i == 0) {
107  id[lines] = atoi(car.c_str());
108  } else if (i == 1) {
109  type[lines] = car;
110  } else if (i == 2) {
111  sid[lines] = atoi(car.c_str());
112  } else if (i == 3) {
113  nw[lines] = atoi(car.c_str());
114  } else if (i == 4) {
115  lid[lines] = atoi(car.c_str());
116  } else if (i == 5) {
117  asid[lines] = atoi(car.c_str());
118  } else if (i == 6) {
119  assid[lines] = atoi(car.c_str());
120  }
121  }
122 
123  if (skip)
124  continue;
125 
126  if (DEBUG_LEVEL)
127  cout << lines
128  << " " << id[lines]
129  << " " << type[lines]
130  << " " << sid[lines]
131  << " " << nw[lines]
132  << " " << lid[lines]
133  << " " << asid[lines]
134  << " " << assid[lines]
135  << endl;
136 
137  ++lines;
138  }
139  infile.close();
140 
141  //...Open configuration data...
142  ofstream outfile(outname.c_str(), ios::out);
143  if (outfile.fail()) {
144  cout << NAME << " !!! can not open file" << endl
145  << " " << outname << endl;
146  return -2;
147  }
148 
149  //...Front-end...
150  unsigned fidBase = 0;
151  vector<unsigned> front[N_FRONT];
152 
153  //...Merger...
154  unsigned midBase = 0;
155  vector<unsigned> merge[N_FRONT];
156 
157  //...Wire by wire connection table...
158  outfile << "# Generated by " << NAME << " " << VERSION << endl;
159  outfile << "# " << ts1 << endl;
160  outfile << "#" << endl;
161  outfile << cdcVersion << endl;
162  outfile << "CDC Trigger Config Version " << version << endl;
163  outfile << "#" << endl;
164  outfile << "# note : id is id of a board. Therefore tsf id is same as "
165  << "super layer id" << endl;
166  outfile << "# note : id 99999 means no connection" << endl;
167  outfile << "#" << endl;
168  outfile << "# wire id | layer id | front id | merge id | tsf id" << endl;
169  unsigned nWires = 0;
170  for (unsigned i = 0; i < lines; i++) {
171 
172  //...# of front-end...
173  const unsigned nFront = nw[i] / 16;
174 
175  //...# of merger...
176  unsigned nMerge = nFront / 4;
177  if (nFront % 4)
178  ++nMerge;
179 
180 // cout << "nMerge = " << nMerge << endl;
181 
182  //...Wire loop...
183  for (unsigned j = 0; j < nw[i]; j++) {
184 
185  //...Wire ID...
186  const unsigned wid = nWires + j;
187 
188  //...Front-end...
189  const unsigned fid = j / 16 + fidBase;
190  front[fid].push_back(wid);
191 
192  //...Merger...
193  unsigned mid = j / 64 + midBase;
194  if (fid < 10)
195  mid = NOT_CONNECTED;
196  else
197  merge[mid].push_back(wid);
198 
199  outfile.width(9);
200  outfile << wid;
201  outfile << " ";
202  outfile.width(8);
203  outfile << i;
204  outfile << " ";
205  outfile.width(8);
206  outfile << fid;
207  outfile << " ";
208  outfile.width(8);
209  outfile << mid;
210  outfile << " ";
211  outfile.width(6);
212  if (mid == NOT_CONNECTED)
213  outfile << NOT_CONNECTED;
214  else
215  outfile << sid[i];
216  outfile << endl;
217  }
218 
219  //...Front-end...
220  if (fidBase == 0) {
221  if (front[fidBase].size() == 32)
222  fidBase += nFront;
223  } else {
224  if (front[fidBase].size() == 48)
225  fidBase += nFront;
226  }
227 
228  //...Merger...
229  if (merge[midBase].size() == 384)
230  midBase += nMerge;
231 
232  //...# of wires...
233  nWires += nw[i];
234 
235  }
236 
237  //...Implementaion...
238 
239 
240  //...Termination...
241  cout << NAME << " ... terminated" << endl;
242 }
Belle2::merge
std::vector< std::vector< double > > merge(std::vector< std::vector< std::vector< double >>> toMerge)
merge { vector<double> a, vector<double> b} into {a, b}
Definition: tools.h:44
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