Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2023-01-23 02:42:43

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