Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2022-05-01 22:52:19

0001 #ifndef RecoTracker_MkFitCore_interface_IterationConfig_h
0002 #define RecoTracker_MkFitCore_interface_IterationConfig_h
0003 
0004 #include "RecoTracker/MkFitCore/interface/SteeringParams.h"
0005 
0006 #include "nlohmann/json_fwd.hpp"
0007 
0008 #include <functional>
0009 
0010 namespace mkfit {
0011 
0012   class EventOfHits;
0013   class TrackerInfo;
0014   class Track;
0015 
0016   typedef std::vector<Track> TrackVec;
0017 
0018   //==============================================================================
0019   // Hit masks / IterationMaskIfc
0020   //==============================================================================
0021 
0022   struct IterationMaskIfcBase {
0023     virtual ~IterationMaskIfcBase() {}
0024 
0025     virtual const std::vector<bool> *get_mask_for_layer(int layer) const { return nullptr; }
0026   };
0027 
0028   struct IterationMaskIfc : public IterationMaskIfcBase {
0029     std::vector<std::vector<bool>> m_mask_vector;
0030 
0031     const std::vector<bool> *get_mask_for_layer(int layer) const override { return &m_mask_vector[layer]; }
0032   };
0033 
0034   //==============================================================================
0035   // IterationLayerConfig
0036   //==============================================================================
0037 
0038   class IterationConfig;
0039 
0040   class IterationLayerConfig {
0041   public:
0042     // Selection limits.
0043     float m_select_min_dphi;
0044     float m_select_max_dphi;
0045     float m_select_min_dq;
0046     float m_select_max_dq;
0047 
0048     void set_selection_limits(float p1, float p2, float q1, float q2) {
0049       m_select_min_dphi = p1;
0050       m_select_max_dphi = p2;
0051       m_select_min_dq = q1;
0052       m_select_max_dq = q2;
0053     }
0054 
0055     //----------------------------------------------------------------------------
0056 
0057     float min_dphi() const { return m_select_min_dphi; }
0058     float max_dphi() const { return m_select_max_dphi; }
0059     float min_dq() const { return m_select_min_dq; }
0060     float max_dq() const { return m_select_max_dq; }
0061 
0062     //Hit selection windows: 2D fit/layer (72 in phase-1 CMS geometry)
0063     //cut = [0]*1/pT + [1]*std::fabs(theta-pi/2) + [2])
0064     float c_dp_sf = 1.1;
0065     float c_dp_0 = 0.0;
0066     float c_dp_1 = 0.0;
0067     float c_dp_2 = 0.0;
0068     //
0069     float c_dq_sf = 1.1;
0070     float c_dq_0 = 0.0;
0071     float c_dq_1 = 0.0;
0072     float c_dq_2 = 0.0;
0073     //
0074     float c_c2_sf = 1.1;
0075     float c_c2_0 = 0.0;
0076     float c_c2_1 = 0.0;
0077     float c_c2_2 = 0.0;
0078 
0079     //----------------------------------------------------------------------------
0080 
0081     IterationLayerConfig() {}
0082   };
0083 
0084   //==============================================================================
0085   // IterationParams
0086   //==============================================================================
0087 
0088   class IterationParams {
0089   public:
0090     int nlayers_per_seed = 3;
0091     int maxCandsPerSeed = 5;
0092     int maxHolesPerCand = 4;
0093     int maxConsecHoles = 1;
0094     float chi2Cut_min = 15.0;
0095     float chi2CutOverlap = 3.5;
0096     float pTCutOverlap = 0.0;
0097 
0098     //seed cleaning params
0099     float c_ptthr_hpt = 2.0;
0100     //initial
0101     float c_drmax_bh = 0.010;
0102     float c_dzmax_bh = 0.005;
0103     float c_drmax_eh = 0.020;
0104     float c_dzmax_eh = 0.020;
0105     float c_drmax_bl = 0.010;
0106     float c_dzmax_bl = 0.005;
0107     float c_drmax_el = 0.030;
0108     float c_dzmax_el = 0.030;
0109 
0110     int minHitsQF = 4;
0111     float fracSharedHits = 0.19;
0112     float drth_central = 0.001;
0113     float drth_obarrel = 0.001;
0114     float drth_forward = 0.001;
0115 
0116     //min pT cut
0117     float minPtCut = 0.0;
0118 
0119     //max cluster size cut for SiStrip hits
0120     unsigned int maxClusterSize = 8;
0121   };
0122 
0123   //==============================================================================
0124   // IterationSeedPartition
0125   //==============================================================================
0126 
0127   class IterationSeedPartition {
0128   public:
0129     using register_seed_phi_eta_foo = void(float, float);
0130 
0131     std::vector<int> m_region;
0132     std::function<register_seed_phi_eta_foo> m_phi_eta_foo;
0133 
0134     IterationSeedPartition(int size) : m_region(size) {}
0135   };
0136 
0137   //==============================================================================
0138   // IterationConfig
0139   //==============================================================================
0140 
0141   class IterationConfig {
0142   public:
0143     using partition_seeds_foo = void(const TrackerInfo &,
0144                                      const TrackVec &,
0145                                      const EventOfHits &,
0146                                      IterationSeedPartition &);
0147 
0148     int m_iteration_index = -1;
0149     int m_track_algorithm = -1;
0150 
0151     bool m_requires_seed_hit_sorting = false;
0152     bool m_requires_quality_filter = false;
0153     bool m_requires_dupclean_tight = false;
0154 
0155     bool m_backward_search = false;
0156     bool m_backward_drop_seed_hits = false;
0157 
0158     int m_backward_fit_min_hits = -1;  // Min number of hits to keep when m_backward_drop_seed_hits is true
0159 
0160     // Iteration parameters (could be a ptr)
0161     IterationParams m_params;
0162     IterationParams m_backward_params;
0163 
0164     int m_n_regions = -1;
0165     std::vector<int> m_region_order;
0166     std::vector<SteeringParams> m_steering_params;
0167     std::vector<IterationLayerConfig> m_layer_configs;
0168 
0169     std::function<partition_seeds_foo> m_partition_seeds;
0170 
0171     //----------------------------------------------------------------------------
0172 
0173     IterationConfig() {}
0174 
0175     // -------- Getter functions
0176 
0177     IterationLayerConfig &layer(int i) { return m_layer_configs[i]; }
0178     SteeringParams &steering_params(int region) { return m_steering_params[region]; }
0179 
0180     bool merge_seed_hits_during_cleaning() const { return m_backward_search && m_backward_drop_seed_hits; }
0181 
0182     // -------- Setup function
0183 
0184     void cloneLayerSteerCore(const IterationConfig &o) {
0185       // Clone common settings for an iteration.
0186       // m_iteration_index, m_track_algorithm, cleaning and bkw-search flags,
0187       // and IterationParams are not copied.
0188 
0189       m_n_regions = o.m_n_regions;
0190       m_region_order = o.m_region_order;
0191       m_steering_params = o.m_steering_params;
0192       m_layer_configs = o.m_layer_configs;
0193 
0194       m_partition_seeds = o.m_partition_seeds;
0195     }
0196 
0197     void set_iteration_index_and_track_algorithm(int idx, int trk_alg) {
0198       m_iteration_index = idx;
0199       m_track_algorithm = trk_alg;
0200     }
0201 
0202     void set_qf_flags() {
0203       m_requires_seed_hit_sorting = true;
0204       m_requires_quality_filter = true;
0205     }
0206 
0207     void set_qf_params(int minHits, float sharedFrac) {
0208       m_params.minHitsQF = minHits;
0209       m_params.fracSharedHits = sharedFrac;
0210     }
0211 
0212     void set_dupclean_flag() { m_requires_dupclean_tight = true; }
0213 
0214     void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward) {
0215       m_params.fracSharedHits = sharedFrac;
0216       m_params.drth_central = drthCentral;
0217       m_params.drth_obarrel = drthObarrel;
0218       m_params.drth_forward = drthForward;
0219     }
0220 
0221     void set_seed_cleaning_params(float pt_thr,
0222                                   float dzmax_bh,
0223                                   float drmax_bh,
0224                                   float dzmax_bl,
0225                                   float drmax_bl,
0226                                   float dzmax_eh,
0227                                   float drmax_eh,
0228                                   float dzmax_el,
0229                                   float drmax_el) {
0230       m_params.c_ptthr_hpt = pt_thr;
0231       m_params.c_drmax_bh = drmax_bh;
0232       m_params.c_dzmax_bh = dzmax_bh;
0233       m_params.c_drmax_eh = drmax_eh;
0234       m_params.c_dzmax_eh = dzmax_eh;
0235       m_params.c_drmax_bl = drmax_bl;
0236       m_params.c_dzmax_bl = dzmax_bl;
0237       m_params.c_drmax_el = drmax_el;
0238       m_params.c_dzmax_el = dzmax_el;
0239     }
0240 
0241     void set_num_regions_layers(int nreg, int nlay) {
0242       m_n_regions = nreg;
0243       m_region_order.resize(nreg);
0244       m_steering_params.resize(nreg);
0245       for (int i = 0; i < nreg; ++i)
0246         m_steering_params[i].m_region = i;
0247       m_layer_configs.resize(nlay);
0248     }
0249   };
0250 
0251   //==============================================================================
0252   // IterationsInfo
0253   //==============================================================================
0254 
0255   class IterationsInfo {
0256   public:
0257     std::vector<IterationConfig> m_iterations;
0258 
0259     IterationsInfo() {}
0260 
0261     void resize(int ni) { m_iterations.resize(ni); }
0262 
0263     int size() const { return m_iterations.size(); }
0264 
0265     IterationConfig &operator[](int i) { return m_iterations[i]; }
0266     const IterationConfig &operator[](int i) const { return m_iterations[i]; }
0267   };
0268 
0269   //==============================================================================
0270 
0271   // IterationConfig instances are created in Geoms/CMS-2017.cc, Create_CMS_2017(),
0272   // filling the IterationsInfo object passed in by reference.
0273 
0274   //==============================================================================
0275   // JSON config interface
0276   //==============================================================================
0277 
0278   class ConfigJsonPatcher {
0279   public:
0280     struct PatchReport {
0281       int n_files = 0;
0282       int n_json_entities = 0;
0283       int n_replacements = 0;
0284 
0285       void inc_counts(int f, int e, int r) {
0286         n_files += f;
0287         n_json_entities += e;
0288         n_replacements += r;
0289       }
0290       void inc_counts(const PatchReport &pr) {
0291         n_files += pr.n_files;
0292         n_json_entities += pr.n_json_entities;
0293         n_replacements += pr.n_replacements;
0294       }
0295       void reset() { n_files = n_json_entities = n_replacements = 0; }
0296     };
0297 
0298   private:
0299     std::unique_ptr<nlohmann::json> m_json;
0300     nlohmann::json *m_current = nullptr;
0301 
0302     // add stack and cd_up() ? also, name stack for exceptions and printouts
0303     std::vector<nlohmann::json *> m_json_stack;
0304     std::vector<std::string> m_path_stack;
0305 
0306     bool m_verbose = false;
0307 
0308     std::string get_abs_path() const;
0309     std::string exc_hdr(const char *func = nullptr) const;
0310 
0311   public:
0312     ConfigJsonPatcher(bool verbose = false);
0313     ~ConfigJsonPatcher();
0314 
0315     template <class T>
0316     void load(const T &o);
0317     template <class T>
0318     void save(T &o);
0319 
0320     void cd(const std::string &path);
0321     void cd_up(const std::string &path = "");
0322     void cd_top(const std::string &path = "");
0323 
0324     template <typename T>
0325     void replace(const std::string &path, T val);
0326 
0327     template <typename T>
0328     void replace(int first, int last, const std::string &path, T val);
0329 
0330     nlohmann::json &get(const std::string &path);
0331 
0332     int replace(const nlohmann::json &j);
0333 
0334     std::string dump(int indent = 2);
0335   };
0336 
0337   class ConfigJson {
0338   public:
0339     ConfigJson(bool verbose = false) : m_verbose(verbose) {}
0340 
0341     // Patch IterationsInfo from a vector of files.
0342     // Assumes patch files include iteration-info preambles, i.e., they
0343     // were saved with include_iter_info_preamble=true.
0344     // If report is non-null counts are added to existing object.
0345     void patch_Files(IterationsInfo &its_info,
0346                      const std::vector<std::string> &fnames,
0347                      ConfigJsonPatcher::PatchReport *report = nullptr);
0348 
0349     // Load a single iteration from JSON file.
0350     // Searches for a match between m_algorithm in its_info and in JSON file to decide
0351     // which IterationConfig it will clone and patch-load the JSON file over.
0352     // The IterationConfig in question *must* match in structure to what is on file,
0353     // in particular, arrays must be of same lengths.
0354     // Assumes JSON file has been saved WITHOUT iteration-info preamble.
0355     // Returns a unique_ptr to the cloned IterationConfig.
0356     // If report is non-null counts are added to existing object.
0357     std::unique_ptr<IterationConfig> patchLoad_File(const IterationsInfo &its_info,
0358                                                     const std::string &fname,
0359                                                     ConfigJsonPatcher::PatchReport *report = nullptr);
0360 
0361     // Load a single iteration from JSON file.
0362     // This leaves IterationConfig data-members that are not registered
0363     // in JSON schema at their default values.
0364     // The only such member is std::function m_partition_seeds.
0365     // Assumes JSON file has been saved WITHOUT iteration-info preamble.
0366     // Returns a unique_ptr to the cloned IterationConfig.
0367     std::unique_ptr<IterationConfig> load_File(const std::string &fname);
0368 
0369     void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble);
0370 
0371     void dump(IterationsInfo &its_info);
0372 
0373     void test_Direct(IterationConfig &it_cfg);
0374     void test_Patcher(IterationConfig &it_cfg);
0375 
0376   private:
0377     bool m_verbose = false;
0378   };
0379 }  // end namespace mkfit
0380 
0381 #endif