File indexing completed on 2024-04-06 12:08:25
0001 #ifndef SiPixel_GeometryInterface_h
0002 #define SiPixel_GeometryInterface_h
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #include "DataFormats/DetId/interface/DetId.h"
0015 #include "FWCore/ParameterSet/interface/ParameterSet.h"
0016 #include "FWCore/Framework/interface/ConsumesCollector.h"
0017 #include "FWCore/Framework/interface/Event.h"
0018 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0019
0020 #include "FWCore/Utilities/interface/ESGetToken.h"
0021 #include "FWCore/Utilities/interface/Transition.h"
0022 #include "CondFormats/SiPixelObjects/interface/SiPixelFedCablingMap.h"
0023 #include "CondFormats/DataRecord/interface/SiPixelFedCablingMapRcd.h"
0024 #include "Geometry/TrackerGeometryBuilder/interface/TrackerGeometry.h"
0025 #include "Geometry/Records/interface/TrackerDigiGeometryRecord.h"
0026 #include "DataFormats/TrackerCommon/interface/TrackerTopology.h"
0027 #include "Geometry/Records/interface/TrackerTopologyRcd.h"
0028
0029 #include <functional>
0030 #include <map>
0031 #include <string>
0032 #include <array>
0033
0034 class GeometryInterface {
0035 public:
0036
0037 typedef int ID;
0038
0039
0040 typedef ID Column;
0041 typedef double Value;
0042 static const Value UNDEFINED;
0043
0044
0045
0046 typedef std::vector<std::pair<Column, Value>> Values;
0047
0048 GeometryInterface(const edm::ParameterSet&,
0049 edm::ConsumesCollector&&,
0050 edm::Transition transition = edm::Transition::BeginRun);
0051
0052 bool loaded() { return is_loaded; };
0053
0054
0055 void load(edm::EventSetup const& iSetup);
0056
0057 struct InterestingQuantities {
0058
0059 const edm::Event* sourceEvent = nullptr;
0060 DetId sourceModule;
0061 int16_t col = 0;
0062 int16_t row = 0;
0063 };
0064
0065
0066 void extractColumns(std::vector<Column> const& names, InterestingQuantities const& iq, Values& out) {
0067 out.clear();
0068 for (Column const& col : names) {
0069 auto val = extract(col, iq);
0070 out.push_back(val);
0071 }
0072 }
0073
0074
0075
0076 std::pair<Column, Value> extract(Column const& col, InterestingQuantities const& iq) {
0077 assert(col != 0 || !"Extracting invalid column.");
0078 ID id = col;
0079 assert(ID(extractors.size()) > id || !"extractors vector too small!");
0080 auto& ex = extractors[id];
0081 if (!ex) {
0082 edm::LogError("GeometryInterface") << "Undefined column used: " << unintern(id) << ". Check your spelling.\n";
0083 } else {
0084 auto val = ex(iq);
0085 if (val != UNDEFINED) {
0086 return std::make_pair(Column{id}, val);
0087 }
0088 }
0089 return std::make_pair(col, UNDEFINED);
0090 }
0091
0092 Value extract(ID id, DetId did, edm::Event* ev = nullptr, int16_t col = 0, int16_t row = 0) {
0093 InterestingQuantities iq = {ev, did, col, row};
0094 return extractors[id](iq);
0095 }
0096
0097
0098
0099 std::vector<InterestingQuantities> const& allModules() { return all_modules; }
0100
0101 Value maxValue(ID id) { return max_value[id]; };
0102 Value minValue(ID id) { return min_value[id]; };
0103 Value binWidth(ID id) { return bin_width[id]; };
0104
0105
0106 ID intern(std::string const& id) {
0107 auto it = ids.find(id);
0108 if (it == ids.end()) {
0109 ids[id] = ++max_id;
0110 extractors.resize(max_id + 1);
0111 }
0112 return ids[id];
0113 };
0114
0115
0116
0117
0118 std::string unintern(ID id) {
0119 for (auto& e : ids)
0120 if (e.second == id)
0121 return e.first;
0122 return "INVALID";
0123 }
0124
0125 std::string pretty(Column col) { return unintern(col); }
0126
0127 std::string formatValue(Column, Value);
0128
0129 private:
0130 void loadFromTopology(const TrackerGeometry&, const TrackerTopology&, const edm::ParameterSet&);
0131 void loadFromSiPixelCoordinates(const TrackerGeometry&,
0132 const TrackerTopology&,
0133 const SiPixelFedCablingMap&,
0134 const edm::ParameterSet&);
0135 void loadTimebased(const edm::ParameterSet& iConfig);
0136 void loadModuleLevel(const edm::ParameterSet& iConfig);
0137 void loadFEDCabling(const SiPixelFedCablingMap*);
0138
0139 const edm::ParameterSet iConfig;
0140
0141 edm::ESGetToken<TrackerGeometry, TrackerDigiGeometryRecord> trackerGeometryToken_;
0142 edm::ESGetToken<TrackerTopology, TrackerTopologyRcd> trackerTopologyToken_;
0143
0144
0145
0146
0147
0148
0149
0150 edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> siPixelFedCablingMapToken_;
0151 edm::ESGetToken<SiPixelFedCablingMap, SiPixelFedCablingMapRcd> labeledSiPixelFedCablingMapToken_;
0152 std::string cablingMapLabel_;
0153
0154 bool is_loaded = false;
0155
0156
0157
0158 std::vector<std::function<Value(InterestingQuantities const& iq)>> extractors;
0159
0160
0161 std::map<ID, Value> max_value;
0162 std::map<ID, Value> min_value;
0163 std::map<ID, Value> bin_width;
0164
0165
0166
0167 std::map<std::pair<Column, Value>, std::string> format_value;
0168
0169 void addExtractor(ID id,
0170 std::function<Value(InterestingQuantities const& iq)> func,
0171 Value min = UNDEFINED,
0172 Value max = UNDEFINED,
0173 Value binwidth = 1) {
0174 max_value[id] = max;
0175 min_value[id] = min;
0176 bin_width[id] = binwidth;
0177 extractors[id] = func;
0178 }
0179
0180 std::vector<InterestingQuantities> all_modules;
0181
0182
0183 std::map<std::string, ID> ids{std::make_pair(std::string("INVALID"), ID(0))};
0184 ID max_id = 0;
0185 };
0186
0187 #endif