CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups 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.
10 //
11 // Original Author: Marcel Schneider
12 //
13 
19 
28 
29 #include <functional>
30 #include <map>
31 #include <string>
32 #include <array>
33 
35 public:
36  // an ID is produced by interning a string name.
37  typedef int ID;
38  // A column could have multiple IDs if it is a or-form.
39  // Not used atm, makes many things much easier.
40  typedef ID Column;
41  typedef double Value;
42  static const Value UNDEFINED;
43 
44  // Essentially a map backed by a vector (for the small counts here
45  // this should always be faster). Most ops turned out to be not needed.
46  typedef std::vector<std::pair<Column, Value>> Values;
47 
51 
52  bool loaded() { return is_loaded; };
53 
54  // The hard work happens here.
55  void load(edm::EventSetup const& iSetup);
56 
58  // in this order the struct should fit 2 64bit words and is cheap to copy.
59  const edm::Event* sourceEvent = nullptr;
61  int16_t col = 0;
62  int16_t row = 0;
63  };
64 
65  // This has to be fast, _should_ not malloc.
66  void extractColumns(std::vector<Column> const& names, InterestingQuantities const& iq, Values& out) {
67  out.clear();
68  for (Column const& col : names) {
69  auto val = extract(col, iq);
70  out.push_back(val);
71  }
72  }
73 
74  // the pair return value is historical; it is only really needed with or-columns.
75  // But it is cleaner to carry it around.
76  std::pair<Column, Value> extract(Column const& col, InterestingQuantities const& iq) {
77  assert(col != 0 || !"Extracting invalid column.");
78  ID id = col;
79  assert(ID(extractors.size()) > id || !"extractors vector too small!");
80  auto& ex = extractors[id];
81  if (!ex) { // we have never heard about this. This is a typo for sure.
82  edm::LogError("GeometryInterface") << "Undefined column used: " << unintern(id) << ". Check your spelling.\n";
83  } else {
84  auto val = ex(iq);
85  if (val != UNDEFINED) {
86  return std::make_pair(Column{id}, val);
87  }
88  }
89  return std::make_pair(col, UNDEFINED);
90  }
91 
92  Value extract(ID id, DetId did, edm::Event* ev = nullptr, int16_t col = 0, int16_t row = 0) {
93  InterestingQuantities iq = {ev, did, col, row};
94  return extractors[id](iq);
95  }
96 
97  // TODO: for Phase0 (and maybe also Phase2) this should include the 4 corners
98  // of each ROC (or the *correct* corners of the respective modules).
99  std::vector<InterestingQuantities> const& allModules() { return all_modules; }
100 
101  Value maxValue(ID id) { return max_value[id]; };
102  Value minValue(ID id) { return min_value[id]; };
103  Value binWidth(ID id) { return bin_width[id]; };
104 
105  // turn string into an ID, adding it if needed.
106  ID intern(std::string const& id) {
107  auto it = ids.find(id);
108  if (it == ids.end()) {
109  ids[id] = ++max_id;
110  extractors.resize(max_id + 1);
111  }
112  return ids[id];
113  };
114 
115  // turn an ID back into a string. Only for pretty output (including histo
116  // labels), so it can be slow (though intern() does not have to be fast
117  // either).
119  for (auto& e : ids)
120  if (e.second == id)
121  return e.first;
122  return "INVALID";
123  }
124 
126 
128 
129 private:
132  const TrackerTopology&,
133  const SiPixelFedCablingMap&,
134  const edm::ParameterSet&);
138 
140 
143  // When converting this module to use esConsumes I preserved the previous behavior.
144  // In one place a configurable label is allowed when getting SiPixelFedCablingMap
145  // and in another place it always assumes an empty label. I am not sure if this is
146  // actually correct (seems strange). Maybe it does not matter as in all the configurations
147  // I could find in the repository the parameter was an empty string... An expert who
148  // understands this might want to fix this or eliminate this comment if the behavior is
149  // correct.
153 
154  bool is_loaded = false;
155 
156  // This holds closures that compute the column values in step1.
157  // can be a Vector since ids are dense.
158  std::vector<std::function<Value(InterestingQuantities const& iq)>> extractors;
159  // quantity range if it is known. Can be UNDEFINED, in this case booking will
160  // determine the range. Map for ease of use.
161  std::map<ID, Value> max_value;
162  std::map<ID, Value> min_value;
163  std::map<ID, Value> bin_width;
164 
165  // cache of pre-formatted values. Can be pre-populated while loading
166  // (used for Pixel*Name)
167  std::map<std::pair<Column, Value>, std::string> format_value;
168 
169  void addExtractor(ID id,
171  Value min = UNDEFINED,
172  Value max = UNDEFINED,
173  Value binwidth = 1) {
174  max_value[id] = max;
175  min_value[id] = min;
176  bin_width[id] = binwidth;
177  extractors[id] = func;
178  }
179 
180  std::vector<InterestingQuantities> all_modules;
181 
182  // interning table. Maps string IDs to a dense set of integer IDs
183  std::map<std::string, ID> ids{std::make_pair(std::string("INVALID"), ID(0))};
184  ID max_id = 0;
185 };
186 
187 #endif
std::map< ID, Value > max_value
std::string cablingMapLabel_
std::map< std::pair< Column, Value >, std::string > format_value
GeometryInterface(const edm::ParameterSet &, edm::ConsumesCollector &&, edm::Transition transition=edm::Transition::BeginRun)
std::map< std::string, ID > ids
uint16_t *__restrict__ id
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)
uint32_t T const *__restrict__ uint32_t const *__restrict__ int32_t int Histo::index_type cudaStream_t Func __host__ __device__ V int Func func
bool ev
Log< level::Error, false > LogError
assert(be >=bs)
const std::string names[nVars_]
std::string unintern(ID id)
void loadFromTopology(const TrackerGeometry &, const TrackerTopology &, const edm::ParameterSet &)
edm::ESGetToken< TrackerTopology, TrackerTopologyRcd > trackerTopologyToken_
void loadModuleLevel(const edm::ParameterSet &iConfig)
void load(edm::EventSetup const &iSetup)
void extractColumns(std::vector< Column > const &names, InterestingQuantities const &iq, Values &out)
string function
Definition: callgraph.py:50
void loadFromSiPixelCoordinates(const TrackerGeometry &, const TrackerTopology &, const SiPixelFedCablingMap &, const edm::ParameterSet &)
const edm::ParameterSet iConfig
std::vector< InterestingQuantities > all_modules
Transition
Definition: Transition.h:12
std::map< ID, Value > bin_width
edm::ESGetToken< SiPixelFedCablingMap, SiPixelFedCablingMapRcd > labeledSiPixelFedCablingMapToken_
T min(T a, T b)
Definition: MathUtil.h:58
static const Value UNDEFINED
void loadFEDCabling(const SiPixelFedCablingMap *)
std::vector< std::pair< Column, Value > > Values
Definition: DetId.h:17
Value extract(ID id, DetId did, edm::Event *ev=nullptr, int16_t col=0, int16_t row=0)
edm::ESGetToken< TrackerGeometry, TrackerDigiGeometryRecord > trackerGeometryToken_
edm::ESGetToken< SiPixelFedCablingMap, SiPixelFedCablingMapRcd > siPixelFedCablingMapToken_
std::pair< Column, Value > extract(Column const &col, InterestingQuantities const &iq)
std::vector< std::function< Value(InterestingQuantities const &iq)> > extractors
std::vector< InterestingQuantities > const & allModules()
void loadTimebased(const edm::ParameterSet &iConfig)
std::map< ID, Value > min_value
int col
Definition: cuy.py:1009
ID intern(std::string const &id)