CMS 3D CMS Logo

GeometryInterface.h
Go to the documentation of this file.
1 #ifndef SiPixel_GeometryInterface_h
2 #define SiPixel_GeometryInterface_h
3 // -*- C++ -*-
4 //
5 // Package: SiPixelPhase1Common
6 // Class: GeometryInterface
7 //
8 // The histogram manager uses this class to gather information about a sample.
9 // All geometry dependence goes here.
10 //
11 // Original Author: Marcel Schneider
12 //
13 
18 
19 #include <functional>
20 #include <map>
21 #include <string>
22 #include <array>
23 
25 public:
26  // an ID is produced by interning a string name.
27  typedef int ID;
28  // A column could have multiple IDs if it is a or-form.
29  // Not used atm, makes many things much easier.
30  typedef ID Column;
31  typedef double Value;
32  static const Value UNDEFINED;
33 
34  // Essentially a map backed by a vector (for the small counts here
35  // this should always be faster). Most ops turned out to be not needed.
36  typedef std::vector<std::pair<Column, Value>> Values;
37 
39 
40  bool loaded() { return is_loaded; };
41 
42  // The hard work happens here.
43  void load(edm::EventSetup const& iSetup);
44 
46  // in this order the struct should fit 2 64bit words and is cheap to copy.
47  const edm::Event* sourceEvent = nullptr;
49  int16_t col = 0;
50  int16_t row = 0;
51  };
52 
53  // This has to be fast, _should_ not malloc.
54  void extractColumns(std::vector<Column> const& names, InterestingQuantities const& iq, Values& out) {
55  out.clear();
56  for (Column const& col : names) {
57  auto val = extract(col, iq);
58  out.push_back(val);
59  }
60  }
61 
62  // the pair return value is historical; it is only really needed with or-columns.
63  // But it is cleaner to carry it around.
64  std::pair<Column, Value> extract(Column const& col, InterestingQuantities const& iq) {
65  assert(col != 0 || !"Extracting invalid column.");
66  ID id = col;
67  assert(ID(extractors.size()) > id || !"extractors vector too small!");
68  auto& ex = extractors[id];
69  if (!ex) { // we have never heard about this. This is a typo for sure.
70  edm::LogError("GeometryInterface") << "Undefined column used: " << unintern(id) << ". Check your spelling.\n";
71  } else {
72  auto val = ex(iq);
73  if (val != UNDEFINED) {
74  return std::make_pair(Column{id}, val);
75  }
76  }
77  return std::make_pair(col, UNDEFINED);
78  }
79 
80  Value extract(ID id, DetId did, edm::Event* ev = nullptr, int16_t col = 0, int16_t row = 0) {
81  InterestingQuantities iq = {ev, did, col, row};
82  return extractors[id](iq);
83  }
84 
85  // TODO: for Phase0 (and maybe also Phase2) this should include the 4 corners
86  // of each ROC (or the *correct* corners of the respective modules).
87  std::vector<InterestingQuantities> const& allModules() { return all_modules; }
88 
89  Value maxValue(ID id) { return max_value[id]; };
90  Value minValue(ID id) { return min_value[id]; };
91  Value binWidth(ID id) { return bin_width[id]; };
92 
93  // turn string into an ID, adding it if needed.
94  ID intern(std::string const& id) {
95  auto it = ids.find(id);
96  if (it == ids.end()) {
97  ids[id] = ++max_id;
98  extractors.resize(max_id + 1);
99  }
100  return ids[id];
101  };
102 
103  // turn an ID back into a string. Only for pretty output (including histo
104  // labels), so it can be slow (though intern() does not have to be fast
105  // either).
107  for (auto& e : ids)
108  if (e.second == id)
109  return e.first;
110  return "INVALID";
111  }
112 
113  std::string pretty(Column col) { return unintern(col); }
114 
115  std::string formatValue(Column, Value);
116 
117 private:
118  void loadFromTopology(edm::EventSetup const& iSetup, const edm::ParameterSet& iConfig);
119  void loadFromSiPixelCoordinates(edm::EventSetup const& iSetup, const edm::ParameterSet& iConfig);
120  void loadTimebased(edm::EventSetup const& iSetup, const edm::ParameterSet& iConfig);
121  void loadModuleLevel(edm::EventSetup const& iSetup, const edm::ParameterSet& iConfig);
122  void loadFEDCabling(edm::EventSetup const& iSetup, const edm::ParameterSet& iConfig);
123 
125 
126  bool is_loaded = false;
127 
128  // This holds closures that compute the column values in step1.
129  // can be a Vector since ids are dense.
130  std::vector<std::function<Value(InterestingQuantities const& iq)>> extractors;
131  // quantity range if it is known. Can be UNDEFINED, in this case booking will
132  // determine the range. Map for ease of use.
133  std::map<ID, Value> max_value;
134  std::map<ID, Value> min_value;
135  std::map<ID, Value> bin_width;
136 
137  // cache of pre-formatted values. Can be pre-populated while loading
138  // (used for Pixel*Name)
139  std::map<std::pair<Column, Value>, std::string> format_value;
140 
141  void addExtractor(ID id,
143  Value min = UNDEFINED,
144  Value max = UNDEFINED,
145  Value binwidth = 1) {
146  max_value[id] = max;
147  min_value[id] = min;
148  bin_width[id] = binwidth;
149  extractors[id] = func;
150  }
151 
152  std::vector<InterestingQuantities> all_modules;
153 
154  // interning table. Maps string IDs to a dense set of integer IDs
155  std::map<std::string, ID> ids{std::make_pair(std::string("INVALID"), ID(0))};
156  ID max_id = 0;
157 };
158 
159 #endif
std::map< ID, Value > max_value
std::map< std::pair< Column, Value >, std::string > format_value
std::map< std::string, ID > ids
GeometryInterface(const edm::ParameterSet &conf)
std::string pretty(Column col)
void addExtractor(ID id, std::function< Value(InterestingQuantities const &iq)> func, Value min=UNDEFINED, Value max=UNDEFINED, Value binwidth=1)
std::string formatValue(Column, Value)
Value binWidth(ID id)
bool ev
const std::string names[nVars_]
std::string unintern(ID id)
void loadFromTopology(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
Value maxValue(ID id)
void load(edm::EventSetup const &iSetup)
void extractColumns(std::vector< Column > const &names, InterestingQuantities const &iq, Values &out)
const edm::ParameterSet iConfig
std::vector< InterestingQuantities > all_modules
std::map< ID, Value > bin_width
T min(T a, T b)
Definition: MathUtil.h:58
static const Value UNDEFINED
std::vector< std::pair< Column, Value > > Values
Value extract(ID id, DetId did, edm::Event *ev=0, int16_t col=0, int16_t row=0)
Definition: DetId.h:17
void loadFEDCabling(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
Value minValue(ID id)
std::pair< Column, Value > extract(Column const &col, InterestingQuantities const &iq)
void loadModuleLevel(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
std::vector< std::function< Value(InterestingQuantities const &iq)> > extractors
std::vector< InterestingQuantities > const & allModules()
std::map< ID, Value > min_value
ID intern(std::string const &id)
void loadTimebased(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)
void loadFromSiPixelCoordinates(edm::EventSetup const &iSetup, const edm::ParameterSet &iConfig)