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)"
58  " is_brl=%d, is_pix=%d, is_stereo=%d, has_charge=%d, q_bin=%.2f\n",
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_sizeof_moduleshape = sizeof(ModuleShape);
137  int f_n_layers = -1;
138 
139  GeomFileHeader() = default;
140 
141  constexpr static int s_magic = 0xB00F;
142  constexpr static int s_version = 3;
143  };
144 
145  template <typename T>
146  int write_std_vec(FILE* fp, const std::vector<T>& vec, int el_size = 0) {
147  int n = vec.size();
148  fwrite(&n, sizeof(int), 1, fp);
149  if (el_size == 0) {
150  fwrite(vec.data(), sizeof(T), n, fp);
151  } else {
152  for (int i = 0; i < n; ++i)
153  fwrite(&vec[i], el_size, 1, fp);
154  }
155  return n;
156  }
157 
158  template <typename T>
159  int read_std_vec(FILE* fp, std::vector<T>& vec, int el_size = 0) {
160  int n;
161  fread(&n, sizeof(int), 1, fp);
162  vec.resize(n);
163  if (el_size == 0) {
164  fread(vec.data(), sizeof(T), n, fp);
165  } else {
166  for (int i = 0; i < n; ++i)
167  fread(&vec[i], el_size, 1, fp);
168  }
169  return n;
170  }
171 
172  void assert_sizeof_match(int size_on_file, int size_of_class, const char* class_name) {
173  if (size_on_file != size_of_class) {
174  fprintf(stderr,
175  "sizeof(%s) on file (%d) different from current value (%d).\n",
176  class_name,
177  size_on_file,
178  size_of_class);
179  throw std::runtime_error("class sizeof mismatch for " + std::string(class_name));
180  }
181  }
182  } // namespace
183 
185  FILE* fp = fopen(fname.c_str(), "w");
186  if (!fp) {
187  fprintf(stderr,
188  "TrackerInfo::write_bin_file error opening file '%s', errno=%d: '%s'",
189  fname.c_str(),
190  errno,
191  strerror(errno));
192  throw std::runtime_error("Filed opening file in TrackerInfo::write_bin_file");
193  }
194  GeomFileHeader fh;
195  fh.f_n_layers = n_layers();
196  fwrite(&fh, sizeof(GeomFileHeader), 1, fp);
197 
198  write_std_vec(fp, m_layers, (int)offsetof(LayerInfo, m_final_member_for_streaming));
199  write_std_vec(fp, m_barrel);
200  write_std_vec(fp, m_ecap_pos);
201  write_std_vec(fp, m_ecap_neg);
202 
203  for (int l = 0; l < fh.f_n_layers; ++l) {
204  write_std_vec(fp, m_layers[l].m_modules);
205  write_std_vec(fp, m_layers[l].m_shapes);
206  }
207 
208  fwrite(&m_mat_range_z, 4 * sizeof(float), 1, fp);
209  fwrite(&m_mat_vec, 2 * sizeof(int), 1, fp);
210  write_std_vec(fp, m_mat_vec.vector());
211 
212  fclose(fp);
213  }
214 
216  FILE* fp = fopen(fname.c_str(), "r");
217  if (!fp) {
218  fprintf(stderr,
219  "TrackerInfo::read_bin_file error opening file '%s', errno=%d: '%s'\n",
220  fname.c_str(),
221  errno,
222  strerror(errno));
223  throw std::runtime_error("Failed opening file in TrackerInfo::read_bin_file");
224  }
225  GeomFileHeader fh;
226  fread(&fh, sizeof(GeomFileHeader), 1, fp);
227 
228  if (fh.f_magic != GeomFileHeader::s_magic) {
229  fprintf(stderr, "Incompatible input file (wrong magick).\n");
230  throw std::runtime_error("Filed opening file in TrackerInfo::read_bin_file");
231  }
232  if (fh.f_format_version != GeomFileHeader::s_version) {
233  fprintf(stderr,
234  "Unsupported file version %d. Supported version is %d.\n",
235  fh.f_format_version,
236  GeomFileHeader::s_version);
237  throw std::runtime_error("Unsupported file version in TrackerInfo::read_bin_file");
238  }
239  assert_sizeof_match(fh.f_sizeof_trackerinfo, sizeof(TrackerInfo), "TrackerInfo");
240  assert_sizeof_match(fh.f_sizeof_layerinfo, sizeof(LayerInfo), "LayerInfo");
241  assert_sizeof_match(fh.f_sizeof_moduleinfo, sizeof(ModuleInfo), "ModuleInfo");
242  assert_sizeof_match(fh.f_sizeof_moduleshape, sizeof(ModuleShape), "ModuleShape");
243 
244  printf("Opened TrackerInfoGeom file '%s', format version %d, n_layers %d\n",
245  fname.c_str(),
246  fh.f_format_version,
247  fh.f_n_layers);
248 
249  read_std_vec(fp, m_layers, (int)offsetof(LayerInfo, m_final_member_for_streaming));
250  read_std_vec(fp, m_barrel);
251  read_std_vec(fp, m_ecap_pos);
252  read_std_vec(fp, m_ecap_neg);
253 
254  for (int l = 0; l < fh.f_n_layers; ++l) {
255  LayerInfo& li = m_layers[l];
256  int nm = read_std_vec(fp, li.m_modules);
257 
258  li.m_detid2sid.clear();
259  for (int m = 0; m < nm; ++m) {
260  li.m_detid2sid.insert({li.m_modules[m].detid, m});
261  }
262 
263  read_std_vec(fp, li.m_shapes);
264  }
265 
266  fread(&m_mat_range_z, 4 * sizeof(float), 1, fp);
267  fread(&m_mat_vec, 2 * sizeof(int), 1, fp);
268  read_std_vec(fp, m_mat_vec.vector());
269 
270  fclose(fp);
271  }
272 
274  // clang-format off
275  if (level > 0) {
276  for (int i = 0; i < n_layers(); ++i) {
277  const LayerInfo& li = layer(i);
278  li.print_layer();
279  if (level > 1) {
280  printf(" Detailed module list N=%d, N_shapes=%d\n", li.n_modules(), li.n_shapes());
281  for (int j = 0; j < li.n_shapes(); ++j) {
282  const ModuleShape &ms = li.module_shape(j);
283  const int w = precision + 1;
284  printf(" Shape id=%u: dx1=%.*f, dx2=%.*f, dy=%.*f, dz=%.*f\n",
285  j, w, ms.dx1, w, ms.dx2, w, ms.dy, w, ms.dz);
286  }
287  for (int j = 0; j < li.n_modules(); ++j) {
288  const ModuleInfo& mi = li.module_info(j);
289  auto* p = mi.pos.Array();
290  auto* z = mi.zdir.Array();
291  auto* x = mi.xdir.Array();
292  const int w = precision;
293  printf(" Module id=%u: detid=0x%x shapeid=%u pos=%.*f,%.*f,%.*f, "
294  "norm=%.*f,%.*f,%.*f, phi=%.*f,%.*f,%.*f\n",
295  j, mi.detid, mi.shapeid, w, p[0], w, p[1], w, p[2],
296  w, z[0], w, z[1], w, z[2], w, x[0], w, x[1], w, x[2]);
297  }
298  printf("\n");
299  }
300  }
301  }
302  // clang-format on
303  }
304 } // end namespace mkfit
unsigned short shapeid
Definition: TrackerInfo.h:57
void extend_limits(float r, float z)
Definition: TrackerInfo.cc:33
void read_bin_file(const std::string &fname)
Definition: TrackerInfo.cc:215
void set_r_hole_range(float rh1, float rh2)
Definition: TrackerInfo.cc:49
T w() const
std::vector< int > m_barrel
Definition: TrackerInfo.h:285
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:287
LayerInfo & new_ecap_neg_layer()
Definition: TrackerInfo.cc:103
unsigned int detid
Definition: TrackerInfo.h:56
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
int n_shapes() const
Definition: TrackerInfo.h:159
std::vector< ModuleInfo > m_modules
Definition: TrackerInfo.h:183
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:160
int n_layers() const
Definition: TrackerInfo.h:240
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:158
const LayerInfo & layer(int l) const
Definition: TrackerInfo.h:241
rectvec< Material > m_mat_vec
Definition: TrackerInfo.h:291
int n_total_modules() const
Definition: TrackerInfo.cc:108
const ModuleShape & module_shape(unsigned short msid) const
Definition: TrackerInfo.h:161
std::vector< int > m_ecap_pos
Definition: TrackerInfo.h:286
string fname
main script
void apply_tracker_info(const TrackerInfo *ti)
Definition: TrackerInfo.cc:13
void print_tracker(int level, int precision=3) const
Definition: TrackerInfo.cc:273
void write_bin_file(const std::string &fname) const
Definition: TrackerInfo.cc:184
float x
PropagationFlags finding_intra_layer_pflags
std::unordered_map< unsigned int, unsigned int > m_detid2sid
Definition: TrackerInfo.h:182
long double T
std::vector< ModuleShape > m_shapes
Definition: TrackerInfo.h:184
std::vector< LayerInfo > m_layers
Definition: TrackerInfo.h:283
bool is_barrel() const
Definition: TrackerInfo.h:105
void reserve_layers(int n_brl, int n_ec_pos, int n_ec_neg)
Definition: TrackerInfo.cc:70