CMS 3D CMS Logo

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