CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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. This is a singleton, (ed::Service) but
10 // only for performance reasons.
11 // At some point we might need to switch to sth. more complicated (if we want
12 // to deal with more than one geometry per process).
13 //
14 // Original Author: Marcel Schneider
15 //
16 
21 
22 #include <functional>
23 #include <map>
24 #include <string>
25 #include <array>
26 
28  public:
29  // an ID is produced by interning a string name.
30  typedef int ID;
31  // A column could have multiple IDs if it is a or-form.
32  // Not used atm, makes many things much easier.
33  typedef ID Column;
34  typedef double Value;
35  static const Value UNDEFINED;
36 
37  // Essentially a map backed by a vector (for the small counts here
38  // this should always be faster). Most ops turned out to be not needed.
39  typedef std::vector<std::pair<Column, Value>> Values;
40 
42 
43  bool loaded() { return is_loaded; };
44 
45  // The hard work happens here.
46  // this is _not_ thread save, but it should only be called in
47  // booking/harvesting.
48  void load(edm::EventSetup const& iSetup);
49 
51  // in this order the struct should fit 2 64bit words and is cheap to copy.
54  int16_t col;
55  int16_t row;
56  };
57 
58  // This has to be fast, _should_ not malloc.
59  void extractColumns(std::vector<Column> const& names,
60  InterestingQuantities const& iq, Values& out) {
61  out.clear();
62  for (Column const& col : names) {
63  auto val = extract(col, iq);
64  out.push_back(val);
65  }
66  }
67 
68  // the pair return value is historical; it is only really needed with or-columns.
69  // But it is cleaner to carry it around.
70  std::pair<Column, Value> extract(Column const& col, InterestingQuantities const& iq) {
71  assert(col != 0 || !"Extracting invalid column.");
72  ID id = col;
73  assert(ID(extractors.size()) > id || !"extractors vector too small!");
74  auto& ex = extractors[id];
75  if (!ex) { // we have never heard about this. This is a typo for sure.
76  edm::LogError("GeometryInterface")
77  << "Undefined column used: " << unintern(id)
78  << ". Check your spelling.\n";
79  } else {
80  auto val = ex(iq);
81  if (val != UNDEFINED) {
82  return std::make_pair(Column{id}, val); // double braces for g++
83  }
84  }
85  return std::make_pair(col, UNDEFINED);
86  }
87 
88  Value extract(ID id, DetId did, edm::Event* ev = nullptr, int16_t col = 0,
89  int16_t row = 0) {
90  InterestingQuantities iq = {ev, did, col, row};
91  return extractors[id](iq);
92  }
93 
94  std::vector<InterestingQuantities> const& allModules() {
95  return all_modules;
96  }
97 
98  Value maxValue(ID id) { return max_value[id]; };
99  Value minValue(ID id) { return min_value[id]; };
100  Value binWidth(ID id) { return bin_width[id]; };
101 
102  // turn string into an ID, adding it if needed.
103  // needs the lock since this will be called from the spec builder, which will
104  // run in the constructor and this is parallel.
105  ID intern(std::string const& id) {
106  auto it = ids.find(id);
107  if (it == ids.end()) {
108  ids[id] = ++max_id;
109  extractors.resize(max_id + 1);
110  }
111  return ids[id];
112  };
113 
114  // turn an ID back into a string. Only for pretty output (including histo
115  // labels), so it can be slow (though intern() does not have to be fast
116  // either). Also locks, might not be needed but better save than sorry.
118  for (auto& e : ids)
119  if (e.second == id) return e.first;
120  return "INVALID";
121  }
122 
124  return unintern(col);
125  }
126 
128 
129  private:
130  // void loadFromAlignment(edm::EventSetup const& iSetup, const
131  // edm::ParameterSet& iConfig);
132  void loadFromTopology(edm::EventSetup const& iSetup,
133  const edm::ParameterSet& iConfig);
134  void loadFromSiPixelCoordinates(edm::EventSetup const& iSetup,
135  const edm::ParameterSet& iConfig);
136  void loadTimebased(edm::EventSetup const& iSetup,
137  const edm::ParameterSet& iConfig);
138  void loadModuleLevel(edm::EventSetup const& iSetup,
139  const edm::ParameterSet& iConfig);
140  void loadFEDCabling(edm::EventSetup const& iSetup,
141  const edm::ParameterSet& iConfig);
142 
144 
145  bool is_loaded = false;
146 
147  // This holds closures that compute the column values in step1.
148  // can be a Vector since ids are dense.
149  std::vector<std::function<Value(InterestingQuantities const& iq)>> extractors;
150  // quantity range if it is known. Can be UNDEFINED, in this case booking will
151  // determine the range.
152  // map for ease of use.
153  std::map<ID, Value> max_value;
154  std::map<ID, Value> min_value;
155  std::map<ID, Value> bin_width;
156 
157  // cache of pre-formatted values. Can be pre-populated whhile loading
158  // (used for Pixel*Name)
159  std::map<std::pair<Column, Value>, std::string> format_value;
160 
161  void addExtractor(ID id,
163  Value min = UNDEFINED, Value max = UNDEFINED, Value binwidth = 1) {
164  max_value[id] = max;
165  min_value[id] = min;
166  bin_width[id] = binwidth;
167  extractors[id] = func;
168  }
169 
170  std::vector<InterestingQuantities> all_modules;
171 
172  // interning table. Maps string IDs to a dense set of integer IDs
173  std::map<std::string, ID> ids{std::make_pair(std::string("INVALID"), ID(0))};
174  ID max_id = 0;
175 };
176 
177 #endif
std::map< ID, Value > max_value
std::map< std::pair< Column, Value >, std::string > format_value
std::map< std::string, ID > ids
static const HistoName names[]
GeometryInterface(const edm::ParameterSet &conf)
std::string pretty(Column col)
assert(m_qm.get())
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)
bool ev
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)
helper::RootFunctionHelper< F, args >::root_function function(F &f)
Definition: rootFunction.h:14
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:18
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
int col
Definition: cuy.py:1008
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)