Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2024-04-06 12:28:16

0001 #ifndef RecoTracker_MkFitCore_interface_IterationConfig_h
0002 #define RecoTracker_MkFitCore_interface_IterationConfig_h
0003 
0004 #include "RecoTracker/MkFitCore/interface/FunctionTypes.h"
0005 #include "RecoTracker/MkFitCore/interface/SteeringParams.h"
0006 
0007 #include "nlohmann/json_fwd.hpp"
0008 
0009 #include <functional>
0010 
0011 namespace mkfit {
0012 
0013   //==============================================================================
0014   // Hit masks / IterationMaskIfc
0015   //==============================================================================
0016 
0017   struct IterationMaskIfcBase {
0018     virtual ~IterationMaskIfcBase() {}
0019 
0020     virtual const std::vector<bool> *get_mask_for_layer(int layer) const { return nullptr; }
0021   };
0022 
0023   struct IterationMaskIfc : public IterationMaskIfcBase {
0024     std::vector<std::vector<bool>> m_mask_vector;
0025 
0026     const std::vector<bool> *get_mask_for_layer(int layer) const override { return &m_mask_vector[layer]; }
0027   };
0028 
0029   //==============================================================================
0030   // IterationLayerConfig
0031   //==============================================================================
0032 
0033   class IterationConfig;
0034 
0035   class IterationLayerConfig {
0036   public:
0037     int m_layer = -1;
0038     // Selection limits.
0039     float m_select_min_dphi;
0040     float m_select_max_dphi;
0041     float m_select_min_dq;
0042     float m_select_max_dq;
0043 
0044     void set_selection_limits(float p1, float p2, float q1, float q2) {
0045       m_select_min_dphi = p1;
0046       m_select_max_dphi = p2;
0047       m_select_min_dq = q1;
0048       m_select_max_dq = q2;
0049     }
0050 
0051     //----------------------------------------------------------------------------
0052 
0053     float min_dphi() const { return m_select_min_dphi; }
0054     float max_dphi() const { return m_select_max_dphi; }
0055     float min_dq() const { return m_select_min_dq; }
0056     float max_dq() const { return m_select_max_dq; }
0057 
0058     const std::vector<float> &get_window_params(bool forward, bool fallback_to_other) const {
0059       if (fallback_to_other) {
0060         // Empty vector is a valid result, we do not need to check both.
0061         if (forward)
0062           return m_winpars_fwd.empty() ? m_winpars_bkw : m_winpars_fwd;
0063         else
0064           return m_winpars_bkw.empty() ? m_winpars_fwd : m_winpars_bkw;
0065       } else {
0066         return forward ? m_winpars_fwd : m_winpars_bkw;
0067       }
0068     }
0069 
0070     //Hit selection window parameters: 2D fit/layer (72 in phase-1 CMS geometry).
0071     //Used in MkFinder::selectHitIndices().
0072     std::vector<float> m_winpars_fwd;
0073     std::vector<float> m_winpars_bkw;
0074 
0075     //----------------------------------------------------------------------------
0076 
0077     IterationLayerConfig() {}
0078   };
0079 
0080   //==============================================================================
0081   // IterationParams
0082   //==============================================================================
0083 
0084   class IterationParams {
0085   public:
0086     int nlayers_per_seed = 3;
0087     int maxCandsPerSeed = 5;
0088     int maxHolesPerCand = 4;
0089     int maxConsecHoles = 1;
0090     float chi2Cut_min = 15.0;
0091     float chi2CutOverlap = 3.5;
0092     float pTCutOverlap = 0.0;
0093     bool recheckOverlap = false;
0094     bool useHitSelectionV2 = false;
0095 
0096     //quality filter params
0097     int minHitsQF = 4;
0098 
0099     //min pT cut
0100     float minPtCut = 0.0;
0101 
0102     //max cluster size cut for SiStrip hits
0103     unsigned int maxClusterSize = 8;
0104   };
0105 
0106   //==============================================================================
0107   // IterationSeedPartition
0108   //==============================================================================
0109 
0110   class IterationSeedPartition {
0111   public:
0112     using register_seed_phi_eta_foo = void(float, float);
0113 
0114     std::vector<int> m_region;
0115     std::function<register_seed_phi_eta_foo> m_phi_eta_foo;
0116 
0117     IterationSeedPartition(int size) : m_region(size) {}
0118   };
0119 
0120   //==============================================================================
0121   // IterationConfig
0122   //==============================================================================
0123 
0124   class IterationConfig {
0125   public:
0126     int m_iteration_index = -1;
0127     int m_track_algorithm = -1;
0128 
0129     bool m_requires_seed_hit_sorting = false;
0130 
0131     bool m_backward_search = false;
0132     bool m_backward_drop_seed_hits = false;
0133 
0134     int m_backward_fit_min_hits = -1;  // Min number of hits to keep when m_backward_drop_seed_hits is true
0135 
0136     // seed cleaning params with good defaults (all configurable)
0137     float sc_ptthr_hpt = 2.0;
0138     float sc_drmax_bh = 0.010;
0139     float sc_dzmax_bh = 0.005;
0140     float sc_drmax_eh = 0.020;
0141     float sc_dzmax_eh = 0.020;
0142     float sc_drmax_bl = 0.010;
0143     float sc_dzmax_bl = 0.005;
0144     float sc_drmax_el = 0.030;
0145     float sc_dzmax_el = 0.030;
0146 
0147     // duplicate cleaning params with good defaults (all configurable)
0148     float dc_fracSharedHits = 0.19;
0149     float dc_drth_central = 0.001;
0150     float dc_drth_obarrel = 0.001;
0151     float dc_drth_forward = 0.001;
0152 
0153     // Iteration parameters (could be a ptr)
0154     IterationParams m_params;
0155     IterationParams m_backward_params;
0156 
0157     int m_n_regions = -1;
0158     std::vector<int> m_region_order;
0159     std::vector<SteeringParams> m_steering_params;
0160     std::vector<IterationLayerConfig> m_layer_configs;
0161 
0162     // *** Standard functions
0163     // - seed cleaning: called directly from top-level per-iteration steering code.
0164     clean_seeds_func m_seed_cleaner;
0165     // - seed partitioning into eta regions: called from MkBuilder::find_tracks_load_seeds().
0166     partition_seeds_func m_seed_partitioner;
0167     // - candidate filtering: passed to MkBuilder::filter_comb_cands().
0168     filter_candidates_func m_pre_bkfit_filter, m_post_bkfit_filter;
0169     // - duplicate cleaning: called directly from top-level per-iteration steering code.
0170     clean_duplicates_func m_duplicate_cleaner;
0171     // - default track scoring function, can be overriden in SteeringParams for each eta region.
0172     track_score_func m_default_track_scorer;
0173 
0174     // Names for Standard functions that get saved to / loaded from JSON.
0175     std::string m_seed_cleaner_name;
0176     std::string m_seed_partitioner_name;
0177     std::string m_pre_bkfit_filter_name, m_post_bkfit_filter_name;
0178     std::string m_duplicate_cleaner_name;
0179     std::string m_default_track_scorer_name = "default";
0180 
0181     //----------------------------------------------------------------------------
0182 
0183     IterationConfig() {}
0184 
0185     // -------- Getter functions
0186 
0187     IterationLayerConfig &layer(int i) { return m_layer_configs[i]; }
0188     SteeringParams &steering_params(int region) { return m_steering_params[region]; }
0189 
0190     bool merge_seed_hits_during_cleaning() const { return m_backward_search && m_backward_drop_seed_hits; }
0191 
0192     // -------- Setup functions
0193 
0194     void setupStandardFunctionsFromNames();
0195 
0196     void cloneLayerSteerCore(const IterationConfig &o) {
0197       // Clone common settings for an iteration.
0198       // m_iteration_index, m_track_algorithm, cleaning and bkw-search flags,
0199       // and IterationParams are not copied.
0200       // Standard functions are also not copied, only their names so one should
0201       // call setupStandardFunctionsFromNames() later on.
0202 
0203       m_n_regions = o.m_n_regions;
0204       m_region_order = o.m_region_order;
0205       m_steering_params = o.m_steering_params;
0206       m_layer_configs = o.m_layer_configs;
0207 
0208       m_seed_cleaner_name = o.m_seed_cleaner_name;
0209       m_seed_partitioner_name = o.m_seed_partitioner_name;
0210       m_pre_bkfit_filter_name = o.m_pre_bkfit_filter_name;
0211       m_post_bkfit_filter_name = o.m_post_bkfit_filter_name;
0212       m_duplicate_cleaner_name = o.m_duplicate_cleaner_name;
0213       m_default_track_scorer_name = o.m_default_track_scorer_name;
0214     }
0215 
0216     void set_iteration_index_and_track_algorithm(int idx, int trk_alg) {
0217       m_iteration_index = idx;
0218       m_track_algorithm = trk_alg;
0219     }
0220 
0221     void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward) {
0222       dc_fracSharedHits = sharedFrac;
0223       dc_drth_central = drthCentral;
0224       dc_drth_obarrel = drthObarrel;
0225       dc_drth_forward = drthForward;
0226     }
0227 
0228     void set_seed_cleaning_params(float pt_thr,
0229                                   float dzmax_bh,
0230                                   float drmax_bh,
0231                                   float dzmax_bl,
0232                                   float drmax_bl,
0233                                   float dzmax_eh,
0234                                   float drmax_eh,
0235                                   float dzmax_el,
0236                                   float drmax_el) {
0237       sc_ptthr_hpt = pt_thr;
0238       sc_drmax_bh = drmax_bh;
0239       sc_dzmax_bh = dzmax_bh;
0240       sc_drmax_eh = drmax_eh;
0241       sc_dzmax_eh = dzmax_eh;
0242       sc_drmax_bl = drmax_bl;
0243       sc_dzmax_bl = dzmax_bl;
0244       sc_drmax_el = drmax_el;
0245       sc_dzmax_el = dzmax_el;
0246     }
0247 
0248     void set_num_regions_layers(int nreg, int nlay) {
0249       m_n_regions = nreg;
0250       m_region_order.resize(nreg);
0251       m_steering_params.resize(nreg);
0252       for (int i = 0; i < nreg; ++i)
0253         m_steering_params[i].m_region = i;
0254       m_layer_configs.resize(nlay);
0255       for (int i = 0; i < nlay; ++i)
0256         m_layer_configs[i].m_layer = i;
0257     }
0258 
0259     // Catalog of Standard functions
0260     static void register_seed_cleaner(const std::string &name, clean_seeds_func func);
0261     static void register_seed_partitioner(const std::string &name, partition_seeds_func func);
0262     static void register_candidate_filter(const std::string &name, filter_candidates_func func);
0263     static void register_duplicate_cleaner(const std::string &name, clean_duplicates_func func);
0264     static void register_track_scorer(const std::string &name, track_score_func func);
0265 
0266     static clean_seeds_func get_seed_cleaner(const std::string &name);
0267     static partition_seeds_func get_seed_partitioner(const std::string &name);
0268     static filter_candidates_func get_candidate_filter(const std::string &name);
0269     static clean_duplicates_func get_duplicate_cleaner(const std::string &name);
0270     static track_score_func get_track_scorer(const std::string &name);
0271   };
0272 
0273   //==============================================================================
0274   // IterationsInfo
0275   //==============================================================================
0276 
0277   class IterationsInfo {
0278   public:
0279     std::vector<IterationConfig> m_iterations;
0280 
0281     IterationsInfo() {}
0282 
0283     void resize(int ni) { m_iterations.resize(ni); }
0284 
0285     int size() const { return m_iterations.size(); }
0286 
0287     IterationConfig &operator[](int i) { return m_iterations[i]; }
0288     const IterationConfig &operator[](int i) const { return m_iterations[i]; }
0289 
0290     void setupStandardFunctionsFromNames() {
0291       for (auto &i : m_iterations)
0292         i.setupStandardFunctionsFromNames();
0293     }
0294   };
0295 
0296   //==============================================================================
0297 
0298   // IterationConfig instances are created in Geoms/CMS-phase1.cc, Create_CMS_phase1(),
0299   // filling the IterationsInfo object passed in by reference.
0300 
0301   //==============================================================================
0302   // JSON config interface
0303   //==============================================================================
0304 
0305   class ConfigJsonPatcher {
0306   public:
0307     struct PatchReport {
0308       int n_files = 0;
0309       int n_json_entities = 0;
0310       int n_replacements = 0;
0311 
0312       void inc_counts(int f, int e, int r) {
0313         n_files += f;
0314         n_json_entities += e;
0315         n_replacements += r;
0316       }
0317       void inc_counts(const PatchReport &pr) {
0318         n_files += pr.n_files;
0319         n_json_entities += pr.n_json_entities;
0320         n_replacements += pr.n_replacements;
0321       }
0322       void reset() { n_files = n_json_entities = n_replacements = 0; }
0323     };
0324 
0325   private:
0326     std::unique_ptr<nlohmann::json> m_json;
0327     nlohmann::json *m_current = nullptr;
0328 
0329     // add stack and cd_up() ? also, name stack for exceptions and printouts
0330     std::vector<nlohmann::json *> m_json_stack;
0331     std::vector<std::string> m_path_stack;
0332 
0333     bool m_verbose = false;
0334 
0335     std::string get_abs_path() const;
0336     std::string exc_hdr(const char *func = nullptr) const;
0337 
0338   public:
0339     ConfigJsonPatcher(bool verbose = false);
0340     ~ConfigJsonPatcher();
0341 
0342     template <class T>
0343     void load(const T &o);
0344     template <class T>
0345     void save(T &o);
0346 
0347     void cd(const std::string &path);
0348     void cd_up(const std::string &path = "");
0349     void cd_top(const std::string &path = "");
0350 
0351     template <typename T>
0352     void replace(const std::string &path, T val);
0353 
0354     template <typename T>
0355     void replace(int first, int last, const std::string &path, T val);
0356 
0357     nlohmann::json &get(const std::string &path);
0358 
0359     int replace(const nlohmann::json &j);
0360 
0361     std::string dump(int indent = 2);
0362   };
0363 
0364   class ConfigJson {
0365   public:
0366     ConfigJson(bool verbose = false) : m_verbose(verbose) {}
0367 
0368     // Patch IterationsInfo from a vector of files.
0369     // Assumes patch files include iteration-info preambles, i.e., they
0370     // were saved with include_iter_info_preamble=true.
0371     // If report is non-null counts are added to existing object.
0372     void patch_Files(IterationsInfo &its_info,
0373                      const std::vector<std::string> &fnames,
0374                      ConfigJsonPatcher::PatchReport *report = nullptr);
0375 
0376     // Load a single iteration from JSON file.
0377     // Searches for a match between m_algorithm in its_info and in JSON file to decide
0378     // which IterationConfig it will clone and patch-load the JSON file over.
0379     // The IterationConfig in question *must* match in structure to what is on file,
0380     // in particular, arrays must be of same lengths.
0381     // Assumes JSON file has been saved WITHOUT iteration-info preamble.
0382     // Returns a unique_ptr to the cloned IterationConfig.
0383     // If report is non-null counts are added to existing object.
0384     std::unique_ptr<IterationConfig> patchLoad_File(const IterationsInfo &its_info,
0385                                                     const std::string &fname,
0386                                                     ConfigJsonPatcher::PatchReport *report = nullptr);
0387 
0388     // Load a single iteration from JSON file.
0389     // This leaves IterationConfig data-members that are not registered
0390     // in JSON schema at their default values.
0391     // There are several std::function members like this.
0392     // Assumes JSON file has been saved WITHOUT iteration-info preamble.
0393     // Returns a unique_ptr to the cloned IterationConfig.
0394     std::unique_ptr<IterationConfig> load_File(const std::string &fname);
0395 
0396     void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble);
0397 
0398     void dump(IterationsInfo &its_info);
0399 
0400     void test_Direct(IterationConfig &it_cfg);
0401     void test_Patcher(IterationConfig &it_cfg);
0402 
0403   private:
0404     bool m_verbose = false;
0405   };
0406 }  // end namespace mkfit
0407 
0408 #endif