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 
5 
6 #include "nlohmann/json_fwd.hpp"
7 
8 #include <functional>
9 
10 namespace mkfit {
11 
12  struct BeamSpot;
13  class EventOfHits;
14  class TrackerInfo;
15  class Track;
16  class TrackCand;
17  class MkJob;
18 
19  typedef std::vector<Track> TrackVec;
20 
21  //==============================================================================
22  // Hit masks / IterationMaskIfc
23  //==============================================================================
24 
26  virtual ~IterationMaskIfcBase() {}
27 
28  virtual const std::vector<bool> *get_mask_for_layer(int layer) const { return nullptr; }
29  };
30 
32  std::vector<std::vector<bool>> m_mask_vector;
33 
34  const std::vector<bool> *get_mask_for_layer(int layer) const override { return &m_mask_vector[layer]; }
35  };
36 
37  //==============================================================================
38  // IterationLayerConfig
39  //==============================================================================
40 
41  class IterationConfig;
42 
44  public:
45  int m_layer = -1;
46  // Selection limits.
51 
52  void set_selection_limits(float p1, float p2, float q1, float q2) {
55  m_select_min_dq = q1;
56  m_select_max_dq = q2;
57  }
58 
59  //----------------------------------------------------------------------------
60 
61  float min_dphi() const { return m_select_min_dphi; }
62  float max_dphi() const { return m_select_max_dphi; }
63  float min_dq() const { return m_select_min_dq; }
64  float max_dq() const { return m_select_max_dq; }
65 
66  //Hit selection windows: 2D fit/layer (72 in phase-1 CMS geometry)
67  //cut = [0]*1/pT + [1]*std::fabs(theta-pi/2) + [2])
68  float c_dp_sf = 1.1;
69  float c_dp_0 = 0.0;
70  float c_dp_1 = 0.0;
71  float c_dp_2 = 0.0;
72  //
73  float c_dq_sf = 1.1;
74  float c_dq_0 = 0.0;
75  float c_dq_1 = 0.0;
76  float c_dq_2 = 0.0;
77  //
78  float c_c2_sf = 1.1;
79  float c_c2_0 = 0.0;
80  float c_c2_1 = 0.0;
81  float c_c2_2 = 0.0;
82 
83  //----------------------------------------------------------------------------
84 
86  };
87 
88  //==============================================================================
89  // IterationParams
90  //==============================================================================
91 
93  public:
95  int maxCandsPerSeed = 5;
96  int maxHolesPerCand = 4;
97  int maxConsecHoles = 1;
98  float chi2Cut_min = 15.0;
99  float chi2CutOverlap = 3.5;
100  float pTCutOverlap = 0.0;
101 
102  //quality filter params
103  int minHitsQF = 4;
104 
105  //min pT cut
106  float minPtCut = 0.0;
107 
108  //max cluster size cut for SiStrip hits
109  unsigned int maxClusterSize = 8;
110  };
111 
112  //==============================================================================
113  // IterationSeedPartition
114  //==============================================================================
115 
117  public:
118  using register_seed_phi_eta_foo = void(float, float);
119 
120  std::vector<int> m_region;
121  std::function<register_seed_phi_eta_foo> m_phi_eta_foo;
122 
124  };
125 
126  //==============================================================================
127  // IterationConfig
128  //==============================================================================
129 
131  public:
132  // Called directly.
133  using clean_seeds_cf = int(TrackVec &, const IterationConfig &, const BeamSpot &);
134  using clean_seeds_func = std::function<clean_seeds_cf>;
135  // Called from MkBuilder::find_tracks_load_seeds().
136  using partition_seeds_cf = void(const TrackerInfo &,
137  const TrackVec &,
138  const EventOfHits &,
140  using partition_seeds_func = std::function<partition_seeds_cf>;
141  // Passed to MkBuilder::filter_comb_cands().
142  using filter_candidates_cf = bool(const TrackCand &, const MkJob &);
143  using filter_candidates_func = std::function<filter_candidates_cf>;
144  // Called directly.
146  using clean_duplicates_func = std::function<clean_duplicates_cf>;
147 
150 
152 
153  bool m_backward_search = false;
155 
156  int m_backward_fit_min_hits = -1; // Min number of hits to keep when m_backward_drop_seed_hits is true
157 
158  // seed cleaning params with good defaults (all configurable)
159  float sc_ptthr_hpt = 2.0;
160  float sc_drmax_bh = 0.010;
161  float sc_dzmax_bh = 0.005;
162  float sc_drmax_eh = 0.020;
163  float sc_dzmax_eh = 0.020;
164  float sc_drmax_bl = 0.010;
165  float sc_dzmax_bl = 0.005;
166  float sc_drmax_el = 0.030;
167  float sc_dzmax_el = 0.030;
168 
169  // duplicate cleaning params with good defaults (all configurable)
170  float dc_fracSharedHits = 0.19;
171  float dc_drth_central = 0.001;
172  float dc_drth_obarrel = 0.001;
173  float dc_drth_forward = 0.001;
174 
175  // Iteration parameters (could be a ptr)
178 
179  int m_n_regions = -1;
180  std::vector<int> m_region_order;
181  std::vector<SteeringParams> m_steering_params;
182  std::vector<IterationLayerConfig> m_layer_configs;
183 
184  // Standard functions
189 
190  // Names for Standard functions that get saved to / loaded from JSON.
195 
196  //----------------------------------------------------------------------------
197 
199 
200  // -------- Getter functions
201 
204 
206 
207  // -------- Setup functions
208 
210 
212  // Clone common settings for an iteration.
213  // m_iteration_index, m_track_algorithm, cleaning and bkw-search flags,
214  // and IterationParams are not copied.
215  // Standard functions are also not copied, only their names so one should
216  // call setupStandardFunctionsFromNames() later on.
217 
218  m_n_regions = o.m_n_regions;
219  m_region_order = o.m_region_order;
220  m_steering_params = o.m_steering_params;
221  m_layer_configs = o.m_layer_configs;
222 
223  m_seed_cleaner_name = o.m_seed_cleaner_name;
224  m_seed_partitioner_name = o.m_seed_partitioner_name;
225  m_pre_bkfit_filter_name = o.m_pre_bkfit_filter_name;
226  m_post_bkfit_filter_name = o.m_post_bkfit_filter_name;
227  m_duplicate_cleaner_name = o.m_duplicate_cleaner_name;
228  }
229 
232  m_track_algorithm = trk_alg;
233  }
234 
235  void set_dupl_params(float sharedFrac, float drthCentral, float drthObarrel, float drthForward) {
237  dc_drth_central = drthCentral;
238  dc_drth_obarrel = drthObarrel;
239  dc_drth_forward = drthForward;
240  }
241 
242  void set_seed_cleaning_params(float pt_thr,
243  float dzmax_bh,
244  float drmax_bh,
245  float dzmax_bl,
246  float drmax_bl,
247  float dzmax_eh,
248  float drmax_eh,
249  float dzmax_el,
250  float drmax_el) {
251  sc_ptthr_hpt = pt_thr;
252  sc_drmax_bh = drmax_bh;
253  sc_dzmax_bh = dzmax_bh;
254  sc_drmax_eh = drmax_eh;
255  sc_dzmax_eh = dzmax_eh;
256  sc_drmax_bl = drmax_bl;
257  sc_dzmax_bl = dzmax_bl;
258  sc_drmax_el = drmax_el;
259  sc_dzmax_el = dzmax_el;
260  }
261 
262  void set_num_regions_layers(int nreg, int nlay) {
263  m_n_regions = nreg;
264  m_region_order.resize(nreg);
265  m_steering_params.resize(nreg);
266  for (int i = 0; i < nreg; ++i)
267  m_steering_params[i].m_region = i;
268  m_layer_configs.resize(nlay);
269  for (int i = 0; i < nlay; ++i)
270  m_layer_configs[i].m_layer = i;
271  }
272 
273  // Catalog of Standard functions
278 
283  };
284 
285  //==============================================================================
286  // IterationsInfo
287  //==============================================================================
288 
290  public:
291  std::vector<IterationConfig> m_iterations;
292 
294 
295  void resize(int ni) { m_iterations.resize(ni); }
296 
297  int size() const { return m_iterations.size(); }
298 
300  const IterationConfig &operator[](int i) const { return m_iterations[i]; }
301 
303  for (auto &i : m_iterations)
304  i.setupStandardFunctionsFromNames();
305  }
306  };
307 
308  //==============================================================================
309 
310  // IterationConfig instances are created in Geoms/CMS-phase1.cc, Create_CMS_phase1(),
311  // filling the IterationsInfo object passed in by reference.
312 
313  //==============================================================================
314  // JSON config interface
315  //==============================================================================
316 
318  public:
319  struct PatchReport {
320  int n_files = 0;
322  int n_replacements = 0;
323 
324  void inc_counts(int f, int e, int r) {
325  n_files += f;
326  n_json_entities += e;
327  n_replacements += r;
328  }
329  void inc_counts(const PatchReport &pr) {
330  n_files += pr.n_files;
331  n_json_entities += pr.n_json_entities;
332  n_replacements += pr.n_replacements;
333  }
335  };
336 
337  private:
338  std::unique_ptr<nlohmann::json> m_json;
340 
341  // add stack and cd_up() ? also, name stack for exceptions and printouts
342  std::vector<nlohmann::json *> m_json_stack;
343  std::vector<std::string> m_path_stack;
344 
345  bool m_verbose = false;
346 
347  std::string get_abs_path() const;
348  std::string exc_hdr(const char *func = nullptr) const;
349 
350  public:
351  ConfigJsonPatcher(bool verbose = false);
353 
354  template <class T>
355  void load(const T &o);
356  template <class T>
357  void save(T &o);
358 
359  void cd(const std::string &path);
360  void cd_up(const std::string &path = "");
361  void cd_top(const std::string &path = "");
362 
363  template <typename T>
364  void replace(const std::string &path, T val);
365 
366  template <typename T>
367  void replace(int first, int last, const std::string &path, T val);
368 
369  nlohmann::json &get(const std::string &path);
370 
371  int replace(const nlohmann::json &j);
372 
373  std::string dump(int indent = 2);
374  };
375 
376  class ConfigJson {
377  public:
378  ConfigJson(bool verbose = false) : m_verbose(verbose) {}
379 
380  // Patch IterationsInfo from a vector of files.
381  // Assumes patch files include iteration-info preambles, i.e., they
382  // were saved with include_iter_info_preamble=true.
383  // If report is non-null counts are added to existing object.
384  void patch_Files(IterationsInfo &its_info,
385  const std::vector<std::string> &fnames,
387 
388  // Load a single iteration from JSON file.
389  // Searches for a match between m_algorithm in its_info and in JSON file to decide
390  // which IterationConfig it will clone and patch-load the JSON file over.
391  // The IterationConfig in question *must* match in structure to what is on file,
392  // in particular, arrays must be of same lengths.
393  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
394  // Returns a unique_ptr to the cloned IterationConfig.
395  // If report is non-null counts are added to existing object.
396  std::unique_ptr<IterationConfig> patchLoad_File(const IterationsInfo &its_info,
397  const std::string &fname,
399 
400  // Load a single iteration from JSON file.
401  // This leaves IterationConfig data-members that are not registered
402  // in JSON schema at their default values.
403  // There are several std::function members like this.
404  // Assumes JSON file has been saved WITHOUT iteration-info preamble.
405  // Returns a unique_ptr to the cloned IterationConfig.
406  std::unique_ptr<IterationConfig> load_File(const std::string &fname);
407 
408  void save_Iterations(IterationsInfo &its_info, const std::string &fname_fmt, bool include_iter_info_preamble);
409 
410  void dump(IterationsInfo &its_info);
411 
412  void test_Direct(IterationConfig &it_cfg);
413  void test_Patcher(IterationConfig &it_cfg);
414 
415  private:
416  bool m_verbose = false;
417  };
418 } // end namespace mkfit
419 
420 #endif
size
Write out results.
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)
static partition_seeds_func get_seed_partitioner(const std::string &name)
static clean_duplicates_func get_duplicate_cleaner(const std::string &name)
IterationParams m_backward_params
std::function< register_seed_phi_eta_foo > m_phi_eta_foo
std::function< partition_seeds_cf > partition_seeds_func
static filter_candidates_func get_candidate_filter(const std::string &name)
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::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)
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::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 clean_seeds_func get_seed_cleaner(const std::string &name)
partition_seeds_func m_seed_partitioner
int(TrackVec &, const IterationConfig &, const BeamSpot &) clean_seeds_cf
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(TrackVec &, const IterationConfig &) clean_duplicates_cf
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)
void test_Patcher(IterationConfig &it_cfg)
filter_candidates_func m_post_bkfit_filter
std::vector< Track > TrackVec
void(const TrackerInfo &, const TrackVec &, const EventOfHits &, IterationSeedPartition &) partition_seeds_cf
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)
bool(const TrackCand &, const MkJob &) filter_candidates_cf
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="")
IterationLayerConfig & layer(int i)
ConfigJsonPatcher(bool verbose=false)
std::function< clean_seeds_cf > clean_seeds_func
std::vector< std::vector< bool > > m_mask_vector
std::function< clean_duplicates_cf > clean_duplicates_func
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
virtual const std::vector< bool > * get_mask_for_layer(int layer) const
std::function< filter_candidates_cf > filter_candidates_func
std::string m_duplicate_cleaner_name
static void register_candidate_filter(const std::string &name, filter_candidates_func func)
std::string get_abs_path() const