| 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 | #include "na64util/transformations.hh" | ||
| 21 | |||
| 22 | namespace na64dp { | ||
| 23 | namespace calib { | ||
| 24 | |||
| 25 | template<typename T> | ||
| 26 | struct PlacementDefinedTypeTraits { | ||
| 27 | static T create_item(const calib::Placement & pl) { return T(pl); } | ||
| 28 | }; | ||
| 29 | |||
| 30 | template<> | ||
| 31 | struct PlacementDefinedTypeTraits<util::Transformation> { | ||
| 32 | static util::Transformation create_item(const calib::Placement & pl) | ||
| 33 | { return util::transformation_by(pl); } | ||
| 34 | }; | ||
| 35 | |||
| 36 | ///\brief Helper class keeping items defined by placements | ||
| 37 | template<typename T> | ||
| 38 | class PlacementsDefinedCache | ||
| 39 | : public SetupGeometryCache | ||
| 40 | , private std::unordered_map<DetID, T> { | ||
| 41 | protected: | ||
| 42 | /// Caches item induced by detector's placement | ||
| 43 | void _cache_item(DetID did, const calib::Placement & pl ) { | ||
| 44 | const auto obj = PlacementDefinedTypeTraits<T>::create_item(pl); | ||
| 45 | auto ir = this->emplace(did, obj); | ||
| 46 | if(ir.second) { | ||
| 47 | _log << log4cpp::Priority::DEBUG | ||
| 48 | << "Cached entry of item defined by placement " << naming()[did]; | ||
| 49 | } else { | ||
| 50 | ir.first->second = T(obj); | ||
| 51 | _log << log4cpp::Priority::DEBUG | ||
| 52 | << "Replaced entry of item defined by " << naming()[did]; | ||
| 53 | } | ||
| 54 | } | ||
| 55 | public: | ||
| 56 | PlacementsDefinedCache( calib::Dispatcher & cdsp | ||
| 57 | , log4cpp::Category & logCat | ||
| 58 | , const std::string & namingCalibClass="default" | ||
| 59 | , const std::string & placementsCalibClass="default") | ||
| 60 | : SetupGeometryCache(cdsp, logCat, namingCalibClass, placementsCalibClass) | ||
| 61 | {} | ||
| 62 | ///\brief Caches transformation induced by placement | ||
| 63 | /// | ||
| 64 | /// Forwards execution to `_cache_transform_by()`. | ||
| 65 | void handle_single_placement_update(const calib::Placement & pl) override { | ||
| 66 | if(!( ((int) pl.suppInfoType) | ||
| 67 | & ((int) calib::Placement::kDetectorsGeometry) | ||
| 68 | ) | ||
| 69 | ) return; // omit non-detector items | ||
| 70 | _cache_item(naming()[pl.name], pl); | ||
| 71 | } | ||
| 72 | ///\brief Returns transformation induced by detector's placement | ||
| 73 | /// | ||
| 74 | /// Should be previously added by `_cache_transform_by()` | ||
| 75 | ✗ | const T & get_placement_defined_item_for(DetID did) const { | |
| 76 | ✗ | auto it = this->find(did); | |
| 77 | ✗ | if(this->end() == it) { | |
| 78 | ✗ | NA64DP_RUNTIME_ERROR( "No entry cached for %s." | |
| 79 | , this->naming()[did].c_str() ); | ||
| 80 | } | ||
| 81 | ✗ | return it->second; | |
| 82 | } | ||
| 83 | }; | ||
| 84 | |||
| 85 | } // namespace ::na64dp::calib | ||
| 86 | } // namespace na64dp | ||
| 87 | |||
| 88 |