CMS 3D CMS Logo

IterationConfig.h
Go to the documentation of this file.
1 #ifndef RecoTracker_MkFitCore_interface_IterationConfig_h
2 #define RecoTracker_MkFitCore_interface_IterationConfig_h
3 
6 
7 #include "nlohmann/json_fwd.hpp"
8 
9 #include <functional>
10 
11 namespace mkfit {
12 
13  //==============================================================================
14  // Hit masks / IterationMaskIfc
15  //==============================================================================
16 
18  virtual ~IterationMaskIfcBase() {}
19 
20  virtual const std::vector<bool> *get_mask_for_layer(int layer) const { return nullptr; }
21  };
22 
24  std::vector<std::vector<bool>> m_mask_vector;
25 
26  const std::vector<bool> *get_mask_for_layer(int layer) const override { return &m_mask_vector[layer]; }
27  };
28 
29  //==============================================================================
30  // IterationLayerConfig
31  //==============================================================================
32 
33  class IterationConfig;
34 
36  public:
37  int m_layer = -1;
38  // Selection limits.
43 
44  void set_selection_limits(float p1, float p2, float q1, float q2) {
47  m_select_min_dq = q1;
48  m_select_max_dq = q2;
49  }
50 
51  //----------------------------------------------------------------------------
52 
53  float min_dphi() const { return m_select_min_dphi; }
54  float max_dphi() const { return m_select_max_dphi; }
55  float min_dq() const { return m_select_min_dq; }
56  float max_dq() const { return m_select_max_dq; }
57 
58  const std::vector<float> &get_window_params(bool forward, bool fallback_to_other) const {
59  if (fallback_to_other) {
60  // Empty vector is a valid result, we do not need to check both.
61  if (forward)
62  return m_winpars_fwd.empty() ? m_winpars_bkw : m_winpars_fwd;
63  else
64  return m_winpars_bkw.empty() ? m_winpars_fwd : m_winpars_bkw;
65  } else {
66  return forward ? m_winpars_fwd : m_winpars_bkw;
67  }
68  }
69 
70  //Hit selection window parameters: 2D fit/layer (72 in phase-1 CMS geometry).
71  //Used in MkFinder::selectHitIndices().
72  std::vector<float> m_winpars_fwd;
73  std::vector<float> m_winpars_bkw;
74 
75  //----------------------------------------------------------------------------
76 
78  };
79 
80  //==============================================================================
81  // IterationParams
82  //==============================================================================
83 
85  public:
87  int maxCandsPerSeed = 5;
88  int maxHolesPerCand = 4;
89  int maxConsecHoles = 1;
90  float chi2Cut_min = 15.0;
91  float chi2CutOverlap = 3.5;
92  float pTCutOverlap = 0.0;
93  bool recheckOverlap = false;
94  bool useHitSelectionV2 = false;
95 
96  //quality filter params
97  int minHitsQF = 4;
98 
99  //min pT cut
100  float minPtCut = 0.0;
101 
102  //max cluster size cut for SiStrip hits
103  unsigned int maxClusterSize = 8;
104  };
105 
106  //==============================================================================
107  // IterationSeedPartition
108  //==============================================================================
109 
111  public:
112  using register_seed_phi_eta_foo = void(float, float);
113 
114  std::vector<int> m_region;
115  std::function<register_seed_phi_eta_foo> m_phi_eta_foo;
116 
117  IterationSeedPartition(int size) : m_region(size) {}
118  };
119 
120  //==============================================================================
121  // IterationConfig
122  //==============================================================================
123 
125  public:
128 
130 
131  bool m_backward_search = false;
133 
134  int m_backward_fit_min_hits = -1; // Min number of hits to keep when m_backward_drop_seed_hits is true
135 
136  // seed cleaning params with good defaults (all configurable)
137  float sc_ptthr_hpt = 2.0;
138  float sc_drmax_bh = 0.010;
139  float sc_dzmax_bh = 0.005;
140  float sc_drmax_eh = 0.020;
141  float sc_dzmax_eh = 0.020;
142  float sc_drmax_bl = 0.010;
143  float sc_dzmax_bl = 0.005;
144  float sc_drmax_el = 0.030;
145  float sc_dzmax_el = 0.030;
146 
147  // duplicate cleaning params with good defaults (all configurable)
148  float dc_fracSharedHits = 0.19;
149  float dc_drth_central = 0.001;
150  float dc_drth_obarrel = 0.001;
151  float dc_drth_forward = 0.001;
152 
153  // Iteration parameters (could be a ptr)
156 
157  int m_n_regions = -1;
158  std::vector<int> m_region_order;
159  std::vector<SteeringParams> m_steering_params;
160  std::vector<IterationLayerConfig> m_layer_configs;
161 
162  // *** Standard functions
163  // - seed cleaning: called directly from top-level per-iteration steering code.
165  // - seed partitioning into eta regions: called from MkBuilder::find_tracks_load_seeds().
167  // - candidate filtering: passed to MkBuilder::filter_comb_cands().
169  // - duplicate cleaning: called directly from top-level per-iteration steering code.
171  // - default track scoring function, can be overriden in SteeringParams for each eta region.
173 
174  // Names for Standard functions that get saved to / loaded from JSON.
180 
181  //----------------------------------------------------------------------------
182 
184 
185  // -------- Getter functions
186 
189 
191 
192  // -------- Setup functions
193 
195 
197  // Clone common settings for an iteration.
198  // m_iteration_index, m_track_algorithm, cleaning and bkw-search flags,
199  // and IterationParams are not copied.
200  // Standard functions are also not copied, only their names so one should
201  // call setupStandardFunctionsFromNames() later on.
202 
203  m_n_regions = o.m_n_regions;
204  m_region_order = o.m_region_order;
205  m_steering_params = o.m_steering_params;
206  m_layer_configs = o.m_layer_configs;
207 
208  m_seed_cleaner_name = o.m_seed_cleaner_name;
209  m_seed_partitioner_name = o.m_seed_partitioner_name;
210  m_pre_bkfit_filter_name = o.m_pre_bkfit_filter_name;
211  m_post_bkfit_filter_name = o.m_post_bkfit_filter_name;
212  m_duplicate_cleaner_name = o.m_duplicate_cleaner_name;
213  m_default_track_scorer_name = o.m_default_track_scorer_name;
214  }
215 
218  m_track_algorithm = trk_alg;
219  }
220 
221  void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward) {
223  dc_drth_central = drthCentral;
224  dc_drth_obarrel = drthObarrel;
225  dc_drth_forward = drthForward;
226  }
227 
228  void set_seed_cleaning_params(float pt_thr,
229  float dzmax_bh,
230  float drmax_bh,
231  float dzmax_bl,
232  float drmax_bl,
233  float dzmax_eh,
234  float drmax_eh,
235  float dzmax_el,
236  float drmax_el) {
237  sc_ptthr_hpt = pt_thr;
238  sc_drmax_bh = drmax_bh;
239  sc_dzmax_bh = dzmax_bh;
240  sc_drmax_eh = drmax_eh;
241  sc_dzmax_eh = dzmax_eh;
242  sc_drmax_bl = drmax_bl;
243  sc_dzmax_bl = dzmax_bl;
244  sc_drmax_el = drmax_el;
245  sc_dzmax_el = dzmax_el;
246  }
247 
248  void set_num_regions_layers(int nreg, int nlay) {
249  m_n_regions = nreg;
250  m_region_order.resize(nreg);
251  m_steering_params.resize(nreg);
252  for (int i = 0; i < nreg; ++i)
253  m_steering_params[i].m_region = i;
254  m_layer_configs.resize(nlay);
255  for (int i = 0; i < nlay; ++i)
256  m_layer_configs[i].m_layer = i;
257  }
258 
259  // Catalog of Standard functions
265 
271  };
272 
273  //==============================================================================
274  // IterationsInfo
275  //==============================================================================
276 
278  public:
279  std::vector<IterationConfig> m_iterations;
280 
282 
283  void resize(int ni) { m_iterations.resize(ni); }
284 
285  int size() const { return m_iterations.size(); }
286 
288  const IterationConfig &operator[](int i) const { return m_iterations[i]; }
289 
291  for (auto &i : m_iterations)
292  i.setupStandardFunctionsFromNames();
293  }
294  };
295 
296  //==============================================================================
297 
298  // IterationConfig instances are created in Geoms/CMS-phase1.cc, Create_CMS_phase1(),
299  // filling the IterationsInfo object passed in by reference.
300 
301  //==============================================================================
302  // JSON config interface
303  //==============================================================================
304 
306  public:
307  struct PatchReport {
308  int n_files = 0;
310  int n_replacements = 0;
311 
312  void inc_counts(int f, int e, int r) {
313  n_files += f;
314  n_json_entities += e;
315  n_replacements += r;
316  }
317  void inc_counts(const PatchReport &pr) {
318  n_files += pr.n_files;
319  n_json_entities += pr.n_json_entities;
320  n_replacements += pr.n_replacements;
321  }
323  };
324 
325  private:
326  std::unique_ptr<nlohmann::json> m_json;
328 
329  // add stack and cd_up() ? also, name stack for exceptions and printouts
330  std::vector<nlohmann::json *> m_json_stack;
331  std::vector<std::string> m_path_stack;
332 
333  bool m_verbose = false;
334 
335  std::string get_abs_path() const;
336  std::string exc_hdr(const char *func = nullptr) const;
337 
338  public:
339  ConfigJsonPatcher(bool verbose = false);
341 
342  template <class T>
343  void load(const T &o);
344  template <class T>
345  void save(T &o);
346 
347  void cd(const std::string &path);
348  void cd_up(const std::string &path = "");
349  void cd_top(const std::string &path = "");
350 
351  template <typename T>
352  void replace(const std::string &path, T val);
353 
354  template <typename T>
355  void replace(int first, int last, const std::string &path, T val);
356 
357  nlohmann::json &get(const std::string &path);
358 
359  int replace(const nlohmann::json &j);
360 
361  std::string dump(int indent = 2);
362  };
363 
364  class ConfigJson {
365  public:
366  ConfigJson(bool verbose = false) : m_verbose(verbose) {}
367 
368  // Patch IterationsInfo from a vector of files.
369  // Assumes patch files include iteration-info preambles, i.e., they
370  // were saved with include_iter_info_preamble=true.
371  // If report is non-null counts are added to existing object.
372  void patch_Files(IterationsInfo &its_info,
373  const std::vector<std::string> &fnames,
375 
376  // Load a single iteration from JSON file.
377  // Searches for a match between m_algorithm in its_info and in JSON file to decide
378  // which IterationConfig it will clone and patch-load the JSON file over.
379  // The IterationConfig in question *must* match in structure to what is on file,
380  // in particular, arrays must be of same lengths.
381  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
382  // Returns a unique_ptr to the cloned IterationConfig.
383  // If report is non-null counts are added to existing object.
384  std::unique_ptr<IterationConfig> patchLoad_File(const IterationsInfo &its_info,
385  const std::string &fname,
387 
388  // Load a single iteration from JSON file.
389  // This leaves IterationConfig data-members that are not registered
390  // in JSON schema at their default values.
391  // There are several std::function members like this.
392  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
393  // Returns a unique_ptr to the cloned IterationConfig.
394  std::unique_ptr<IterationConfig> load_File(const std::string &fname);
395 
396  void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble);
397 
398  void dump(IterationsInfo &its_info);
399 
400  void test_Direct(IterationConfig &it_cfg);
401  void test_Patcher(IterationConfig &it_cfg);
402 
403  private:
404  bool m_verbose = false;
405  };
406 } // end namespace mkfit
407 
408 #endif
nlohmann::json * m_current
IterationParams m_params
std::string m_pre_bkfit_filter_name
static void register_seed_partitioner(const std::string &name, partition_seeds_func func)
IterationParams m_backward_params
std::function< register_seed_phi_eta_foo > m_phi_eta_foo
static void register_track_scorer(const std::string &name, track_score_func func)
SteeringParams & steering_params(int region)
bool verbose
void inc_counts(const PatchReport &pr)
void set_seed_cleaning_params(float pt_thr, float dzmax_bh, float drmax_bh, float dzmax_bl, float drmax_bl, float dzmax_eh, float drmax_eh, float dzmax_el, float drmax_el)
std::string dump(int indent=2)
std::string m_seed_cleaner_name
std::string m_seed_partitioner_name
static void register_duplicate_cleaner(const std::string &name, clean_duplicates_func func)
void inc_counts(int f, int e, int r)
void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward)
std::function< track_score_cf > track_score_func
Definition: FunctionTypes.h:40
std::vector< int > m_region_order
clean_seeds_func m_seed_cleaner
TEMPL(T2) struct Divides void
Definition: Factorize.h:24
nlohmann::json json
void replace(const std::string &path, T val)
static filter_candidates_func get_candidate_filter(const std::string &name)
void test_Direct(IterationConfig &it_cfg)
void patch_Files(IterationsInfo &its_info, const std::vector< std::string > &fnames, ConfigJsonPatcher::PatchReport *report=nullptr)
std::string exc_hdr(const char *func=nullptr) const
std::function< filter_candidates_cf > filter_candidates_func
Definition: FunctionTypes.h:28
std::vector< nlohmann::json * > m_json_stack
std::vector< IterationLayerConfig > m_layer_configs
static void register_seed_cleaner(const std::string &name, clean_seeds_func func)
void setupStandardFunctionsFromNames()
void set_selection_limits(float p1, float p2, float q1, float q2)
const std::vector< bool > * get_mask_for_layer(int layer) const override
static partition_seeds_func get_seed_partitioner(const std::string &name)
std::string m_default_track_scorer_name
partition_seeds_func m_seed_partitioner
std::unique_ptr< IterationConfig > patchLoad_File(const IterationsInfo &its_info, const std::string &fname, ConfigJsonPatcher::PatchReport *report=nullptr)
double f[11][100]
std::string m_post_bkfit_filter_name
void cd(const std::string &path)
void set_iteration_index_and_track_algorithm(int idx, int trk_alg)
std::vector< std::string > m_path_stack
void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble)
std::vector< float > m_winpars_bkw
void test_Patcher(IterationConfig &it_cfg)
filter_candidates_func m_post_bkfit_filter
static track_score_func get_track_scorer(const std::string &name)
static clean_duplicates_func get_duplicate_cleaner(const std::string &name)
bool merge_seed_hits_during_cleaning() const
void cloneLayerSteerCore(const IterationConfig &o)
clean_duplicates_func m_duplicate_cleaner
std::unique_ptr< IterationConfig > load_File(const std::string &fname)
const IterationConfig & operator[](int i) const
IterationConfig & operator[](int i)
void dump(IterationsInfo &its_info)
void(float, float) register_seed_phi_eta_foo
void set_num_regions_layers(int nreg, int nlay)
string fname
main script
std::unique_ptr< nlohmann::json > m_json
void cd_up(const std::string &path="")
const std::vector< float > & get_window_params(bool forward, bool fallback_to_other) const
IterationLayerConfig & layer(int i)
ConfigJsonPatcher(bool verbose=false)
track_score_func m_default_track_scorer
std::vector< float > m_winpars_fwd
std::vector< std::vector< bool > > m_mask_vector
std::function< clean_seeds_cf > clean_seeds_func
Definition: FunctionTypes.h:22
static clean_seeds_func get_seed_cleaner(const std::string &name)
filter_candidates_func m_pre_bkfit_filter
void cd_top(const std::string &path="")
std::vector< IterationConfig > m_iterations
long double T
ConfigJson(bool verbose=false)
std::vector< SteeringParams > m_steering_params
std::function< clean_duplicates_cf > clean_duplicates_func
Definition: FunctionTypes.h:31
virtual const std::vector< bool > * get_mask_for_layer(int layer) const
std::string m_duplicate_cleaner_name
static void register_candidate_filter(const std::string &name, filter_candidates_func func)
std::function< partition_seeds_cf > partition_seeds_func
Definition: FunctionTypes.h:25
std::string get_abs_path() const