| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* This file is a part of NA64SW software. | ||
| 2 | * Copyright (C) 2015-2023 NA64 Collaboration, CERN | ||
| 3 | * | ||
| 4 | * NA64SW is free software: you can redistribute it and/or modify | ||
| 5 | * it under the terms of the GNU General Public License as published by | ||
| 6 | * the Free Software Foundation, either version 3 of the License, or | ||
| 7 | * (at your option) any later version. | ||
| 8 | * | ||
| 9 | * This program is distributed in the hope that it will be useful, | ||
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | * GNU General Public License for more details. | ||
| 13 | * | ||
| 14 | * You should have received a copy of the GNU General Public License | ||
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. */ | ||
| 16 | |||
| 17 | #pragma once | ||
| 18 | |||
| 19 | #include "setupGeoCache.hh" | ||
| 20 | |||
| 21 | namespace na64dp { | ||
| 22 | namespace calib { | ||
| 23 | |||
| 24 | ///\brief Defines spatial ordering of the detectos | ||
| 25 | /// | ||
| 26 | /// Can accept pointer to external selector instance to selectively | ||
| 27 | /// include/exclude certain placements (does not maintain its lifetime). | ||
| 28 | /// Items passed validation then become subject of sorting algorithm. | ||
| 29 | /// | ||
| 30 | /// To cope with calib info update procedure order numbers will not be | ||
| 31 | /// calculated until order is requested first time | ||
| 32 | /// | ||
| 33 | /// \todo Placements invalidation. | ||
| 34 | template<typename OrderedT> | ||
| 35 | class AbstractSpatialDetectorsOrder : public SetupGeometryCache { | ||
| 36 | protected: | ||
| 37 | /// Ptrs to external selectors (can be empty) | ||
| 38 | std::vector<const std::pair<std::string, DetSelect *> *> _selectorPtrs; | ||
| 39 | /// List of permitted zones (empty means all) | ||
| 40 | std::unordered_set<ZoneID_t> _permittedZones; | ||
| 41 | private: | ||
| 42 | /// To-be sorted cache, filled by `handle_update()` for placements | ||
| 43 | std::unordered_map<DetID, Placement> _placementsByID; | ||
| 44 | /// Station ID to layer number mapping (CATSC uses orderly numbers) | ||
| 45 | mutable OrderedT _ordered; | ||
| 46 | /// Number of unique items (can differ from `_ordered` size) | ||
| 47 | mutable size_t _nSortedGroups; | ||
| 48 | /// Cache validation flag | ||
| 49 | mutable bool _cacheValid; | ||
| 50 | protected: | ||
| 51 | /// Recomputes layer numbering cache | ||
| 52 | void _recache() const { | ||
| 53 | _ordered = _sort_detectors(_placementsByID, _nSortedGroups); | ||
| 54 | _cacheValid = true; | ||
| 55 | } | ||
| 56 | /// Subclasses shall define this method for particular sort | ||
| 57 | virtual OrderedT _sort_detectors(const std::unordered_map<DetID, Placement> &, size_t &) const = 0; | ||
| 58 | public: | ||
| 59 | AbstractSpatialDetectorsOrder( calib::Manager & cmgr | ||
| 60 | , log4cpp::Category & logCat | ||
| 61 | , const std::string & namingCalibClass="default" | ||
| 62 | , const std::string & placementsCalibClass="default" | ||
| 63 | ) : SetupGeometryCache(cmgr, logCat, namingCalibClass, placementsCalibClass) | ||
| 64 | , _cacheValid(false) | ||
| 65 | { | ||
| 66 | cmgr.subscribe<calib::Placements>(*this, placementsCalibClass); | ||
| 67 | } | ||
| 68 | /// Returns ptr to selector with which this order was ctrd | ||
| 69 | const std::vector<const std::pair<std::string, DetSelect *> *> & | ||
| 70 | det_selector() const { return _selectorPtrs; } | ||
| 71 | /// Puts placement into internal container, invalidates the cache | ||
| 72 | ✗ | void handle_single_placement_update(const calib::Placement & pl) override { | |
| 73 | ✗ | if(!(((int) calib::Placement::kDetectorsGeometry) & ((int) pl.suppInfoType))) return; | |
| 74 | ✗ | _cacheValid = false; | |
| 75 | ✗ | DetID did = naming()[pl.name]; | |
| 76 | ✗ | _placementsByID[did] = pl; | |
| 77 | } | ||
| 78 | /// Re-caches if need and returns ordered detector entries | ||
| 79 | const OrderedT & spatial_ordered_detectors() const { | ||
| 80 | if(!_cacheValid) _recache(); | ||
| 81 | assert(_cacheValid); | ||
| 82 | return _ordered; | ||
| 83 | } | ||
| 84 | size_t n_sorted_groups() const { | ||
| 85 | if(!_cacheValid) _recache(); | ||
| 86 | assert(_cacheValid); | ||
| 87 | return _nSortedGroups; | ||
| 88 | } | ||
| 89 | |||
| 90 | void additional_selection_criterion(const std::pair<std::string, DetSelect *> & sel) { | ||
| 91 | _cacheValid = false; | ||
| 92 | _selectorPtrs.push_back(&sel); | ||
| 93 | } | ||
| 94 | |||
| 95 | void set_zone_numbers(const std::vector<ZoneID_t> & zoneNums) { | ||
| 96 | _cacheValid = false; | ||
| 97 | _permittedZones = std::unordered_set<ZoneID_t>( | ||
| 98 | zoneNums.begin(), zoneNums.end()); | ||
| 99 | } | ||
| 100 | }; | ||
| 101 | |||
| 102 | |||
| 103 | ///\brief Common sorting case for tracking -- stations ordered by z | ||
| 104 | /// | ||
| 105 | /// It is intentionally designed to map layer's and stations keys into same | ||
| 106 | /// orderly number | ||
| 107 | class OrderedStations | ||
| 108 | : public AbstractSpatialDetectorsOrder< std::map<DetID, unsigned int> > { | ||
| 109 | private: | ||
| 110 | float (*_callback)(DetID, const Placement &); | ||
| 111 | protected: | ||
| 112 | mutable size_t _nStations; | ||
| 113 | std::map<DetID, unsigned int> _sort_detectors( | ||
| 114 | const std::unordered_map<DetID, Placement> &, size_t & ) const override; | ||
| 115 | public: | ||
| 116 | OrderedStations( calib::Manager & cmgr | ||
| 117 | , log4cpp::Category & logCat | ||
| 118 | , float (*cllb)(DetID, const Placement &) | ||
| 119 | , const std::string & namingCalibClass="default" | ||
| 120 | , const std::string & placementsCalibClass="default" | ||
| 121 | ) : AbstractSpatialDetectorsOrder( cmgr, logCat | ||
| 122 | , namingCalibClass, placementsCalibClass ) | ||
| 123 | , _callback(cllb) { assert(_callback); } | ||
| 124 | static float z_center(DetID, const Placement &); | ||
| 125 | // ... other orsers? | ||
| 126 | }; | ||
| 127 | |||
| 128 | } // namespace ::na64dp::calib | ||
| 129 | } // namespace na64dp | ||
| 130 |