Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-04-07 05:53:44

0001 #include "RecoTracker/MkFitCore/interface/TrackerInfo.h"
0002 
0003 #include <cassert>
0004 #include <cstring>
0005 
0006 namespace mkfit {
0007 
0008   void LayerInfo::set_limits(float r1, float r2, float z1, float z2) {
0009     m_rin = r1;
0010     m_rout = r2;
0011     m_zmin = z1;
0012     m_zmax = z2;
0013   }
0014 
0015   void LayerInfo::extend_limits(float r, float z) {
0016     if (z > m_zmax)
0017       m_zmax = z;
0018     if (z < m_zmin)
0019       m_zmin = z;
0020     if (r > m_rout)
0021       m_rout = r;
0022     if (r < m_rin)
0023       m_rin = r;
0024   }
0025 
0026   void LayerInfo::set_r_in_out(float r1, float r2) {
0027     m_rin = r1;
0028     m_rout = r2;
0029   }
0030 
0031   void LayerInfo::set_r_hole_range(float rh1, float rh2) {
0032     m_has_r_range_hole = true;
0033     m_hole_r_min = rh1;
0034     m_hole_r_max = rh2;
0035   }
0036 
0037   void LayerInfo::print_layer() const {
0038     // clang-format off
0039     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",
0040            m_layer_id,
0041            m_rin, m_rout, m_zmin, m_zmax,
0042            is_barrel(), m_is_pixel, m_is_stereo, m_q_bin);
0043     if (m_has_r_range_hole)
0044       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);
0045     // clang-format on
0046   }
0047 
0048   //==============================================================================
0049   // TrackerInfo
0050   //==============================================================================
0051 
0052   void TrackerInfo::reserve_layers(int n_brl, int n_ec_pos, int n_ec_neg) {
0053     m_layers.reserve(n_brl + n_ec_pos + n_ec_neg);
0054     m_barrel.reserve(n_brl);
0055     m_ecap_pos.reserve(n_ec_pos);
0056     m_ecap_neg.reserve(n_ec_neg);
0057   }
0058 
0059   void TrackerInfo::create_layers(int n_brl, int n_ec_pos, int n_ec_neg) {
0060     reserve_layers(n_brl, n_ec_pos, n_ec_neg);
0061     for (int i = 0; i < n_brl; ++i)
0062       new_barrel_layer();
0063     for (int i = 0; i < n_ec_pos; ++i)
0064       new_ecap_pos_layer();
0065     for (int i = 0; i < n_ec_neg; ++i)
0066       new_ecap_neg_layer();
0067   }
0068 
0069   int TrackerInfo::new_layer(LayerInfo::LayerType_e type) {
0070     int l = (int)m_layers.size();
0071     m_layers.emplace_back(LayerInfo(l, type));
0072     return l;
0073   }
0074 
0075   LayerInfo& TrackerInfo::new_barrel_layer() {
0076     m_barrel.push_back(new_layer(LayerInfo::Barrel));
0077     return m_layers.back();
0078   }
0079 
0080   LayerInfo& TrackerInfo::new_ecap_pos_layer() {
0081     m_ecap_pos.push_back(new_layer(LayerInfo::EndCapPos));
0082     return m_layers.back();
0083   }
0084 
0085   LayerInfo& TrackerInfo::new_ecap_neg_layer() {
0086     m_ecap_neg.push_back(new_layer(LayerInfo::EndCapNeg));
0087     return m_layers.back();
0088   }
0089 
0090   int TrackerInfo::n_total_modules() const {
0091     int nm = 0;
0092     for (auto& l : m_layers)
0093       nm += l.n_modules();
0094     return nm;
0095   }
0096 
0097   //==============================================================================
0098 
0099   namespace {
0100     struct GeomFileHeader {
0101       int f_magic = s_magic;
0102       int f_format_version = s_version;
0103       int f_sizeof_trackerinfo = sizeof(TrackerInfo);
0104       int f_sizeof_layerinfo = sizeof(LayerInfo);
0105       int f_sizeof_moduleinfo = sizeof(ModuleInfo);
0106       int f_n_layers = -1;
0107 
0108       GeomFileHeader() = default;
0109 
0110       constexpr static int s_magic = 0xB00F;
0111       constexpr static int s_version = 1;
0112     };
0113 
0114     template <typename T>
0115     int write_std_vec(FILE* fp, const std::vector<T>& vec, int el_size = 0) {
0116       int n = vec.size();
0117       fwrite(&n, sizeof(int), 1, fp);
0118       if (el_size == 0) {
0119         fwrite(vec.data(), sizeof(T), n, fp);
0120       } else {
0121         for (int i = 0; i < n; ++i)
0122           fwrite(&vec[i], el_size, 1, fp);
0123       }
0124       return n;
0125     }
0126 
0127     template <typename T>
0128     int read_std_vec(FILE* fp, std::vector<T>& vec, int el_size = 0) {
0129       int n;
0130       fread(&n, sizeof(int), 1, fp);
0131       vec.resize(n);
0132       if (el_size == 0) {
0133         fread(vec.data(), sizeof(T), n, fp);
0134       } else {
0135         for (int i = 0; i < n; ++i)
0136           fread(&vec[i], el_size, 1, fp);
0137       }
0138       return n;
0139     }
0140   }  // namespace
0141 
0142   void TrackerInfo::write_bin_file(const std::string& fname) const {
0143     FILE* fp = fopen(fname.c_str(), "w");
0144     if (!fp) {
0145       fprintf(stderr,
0146               "TrackerInfo::write_bin_file error opening file '%s', errno=%d: '%s'",
0147               fname.c_str(),
0148               errno,
0149               strerror(errno));
0150       throw std::runtime_error("Filed opening file in TrackerInfo::write_bin_file");
0151     }
0152     GeomFileHeader fh;
0153     fh.f_n_layers = n_layers();
0154     fwrite(&fh, sizeof(GeomFileHeader), 1, fp);
0155 
0156     write_std_vec(fp, m_layers, (int)(offsetof(LayerInfo, m_is_pixel)) + 1);
0157     write_std_vec(fp, m_barrel);
0158     write_std_vec(fp, m_ecap_pos);
0159     write_std_vec(fp, m_ecap_neg);
0160 
0161     for (int l = 0; l < fh.f_n_layers; ++l) {
0162       write_std_vec(fp, m_layers[l].m_modules);
0163     }
0164 
0165     fclose(fp);
0166   }
0167 
0168   void TrackerInfo::read_bin_file(const std::string& fname) {
0169     FILE* fp = fopen(fname.c_str(), "r");
0170     if (!fp) {
0171       fprintf(stderr,
0172               "TrackerInfo::read_bin_file error opening file '%s', errno=%d: '%s'\n",
0173               fname.c_str(),
0174               errno,
0175               strerror(errno));
0176       throw std::runtime_error("Failed opening file in TrackerInfo::read_bin_file");
0177     }
0178     GeomFileHeader fh;
0179     fread(&fh, sizeof(GeomFileHeader), 1, fp);
0180 
0181     if (fh.f_magic != GeomFileHeader::s_magic) {
0182       fprintf(stderr, "Incompatible input file (wrong magick).\n");
0183       throw std::runtime_error("Filed opening file in TrackerInfo::read_bin_file");
0184     }
0185     if (fh.f_format_version != GeomFileHeader::s_version) {
0186       fprintf(stderr,
0187               "Unsupported file version %d. Supported version is %d.\n",
0188               fh.f_format_version,
0189               GeomFileHeader::s_version);
0190       throw std::runtime_error("Unsupported file version in TrackerInfo::read_bin_file");
0191     }
0192     if (fh.f_sizeof_trackerinfo != sizeof(TrackerInfo)) {
0193       fprintf(stderr,
0194               "sizeof(TrackerInfo) on file (%d) different from current value (%d).\n",
0195               fh.f_sizeof_trackerinfo,
0196               (int)sizeof(TrackerInfo));
0197       throw std::runtime_error("sizeof(TrackerInfo) mismatch in TrackerInfo::read_bin_file");
0198     }
0199     if (fh.f_sizeof_layerinfo != sizeof(LayerInfo)) {
0200       fprintf(stderr,
0201               "sizeof(LayerInfo) on file (%d) different from current value (%d).\n",
0202               fh.f_sizeof_layerinfo,
0203               (int)sizeof(LayerInfo));
0204       throw std::runtime_error("sizeof(LayerInfo) mismatch in TrackerInfo::read_bin_file");
0205     }
0206     if (fh.f_sizeof_moduleinfo != sizeof(ModuleInfo)) {
0207       fprintf(stderr,
0208               "sizeof(ModuleInfo) on file (%d) different from current value (%d).\n",
0209               fh.f_sizeof_moduleinfo,
0210               (int)sizeof(ModuleInfo));
0211       throw std::runtime_error("sizeof(ModuleInfo) mismatch in TrackerInfo::read_bin_file");
0212     }
0213 
0214     printf("Opened TrackerInfoGeom file '%s', format version %d, n_layers %d\n",
0215            fname.c_str(),
0216            fh.f_format_version,
0217            fh.f_n_layers);
0218 
0219     read_std_vec(fp, m_layers, (int)(offsetof(LayerInfo, m_is_pixel)) + 1);
0220     read_std_vec(fp, m_barrel);
0221     read_std_vec(fp, m_ecap_pos);
0222     read_std_vec(fp, m_ecap_neg);
0223 
0224     for (int l = 0; l < fh.f_n_layers; ++l) {
0225       LayerInfo& li = m_layers[l];
0226       int nm = read_std_vec(fp, li.m_modules);
0227 
0228       li.m_detid2sid.clear();
0229       for (int m = 0; m < nm; ++m) {
0230         li.m_detid2sid.insert({li.m_modules[m].detid, m});
0231       }
0232     }
0233 
0234     fclose(fp);
0235   }
0236 
0237   void TrackerInfo::print_tracker(int level) const {
0238     if (level > 0) {
0239       int n_modules = 0;
0240       for (int i = 0; i < n_layers(); ++i) {
0241         const LayerInfo& li = layer(i);
0242         li.print_layer();
0243         n_modules += li.n_modules();
0244         if (level > 1) {
0245           printf("  Detailed module list N=%d\n", li.n_modules());
0246           for (int j = 0; j < li.n_modules(); ++j) {
0247             const ModuleInfo& mi = li.module_info(j);
0248             auto* p = mi.pos.Array();
0249             auto* z = mi.zdir.Array();
0250             auto* x = mi.xdir.Array();
0251             // clang-format off
0252             printf("Layer %d, mid=%u; detid=0x%x pos=%.3f,%.3f,%.3f, "
0253                   "norm=%.3f,%.3f,%.3f, phi=%.3f,%.3f,%.3f\n",
0254                   i, j, mi.detid, p[0], p[1], p[2],
0255                   z[0], z[1], z[2], x[0], x[1], x[2]);
0256             // clang-format on
0257           }
0258           printf("\n");
0259         }
0260       }
0261     }
0262   }
0263 }  // end namespace mkfit