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 
94  //quality filter params
95  int minHitsQF = 4;
96 
97  //min pT cut
98  float minPtCut = 0.0;
99 
100  //max cluster size cut for SiStrip hits
101  unsigned int maxClusterSize = 8;
102  };
103 
104  //==============================================================================
105  // IterationSeedPartition
106  //==============================================================================
107 
109  public:
110  using register_seed_phi_eta_foo = void(float, float);
111 
112  std::vector<int> m_region;
113  std::function<register_seed_phi_eta_foo> m_phi_eta_foo;
114 
115  IterationSeedPartition(int size) : m_region(size) {}
116  };
117 
118  //==============================================================================
119  // IterationConfig
120  //==============================================================================
121 
123  public:
126 
128 
129  bool m_backward_search = false;
131 
132  int m_backward_fit_min_hits = -1; // Min number of hits to keep when m_backward_drop_seed_hits is true
133 
134  // seed cleaning params with good defaults (all configurable)
135  float sc_ptthr_hpt = 2.0;
136  float sc_drmax_bh = 0.010;
137  float sc_dzmax_bh = 0.005;
138  float sc_drmax_eh = 0.020;
139  float sc_dzmax_eh = 0.020;
140  float sc_drmax_bl = 0.010;
141  float sc_dzmax_bl = 0.005;
142  float sc_drmax_el = 0.030;
143  float sc_dzmax_el = 0.030;
144 
145  // duplicate cleaning params with good defaults (all configurable)
146  float dc_fracSharedHits = 0.19;
147  float dc_drth_central = 0.001;
148  float dc_drth_obarrel = 0.001;
149  float dc_drth_forward = 0.001;
150 
151  // Iteration parameters (could be a ptr)
154 
155  int m_n_regions = -1;
156  std::vector<int> m_region_order;
157  std::vector<SteeringParams> m_steering_params;
158  std::vector<IterationLayerConfig> m_layer_configs;
159 
160  // *** Standard functions
161  // - seed cleaning: called directly from top-level per-iteration steering code.
163  // - seed partitioning into eta regions: called from MkBuilder::find_tracks_load_seeds().
165  // - candidate filtering: passed to MkBuilder::filter_comb_cands().
167  // - duplicate cleaning: called directly from top-level per-iteration steering code.
169  // - default track scoring function, can be overriden in SteeringParams for each eta region.
171 
172  // Names for Standard functions that get saved to / loaded from JSON.
178 
179  //----------------------------------------------------------------------------
180 
182 
183  // -------- Getter functions
184 
187 
189 
190  // -------- Setup functions
191 
193 
195  // Clone common settings for an iteration.
196  // m_iteration_index, m_track_algorithm, cleaning and bkw-search flags,
197  // and IterationParams are not copied.
198  // Standard functions are also not copied, only their names so one should
199  // call setupStandardFunctionsFromNames() later on.
200 
201  m_n_regions = o.m_n_regions;
202  m_region_order = o.m_region_order;
203  m_steering_params = o.m_steering_params;
204  m_layer_configs = o.m_layer_configs;
205 
206  m_seed_cleaner_name = o.m_seed_cleaner_name;
207  m_seed_partitioner_name = o.m_seed_partitioner_name;
208  m_pre_bkfit_filter_name = o.m_pre_bkfit_filter_name;
209  m_post_bkfit_filter_name = o.m_post_bkfit_filter_name;
210  m_duplicate_cleaner_name = o.m_duplicate_cleaner_name;
211  m_default_track_scorer_name = o.m_default_track_scorer_name;
212  }
213 
216  m_track_algorithm = trk_alg;
217  }
218 
219  void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward) {
221  dc_drth_central = drthCentral;
222  dc_drth_obarrel = drthObarrel;
223  dc_drth_forward = drthForward;
224  }
225 
226  void set_seed_cleaning_params(float pt_thr,
227  float dzmax_bh,
228  float drmax_bh,
229  float dzmax_bl,
230  float drmax_bl,
231  float dzmax_eh,
232  float drmax_eh,
233  float dzmax_el,
234  float drmax_el) {
235  sc_ptthr_hpt = pt_thr;
236  sc_drmax_bh = drmax_bh;
237  sc_dzmax_bh = dzmax_bh;
238  sc_drmax_eh = drmax_eh;
239  sc_dzmax_eh = dzmax_eh;
240  sc_drmax_bl = drmax_bl;
241  sc_dzmax_bl = dzmax_bl;
242  sc_drmax_el = drmax_el;
243  sc_dzmax_el = dzmax_el;
244  }
245 
246  void set_num_regions_layers(int nreg, int nlay) {
247  m_n_regions = nreg;
248  m_region_order.resize(nreg);
249  m_steering_params.resize(nreg);
250  for (int i = 0; i < nreg; ++i)
251  m_steering_params[i].m_region = i;
252  m_layer_configs.resize(nlay);
253  for (int i = 0; i < nlay; ++i)
254  m_layer_configs[i].m_layer = i;
255  }
256 
257  // Catalog of Standard functions
263 
269  };
270 
271  //==============================================================================
272  // IterationsInfo
273  //==============================================================================
274 
276  public:
277  std::vector<IterationConfig> m_iterations;
278 
280 
281  void resize(int ni) { m_iterations.resize(ni); }
282 
283  int size() const { return m_iterations.size(); }
284 
286  const IterationConfig &operator[](int i) const { return m_iterations[i]; }
287 
289  for (auto &i : m_iterations)
290  i.setupStandardFunctionsFromNames();
291  }
292  };
293 
294  //==============================================================================
295 
296  // IterationConfig instances are created in Geoms/CMS-phase1.cc, Create_CMS_phase1(),
297  // filling the IterationsInfo object passed in by reference.
298 
299  //==============================================================================
300  // JSON config interface
301  //==============================================================================
302 
304  public:
305  struct PatchReport {
306  int n_files = 0;
308  int n_replacements = 0;
309 
310  void inc_counts(int f, int e, int r) {
311  n_files += f;
312  n_json_entities += e;
313  n_replacements += r;
314  }
315  void inc_counts(const PatchReport &pr) {
316  n_files += pr.n_files;
317  n_json_entities += pr.n_json_entities;
318  n_replacements += pr.n_replacements;
319  }
321  };
322 
323  private:
324  std::unique_ptr<nlohmann::json> m_json;
326 
327  // add stack and cd_up() ? also, name stack for exceptions and printouts
328  std::vector<nlohmann::json *> m_json_stack;
329  std::vector<std::string> m_path_stack;
330 
331  bool m_verbose = false;
332 
333  std::string get_abs_path() const;
334  std::string exc_hdr(const char *func = nullptr) const;
335 
336  public:
337  ConfigJsonPatcher(bool verbose = false);
339 
340  template <class T>
341  void load(const T &o);
342  template <class T>
343  void save(T &o);
344 
345  void cd(const std::string &path);
346  void cd_up(const std::string &path = "");
347  void cd_top(const std::string &path = "");
348 
349  template <typename T>
350  void replace(const std::string &path, T val);
351 
352  template <typename T>
353  void replace(int first, int last, const std::string &path, T val);
354 
355  nlohmann::json &get(const std::string &path);
356 
357  int replace(const nlohmann::json &j);
358 
359  std::string dump(int indent = 2);
360  };
361 
362  class ConfigJson {
363  public:
364  ConfigJson(bool verbose = false) : m_verbose(verbose) {}
365 
366  // Patch IterationsInfo from a vector of files.
367  // Assumes patch files include iteration-info preambles, i.e., they
368  // were saved with include_iter_info_preamble=true.
369  // If report is non-null counts are added to existing object.
370  void patch_Files(IterationsInfo &its_info,
371  const std::vector<std::string> &fnames,
373 
374  // Load a single iteration from JSON file.
375  // Searches for a match between m_algorithm in its_info and in JSON file to decide
376  // which IterationConfig it will clone and patch-load the JSON file over.
377  // The IterationConfig in question *must* match in structure to what is on file,
378  // in particular, arrays must be of same lengths.
379  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
380  // Returns a unique_ptr to the cloned IterationConfig.
381  // If report is non-null counts are added to existing object.
382  std::unique_ptr<IterationConfig> patchLoad_File(const IterationsInfo &its_info,
383  const std::string &fname,
385 
386  // Load a single iteration from JSON file.
387  // This leaves IterationConfig data-members that are not registered
388  // in JSON schema at their default values.
389  // There are several std::function members like this.
390  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
391  // Returns a unique_ptr to the cloned IterationConfig.
392  std::unique_ptr<IterationConfig> load_File(const std::string &fname);
393 
394  void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble);
395 
396  void dump(IterationsInfo &its_info);
397 
398  void test_Direct(IterationConfig &it_cfg);
399  void test_Patcher(IterationConfig &it_cfg);
400 
401  private:
402  bool m_verbose = false;
403  };
404 } // end namespace mkfit
405 
406 #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)
nlohmann::json json
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
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