CMS 3D CMS Logo

TrackerInfo.cc
Go to the documentation of this file.
2 
3 #include <cassert>
4 #include <cstring>
5 
6 namespace mkfit {
7 
8  void LayerInfo::set_limits(float r1, float r2, float z1, float z2) {
9  m_rin = r1;
10  m_rout = r2;
11  m_zmin = z1;
12  m_zmax = z2;
13  }
14 
15  void LayerInfo::extend_limits(float r, float z) {
16  if (z > m_zmax)
17  m_zmax = z;
18  if (z < m_zmin)
19  m_zmin = z;
20  if (r > m_rout)
21  m_rout = r;
22  if (r < m_rin)
23  m_rin = r;
24  }
25 
26  void LayerInfo::set_r_in_out(float r1, float r2) {
27  m_rin = r1;
28  m_rout = r2;
29  }
30 
31  void LayerInfo::set_r_hole_range(float rh1, float rh2) {
32  m_has_r_range_hole = true;
33  m_hole_r_min = rh1;
34  m_hole_r_max = rh2;
35  }
36 
37  void LayerInfo::print_layer() const {
38  // clang-format off
39  printf("Layer %2d r(%7.4f, %7.4f) z(% 9.4f, % 9.4f) is_brl=%d, is_pix=%d, is_stereo=%d, q_bin=%.2f\n",
40  m_layer_id,
44  printf(" has_r_range_hole: %.2f -> %.2f, dr: %f\n", m_hole_r_min, m_hole_r_max, m_hole_r_max - m_hole_r_min);
45  // clang-format on
46  }
47 
48  //==============================================================================
49  // TrackerInfo
50  //==============================================================================
51 
52  void TrackerInfo::reserve_layers(int n_brl, int n_ec_pos, int n_ec_neg) {
53  m_layers.reserve(n_brl + n_ec_pos + n_ec_neg);
54  m_barrel.reserve(n_brl);
55  m_ecap_pos.reserve(n_ec_pos);
56  m_ecap_neg.reserve(n_ec_neg);
57  }
58 
59  void TrackerInfo::create_layers(int n_brl, int n_ec_pos, int n_ec_neg) {
60  reserve_layers(n_brl, n_ec_pos, n_ec_neg);
61  for (int i = 0; i < n_brl; ++i)
63  for (int i = 0; i < n_ec_pos; ++i)
65  for (int i = 0; i < n_ec_neg; ++i)
67  }
68 
70  int l = (int)m_layers.size();
71  m_layers.emplace_back(LayerInfo(l, type));
72  return l;
73  }
74 
77  return m_layers.back();
78  }
79 
82  return m_layers.back();
83  }
84 
87  return m_layers.back();
88  }
89 
91  int nm = 0;
92  for (auto& l : m_layers)
93  nm += l.n_modules();
94  return nm;
95  }
96 
97  //==============================================================================
98 
99  namespace {
100  struct GeomFileHeader {
101  int f_magic = s_magic;
102  int f_format_version = s_version;
103  int f_sizeof_trackerinfo = sizeof(TrackerInfo);
104  int f_sizeof_layerinfo = sizeof(LayerInfo);
105  int f_sizeof_moduleinfo = sizeof(ModuleInfo);
106  int f_n_layers = -1;
107 
108  GeomFileHeader() = default;
109 
110  constexpr static int s_magic = 0xB00F;
111  constexpr static int s_version = 1;
112  };
113 
114  template <typename T>
115  int write_std_vec(FILE* fp, const std::vector<T>& vec, int el_size = 0) {
116  int n = vec.size();
117  fwrite(&n, sizeof(int), 1, fp);
118  if (el_size == 0) {
119  fwrite(vec.data(), sizeof(T), n, fp);
120  } else {
121  for (int i = 0; i < n; ++i)
122  fwrite(&vec[i], el_size, 1, fp);
123  }
124  return n;
125  }
126 
127  template <typename T>
128  int read_std_vec(FILE* fp, std::vector<T>& vec, int el_size = 0) {
129  int n;
130  fread(&n, sizeof(int), 1, fp);
131  vec.resize(n);
132  if (el_size == 0) {
133  fread(vec.data(), sizeof(T), n, fp);
134  } else {
135  for (int i = 0; i < n; ++i)
136  fread(&vec[i], el_size, 1, fp);
137  }
138  return n;
139  }
140  } // namespace
141 
143  FILE* fp = fopen(fname.c_str(), "w");
144  if (!fp) {
145  fprintf(stderr,
146  "TrackerInfo::write_bin_file error opening file '%s', errno=%d: '%s'",
147  fname.c_str(),
148  errno,
149  strerror(errno));
150  throw std::runtime_error("Filed opening file in TrackerInfo::write_bin_file");
151  }
152  GeomFileHeader fh;
153  fh.f_n_layers = n_layers();
154  fwrite(&fh, sizeof(GeomFileHeader), 1, fp);
155 
156  write_std_vec(fp, m_layers, (int)(offsetof(LayerInfo, m_is_pixel)) + 1);
157  write_std_vec(fp, m_barrel);
158  write_std_vec(fp, m_ecap_pos);
159  write_std_vec(fp, m_ecap_neg);
160 
161  for (int l = 0; l < fh.f_n_layers; ++l) {
162  write_std_vec(fp, m_layers[l].m_modules);
163  }
164 
165  fclose(fp);
166  }
167 
169  FILE* fp = fopen(fname.c_str(), "r");
170  if (!fp) {
171  fprintf(stderr,
172  "TrackerInfo::read_bin_file error opening file '%s', errno=%d: '%s'\n",
173  fname.c_str(),
174  errno,
175  strerror(errno));
176  throw std::runtime_error("Failed opening file in TrackerInfo::read_bin_file");
177  }
178  GeomFileHeader fh;
179  fread(&fh, sizeof(GeomFileHeader), 1, fp);
180 
181  if (fh.f_magic != GeomFileHeader::s_magic) {
182  fprintf(stderr, "Incompatible input file (wrong magick).\n");
183  throw std::runtime_error("Filed opening file in TrackerInfo::read_bin_file");
184  }
185  if (fh.f_format_version != GeomFileHeader::s_version) {
186  fprintf(stderr,
187  "Unsupported file version %d. Supported version is %d.\n",
188  fh.f_format_version,
189  GeomFileHeader::s_version);
190  throw std::runtime_error("Unsupported file version in TrackerInfo::read_bin_file");
191  }
192  if (fh.f_sizeof_trackerinfo != sizeof(TrackerInfo)) {
193  fprintf(stderr,
194  "sizeof(TrackerInfo) on file (%d) different from current value (%d).\n",
195  fh.f_sizeof_trackerinfo,
196  (int)sizeof(TrackerInfo));
197  throw std::runtime_error("sizeof(TrackerInfo) mismatch in TrackerInfo::read_bin_file");
198  }
199  if (fh.f_sizeof_layerinfo != sizeof(LayerInfo)) {
200  fprintf(stderr,
201  "sizeof(LayerInfo) on file (%d) different from current value (%d).\n",
202  fh.f_sizeof_layerinfo,
203  (int)sizeof(LayerInfo));
204  throw std::runtime_error("sizeof(LayerInfo) mismatch in TrackerInfo::read_bin_file");
205  }
206  if (fh.f_sizeof_moduleinfo != sizeof(ModuleInfo)) {
207  fprintf(stderr,
208  "sizeof(ModuleInfo) on file (%d) different from current value (%d).\n",
209  fh.f_sizeof_moduleinfo,
210  (int)sizeof(ModuleInfo));
211  throw std::runtime_error("sizeof(ModuleInfo) mismatch in TrackerInfo::read_bin_file");
212  }
213 
214  printf("Opened TrackerInfoGeom file '%s', format version %d, n_layers %d\n",
215  fname.c_str(),
216  fh.f_format_version,
217  fh.f_n_layers);
218 
219  read_std_vec(fp, m_layers, (int)(offsetof(LayerInfo, m_is_pixel)) + 1);
220  read_std_vec(fp, m_barrel);
221  read_std_vec(fp, m_ecap_pos);
222  read_std_vec(fp, m_ecap_neg);
223 
224  for (int l = 0; l < fh.f_n_layers; ++l) {
225  LayerInfo& li = m_layers[l];
226  int nm = read_std_vec(fp, li.m_modules);
227 
228  li.m_detid2sid.clear();
229  for (int m = 0; m < nm; ++m) {
230  li.m_detid2sid.insert({li.m_modules[m].detid, m});
231  }
232  }
233 
234  fclose(fp);
235  }
236 
238  if (level > 0) {
239  for (int i = 0; i < n_layers(); ++i) {
240  const LayerInfo& li = layer(i);
241  li.print_layer();
242  if (level > 1) {
243  printf(" Detailed module list N=%d\n", li.n_modules());
244  for (int j = 0; j < li.n_modules(); ++j) {
245  const ModuleInfo& mi = li.module_info(j);
246  auto* p = mi.pos.Array();
247  auto* z = mi.zdir.Array();
248  auto* x = mi.xdir.Array();
249  // clang-format off
250  printf("Layer %d, mid=%u; detid=0x%x pos=%.3f,%.3f,%.3f, "
251  "norm=%.3f,%.3f,%.3f, phi=%.3f,%.3f,%.3f\n",
252  i, j, mi.detid, p[0], p[1], p[2],
253  z[0], z[1], z[2], x[0], x[1], x[2]);
254  // clang-format on
255  }
256  printf("\n");
257  }
258  }
259  }
260  }
261 } // end namespace mkfit
void extend_limits(float r, float z)
Definition: TrackerInfo.cc:15
void read_bin_file(const std::string &fname)
Definition: TrackerInfo.cc:168
void set_r_hole_range(float rh1, float rh2)
Definition: TrackerInfo.cc:31
std::vector< int > m_barrel
Definition: TrackerInfo.h:184
void print_layer() const
Definition: TrackerInfo.cc:37
LayerInfo & new_barrel_layer()
Definition: TrackerInfo.cc:75
std::vector< int > m_ecap_neg
Definition: TrackerInfo.h:186
LayerInfo & new_ecap_neg_layer()
Definition: TrackerInfo.cc:85
unsigned int detid
Definition: TrackerInfo.h:28
void set_r_in_out(float r1, float r2)
Definition: TrackerInfo.cc:26
std::vector< ModuleInfo > m_modules
Definition: TrackerInfo.h:137
void set_limits(float r1, float r2, float z1, float z2)
Definition: TrackerInfo.cc:8
const ModuleInfo & module_info(unsigned int sid) const
Definition: TrackerInfo.h:118
int n_layers() const
Definition: TrackerInfo.h:161
LayerInfo & new_ecap_pos_layer()
Definition: TrackerInfo.cc:80
int new_layer(LayerInfo::LayerType_e type)
Definition: TrackerInfo.cc:69
void create_layers(int n_brl, int n_ec_pos, int n_ec_neg)
Definition: TrackerInfo.cc:59
int n_modules() const
Definition: TrackerInfo.h:117
const LayerInfo & layer(int l) const
Definition: TrackerInfo.h:162
int n_total_modules() const
Definition: TrackerInfo.cc:90
void print_tracker(int level) const
Definition: TrackerInfo.cc:237
std::vector< int > m_ecap_pos
Definition: TrackerInfo.h:185
string fname
main script
void write_bin_file(const std::string &fname) const
Definition: TrackerInfo.cc:142
float x
std::unordered_map< unsigned int, unsigned int > m_detid2sid
Definition: TrackerInfo.h:136
long double T
std::vector< LayerInfo > m_layers
Definition: TrackerInfo.h:182
bool is_barrel() const
Definition: TrackerInfo.h:68
void reserve_layers(int n_brl, int n_ec_pos, int n_ec_neg)
Definition: TrackerInfo.cc:52